Files
2026-11-19 04:54:17 -06:00

321 lines
12 KiB
Plaintext

#! @file notes.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains how the checker reports what is wrong: at a node of the file being
#! checked, or of another, with the instance of a generic it was found in and
#! a related place where there is one. A note holds a copy of its text, made
#! once the note is admitted, so text made for a note that is left out, as one
#! made where what is checked is quiet, is let go of with what made it.
module sakura::analyzer;
#! Reports what is wrong at the node of the file being checked.
#! @param this The checker.
#! @param node The node.
#! @param code What kind of mistake it is.
#! @param detail What the note says.
pub def checker::report(&mut this, node: u32, code: ec, detail: strview): void {
this.report_in(this.at.file, node, code, detail);
}
#! Reports what is wrong at the node, with text made for it.
#! @param this The checker.
#! @param node The node.
#! @param code What kind of mistake it is.
#! @param text What the note says, which the note copies.
pub def checker::report_text(&mut this, node: u32, code: ec, text: &string): void {
this.report_in(this.at.file, node, code, text.span);
}
#! Reports what is wrong at the node of the file.
#! @param this The checker.
#! @param file The file.
#! @param node The node.
#! @param code What kind of mistake it is.
#! @param detail What the note says.
pub def checker::report_in(&mut this, file: u32, node: u32, code: ec, detail: strview): void {
this.report_place(file, node, code, detail, 0, 0, 0, "");
}
#! Reports what is wrong at the declaration, in its own file, wherever checking
#! is.
#! @param this The checker.
#! @param id The declaration.
#! @param code What kind of mistake it is.
#! @param detail What the note says.
pub def checker::report_decl(&mut this, id: u32, code: ec, detail: strview): void {
this.report_in(this.decls.files.p[id], this.decls.nodes.p[id], code, detail);
}
#! Reports what is wrong at the node of the file, and a place it relates to,
#! where it says something of one: to whatever hears the checker's notes, or in
#! its notes.
#! @param this The checker.
#! @param file The file.
#! @param node The node.
#! @param code What kind of mistake it is.
#! @param detail What the note says.
#! @param related_file The file of the place it relates to.
#! @param related_offset Where that place begins.
#! @param related_length How many bytes it covers.
#! @param related What the note says of it, or empty where it relates
#! to none.
def checker::report_place(
&mut this,
file: u32,
node: u32,
code: ec,
detail: strview,
related_file: u32,
related_offset: u32,
related_length: u32,
related: strview
): void {
return if this.quiet != 0;
# A body checked once for each case it holds in says each note once, which is
# held until it ends.
val key := ((file as u64) << 40) | ((node as u64) << 8) | (code as u64);
if this.repeating != 0 {
for &mut each in this.held_notes {
continue if each.key != key;
if each.serial != this.case_serial {
each.serial = this.case_serial;
each.passes++;
}
return;
}
}
this.failed = true;
this.errors++;
return if !this.sink.admits();
val syntax := &this.units[file].syntax;
val token := syntax.node_tokens[node];
var made := note {
file = file,
offset = syntax.token_offsets[token],
length = syntax.token_length(token, this.names),
code = code,
detail = string::from(detail, nothing {}),
instance = this.instance_text(),
related_file = related_file,
related_offset = related_offset,
related_length = related_length,
related = string::from(related, nothing {}),
origin = this.at.decl
};
if related.length == 0 {
this.relate_instance(&mut made);
}
if this.repeating != 0 {
this.held_notes.append(checker::held_note {
key = key, serial = this.case_serial, passes = 1, made = move(&mut made), text = string::copy_of(&this.case_text)
});
return;
}
this.sink.give(&mut made);
}
#! Relates a note made in an instance to where what is written made it, where
#! it relates to nothing else.
#! @param this The checker.
#! @param made The note.
def checker::relate_instance(&mut this, made: &mut note): void {
return if this.at.instance == 0;
val at := this.instance_made(this.at.instance);
return if at.node == 0;
val syntax := &this.units[at.file].syntax;
val token := this.made_token(at);
made.related_file = at.file;
made.related_offset = syntax.token_offsets[token];
made.related_length = syntax.token_length(token, this.names);
made.related = string::from("The instance is made here.", nothing {});
}
#! Warns of what is likely wrong at the node of the file being checked, which
#! never stops a program, nor marks what is checked as failed.
#! @param this The checker.
#! @param node The node.
#! @param code What kind of mistake it is about.
#! @param detail What the note says.
pub def checker::warn(&mut this, node: u32, code: ec, detail: strview): void {
this.warn_place(node, code, detail, 0);
}
#! Warns of what is likely wrong at the node, with text made for it, and the
#! node declaring what it is about, where one is given.
#! @param this The checker.
#! @param node The node.
#! @param code What kind of mistake it is about.
#! @param text What the note says, which the note copies.
#! @param related The node declaring what it is about, in the same file, or 0.
pub def checker::warn_text(&mut this, node: u32, code: ec, text: &string, related: u32): void {
this.warn_place(node, code, text.span, related);
}
#! Warns of what is likely wrong at the node: to whatever hears the checker's
#! notes, or in its notes.
#! @param this The checker.
#! @param node The node.
#! @param code What kind of mistake it is about.
#! @param detail What the note says.
#! @param related The node declaring what it is about, in the same file, or 0.
def checker::warn_place(&mut this, node: u32, code: ec, detail: strview, related: u32): void {
return if this.quiet != 0 || !this.sink.admits();
val file := this.at.file;
val syntax := &this.units[file].syntax;
val token := syntax.node_tokens[node];
var made := note {
file = file,
offset = syntax.token_offsets[token],
length = syntax.token_length(token, this.names),
code = code,
detail = string::from(detail, nothing {}),
warning = true,
instance = this.instance_text(),
origin = this.at.decl
};
if related != 0 {
val other := syntax.node_tokens[related];
made.related_file = file;
made.related_offset = syntax.token_offsets[other];
made.related_length = syntax.token_length(other, this.names);
made.related = string::from("It is declared here.", nothing {});
} else {
this.relate_instance(&mut made);
}
this.sink.give(&mut made);
}
#! Reports what is wrong at the node, and the place of another it relates to,
#! as where a name was first declared.
#! @param this The checker.
#! @param node The node.
#! @param code What kind of mistake it is.
#! @param detail What the note says.
#! @param file The file of the other node.
#! @param other The other node.
#! @param related What the note says of it.
pub def checker::report_related(
&mut this,
node: u32,
code: ec,
detail: strview,
file: u32,
other: u32,
related: strview
): void {
this.report_related_in(this.at.file, node, code, detail, file, other, related);
}
#! Reports what is wrong at the declaration, in its own file, wherever checking
#! is, and the place of another declaration it relates to, as where the name
#! was first declared.
#! @param this The checker.
#! @param id The declaration.
#! @param code What kind of mistake it is.
#! @param detail What the note says.
#! @param other The other declaration.
#! @param related What the note says of it.
pub def checker::report_decl_related(&mut this, id: u32, code: ec, detail: strview, other: u32, related: strview): void {
this.report_related_in(this.decls.files.p[id], this.decls.nodes.p[id], code, detail,
this.decls.files.p[other], this.decls.nodes.p[other], related);
}
#! Reports what is wrong at the node of the file, and the place of another node
#! it relates to.
#! @param this The checker.
#! @param file The file.
#! @param node The node.
#! @param code What kind of mistake it is.
#! @param detail What the note says.
#! @param other_file The file of the other node.
#! @param other The other node.
#! @param related What the note says of it.
def checker::report_related_in(
&mut this,
file: u32,
node: u32,
code: ec,
detail: strview,
other_file: u32,
other: u32,
related: strview
): void {
val syntax := &this.units[other_file].syntax;
val token := syntax.node_tokens[other];
this.report_place(file, node, code, detail, other_file, syntax.token_offsets[token], syntax.token_length(token, this.names), related);
}
#! What notes made in the instance being checked say of it, as "In `max<u8>`",
#! with where what is written made it, as ", made at main.sakura:4:12", or
#! nothing outside an instance. An instance an instance made is made by what
#! made that one, so the place is in the program's own code where it can be.
#! @param this The checker.
#! @returns The text, or empty text.
def checker::instance_text(&mut this): string {
return string::new(nothing {}) if this.at.instance == 0;
val held: instance = this.instances[this.at.instance];
var text := string::from("In `", nothing {});
# A variable of a generic type is written through the type, as
# `counter<u8>::count`.
val variable := this.decls.kinds.p[held.decl] == dk::variable && this.decls.owners.p[held.decl] != 0;
text.append(this.names.text(this.decls.names.p[variable ? this.decls.owners.p[held.decl] : held.decl]));
text.append("<");
# A pack's types are written as the arguments they are, as `show_all<i64, bool>`.
this.describe_args(this.types.parts_view(held.args), &mut text);
text.append(">");
if variable {
text.append("::");
text.append(this.names.text(this.decls.names.p[held.decl]));
}
text.append("`");
val made := this.instance_made(this.at.instance);
if made.node != 0 {
val source := &this.units[made.file].source;
val syntax := &this.units[made.file].syntax;
val place := source.lines.place_of(syntax.token_offsets[this.made_token(made)]);
text.append($", made at {source.path}:{place.line}:{place.column}");
}
return text;
}
#! The token a note says an instance was made at: of the function a call
#! calls, where a call made it, as `max` of `max::<u8>(a, b)`, or else of the
#! node.
#! @param this The checker.
#! @param made The file and node, as `instance_made` gives them.
#! @returns The token's index.
def checker::made_token(&this, made: file_node): u32 {
val syntax := &this.units[made.file].syntax;
val node: u32 = syntax.node_kinds[made.node] == nk::call && syntax.node_lefts[made.node] != 0
? syntax.node_lefts[made.node] : made.node;
return syntax.node_tokens[node];
}
#! Where what is written made an instance: the node first asking for it, or for
#! the outermost instance whose checking made it, or none where nothing written
#! asked for it.
#! @param this The checker.
#! @param id The instance.
#! @returns The file and node, or node 0.
def checker::instance_made(&this, id: u32): file_node {
var at := id;
while this.instances[at].made_in != 0 && this.instances[this.instances[at].made_in].made_node != 0 {
at = this.instances[at].made_in;
}
return file_node { file = this.instances[at].made_file, node = this.instances[at].made_node };
}
#! Counts what the checker met and does not check yet, which it lets through
#! rather than reporting, so a program using it is not wrongly refused.
#! @param this The checker.
#! @param node The node.
pub def checker::unsupported(&mut this, node: u32): void {
this.skipped++;
}