#! @file declare.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the declaring of what each file declares: its modules, its #! imports, and every declaration outside a function, with the members of #! types and the type parameters of generics. A declaration made through a #! type, as `vec2::length`, waits until every file is declared, since its type #! may be declared in any of them. module sakura::analyzer; #! Declares what the file declares. #! @param this The checker. #! @param file The file. pub def checker::declare_unit(&mut this, file: u32): void { this.use_table(this.units[file].table); this.at.home = 0; this.at.scope = 0; val decls := this.left_of(this.units[file].root); var active := true; var begun := false; var sections := false; var first_library: u32 = 0; this.external = false; for &node in this.run_view(decls) { match this.kind_of(node) { nk::import_decl, nk::extern_import => { if begun { this.report(node, ec::invalid, "Imports come before every other declaration."); } if this.kind_of(node) == nk::import_decl { this.pending_imports.append(pair_key(file, node)); } else if this.right_of(node) == 0 || this.target_holds(this.right_of(node)) { first_library = first_library == 0 ? node : first_library; this.add_library(file, node); } } nk::module_decl, nk::extern_module => { begun = true; this.at.home = this.module_path(this.left_of(node), true); this.modules.named[this.at.home] = true; active = this.right_of(node) == 0 || this.target_holds(this.right_of(node)); this.external = this.kind_of(node) == nk::extern_module; if this.external { sections = true; this.check_section(node); } } else => { begun = true; if active { this.declare_top(node); } } } } this.external = false; if first_library != 0 && !sections { this.warn(first_library, ec::invalid, "Nothing in this file is C's, so nothing uses the libraries it imports: C's functions are declared in an `extern module`."); } } #! Checks that an `extern module` says which systems it is for, since a library #! is one system's and C's calling convention is the system's too. #! @param this The checker. #! @param node The `extern_module` node. def checker::check_section(&mut this, node: u32): void { val condition := this.right_of(node); if condition == 0 || !this.names_system(condition) { this.report(node, ec::invalid, "An `extern module` says which systems it's for: `when os == ...`, `arch == ...`, or both."); } } #! Whether a condition on the target names `os` or `arch` anywhere. #! @param this The checker. #! @param node The condition's node. #! @returns Whether it does. def checker::names_system(&this, node: u32): bool { return match this.kind_of(node) { nk::unary => this.names_system(this.left_of(node)), nk::binary => this.token_kind(node) == tk::logand || this.token_kind(node) == tk::logor ? this.names_system(this.left_of(node)) || this.names_system(this.right_of(node)) : this.name_of(this.left_of(node)) == word::os || this.name_of(this.left_of(node)) == word::arch, else => false }; } #! Notes a library a file's C functions come from. On Windows each function is #! listed under its library, so a file names one there. #! @param this The checker. #! @param file The file. #! @param node The `extern_import` node. def checker::add_library(&mut this, file: u32, node: u32): void { if this.names.text(this.target.os) == "windows" { for i in 0..this.libraries.length { if (this.libraries[i] >> 32) as u32 == file { this.report(node, ec::invalid, "A file names one library on Windows, which lists each C function under its library: declare the functions of another in a file of their own."); return; } } } this.libraries.append(pair_key(file, node)); } #! Marks what an `extern module` declares as C's: a function without a body, a #! struct, or a union. Whatever else it declares is ordinary Sakura. #! @param this The checker. #! @param id The declaration. #! @param kind Its kind. #! @param node Its node. def checker::mark_external(&mut this, id: u32, kind: dk, node: u32): void { if kind == dk::struct_type || kind == dk::union_type || kind == dk::function && this.record(node, function_slot::body) == 0 { this.decls.mark(id, df::foreign); } } #! The module a path names, made where `make` says and it is new, or 0 where #! it names none. #! @param this The checker. #! @param path The `path` node. #! @param make Whether a module it names is made where it is new. #! @returns The module, or 0. pub def checker::module_path(&mut this, path: u32, make: bool): u32 { var at: u32 = 0; for i in 0..this.segment_count(path) { val name := this.segment_name(path, i as u32); if make { at = this.modules.child(at, name); continue; } at = this.modules.find(at, name); return 0 if at == 0; } return at; } #! Declares a declaration at the top level, or those of the branch of a `when` #! the target takes. #! @param this The checker. #! @param node The top-level node. def checker::declare_top(&mut this, node: u32): void { match this.kind_of(node) { nk::when_block => { val chosen := this.when_branch(node); return if chosen == 0; val decls := this.left_of(chosen); for &node in this.run_view(decls) { this.declare_top(node); } } nk::error => {} else => { this.decls.top = node; this.declare_decl(node, 0); this.decls.top = 0; } } } #! The block of a `when` the target takes, following `else when`, or 0. #! @param this The checker. #! @param node The `when_block` node. #! @returns The block, or 0. pub def checker::when_branch(&mut this, node: u32): u32 { var at := node; while at != 0 && this.kind_of(at) == nk::when_block { return this.record(at, if_slot::then) if this.target_holds(this.left_of(at)); at = this.record(at, if_slot::other); } return at; } #! Whether the target meets the condition of a `when`, reporting a key or a #! value it does not know. #! @param this The checker. #! @param node The condition's node. #! @returns Whether it does. pub def checker::target_holds(&mut this, node: u32): bool { match this.kind_of(node) { nk::unary => return !this.target_holds(this.left_of(node)); nk::binary => { val operator := this.token_kind(node); if operator == tk::logand { val left := this.target_holds(this.left_of(node)); return this.target_holds(this.right_of(node)) && left; } if operator == tk::logor { val left := this.target_holds(this.left_of(node)); return this.target_holds(this.right_of(node)) || left; } val key := this.name_of(this.left_of(node)); val value := this.name_of(this.right_of(node)); val held := this.target_value(this.left_of(node), key, this.right_of(node), value); return operator == tk::equals ? held : !held; } else => return false; } return false; } #! Whether the target's value of the key is the value, reporting a key or a #! value no target has. #! @param this The checker. #! @param key_node The node of the key, which a note is placed at. #! @param key The ID of the key, as `os`. #! @param value_node The node of the value. #! @param value The ID of the value, as `linux`. #! @returns Whether it is. def checker::target_value(&mut this, key_node: u32, key: u32, value_node: u32, value: u32): bool { val text := this.names.text(value); if key == word::os { if text != "linux" && text != "macos" && text != "windows" && text != "freebsd" { this.report(value_node, ec::invalid, "`os` is `linux`, `macos`, `windows`, or `freebsd`."); } return value == this.target.os; } if key == word::arch { if text != "x86_64" && text != "aarch64" { this.report(value_node, ec::invalid, "`arch` is `x86_64` or `aarch64`."); } return value == this.target.arch; } if key == word::backend { if text != "vm" && text != "native" { this.report(value_node, ec::invalid, "`backend` is `vm` or `native`."); } return value == this.target.backend; } this.report(key_node, ec::invalid, "The target keys are `os`, `arch`, and `backend`."); return false; } #! The kind of declaration a node declares. #! @param this The checker. #! @param node The node. #! @returns The kind. def checker::decl_kind(&this, node: u32): dk { return match this.kind_of(node) { nk::function => dk::function, nk::variable => dk::variable, nk::field => dk::field, nk::property => dk::property, nk::struct_decl => dk::struct_type, nk::union_decl => dk::union_type, nk::trait_decl => dk::trait_type, nk::enumeration => dk::enum_type, nk::alias => dk::alias_type, nk::assoc_type => dk::assoc_type, nk::enumerator => dk::enum_member, else => dk::none }; } #! The receiver a declaration is made through, as `vec2` of `vec2::length`, or #! 0. #! @param this The checker. #! @param node The declaration's node. #! @returns The receiver's `type_name`, or 0. def checker::receiver_of(&this, node: u32): u32 { 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 => this.record(node, decl_slot::receiver), else => 0 }; } #! The run of a declaration's own type parameters, or 0. #! @param this The checker. #! @param node The declaration's node. #! @returns The run, or 0. def checker::own_params_of(&this, node: u32): u32 { return match this.kind_of(node) { nk::function, nk::struct_decl, nk::union_decl, nk::trait_decl, nk::alias => this.record(node, decl_slot::type_params), else => 0 }; } #! The run of a declaration's attributes, or 0. #! @param this The checker. #! @param node The declaration's node. #! @returns The run, or 0. pub def checker::attributes_of(&this, node: u32): u32 { return match this.kind_of(node) { nk::function, nk::variable, nk::field, nk::property, nk::struct_decl, nk::union_decl, nk::trait_decl, nk::enumeration, nk::alias, nk::assoc_type => this.record(node, decl_slot::attributes), nk::enumerator => this.left_of(node), else => 0 }; } #! Declares the declaration, as a member of `owner` where it is given, or #! else in the module, or waiting for its receiver. #! @param this The checker. #! @param node The declaration's node. #! @param owner The type it is a member of, or 0. #! @returns Its ID, or 0 where it declares nothing. pub def checker::declare_decl(&mut this, node: u32, owner: u32): u32 { val kind := this.decl_kind(node); return 0 if kind == dk::none; val id := this.decls.add(kind, this.name_of(node), this.at.file, node, this.at.home); val flags := this.node_flags(node); if (flags & nf::exported as u32) != 0 { this.decls.mark(id, df::exported); } if (flags & nf::mutable as u32) != 0 { this.decls.mark(id, df::mutable); } if (flags & nf::constant as u32) != 0 { this.decls.mark(id, df::constant); } if kind == dk::assoc_type && (flags & nf::packed as u32) != 0 { this.decls.mark(id, df::packed); } this.mark_attributes(id, node); if this.external && owner == 0 { this.mark_external(id, kind, node); } if kind == dk::function { this.mark_signature(id, node); } # A trait's members and the fields of a struct belong to it as they are # declared. Anything else declared through a type waits for its type. if owner != 0 { this.add_member(owner, id); } else if this.receiver_of(node) != 0 { this.pending.append(id); } else { this.add_global(id); } val own := this.declare_params(this.own_params_of(node), id); this.record_shape(id, this.own_params_of(node)); # A function's type parameters are inferred from its arguments, and an alias is # the type it is written as, so only a struct, union, or trait takes a pattern. if kind == dk::function || kind == dk::alias_type { val run := this.own_params_of(node); for ¶m in this.run_view(run) { val written := this.kind_of(param); if written == nk::type_pattern || written == nk::array_pattern { this.report(param, ec::generic, "Only a struct, union, or trait takes a pattern parameter, as `def(Ts...): R` or `L[N]`."); } } } # The names of generic functions, which an argument naming one waits by. if kind == dk::function && own.length != 0 { val name := this.name_of(node); if this.generic_names.length <= name as u64 { this.generic_names.resize(name as u64 + 1, false); } this.generic_names[name] = true; } if this.receiver_of(node) == 0 { this.set_params(id, own.view()); } else { this.decls.params.p[id] = this.decls.make_list(own.view()); } this.declare_members(id, node); return id; } #! Gives the declaration its own type parameters, which make it generic. #! @param this The checker. #! @param id The declaration. #! @param own Its own type parameters' declarations. def checker::set_params(&mut this, id: u32, own: span): void { this.decls.params.p[id] = this.decls.make_list(own); if own.length != 0 { this.decls.mark(id, df::generic); } } #! Declares the type parameters of the run, each owned by the declaration. #! @param this The checker. #! @param run The run of `type_param` nodes. #! @param owner The declaration they belong to. #! @returns Their declarations. def checker::declare_params(&mut this, run: u32, owner: u32): vector { var made := vector::::new(); for &node in this.run_view(run) { # A pattern, `def(Ts...): R`, declares the type parameters it names, those of # its parameters and then its result's. if this.kind_of(node) == nk::type_pattern { val named := this.declare_params(this.left_of(node), owner); made.extend(named.view()); if this.right_of(node) != 0 { val result := this.decls.add(dk::type_param, this.name_of(this.right_of(node)), this.at.file, this.right_of(node), this.at.home); this.decls.own(result, owner); made.append(result); } continue; } # A pattern, `L[N]`, declares its element's type parameter, and its length's # constant one. if this.kind_of(node) == nk::array_pattern { val element := this.decls.add(dk::type_param, this.name_of(this.left_of(node)), this.at.file, this.left_of(node), this.at.home); this.decls.own(element, owner); made.append(element); if this.right_of(node) != 0 { val length := this.decls.add(dk::type_param, this.name_of(this.right_of(node)), this.at.file, this.right_of(node), this.at.home); this.decls.own(length, owner); this.decls.mark(length, df::valued); made.append(length); } continue; } val id := this.decls.add(dk::type_param, this.name_of(node), this.at.file, node, this.at.home); this.decls.own(id, owner); # A constant parameter stands for a value, whose type it resolves with its bounds. if this.kind_of(node) == nk::const_param { this.decls.mark(id, df::valued); } else if this.node_has(node, nf::packed) { this.decls.mark(id, df::packed); # A function's variadic parameter takes a pack, and a struct, union, or # trait takes one for what it holds or asks for to pass on. val kind: dk = this.decls.kinds.p[owner]; if kind != dk::function && kind != dk::struct_type && kind != dk::union_type && kind != dk::trait_type { this.report(node, ec::generic, "Only a function, struct, union, or trait takes a pack."); } } made.append(id); } return made; } #! Declares the fields of a struct or union, the members of a trait, the #! members of an enum, and the accessors of a property. #! @param this The checker. #! @param id The declaration. #! @param node Its node. def checker::declare_members(&mut this, id: u32, node: u32): void { val kind := this.kind_of(node); if kind != nk::struct_decl && kind != nk::union_decl && kind != nk::trait_decl && kind != nk::enumeration && kind != nk::property { return; } var children := vector::::new(); val members := this.right_of(node); for &member in this.run_view(members) { if kind == nk::property { children.append(this.declare_accessor(id, member)); continue; } val made := this.declare_decl(member, id); if made != 0 { children.append(made); } } this.decls.children.p[id] = this.decls.make_list(children.view()); } #! Declares an accessor of a property, a function of the property's name taking #! `this`, which is no member of its own: reading or writing the property #! calls it. #! @param this The checker. #! @param property The property's declaration. #! @param node The `accessor` node. #! @returns The accessor's declaration. def checker::declare_accessor(&mut this, property: u32, node: u32): u32 { val id := this.decls.add(dk::function, this.decls.names.p[property], this.at.file, node, this.at.home); this.decls.own(id, property); this.decls.mark(id, df::accessor); this.decls.mark(id, df::method); if this.name_of(node) == this.names.intern(&"set") { this.decls.mark(id, df::setter); } if this.decls.has(property, df::exported) || this.node_has(node, nf::exported) { this.decls.mark(id, df::exported); } if this.decls.has(property, df::inline) { this.decls.mark(id, df::inline); } val params := this.left_of(node); if this.run_length(params) != 0 && this.node_has(this.run_at(params, 0), nf::mutable) { this.decls.mark(id, df::mut_this); } return id; } #! Marks what the attributes the compiler reads say of the declaration. #! @param this The checker. #! @param id The declaration. #! @param node Its node. def checker::mark_attributes(&mut this, id: u32, node: u32): void { val attrs := this.attributes_of(node); for &attr in this.run_view(attrs) { continue if this.kind_of(attr) != nk::attribute; val named := this.left_of(attr); val name := this.segment_name(named, this.segment_count(named) - 1); if name == word::intrinsic { this.decls.mark(id, df::intrinsic); } else if name == word::optional { this.decls.mark(id, df::optional); } else if name == word::nodiscard { this.decls.mark(id, df::nodiscard); } else if name == word::allow { this.decls.mark(id, df::allows); } else if name == word::inline { this.decls.mark(id, df::inline); } } } #! Marks what a function's parameters say of it: whether it takes `this`, and #! `&mut this`, and whether its last parameter is variadic. #! @param this The checker. #! @param id The function's declaration. #! @param node Its node. def checker::mark_signature(&mut this, id: u32, node: u32): void { val params := this.record(node, function_slot::params); val count := this.run_length(params); return if count == 0; # `&this` takes the value a method is called on, as does `this` written with # a reference to the method's type, `this: &mut T`. val first := this.run_at(params, 0); if this.name_of(first) == names::this_id { val written := this.left_of(first); if written == 0 { this.decls.mark(id, df::method); if this.node_has(first, nf::mutable) { this.decls.mark(id, df::mut_this); } } else if this.kind_of(written) == nk::type_ref { this.decls.mark(id, df::method); if this.node_has(written, nf::mutable) { this.decls.mark(id, df::mut_this); } } } if this.node_has(this.run_at(params, count - 1), nf::variadic) { this.decls.mark(id, df::variadic); } } #! Declares the declaration in its module, as an overload of the functions of #! its name there, or reporting a name already taken. #! @param this The checker. #! @param id The declaration. def checker::add_global(&mut this, id: u32): void { val home: u32 = this.decls.modules.p[id]; val name: u32 = this.decls.names.p[id]; val first := this.modules.decl(home, name); if first == 0 { this.modules.set_decl(home, name, id); return; } if this.decls.kinds.p[first] == dk::function && this.decls.kinds.p[id] == dk::function { this.decls.chain(first, id); return; } this.report_duplicate(id, first); } #! Makes the declaration a member of the type, as an overload of the functions #! of its name, or reporting a name already taken. A field is reached through a #! value, and a type declared through the type through the type, so those two #! may share a name, as `kind` and `token::kind`. One declared outside `core` on #! `core`'s type, of a name `core` gives it a member or type of too, is kept #! apart from `core`'s. #! @param this The checker. #! @param owner The type's declaration. #! @param id The member's declaration. def checker::add_member(&mut this, owner: u32, id: u32): void { val name: u32 = this.decls.names.p[id]; val types := this.decls.nests(id); val same := this.decls.member_in(owner, name, types); val other := this.decls.member_in(owner, name, !types); # A member or type of `core`'s type declared outside `core`, of a name `core` # gives it too, is kept apart and chosen over `core`'s where it is seen, so # `core` adding one later never refuses a program or takes its calls. val core_id := this.in_core(this.decls.modules.p[id]); if !core_id && ((same != 0 && this.in_core(this.decls.modules.p[same])) || (other != 0 && this.in_core(this.decls.modules.p[other]))) { val first := this.decls.added_member(owner, name); if first == 0 || (this.decls.kinds.p[first] == dk::function && this.decls.kinds.p[id] == dk::function) { this.decls.add_added(owner, id); return; } this.decls.own(id, owner); this.report_duplicate(id, first); return; } # `core`'s, declared after them, takes their place. if core_id && this.decls.added_member(owner, name) == 0 { if same != 0 && !this.in_core(this.decls.modules.p[same]) { this.decls.set_aside(owner, name, types); this.add_member(owner, id); return; } if other != 0 && !this.in_core(this.decls.modules.p[other]) { this.decls.set_aside(owner, name, !types); this.add_member(owner, id); return; } } val shares := other == 0 || this.decls.kinds.p[types ? other : id] == dk::field; if shares && (same == 0 || (this.decls.kinds.p[same] == dk::function && this.decls.kinds.p[id] == dk::function)) { this.decls.add_member(owner, id); return; } this.decls.own(id, owner); this.report_duplicate(id, same != 0 ? same : other); } def checker::report_duplicate(&mut this, id: u32, first: u32): void { this.report_decl_related(id, ec::duplicate, "The name is already declared here.", first, "First declared here."); this.decls.mark(id, df::failed); }