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

+184
View File
@@ -0,0 +1,184 @@
#! @file ownership.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the rules of owning values: a value of a type that closes, as a
#! `string`, has one owner, so it is never copied out of the place holding it
#! without saying so, and what a loop iterates is locked while the loop's body
#! runs, so the body cannot change it.
module sakura::analyzer;
#! Whether values of the type are closed where their scope ends: the type
#! names `owning`, or holds a value that does in its fields or
#! elements. A reference or pointer holds nothing of its own.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether they are.
pub def checker::closes(&mut this, kind: u32): bool {
return false if kind == bt::error || this.types.is_untyped(kind);
val flags := this.types.flags.p[kind];
return (flags & tf::closes as u8) != 0 if (flags & tf::weighed as u8) != 0;
# Weighed as closing nothing while it is worked out, so a type holding
# itself through a pointer is met again as one that does not.
this.types.flags.p[kind] = flags | tf::weighed as u8;
val plain := this.unalias(kind);
var made := false;
match this.types.kind_of(plain) {
ty::fixed => made = this.closes(this.types.first_of(plain));
ty::named => {
val decl := this.types.first_of(plain);
val kind_of: dk = this.decls.kinds.p[decl];
if kind_of == dk::struct_type || kind_of == dk::union_type {
val owning := this.std_decl(word::owning);
made = owning != 0 && this.names_trait(plain, owning) && this.has_close(plain);
if !made {
val fields := this.field_types(plain);
for &each in fields {
made = made || this.closes(each);
}
}
}
}
ty::param => {
val owning := this.std_decl(word::owning);
made = owning != 0 && this.param_bounded(plain, owning);
}
else => {}
}
if made {
this.types.flags.p[kind] |= tf::closes as u8;
}
return made;
}
#! Whether the type has the `close(&mut this): void` of `owning`.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether it has.
def checker::has_close(&mut this, kind: u32): bool {
val found := this.find_member(kind, word::close);
return false if found.decl == 0 || this.decls.kinds.p[found.decl] != dk::function;
var walk := this.decls.walk_overloads(found.decl);
while true {
val at := walk.next();
break if at == 0;
continue if !this.member_seen(at);
this.resolve(at);
if this.decls.has(at, df::mut_this) && this.types.part_count(this.decls.types.p[at]) == 1 {
return true;
}
}
return false;
}
#! Reports a value that closes copied out of the place holding it, where it is
#! taken as a value of the type `to`. A new value, as a call gives, is moved.
#! What a reference refers to is copied where it is read through as a whole, and
#! its owner still holds it.
#! @param this The checker.
#! @param node The value's node.
#! @param to The type it is taken as.
pub def checker::check_owned(&mut this, node: u32, to: u32): void {
return if node == 0 || to == bt::error;
return if this.types.kind_of(this.unalias(to)) == ty::reference;
val kind := this.type_at(node);
if this.types.kind_of(kind) == ty::reference {
return if !this.closes(to);
var text := string::from("`", nothing {});
val named := this.describe(to);
text.append(named.view());
text.append("` is closed where its scope ends, so it has one owner, which still holds what the reference refers to. Refer to it with `&` or `&mut`, or copy or move it by name.");
this.report_text(node, ec::owned, &text);
return;
}
# The field of a union is read as a copy, but the union still owns what it
# holds and closes it, even where the union is only a call's result.
if this.union_field(node) && this.closes(kind) && this.closes(to) {
var text := string::from("`", nothing {});
val named := this.describe(kind);
text.append(named.view());
text.append("` is closed where its scope ends, so the union holding it owns it and it cannot be copied out. Name it with `match` by `&` or `&mut`, and copy or move it by name.");
this.report_text(node, ec::owned, &text);
return;
}
# Taken as a view, as a string lends a `span<u8>` of itself, it is not copied.
return if !this.is_place(node) || !this.closes(kind) || !this.closes(to);
var text := string::from("`", nothing {});
val named := this.describe(kind);
text.append(named.view());
text.append("` is closed where its scope ends, so it has one owner and is not copied out of the place holding it. Refer to it with `&` or `&mut`, or copy or move it by name.");
this.report_text(node, ec::owned, &text);
}
#! Whether a node reads a field of a union, as `r.value` does of a `result`.
#! @param this The checker.
#! @param node The node.
#! @returns Whether it does.
def checker::union_field(&mut this, node: u32): bool {
return false if this.kind_of(node) != nk::member;
val held := this.unalias(this.reached_value(this.type_at(this.left_of(node))));
return this.named_kind(held) == dk::union_type;
}
#! Whether a place is part of what a pointer points at, as `p[0]` and
#! `(*p).next` are, which the compiler never closes, since a pointer owns
#! nothing.
#! @param this The checker.
#! @param node The place's node.
#! @returns Whether it is.
pub def checker::pointed_at(&mut this, node: u32): bool {
var at := node;
var steps := 0;
while at != 0 && steps < 64 {
steps++;
val kind := this.kind_of(at);
return false if kind != nk::member && kind != nk::index && !(kind == nk::unary && this.token_kind(at) == tk::star);
at = this.left_of(at);
return true if this.types.kind_of(this.unalias(this.type_at(at))) == ty::pointer;
}
return false;
}
#! What a place is a part of, followed to the variable it is in: the local or
#! declaration it names, or 0 where it is in none, as a new value.
#! @param this The checker.
#! @param node The place's node.
#! @returns What the variable's name refers to, or 0.
pub def checker::root_ref(&this, node: u32): u32 {
var at := node;
var steps := 0;
while at != 0 && steps < 64 {
steps++;
match this.kind_of(at) {
nk::name => return this.ref_at(at);
nk::member, nk::index, nk::slice => at = this.left_of(at);
nk::unary => {
return 0 if this.token_kind(at) != tk::star;
at = this.left_of(at);
}
else => return 0;
}
}
return 0;
}
#! Reports a change of what a loop around it iterates.
#! @param this The checker.
#! @param node The node of what is changed.
pub def checker::check_unlocked(&mut this, node: u32): void {
return if this.locks.length == 0;
val root := this.root_ref(node);
return if root == 0;
for i in 0..this.locks.length {
if this.locks[i] == root {
this.report(node, ec::locked, "A loop iterates this, so the loop's body does not change it.");
return;
}
}
}