#! @file decls.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the declaration table, which gives every declaration outside a #! function an ID of its own, with each of its fields in a column of its own #! and the declarations of each file, owner, and module listed, and the modules #! declarations are made in. Locals and parameters are no declarations here: #! each is known by the node declaring it, whose type the results of the body #! it is in hold. import std::containers; module sakura::analyzer; #! The kinds of declaration. pub enum dk : u8 { none, #! Declaration 0, which stands for "no declaration". builtin, #! A type every program has, as `i64`, which methods may be declared through. function, #! A function or method, whose type is its function type. variable, #! A global variable or constant, or one declared through a type. field, #! A field of a struct or union. struct_type, #! A struct. union_type, #! A union. enum_type, #! An enum. enum_member, #! A member of an enum, a constant of it. trait_type, #! A trait. alias_type, #! A type alias. type_param, #! A type parameter of a generic declaration. assoc_type, #! A type a trait asks for. property, #! A property, whose accessors are functions declared through its type. attribute, #! An attribute written on a declaration, as `attributes_of` gives it: a constant of its type, whose value it writes. } #! What a declaration's `flags` hold. pub enum df : u32 { exported = 0x0001, #! `pub`. mutable = 0x0002, #! `var`. constant = 0x0004, #! `const`, or `const def`. method = 0x0008, #! A function taking `this`. mut_this = 0x0010, #! A method taking `&mut this`. variadic = 0x0020, #! A function whose last parameter is variadic. generic = 0x0040, #! A declaration with type parameters of its own or of its owner. intrinsic = 0x0080, #! Given by the compiler, `@intrinsic`. foreign = 0x0100, #! C's: a function without a body, a struct, or a union of an `extern module`. failed = 0x0200, #! Its signature is wrong, so what uses it reports nothing more. optional = 0x0400, #! A trait's member written `@optional`. packed = 0x0800, #! A type parameter that is a pack, `Ts...`. accessor = 0x1000, #! A function that is a property's accessor. setter = 0x2000, #! The `set` of a property. checked = 0x4000, #! Its body has been checked. nodiscard = 0x8000, #! What it gives, or a value of it, must be handled where it is made: `@nodiscard`. valued = 0x10000, #! A type parameter that is a constant parameter, `const N: u64`, whose value's type is its target. bounding = 0x20000, #! A method whose `where` bounds its type's parameters further, so only the types meeting them have it. allows = 0x40000, #! A function whose warnings are quieted: `@allow`. callback = 0x80000, #! A function given where a `cfn` is expected, which C calls back through an entry of its own. inline = 0x100000, #! A function each call of which is copied, at every level: `@inline`, or an accessor of a property written so. } #! How far a declaration's signature is resolved, which says where one depends #! on itself. pub enum ds : u8 { unresolved, #! Not yet resolved. resolving, #! Being resolved, so meeting it again means it depends on itself. resolved, #! Resolved. } #! Every declaration, by its ID, a field in a column of its own. pub struct decl_table { # The columns the compiler reads too, through each one's `p`, without a call. pub var kinds: column; #! The kind of each declaration. pub var names: column; #! The name of each, by its ID. pub var files: column; #! The file each is declared in. pub var nodes: column; #! The node declaring each. pub var modules: column; #! The module each belongs to. pub var owners: column; #! The type a member belongs to, or the declaration a type parameter belongs to. pub var types: column; #! What a variable holds, a function's type, or the type a type declares. pub var overloads: column; #! The next declaration of the same name and place. pub var children: column; #! The run of a type's fields, enum members, or trait members in `lists`, or a type parameter's bounds. var flags: column; #! The flags of each, as `df` says. var states: column; #! How far each is resolved. var params: column; #! The run of a declaration's type parameters in `lists`, its owner's first. var targets: column; #! What an alias stands for, an enum's integer type, or the default of a type a trait asks for. var tops: column; #! The node of the top-level declaration each was declared with, or 0 for what the checker made. var top: u32; #! The node of the top-level declaration being declared, which `add` gives what it adds. var lists: runs; #! The runs `params` and `children` name. var members: keymap; #! The first member of each name of a type: `owner << 32 | name`. var nested: keymap; #! The type of each name declared through a type, as `token::kind`: `owner << 32 | name`. var added: keymap; #! The first member, or the type, of each name a module outside `core` declares through a type `core` gives a member or type of that name too: `owner << 32 | name`. # The declarations of each file, owner, and module, for what walks only those # rather than every declaration. var by_file: decl_index; #! The declarations of each file. var by_owner: decl_index; #! The declarations each declaration has owned: a type's members, or a declaration's type parameters. var by_module: decl_index; #! The declarations each module has held. } #! Makes a declaration table holding declaration 0, which stands for no #! declaration. #! @returns The table. pub def decl_table::new(): decl_table { var made := decl_table { kinds = column::::new(), names = column::::new(), files = column::::new(), nodes = column::::new(), modules = column::::new(), owners = column::::new(), types = column::::new(), flags = column::::new(), states = column::::new(), overloads = column::::new(), params = column::::new(), children = column::::new(), targets = column::::new(), tops = column::::new(), lists = runs::::new(), members = keymap::new(), nested = keymap::new(), added = keymap::new(), by_file = decl_index::new(), by_owner = decl_index::new(), by_module = decl_index::new() }; made.add(dk::none, 0, 0, 0, 0); return made; } #! How many declarations there are. #! @param this The declaration table. #! @returns The number, which is one more than the newest's ID. pub val decl_table::count: u64 { get => this.kinds.length; } #! Appends a declaration. #! @param this The declaration table. #! @param kind Its kind. #! @param name Its name's ID. #! @param file The file it is declared in. #! @param node The node declaring it. #! @param home The module it belongs to. #! @returns Its ID. pub def decl_table::add(&mut this, kind: dk, name: u32, file: u32, node: u32, home: u32): u32 { val id := this.kinds.length as u32; this.kinds.append(kind); this.names.append(name); this.files.append(file); this.nodes.append(node); this.modules.append(home); this.owners.append(0); this.types.append(0); this.flags.append(0); this.states.append(ds::unresolved); this.overloads.append(0); this.params.append(0); this.children.append(0); this.targets.append(0); this.tops.append(this.top); if id != 0 { this.by_file.add(file, id); this.by_module.add(home, id); } return id; } #! Makes a declaration belong to an owner: a member to its type, or a type #! parameter to the declaration it is of. #! @param this The declaration table. #! @param id The declaration. #! @param owner Its owner. pub def decl_table::own(&mut this, id: u32, owner: u32): void { this.owners.p[id] = owner; if owner != 0 { this.by_owner.add(owner, id); } } #! Makes a top-level declaration anew at its new node, as declaring it again #! does: it keeps its ID, kind, name, file, and type, and is otherwise as `add` #! made it, with no owner, overloads, flags, or runs. #! @param this The declaration table. #! @param id The declaration. #! @param node Its new node, which is its top-level declaration's too. #! @param home The module it belongs to now. pub def decl_table::renew(&mut this, id: u32, node: u32, home: u32): void { this.nodes.p[id] = node; this.tops.p[id] = node; this.modules.p[id] = home; this.owners.p[id] = 0; this.overloads.p[id] = 0; this.flags.p[id] = 0; this.states.p[id] = ds::unresolved; this.params.p[id] = 0; this.children.p[id] = 0; this.targets.p[id] = 0; this.by_module.add(home, id); } #! Whether a declaration has a flag. #! @param this The declaration table. #! @param id The declaration. #! @param flag The flag. #! @returns Whether it has it. pub def decl_table::has(&this, id: u32, flag: df): bool { return (this.flags.p[id] & flag as u32) != 0; } #! Gives a declaration a flag. #! @param this The declaration table. #! @param id The declaration. #! @param flag The flag. pub def decl_table::mark(&mut this, id: u32, flag: df): void { this.flags.p[id] |= flag as u32; } #! Adds a run of the items to `lists`. #! @param this The declaration table. #! @param items The items, which are no view of `lists`. #! @returns The run's ID, or 0 for none. pub def decl_table::make_list(&mut this, items: span): u32 { return this.lists.add(items); } #! How many items a run of `lists` holds. #! @param this The declaration table. #! @param run The run's ID. #! @returns How many items it holds. pub def decl_table::list_length(&this, run: u32): u32 { return this.lists.length(run) as u32; } #! The items of a run of `lists`, copied. #! @param this The declaration table. #! @param run The run's ID, or 0 for none. #! @returns The items. pub def decl_table::list_of(&this, run: u32): vector { var made := vector::::new(); made.extend(this.lists.view(run)); return made; } #! The items of a run of `lists`, where they are. #! @param this The declaration table. #! @param run The run's ID, or 0 for none. #! @returns The items, a view only good until a run is added, so a loop that #! may add one, as by resolving a declaration, walks them by #! `list_at`, or copies them by `list_of`. pub def decl_table::list_view(&this, run: u32): span { return this.lists.view(run); } #! An item of a run of `lists`, by its position. #! @param this The declaration table. #! @param run The run's ID. #! @param index The position, below its length. #! @returns The item. pub def decl_table::list_at(&this, run: u32, index: u32): u32 { return this.lists.at(run, index as u64); } #! The first member of the type of the name, or 0. A field and a type declared #! through the type may share a name, and this is the field. #! @param this The declaration table. #! @param owner The type's declaration. #! @param name The name's ID. #! @returns The member, whose overloads follow it, or 0 where the type has none #! of the name. pub def decl_table::member(&this, owner: u32, name: u32): u32 { val found := this.member_in(owner, name, false); return found != 0 ? found : this.member_in(owner, name, true); } #! The member of the type of the name `::` reaches: a type declared through it, #! as `token::kind`, before a field of the same name, which only a value reaches. #! @param this The declaration table. #! @param owner The type's declaration. #! @param name The name's ID. #! @returns The member, whose overloads follow it, or 0 where the type has none #! of the name. pub def decl_table::path_member(&this, owner: u32, name: u32): u32 { val found := this.member_in(owner, name, true); return found != 0 ? found : this.member_in(owner, name, false); } #! The first member of the type of the name among its types, or among the #! rest of its members. #! @param this The declaration table. #! @param owner The type's declaration. #! @param name The name's ID. #! @param types Whether it is a type declared through the type. #! @returns The member, or 0. pub def decl_table::member_in(&this, owner: u32, name: u32, types: bool): u32 { val key := pair_key(owner, name); return types ? this.nested.get_or(&key, 0) : this.members.get_or(&key, 0); } #! The first member of the name a module outside `core` declares on the type, #! where `core` declares one of that name on it too, or 0. Where it is seen, it #! is chosen over `core`'s, so `core` adding a member never takes a program's #! place. #! @param this The declaration table. #! @param owner The type's declaration. #! @param name The name's ID. #! @returns The member, whose overloads follow it, or 0. pub def decl_table::added_member(&this, owner: u32, name: u32): u32 { val key := pair_key(owner, name); return this.added.get_or(&key, 0); } #! Makes a member declared outside `core` one of those `added_member` gives, #! after the others of its name, or the first. #! @param this The declaration table. #! @param owner The type's declaration. #! @param id The member's declaration. pub def decl_table::add_added(&mut this, owner: u32, id: u32): void { this.own(id, owner); val key := pair_key(owner, this.names.p[id]); val first := this.added.get_or(&key, 0); if first == 0 { this.added.set(key, id); } else { this.chain(first, id); } } #! Makes the members or the type of a name declared outside `core` what #! `added_member` gives, once `core` declares a member or type of that name too. #! @param this The declaration table. #! @param owner The type's declaration. #! @param name The name's ID. #! @param types Whether it is a type declared through the type. pub def decl_table::set_aside(&mut this, owner: u32, name: u32, types: bool): void { val key := pair_key(owner, name); val first := this.member_in(owner, name, types); return if first == 0; this.added.set(key, first); if types { this.nested.remove(&key); } else { this.members.remove(&key); } } #! Whether a member is a type declared through its type, which is kept apart #! from the type's other members. #! @param this The declaration table. #! @param id The member's declaration. #! @returns Whether it is. pub def decl_table::nests(&this, id: u32): bool { return match this.kinds.p[id] { dk::struct_type, dk::union_type, dk::enum_type, dk::trait_type, dk::alias_type => true, else => false }; } #! Makes the declaration a member of its type, after the members of its name #! already there. #! @param this The declaration table. #! @param owner The type's declaration. #! @param id The member's declaration. #! @returns The first member of the name. pub def decl_table::add_member(&mut this, owner: u32, id: u32): u32 { this.own(id, owner); val key := pair_key(owner, this.names.p[id]); val types := this.nests(id); val first := this.member_in(owner, this.names.p[id], types); if first == 0 { if types { this.nested.set(key, id); } else { this.members.set(key, id); } return id; } this.chain(first, id); return first; } #! Puts the declaration last among the overloads beginning with `first`. #! @param this The declaration table. #! @param first The first of the overloads. #! @param id The declaration to put last. pub def decl_table::chain(&mut this, first: u32, id: u32): void { var last := first; var walk := this.walk_overloads(first); while true { val at := walk.next(); break if at == 0; last = at; } this.overloads.p[last] = id; } #! A walk along a chain of overloads from its first, in the order they were #! chained: `next` gives each in turn, then 0 from then on. #! #! Each step reads the overload after the one given last as `next` is called, #! so what is chained after it while the walk is on it is met, and the table's #! columns may move as declarations are made. pub struct overload_walk { var decls: &decl_table; #! The table holding the chain. var at: u32; #! The overload given last, or 0 before the first. var first: u32; #! The first overload, until it is given. var left: u32; #! How many more overloads it gives at most. } #! The most overloads a walk gives where it is not limited, more than any chain #! holds. pub const overload_walk::unlimited: u32 = 0xFFFFFFFF; #! A walk along the overloads beginning with `first`. #! @param this The declaration table. #! @param first The first of the overloads, or 0 for none. #! @param limit The most overloads it gives, past which it ends. #! @returns The walk. pub def decl_table::walk_overloads(&this, first: u32, limit: u32 = overload_walk::unlimited): overload_walk { return overload_walk { decls = this, first = first, left = limit }; } #! The next overload of the walk. #! @param this The walk. #! @returns The overload, or 0 where there are no more, or the walk gave as #! many as it was limited to. pub def overload_walk::next(&mut this): u32 { return 0 if this.left == 0; this.left--; # Held in a local, so it is not read back after it is stored, which keeps the # walk short enough to be inlined where it is used. val at := this.at == 0 ? this.first : this.decls.overloads.p[this.at]; this.at = at; this.first = 0; return at; } #! Lists of declarations by a key, as the declarations of each file, each in #! increasing ID order, so walking one meets them in the order walking every #! declaration would. #! #! A declaration is listed under a key once given it, and stays listed once it #! no longer has it, as where it is forgotten or declared again elsewhere, so #! what walks a list asks each declaration for the key, and its kind, as it #! would walking every declaration. pub struct decl_index { var lists: vector>; #! The declarations of each key, in increasing ID order. } #! Makes an index listing no declaration. #! @returns The index. pub def decl_index::new(): decl_index { return decl_index { lists = vector::>::new() }; } #! Lists a declaration under a key, where it is not listed already. #! @param this The index. #! @param key The key, as a file, a module, or a declaration, which are #! counted from 0, so each has a list of its own. #! @param id The declaration. pub def decl_index::add(&mut this, key: u32, id: u32): void { if key as u64 >= this.lists.length { this.lists.resize(key as u64 + 1, vector::::new()); } val list := &mut this.lists[key]; # A declaration just made is the newest, which goes last. if list.length == 0 || list.last() < id { list.append(id); return; } val at := list.view().lower_bound(&id); if list[at] != id { list.insert_at(at, id); } } #! The declarations listed under a key. #! @param this The index. #! @param key The key. #! @returns The declarations, in increasing ID order, a view only good until #! one is listed under the key, so a loop that may add a declaration #! of it, as by resolving one, copies them first. pub def decl_index::of(&this, key: u32): span { return (key as u64) < this.lists.length ? this.lists[key].view() : span::::empty(); } #! The modules of a program. Module 0 is the program itself, which files #! declaring no module are part of. pub struct module_table { pub var names: vector; #! The name of each module, by its ID. pub var parents: vector; #! The module each is within. pub var named: vector; #! Whether a file's `module` line names each, rather than only one within it. var children: keymap; #! Each module by its parent and name: `parent << 32 | name`. var decls: keymap; #! The first declaration of each name a module makes: `module << 32 | name`. } #! Makes a module table holding module 0, the program itself. #! @returns The table. pub def module_table::new(): module_table { var made := module_table { names = vector::::new(), parents = vector::::new(), named = vector::::new(), children = keymap::new(), decls = keymap::new() }; made.names.append(0); made.parents.append(0); made.named.append(true); return made; } #! The module of the name within the parent, made where it is new. #! @param this The module table. #! @param parent The module it is within. #! @param name Its name's ID. #! @returns The module. pub def module_table::child(&mut this, parent: u32, name: u32): u32 { val key := pair_key(parent, name); if this.children.get(&key) is some(got) { return got; } val id := this.names.length as u32; this.names.append(name); this.parents.append(parent); this.named.append(false); this.children.set(key, id); return id; } #! The module of the name within the parent, or 0 where there is none. #! @param this The module table. #! @param parent The module it is within. #! @param name Its name's ID. #! @returns The module, or 0. pub def module_table::find(&this, parent: u32, name: u32): u32 { val key := pair_key(parent, name); return this.children.get_or(&key, 0); } #! The first declaration of the name the module makes, or 0. #! @param this The module table. #! @param home The module. #! @param name The name's ID. #! @returns The declaration, whose overloads follow it, or 0. pub def module_table::decl(&this, home: u32, name: u32): u32 { val key := pair_key(home, name); return this.decls.get_or(&key, 0); } #! Records the first declaration of a name a module makes. #! @param this The module table. #! @param home The module. #! @param name The name's ID. #! @param id The declaration. pub def module_table::set_decl(&mut this, home: u32, name: u32, id: u32): void { this.decls.set(pair_key(home, name), id); } #! Whether `inner` is `outer`, or within it. #! @param this The module table. #! @param inner The module asked about. #! @param outer The module it may be within. #! @returns Whether it is. pub def module_table::within(&this, inner: u32, outer: u32): bool { var at := inner; var steps := 0; while steps < 64 { return true if at == outer; return false if at == 0; at = this.parents[at]; steps++; } return false; }