#! @file checker.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the checker, which works out what the parsed files of a program #! mean: what each name refers to, the type of each expression, and whether #! the program is valid, and `check`, where it begins. What it checks is split #! across the files of the `checker` directory, one for each part of the #! language, as the parser's are. #! #! What it checks whole is the program's own files. The bodies and values of a #! library's declarations are checked once something checked reaches them, so #! what nothing in the program uses of a library is not checked, nor are notes #! made of it, but where every declaration is asked for, as `check --all` does. #! #! The checker never changes a syntax. What it works out is kept in tables of #! its own: the types and declarations of the type table and declaration #! table, and the results of each node, which a result table holds for the #! node's file, or for the instance of a generic the node is checked in. An #! instance is checked over its template's nodes with a result table of its #! own, so nothing of the template is ever copied. import std::containers; module sakura::analyzer; #! A file the checker is given: its source, its syntax, and what the checker #! keeps of it. pub struct unit { pub var source: &source; #! The file's source, which its notes are placed in. pub val syntax: &syntax; #! What the file parsed into. var root: u32; #! The `document` node. var imports: u32; #! The run of the modules it imports, in the lists of the declarations. pub var table: u32; #! The result table of its newest nodes, where its checking begins. var segments: vector; #! Its result tables, each of a run of its nodes after the one before, the first of those it was parsed with, and each after of those an edit added. var parses: u32; #! How many times its syntax had been parsed whole again when it was last checked. pub var own: bool; #! Whether it is one of the program's own files, which are checked whole, rather than a library's. } #! The results of a run of nodes of a file: those it was parsed with, those an #! edit added, or those of a template, checked for one of its instances. A #! node's results are at `offset + node - base` of the result vectors. pub struct rtable { pub var file: u32; #! The file whose nodes it holds the results of. pub var base: u32; #! The first node it holds. pub var count: u32; #! How many nodes it holds. var offset: u32; #! Where its results begin in the result vectors. var instance: u32; #! The instance it is of, or 0 for a file's. pub var parent: u32; #! The table it was made within, which holds the nodes it does not, or 0. } #! A value known while compiling: an integer, as its magnitude and sign, a #! float, or a `bool`, as a magnitude of 0 or 1. An integer's magnitude and a #! float's bits share one word, which `magnitude` and `real` read as the kind #! of constant says, so it takes sixteen bytes rather than thirty-two. pub struct constant { var word: u64; #! An integer's magnitude, a `bool` as 0 or 1, or a float's bits. pub var negative: bool; #! Whether an integer is below zero. pub var floating: bool; #! Whether it is a float, whose bits `word` holds. } #! An instance of a generic function: its template, given type arguments, whose #! body is checked with a result table of its own once something uses it. pub struct instance { pub var decl: u32; #! The template. pub var args: u32; #! The pack of its type arguments, its owner's first. pub var table: u32; #! Its result table, once its body is checked, or 0. pub var kind: u32; #! Its function type, or of a variable's instance, its type. pub var value: u32; #! Of a constant's instance, its value among the checker's known values, or 0 where checking did not work it out. pub var flawed: bool; #! Whether checking its body found an error, so it is never run while compiling. var made_file: u32; #! The file of the node first asking for it, as the call making it. var made_node: u32; #! That node, or 0 where nothing written asked for it. var made_in: u32; #! The instance that node was checked in, or 0 outside one. } #! Where checking is: what a name resolves within, and what a body's #! statements are checked against. It is saved and put back as a whole where #! checking moves to another declaration, so nothing of one leaks into another. pub struct checker::frame { pub var file: u32; #! The file being checked. pub var table: u32; #! The result table being written. var base: u32; #! Of that table, kept here so reaching a node's results is quick. var offset: u32; #! Of that table, where its results begin. var count: u32; #! Of that table, how many nodes it holds. var decl: u32; #! The declaration whose signature or body is checked. var scope: u32; #! The type whose own names are named without it there, or 0. var home: u32; #! The module names resolve in. var result: u32; #! What `return` gives, in a function's body. var loops: u32; #! How many loops are around what is checked. var block: u32; #! The block whose statements are checked. var opened: u32; #! Where the bindings of that block begin, past which a local of the same name is declared twice. var body: u32; #! The block of the function whose body is checked, which every `return` leaves, so its `ensures` may name what it returns. pub var instance: u32; #! The instance being checked, which notes name, or 0. var marks: u32; #! Where the bindings of the body begin. var given: bool; #! Whether the declaration's type parameters are given types, as where a constant kept as its expression is worked out again, so it is no template. # The fields of the nodes and tokens of the file being checked, which never # change while it is checked, so each is read where it is without a call. var kinds: *nk; #! The kind of each node. var tokens: *u32; #! The token each node says what it is by. var lefts: *u32; #! The first slot of each node. var rights: *u32; #! The second slot of each node. var extras: *u32; #! The records and runs the slots begin at. var names: *u32; #! The name of each token that is a word, or 0. var tkinds: *tk; #! The kind of each token. } #! The target a program is checked for, which `when` decides by, each value #! the ID of its name. pub struct target { pub var os: u32; #! The operating system's name, as `linux`. pub var arch: u32; #! The processor's name, as `x86_64`. pub var backend: u32; #! How the program runs, as `vm`. } #! A type parameter standing for a type a bound asks of another, as #! `C::iterator` where `C: walk`, which becomes the type the type given for `C` #! gives. pub struct checker::stand_in { var base: u32; #! The type parameter's type it is asked of. var asked: u32; #! The declaration of the type the bound asks for. var param: u32; #! The declaration of the parameter standing for it. var bound: u32; #! The bound asking for it. } #! A note said while a body is checked once for each case it holds in, held #! until it is, so it is said once. pub struct checker::held_note { var key: u64; #! Its file, node, and code, by which it is said once. var serial: u64; #! The pass it was last said in. var passes: u32; #! How many passes said it. var made: note; #! The note. var text: string; #! What the case it was first said in says. } #! A type parameter standing for a pack's type at an index while the pack's #! types are not known, as `Ts...[0]`. pub struct checker::element_in { var pack: u32; #! The pack's type. var index: u32; #! The index. var param: u32; #! The declaration of the parameter standing for it. var generic: u32; #! The generic whose parameter at the index it stands for in a shape, as `vector` of `T is vector`, or 0. var rest: bool; #! Whether it stands for the types from the index on, as a generic's pack parameter takes them. var at: u32; #! The constant type argument the index is, where only each instance knows it, as `I` of `Ts...[I]`, or 0. } #! The checker. pub struct checker { pub var names: &mut names; #! The names the files were lexed with, which the checker interns more into. pub var sink: note_sink; #! Where what the checker finds is noted, kept or heard as it is made, which `max_notes` bounds. pub var units: vector; #! The files it is given, by their index. pub var types: type_table; #! Every type, by its ID. pub var decls: decl_table; #! Every declaration, by its ID. pub var modules: module_table; #! Every module, by its ID. pub var target: target; #! The target the program is checked for. pub var contracts_off: bool; #! Whether contracts are left out, conditions and all, as an unchecked program and `--contracts off` ask. pub var checks_off: bool; #! Whether the checks a checked build makes are left out, as a build without `--checks` and `run --fast` ask. pub var at: checker::frame; #! Where checking is. pub var whole: bool; #! Whether every declaration is checked, a library's too, rather than those the program's own files reach. # Results: a table for each file and each instance, whose nodes' types, # what they refer to, and flags are runs of these columns, a row for each # node. The few nodes that convert, or whose values are known, are found by # their rows in maps of their own, rather than in columns mostly of zeros. pub var tables: column; #! The result tables, of each file and each instance, the first standing for none. var r_types: column; #! The type of each node, by table. var r_refs: column; #! What each node refers to, by table. var r_flags: column; #! The flags of each node, by table. var r_converts: keymap; #! The type each node converts to, where it is not its own, by its row. var r_values: keymap; #! The value each node is, a number among `knowns`, by its row, where it is known. var converts: keyed; #! The type a node no table holds converts to, by its table and node. var values: keyed; #! The value a node no table holds is, a number among `knowns`, by its table and node. # Bindings: the locals, parameters, and type parameters names refer to, a # stack cut back where each scope ends, the first standing for none. # `innermost` holds the binding of each name nearest the top. var bind_names: vector; #! The name of each binding, from the bottom of the stack. var bind_values: vector; #! What each binding refers to. var bind_prev: vector; #! The binding of the same name each hides, or 0. var innermost: vector; #! The binding of each name nearest the top, by its name, or 0. # Declarations: those every program has, those declared through a type that # wait for their type to resolve, and those of std the checker gives meaning. var builtins: dict; #! The declaration of each built-in type, by its name. pub var shelves: vector; #! The libraries the program's modules are found in, which completion lists the modules of by their folders. var builtin_of: vector; #! The declaration of each built-in type, by its type. var pending: vector; #! Declarations through a type, waiting for it to resolve. var pending_imports: vector; #! Each import, resolved once every module is declared. pub var libraries: vector; #! Each `extern import` the target takes, which names the library of its file's C functions. var external: bool; #! Whether what is being declared is in an `extern module`, where functions without a body, structs, and unions are C's. var cores_own: u32; #! How deeply what a trait of `core`'s asks for is being met, where `core`'s types meet it by `core`'s own members. var known: dict; #! The declarations of std the checker knows, by name. var selves: dict; #! The declaration `self` is within each trait. var decl_values: column; #! The value each constant and enum member holds, a number among `knowns`, or 0, by its declaration. # Generics. pub var instances: vector; #! The instances of generic functions, the first standing for none. var instanced: keymap; #! The instance of each template and arguments: `decl << 32 | args`. var wants: vector; #! Instances used whose bodies are not yet checked. var wants_checked: u64; #! How many of `wants` have been checked, which checking goes on from. var satisfied: keymap; #! Whether a type satisfies a trait: `type << 32 | trait`. var composed: column; #! The run of the traits a struct, union, or trait names, once `df::composed` says they are worked out, by its declaration. var decl_bases: column; #! The first node of each generic declaration, whose nodes an instance's table holds, once worked out, or 0, by its declaration. pub var called: keyed; #! The instance a call uses, by its table and node. var mirrors: column; #! The type's parameter each parameter a receiver names stands for, or 0, by the parameter. var shapes: dict; #! The written shape of each declaration with a pattern among its type parameters, as `record_shape` makes it. var only_bounds: dict; #! Whether each trait is only a bound, once worked out. var outers: dict; #! The bounds each trait's method puts on the trait's type parameters and `self`, once worked out. var walked: vector; #! The places the loops around what is checked walk, each its table then its node, which changing whole may move what the loop reads. var undecided: u32; #! How many `if const` and `for const` around what is checked are not decided. # Reflection: the descriptions of declarations, and what each expression of # reflection gives, as `kind, descriptions...` runs of `reflected`. pub var descs: vector; #! The descriptions of declarations reflection gives. var desc_ids: keymap; #! Each description by `decl << 32 | param`. var reflected: runs; #! What expressions of reflection give, as runs of `kind, descriptions...`. var reflections: keyed; #! The run of `reflected` each node gives, by its table and node. var lengths: keyed; #! How many values a view of reflection holds, by its table and node. var consent: u32; #! A declaration reflection reaches, which its attribute consents to. var attr_decls: keyed; #! The declaration each attribute is as `attributes_of` gives it, by its file and node. # Dependencies, recorded as declarations are checked, for checking again # only what an edit affects: from each top-level declaration to what it # resolved, or to a name with `named_dependency` set. pub var tracks: bool; #! Whether dependencies are recorded, which a program checked only once has no use for. var dep_from: vector; #! Of each dependency, the declaration that resolved something. var dep_to: vector; #! Of each dependency, what it resolved. var dep_seen: keymap; #! Which dependencies are recorded, each as 1: `from << 32 | to`. var depended: vector; #! Whether any declaration resolved each declaration, whether or not dependencies are recorded. var queued: vector; #! The declarations checking reached, in the order it did, each once, whose own checking follows. var queued_checked: u64; #! How many of `queued` have been checked, which checking goes on from. pub var checked_globals: u32; #! How many globals have been checked, which tells lowering whether checking late reached one. # What walks each function's body to warn of what it keeps past what it # refers to, made once and walking each in turn. var lifetimes: lifetime_walk; #! The walk. # What runs code while checking, where a value must be known that folding does # not give: the program's machine, which whoever checks a program to run or # build it gives. pub var evaluator: maybe; #! The program's evaluator, or nothing. var projections: keymap; #! The parameter standing for a type a bound asks for: `param << 32 | asked`. var shape_params: keymap; #! The parameter standing for a generic's parameter in a shape: `pack << 32 | generic << 8 | index`. var stand_ins: vector; #! Each parameter standing for a type a bound asks for, which substituting its type parameter resolves. var element_ins: vector; #! Each parameter standing for a pack's type at an index, which substituting the pack resolves. var settling_index: u32; #! How deeply substituting works out an index of a pack's type, within which those it reads are left as they are. var indexed: keymap; #! The declarations of the parameters substituting which resolves a parameter standing for a pack's type at an index, each as `decl -> 1`. # Constants: the values constant type arguments are, each interned once. var knowns: column; #! The values worked out while checking, by ID, the first standing for none. var known_parts: runs; #! The parts of each, a run each. var known_texts: vector; #! The bytes text values hold. var known_index: id_index; #! The values of each hash, which interning finds them by. var worked_again: keymap; #! What each value kept as its expression within a template is, worked out again given types: by `id << 32 | given`, 1 more than its ID, or 0 while being worked out. # Following aliases reaches nearly all of the checker, as choosing a type may # work out a constant again, so `unalias` calls it through this: calling # nothing directly, `unalias` is optimized before whatever calls it, which # then inlines it, however the checker's calls loop. var follows: def(&mut checker, u32): u32; #! `follow_aliases`. var value_index: id_index; #! The numbers and floats of no type in particular, by the hash of the constant each is, which nodes and declarations are, found quickly. # What an `if const` branch or a `match const` arm says of a type parameter # within a generic's own body, for what it guards: a bound it has, by its # declaration, or with `fit_known` added, a pattern it fits, or with # `unfit_known` added, one it does not. var proved_params: vector; #! The type parameters an `if const` proved bounds of. var proved_bounds: vector; #! The bound proved of each. # A body checked once for each case its condition holds in, as `if const A || # B` or an arm of several patterns, says each note once, with the case where # only one case says it. var repeating: u32; #! How many such bodies are being checked within one another. var held_notes: vector; #! The notes said while one is, said once it ends. var case_serial: u64; #! Which pass in a case is checking, counted up. var using_ways: u32; #! How many uses of values of types not chosen yet are checked within one another. var choices: u32; #! How many types `match const` and `C ? A : B` made, without which no type is one not chosen yet. var sealed_depth: u32; #! How many sealed traits' constants the cases of a condition are worked out within. var case_text: string; #! What the case a note is said in says, or empty. # Notes. pub var failed: bool; #! Whether the program is not valid. var errors: u64; #! How many errors were reported, which a function's body must add none of to be warned of. pub var skipped: u32; #! What was met that the checker does not check yet, and let through. # What is checked for its results alone, whose notes are not the program's: # a type written for `this`, which `this` is whatever it says; an attribute's # type as reflection reads it, which checking its declaration reports; and # what a query for an editor checks. var quiet: u32; #! Whether notes are left out, where more than 0. var waits_for: u32; #! The call being checked where it may wait for its parameter's type, as `waiting` checks one. var free_result: bool; #! Whether the next lambda takes its result from its body, or the next generic function named as a value from its parameters, whatever the type expected says. var generic_names: vector; #! Whether a generic function is declared of each name, by its ID. var named_paths: vector; #! What each path checked names, as `path_target` found it, the first standing for none. var named_ids: keyed; #! Each path's index in `named_paths`, by its table and node. var chosen_arms: keyed; #! The arm each `match const` decided chose, by its table and node. var reducing: u32; #! How many `match const` types are being chosen within one another. var scratch: u32; #! A type parameter bounded by nothing, which what one bound alone gives is asked of, or 0 before it is made. var exact_facts: u32; #! How many of what is proved say a parameter is a type, as `T is strview` does, by which the types of what is checked are read. var rest_cases: checker::cases; #! The cases what follows an `if const` whose branch leaves is checked in, where they are several. var rest_pending: bool; #! Whether `rest_cases` waits for the block holding the `if const` to check what follows it. var choosing_aliases: u32; #! How many aliases of `match const` are declared, so a type is looked through for them only where there are. var owned_names: bool; #! Whether types declared through others are described with them, as `token::kind`, as an editor shows them. var qualified_names: bool; #! Whether types are described with the modules they're declared in, as `path_of` spells them. } #! The most notes the checker makes. pub const checker::max_notes: u32 = 200; #! How many notes the checker has made, whether kept in its notes or heard as #! they were made. #! @param this The checker. #! @returns How many, at most `max_notes`. pub val checker::notes_made: u32 { get => this.sink.count; } #! How deeply type arguments may nest, which a generic instantiating itself #! with ever larger types soon passes. pub const checker::max_nesting: u32 = 32; #! The most instances the checker makes. pub const checker::max_instances: u32 = 65536; #! Makes a checker of the names the files were lexed with, which notes what it #! finds in `notes`. #! @param names The names the files were lexed with. #! @param notes Where what the checker finds is noted. #! @returns The checker, which knows the built-in types and the names it looks #! for. pub def checker::new(names: &mut names, notes: &mut vector): checker { var made := checker { names = names, sink = note_sink::new(notes, checker::max_notes), units = vector::::new(), types = type_table::new(), decls = decl_table::new(), modules = module_table::new(), tables = column::::new(), r_types = column::::new(), r_refs = column::::new(), r_flags = column::::new(), r_converts = keymap::new(), r_values = keymap::new(), converts = keyed::::new(), values = keyed::::new(), bind_names = vector::::new(), bind_values = vector::::new(), bind_prev = vector::::new(), innermost = vector::::new(), builtins = dict::::new(), shelves = vector::::new(), builtin_of = vector::::new(), pending = vector::::new(), pending_imports = vector::::new(), libraries = vector::::new(), known = dict::::new(), selves = dict::::new(), decl_values = column::::new(), instances = vector::::new(), instanced = keymap::new(), wants = vector::::new(), satisfied = keymap::new(), composed = column::::new(), decl_bases = column::::new(), called = keyed::::new(), mirrors = column::::new(), shapes = dict::::new(), only_bounds = dict::::new(), outers = dict::::new(), walked = vector::::new(), descs = vector::::new(), desc_ids = keymap::new(), reflected = runs::::new(), reflections = keyed::::new(), lengths = keyed::::new(), attr_decls = keyed::::new(), tracks = true, dep_from = vector::::new(), dep_to = vector::::new(), dep_seen = keymap::new(), lifetimes = lifetime_walk::new(), depended = vector::::new(), queued = vector::::new(), projections = keymap::new(), shape_params = keymap::new(), indexed = keymap::new(), stand_ins = vector::::new(), element_ins = vector::::new(), knowns = column::::new(), known_parts = runs::::new(), known_texts = vector::::new(), known_index = id_index::new(), worked_again = keymap::new(), value_index = id_index::new(), generic_names = vector::::new(), named_paths = vector::::new(), named_ids = keyed::::new(), chosen_arms = keyed::::new(), proved_params = vector::::new(), proved_bounds = vector::::new(), held_notes = vector::::new(), rest_cases = checker::cases::none(), case_text = string::new(nothing {}), evaluator = nothing {}, follows = checker::follow_aliases }; # Known value 0 stands for none, which reads as a zero, and is never interned, # so a zero interned is a value of its own; so do path 0, table 0, and # instance 0. made.knowns.append(known {}); made.named_paths.append(checker::named {}); made.tables.append(rtable {}); made.instances.append(instance {}); made.declare_builtins(); made.target = target { os = made.names.intern(&"linux"), arch = made.names.intern(&"x86_64"), backend = made.names.intern(&"vm") }; # Binding 0 stands for none too. made.bind_names.append(0); made.bind_values.append(0); made.bind_prev.append(0); return made; } #! Gives the checker a file to check, lexed with its names and parsed. #! @param this The checker. #! @param source The file's source. #! @param syntax What it parsed into. #! @param root Its `document` node. #! @param own Whether it is one of the program's own files, checked whole, #! rather than a library's, whose declarations are checked #! where the program reaches them. #! @returns The index of the file, which its notes give. pub def checker::add(&mut this, source: &source, syntax: &syntax, root: u32, own: bool = true): u32 { val file := this.units.length as u32; val table := this.add_table(file, 0, syntax.node_count as u32, 0); var segments := vector::::new(); segments.append(table); this.units.append(unit { source = source, syntax = syntax, root = root, table = table, parses = syntax.parses, own = own, segments = move(&mut segments) }); return file; } #! Checks the files given: declares what each declares and resolves what each #! declaration is, then checks the bodies of the functions and the values of #! the variables of the program's own files, then of what those reach, a #! library's declarations and the instances of generics, until they reach #! nothing new. Every declaration's signature is resolved first, whatever #! reaches it, since much of checking reads what one is without asking. #! @param this The checker. #! @returns Whether the program is valid. pub def checker::check(&mut this): bool { for file in 0..this.units.length { this.declare_unit(file as u32); } this.link(); this.name_types(); for id in 1..this.decls.count { this.resolve(id as u32); } for id in 1..this.decls.count { if this.is_root(id as u32) { this.check_decl(id as u32); } } this.check_reached(); return !this.failed; } #! Whether a declaration is checked whatever reaches it: one of the program's #! own files, or any where every declaration is checked. #! @param this The checker. #! @param id The declaration. #! @returns Whether it is. def checker::is_root(&this, id: u32): bool { return this.whole || this.decls.nodes.p[id] != 0 && this.units[this.decls.files.p[id]].own; } #! Checks what checking reached and has not checked yet: the declarations it #! resolved, and the instances of generics it used, and what those reach in #! turn, until they reach nothing new. What calls `system::handle` has its #! signals handled by `system::dispatch`, which the compiler calls where nothing #! written does, so it is reached with it. #! @param this The checker. pub def checker::check_reached(&mut this): void { while true { while this.queued_checked < this.queued.length { val id: u32 = this.queued[this.queued_checked]; this.queued_checked++; this.check_decl(id); } this.check_wanted(); if this.queued_checked == this.queued.length { val dispatch := this.dispatch_needed(); if dispatch != 0 { this.reach(dispatch); } } break if this.queued_checked == this.queued.length && this.wants_checked == this.wants.length; } } #! `system::dispatch`, where something reached `system::handle` and nothing #! reached it. #! @param this The checker. #! @returns Its declaration, or 0. def checker::dispatch_needed(&mut this): u32 { val sys := this.modules.find(0, word::system); return 0 if sys == 0; val handle := this.modules.decl(sys, this.names.intern(&"handle")); return 0 if handle == 0 || !this.is_depended(handle); val dispatch := this.modules.decl(sys, this.names.intern(&"dispatch")); return 0 if dispatch == 0 || this.is_depended(dispatch); return dispatch; } #! A built-in type and the names it goes by. struct checker::builtin_name { val text: strview; val kind: bt; } const _builtin_names: checker::builtin_name[15] = [ checker::builtin_name { text = "void", kind = bt::void_type }, checker::builtin_name { text = "bool", kind = bt::boolean }, checker::builtin_name { text = "i8", kind = bt::int8 }, checker::builtin_name { text = "i16", kind = bt::int16 }, checker::builtin_name { text = "i32", kind = bt::int32 }, checker::builtin_name { text = "i64", kind = bt::int64 }, checker::builtin_name { text = "u8", kind = bt::uint8 }, checker::builtin_name { text = "u16", kind = bt::uint16 }, checker::builtin_name { text = "u32", kind = bt::uint32 }, checker::builtin_name { text = "u64", kind = bt::uint64 }, checker::builtin_name { text = "f32", kind = bt::float32 }, checker::builtin_name { text = "f64", kind = bt::float64 }, checker::builtin_name { text = "int", kind = bt::int64 }, checker::builtin_name { text = "uint", kind = bt::uint64 }, checker::builtin_name { text = "float", kind = bt::float64 }, ]; #! Declares the built-in types, one declaration for each type, which every #! name it goes by finds, so `int::f` and `i64::f` are methods of one type. #! @param this The checker. def checker::declare_builtins(&mut this): void { this.builtin_of.resize(bt::count as u64); for &each in _builtin_names { val name := this.names.intern(&each.text); var decl: u32 = this.builtin_of[each.kind as u64]; if decl == 0 { decl = this.decls.add(dk::builtin, name, 0, 0, 0); this.decls.types.p[decl as u32] = each.kind; this.decls.states.p[decl as u32] = ds::resolved; this.decls.mark(decl, df::exported); this.builtin_of[each.kind as u64] = decl; } this.builtins.set(name, decl); } }