Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+464
View File
@@ -0,0 +1,464 @@
#! @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.
#!
#! 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 nodes.
var parses: u32; #! How many times its syntax had been parsed whole again when it was last checked.
}
#! The results of a run of nodes of a file: those of the whole file, or of the
#! nodes 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; #! One more than the instance it is of, or 0 for a file's.
pub var parent: u32; #! One more than 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 {
var decl: u32; #! The template.
pub var args: u32; #! The pack of its type arguments, its owner's first.
pub var table: u32; #! One more than its result table, once its body is checked.
pub var kind: u32; #! Its function type.
}
#! 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; #! One more than the instance being checked, which notes name.
var marks: u32; #! Where the bindings of the body begin.
# 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::cursor`
#! 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 branch of an `if const` whose condition only evaluating decides, around
#! what is checked.
pub struct checker::branch {
pub var table: u32; #! The result table the `if const` was checked in.
pub var node: u32; #! The `if_const` node.
pub var holds: bool; #! Whether it is the branch taken where the condition holds.
}
#! A `compile_error` checking leaves to evaluating: one within branches of
#! `if const` only evaluating decides, reported where each is the one taken, or
#! one given text that is not written, which evaluating works out.
pub struct checker::held_error {
pub var table: u32; #! The result table it was checked in.
pub var node: u32; #! The `call` node.
var file: u32; #! The file it is reported in.
var place: u32; #! The node it is reported at: the call, or the declaration whose `at` it was given.
var decl: u32; #! The declaration whose body it is in.
var instance: u32; #! One more than the instance it was checked in, which its note names, or 0.
var detail: string; #! What it says, where checking knew it.
pub var first: u32; #! The first of the branches around it, in `held_branches`.
pub var count: u32; #! How many branches are around it.
pub var computed: bool; #! Whether evaluating works out what it says.
}
#! 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<unit>; #! 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 `--contracts off` asks.
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.
# 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<rtable>; #! The result tables, of each file and each instance.
var r_types: column<u32>; #! The type of each node, by table.
var r_refs: column<u32>; #! What each node refers to, by table.
var r_flags: column<u16>; #! 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 constant each node is, by index in `constants`, by its row, where it is known.
var converts: keymap; #! The type a node no table holds converts to: `table << 32 | node`.
var values: keymap; #! The constant a node no table holds is, by index in `constants`: `table << 32 | node`.
var constants: column<constant>; #! The constants nodes and declarations are, each once, the first a zero constant standing for none.
var constant_index: id_index; #! The constants of each hash, which interning finds them by.
# Bindings: the locals, parameters, and type parameters names refer to, a
# stack cut back where each scope ends. `innermost` holds one more than the
# binding of each name nearest the top.
var bind_names: vector<u32>; #! The name of each binding, from the bottom of the stack.
var bind_values: vector<u32>; #! What each binding refers to.
var bind_prev: vector<u32>; #! The binding of the same name each hides, plus one, or 0.
var innermost: vector<u32>; #! One more than the binding of each name nearest the top, by its name.
# 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<u32, u32>; #! The declaration of each built-in type, by its name.
pub var shelves: vector<library>; #! The libraries the program's modules are found in, which completion lists the modules of by their folders.
var builtin_of: vector<u32>; #! The declaration of each built-in type, by its type.
var pending: vector<u32>; #! Declarations through a type, waiting for it to resolve.
var pending_imports: vector<u64>; #! Each import, as `file << 32 | node`, resolved once every module is declared.
pub var libraries: vector<u64>; #! Each `extern import` the target takes, as `file << 32 | node`, 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<u32, u32>; #! The declarations of std the checker knows, by name.
var selves: dict<u32, u32>; #! The declaration `self` is within each trait.
var decl_values: column<u32>; #! One more than the constant each constant and enum member holds, by index in `constants`, or 0, by its declaration.
# Generics.
pub var instances: vector<instance>; #! The instances of generic functions.
var instanced: keymap; #! One more than the instance of each template and arguments: `decl << 32 | args`.
var wants: vector<u32>; #! 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<u32>; #! One more than the run of the traits a struct, union, or trait names, once worked out, or 0, by its declaration.
var decl_bases: column<u32>; #! 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: keymap; #! One more than the instance a call uses: `table << 32 | node`.
var mirrors: column<u32>; #! The type's parameter each parameter a receiver names stands for, or 0, by the parameter.
var shapes: dict<u32, u32>; #! The written shape of each declaration with a pattern among its type parameters, as `record_shape` makes it.
var only_bounds: dict<u32, bool>; #! Whether each trait is only a bound, once worked out.
var outers: dict<u32, u32>; #! The bounds each trait's method puts on the trait's type parameters and `self`, once worked out.
var locks: vector<u32>; #! What the loops around what is checked iterate.
var undecided: u32; #! How many `if const` and `for const` around what is checked are not decided.
pub var to_evaluate: vector<u64>; #! What only evaluating can work out, once checking is done: local constants, conditions of `if const`, and the text of `compile_error`, each as its table and node.
var branches: vector<checker::branch>; #! The branches of `if const` only evaluating decides around what is checked, the outermost first.
pub var held_errors: vector<checker::held_error>; #! The `compile_error`s left to evaluating, in the order checking reached them.
pub var held_branches: vector<checker::branch>; #! The branches around each held `compile_error`, a run of each.
# Reflection: the descriptions of declarations, and what each expression of
# reflection gives, as `kind, descriptions...` runs of `reflected`.
pub var descs: vector<desc>; #! The descriptions of declarations reflection gives.
var desc_ids: keymap; #! Each description by `decl << 32 | param`.
var reflected: runs<u32>; #! What expressions of reflection give, as runs of `kind, descriptions...`.
var reflections: keymap; #! The run of `reflected` each node gives: `table << 32 | node`.
var lengths: keymap; #! How many values a view of reflection holds: `table << 32 | node`.
var consent: u32; #! A declaration reflection reaches, which its attribute consents to.
var attr_decls: keymap; #! The declaration each attribute is as `attributes_of` gives it: `file << 32 | 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<u32>; #! Of each dependency, the declaration that resolved something.
var dep_to: vector<u32>; #! Of each dependency, what it resolved.
var dep_seen: keymap; #! Which dependencies are recorded, each as 1: `from << 32 | to`.
var depended: vector<bool>; #! Whether any declaration resolved each declaration, whether or not dependencies are recorded.
# 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.
# The evaluator: the frames of the calls it runs, each the values of its
# locals by the node declaring them, the elements of the arrays it holds,
# and the arrays constants hold.
var ev_frames: vector<dict<u32, ev>>; #! The locals of each call being evaluated, by the node declaring them.
var ev_heap: vector<constant>; #! The elements of the arrays being evaluated.
var ev_arrays: dict<u32, ev>; #! The arrays constants hold, by the node declaring them.
var ev_result: ev; #! What the call being evaluated gives.
var ev_steps: u64; #! How many steps evaluation has taken, which a limit stops.
var ev_depth: u32; #! How deeply evaluation's calls nest.
var ev_site: u32; #! The node evaluation was asked at, which its failure is noted at.
var ev_file: u32; #! The file of that node.
var ev_failed: bool; #! Whether evaluation failed.
var projections: keymap; #! The parameter standing for a type a bound asks for: `param << 32 | asked`.
var stand_ins: vector<checker::stand_in>; #! Each parameter standing for a type a bound asks for, which substituting its type parameter resolves.
# Constants: the values constant type arguments are, each interned once.
var knowns: column<known>; #! The values worked out while checking, by ID, the first standing for none.
var known_parts: runs<u32>; #! The parts of each, a run each.
var known_texts: vector<string>; #! The bytes text values hold.
var known_index: id_index; #! The values of each hash, which interning finds them by.
# Bounds an `if const` proved of type parameters, for the branch it guards.
var proved_params: vector<u32>; #! The type parameters an `if const` proved bounds of.
var proved_bounds: vector<u32>; #! The bound proved of each.
# Notes.
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.
# Notes dropped while an argument is tried quietly, and how many of them were
# type arguments nothing gave, which tells one that needs another argument's
# type first from one that is wrong.
var mute: u32; #! Whether notes are dropped, where more than 0.
var dropped: u32; #! How many notes were dropped.
var uninferred: u32; #! How many of those were type arguments nothing gave.
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<bool>; #! Whether a generic function is declared of each name, by its ID.
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<note>): checker {
var made := checker {
names = names,
sink = note_sink::new(notes, checker::max_notes),
units = vector::<unit>::new(),
types = type_table::new(),
decls = decl_table::new(),
modules = module_table::new(),
tables = column::<rtable>::new(),
r_types = column::<u32>::new(),
r_refs = column::<u32>::new(),
r_flags = column::<u16>::new(),
r_converts = keymap::new(),
r_values = keymap::new(),
converts = keymap::new(),
values = keymap::new(),
constants = column::<constant>::new(),
constant_index = id_index::new(),
bind_names = vector::<u32>::new(),
bind_values = vector::<u32>::new(),
bind_prev = vector::<u32>::new(),
innermost = vector::<u32>::new(),
builtins = dict::<u32, u32>::new(),
shelves = vector::<library>::new(),
builtin_of = vector::<u32>::new(),
pending = vector::<u32>::new(),
pending_imports = vector::<u64>::new(),
libraries = vector::<u64>::new(),
known = dict::<u32, u32>::new(),
selves = dict::<u32, u32>::new(),
decl_values = column::<u32>::new(),
instances = vector::<instance>::new(),
instanced = keymap::new(),
wants = vector::<u32>::new(),
satisfied = keymap::new(),
composed = column::<u32>::new(),
decl_bases = column::<u32>::new(),
called = keymap::new(),
mirrors = column::<u32>::new(),
shapes = dict::<u32, u32>::new(),
only_bounds = dict::<u32, bool>::new(),
outers = dict::<u32, u32>::new(),
locks = vector::<u32>::new(),
descs = vector::<desc>::new(),
desc_ids = keymap::new(),
reflected = runs::<u32>::new(),
reflections = keymap::new(),
lengths = keymap::new(),
attr_decls = keymap::new(),
tracks = true,
dep_from = vector::<u32>::new(),
dep_to = vector::<u32>::new(),
dep_seen = keymap::new(),
lifetimes = lifetime_walk::new(),
depended = vector::<bool>::new(),
ev_frames = vector::<dict<u32, ev>>::new(),
ev_heap = vector::<constant>::new(),
ev_arrays = dict::<u32, ev>::new(),
projections = keymap::new(),
stand_ins = vector::<checker::stand_in>::new(),
knowns = column::<known>::new(),
known_parts = runs::<u32>::new(),
known_texts = vector::<string>::new(),
known_index = id_index::new(),
generic_names = vector::<bool>::new(),
proved_params = vector::<u32>::new(),
proved_bounds = vector::<u32>::new(),
to_evaluate = vector::<u64>::new(),
branches = vector::<checker::branch>::new(),
held_errors = vector::<checker::held_error>::new(),
held_branches = vector::<checker::branch>::new()
};
# Constant 0 stands for none, and is the integer 0 too.
_ = made.intern_constant(constant {});
made.declare_builtins();
made.target = target {
os = made.names.intern(&"linux"),
arch = made.names.intern(&"x86_64"),
backend = made.names.intern(&"vm")
};
# Known value 0 stands for none.
made.knowns.append(known {});
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.
#! @returns The index of the file, which its notes give.
pub def checker::add(&mut this, source: &source, syntax: &syntax, root: u32): u32 {
val file := this.units.length as u32;
val table := this.add_table(file, 0, syntax.node_count as u32, 0);
this.units.append(unit {
source = source,
syntax = syntax,
root = root,
table = table,
parses = syntax.parses
});
return file;
}
#! Checks the files given: declares what each declares, resolves what each
#! declaration is, then checks the bodies of functions and the values of
#! variables, and then the bodies of the instances of generics they use.
#! @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 {
this.check_decl(id as u32);
}
this.check_wanted();
return !this.failed;
}
#! 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);
}
}