#! @file symbols.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains what the queries an editor asks of a checked program share: what a #! name is, as `symbol` says, where one is written, as a `spot`, the walk of a #! file that finds what holds each node, and what the name a node writes #! refers to. #! #! The queries read what checking recorded of each node, as the declaration a #! name refers to and the type of each expression, together with the tables the #! check built. They ask the checker nothing that reports, so a query never #! adds a note. import std::containers; module sakura::analyzer; #! What a name is, as an editor offers it to complete and colours it. pub enum symbol : u8 { keyword, #! A keyword, as `def`. namespace, #! A module. function, #! A function, or a function declared through a type. method, #! A method, which takes `this`. variable, #! A variable, or a local. constant, #! A constant, or a variable that is never written, declared outside any function. parameter, #! A parameter. field, #! A field, or a property. struct_type, #! A struct or union. trait_type, #! A trait. enum_type, #! An enum. enum_member, #! A member of an enum. type_name, #! Any other type, as a built-in one or an alias. type_param, #! A type parameter. } #! Where a name is written: its file, and the bytes of its text it covers. pub struct spot { pub var file: u32; #! The index of the file. pub var offset: u32; #! Where the name begins in the file's text. pub var length: u32; #! How many bytes it covers. } #! What a name refers to: a declaration, a local, or a module. struct checker::meant { var decl: u32; #! The declaration, or 0. var file: u32; #! The file of the local. var local: u32; #! The node declaring the local, as a `variable` or `param`, or 0. var named_module: u32; #! The module, where `is_module` says it is one. var is_module: bool; #! Whether it is a module. } #! Whether a name refers to anything. #! @param this What it refers to. #! @returns Whether it does. def checker::meant::found(&this): bool { return this.decl != 0 || this.local != 0 || this.is_module; } #! Whether two names refer to the same thing. #! @param this What one refers to. #! @param other What the other refers to. #! @returns Whether they do. def checker::meant::same(&this, other: &checker::meant): bool { return this.decl == other.decl && this.local == other.local && this.file == other.file && this.is_module == other.is_module && this.named_module == other.named_module; } #! The nodes of a file a walk from its `document` meets, each before the nodes #! it holds, and the node holding each. The nodes a reparse left behind are no #! longer in the tree, and the walk never meets them. struct tree_walk { var order: vector; #! The nodes, in the order the walk meets them. var parents: vector; #! The node holding each, by its index, or 0. } #! The tree of the file, walked from its `document`. #! @param this The checker. #! @param file The file. #! @returns The tree. def checker::tree_of(&this, file: u32): tree_walk { val syntax := &this.units[file].syntax; var made := tree_walk { order = vector::::new(), parents = vector::::new() }; made.parents.resize(syntax.node_count); var todo := vector::::new(); var held := vector::::new(); todo.append(this.units[file].root); while todo.length != 0 { val at: u32 = todo.last; todo.truncate(); made.order.append(at); held.clear(); syntax.children(at, &mut held); var i := held.length; while i > 0 { i--; val child: u32 = held[i]; continue if child as u64 >= syntax.node_count || made.parents[child] != 0; made.parents[child] = at; todo.append(child); } } return made; } #! The token of the file whose text holds the offset, or the count of its #! tokens where none does, as between two. #! @param this The checker. #! @param file The file. #! @param offset The byte of its text. #! @returns The token. def checker::token_at(&this, file: u32, offset: u32): u32 { val syntax := &this.units[file].syntax; val count := syntax.token_count as u32; # The token before the first beginning past the offset is the one that may # hold it. val after := syntax.token_offsets.span.upper_bound(&offset) as u32; return count if after == 0; val token := after - 1; val start: u32 = syntax.token_offsets[token]; return token if offset < start + syntax.token_length(token, this.names); return count; } #! Where the token is written. #! @param this The checker. #! @param file The file. #! @param token The token. #! @returns Its spot. def checker::spot_of(&this, file: u32, token: u32): spot { val syntax := &this.units[file].syntax; return spot { file = file, offset = syntax.token_offsets[token], length = syntax.token_length(token, this.names) }; } #! The text of a token of a file. #! @param this The checker. #! @param file The file. #! @param token The token. #! @returns The text, which views the file's source. def checker::token_text(&this, file: u32, token: u32): strview { val syntax := &this.units[file].syntax; return this.units[file].source.make_lexeme(syntax.token_offsets[token], syntax.token_length(token, this.names)); } #! The text a node of a file is written as, from its first token through its #! last, with each run of spacing made one space, as `std::list`. #! @param this The checker. #! @param file The file. #! @param node The node. #! @returns The text. def checker::written(&this, file: u32, node: u32): string { val syntax := &this.units[file].syntax; var made := string::new(nothing {}); return made if node == 0; val first: u32 = syntax.node_tokens[node]; var last: u32 = syntax.node_end(node); last = last < first ? first : last; val start: u32 = syntax.token_offsets[first]; val end: u32 = syntax.token_offsets[last] + syntax.token_length(last, this.names); val text := this.units[file].source.make_lexeme(start, end - start); var spaced := false; for i in 0..text.length { val byte: u8 = text[i]; if byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r' { spaced = true; continue; } if spaced && made.length != 0 { made.push(' '); } spaced = false; made.push(byte); } return made; } #! A query of a file under way: where checking was before it began, the walk #! of the file's tree, and the declarations of the file's nodes. It closes #! where its scope ends, however the query returns, which puts back where #! checking was. struct checker::query : owning { var checker: &mut checker; #! The checker queried. var saved: checker::frame; #! Where checking was, which closing puts back. var walked: tree_walk; #! The file's tree. var decls: dict; #! The declarations each node of the file declares, by the node. var open: bool; #! Whether it is yet to close. } #! Begins a query of the file: its results are what is read, nothing the #! checker works out depends on the query, and nothing it reports is noted, #! until the query closes. #! @param this The checker. #! @param file The file. #! @returns The query, holding the file's tree and the declarations of its #! nodes. def checker::begin_query(&mut this, file: u32): checker::query { val saved := this.at; this.use_table(this.units[file].table); this.at.decl = 0; this.at.instance = 0; this.at.scope = 0; this.at.home = 0; this.quiet++; this.owned_names = true; return checker::query { checker = this, saved = saved, walked = this.tree_of(file), decls = this.decls_in(file), open = true }; } #! Ends the query, putting back where checking was, once. #! @param this The query. def checker::query::close(&mut this): void { return if !this.open; this.open = false; this.checker.quiet--; this.checker.owned_names = this.checker.quiet != 0 && this.checker.owned_names; this.checker.at = this.saved; close(&mut this.walked); close(&mut this.decls); } #! Moves the query to another file, whose results are read from then on. #! @param this The checker. #! @param file The file. def checker::query_file(&mut this, file: u32): void { this.use_table(this.units[file].table); this.at.decl = 0; this.at.instance = 0; } #! The module the declarations a file makes before the offset are in: that of #! the last `module` before it, or the program's own. #! @param this The checker. #! @param file The file. #! @param offset The byte of its text. #! @returns The module. def checker::home_at(&mut this, file: u32, offset: u32): u32 { val syntax := &this.units[file].syntax; val decls := syntax.node_lefts[this.units[file].root]; var home: u32 = 0; for &node in syntax.run_view(decls) { break if syntax.token_offsets[syntax.node_tokens[node]] > offset; continue if syntax.node_kinds[node] != nk::module_decl && syntax.node_kinds[node] != nk::extern_module; val found := this.module_named(file, syntax.node_lefts[node], 0xFFFFFFFF); if found != 0 { home = found; } } return home; } #! The module a `path` of a file names from the program's own, through its #! segment at `last`, or 0 where it names none. #! @param this The checker. #! @param file The file. #! @param path The `path` node. #! @param last The position of the last segment read, or more for all. #! @returns The module, or 0. def checker::module_named(&this, file: u32, path: u32, last: u32): u32 { val syntax := &this.units[file].syntax; val run: u32 = syntax.node_lefts[path]; if run == 0 { return this.modules.find(0, syntax.token_names[syntax.node_tokens[path]]); } var at: u32 = 0; for i in 0..syntax.run_length(run) { break if i as u32 > last; val segment := syntax.run_at(run, i as u32); at = this.modules.find(at, syntax.token_names[syntax.node_tokens[segment]]); return 0 if at == 0; } return at; } #! The declarations each node of a file declares, by the node. #! @param this The checker. #! @param file The file. #! @returns The declarations, by their nodes. def checker::decls_in(&this, file: u32): dict { var made := dict::::new(); for &id in this.decls.by_file.of(file) { continue if this.decls.kinds.p[id] == dk::none || this.decls.kinds.p[id] == dk::builtin; made.insert(this.decls.nodes.p[id], id); } return made; } #! What a declaration a name refers to stands for: a property for its accessors, #! which are renamed and shown as one. #! @param this The checker. #! @param decl The declaration. #! @returns What it stands for. def checker::standing_for(&this, decl: u32): u32 { if decl != 0 && this.decls.kinds.p[decl] == dk::function && this.decls.has(decl, df::accessor) { return this.decls.owners.p[decl]; } return decl; } #! What a reference a node records refers to. #! @param this The checker. #! @param ref The reference, as `ref_at` gives it. #! @returns What it refers to. def checker::meant_by(&this, ref: u32): checker::meant { return checker::meant {} if ref == 0; if (ref & ref_local) != 0 { return checker::meant { file = this.at.file, local = ref & ref_mask }; } if (ref & ref_type) != 0 { # A pack whose types are not known is of the pack parameter it is. val kind := ref & ref_mask; val decl := this.type_decl(kind); if decl == 0 && this.types.kind_of(kind) == ty::pack && this.types.part_count(kind) == 0 { return checker::meant { decl = this.types.first_of(kind) }; } return checker::meant { decl = decl }; } return checker::meant {} if ref >= this.decls.count as u32; return checker::meant { decl = this.standing_for(ref) }; } #! What the name a node of the file being queried writes refers to, or nothing #! where it writes none, or one checking left unresolved. #! @param this The checker. #! @param node The node. #! @param walked The file's tree. #! @param decls The declarations of the file's nodes. #! @returns What it refers to. def checker::meaning_of(&mut this, node: u32, walked: &tree_walk, decls: &dict): checker::meant { val parent: u32 = walked.parents[node]; match this.kind_of(node) { nk::name => { val ref := this.ref_at(node); return this.meant_by(ref) if ref != 0; # A name a pattern binds, or a loop or `else` names, declares it. if this.has_flag(node, rf::local) { return checker::meant { file = this.at.file, local = node }; } # A field a pattern names, as `number` of `number(n)`, which the pattern's # call refers to. if parent != 0 && this.kind_of(parent) == nk::call && this.left_of(parent) == node { val held := this.ref_at(parent); if held != 0 && (held & (ref_local | ref_type)) == 0 && held < this.decls.count as u32 && this.decls.kinds.p[held] == dk::field { return checker::meant { decl = held }; } } return checker::meant {}; } nk::segment => return this.segment_meaning(parent, node, walked, decls); nk::type_name, nk::path => { # A name of one segment is its own segment, and one of more is its # segments', which the walk meets after it. return checker::meant {} if this.left_of(node) != 0; return this.segment_meaning(node, node, walked, decls); } nk::member, nk::member_of => { val ref := this.ref_at(node); return this.meant_by(ref) if ref != 0; return checker::meant {} if this.kind_of(node) == nk::member_of; return this.member_meaning(this.type_at(this.left_of(node)), this.name_of(node)); } nk::field_init => { return checker::meant {} if !this.node_has(node, nf::named); return this.meant_by(this.ref_at(node)); } nk::rename => { # The name a spread renames is a field of the struct spread. return checker::meant {} if parent == 0 || this.kind_of(parent) != nk::spread; val spread := this.type_name_meaning(this.left_of(parent)); return checker::meant {} if spread.decl == 0; return this.member_meaning(this.decls.types.p[spread.decl], this.name_of(node)); } nk::function, nk::struct_decl, nk::union_decl, nk::trait_decl, nk::alias, nk::enumeration, nk::enumerator, nk::field, nk::property, nk::assoc_type, nk::type_param, nk::const_param => { if decls.get(&node) is some(found) { return checker::meant { decl = this.standing_for(found) }; } return checker::meant {}; } nk::variable => { if decls.get(&node) is some(found) { return checker::meant { decl = found }; } return checker::meant { file = this.at.file, local = node } if this.has_flag(node, rf::local); return checker::meant {}; } nk::param => { # A parameter an accessor implies has no name of its own written, and its # token is the `get` or `set`. return checker::meant {} if this.node_has(node, nf::implied); # A parameter of a function without a body is no local, but is still # the function's. return checker::meant { file = this.at.file, local = node }; } else => return checker::meant {}; } return checker::meant {}; } #! What a segment of a `type_name`, `path`, `import`, or `module` names: the #! last what the whole names, and those before it the modules and types it is #! within. #! @param this The checker. #! @param holder The `type_name` or `path` node holding the segment. #! @param segment The segment, which is the holder itself where it has one. #! @param walked The file's tree. #! @param decls The declarations of the file's nodes. #! @returns What it names. def checker::segment_meaning(&mut this, holder: u32, segment: u32, walked: &tree_walk, decls: &dict): checker::meant { return checker::meant {} if holder == 0; val ref := this.ref_at(segment); if ref != 0 { return this.meant_by(ref); } # The position of the segment in the holder. val run := this.left_of(holder); var index: u32 = 0; for i in 0..(run == 0 ? 0 : this.run_length(run)) { if this.run_at(run, i as u32) == segment { index = i as u32; } } val last := run == 0 || index + 1 == this.run_length(run); # A path an `import` or `module` names is of modules alone. val outer: u32 = walked.parents[holder]; if outer != 0 && (this.kind_of(outer) == nk::import_decl || this.kind_of(outer) == nk::module_decl || this.kind_of(outer) == nk::extern_module) { val found := this.module_named(this.at.file, holder, index); return checker::meant { named_module = found, is_module = true } if found != 0; return checker::meant {}; } # The type a declaration is made through, as `rect` of `def rect::area`, is # what it is a member of, and a segment before the last, what that is # declared through in turn. if outer != 0 && this.is_receiving(outer) && this.record(outer, decl_slot::receiver) == holder { if decls.get(&outer) is some(found) { var at: u32 = this.decls.owners.p[found]; if this.decls.kinds.p[at] == dk::property { at = this.decls.owners.p[at]; } val count := run == 0 ? 1 : this.run_length(run); var steps := count - 1 - index; while steps > 0 && at != 0 { at = this.decls.owners.p[at]; steps--; } return checker::meant { decl = at } if at != 0; } } # A whole path refers to what its node says, and its last segment with it. if last && segment != holder { val whole := this.ref_at(holder); if whole != 0 { return this.meant_by(whole); } } # After `is`, a name that is no type is the field of the union tested, which # the test refers to. if outer != 0 && this.kind_of(outer) == nk::is_test && this.record(outer, is_slot::target) == holder && index == 0 { val held := this.ref_at(outer); if held != 0 && (held & (ref_local | ref_type)) == 0 && held < this.decls.count as u32 { return checker::meant { decl = held }; } } # A segment naming no declaration may name a module, from where the file is. if !last || run != 0 { val name := this.name_of(segment); var home := this.home_at(this.at.file, this.units[this.at.file].syntax.token_offsets[this.token_of(segment)]); if index == 0 { val saved := this.at.home; this.at.home = home; val found := this.lookup_module(name); this.at.home = saved; return checker::meant { named_module = found, is_module = true } if found != 0; return checker::meant {}; } val before := this.segment_meaning(holder, this.run_at(run, index - 1), walked, decls); if before.is_module { val found := this.modules.find(before.named_module, name); return checker::meant { named_module = found, is_module = true } if found != 0; } } return checker::meant {}; } #! What a `type_name` names, as its last segment refers to it. #! @param this The checker. #! @param node The `type_name` node. #! @returns What it names. def checker::type_name_meaning(&mut this, node: u32): checker::meant { return checker::meant {} if node == 0 || this.kind_of(node) != nk::type_name; val run := this.left_of(node); val last := run == 0 ? node : this.run_at(run, this.run_length(run) - 1); return this.meant_by(this.ref_at(last)); } #! Whether a node declares what may be made through a type, as `rect` of #! `def rect::area`, which its record's second slot names. #! @param this The checker. #! @param node The node. #! @returns Whether it does. def checker::is_receiving(&this, node: u32): bool { return match this.kind_of(node) { nk::function, nk::variable, nk::property, nk::struct_decl, nk::union_decl, nk::trait_decl, nk::enumeration, nk::alias => true, else => false }; } #! The member of a type a name reaches after `.`, as a field, a property, or a #! method, or nothing where the type has none of the name. #! @param this The checker. #! @param kind The type of the value. #! @param name The ID of the name. #! @returns What the name refers to. def checker::member_meaning(&mut this, kind: u32, name: u32): checker::meant { return checker::meant {} if kind == 0 || kind == bt::error; val found := this.find_member(this.reached(kind), name); return checker::meant {} if found.decl == 0; return checker::meant { decl = this.standing_for(found.decl) }; } #! What a declaration is, as `symbol` says. #! @param this The checker. #! @param decl The declaration. #! @returns What it is. def checker::symbol_of(&mut this, decl: u32): symbol { val kind: dk = this.decls.kinds.p[decl]; if kind == dk::function { # A property is read and written by its name alone, so it is offered and # coloured as a field is rather than as the methods its accessors are. return symbol::field if this.decls.has(decl, df::accessor); return this.decls.owners.p[decl] != 0 && this.decls.has(decl, df::method) ? symbol::method : symbol::function; } if kind == dk::type_param { return this.decls.has(decl, df::valued) ? symbol::constant : symbol::type_param; } if kind == dk::variable { return this.decls.has(decl, df::mutable) ? symbol::variable : symbol::constant; } return match kind { dk::property => symbol::field, dk::field => symbol::field, dk::attribute => symbol::constant, dk::enum_member => symbol::enum_member, dk::struct_type, dk::union_type => symbol::struct_type, dk::trait_type => symbol::trait_type, dk::enum_type => symbol::enum_type, else => symbol::type_name }; } #! What a name refers to is, as `symbol` says. #! @param this The checker. #! @param meant What it refers to. #! @returns What it is. def checker::symbol_meant(&mut this, meant: &checker::meant): symbol { return symbol::namespace if meant.is_module; return this.symbol_of(meant.decl) if meant.decl != 0; val kind := this.units[meant.file].syntax.node_kinds[meant.local]; return kind == nk::param ? symbol::parameter : symbol::variable; } #! Whether what a name refers to is never written, as a `val`, a `const`, or a #! member of an enum. #! @param this The checker. #! @param meant What it refers to. #! @returns Whether it is. def checker::readonly_meant(&mut this, meant: &checker::meant): bool { return true if meant.is_module; if meant.decl != 0 { return match this.decls.kinds.p[meant.decl] { dk::variable, dk::field, dk::attribute => !this.decls.has(meant.decl, df::mutable), dk::enum_member => true, dk::property => this.property_setter(meant.decl) == 0, dk::type_param => true, else => false }; } # A parameter is a binding the body writes, so it is not marked read-only. val syntax := &this.units[meant.file].syntax; return false if syntax.node_kinds[meant.local] == nk::param; val saved := this.at; this.use_table(this.units[meant.file].table); val writes := this.has_flag(meant.local, rf::mutable); this.at = saved; return !writes; }