#! @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 the warning where a loop's body changes whole what the #! loop walks, which may move what the loop reads next. 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); } # A pack held by a field closes what of it closes, and one whose types are # not known yet what its bounds say does. ty::pack => { if this.types.is_open_pack(plain) { val owning := this.std_decl(word::owning); made = owning != 0 && this.param_bounded(plain, owning); } else { for i in 0..this.types.part_count(plain) { made = made || this.closes(this.types.part_at(plain, i as u32)); } } } 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.span); 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.span); 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` 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.span); 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); } #! Checks a pack given whole, as `...args` gives it to a call, or a literal to a #! field holding a pack: each value that closes is moved to where it goes, #! which takes it by value, leaving the pack's place empty, so the pack is a #! variadic parameter, which the function owns, a new value's, as a call's #! result's, or a place that can be written. #! @param this The checker. #! @param node The pack's node. #! @param pack The pack's type. #! @param to The pack of the types its values go to. pub def checker::check_pack_moved(&mut this, node: u32, pack: u32, to: u32): void { val plain := this.unalias(this.reached(pack)); return if this.types.kind_of(plain) != ty::pack || this.types.kind_of(to) != ty::pack; return if this.kind_of(node) == nk::name || !this.closes(plain); var moves := false; for j in 0..this.types.part_count(plain) { val each := this.types.part_at(plain, j as u32); val goes := j < this.types.part_count(to) ? this.types.part_at(to, j as u32) : bt::error; moves = moves || (this.closes(each) && this.types.kind_of(this.unalias(goes)) != ty::reference); } return if !moves || !this.is_place(node) || this.is_writable(node); var text := string::from("`", nothing {}); val named := this.describe(plain); text.append(named.span); text.append("` holds what is closed where its scope ends, which is moved out of the pack, leaving it empty, so the pack is one that can be written: a `var`'s field, or one reached through `&mut`."); 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; } #! Whether a node reads a property, `x.name` or `x.(d)` of one, whose value its #! getter makes, as a call's makes one, rather than being a place. #! @param this The checker. #! @param node The node. #! @returns Whether it does. pub def checker::reads_property(&this, node: u32): bool { val kind := this.kind_of(node); return false if kind != nk::member && kind != nk::member_of; val ref := this.ref_at(node); return false if ref == 0 || (ref & (ref_local | ref_type)) != 0; val decl: dk = this.decls.kinds.p[ref]; return decl == dk::property || (decl == dk::function && this.decls.has(ref, df::accessor)); } #! The place a loop walks what it reads from, which changing whole may move or #! release: what is iterated, or what it is lent from, as `v` of a slice of it, #! `v[1:]`, of a property of it giving a view or an iterator, `v.span`, and of #! its `iter()`. What another method or property gives may hold nothing of the #! value, so it is no place the loop is sure to walk. It is 0 where that is no #! place `same_place` tells apart, or where nothing moves what the loop reads: a #! fixed array, a view, or an iterator, which the loop and its body advance #! alike. #! @param this The checker. #! @param iterated The node of what the loop iterates. #! @returns The place's node, or 0. pub def checker::walked_place(&mut this, iterated: u32): u32 { var at := iterated; var steps := 0; while steps < 16 { steps++; if this.kind_of(at) == nk::slice || (this.reads_property(at) && this.lends_walk(this.type_at(at))) { at = this.left_of(at); continue; } if this.kind_of(at) == nk::call { val callee := this.left_of(at); val decl := this.ref_at(at); if this.kind_of(callee) == nk::member && decl != 0 && (decl & (ref_local | ref_type)) == 0 && this.decls.kinds.p[decl] == dk::function && this.decls.has(decl, df::method) && this.decls.names.p[decl] == word::iter { at = this.left_of(callee); continue; } } break; } return 0 if !this.plain_place(at) || this.lends_walk(this.type_at(at)); return 0 if this.types.kind_of(this.unalias(this.reached(this.type_at(at)))) != ty::named; return at; } #! Whether a value of a type walks what something else holds without owning it: #! a view, or an iterator, as what `v.span` and `v.iter()` give. #! @param this The checker. #! @param kind The type. #! @returns Whether it does. def checker::lends_walk(&mut this, kind: u32): bool { val held := this.unalias(this.reached(kind)); return this.is_std(held, word::span) || this.is_std(held, word::slice) || (this.types.kind_of(held) == ty::named && this.find_member(held, word::next).decl != 0); } #! Whether a node is a place written with names, fields, `*`, and indexes that #! are names or constants alone, which `same_place` tells apart. #! @param this The checker. #! @param node The node. #! @returns Whether it is. def checker::plain_place(&mut this, node: u32): bool { var at := node; var steps := 0; while steps < 32 { steps++; match this.kind_of(at) { nk::name, nk::path => { val ref := this.ref_at(at); return ref != 0 && (ref & ref_type) == 0; } nk::member => { val ref := this.ref_at(at); return false if ref == 0 || (ref & (ref_local | ref_type)) != 0 || this.decls.kinds.p[ref] != dk::field; } nk::index => { val at_index := this.right_of(at); return false if !this.is_known(at_index) && (this.kind_of(at_index) != nk::name || this.ref_at(at_index) == 0); } nk::unary => { return false if this.token_kind(at) != tk::star; } else => return false; } at = this.left_of(at); } return false; } #! Whether two places are written alike: the same local or global, its same #! fields, through `*` alike, at indexes of the same names or constants. #! @param this The checker. #! @param a One place's node. #! @param b The other's. #! @returns Whether they are. def checker::same_place(&mut this, a: u32, b: u32): bool { var x := a; var y := b; var steps := 0; while steps < 32 { steps++; val kind := this.kind_of(x); return false if kind != this.kind_of(y); match kind { nk::name, nk::path => return this.ref_at(x) != 0 && this.ref_at(x) == this.ref_at(y); nk::member => { return false if this.ref_at(x) == 0 || this.ref_at(x) != this.ref_at(y); } nk::index => { val i := this.right_of(x); val j := this.right_of(y); if this.is_known(i) && this.is_known(j) { return false if this.value_at(i).magnitude != this.value_at(j).magnitude; } else { return false if this.kind_of(i) != nk::name || this.kind_of(j) != nk::name || this.ref_at(i) == 0 || this.ref_at(i) != this.ref_at(j); } } nk::unary => { return false if this.token_kind(x) != tk::star || this.token_kind(y) != tk::star; } else => return false; } x = this.left_of(x); y = this.left_of(y); } return false; } #! Warns where a loop around the node walks the place it is, which changing #! whole, as a method taking `&mut this` does, `&mut` of it, or assigning it, #! may move or release, so what the loop reads next may be gone. What changes #! what it holds in place, as `v[i] = x` or `v.first = x`, moves nothing, and #! is not warned of; nor is what changes what holds it, which may not change it. #! @param this The checker. #! @param node The node of what is changed. pub def checker::check_walked(&mut this, node: u32): void { return if this.walked.length == 0; for i in 0..this.walked.length { val held := this.walked[i]; continue if (held >> 32) as u32 != this.at.table; val place := (held & 0xFFFFFFFF) as u32; continue if !this.same_place(place, node); var text := string::from("`", nothing {}); this.written_text(place, &mut text); text.append("` is walked by the loop around this, and changing it whole, as this may, can move or release what the loop reads next. Walk a copy of it, or its indexes, to change it."); this.warn_text(node, ec::lifetime, &text, 0); return; } }