#! @file completion.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains completion: the names that may be written at a position. After #! `value.` they are the members of the value, after `path::` what the module #! or type the path names declares, and otherwise what the place takes, then #! the locals, declarations, modules, and keywords in scope there. import std::containers; module sakura::analyzer; #! A name that may be written at a position. pub struct completion { pub var name: string; #! The name to write. pub var kind: symbol; #! What it is. pub var signature: string; #! How it is declared, as `hover` shows it, or empty. pub var documentation: string; #! Its documentation comment, or empty. pub var insert: string; #! What to write where it is more than the name, as `w = ` for a field of a struct literal or `red => ` for an arm of a match, or empty. } #! The kind of place a name is written in, which says what is offered there. enum place_kind : u8 { anywhere, type_only, #! A type: after the `:` of a declaration or bound, `as`, the `&` of a type, or `::<`. imported, #! A module, after `import`. field, #! A field a struct literal gives, after its `{` or a `,`. arm, #! The pattern an arm of a match begins with. test, #! What `is` tests for: a field of a union, or a trait. } #! Where a name is completed: the kind of place, and what says more of it. struct completing { var kind: place_kind; var opening: u32; #! Of a field, the `{` of the literal; of an arm, the `match`; of a test, the `is`. A token. var written: vector; #! The names of the fields the literal gives, or of the patterns the match has, already. } #! What a completion gathers as it goes. struct offering { var items: vector; #! The names offered, in order. var fits: vector; #! Of each, whether its value is of the type expected. var seen: hashset; #! The names offered already, each once. var types: bool; #! Whether only what names a type is offered. var expected: u32; #! The type of the argument being written, or 0. } #! The keywords offered where a name may be written. const completion_keywords: strview[34] = [ "var", "val", "mut", "def", "return", "if", "else", "for", "in", "while", "break", "continue", "match", "when", "where", "as", "is", "not", "true", "false", "null", "type", "struct", "union", "trait", "enum", "const", "pub", "import", "module", "extern", "expects", "asserts", "ensures" ]; #! The names that may be written at the offset of the file, in the order they #! are found, so locals come before what is declared further away. They are not #! filtered by what is already written of the name being completed. A member #! name must follow `.` for the value before it to parse, so an editor completing #! right after `.` checks the text with a name written there. #! @param this The checker. #! @param file The file. #! @param offset The byte of its text. #! @returns The names. pub def checker::complete(&mut this, file: u32, offset: u32): vector { var offer := offering { items = vector::::new(), fits = vector::::new(), seen = hashset::::new() }; val text := this.units[file].source.make_lexeme(0, this.units[file].source.length); return move(&mut offer.items) if offset as u64 > text.length || this.in_text(text, offset); var start := offset; while start > 0 && is_word_byte(text[(start - 1) as u64]) { start--; } val asked := this.begin_query(file); this.at.home = this.home_at(file, start); # After `value.`, the members of the value's type, but not after `..` of a range. if start >= 1 && text[(start - 1) as u64] == '.' && (start < 2 || text[(start - 2) as u64] != '.') { for &node in asked.walked.order { continue if this.kind_of(node) != nk::member; continue if this.units[file].syntax.token_offsets[this.token_of(node)] != start; this.offer_value(&mut offer, this.type_at(this.left_of(node))); break; } return move(&mut offer.items); } # After `path::`, what the module or type the path names declares. if start >= 2 && text[(start - 1) as u64] == ':' && text[(start - 2) as u64] == ':' { this.offer_path(&mut offer, file, start, text); return move(&mut offer.items); } # Otherwise what the place the name is written in takes: a module after # `import`, a type where one is written, the fields a struct literal has yet to # give, the members of an enum or fields of a union an arm of a match has yet # to handle, and after `is`, the fields of the union tested or a trait. var site := this.place_of_completion(file, start); match site.kind { place_kind::imported => { this.offer_modules(&mut offer, 0); return move(&mut offer.items); } place_kind::field => { val made := this.literal_type(file, site.opening, &asked.walked); if made != 0 { val run: u32 = this.decls.children.p[made]; for i in 0..this.decls.list_length(run) { val field := this.decls.list_at(run, i as u32); continue if this.decls.kinds.p[field] != dk::field || this.is_written(&site, this.decls.names.p[field]); continue if !this.visible(field); val index := offer.items.length; this.offer_decl(&mut offer, field); this.give_insert(&mut offer, index, " = "); } } } place_kind::arm => { val subject := this.subject_type(file, site.opening, &asked.walked); if subject != 0 { val decl := this.types.first_of(subject); val run: u32 = this.decls.children.p[decl]; for i in 0..this.decls.list_length(run) { val member := this.decls.list_at(run, i as u32); val kind: dk = this.decls.kinds.p[member]; continue if (kind != dk::field && kind != dk::enum_member) || this.is_written(&site, this.decls.names.p[member]); val index := offer.items.length; this.offer_decl(&mut offer, member); this.give_insert(&mut offer, index, " => "); } val index := offer.items.length; this.offer_name(&mut offer, "else", symbol::keyword, "", ""); this.give_insert(&mut offer, index, " => "); return move(&mut offer.items); } } place_kind::test => { val tested := this.tested_union(file, site.opening, &asked.walked); if tested != 0 { val run: u32 = this.decls.children.p[this.types.first_of(tested)]; for i in 0..this.decls.list_length(run) { val field := this.decls.list_at(run, i as u32); continue if this.decls.kinds.p[field] != dk::field; this.offer_decl(&mut offer, field); } } offer.types = true; } place_kind::type_only => offer.types = true; place_kind::anywhere => { # Within a call, the names whose values are of the argument's type come first. offer.expected = this.argument_type(file, start, &asked.walked, &asked.decls); } } # Then the names in scope: locals first, then what the types around the place # give, what is declared in the module, imported, and built in, then modules # and keywords. this.offer_locals(&mut offer, file, start, &asked.walked); this.offer_scope(&mut offer, file, start, &asked.walked, &asked.decls); this.offer_module_decls(&mut offer, this.at.home, true); # The program's own modules come before the standard library, and the built-in # types between them, since they are what a type is most often written as. val imports: u32 = this.units[file].imports; for pass in 0..2 { for i in 0..this.decls.list_length(imports) { val imported := this.decls.list_at(imports, i as u32); continue if this.in_library(imported) != (pass == 1); this.offer_module_decls(&mut offer, imported, false); } # Each name of a built-in type is offered, as `int` and `i64` of one type, # each name and type copied out first, since offering one changes the # checker a loop over its built-in types would lock. if pass == 0 { var built := vector::::new(); for &each in this.builtins { built.append(pair_key(each.key, each.value)); } for &each in built { val signature := this.signature_of(pair_low(each)); this.offer_name(&mut offer, this.names.text(pair_high(each)), symbol::type_name, signature.span, ""); } } } # Then `core`'s root, which every file sees, after what the file imports. val core := this.modules.find(0, word::core); if core != 0 { this.offer_module_decls(&mut offer, core, this.modules.within(this.at.home, core)); } this.offer_modules(&mut offer, 0); for &each in this.decls.list_view(imports) { this.offer_module(&mut offer, each); } # Where a type is written, `def` begins a function type and `mut` follows `&`. val types := offer.types; offer.types = false; for &keyword in completion_keywords { continue if types && keyword != "def" && keyword != "mut"; this.offer_name(&mut offer, keyword, symbol::keyword, "", ""); } # The names of the argument's type are put first, each kind keeping its order. if offer.expected != 0 { var ordered := vector::::new(); for i in 0..offer.items.length { if offer.fits[i] { ordered.append(move(&mut offer.items[i])); } } for i in 0..offer.items.length { if !offer.fits[i] { ordered.append(move(&mut offer.items[i])); } } return ordered; } return move(&mut offer.items); } #! Whether the offset is within a comment or text on its line, where no names #! are completed. #! @param this The checker. #! @param text The file's text. #! @param offset The byte of the text. #! @returns Whether it is. def checker::in_text(&this, text: strview, offset: u32): bool { var line := offset; while line > 0 && text[(line - 1) as u64] != '\n' { line--; } var quote: u8 = 0; var at := line; while at < offset { val byte: u8 = text[at as u64]; if quote != 0 { if byte == '\\' { at++; } else if byte == quote { quote = 0; } } else if byte == '"' || byte == '\'' { quote = byte; } else if byte == '#' { return true; } at++; } return quote != 0; } #! Offers a name, once, where the place takes it. #! @param this The checker. #! @param offer What is offered so far. #! @param name The name. #! @param kind What it is. #! @param signature How it is declared. #! @param documentation Its documentation. def checker::offer_name(&this, offer: &mut offering, name: strview, kind: symbol, signature: strview, documentation: strview): void { return if name.length == 0 || name == "_"; # Where a type is written, only what names one is offered. if offer.types && kind != symbol::struct_type && kind != symbol::trait_type && kind != symbol::enum_type && kind != symbol::type_name && kind != symbol::type_param && kind != symbol::namespace { return; } var key := string::from(name, nothing {}); return if !offer.seen.insert(move(&mut key)); offer.items.append(completion { name = string::from(name, nothing {}), kind = kind, signature = string::from(signature, nothing {}), documentation = string::from(documentation, nothing {}), insert = string::new(nothing {}) }); offer.fits.append(false); } #! Offers a declaration, and says whether its value is of the type of the #! argument being written: a variable, constant, or member of an enum of the #! type, or a function giving one. #! @param this The checker. #! @param offer What is offered so far. #! @param decl The declaration. def checker::offer_decl(&mut this, offer: &mut offering, decl: u32): void { return if decl == 0 || this.decls.kinds.p[decl] == dk::none || this.decls.has(decl, df::failed); val shown := this.standing_for(decl); val index := offer.items.length; val file: u32 = this.decls.files.p[shown]; val signature := this.signature_of(shown); val documentation := this.documentation_of(file, this.decls.nodes.p[shown]); this.offer_name(offer, this.decl_text(shown), this.symbol_of(shown), signature.span, documentation.span); return if offer.expected == 0 || offer.items.length == index; var have: u32 = 0; match this.decls.kinds.p[shown] { dk::variable, dk::enum_member, dk::field => have = this.decls.types.p[shown]; dk::function => { val kind: u32 = this.decls.types.p[shown]; have = kind != 0 && this.types.kind_of(kind) == ty::function ? this.types.first_of(kind) : 0; } else => {} } var want := offer.expected; have = have == 0 ? 0 : this.reached(have); want = this.reached(want); offer.fits[index] = have != 0 && have != bt::error && (have == want || (this.type_decl(have) != 0 && this.type_decl(have) == this.type_decl(want) && this.types.kind_of(have) == this.types.kind_of(want))); } #! Offers a local, by the node declaring it. #! @param this The checker. #! @param offer What is offered so far. #! @param file The file. #! @param node The node declaring it. def checker::offer_local(&mut this, offer: &mut offering, file: u32, node: u32): void { val syntax := &this.units[file].syntax; val kind := syntax.node_kinds[node] == nk::param ? symbol::parameter : symbol::variable; val index := offer.items.length; val signature := this.local_signature(file, node); val documentation := this.documentation_of(file, node); this.offer_name(offer, this.local_text(file, node), kind, signature.span, documentation.span); this.query_file(file); return if offer.expected == 0 || offer.items.length == index; val have := this.reached(this.type_at(node)); val want := this.reached(offer.expected); offer.fits[index] = have != 0 && have != bt::error && (have == want || (this.type_decl(have) != 0 && this.type_decl(have) == this.type_decl(want))); } #! Gives the item offered at the index a text to write of its own, its name and #! then `suffix`, as `w = `. #! @param this The checker. #! @param offer What is offered so far. #! @param index The item. #! @param suffix What follows its name. def checker::give_insert(&this, offer: &mut offering, index: u64, suffix: strview): void { return if index >= offer.items.length; var made := string::copy_of(&offer.items[index].name); made.append(suffix); offer.items[index].insert = move(&mut made); } #! Offers a module, by the last segment of its path. #! @param this The checker. #! @param offer What is offered so far. #! @param id The module. def checker::offer_module(&this, offer: &mut offering, id: u32): void { return if id == 0; val signature := this.module_signature(id); this.offer_name(offer, this.names.text(this.modules.names[id]), symbol::namespace, signature.span, ""); } #! Offers the modules within a module. #! @param this The checker. #! @param offer What is offered so far. #! @param parent The module. def checker::offer_modules(&this, offer: &mut offering, parent: u32): void { for id in 1..this.modules.names.length { if this.modules.parents[id] == parent && this.declares_any(id as u32) { this.offer_module(offer, id as u32); } } } #! Whether a module, or one within it, declares anything for the target, as #! the runtime of executables does not for the machine. #! @param this The checker. #! @param id The module. #! @returns Whether it does. def checker::declares_any(&this, id: u32): bool { for decl in 1..this.decls.count { continue if this.decls.kinds.p[decl] == dk::none; return true if this.modules.within(this.decls.modules.p[decl], id); } return false; } #! Offers what a module declares that the place can name: all of it within the #! module or one it holds, and else only what it declares `pub`. #! @param this The checker. #! @param offer What is offered so far. #! @param id The module. #! @param own Whether it is the module the place is in. def checker::offer_module_decls(&mut this, offer: &mut offering, id: u32, own: bool): void { # Offering may make declarations, as of attributes, which the module may list, # so what it lists is copied first. var listed := vector::::new(); listed.extend(this.decls.by_module.of(id)); for &decl in listed { continue if this.decls.modules.p[decl] != id || this.decls.owners.p[decl] != 0; match this.decls.kinds.p[decl] { dk::function, dk::variable, dk::struct_type, dk::union_type, dk::enum_type, dk::trait_type, dk::alias_type => {} else => continue; } continue if this.modules.decl(id, this.decls.names.p[decl]) == 0; continue if !own && !this.visible(decl); this.offer_decl(offer, decl); } } #! Offers what the types around the place give its names, as `kind` within #! `token` is `token::kind`: their nested types and associated variables, and #! an enum's members. #! @param this The checker. #! @param offer What is offered so far. #! @param file The file. #! @param offset The byte of its text. #! @param walked The file's tree. #! @param decls The declarations of the file's nodes. def checker::offer_scope(&mut this, offer: &mut offering, file: u32, offset: u32, walked: &tree_walk, decls: &dict): void { # The top-level declaration holding the place. var top: u32 = 0; val syntax := &this.units[file].syntax; val run := syntax.node_lefts[this.units[file].root]; for &node in syntax.run_view(run) { break if syntax.token_offsets[syntax.start_of(node)] > offset; top = node; } return if top == 0; var held: u32 = 0; if decls.get(&top) is some(found) { held = found; } return if held == 0; var kind := this.scope_of(held, 0); var steps := 0; while kind != 0 && steps < 16 { steps++; val holder := this.type_decl(kind); break if holder == 0; var given := vector::::new(); val nested := &this.decls.nested; for at in 0..nested.capacity { val key := nested.key_at(at); if key != keymap::free && pair_high(key) == holder { given.append(nested.value_at(at)); } } val members := &this.decls.members; for at in 0..members.capacity { val key := members.key_at(at); continue if key == keymap::free || pair_high(key) != holder; val member := members.value_at(at); val member_kind: dk = this.decls.kinds.p[member]; if member_kind == dk::variable || member_kind == dk::enum_member { given.append(member); } } for &each in given { if this.visible(each) { this.offer_decl(offer, each); } } val outer: u32 = this.decls.owners.p[holder]; break if outer == 0 || !this.gives_scope(outer); kind = this.decls.types.p[outer]; } } #! Offers what may follow `value.`: fields, properties, and methods, through #! references and aliases, and the methods a type parameter's bounds give. #! @param this The checker. #! @param offer What is offered so far. #! @param kind The value's type. def checker::offer_value(&mut this, offer: &mut offering, kind: u32): void { return if kind == 0 || kind == bt::error; var at := this.reached(kind); if this.types.kind_of(at) == ty::pointer { at = this.types.first_of(at); } var steps := 0; while steps < 16 { steps++; if this.types.kind_of(at) == ty::param { # What its bounds ask for, and within a trait, `self` has the trait's. val param := this.types.first_of(at); val run: u32 = this.decls.children.p[param]; for i in 0..this.decls.list_length(run) { this.offer_trait(offer, this.types.first_of(this.decls.list_at(run, i as u32)), 0); } val holder: u32 = this.decls.owners.p[param]; if this.decls.names.p[param] == names::self_id && holder != 0 && this.decls.kinds.p[holder] == dk::trait_type { this.offer_trait(offer, holder, 0); } return; } val decl := this.type_decl(at); if decl != 0 { if this.decls.kinds.p[decl] == dk::struct_type || this.decls.kinds.p[decl] == dk::union_type { this.offer_fields(offer, decl, 0); } this.offer_members(offer, decl, true); # The methods the traits it implements write for it. if this.decls.kinds.p[decl] == dk::struct_type || this.decls.kinds.p[decl] == dk::union_type { val traits := this.composed_of(decl); for i in 0..this.decls.list_length(traits) { this.offer_trait(offer, this.types.first_of(this.decls.list_at(traits, i as u32)), 0); } } if this.decls.kinds.p[decl] == dk::trait_type { this.offer_trait(offer, decl, 0); } } break if !this.is_alias(at); at = this.alias_target(at); } } #! Offers the methods a trait asks for, with those of the traits it composes. #! @param this The checker. #! @param offer What is offered so far. #! @param trait_decl The trait. #! @param depth How deeply composition has been followed. def checker::offer_trait(&mut this, offer: &mut offering, trait_decl: u32, depth: u32): void { return if trait_decl == 0 || depth > 16 || this.decls.kinds.p[trait_decl] != dk::trait_type; this.offer_members(offer, trait_decl, true); val run := this.composed_of(trait_decl); for i in 0..this.decls.list_length(run) { this.offer_trait(offer, this.types.first_of(this.decls.list_at(run, i as u32)), depth + 1); } } #! Offers the fields of a struct or union, with those its spread fields bring in #! by the names they are reached by. #! @param this The checker. #! @param offer What is offered so far. #! @param decl The struct or union. #! @param depth How deeply spreads have been followed. def checker::offer_fields(&mut this, offer: &mut offering, decl: u32, depth: u32): void { return if depth > 16; val run: u32 = this.decls.children.p[decl]; for i in 0..this.decls.list_length(run) { val field := this.decls.list_at(run, i as u32); continue if this.decls.kinds.p[field] != dk::field || !this.visible(field); this.offer_decl(offer, field); if this.is_spread(field) { this.resolve(field); val spread := this.type_decl(this.reached(this.decls.types.p[field])); if spread != 0 && this.decls.kinds.p[spread] == dk::struct_type { this.offer_fields(offer, spread, depth + 1); } } } } #! Offers the members a type declares: only its methods and properties, where #! they follow a value, or everything `::` reaches, as its functions, associated #! variables, members of an enum, and nested types. #! @param this The checker. #! @param offer What is offered so far. #! @param decl The type. #! @param values Whether they follow a value. def checker::offer_members(&mut this, offer: &mut offering, decl: u32, values: bool): void { var firsts := vector::::new(); val members := &this.decls.members; for at in 0..members.capacity { val key := members.key_at(at); if key != keymap::free && pair_high(key) == decl { firsts.append(members.value_at(at)); } } var nested := vector::::new(); val types := &this.decls.nested; for at in 0..types.capacity { val key := types.key_at(at); if key != keymap::free && pair_high(key) == decl { nested.append(types.value_at(at)); } } for &first in firsts { var walk := this.decls.walk_overloads(first, 64); while true { val at := walk.next(); break if at == 0; val kind: dk = this.decls.kinds.p[at]; val wanted := values ? (kind == dk::function && (this.decls.has(at, df::method) || this.decls.has(at, df::accessor))) || kind == dk::property : kind != dk::field; if wanted && this.visible(at) && this.member_seen(at) { this.offer_decl(offer, at); break; } } } if !values { for &each in nested { if this.visible(each) { this.offer_decl(offer, each); } } } } #! Offers what may follow `path::`: what the module the path names declares and #! the modules within it, or the members of the type it names, with those of #! the types it aliases. #! @param this The checker. #! @param offer What is offered so far. #! @param file The file. #! @param start Where the name completed begins. #! @param text The file's text. def checker::offer_path(&mut this, offer: &mut offering, file: u32, start: u32, text: strview): void { # The segments before `::`, last first. Type arguments before `::`, as in # `stack::`, are passed over. var names := vector::::new(); var at := start - 2; while names.length < 16 { if at > 0 && text[(at - 1) as u64] == '>' { var angles := 0; while at > 0 { at--; if text[at as u64] == '>' { angles++; } else if text[at as u64] == '<' { angles--; break if angles == 0; } } if at >= 2 && text[(at - 1) as u64] == ':' && text[(at - 2) as u64] == ':' { at -= 2; } } val end := at; while at > 0 && is_word_byte(text[(at - 1) as u64]) { at--; } break if at == end; val word := text.substr(at as u64, (end - at) as u64); names.append(this.names.intern(&word)); break if at < 2 || text[(at - 1) as u64] != ':' || text[(at - 2) as u64] != ':'; at -= 2; } return if names.length == 0; # The first segment is a module, a type, or a type parameter, as the place # names it; the rest are what the one before declares. var home: u32 = 0; var decl: u32 = 0; var in_module := false; val first: u32 = names.last; return if first == 0; val bound := this.bound(first); if (bound & ref_type) != 0 { decl = this.type_decl(bound & ref_mask); } else { home = this.lookup_module(first); in_module = home != 0; if !in_module { decl = this.lookup_type(first, 0); } } var i := names.length - 1; while i > 0 { i--; val name: u32 = names[i]; return if name == 0; if in_module { decl = this.modules.decl(home, name); if decl == 0 { home = this.modules.find(home, name); return if home == 0; } else { in_module = false; } } else { return if decl == 0; decl = this.decls.path_member(decl, name); } } if in_module { this.offer_module_decls(offer, home, this.modules.within(this.at.home, home)); this.offer_modules(offer, home); # A library's modules no file read refers to yet are found by their folders. var path := vector::::new(); this.module_segments(home, &mut path); for &each in modules_within(this.shelves.span, path.span) { this.offer_name(offer, each.span, symbol::namespace, "", ""); } return; } return if decl == 0 || !this.is_type_decl(decl); var kind: u32 = this.decls.types.p[decl]; var steps := 0; while steps < 16 { steps++; val holder := this.type_decl(kind); if holder != 0 { this.offer_members(offer, holder, false); } break if !this.is_alias(kind); kind = this.alias_target(kind); } } #! Offers the locals in scope at the offset: the parameters and type parameters #! of the function holding it, and the variables of each block holding it #! declared before it, with those its loops, arms, and tests bind there. #! @param this The checker. #! @param offer What is offered so far. #! @param file The file. #! @param offset The byte of its text. #! @param walked The file's tree. def checker::offer_locals(&mut this, offer: &mut offering, file: u32, offset: u32, walked: &tree_walk): void { val syntax := &this.units[file].syntax; # The nodes holding the place, innermost first, found from the innermost node # whose tokens hold it. var holders := vector::::new(); for &node in walked.order { continue if !this.node_holds(file, node, offset); holders.append(node); } # The walk meets what holds a node before it, so the last is the innermost. var i := holders.length; while i > 0 { i--; val node: u32 = holders[i]; match syntax.node_kinds[node] { nk::block => { val run: u32 = syntax.node_lefts[node]; for &statement in syntax.run_view(run) { break if syntax.token_offsets[syntax.start_of(statement)] >= offset; # A variable is in scope after its declaration, not within its value. if syntax.node_kinds[statement] == nk::variable && this.node_end(file, statement) <= offset && this.has_flag(statement, rf::local) { this.offer_local(offer, file, statement); } } } nk::function => { val params := syntax.record(node, function_slot::params); for ¶m in syntax.run_view(params) { this.offer_local(offer, file, param); } this.offer_type_params(offer, file, syntax.record(node, function_slot::type_params)); # Type parameters named before `::` belong to the method, as `T` in `box::get`. val receiver := syntax.record(node, function_slot::receiver); if receiver != 0 && syntax.node_lefts[receiver] != 0 { val segments: u32 = syntax.node_lefts[receiver]; val last := syntax.run_at(segments, syntax.run_length(segments) - 1); this.offer_type_params(offer, file, syntax.node_lefts[last]); } } nk::lambda => { val params: u32 = syntax.node_lefts[node]; for ¶m in syntax.run_view(params) { this.offer_local(offer, file, param); } } nk::for_stmt => { if this.node_holds(file, syntax.node_rights[node], offset) { # Its index and variable, `for_slot::index` and `for_slot::variable`. for j in 0u..2u { val bound := syntax.record(node, j as u32); if bound != 0 && this.has_flag(bound, rf::local) { this.offer_local(offer, file, bound); } } } } nk::match_arm => { val patterns: u32 = syntax.node_lefts[node]; for &pattern in syntax.run_view(patterns) { this.offer_bindings(offer, file, pattern); } } nk::if_stmt => { if this.node_holds(file, syntax.record(node, if_slot::then), offset) { this.offer_tests(offer, file, syntax.node_lefts[node]); } } nk::fallback => { val error := syntax.record(node, fallback_slot::error); if error != 0 && this.node_holds(file, syntax.record(node, fallback_slot::other), offset) && this.has_flag(error, rf::local) { this.offer_local(offer, file, error); } } else => {} } } } #! Offers the names a pattern binds, as `n` of `number(n)` or `number(&mut n)`. #! @param this The checker. #! @param offer What is offered so far. #! @param file The file. #! @param pattern The pattern. def checker::offer_bindings(&mut this, offer: &mut offering, file: u32, pattern: u32): void { val syntax := &this.units[file].syntax; return if pattern == 0 || syntax.node_kinds[pattern] != nk::call; val run: u32 = syntax.node_rights[pattern]; for &each in syntax.run_view(run) { var bound := each; if syntax.node_kinds[bound] == nk::reference { bound = syntax.node_lefts[bound]; } if syntax.node_kinds[bound] == nk::name && this.has_flag(bound, rf::local) { this.offer_local(offer, file, bound); } } } #! Offers the names the `is` tests of a condition bind, as `v` of #! `x is some(v)`, in the branch they guard. #! @param this The checker. #! @param offer What is offered so far. #! @param file The file. #! @param condition The condition. def checker::offer_tests(&mut this, offer: &mut offering, file: u32, condition: u32): void { val syntax := &this.units[file].syntax; return if condition == 0; match syntax.node_kinds[condition] { nk::is_test => { var bound := syntax.record(condition, is_slot::binding); if bound != 0 && syntax.node_kinds[bound] == nk::reference { bound = syntax.node_lefts[bound]; } if bound != 0 && this.has_flag(bound, rf::local) { this.offer_local(offer, file, bound); } } nk::binary => { this.offer_tests(offer, file, syntax.node_lefts[condition]); this.offer_tests(offer, file, syntax.node_rights[condition]); } else => {} } } #! Offers the type parameters of a run. #! @param this The checker. #! @param offer What is offered so far. #! @param file The file. #! @param run The run of `type_param` nodes. def checker::offer_type_params(&mut this, offer: &mut offering, file: u32, run: u32): void { val syntax := &this.units[file].syntax; return if run == 0; for ¶m in syntax.run_view(run) { # A pattern offers the type parameters it names. if syntax.node_kinds[param] == nk::type_pattern { this.offer_type_params(offer, file, syntax.node_lefts[param]); if syntax.node_rights[param] != 0 { this.offer_type_param(offer, file, syntax.node_rights[param]); } continue; } if syntax.node_kinds[param] == nk::array_pattern { this.offer_type_param(offer, file, syntax.node_lefts[param]); if syntax.node_rights[param] != 0 { this.offer_type_param(offer, file, syntax.node_rights[param]); } continue; } this.offer_type_param(offer, file, param); } } #! Offers a type parameter, or a constant one. #! @param this The checker. #! @param offer What is offered so far. #! @param file The file. #! @param param The `type_param` or `const_param` node. def checker::offer_type_param(&mut this, offer: &mut offering, file: u32, param: u32): void { val syntax := &this.units[file].syntax; var text := string::from(syntax.node_kinds[param] == nk::const_param ? "constant parameter " : "type parameter ", nothing {}); this.param_text(file, param, &mut text); this.offer_name(offer, this.token_text(file, syntax.node_tokens[param]), syntax.node_kinds[param] == nk::const_param ? symbol::constant : symbol::type_param, text.span, ""); } #! Whether a node's tokens hold the offset: from where it begins to where its #! last token ends. A block holds what is after its `{` through its `}`. #! @param this The checker. #! @param file The file. #! @param node The node. #! @param offset The byte of its text. #! @returns Whether they do. def checker::node_holds(&this, file: u32, node: u32, offset: u32): bool { return false if node == 0; val syntax := &this.units[file].syntax; val first: u32 = syntax.token_offsets[syntax.node_tokens[node]]; val brace := syntax.node_kinds[node] == nk::block && syntax.token_kinds[syntax.node_tokens[node]] == tk::lbrace; return false if brace ? offset <= first : offset < first; val last: u32 = syntax.node_end(node); if brace { # A block whose `}` is missing, as one being written, holds the rest. return syntax.token_kinds[last] != tk::rbrace || offset <= syntax.token_offsets[last]; } return offset <= this.node_end(file, node); } #! Where a node's last token ends. #! @param this The checker. #! @param file The file. #! @param node The node. #! @returns The byte its last token ends at. def checker::node_end(&this, file: u32, node: u32): u32 { val syntax := &this.units[file].syntax; val last: u32 = syntax.node_end(node); return syntax.token_offsets[last] + syntax.token_length(last, this.names); } #! Whether the name of a field or pattern is one the literal or match has #! already. #! @param this The checker. #! @param site Where the name is completed. #! @param name The ID of the name. #! @returns Whether it is. def checker::is_written(&this, site: &completing, name: u32): bool { for &each in site.written { return true if each == name; } return false; } #! The segments of a module's path, its outermost first. #! @param this The checker. #! @param id The module. #! @param into Where they are added. def checker::module_segments(&this, id: u32, into: &mut vector): void { return if id == 0; this.module_segments(this.modules.parents[id], into); into.append(string::from(this.names.text(this.modules.names[id]), nothing {})); } #! Whether a module is `core`'s, the standard library's, or `system`'s, or one #! within them. #! @param this The checker. #! @param id The module. #! @returns Whether it is. def checker::in_library(&this, id: u32): bool { var at := id; var steps := 0; while at != 0 && steps < 64 { return this.modules.names[at] == word::std || this.modules.names[at] == word::core || this.modules.names[at] == word::system if this.modules.parents[at] == 0; at = this.modules.parents[at]; steps++; } return false; } #! Whether a token opens a group, as `(`. #! @param kind The token's kind. #! @returns Whether it does. def opens_group(kind: tk): bool { return kind == tk::lparen || kind == tk::lbracket || kind == tk::lbrace; } #! Whether a token closes a group, as `)`. #! @param kind The token's kind. #! @returns Whether it does. def closes_group(kind: tk): bool { return kind == tk::rparen || kind == tk::rbracket || kind == tk::rbrace; } #! What stands for no token, where completion looks for one before a place and #! finds none. const no_token: u32 = 0xFFFFFFFF; #! The token before another at the same depth of groups, passing over whole #! groups, or `no_token` where there is none. #! @param kinds The kinds of the tokens. #! @param at The token. #! @returns The token before it, or `no_token`. def token_before(kinds: &vector, at: u32): u32 { var depth := 0; var k := at; while k > 0 { k--; if closes_group(kinds[k]) { depth++; } else if opens_group(kinds[k]) { return no_token if depth == 0; depth--; } return k if depth == 0; } return no_token; } #! Whether a name written after the first `count` tokens names a type: after #! `as`, after the `:` of a declaration, parameter, result, or bound, rather than #! of a ternary or a slice, after `&`, `*`, or `mut` of a type, and among type #! arguments. #! @param kinds The kinds of the tokens. #! @param count How many come before the name. #! @param depth How deeply type arguments have been followed. #! @returns Whether it does. def typed_place(kinds: &vector, count: u32, depth: u32): bool { var at := count; while at > 0 && (kinds[at - 1] == tk::bitand || kinds[at - 1] == tk::star || kinds[at - 1] == tk::kw_mut) { at--; } return false if at == 0 || depth > 16; val before := kinds[at - 1]; return true if before == tk::kw_as; if before == tk::colon { # Within the statement, a `?` before the `:` makes it a ternary's, and a `[` # holding it a slice's. var k := token_before(kinds, at - 1); while k != no_token { return false if kinds[k] == tk::quest; break if kinds[k] == tk::semicolon || kinds[k] == tk::lbrace || kinds[k] == tk::rbrace; k = token_before(kinds, k); } var open := 0; var j := at - 1; while j > 0 { j--; if closes_group(kinds[j]) { open++; } else if opens_group(kinds[j]) { return kinds[j] != tk::lbracket if open == 0; open--; } } return true; } # Type arguments: after the `<` of `::<`, or of a name that is itself a type. if before == tk::less || before == tk::comma { var angles := 0; var j := at; while j > 0 { j--; val kind := kinds[j]; if kind == tk::more { angles++; } else if kind == tk::rshift { angles += 2; } else if kind == tk::less && angles > 0 { angles--; } else if kind == tk::less { return (j > 0 && kinds[j - 1] == tk::scope) || (j > 0 && kinds[j - 1] == tk::name && typed_place(kinds, j - 1, depth + 1)); } else if opens_group(kind) || closes_group(kind) || kind == tk::semicolon { return false; } } } return false; } #! Works out what kind of place a name beginning at `start` is written in, from #! the tokens of the top-level declaration holding it, up to it. #! @param this The checker. #! @param file The file. #! @param start Where the name begins. #! @returns The place. def checker::place_of_completion(&mut this, file: u32, start: u32): completing { var found := completing { kind = place_kind::anywhere, written = vector::::new() }; val syntax := &this.units[file].syntax; # The tokens from the top-level declaration holding the place. var first: u32 = 0; val run := syntax.node_lefts[this.units[file].root]; for &node in syntax.run_view(run) { val begins := syntax.start_of(node); break if syntax.token_offsets[begins] >= start; first = begins; } var kinds := vector::::new(); var tokens := vector::::new(); var at := first; while (at as u64) < syntax.token_count && syntax.token_offsets[at] < start && syntax.token_kinds[at] != tk::eof { kinds.append(syntax.token_kinds[at]); tokens.append(at); at++; } val count := kinds.length as u32; return found if count == 0; val last := kinds[count - 1]; if last == tk::kw_imp && !(count >= 2 && kinds[count - 2] == tk::kw_extern) { found.kind = place_kind::imported; return found; } if last == tk::kw_is || (last == tk::kw_not && count >= 2 && kinds[count - 2] == tk::kw_is) { found.kind = place_kind::test; found.opening = tokens[last == tk::kw_is ? count - 1 : count - 2]; return found; } if typed_place(&kinds, count, 0) { found.kind = place_kind::type_only; return found; } # Within braces, where a field is named or an arm begins: right after the `{`, # or after a `,`, or after a `;` or `}` ending an arm. var brace: u32 = no_token; var open := 0; var k := count; while k > 0 { k--; if closes_group(kinds[k]) { open++; } else if opens_group(kinds[k]) { if open == 0 { if kinds[k] == tk::lbrace { brace = k; } break; } open--; } } return found if brace == no_token || (last != tk::lbrace && last != tk::comma && last != tk::semicolon && last != tk::rbrace); # A match: `match` begins the statement the brace follows. var j := token_before(&kinds, brace); while j != no_token { val kind := kinds[j]; if kind == tk::kw_match { found.kind = place_kind::arm; found.opening = tokens[j]; break; } break if kind == tk::semicolon || kind == tk::lbrace || kind == tk::rbrace || kind == tk::arrow || kind == tk::assign || kind == tk::kw_ret || kind == tk::comma; j = token_before(&kinds, j); } # A struct literal, which parsed as one, of a type named before the brace. if found.kind == place_kind::anywhere && (last == tk::lbrace || last == tk::comma) && brace > 0 { found.kind = place_kind::field; found.opening = tokens[brace]; } # The fields a literal gives, as `name =`, and the patterns a match has, each a # name beginning an arm, directly within the braces. var depth := 0; for m in (brace + 1)..count { val index := m as u32; val kind := kinds[index]; if depth == 0 && kind == tk::name { val prior := kinds[index - 1]; val next := index + 1 < count ? kinds[index + 1] : tk::eof; val given := found.kind == place_kind::field ? next == tk::assign && (prior == tk::lbrace || prior == tk::comma) : prior == tk::lbrace || prior == tk::comma || prior == tk::semicolon || prior == tk::rbrace; if given { found.written.append(syntax.token_names[tokens[index]]); } } if opens_group(kind) { depth++; } else if closes_group(kind) && depth > 0 { depth--; } } return found; } #! The struct or union a literal makes, by the `{` beginning its fields, or 0 #! where the brace begins none. #! @param this The checker. #! @param file The file. #! @param brace The `{` token. #! @param walked The file's tree. #! @returns The struct's or union's declaration, or 0. def checker::literal_type(&mut this, file: u32, brace: u32, walked: &tree_walk): u32 { for &node in walked.order { continue if this.kind_of(node) != nk::construct || this.token_of(node) != brace; var made := this.type_at(node); if made == 0 || made == bt::error { val named := this.type_name_meaning(this.left_of(node)); return 0 if named.decl == 0; made = this.decls.types.p[named.decl]; } val decl := this.type_decl(this.unalias(made)); return decl if decl != 0 && (this.decls.kinds.p[decl] == dk::struct_type || this.decls.kinds.p[decl] == dk::union_type); return 0; } return 0; } #! The enum or union a match tests, through references and aliases, by its #! `match`, or 0 where it tests neither. #! @param this The checker. #! @param file The file. #! @param token The `match` token. #! @param walked The file's tree. #! @returns The enum's or union's type, or 0. def checker::subject_type(&mut this, file: u32, token: u32, walked: &tree_walk): u32 { for &node in walked.order { val kind := this.kind_of(node); continue if (kind != nk::match_stmt && kind != nk::match_value) || this.token_of(node) != token; val held := this.unalias(this.reached(this.type_at(this.left_of(node)))); val named := this.named_kind(held); return held if named == dk::enum_type || named == dk::union_type; return 0; } return 0; } #! The union an `is` tests, by its `is`, or 0 where it tests none. #! @param this The checker. #! @param file The file. #! @param token The `is` token. #! @param walked The file's tree. #! @returns The union's type, or 0. def checker::tested_union(&mut this, file: u32, token: u32, walked: &tree_walk): u32 { for &node in walked.order { continue if this.kind_of(node) != nk::is_test || this.token_of(node) != token; val held := this.unalias(this.reached(this.type_at(this.left_of(node)))); return held if this.named_kind(held) == dk::union_type; return 0; } return 0; }