#! @file escapes.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the scan of a body for where the references it makes, and those it #! is given, may go: the flows `flows.sakura` solves, found from the checked #! syntax as lowering would lower it. Each local is a spot, and so is a value a #! statement holds where a reference to it is taken, the environment of an #! anonymous function capturing locals, and what the caller gives each #! parameter. An expression gives the set of spots its value may hold #! references to, or none where its type holds no reference, and a place gives #! the set of spots it may lie within. #! #! A call gives each argument where the summary of the function called says, #! as `keeps.sakura` works it out: kept, given back in what the call gives, or #! neither; and what the argument reaches likewise. What the scan does not #! know, as a call of a function value, keeps what it is given and gives back #! what any code may reach. Calling a function value keeps nothing of it, since #! its body was scanned where it was made. That body is scanned there, sharing #! the spots of the locals it captures, with what it is given and gives back #! being anyone's. #! #! Pointers are left out: one made of a reference keeps nothing alive, as it is #! good only while what it points at is where it is, but what that holds may be #! read through it by any code, and so is kept. import sakura::analyzer; import std::containers; module sakura::compiler; #! What a scan of a body holds as it goes. pub struct escape_scan { var graph: flow; #! The flows found. var slots: dict; #! The spot of each local, by the node declaring it. var temps: dict; #! The spot of each value a statement holds that a reference refers to, by the node it is held for. var envs: dict; #! The spot of the environment of each anonymous function capturing locals, by its node. var outers: vector; #! Of each parameter, the spot of what it refers to, or 0 where it takes a value. var inners: vector; #! Of each parameter, the spot of what the caller's reference or value reaches, or 0. var returns: u32; #! The set what `return` gives flows into. var result: u32; #! The type `return` gives. var within: vector; #! The anonymous functions the scan is within, each as `pair_key(lowest, node)` of its nodes. } #! Makes a scan, which has found nothing yet. #! @param alloc The optional allocator its memory is taken from. #! @returns The scan. pub def escape_scan::new(alloc: maybe<&mut allocator>): escape_scan { var made := escape_scan { graph = flow::new(alloc), slots = dict::::new(alloc), temps = dict::::new(alloc), envs = dict::::new(alloc), outers = vector::::new(alloc), inners = vector::::new(alloc), within = vector::::new(alloc) }; made.returns = made.graph.given; return made; } # Spots. #! The spot of a local, made where it is new, as deep within anonymous #! functions as it is declared. #! @param this The compiler. #! @param s The scan. #! @param local The node declaring the local. #! @returns The spot. pub def lowerer::slot_of(&mut this, s: &mut escape_scan, local: u32): u32 { if s.slots.get(&local) is some(got) { return got; } var depth: u32 = 0; for &each in s.within { if pair_high(each) <= local && local <= pair_low(each) { depth++; } } val made := s.graph.new_spot(spot_kind::local, local, depth); s.slots.set(local, made); return made; } #! The place of a local's storage, as a set, noting where a reference to it is #! made. #! @param this The compiler. #! @param s The scan. #! @param local The node declaring the local. #! @param refers Whether a reference to the place is made, rather than what it #! holds read or written. #! @returns The set. def lowerer::local_place(&mut this, s: &mut escape_scan, local: u32, refers: bool): u32 { val at := this.slot_of(s, local); if refers { s.graph.spots[at as u64].taken = true; } return s.graph.alone(at); } #! A reference to a value a statement holds for a node, as a set: the spot it #! is held in, made where it is new, holding the value. #! @param this The compiler. #! @param s The scan. #! @param node The node it is held for. #! @param value The set of the value, or 0. #! @returns The set. def lowerer::temp_place(&mut this, s: &mut escape_scan, node: u32, value: u32): u32 { var at: u32 = 0; if s.temps.get(&node) is some(got) { at = got; } else { at = s.graph.new_spot(spot_kind::temp, node, s.within.length as u32); s.temps.set(node, at); } s.graph.into(value, s.graph.held_by(at)); return s.graph.alone(at); } #! A set of what any code may reach, or none where the type holds no reference. #! @param this The compiler. #! @param s The scan. #! @param kind The type. #! @returns The set, or 0. def lowerer::shared_flow(&mut this, s: &mut escape_scan, kind: u32): u32 { return 0 if !this.holds_references(kind); return s.graph.alone(s.graph.shared); } #! Whether a value of a type may hold a reference: a reference, a function #! value, which holds its environment, or what holds one of those. A type still #! open, as a parameter of a generic, may. #! @param this The compiler. #! @param kind The type. #! @returns Whether it may. pub def lowerer::holds_references(&mut this, kind: u32): bool { return false if kind == 0 || kind == bt::error; val plain := this.layout_type(kind); val held := this.checker.types.kind_of(plain); return true if held == ty::param || held == ty::pack; return held != ty::void_type && this.shape_of(plain).pointers; } # Parameters. #! Gives the scan the parameters of a function: each a local holding what the #! caller gives, a reference to what is the caller's, or a value whose #! references reach what is the caller's, and given its default where it has #! one, which a call leaving it out gives it. #! @param this The compiler. #! @param s The scan. #! @param params The run of the parameters' nodes. #! @param kind The function's type. #! @param packed Whether the last parameter is a pack, taking every parameter from its own on. pub def lowerer::scan_params(&mut this, s: &mut escape_scan, params: u32, kind: u32, packed: bool): void { val count := this.checker.types.part_count(kind); val written := this.checker.run_length(params); for i in 0..written { val param := this.checker.run_at(params, i as u32); val slot := this.slot_of(s, param); # What a pack holds, and what the type leaves out, is taken as anyone's. if i >= count as u64 || (packed && i + 1 == written as u64) { s.graph.add(s.graph.held_by(slot), s.graph.shared); s.outers.append(0); s.inners.append(0); continue; } val taken := this.checker.types.part_at(kind, i as u32); val inner := s.graph.new_spot(spot_kind::inner, param, 0); s.graph.add(s.graph.held_by(inner), inner); if this.is_reference_type(taken) { val outer := s.graph.new_spot(spot_kind::outer, param, 0); s.graph.add(s.graph.held_by(outer), inner); s.graph.add(s.graph.held_by(slot), outer); s.outers.append(outer); } else { if this.holds_references(taken) { s.graph.add(s.graph.held_by(slot), inner); } s.outers.append(0); } s.inners.append(inner); val value := this.checker.record(param, parameter_slot::value); if value != 0 { s.graph.into(this.value_flow_as(s, value, taken), s.graph.held_by(slot)); } } } # Statements. #! Scans a body: a block, or an expression whose value is returned. #! @param this The compiler. #! @param s The scan. #! @param body The body's node. pub def lowerer::scan_body(&mut this, s: &mut escape_scan, body: u32): void { return if body == 0; if this.checker.kind_of(body) == nk::block { this.scan_block(s, body); return; } s.graph.into(this.value_flow_as(s, body, s.result), s.returns); } #! Scans a block, or a statement standing for one, up to a statement after #! which the checker checked nothing. #! @param this The compiler. #! @param s The scan. #! @param node The block. pub def lowerer::scan_block(&mut this, s: &mut escape_scan, node: u32): void { return if node == 0; if this.checker.kind_of(node) != nk::block { this.scan_statement(s, node); return; } val run := this.checker.left_of(node); for i in 0..this.checker.run_length(run) { val each := this.checker.run_at(run, i as u32); this.scan_statement(s, each); break if this.statement_skips(each); } } #! Has a walk go under a node to what a scan walks: all but the bodies of #! anonymous functions, which are planned as functions of their own, and of an #! `if const` or `when` only the branch taken. #! @param this The compiler. #! @param node The node. #! @param walk The walk. pub def lowerer::scan_children(&mut this, node: u32, walk: &mut node_walk): void { match this.checker.kind_of(node) { nk::lambda => return; nk::when_block => { walk.add(this.checker.when_branch(node)); return; } # A ternary choosing while compiling is the branch the checker chose alone. nk::ternary => { val chosen := this.checker.chosen_arm(node); if chosen != 0 { walk.add(chosen); return; } } # `match const` is the arm the checker chose alone. nk::match_stmt, nk::match_value => { if this.checker.is_match_const(node) { val chosen := this.checker.chosen_arm(node); if chosen != 0 { walk.add(this.checker.right_of(chosen)); } return; } } nk::if_const => { val condition := this.checker.left_of(node); if this.checker.is_known(condition) { walk.add(this.checker.value_at(condition).magnitude != 0 ? this.checker.record(node, if_slot::then) : this.checker.record(node, if_slot::other)); return; } if this.evaluated.decisions.get(&node_key(this.checker.at.table, node)) is some(holds) { walk.add(holds ? this.checker.record(node, if_slot::then) : this.checker.record(node, if_slot::other)); return; } } else => {} } walk.enter(this.checker.syn(), node); } #! Whether a statement leaves where it was decided while checking, so the #! checker checked nothing after it, as `emitter::skips` says. #! @param this The compiler. #! @param node The statement. #! @returns Whether it does. def lowerer::statement_skips(&mut this, node: u32): bool { match this.checker.kind_of(node) { nk::block => { val run := this.checker.left_of(node); for i in 0..this.checker.run_length(run) { return true if this.statement_skips(this.checker.run_at(run, i as u32)); } } nk::when_block => return this.statement_skips(this.checker.when_branch(node)); nk::match_stmt => { return false if !this.checker.is_match_const(node); val chosen := this.checker.chosen_arm(node); return chosen != 0 && this.statement_skips(this.checker.right_of(chosen)); } nk::if_const => { val condition := this.checker.left_of(node); return false if !this.checker.is_known(condition); val then := this.checker.record(node, if_slot::then); val other := this.checker.record(node, if_slot::other); if this.checker.value_at(condition).magnitude != 0 { return this.statement_skips(then) || (other == 0 && this.checker.has_flag(then, rf::leaves)); } return other != 0 && this.statement_skips(other); } else => {} } return false; } #! Scans a statement. #! @param this The compiler. #! @param s The scan. #! @param node The statement. def lowerer::scan_statement(&mut this, s: &mut escape_scan, node: u32): void { match this.checker.kind_of(node) { nk::block => this.scan_block(s, node); nk::variable => this.scan_variable(s, node); nk::if_stmt => { this.value_flow(s, this.checker.left_of(node)); this.scan_block(s, this.checker.record(node, if_slot::then)); this.scan_block(s, this.checker.record(node, if_slot::other)); } nk::if_const => { val condition := this.checker.left_of(node); val then := this.checker.record(node, if_slot::then); val other := this.checker.record(node, if_slot::other); if this.checker.is_known(condition) { this.scan_block(s, this.checker.value_at(condition).magnitude != 0 ? then : other); return; } if this.evaluated.decisions.get(&node_key(this.checker.at.table, node)) is some(holds) { this.scan_block(s, holds ? then : other); return; } this.value_flow(s, condition); this.scan_block(s, then); this.scan_block(s, other); } nk::when_block => this.scan_block(s, this.checker.when_branch(node)); nk::while_stmt => { this.value_flow(s, this.checker.left_of(node)); this.scan_block(s, this.checker.right_of(node)); } nk::for_stmt => this.scan_for(s, node); nk::match_stmt => this.match_flow(s, node, false); nk::return_stmt => { val value := this.checker.left_of(node); if value != 0 { s.graph.into(this.value_flow_as(s, value, s.result), s.returns); } } nk::break_stmt, nk::continue_stmt => {} nk::expr_stmt => this.value_flow(s, this.checker.left_of(node)); nk::contract => this.contract_flow(s, node); else => this.unknown_flow(s, node); } } #! Scans a contract, where contracts are checked: its condition and what it #! says failed are values made where it is, and the name an `ensures` gives #! what is returned may hold what any code may reach. One left out makes #! nothing. #! @param this The compiler. #! @param s The scan. #! @param node The `contract` node. def lowerer::contract_flow(&mut this, s: &mut escape_scan, node: u32): void { return if this.checker.contracts_off; val bound := this.checker.record(node, contract_slot::bound); if bound != 0 && this.holds_references(this.checker.type_at(bound)) { s.graph.add(s.graph.held_by(this.slot_of(s, bound)), s.graph.shared); } this.value_flow(s, this.checker.left_of(node)); this.value_flow(s, this.checker.record(node, contract_slot::text)); } #! Scans a local variable or constant: its storage holds what it is given. A #! constant is no local but a value known while compiling, or data evaluated #! while compiling, which any code may reach. #! @param this The compiler. #! @param s The scan. #! @param node The `variable` node. def lowerer::scan_variable(&mut this, s: &mut escape_scan, node: u32): void { val value := this.checker.record(node, variable_slot::value); val kind := this.checker.type_at(node); if this.checker.node_has(node, nf::constant) { s.slots.set(node, s.graph.shared); return; } # A function declared within a block names its own local, which is made first. val slot := this.slot_of(s, node); if value == 0 { # The defaults of its fields are made where its type is declared. if this.holds_references(kind) { s.graph.add(s.graph.held_by(slot), s.graph.shared); } return; } s.graph.into(this.value_flow_as(s, value, kind), s.graph.held_by(slot)); } #! Scans what a node holds as values that are kept, as for a node the scan does #! not know. #! @param this The compiler. #! @param s The scan. #! @param node The node. def lowerer::unknown_flow(&mut this, s: &mut escape_scan, node: u32): void { var held := vector::::new(); this.checker.syn().children(node, &mut held); for &each in held { continue if each == 0; if this.checker.has_flag(each, rf::borrowed) { s.graph.keep(this.lent_flow(s, each)); } s.graph.keep(this.value_flow(s, each)); } } # Loops. #! Scans `for`: a range counts, the elements of a fixed array or a view are each #! referred to where they are, and an iterator is asked for each, lent by what #! is walked where that is no iterator. #! @param this The compiler. #! @param s The scan. #! @param node The `for_stmt` node. def lowerer::scan_for(&mut this, s: &mut escape_scan, node: u32): void { val iterated := this.checker.record(node, for_slot::iterated); val end := this.checker.record(node, for_slot::end); val variable := this.checker.record(node, for_slot::variable); val body := this.checker.right_of(node); if this.checker.node_has(node, nf::constant) { this.unrolled_flow(s, node); return; } if end != 0 { this.value_flow(s, iterated); this.value_flow(s, end); this.scan_block(s, body); return; } val through := this.checker.unalias(this.checker.reached(this.checker.type_at(iterated))); if this.checker.types.kind_of(through) == ty::fixed || this.viewed(through) != 0 { val fixed := this.checker.types.kind_of(through) == ty::fixed; val whole := this.object_flow(s, iterated, fixed); val element := fixed ? whole : s.graph.alone(s.graph.shared); if variable != 0 { val slot := this.slot_of(s, variable); val bound := this.checker.type_at(variable); val given := this.is_reference_type(bound) ? element : s.graph.read_of(element); if this.holds_references(bound) { s.graph.into(given, s.graph.held_by(slot)); } } this.scan_block(s, body); return; } # The iterator, where it is: what the loop is given, or what `iter` lends, # which the loop holds. var cursor: u32 = 0; var cursor_type := through; if !this.checker.has_method(through, word::next) { val lends := this.checker.pick_iter(through, this.checker.node_has(node, nf::mutable)); val receiver := this.is_reference_type(this.checker.type_at(iterated)) ? this.through_one_flow(s, iterated) : this.lent_flow(s, iterated); val instance := this.method_instance(lends, through); val lent_kind := this.method_kind(lends, instance, through); val made := this.method_flow(s, lends, through, receiver); cursor_type = this.checker.unalias(this.checker.types.first_of(lent_kind)); cursor = this.temp_place(s, node, made); } else if this.is_reference_type(this.checker.type_at(iterated)) { cursor = this.through_one_flow(s, iterated); } else if this.checker.is_place(iterated) { cursor = this.place_flow(s, iterated, true); } else { cursor = this.temp_place(s, node, this.raw_flow(s, iterated)); } # `next` gives `some` of each element, bound as the variable's type takes it. val next := this.checker.find_member(cursor_type, word::next).decl; val given := this.method_flow(s, next, cursor_type, cursor); if variable != 0 && given != 0 { val slot := this.slot_of(s, variable); val bound := this.checker.type_at(variable); val element := this.payload_type(this.checker.types.first_of(this.method_kind(next, this.method_instance(next, cursor_type), cursor_type))); s.graph.into(this.converted_flow(s, given, element, bound), s.graph.held_by(slot)); } this.scan_block(s, body); } #! The type a `maybe` holds as `some`, or the type itself where it is none. #! @param this The compiler. #! @param kind The type. #! @returns The type held. def lowerer::payload_type(&mut this, kind: u32): u32 { val plain := this.checker.unalias(kind); return kind if this.checker.named_kind(plain) != dk::union_type; val index := this.layout_of(plain); for i in 0..this.layout_at(index).count { val field := this.field_at(index, i as u32); return field.type_id if field.name == word::some; } return kind; } #! Scans `for const`: the body the checker checked once for each value of a pack, #! or each declaration reflection gives, each in a result table of its own. The #! variable is the pack's value itself, which is taken as anyone's, or a local #! holding the description of the pass, which is data. #! @param this The compiler. #! @param s The scan. #! @param node The `for_stmt` node. def lowerer::unrolled_flow(&mut this, s: &mut escape_scan, node: u32): void { val variable := this.checker.record(node, for_slot::variable); val iterated := this.checker.record(node, for_slot::iterated); val body := this.checker.right_of(node); val outer := this.checker.at.table; val pack := this.checker.unalias(this.checker.reached(this.checker.type_at(iterated))); if variable != 0 { if this.checker.types.kind_of(pack) == ty::pack { s.slots.set(variable, s.graph.shared); } else { s.graph.add(s.graph.held_by(this.slot_of(s, variable)), s.graph.shared); } } for t in 1..this.checker.tables.length { val held: rtable = this.checker.tables.p[t]; continue if held.parent != outer || held.base != variable || held.count != body - variable + 1; this.checker.use_table(t as u32); this.scan_block(s, body); this.checker.use_table(outer); } } # Values. #! The value of an expression as the type it is used as. #! @param this The compiler. #! @param s The scan. #! @param node The expression, or 0. #! @returns The set of its value, or 0. pub def lowerer::value_flow(&mut this, s: &mut escape_scan, node: u32): u32 { return 0 if node == 0; val own := this.raw_flow(s, node); return this.converted_flow(s, own, this.checker.type_at(node), this.checker.converted(node)); } #! The value of an expression converted to a type. #! @param this The compiler. #! @param s The scan. #! @param node The expression, or 0. #! @param to The type. #! @returns The set of its value, or 0. pub def lowerer::value_flow_as(&mut this, s: &mut escape_scan, node: u32, to: u32): u32 { return 0 if node == 0; val made := this.value_flow(s, node); return this.converted_flow(s, made, this.checker.converted(node), to); } #! What converting a value gives, as `emitter::convert` converts it: a #! reference is read through where a value is wanted, but kept as it is by a #! union holding it, and anything else holds what it held. #! @param this The compiler. #! @param s The scan. #! @param set The value's set, or 0. #! @param from Its type. #! @param to The type converted to. #! @returns The set, or 0. def lowerer::converted_flow(&mut this, s: &mut escape_scan, set: u32, from: u32, to: u32): u32 { return 0 if set == 0 || !this.holds_references(to); return set if to == from || to == bt::error || from == 0 || from == bt::error; var at := set; var kind := from; var steps := 0; while steps < 16 { steps++; val source := this.layout_type(kind); val target := this.layout_type(to); return at if source == target; val fk := this.checker.types.kind_of(source); val tok := this.checker.types.kind_of(target); if fk == ty::reference && tok == ty::reference { val inner_from := this.checker.unalias(this.checker.types.first_of(source)); val inner_to := this.checker.unalias(this.checker.types.first_of(target)); return at if this.checker.types.kind_of(inner_from) != ty::reference || this.checker.types.kind_of(inner_to) == ty::reference; at = s.graph.read_of(at); kind = inner_from; continue; } if this.checker.named_kind(target) == dk::union_type && this.checker.united_field(kind, target) != 0 { return at; } if fk == ty::reference { at = s.graph.read_of(at); kind = this.checker.types.first_of(source); continue; } return at; } return at; } #! The value of an expression as its own type, by the kind of its node. #! @param this The compiler. #! @param s The scan. #! @param node The expression. #! @returns The set of its value, or 0. pub def lowerer::raw_flow(&mut this, s: &mut escape_scan, node: u32): u32 { return 0 if node == 0; val kind := this.checker.type_at(node); # What reflection gives is worked out while compiling, from what any code may reach. if this.checker.has_flag(node, rf::reflected) { return this.shared_flow(s, kind); } if this.checker.is_known(node) && this.is_scalar_type(kind) { return 0; } match this.checker.kind_of(node) { nk::number, nk::character, nk::boolean, nk::string, nk::null_lit, nk::undefined => return 0; nk::istring => { # Each hole is written by its layout, which keeps nothing. val run := this.checker.left_of(node); for i in 0..this.checker.run_length(run) { this.value_flow(s, this.checker.run_at(run, i as u32)); } return 0; } nk::name, nk::path => return this.name_flow(s, node); nk::unary => return this.unary_flow(s, node); nk::postfix => { this.step_flow(s, this.checker.left_of(node)); return 0; } nk::reference => return this.reference_flow(s, node); nk::binary => return this.binary_flow(s, node); nk::assign => return this.assign_flow(s, node); nk::ternary => { # A ternary choosing while compiling is the branch the checker chose alone. val chosen := this.checker.chosen_arm(node); return this.value_flow_as(s, chosen, kind) if chosen != 0; this.value_flow(s, this.checker.left_of(node)); val a := this.value_flow_as(s, this.checker.record(node, if_slot::then), kind); val b := this.value_flow_as(s, this.checker.record(node, if_slot::other), kind); return s.graph.either(a, b); } nk::fallback => return this.fallback_flow(s, node); nk::call => return this.call_flow(s, node); nk::index => return this.index_flow(s, node); nk::member, nk::member_of => return this.member_flow(s, node); nk::type_args => return 0; nk::slice => return this.slice_flow(s, node); nk::cast => return this.cast_flow(s, node); nk::is_test => { this.test_flow(s, node); return 0; } nk::construct => return this.construct_flow(s, node); nk::range => { # Its ends are integers, which keep nothing. this.value_flow(s, this.checker.left_of(node)); this.value_flow(s, this.checker.right_of(node)); return 0; } nk::array => return this.array_flow(s, node); nk::lambda => return this.lambda_flow(s, node); nk::match_value => return this.match_flow(s, node, true); # What `e` was is a copy of what it held. nk::old_value => return this.value_flow(s, this.checker.left_of(node)); else => {} } this.unknown_flow(s, node); return this.shared_flow(s, kind); } #! The value a name gives: what a local holds, or what a global does, which any #! code may reach. #! @param this The compiler. #! @param s The scan. #! @param node The `name` or `path` node. #! @returns The set of its value, or 0. def lowerer::name_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val ref := this.checker.ref_at(node); val kind := this.checker.type_at(node); if (ref & ref_local) != 0 { return 0 if !this.holds_references(kind); return s.graph.held_by(this.slot_of(s, ref & ref_mask)); } return 0 if ref == 0 || (ref & ref_type) != 0; return this.shared_flow(s, kind) if this.checker.decls.kinds.p[ref] == dk::variable; return 0; } #! The place a node names, as `emitter::place_of` finds it: a local's storage, #! a field or element within what holds it, or what a global, a pointer, a #! view, or what implements `indexable` reaches; or else a value the statement #! holds. #! @param this The compiler. #! @param s The scan. #! @param node The node. #! @param refers Whether a reference to the place is made. #! @returns The set of the spots it lies within. pub def lowerer::place_flow(&mut this, s: &mut escape_scan, node: u32, refers: bool): u32 { match this.checker.kind_of(node) { nk::name, nk::path => { val ref := this.checker.ref_at(node); if (ref & ref_local) != 0 { return this.local_place(s, ref & ref_mask, refers); } if ref != 0 && (ref & ref_type) == 0 && this.checker.decls.kinds.p[ref] == dk::variable && !this.checker.is_known(node) { return s.graph.alone(s.graph.shared); } } nk::member, nk::member_of => { val ref := this.checker.ref_at(node); if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.checker.decls.kinds.p[ref] == dk::field { return this.object_flow(s, this.checker.left_of(node), refers); } } nk::index => { if !this.through_at(node) { return this.element_flow(s, node, refers); } } nk::call => { val ref := this.checker.ref_at(node); if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.checker.decls.kinds.p[ref] == dk::variable && !this.checker.is_known(node) { return s.graph.alone(s.graph.shared); } } nk::unary => { if this.checker.token_kind(node) == tk::star { this.value_flow(s, this.checker.left_of(node)); return s.graph.alone(s.graph.shared); } } else => {} } return this.temp_place(s, node, this.raw_flow(s, node)); } #! The place a node's value is at, read through every reference it holds, as #! `emitter::object` finds it. #! @param this The compiler. #! @param s The scan. #! @param node The node. #! @param refers Whether a reference into the place is made. #! @returns The set of the spots it lies within. pub def lowerer::object_flow(&mut this, s: &mut escape_scan, node: u32, refers: bool): u32 { val kind := this.checker.type_at(node); return this.place_flow(s, node, refers) if !this.is_reference_type(kind); var at := this.raw_flow(s, node); var held := this.checker.types.first_of(this.checker.unalias(kind)); var steps := 0; while this.is_reference_type(held) && steps < 16 { at = s.graph.read_of(at); held = this.checker.types.first_of(this.checker.unalias(held)); steps++; } return at; } #! The reference a node is, read through the references it refers to until one #! refers to what is no reference, as `emitter::through_one` reads it. #! @param this The compiler. #! @param s The scan. #! @param node The node, of a reference type. #! @returns The set of the reference. def lowerer::through_one_flow(&mut this, s: &mut escape_scan, node: u32): u32 { var at := this.raw_flow(s, node); var held := this.checker.types.first_of(this.checker.unalias(this.checker.type_at(node))); var steps := 0; while this.is_reference_type(held) && steps < 16 { at = s.graph.read_of(at); held = this.checker.types.first_of(this.checker.unalias(held)); steps++; } return at; } #! A reference a parameter is given to what a node gives, where that is no #! reference, as `emitter::reference_arg` makes it: to its place, or to a value #! the statement holds. #! @param this The compiler. #! @param s The scan. #! @param node The node. #! @returns The set of the reference. pub def lowerer::lent_flow(&mut this, s: &mut escape_scan, node: u32): u32 { return this.place_flow(s, node, true) if this.checker.is_place(node); return this.temp_place(s, node, this.raw_flow(s, node)); } #! The reference a method's `this` is given to the value it is called on, as #! `emitter::receiver_of` makes it: the reference the value is, read through, or #! one to the value. #! @param this The compiler. #! @param s The scan. #! @param object The node of the value. #! @returns The set of the reference. def lowerer::receiver_flow(&mut this, s: &mut escape_scan, object: u32): u32 { return this.through_one_flow(s, object) if this.is_reference_type(this.checker.type_at(object)); return this.lent_flow(s, object); } #! `&x`: a reference to a place, or to a value the statement holds, or the #! reference a value already is, as `emitter::reference` makes it. #! @param this The compiler. #! @param s The scan. #! @param node The `reference` node. #! @returns The set of the reference. def lowerer::reference_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val inner := this.checker.left_of(node); val kind := this.checker.type_at(node); if this.checker.has_flag(node, rf::relent) { return this.raw_flow(s, inner); } if this.checker.is_place(inner) && (!this.checker.is_known(inner) || this.stored_place(inner)) { return this.place_flow(s, inner, true); } val referent := this.checker.types.first_of(this.checker.unalias(kind)); return this.temp_place(s, node, this.value_flow_as(s, inner, referent)); } #! Whether a place whose value is known while compiling is still in memory, as #! `emitter::stored` says. #! @param this The compiler. #! @param node The place's node. #! @returns Whether it is. def lowerer::stored_place(&mut this, node: u32): bool { var at := node; while this.checker.kind_of(at) == nk::index || this.checker.kind_of(at) == nk::member { at = this.checker.left_of(at); } val named := this.checker.kind_of(at) == nk::name || this.checker.kind_of(at) == nk::path; return named && at != node && !this.checker.is_known(at); } #! A prefix operator: a method's call where the checker chose one, the value read #! through a pointer, or arithmetic. #! @param this The compiler. #! @param s The scan. #! @param node The `unary` node. #! @returns The set of its value, or 0. def lowerer::unary_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val operand := this.checker.left_of(node); val kind := this.checker.type_at(node); match this.checker.token_kind(node) { tk::minus, tk::bitnot => { val method := this.operator_called(node); if method != 0 { return this.operator_flow(s, node, method, operand, 0); } this.value_flow(s, operand); return 0; } tk::negate => { this.value_flow(s, operand); return 0; } tk::increment, tk::decrement => { this.step_flow(s, operand); return 0; } tk::star => { this.value_flow(s, operand); return this.shared_flow(s, kind); } else => {} } this.unknown_flow(s, node); return this.shared_flow(s, kind); } #! `x++` or `--x`, which change a number or pointer where it is, or a property #! through its accessors. #! @param this The compiler. #! @param s The scan. #! @param target The node of what changes. def lowerer::step_flow(&mut this, s: &mut escape_scan, target: u32): void { val property := this.property_named(target); if property != 0 && this.writer_named(target) == 0 { this.property_flow(s, property, target, false, 0); this.property_flow(s, property, target, true, 0); return; } this.writable_flow(s, target, false); } #! The place an assignment or step writes, as `emitter::writable_place` finds it. #! @param this The compiler. #! @param s The scan. #! @param node The node written. #! @param refers Whether a reference to the place is made, as a compound #! assignment's method is given one. #! @returns The set of the spots it lies within. def lowerer::writable_flow(&mut this, s: &mut escape_scan, node: u32, refers: bool): u32 { return this.is_reference_type(this.checker.type_at(node)) ? this.object_flow(s, node, refers) : this.place_flow(s, node, refers); } #! A binary operator: what `&&` and `||` test, a comparison, which may call #! `equals` or `compare`, a method the checker chose, or arithmetic. #! @param this The compiler. #! @param s The scan. #! @param node The `binary` node. #! @returns The set of its value, or 0. def lowerer::binary_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val operator := this.checker.token_kind(node); val left := this.checker.left_of(node); val right := this.checker.right_of(node); if operator == tk::logand || operator == tk::logor { this.value_flow(s, left); this.value_flow(s, right); return 0; } if operator.compares() { # What says how it compares is given references to both sides, each to its # place or to a value the statement holds. val plain := this.layout_type(this.checker.read_type(this.checker.converted(left))); val named := this.checker.named_kind(plain); val equal := operator == tk::equals || operator == tk::noteq; if (named == dk::struct_type || named == dk::union_type) && (!equal || this.checker.has_method(plain, word::equals)) { val method := this.picked_method(plain, equal ? word::equals : word::compare, true); val summary := this.keeps_of_method(method, plain); val result := s.graph.new_set(); val subject := this.is_reference_type(this.checker.type_at(left)) ? this.raw_flow(s, left) : this.operand_flow(s, left); this.give_flow(s, &summary, 0, subject, true, result); val other := this.is_reference_type(this.checker.type_at(right)) ? this.raw_flow(s, right) : this.operand_flow(s, right); this.give_flow(s, &summary, 1, other, true, result); return 0; } this.value_flow(s, left); this.value_flow(s, right); return 0; } val method := this.operator_called(node); if method != 0 { return this.operator_flow(s, node, method, left, right); } this.value_flow(s, left); this.value_flow(s, right); return 0; } #! A reference to an operand an operator's method is lent, as #! `emitter::lent_arg` makes it: to its place, or to a value the statement holds, #! which it always does. #! @param this The compiler. #! @param s The scan. #! @param node The operand's node. #! @returns The set of the reference. def lowerer::operand_flow(&mut this, s: &mut escape_scan, node: u32): u32 { return this.place_flow(s, node, true) if this.checker.is_place(node); return this.temp_place(s, node, this.value_flow(s, node)); } #! The method of a name a type has, of the overloads the one taking `this` #! and, where `binary` says, another reference to the type, as #! `emitter::pick_method` picks it. #! @param this The compiler. #! @param kind The type. #! @param word The ID of the method's name. #! @param binary Whether it takes another operand. #! @returns The method's declaration, or 0. def lowerer::picked_method(&mut this, kind: u32, word: u32, binary: bool): u32 { val found := this.checker.find_member(kind, word); return 0 if found.decl == 0; var first: u32 = 0; var walk := this.checker.decls.walk_overloads(found.decl); while true { val at := walk.next(); break if at == 0; this.checker.resolve(at); if this.checker.decls.kinds.p[at] == dk::function && this.checker.decls.has(at, df::method) { val held := this.checker.method_type(at, kind); val count := this.checker.types.part_count(held); if (binary && count == 2) || (!binary && count == 1) { if first == 0 { first = at; } return at if !binary; val other := this.checker.types.part_at(held, 1); if this.is_reference_type(other) && this.checker.strip(this.checker.types.first_of(this.checker.unalias(other)), 0) == this.checker.strip(kind, 0) { return at; } } } } return first; } #! The call of the method an arithmetic operator is, given the operand it is #! called on as `this` and the other as its argument, as a call gives them. #! @param this The compiler. #! @param s The scan. #! @param node The operator's node. #! @param method The method's declaration. #! @param left The node of the operand it is called on. #! @param right The node of the other operand, or 0. #! @returns The set of what it gives, or 0. def lowerer::operator_flow(&mut this, s: &mut escape_scan, node: u32, method: u32, left: u32, right: u32): u32 { val instance := this.called(node); var kind := this.type_of_function(method, instance); if this.trait_of_method(method) != 0 { kind = this.checker.trait_method_type(method, this.checker.reached(this.checker.type_at(left))); } val summary := this.keeps_of_call(method, instance, left); val result := this.result_flow(s, this.checker.types.first_of(kind)); this.receive_flow(s, &summary, left, this.checker.types.part_at(kind, 0), result); if right != 0 && this.checker.types.part_count(kind) > 1 { this.argument_flow(s, &summary, 1, right, this.checker.types.part_at(kind, 1), result); } return result; } #! An assignment, as `emitter::assign` lowers it: `_ = x` lets go of `x`, a #! property is written by its `set`, a variable holding a reference is pointed #! elsewhere, and anything else written where it is, through a method's call #! for a compound assignment the checker chose one for. #! @param this The compiler. #! @param s The scan. #! @param node The `assign` node. #! @returns The set of the value assigned, or 0. def lowerer::assign_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val operator := this.checker.token_kind(node); val target := this.checker.left_of(node); val value := this.checker.right_of(node); val kind := this.checker.type_at(node); if operator == tk::assign && this.checker.kind_of(target) == nk::name && this.checker.name_of(target) == names::underscore_id { return this.value_flow(s, value); } val property := this.property_named(target); if property != 0 && this.writer_named(target) == 0 { var stored: u32 = 0; if operator == tk::assign { stored = this.value_flow_as(s, value, kind); } else { val current := this.property_flow(s, property, target, false, 0); stored = this.compound_flow(s, node, current, value, kind); } this.property_flow(s, property, target, true, stored); return stored; } val target_type := this.checker.type_at(target); if operator == tk::assign && this.is_reference_type(target_type) { if !this.checker.has_flag(node, rf::valued) { val made := this.value_flow_as(s, value, target_type); s.graph.write(this.place_flow(s, target, false), made); return made; } # Any other reference writes what it refers to. val at := this.object_once_flow(s, target); val made := this.value_flow_as(s, value, this.checker.types.first_of(this.checker.unalias(target_type))); s.graph.write(at, made); return made; } # Writing a union's field makes the union hold it. val target_kind := this.checker.kind_of(target); if (target_kind == nk::member || target_kind == nk::member_of) && operator == tk::assign { val ref := this.checker.ref_at(target); if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.checker.decls.kinds.p[ref] == dk::field && this.checker.decls.kinds.p[this.checker.decls.owners.p[ref]] == dk::union_type && !this.checker.decls.has(this.checker.decls.owners.p[ref], df::foreign) { val whole := this.object_flow(s, this.checker.left_of(target), false); val made := this.value_flow_as(s, value, target_type); s.graph.write(whole, made); return made; } } val method := this.operator_called(node); val at := this.writable_flow(s, target, operator != tk::assign && method != 0); if operator == tk::assign { val made := this.value_flow_as(s, value, this.writable_type(target)); s.graph.write(at, made); return made; } # What a compound assignment's method gives is written over what was there. if method != 0 { val instance := this.called(node); var called := this.type_of_function(method, instance); if this.trait_of_method(method) != 0 { called = this.checker.trait_method_type(method, this.checker.reached(target_type)); } val summary := this.keeps_of_call(method, instance, target); val result := this.result_flow(s, this.checker.types.first_of(called)); this.give_flow(s, &summary, 0, at, true, result); if this.checker.types.part_count(called) > 1 { this.argument_flow(s, &summary, 1, value, this.checker.types.part_at(called, 1), result); } s.graph.write(at, result); return result; } this.value_flow(s, value); return 0; } #! The type an assignment writes where its target is, read through a reference #! as `emitter::writable_place` reads it. #! @param this The compiler. #! @param target The target's node. #! @returns The type. def lowerer::writable_type(&mut this, target: u32): u32 { var kind := this.checker.type_at(target); var steps := 0; while this.is_reference_type(kind) && steps < 16 { kind = this.checker.types.first_of(this.checker.unalias(kind)); steps++; } return kind; } #! The place a reference refers to, read through one reference, as #! `emitter::object_by(node, 1)` finds it. #! @param this The compiler. #! @param s The scan. #! @param node The node, of a reference type. #! @returns The set of the spots it lies within. def lowerer::object_once_flow(&mut this, s: &mut escape_scan, node: u32): u32 { return this.raw_flow(s, node); } #! The value a compound assignment of a property works out from what the #! property held, through a method's call where the checker chose one, given #! what the property held as a value the statement holds. #! @param this The compiler. #! @param s The scan. #! @param node The `assign` node. #! @param current The set of what the property held, or 0. #! @param value The value's node. #! @param kind The type assigned. #! @returns The set of the value, or 0. def lowerer::compound_flow(&mut this, s: &mut escape_scan, node: u32, current: u32, value: u32, kind: u32): u32 { val method := this.operator_called(node); if method == 0 { this.value_flow(s, value); return 0; } val instance := this.called(node); val called := this.type_of_function(method, instance); val summary := this.keeps_of_call(method, instance, 0); val result := this.result_flow(s, this.checker.types.first_of(called)); this.give_flow(s, &summary, 0, this.temp_place(s, node, current), true, result); if this.checker.types.part_count(called) > 1 { this.argument_flow(s, &summary, 1, value, this.checker.types.part_at(called, 1), result); } return this.converted_flow(s, result, this.checker.types.first_of(called), kind); } #! `value else fallback`: what a `result` or `maybe` holds, or what stands in, a #! value or a block that leaves, with the error a result holds named for it. #! @param this The compiler. #! @param s The scan. #! @param node The `fallback` node. #! @returns The set of the value held, or 0. def lowerer::fallback_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val kind := this.checker.type_at(node); val error := this.checker.record(node, fallback_slot::error); val other := this.checker.record(node, fallback_slot::other); val whole := this.object_flow(s, this.checker.left_of(node), false); val held := s.graph.read_of(whole); if error != 0 { s.graph.into(held, s.graph.held_by(this.slot_of(s, error))); } var given: u32 = 0; if this.checker.kind_of(other) == nk::block { this.scan_block(s, other); } else { given = this.value_flow_as(s, other, kind); } return s.graph.either(this.holds_references(kind) ? held : 0, given); } #! An element's place, as `emitter::element_place` finds it: within a fixed #! array, what a pointer or a view points at, a pack's value, or what `at` #! gives. #! @param this The compiler. #! @param s The scan. #! @param node The `index` node. #! @param refers Whether a reference to the place is made. #! @returns The set of the spots it lies within. def lowerer::element_flow(&mut this, s: &mut escape_scan, node: u32, refers: bool): u32 { val object := this.checker.left_of(node); val at := this.checker.right_of(node); val through := this.checker.unalias(this.checker.reached(this.object_type(object))); match this.checker.types.kind_of(through) { ty::fixed => { val whole := this.object_flow(s, object, refers); this.value_flow(s, at); return whole; } ty::pointer => { this.value_flow(s, object); this.value_flow(s, at); return s.graph.alone(s.graph.shared); } ty::pack => return s.graph.alone(s.graph.shared); else => {} } if this.viewed(through) != 0 { this.object_flow(s, object, false); this.value_flow(s, at); return s.graph.alone(s.graph.shared); } val method := this.checker.ref_at(node); if method != 0 && (method & (ref_local | ref_type)) == 0 && this.checker.decls.kinds.p[method] == dk::function { val made := this.accessor_flow(s, node, method, object, at); return made if this.is_reference_type(this.checker.type_at(node)); return this.temp_place(s, node, made); } this.value_flow(s, object); this.value_flow(s, at); return s.graph.alone(s.graph.shared); } #! The type of what an element or member is reached through, as #! `emitter::object_type` gives it. #! @param this The compiler. #! @param object The node of the value reached through. #! @returns The type. def lowerer::object_type(&mut this, object: u32): u32 { val own := this.checker.type_at(object); return this.checker.types.is_untyped(own) ? this.checker.converted(object) : own; } #! Whether `x[k]` is indexed through `at`, and so is the reference it gives. #! @param this The compiler. #! @param node The `index` node. #! @returns Whether it is. def lowerer::through_at(&mut this, node: u32): bool { val method := this.checker.ref_at(node); return method != 0 && (method & (ref_local | ref_type)) == 0 && this.checker.decls.kinds.p[method] == dk::function && this.is_reference_type(this.checker.type_at(node)); } #! `x[k]` as a value: the reference `at` gives, or what the element's place holds. #! @param this The compiler. #! @param s The scan. #! @param node The `index` node. #! @returns The set of its value, or 0. def lowerer::index_flow(&mut this, s: &mut escape_scan, node: u32): u32 { if this.through_at(node) { val object := this.checker.left_of(node); val through := this.checker.unalias(this.checker.reached(this.object_type(object))); return this.element_flow(s, node, true) if this.viewed(through) != 0; return this.accessor_flow(s, node, this.checker.ref_at(node), object, this.checker.right_of(node)); } val at := this.element_flow(s, node, false); return 0 if !this.holds_references(this.checker.type_at(node)); return s.graph.read_of(at); } #! The call of an accessor the compiler makes itself, as `at`, given the value it #! is called on and its other argument. #! @param this The compiler. #! @param s The scan. #! @param node The node the call stands for. #! @param decl The accessor's declaration. #! @param object The node of the value it is called on. #! @param arg The node of its other argument, or 0. #! @returns The set of what it gives, or 0. def lowerer::accessor_flow(&mut this, s: &mut escape_scan, node: u32, decl: u32, object: u32, arg: u32): u32 { val owner := this.checker.reached(this.object_type(object)); val instance := this.method_instance(decl, owner); val kind := this.method_kind(decl, instance, owner); val summary := this.keeps_of_method(decl, owner); val result := this.result_flow(s, this.checker.types.first_of(kind)); this.receive_flow(s, &summary, object, this.checker.types.part_at(kind, 0), result); if arg != 0 && this.checker.types.part_count(kind) > 1 { val given := this.value_flow_as(s, arg, this.checker.types.part_at(kind, 1)); this.give_flow(s, &summary, 1, given, this.is_reference_type(this.checker.types.part_at(kind, 1)), result); } return result; } #! `x.name` as a value: what a field holds, or what a property's `get` gives. #! @param this The compiler. #! @param s The scan. #! @param node The `member` or `member_of` node. #! @returns The set of its value, or 0. def lowerer::member_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val property := this.property_named(node); if property != 0 { return this.property_flow(s, property, node, false, 0); } val ref := this.checker.ref_at(node); if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.checker.decls.kinds.p[ref] == dk::field { val at := this.object_flow(s, this.checker.left_of(node), false); return 0 if !this.holds_references(this.checker.type_at(node)); return s.graph.read_of(at); } # The length of a fixed array, or the like, is known while compiling, and what # else a member names reads its value, which may be referred to. if this.checker.is_known(node) { return 0; } val object := this.checker.left_of(node); if this.is_reference_type(this.checker.type_at(object)) { s.graph.keep(this.raw_flow(s, object)); } else { s.graph.keep(this.lent_flow(s, object)); } return this.shared_flow(s, this.checker.type_at(node)); } #! The property a member names, or 0. #! @param this The compiler. #! @param node The node. #! @returns The property's declaration, or 0. def lowerer::property_named(&mut this, node: u32): u32 { val kind := this.checker.kind_of(node); return 0 if kind != nk::member && kind != nk::member_of; val ref := this.checker.ref_at(node); return 0 if ref == 0 || (ref & (ref_local | ref_type)) != 0; if this.checker.decls.kinds.p[ref] == dk::function && this.checker.decls.has(ref, df::accessor) { return this.checker.decls.owners.p[ref]; } return this.checker.decls.kinds.p[ref] == dk::property ? ref : 0; } #! The `get mut` a member reads its property through, as `emitter::writer_of` #! finds it, or 0. #! @param this The compiler. #! @param node The node. #! @returns The `get mut`'s declaration, or 0. def lowerer::writer_named(&mut this, node: u32): u32 { val kind := this.checker.kind_of(node); return 0 if kind != nk::member && kind != nk::member_of; val ref := this.checker.ref_at(node); return 0 if ref == 0 || (ref & (ref_local | ref_type)) != 0; return 0 if this.checker.decls.kinds.p[ref] != dk::function || !this.checker.decls.has(ref, df::accessor); return this.checker.decls.has(ref, df::mut_this) && !this.checker.decls.has(ref, df::setter) ? ref : 0; } #! The call of a property's accessor, on the value the member reads it of. #! @param this The compiler. #! @param s The scan. #! @param property The property's declaration. #! @param node The member's node. #! @param setter Whether the `set` is called, rather than the `get`. #! @param value The set of the value `set` is given, or 0. #! @returns The set of what it gives, or 0. def lowerer::property_flow(&mut this, s: &mut escape_scan, property: u32, node: u32, setter: bool, value: u32): u32 { val object := this.checker.left_of(node); val receiver := this.receiver_flow(s, object); var accessor := setter ? this.checker.property_setter(property) : this.writer_named(node); if !setter && accessor == 0 { accessor = this.checker.property_reader(property, false); } if accessor == 0 { s.graph.keep(receiver); s.graph.keep(value); return 0; } val owner := this.checker.reached(this.checker.type_at(object)); val instance := this.method_instance(accessor, owner); val kind := this.method_kind(accessor, instance, owner); val summary := this.keeps_of_method(accessor, owner); val result := this.result_flow(s, this.checker.types.first_of(kind)); this.give_flow(s, &summary, 0, receiver, true, result); if setter && this.checker.types.part_count(kind) > 1 { this.give_flow(s, &summary, 1, value, this.is_reference_type(this.checker.types.part_at(kind, 1)), result); } return result; } #! `x[a:b]`: a view of what is sliced, which is no reference, so what the view #! reaches may be read through it by anyone. What is no view is sliced through #! the call of its `span_of` or `slice_of`. #! @param this The compiler. #! @param s The scan. #! @param node The `slice` node. #! @returns 0, as a view holds no reference, or the set of what the call gives. def lowerer::slice_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val object := this.checker.left_of(node); val chosen := this.checker.ref_at(node); if chosen != 0 && (chosen & (ref_local | ref_type)) == 0 && this.checker.decls.kinds.p[chosen] == dk::function { val owner := this.checker.reached(this.object_type(object)); val instance := this.method_instance(chosen, owner); val kind := this.method_kind(chosen, instance, owner); val summary := this.keeps_of_method(chosen, owner); val result := this.result_flow(s, this.checker.types.first_of(kind)); this.receive_flow(s, &summary, object, this.checker.types.part_at(kind, 0), result); val start := this.checker.record(node, slice_slot::start); val count := this.checker.record(node, slice_slot::count); if start != 0 { this.give_flow(s, &summary, 1, this.value_flow_as(s, start, this.checker.types.part_at(kind, 1)), false, result); } if count != 0 { this.give_flow(s, &summary, 2, this.value_flow_as(s, count, this.checker.types.part_at(kind, 2)), false, result); } return result; } val through := this.checker.unalias(this.checker.reached(this.checker.type_at(object))); if this.checker.types.kind_of(through) == ty::fixed { s.graph.keep(s.graph.read_of(this.object_flow(s, object, false))); } else if this.checker.types.kind_of(through) == ty::pointer { this.value_flow(s, object); } else { this.object_flow(s, object, false); } this.value_flow(s, this.checker.record(node, slice_slot::start)); this.value_flow(s, this.checker.record(node, slice_slot::count)); return 0; } #! `x as T`: a pointer made of a reference keeps nothing alive, but what it #! reaches may be read through it by anyone; anything else converts. #! @param this The compiler. #! @param s The scan. #! @param node The `cast` node. #! @returns The set of the value, or 0. def lowerer::cast_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val inner := this.checker.left_of(node); val to := this.checker.type_at(node); val target := this.layout_type(to); val made := this.value_flow(s, inner); val from := this.checker.converted(inner); val pointer := this.checker.types.kind_of(target) == ty::pointer || this.checker.is_std(target, word::cfn); if pointer { if this.is_reference_type(from) { s.graph.keep(s.graph.read_of(made)); } else { s.graph.keep(made); } return 0; } return made if this.is_reference_type(to); val reads := this.is_reference_type(from); var source := made; var kind := from; var steps := 0; while reads && this.is_reference_type(kind) && steps < 16 { source = s.graph.read_of(source); kind = this.checker.types.first_of(this.checker.unalias(kind)); steps++; } if !this.holds_references(to) { # What converts to a number holds nothing; anything else is taken as kept. if !this.checker.types.is_numeric(this.layout_type(kind)) && !this.checker.types.is_boolean(this.layout_type(kind)) && this.checker.named_kind(this.layout_type(kind)) != dk::enum_type { s.graph.keep(source); } return 0; } return this.converted_flow(s, source, kind, to); } #! `x is field(v)`: the name given holds a copy of what the union holds, or with #! `&` a reference to where it holds it. #! @param this The compiler. #! @param s The scan. #! @param node The `is_test` node. def lowerer::test_flow(&mut this, s: &mut escape_scan, node: u32): void { val field := this.checker.ref_at(node); val subject := this.checker.left_of(node); if field == 0 || (field & (ref_local | ref_type)) != 0 || this.checker.decls.kinds.p[field] != dk::field { return; } val binding := this.checker.named_binding(this.checker.record(node, is_slot::binding)); val refers := binding != 0 && this.checker.kind_of(binding) == nk::reference; val whole := this.object_flow(s, subject, refers); return if binding == 0; val named := refers ? this.checker.left_of(binding) : binding; val slot := this.slot_of(s, named); # `&` of a field holding a reference gives the reference it holds. if refers { s.graph.into(whole, s.graph.held_by(slot)); } if this.holds_references(this.checker.type_at(named)) { s.graph.read(whole, s.graph.held_by(slot)); } } #! A struct or union literal: the value holds what each field is given, and #! what the defaults of those left out hold, which any code may reach. #! @param this The compiler. #! @param s The scan. #! @param node The `construct` node. #! @returns The set of the value, or 0. def lowerer::construct_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val kind := this.checker.type_at(node); val inits := this.checker.right_of(node); val holds := this.holds_references(kind); var made: u32 = holds ? s.graph.new_set() : 0; val plain := this.checker.unalias(kind); for i in 0..this.checker.run_length(inits) { val init := this.checker.run_at(inits, i as u32); val field := this.checker.ref_at(init); val wanted := field != 0 && (field & (ref_local | ref_type)) == 0 ? this.checker.decls.types.p[field] : 0; val value := this.checker.left_of(init); val given := wanted != 0 ? this.value_flow_as(s, value, this.field_type(plain, field)) : this.value_flow(s, value); if holds { s.graph.into(given, made); } else { s.graph.keep(given); } } if holds && this.checker.types.kind_of(plain) == ty::named { val index := this.layout_of(plain); if this.layout_at(index).count as u64 > this.checker.run_length(inits) as u64 { s.graph.add(made, s.graph.shared); } } return made; } #! The type of a field within a struct or union of a type, given its arguments, #! as its layout lays it out. #! @param this The compiler. #! @param kind The struct's or union's type, without aliases. #! @param field The field's declaration. #! @returns The type, or the field's declared type where the layout has none. def lowerer::field_type(&mut this, kind: u32, field: u32): u32 { if this.checker.types.kind_of(kind) == ty::named { val index := this.layout_of(kind); for i in 0..this.layout_at(index).count { val each := this.field_at(index, i as u32); return each.type_id if each.decl == field; } } return this.checker.decls.types.p[field]; } #! An array literal: a fixed array holds what its elements do; a view of one #! is no reference, so what its elements hold may be read through it by anyone. #! @param this The compiler. #! @param s The scan. #! @param node The `array` node. #! @returns The set of the value, or 0. def lowerer::array_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val kind := this.checker.unalias(this.checker.converted(node)); val viewed := this.viewed(kind); val element := viewed != 0 ? viewed : this.checker.types.first_of(kind); val run := this.checker.left_of(node); val holds := viewed == 0 && this.holds_references(kind); var made: u32 = holds ? s.graph.new_set() : 0; for i in 0..this.checker.run_length(run) { val given := this.value_flow_as(s, this.checker.run_at(run, i as u32), element); if holds { s.graph.into(given, made); } else { s.graph.keep(given); } } return made; } #! A match, as a statement or a value: a union's arm names what it holds, a copy #! or with `&` a reference to it; values are compared with the subject, through #! `equals` where the type has one; and a match used as a value gives what its #! arm gives. #! @param this The compiler. #! @param s The scan. #! @param node The `match_stmt` or `match_value` node. #! @param value Whether it is used as a value. #! @returns The set of what it gives, or 0. def lowerer::match_flow(&mut this, s: &mut escape_scan, node: u32, value: bool): u32 { # `match const` is the arm the checker chose alone. if this.checker.is_match_const(node) { val chosen := this.checker.chosen_arm(node); return 0 if chosen == 0; val body := this.checker.right_of(chosen); return this.value_flow_as(s, body, this.checker.type_at(node)) if value; this.scan_block(s, body); return 0; } val subject := this.checker.left_of(node); val arms := this.checker.right_of(node); val kind := value ? this.checker.type_at(node) : bt::void_type; val whole := this.object_flow(s, subject, this.refers_in_arm(node)); val plain := this.checker.unalias(this.checker.reached(this.checker.type_at(subject))); val union_like := this.checker.named_kind(plain) == dk::union_type; # Values compared through `equals` are lent to it, and what it keeps of what # they reach is kept. var compared := keeping {}; val calls := !union_like && this.checker.has_method(plain, word::equals) && this.checker.named_kind(plain) != dk::enum_type && !this.checker.types.is_numeric(plain); if calls { compared = this.keeps_of_method(this.picked_method(plain, word::equals, true), plain); if (compared.kept | compared.reached) != 0 { s.graph.keep(s.graph.read_of(whole)); } } var made: u32 = 0; for i in 0..this.checker.run_length(arms) { val arm := this.checker.run_at(arms, i as u32); val patterns := this.checker.left_of(arm); val body := this.checker.right_of(arm); for j in 0..this.checker.run_length(patterns) { val pattern := this.checker.run_at(patterns, j as u32); if !union_like { val given := this.value_flow(s, pattern); if calls && (compared.kept | compared.reached) != 0 { s.graph.keep(given); } continue; } continue if this.checker.kind_of(pattern) != nk::call; val bound := this.checker.right_of(pattern); continue if this.checker.run_length(bound) != 1; val binding := this.checker.named_binding(this.checker.run_at(bound, 0)); continue if binding == 0; val refers := this.checker.kind_of(binding) == nk::reference; val named := refers ? this.checker.left_of(binding) : binding; val slot := this.slot_of(s, named); # `&` of a field holding a reference gives the reference it holds. if refers { s.graph.into(whole, s.graph.held_by(slot)); } if this.holds_references(this.checker.type_at(named)) { s.graph.read(whole, s.graph.held_by(slot)); } } if value { made = s.graph.either(made, this.value_flow_as(s, body, kind)); } else { this.scan_block(s, body); } } return made; } #! Whether an arm of a match names the value a field holds by `&` or `&mut`, #! which refers to it where the union is. #! @param this The compiler. #! @param node The match's node. #! @returns Whether one does. pub def lowerer::refers_in_arm(&mut this, node: u32): bool { return false if this.checker.is_match_const(node); val arms := this.checker.right_of(node); for i in 0..this.checker.run_length(arms) { val patterns := this.checker.left_of(this.checker.run_at(arms, i as u32)); for j in 0..this.checker.run_length(patterns) { val pattern := this.checker.run_at(patterns, j as u32); continue if this.checker.kind_of(pattern) != nk::call; val given := this.checker.right_of(pattern); if this.checker.run_length(given) == 1 && this.checker.kind_of(this.checker.run_at(given, 0)) == nk::reference { return true; } } } return false; } # Anonymous functions. #! An anonymous function as a value: where it captures locals, its environment, #! which holds a reference to each. Its body is scanned where it is made, as #! what it does with what it captures, it does with the locals themselves; what #! it is given, and what it gives back, are anyone's. #! @param this The compiler. #! @param s The scan. #! @param node The `lambda` node. #! @returns The set of the value, or 0 where it captures nothing. def lowerer::lambda_flow(&mut this, s: &mut escape_scan, node: u32): u32 { var captures := this.captured_by(node); return 0 if captures.length == 0; var env: u32 = 0; if s.envs.get(&node) is some(got) { env = got; } else { env = s.graph.new_spot(spot_kind::env, node, s.within.length as u32); s.envs.set(node, env); } for &each in captures { s.graph.add(s.graph.held_by(env), this.slot_of(s, each)); s.graph.spots[this.slot_of(s, each) as u64].taken = true; } # The body, as a function of its own within this one. val saved_returns := s.returns; val saved_result := s.result; s.within.append(pair_key(this.lowest_node(node), node)); val kind := this.checker.type_at(node); s.returns = s.graph.kept; s.result = this.checker.types.first_of(kind); val params := this.checker.left_of(node); for i in 0..this.checker.run_length(params) { val param := this.checker.run_at(params, i as u32); s.graph.add(s.graph.held_by(this.slot_of(s, param)), s.graph.shared); } this.scan_body(s, this.checker.record(node, lambda_slot::body)); s.within.truncate(); s.returns = saved_returns; s.result = saved_result; return s.graph.alone(env); } #! The locals of the functions around an anonymous function that it names, or #! that one within it names, in the order it first names them, as #! `emitter::captures_of` finds them. #! @param this The compiler. #! @param node The `lambda` node. #! @returns The nodes declaring them. pub def lowerer::captured_by(&mut this, node: u32): vector { var made := vector::::new(); val first := this.lowest_node(node); var walk := node_walk::new(node); while walk.more() { val at := walk.next(); # A contract left out captures nothing. continue if this.checker.contracts_off && this.checker.kind_of(at) == nk::contract; if this.checker.kind_of(at) == nk::name { val ref := this.checker.ref_at(at); if (ref & ref_local) != 0 { val local := ref & ref_mask; if (local < first || local > node) && !made.span.contains(&local) { made.append(local); } } } walk.enter(this.checker.syn(), at); } return made; } #! The first node of what a node was parsed from, which the parser made before #! the node itself, one after another. #! @param this The compiler. #! @param node The node. #! @returns The lowest node under it. pub def lowerer::lowest_node(&mut this, node: u32): u32 { var lowest := node; var walk := node_walk::new(node); while walk.more() { val at := walk.next(); lowest = at < lowest ? at : lowest; walk.enter(this.checker.syn(), at); } return lowest; } # Calls. #! A set for what a call of a result type gives, holding what any code may #! reach, as a function gives back what it found anywhere, or 0 where the type #! holds no reference. #! @param this The compiler. #! @param s The scan. #! @param kind The result type. #! @returns The set, or 0. def lowerer::result_flow(&mut this, s: &mut escape_scan, kind: u32): u32 { return 0 if !this.holds_references(kind); val made := s.graph.new_set(); s.graph.add(made, s.graph.shared); return made; } #! Gives what a parameter is given where a function's summary says: kept, given #! back in what the call gives, or neither; and what it reaches likewise. #! @param this The compiler. #! @param s The scan. #! @param summary The function's summary. #! @param index The parameter's position, `this` first. #! @param given The set of what it is given, or 0. #! @param reference Whether the parameter takes a reference. #! @param result The set of what the call gives, or 0. def lowerer::give_flow(&mut this, s: &mut escape_scan, summary: &keeping, index: u32, given: u32, reference: bool, result: u32): void { return if given == 0; val bit := index < 62 ? 1u << index : 0; val all := index >= 62; if reference { if all || (summary.kept & bit) != 0 { s.graph.keep(given); } else if (summary.given & bit) != 0 { s.graph.into(given, result != 0 ? result : s.graph.kept); } } if all || (summary.reached & bit) != 0 { s.graph.keep(reference ? s.graph.read_of(given) : given); } else if (summary.passed & bit) != 0 { s.graph.into(reference ? s.graph.read_of(given) : given, result != 0 ? result : s.graph.kept); } } #! Gives a method the value it is called on, as `emitter::receiver` does: the #! reference the value is, or one to it. #! @param this The compiler. #! @param s The scan. #! @param summary The method's summary. #! @param object The node of the value. #! @param param The type of `this`. #! @param result The set of what the call gives, or 0. def lowerer::receive_flow(&mut this, s: &mut escape_scan, summary: &keeping, object: u32, param: u32, result: u32): void { val made := this.receiver_flow(s, object); if this.is_reference_type(param) { this.give_flow(s, summary, 0, made, true, result); } else { this.give_flow(s, summary, 0, s.graph.read_of(made), false, result); } } #! Gives a parameter its argument, as `emitter::argument` does: a reference to #! it where the checker says one is taken, or its value as the parameter takes #! it. #! @param this The compiler. #! @param s The scan. #! @param summary The function's summary. #! @param index The parameter's position. #! @param arg The argument's node. #! @param param The parameter's type. #! @param result The set of what the call gives, or 0. def lowerer::argument_flow(&mut this, s: &mut escape_scan, summary: &keeping, index: u32, arg: u32, param: u32, result: u32): void { if this.checker.has_flag(arg, rf::borrowed) { this.give_flow(s, summary, index, this.lent_flow(s, arg), true, result); return; } this.give_flow(s, summary, index, this.value_flow_as(s, arg, param), this.is_reference_type(param), result); } #! A call, as `emitter::call` lowers it: of an intrinsic, of a declared function #! or method, of a method the compiler gives a built-in type, or of a function #! value. #! @param this The compiler. #! @param s The scan. #! @param node The `call` node. #! @returns The set of what it gives, or 0. def lowerer::call_flow(&mut this, s: &mut escape_scan, node: u32): u32 { val callee := this.checker.left_of(node); var decl := this.checker.ref_at(node); val kind := this.checker.type_at(node); if this.checker.has_flag(node, rf::invokes) && decl != 0 { return this.declared_flow(s, node, decl, callee); } if decl == 0 && this.checker.kind_of(callee) == nk::member_of { decl = this.checker.ref_at(callee); } val named := this.checker.kind_of(callee) == nk::name || this.checker.kind_of(callee) == nk::path ? this.checker.ref_at(callee) : 0; if named != 0 && (named & (ref_local | ref_type)) == 0 && this.checker.decls.kinds.p[named] == dk::function && this.checker.decls.has(named, df::intrinsic) { return this.intrinsic_flow(s, node, named); } if decl != 0 && (decl & (ref_local | ref_type)) == 0 && this.checker.decls.kinds.p[decl] == dk::function { return this.intrinsic_flow(s, node, decl) if this.checker.decls.has(decl, df::intrinsic); var object: u32 = 0; val callee_kind := this.checker.kind_of(callee); if (callee_kind == nk::member || callee_kind == nk::member_of) && this.checker.decls.has(decl, df::method) { object = this.checker.left_of(callee); } return this.declared_flow(s, node, decl, object); } # A method the compiler gives a built-in type keeps nothing of its operands, # but `invoke` calls the function value it is called on. if this.checker.kind_of(callee) == nk::member && decl == 0 { val field := this.checker.ref_at(callee); val is_field := field != 0 && (field & (ref_local | ref_type)) == 0 && this.checker.decls.kinds.p[field] == dk::field; if !is_field { val object := this.checker.left_of(callee); if this.checker.name_of(callee) == word::invoke { this.value_flow(s, object); this.keep_arguments(s, node); return this.shared_flow(s, kind); } val reached := this.checker.unalias(this.checker.reached(this.checker.type_at(object))); if this.checker.types.kind_of(reached) == ty::fixed { this.object_flow(s, object, false); val run := this.checker.right_of(node); for i in 0..this.checker.run_length(run) { this.object_flow(s, this.checker.run_at(run, i as u32), false); } return 0; } this.value_flow(s, object); val run := this.checker.right_of(node); for i in 0..this.checker.run_length(run) { this.value_flow(s, this.checker.run_at(run, i as u32)); } return this.shared_flow(s, kind); } } # A function value's body was scanned where it was made, so calling it keeps # nothing of it, but what it is given goes where that body does not say. this.value_flow(s, callee); this.keep_arguments(s, node); return this.shared_flow(s, kind); } #! A call of an intrinsic. Closing a value where it is keeps nothing of it, as #! a local's scope ending doesn't; what the scan knows nothing more of keeps #! what it's given. #! @param this The compiler. #! @param s The scan. #! @param node The `call` node. #! @param decl The intrinsic's declaration. #! @returns The set of what it gives, or 0. def lowerer::intrinsic_flow(&mut this, s: &mut escape_scan, node: u32, decl: u32): u32 { if this.checker.names.text(this.checker.decls.names.p[decl]) == "close_where_it_is" { val run := this.checker.right_of(node); for i in 0..this.checker.run_length(run) { this.value_flow(s, this.checker.run_at(run, i as u32)); } return 0; } return this.unknown_call_flow(s, node); } #! A call of what the scan does not know: what each argument is, or is lent, is #! kept, and what it gives is anyone's. #! @param this The compiler. #! @param s The scan. #! @param node The `call` node. #! @returns The set of what it gives, or 0. def lowerer::unknown_call_flow(&mut this, s: &mut escape_scan, node: u32): u32 { this.keep_arguments(s, node); return this.shared_flow(s, this.checker.type_at(node)); } #! Keeps what each argument of a call is, or is lent. #! @param this The compiler. #! @param s The scan. #! @param node The `call` node. def lowerer::keep_arguments(&mut this, s: &mut escape_scan, node: u32): void { val run := this.checker.right_of(node); for i in 0..this.checker.run_length(run) { val arg := this.checker.run_at(run, i as u32); if this.checker.has_flag(arg, rf::borrowed) { s.graph.keep(this.lent_flow(s, arg)); continue; } if this.checker.kind_of(arg) == nk::unary && this.checker.token_kind(arg) == tk::spread { this.spread_flow(s, arg); continue; } s.graph.keep(this.value_flow(s, arg)); } } #! `...xs`, which spreads a pack's values, or gives a view of a fixed array or #! of a view: what it reaches is kept. #! @param this The compiler. #! @param s The scan. #! @param node The `unary` node. def lowerer::spread_flow(&mut this, s: &mut escape_scan, node: u32): void { val spread := this.checker.left_of(node); val through := this.checker.unalias(this.checker.reached(this.checker.type_at(spread))); if this.checker.types.kind_of(through) == ty::fixed { s.graph.keep(s.graph.read_of(this.object_flow(s, spread, false))); return; } s.graph.keep(this.value_flow(s, spread)); } #! A call of a declared function or method, giving each argument where its #! summary says: the value a method is called on, those written, those a #! variadic parameter takes as a view of them, and those a pack spreads. #! @param this The compiler. #! @param s The scan. #! @param node The `call` node. #! @param decl The function's declaration. #! @param object The node of the value a method is called on, or 0. #! @returns The set of what it gives, or 0. def lowerer::declared_flow(&mut this, s: &mut escape_scan, node: u32, decl: u32, object: u32): u32 { val instance := this.called(node); var kind := this.type_of_function(decl, instance); if this.trait_of_method(decl) != 0 && object != 0 { kind = this.checker.trait_method_type(decl, this.checker.reached(this.checker.type_at(object))); } val summary := this.keeps_of_call(decl, instance, object); val result := this.result_flow(s, this.checker.types.first_of(kind)); var p: u32 = 0; if object != 0 { this.receive_flow(s, &summary, object, this.checker.types.part_at(kind, 0), result); p = 1; } val count := this.checker.types.part_count(kind); val variadic := this.checker.types.second_of(kind) != 0; val stop: u32 = variadic ? count - 1 : count; val run := this.checker.right_of(node); for i in 0..this.checker.run_length(run) { val arg := this.checker.run_at(run, i as u32); if this.checker.kind_of(arg) == nk::unary && this.checker.token_kind(arg) == tk::spread { this.spread_flow(s, arg); p = count; continue; } # What a variadic parameter gathers is held in a view, which is no reference. if p >= stop || p >= count { s.graph.keep(this.value_flow(s, arg)); continue; } this.argument_flow(s, &summary, p, arg, this.checker.types.part_at(kind, p), result); p++; } return result; } #! A call of a method the compiler makes itself on a value it has a reference #! to, as a loop's `next` or `iter`. #! @param this The compiler. #! @param s The scan. #! @param decl The method's declaration, or 0. #! @param owner The type it is called on. #! @param receiver The set of the reference `this` is given. #! @returns The set of what it gives, or 0. def lowerer::method_flow(&mut this, s: &mut escape_scan, decl: u32, owner: u32, receiver: u32): u32 { if decl == 0 { s.graph.keep(receiver); return 0; } val instance := this.method_instance(decl, owner); val kind := this.method_kind(decl, instance, owner); val summary := this.keeps_of_method(decl, owner); val result := this.result_flow(s, this.checker.types.first_of(kind)); if this.is_reference_type(this.checker.types.part_at(kind, 0)) { this.give_flow(s, &summary, 0, receiver, true, result); } else { this.give_flow(s, &summary, 0, s.graph.read_of(receiver), false, result); } return result; }