#! @file lookup.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains how names are found where they are written. A name is first what #! a scope binds it to, then what the types around it give, as `kind` within #! `token` is `token::kind`, then a declaration of the module it is written in, #! or of the modules holding that one, nearest first, then of the modules its #! file imports, then a built-in type, then a declaration of `core`'s root. The program, where the files declaring #! no module are, holds no module, so a module never names what it declares. A name a type gives that the module or #! a module holding it declares too is ambiguous, as are two imports declaring #! the name, unless they declare the same thing. module sakura::analyzer; #! What the name refers to where it is written, or 0 where nothing declares it. #! @param this The checker. #! @param name The ID of the name. #! @param node Where the name is written, for reporting it ambiguous, or 0. #! @returns A local or type parameter with its bit of `ref_local` or `ref_type`, #! a declaration, or 0. pub def checker::lookup(&mut this, name: u32, node: u32): u32 { val bound := this.bound(name); return bound if bound != 0; return this.lookup_global(name, node); } #! The declaration the name refers to outside any scope, or 0. #! @param this The checker. #! @param name The ID of the name. #! @param node Where the name is written, for reporting it ambiguous, or 0. #! @returns The declaration, or 0. pub def checker::lookup_global(&mut this, name: u32, node: u32): u32 { return this.lookup_named(name, node, false).decl; } #! The declaration the name refers to outside any scope where only a type is #! meant, as in a type, or 0. #! @param this The checker. #! @param name The ID of the name. #! @param node Where the name is written, for reporting it ambiguous, or 0. #! @returns The declaration, or 0. pub def checker::lookup_type(&mut this, name: u32, node: u32): u32 { return this.lookup_named(name, node, true).decl; } #! The declaration the name refers to outside any scope, and where a type #! around it gives it, that type, given the arguments it has there. #! @param this The checker. #! @param name The ID of the name. #! @param node Where the name is written, for reporting it ambiguous, or 0. #! @param types Whether only a type is meant, so what else a type around it #! gives is passed over, as a member `u8` of an enum is in `u8`. #! @returns The declaration and the type giving it, or no declaration. pub def checker::lookup_named(&mut this, name: u32, node: u32, types: bool): checker::found { this.depend_name(name); val scoped := this.in_scope(name, types); var home := this.at.home; var steps := 0; while steps < 64 { val found := this.modules.decl(home, name); if found != 0 { if scoped.decl != 0 && scoped.decl != found && node != 0 { this.report_ambiguous(node, name, scoped.decl, found); } return scoped.decl != 0 ? scoped : checker::found { decl = found }; } # The program is where the files declaring no module are, so it holds no # module: a module of one segment is within none. break if home == 0 || this.modules.parents[home] == 0; home = this.modules.parents[home]; steps++; } return scoped if scoped.decl != 0; var found: u32 = 0; var hidden: u32 = 0; val imports: u32 = this.units[this.at.file].imports; for &imported in this.decls.list_view(imports) { val each := this.modules.decl(imported, name); continue if each == 0; if !this.visible(each) { hidden = each; continue; } if found != 0 && found != each { if node != 0 { var text := string::from("More than one imported module declares `", nothing {}); text.append(this.names.text(name)); text.append("`, so write the module it is of, as `io::println`."); this.report_text(node, ec::ambiguous, &text); } return checker::found { decl = found }; } found = each; } return checker::found { decl = found } if found != 0; if this.builtins.get(&name) is some(got) { return checker::found { decl = got }; } # Last, the names of `core`'s root, which every file sees, and which anything # declared or imported hides. val core := this.modules.find(0, word::core); if core != 0 && core != this.at.home { val each := this.modules.decl(core, name); if each != 0 { return checker::found { decl = each } if this.visible(each); hidden = hidden != 0 ? hidden : each; } } # A declaration only hidden from where it is named is found, so what names # it is told it is not `pub` rather than that it does not exist. return checker::found { decl = hidden }; } #! What the types around where checking is give the name, innermost first. #! Within a type, and what is declared through it, its nested types and #! associated variables, and an enum's members, are named without it, as #! `kind` is `token::kind` within `token`. A field or method belongs to a value, #! so it is named through one, and a type's functions are named through the #! type, since they are often named for the free functions they implement, as #! `vector::decode` calls `decode::`. #! @param this The checker. #! @param name The ID of the name. #! @param types Whether only a type is meant. #! @returns The member and the type giving it, or no member. pub def checker::in_scope(&mut this, name: u32, types: bool): checker::found { var kind := this.at.scope; var steps := 0; while kind != 0 && steps < 16 { steps++; val decl := this.type_decl(kind); break if decl == 0; val member := this.scope_member(decl, name, types); return checker::found { decl = member, owner = kind } if member != 0; # A type nested in another is named within that one, whose arguments are # the first of its own. val outer: u32 = this.decls.owners.p[decl]; break if outer == 0 || !this.gives_scope(outer); kind = this.scope_type(outer, kind); } return checker::found {}; } #! The member of the name a type gives the names within it, or 0. #! @param this The checker. #! @param decl The type's declaration. #! @param name The ID of the name. #! @param types Whether only a type is meant. #! @returns The member, or 0. def checker::scope_member(&mut this, decl: u32, name: u32, types: bool): u32 { val nested := this.decls.member_in(decl, name, true); if nested != 0 { return this.visible(nested) ? nested : 0; } # A sealed trait gives the names of its types too. val sealed := this.is_sealed(decl); val member := types && !sealed ? 0 : this.decls.member_in(decl, name, false); return 0 if member == 0 || !this.visible(member); return match this.decls.kinds.p[member] { dk::variable, dk::enum_member => types ? 0 : member, dk::assoc_type => sealed ? member : 0, else => 0 }; } #! Whether a type gives the names within it: a struct, union, or enum, or a #! built-in type. A trait gives its members `self` and the types it asks for. #! @param this The checker. #! @param decl The type's declaration. #! @returns Whether it does. pub def checker::gives_scope(&this, decl: u32): bool { return match this.decls.kinds.p[decl] { dk::struct_type, dk::union_type, dk::enum_type, dk::builtin => true, else => false }; } #! The type giving the names within a declaration: the type itself, or the #! one it is a member of or is declared through, given the arguments it is #! checked with, or 0. #! @param this The checker. #! @param id The declaration. #! @param args The pack of its type arguments, or 0 where it is checked as #! itself. #! @returns The type, or 0. pub def checker::scope_of(&mut this, id: u32, args: u32): u32 { var owner: u32 = this.decls.owners.p[id]; var holder := id; match this.decls.kinds.p[id] { dk::type_param => return owner == 0 ? 0 : this.scope_of(owner, 0); dk::struct_type, dk::union_type, dk::enum_type => return this.own_scope(id, id, args); dk::field, dk::enum_member => return owner != 0 && this.gives_scope(owner) ? this.decls.types.p[owner] : 0; dk::function, dk::variable, dk::property, dk::alias_type, dk::trait_type, dk::assoc_type => {} else => return 0; } # A sealed trait gives its constants and types the names of the others, of # the trait given the same arguments. if this.decls.kinds.p[id] == dk::trait_type && this.is_sealed(id) { return args == 0 ? this.decls.types.p[id] : this.types.named(id, this.types.parts_of(args).span); } if (this.decls.kinds.p[id] == dk::variable || this.decls.kinds.p[id] == dk::assoc_type || this.decls.kinds.p[id] == dk::function) && this.is_sealed(owner) { return this.decls.types.p[owner] if args == 0; var given := vector::::new(); for i in 0..this.decls.list_length(this.decls.params.p[owner]) { given.append(i < this.types.part_count(args) ? this.types.part_at(args, i as u32) : bt::error); } return this.types.named(owner, given.span); } # An accessor is checked as a member of its property's type. if owner != 0 && this.decls.kinds.p[owner] == dk::property { holder = owner; owner = this.decls.owners.p[owner]; } return 0 if owner == 0 || !this.gives_scope(owner); return this.own_scope(owner, holder, args); } #! A type, given the first of the arguments of a declaration within it, which #! names the type's parameters first. #! @param this The checker. #! @param decl The type's declaration. #! @param holder The declaration within it. #! @param args The pack of the holder's type arguments, or 0 where it is #! checked as itself. #! @returns The type. def checker::own_scope(&mut this, decl: u32, holder: u32, args: u32): u32 { return this.decls.types.p[decl] if this.decls.kinds.p[decl] == dk::builtin; val count := this.decls.list_length(this.decls.params.p[decl]); val run: u32 = this.decls.params.p[holder]; var given := vector::::new(); for i in 0..count { if args != 0 && i < this.types.part_count(args) { given.append(this.types.part_at(args, i as u32)); } else if i < this.decls.list_length(run) { given.append(this.decls.types.p[this.decls.list_at(run, i as u32)]); } else { given.append(bt::error); } } return this.types.named(decl, given.span); } #! A type a nested one is declared through, given the first of the nested #! one's arguments. #! @param this The checker. #! @param outer The outer type's declaration. #! @param inner The nested type, given its arguments. #! @returns The outer type. def checker::scope_type(&mut this, outer: u32, inner: u32): u32 { return this.decls.types.p[outer] if this.decls.kinds.p[outer] == dk::builtin; val count := this.decls.list_length(this.decls.params.p[outer]); var given := vector::::new(); for i in 0..count { given.append(this.types.kind_of(inner) == ty::named && i < this.types.part_count(inner) ? this.types.part_at(inner, i as u32) : bt::error); } return this.types.named(outer, given.span); } #! Reports a name both a type around it and its module give, which only a #! path tells apart. #! @param this The checker. #! @param node Where the name is written. #! @param name The ID of the name. #! @param scoped What the type gives it. #! @param other What the module gives it. def checker::report_ambiguous(&mut this, node: u32, name: u32, scoped: u32, other: u32): void { var text := string::from("`", nothing {}); text.append(this.names.text(name)); text.append("` names both `"); var first := this.path_text(scoped); text.append(first.span); if this.decls.modules.p[other] == 0 && this.decls.owners.p[other] == 0 { text.append("` and the program's own `"); text.append(this.names.text(name)); text.append("`, so write `"); text.append(first.span); text.append("` for the first, or rename one of them."); } else { text.append("` and `"); var second := this.path_text(other); text.append(second.span); text.append("`, so write the path of the one meant."); } this.report_text(node, ec::ambiguous, &text); } #! The path a declaration is written as from anywhere, as `token::kind` or #! `std::io::println`. One declared outside any module has only its name. #! @param this The checker. #! @param id The declaration. #! @returns The path. pub def checker::path_text(&this, id: u32): string { var made := string::new(nothing {}); val owner: u32 = this.decls.owners.p[id]; if owner != 0 && this.decls.kinds.p[id] != dk::type_param { made = this.path_text(owner); made.append("::"); } else { var modules := vector::::new(); var at: u32 = this.decls.modules.p[id]; var steps := 0; while at != 0 && steps < 64 { modules.append(at); at = this.modules.parents[at]; steps++; } var i := modules.length; while i > 0 { i--; made.append(this.names.text(this.modules.names[modules[i]])); made.append("::"); } } made.append(this.names.text(this.decls.names.p[id])); return made; } #! Reports a name nothing declares where it is used. A module sees the names of #! those holding it without an import, so where no file of one was read, as a #! library's root left out, the note says so. #! @param this The checker. #! @param node The node naming it. #! @param name The ID of the name. pub def checker::report_undeclared(&mut this, node: u32, name: u32): void { var text := string::from("`", nothing {}); text.append(this.names.text(name)); text.append("` is not declared where it is used."); var at: u32 = this.at.home == 0 ? 0 : this.modules.parents[this.at.home]; var steps := 0; while at != 0 && steps < 64 { if !this.modules.named[at] { text.append(" No file of `"); text.append(this.module_text(at).span); text.append("`, which holds this file's module and whose names it sees, was read: is one missing?"); break; } at = this.modules.parents[at]; steps++; } this.report_text(node, ec::undefined, &text); } #! A module's path, as `std::io`. #! @param this The checker. #! @param id The module. #! @returns The path. pub def checker::module_text(&this, id: u32): string { var made := string::new(nothing {}); var at := id; var steps := 0; while at != 0 && steps < 64 { var each := string::from(this.names.text(this.modules.names[at]), nothing {}); if made.length != 0 { each.append("::"); each.append(made.span); } made = move(&mut each); at = this.modules.parents[at]; steps++; } return made; } #! The module the name refers to where it is written: one within the module it #! is written in or a module holding it, or the last segment of an import. #! @param this The checker. #! @param name The ID of the name. #! @returns The module, or 0. pub def checker::lookup_module(&mut this, name: u32): u32 { var home := this.at.home; var steps := 0; while steps < 64 { val found := this.modules.find(home, name); return found if found != 0; break if home == 0; home = this.modules.parents[home]; steps++; } val imports: u32 = this.units[this.at.file].imports; for &imported in this.decls.list_view(imports) { return imported if this.modules.names[imported] == name; } return 0; } #! Whether a path whose first name names both a module and a type names the #! type: where the module declares nothing of the path's next name, as #! `uri::parse` where the module `uri` declares the type `uri`, whose function #! `parse` is. #! @param this The checker. #! @param home The module the first name names. #! @param node The `path` or `type_name` node, of two segments or more. #! @returns Whether the type is named. pub def checker::type_over_module(&mut this, home: u32, node: u32): bool { val next := this.segment_name(node, 1); return false if this.modules.decl(home, next) != 0 || this.modules.find(home, next) != 0; val found := this.lookup_named(this.segment_name(node, 0), 0, true); return found.decl != 0 && this.is_type_decl(found.decl); } #! Whether the declaration can be named where checking is: it is `pub`, or #! declared in the module checked or one holding it. #! @param this The checker. #! @param id The declaration. #! @returns Whether it can. pub def checker::visible(&this, id: u32): bool { return true if this.decls.has(id, df::exported) || this.decls.kinds.p[id] == dk::builtin; # A property is seen where an accessor of it is `pub`, as `pub get`. if this.decls.kinds.p[id] == dk::property { val run: u32 = this.decls.children.p[id]; for &each in this.decls.list_view(run) { return true if this.decls.has(each, df::exported); } } # Enum members, and what a trait asks for, are as visible as their type. A # field takes its own `pub`, as a method does. val owner: u32 = this.decls.owners.p[id]; val kind: dk = this.decls.kinds.p[id]; if owner != 0 && (kind == dk::enum_member || (this.decls.kinds.p[owner] == dk::trait_type && !this.receiver_declared(id))) { return this.visible(owner); } var home: u32 = this.decls.modules.p[id]; return this.modules.within(this.at.home, home); } #! Whether a trait asks for the declaration: it is declared within the trait, #! or is an accessor of a property that is. A function declared through a #! trait, outside it, as `def real::parse`, is the trait's own instead, #! called through the trait's name as any type's static function is. #! @param this The checker. #! @param id The declaration. #! @returns Whether it does. pub def checker::trait_asks(&this, id: u32): bool { var at := id; var owner: u32 = this.decls.owners.p[id]; if owner != 0 && this.decls.kinds.p[owner] == dk::property { at = owner; owner = this.decls.owners.p[owner]; } return owner != 0 && this.decls.kinds.p[owner] == dk::trait_type && !this.receiver_declared(at); } #! Whether a trait's default is compiled for each type taking it, with `self` #! that type, as the type's own methods are: one of a trait that is only a #! bound, which no table calls through. Any other default is compiled once, #! taking `this` as a reference to the trait. #! @param this The checker. #! @param id The method's declaration. #! @returns Whether it is. pub def checker::typed_default(&mut this, id: u32): bool { return false if this.decls.kinds.p[id] != dk::function || !this.trait_asks(id) || !this.has_body(id); val holder := this.enclosing_trait(id); return holder != 0 && holder != id && this.bound_only(holder); } #! Whether a declaration is a constant a trait asks for and gives a default of, #! which a type naming the trait and giving none of its own takes, worked out #! with `self` that type. A sealed trait's constants are its own instead. #! @param this The checker. #! @param id The declaration. #! @returns Whether it is. pub def checker::constant_default(&this, id: u32): bool { return this.decls.kinds.p[id] == dk::variable && this.trait_asks(id) && !this.is_sealed(this.decls.owners.p[id]) && this.record_in(id, variable_slot::value) != 0; } #! Whether a constant a trait declares is one constant of the program, rather #! than one for each type taking it or each list of arguments the trait is #! given: one of a sealed trait without type parameters. #! @param this The checker. #! @param decl The constant's declaration. #! @returns Whether it is. pub def checker::sealed_alone(&this, decl: u32): bool { val holder: u32 = this.decls.owners.p[decl]; return this.is_sealed(holder) && this.decls.list_length(this.decls.params.p[holder]) == 0; } #! Whether a declaration was made through a type, outside it, rather than #! within it, so it takes its own `pub`. #! @param this The checker. #! @param id The declaration. #! @returns Whether it was. pub def checker::receiver_declared(&this, id: u32): bool { val syntax := &this.units[this.decls.files.p[id]].syntax; val node: u32 = this.decls.nodes.p[id]; val kind := syntax.node_kinds[node]; return false if kind != nk::function && kind != nk::variable && kind != nk::property; return syntax.record(node, decl_slot::receiver) != 0; }