Files
sakurac/analyzer/checker/bodies.sakura
T

363 lines
15 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file bodies.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the checking of what each declaration holds beyond its signature:
#! the body of a function, checked once against the bounds of its type
#! parameters where it is generic, the values of variables and fields, and
#! that a struct or union implements the traits it names.
module sakura::analyzer;
#! Checks what the declaration holds.
#! @param this The checker.
#! @param id The declaration.
pub def checker::check_decl(&mut this, id: u32): void {
return if this.decls.has(id, df::checked) || this.decls.has(id, df::failed);
this.decls.mark(id, df::checked);
# The attributes written before it, in its own file and scope.
if this.decls.kinds.p[id] != dk::type_param && this.decls.kinds.p[id] != dk::builtin
&& !this.decls.has(id, df::accessor) && this.decls.nodes.p[id] != 0 {
val saved := this.at;
this.enter_decl(id, 0);
this.check_attributes(id);
this.leave(saved);
}
match this.decls.kinds.p[id] {
dk::function => {
this.resolve(id);
this.check_overloads(id);
this.check_main(id);
if this.decls.has(id, df::foreign) {
val saved := this.at;
this.use_table(this.units[this.decls.files.p[id]].table);
this.check_foreign(id);
this.at = saved;
}
return if this.decls.has(id, df::intrinsic) || this.decls.has(id, df::foreign) && !this.has_body(id);
val saved := this.at;
this.enter_decl(id, 0);
this.check_asked(id);
# Its lifetimes are warned of only where its body checks.
val errors := this.errors;
this.check_function_body(id, this.decls.types.p[id]);
if this.errors == errors {
this.check_lifetimes(id);
}
this.leave(saved);
}
dk::variable, dk::field => this.check_value(id);
dk::struct_type, dk::union_type => {
this.check_implements(id);
this.check_contains(id);
if this.decls.has(id, df::foreign) {
val saved := this.at;
this.use_table(this.units[this.decls.files.p[id]].table);
this.check_c_type(id);
this.at = saved;
}
}
dk::trait_type => {
this.resolve_composed(id);
}
else => this.resolve(id);
}
}
#! Checks a function a trait asks for, which each type implementing the trait
#! gives: one with type parameters of its own gives a default, which each type
#! takes as its own, since no table holds a generic, and one taking no `this`
#! has no default, since nothing within the trait names the type implementing
#! it.
#! @param this The checker.
#! @param id The function's declaration, whose file is the one checked.
def checker::check_asked(&mut this, id: u32): void {
return if !this.trait_asks(id) || this.decls.has(id, df::accessor);
val node: u32 = this.decls.nodes.p[id];
val params := this.record(node, function_slot::type_params);
if this.run_length(params) != 0 && !this.has_body(id) {
this.report(this.run_at(params, 0), ec::generic,
"A trait's generic method gives a default, which each type implementing the trait takes as its own, since no table holds a generic.");
}
if !this.decls.has(id, df::method) && this.has_body(id) {
this.report(node, ec::invalid,
"Static methods of a trait have no default, since nothing within the trait names the type implementing them.");
}
}
#! Whether a function has a body, rather than ending at its signature.
#! @param this The checker.
#! @param id The function's declaration.
#! @returns Whether it has.
pub def checker::has_body(&this, id: u32): bool {
val syntax := &this.units[this.decls.files.p[id]].syntax;
val node := this.decls.nodes.p[id];
if this.decls.has(id, df::accessor) {
return syntax.extras[syntax.node_rights[node]] != 0;
}
return syntax.extras[syntax.node_lefts[node] as u64 + function_slot::body as u64] != 0;
}
#! Checks a function's body against its type: its parameters are locals of
#! their types, its default values are checked where its parameters are, and
#! it returns a value on every path where its result is not `void`.
#! @param this The checker.
#! @param id The function's declaration.
#! @param kind Its function type, given the type arguments it is checked with.
pub def checker::check_function_body(&mut this, id: u32, kind: u32): void {
val node: u32 = this.decls.nodes.p[id];
val accessor := this.decls.has(id, df::accessor);
val body := accessor ? this.record(node, accessor_slot::body) : this.record(node, function_slot::body);
val params := accessor ? this.left_of(node) : this.record(node, function_slot::params);
if body == 0 {
if !this.decls.has(id, df::intrinsic) && !this.decls.has(id, df::foreign)
&& this.enclosing_trait(id) == 0 {
this.report(node, ec::invalid, "A function has a body, unless it is `@intrinsic`, given by the compiler, or C's: a function with no body is C's only in an `extern module`.");
}
return;
}
this.at.result = this.types.first_of(kind);
val count := this.types.part_count(kind);
# A trait's method's own body has the bounds it puts on the trait's parameters
# and `self`, which an instance's arguments meet.
val proved := this.proved_params.length;
if this.at.instance == 0 {
this.prove_outer(id);
}
# A pack parameter is a local holding the pack: in the generic's own body,
# a pack of types not known, and in an instance, the types the parameters
# from it on were given.
val written: u32 = this.decls.types.p[id];
val wcount := this.types.part_count(written);
val packed := this.decls.has(id, df::variadic) && wcount != 0
&& this.types.is_open_pack(this.types.part_at(written, wcount - 1));
for i in 0..this.run_length(params) {
val param := this.run_at(params, i as u32);
var held: u32 = i < count ? this.types.part_at(kind, i as u32) : bt::error;
if packed && i as u32 + 1 == wcount {
var rest := vector::<u32>::new();
for j in i..count {
rest.append(this.types.part_at(kind, j as u32));
}
# Given a variadic function's parameters, as a pattern's pack may be, the
# pack ends in a view of the rest, which its type says as a variadic pack.
val open := i < count && this.types.is_open_pack(this.types.part_at(kind, i as u32));
held = open ? this.types.part_at(kind, i as u32)
: this.types.second_of(kind) != 0 && rest.length != 0 ? this.types.variadic_pack(rest.view())
: this.types.pack(rest.view(), 0);
} else if this.types.second_of(kind) != 0 && i as u32 + 1 == count {
held = this.variadic_type(held);
}
# A default value is checked where its parameter is, with the parameters
# before it named, and runs at each call that leaves it out.
val value := accessor ? 0 : this.record(param, parameter_slot::value);
if value != 0 {
this.check_expr(value, held);
}
this.declare_local(param, this.param_name(param), held);
this.set_flag(param, rf::mutable);
}
this.at.body = body;
val how := this.check_block(body);
this.unprove_to(proved);
val result := this.at.result;
if how == flow::goes_on && result != bt::void_type && result != bt::error {
this.report(node, ec::control, "The function can end without returning a value.");
}
}
#! The type a variadic parameter is within its function: a view of its
#! arguments, or with a pack, the pack itself.
#! @param this The checker.
#! @param element The type of the parameter as written: of each argument.
#! @returns The type.
def checker::variadic_type(&mut this, element: u32) : u32 {
return element if this.types.kind_of(element) == ty::pack;
return this.span_of(element);
}
#! Checks the value a variable or field is declared with, against the type
#! written for it.
#! @param this The checker.
#! @param id The variable's or field's declaration.
def checker::check_value(&mut this, id: u32): void {
this.resolve(id);
val node: u32 = this.decls.nodes.p[id];
val saved := this.at;
this.enter_decl(id, 0);
val value := this.record(node, variable_slot::value);
if value != 0 && !this.has_flag(value, rf::checked) {
this.check_expr(value, this.decls.types.p[id]);
} else if value == 0 && this.decls.kinds.p[id] == dk::variable && !this.has_zero(this.decls.types.p[id], 0) {
var text := string::from("`", nothing {});
val named := this.describe(this.decls.types.p[id]);
text.append(named.view());
text.append("` has no zero value, so the variable is given a value where it is declared.");
this.report_text(node, ec::zero_value, &text);
}
this.leave(saved);
}
#! Checks that a struct or union implements each trait it names.
#! @param this The checker.
#! @param id The struct's or union's declaration.
def checker::check_implements(&mut this, id: u32): void {
val run := this.composed_of(id);
val kind: u32 = this.decls.types.p[id];
for i in 0..this.decls.list_length(run) {
val wanted := this.decls.list_at(run, i as u32);
continue if this.satisfies(kind, wanted);
var text := string::from("`", nothing {});
text.append(this.decl_text(id));
text.append("` names `");
val named := this.describe(wanted);
text.append(named.view());
text.append("` but does not implement all of it");
val missing := this.missing_requirement(kind, wanted);
if missing != 0 {
text.append(": it has no `");
text.append(this.decl_text(missing));
text.append("` of the signature the trait asks for");
}
text.append(".");
this.report_decl(id, ec::bound, text.view());
}
}
#! The first thing a trait asks for that the type does not have, or 0.
#! @param this The checker.
#! @param kind The type.
#! @param trait_kind The trait's type, given its arguments.
#! @returns The declaration of what it asks for, or 0.
def checker::missing_requirement(&mut this, kind: u32, trait_kind: u32): u32 {
val holder := this.types.first_of(trait_kind);
val run := this.decls.children.p[holder];
for i in 0..this.decls.list_length(run) {
val wanted := this.decls.list_at(run, i as u32);
continue if this.decls.has(wanted, df::optional);
return wanted if !this.meets(kind, trait_kind, wanted);
}
return 0;
}
#! Reports an overload taking the same parameters as one declared before it,
#! which no call could choose between.
#! @param this The checker.
#! @param id The function's declaration.
def checker::check_overloads(&mut this, id: u32): void {
val owner := this.decls.owners.p[id];
val first: u32 = owner != 0 && this.decls.kinds.p[owner] != dk::property
? this.first_member(id)
: this.modules.decl(this.decls.modules.p[id], this.decls.names.p[id]);
return if first == 0 || first == id || this.decls.has(id, df::accessor);
var walk := this.decls.walk_overloads(first);
while true {
val at := walk.next();
break if at == 0 || at == id;
this.resolve(at);
if this.decls.kinds.p[at] == dk::function && this.same_params(at, id) {
this.report_decl_related(id, ec::duplicate,
"Another overload of this name takes the same parameters, so no call could choose between them.",
at, "The other overload.");
return;
}
}
}
#! Whether two functions take the same parameters: of the same types, an alias
#! being a type of its own, both static or both taking `this` alike.
#! @param this The checker.
#! @param a One function's declaration.
#! @param b The other's.
#! @returns Whether they do.
def checker::same_params(&mut this, a: u32, b: u32): bool {
return false if this.decls.has(a, df::method) != this.decls.has(b, df::method);
return false if this.decls.has(a, df::mut_this) != this.decls.has(b, df::mut_this);
return false if this.decls.has(a, df::generic) || this.decls.has(b, df::generic);
val ka := this.decls.types.p[a];
val kb := this.decls.types.p[b];
return false if this.types.part_count(ka) != this.types.part_count(kb);
return false if (this.types.second_of(ka) != 0) != (this.types.second_of(kb) != 0);
for i in 0..this.types.part_count(ka) {
return false if this.types.part_at(ka, i as u32) != this.types.part_at(kb, i as u32);
}
return true;
}
#! Checks `main`, of the program itself, which takes nothing or the arguments
#! as views of text, and returns nothing.
#! @param this The checker.
#! @param id The declaration of `main`.
def checker::check_main(&mut this, id: u32): void {
return if this.decls.names.p[id] != word::main || this.decls.modules.p[id] != 0 || this.decls.owners.p[id] != 0;
val kind: u32 = this.decls.types.p[id];
var valid := this.types.first_of(kind) == bt::void_type && this.types.part_count(kind) <= 1;
if valid && this.types.part_count(kind) == 1 {
val texts := this.span_of(this.span_of(bt::uint8));
valid = texts == bt::error || this.strip(this.types.part_at(kind, 0), 0) == this.strip(texts, 0);
}
if this.decls.overloads.p[id] != 0 || this.modules.decl(0, word::main) != id {
valid = false;
}
return if valid;
this.report_decl(id, ec::invalid,
"`main` is `def main(): void` or `def main(args: span<strview>): void`, and is not overloaded.");
}
#! Reports a struct or union holding itself, other than through a pointer or a
#! reference, which no amount of memory could hold.
#! @param this The checker.
#! @param id The struct's or union's declaration.
def checker::check_contains(&mut this, id: u32): void {
var seen := vector::<u32>::new();
if this.holds(this.decls.types.p[id], id, &mut seen, 0) {
var text := string::from("`", nothing {});
text.append(this.decl_text(id));
text.append("` holds itself, which only a pointer or a reference to it can.");
this.report_decl(id, ec::recursive, text.view());
}
}
#! Whether a value of the type holds a value of the declaration's type, in its
#! fields or elements, rather than through a pointer or reference.
#! @param this The checker.
#! @param kind The type.
#! @param target The declaration looked for.
#! @param seen The types already looked within, which are not looked within
#! again.
#! @param depth How deeply the search has gone.
#! @returns Whether it does.
def checker::holds(&mut this, kind: u32, target: u32, seen: &mut vector<u32>, depth: u32): bool {
return false if depth > 32;
val plain := this.unalias(kind);
if this.types.kind_of(plain) == ty::fixed {
return this.holds(this.types.first_of(plain), target, seen, depth + 1);
}
val named := this.named_kind(plain);
return false if named != dk::struct_type && named != dk::union_type;
val decl := this.types.first_of(plain);
return true if decl == target && depth > 0;
return false if seen.view().contains(&plain);
seen.append(plain);
val fields := this.field_types(plain);
for &field in fields {
return true if this.holds(field, target, seen, depth + 1);
}
return false;
}