Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

+253
View File
@@ -0,0 +1,253 @@
#! @file reflection.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains what reflection gives a program: descriptions of its declarations,
#! as `core::decl`s, which the checker found while compiling. They are written
#! as data the program holds as long as it runs, the text they name among it,
#! and read where the program stands.
import sakura::analyzer;
module sakura::compiler;
#! What reflection gives an expression: one description, read from the data
#! holding it, or a view of a list of them.
#! @param this The emitter.
#! @param node The expression.
#! @returns The value.
pub def emitter::reflected_value(&mut this, node: u32): operand {
val kind := this.c.checker.type_at(node);
val at := this.c.checker.reflected_at(node);
if at == checker::no_reflection {
this.problem(node, "Reflection gives nothing here.");
return this.zero_value(kind);
}
var items := vector::<u32>::new();
for i in 0..this.c.checker.reflected_count(at) {
items.append(this.c.checker.reflected_item(at, i as u32));
}
if this.c.checker.reflected_kind(at) == rk::one as u32 {
val datum := this.c.descriptions(items.span, kind);
val address := this.build.value(op::datum, span::<u32>::blank(), vt::word, datum, 0);
val place := location { kind = kind, address = address };
return this.load(&place);
}
val view := this.temporary_of(kind);
if items.length == 0 {
this.build.clear(view, 2 * word_size);
return operand::at(kind, view);
}
val element := this.c.viewed(kind);
val datum := this.c.descriptions(items.span, element);
val first := this.build.value(op::datum, span::<u32>::blank(), vt::word, datum, 0);
this.build.store(access::i64, view, 0, first);
this.build.store(access::i64, view, word_size, this.build.word(items.length));
return operand::at(kind, view);
}
#! Declares the variable of a pass of `for const` over a list reflection gives:
#! the description the pass is of.
#! @param this The emitter.
#! @param variable The node of the loop's variable.
pub def emitter::reflected_pass(&mut this, variable: u32): void {
val kind := this.c.checker.type_at(variable);
val made := this.reflected_value(variable);
val place := this.new_place(variable, kind);
this.store(&place, &made);
this.declare(variable, place, false);
}
#! Declares the variable of a pass of `for const` over a list reflection gives
#! where a function the pass doesn't run in names it, as the one evaluating the
#! condition of an `if const` within the pass does: the description the pass is
#! of, as it is in the pass.
#! @param this The emitter.
#! @param variable The node of the loop's variable.
#! @returns Whether it is such a variable, and was declared.
pub def emitter::pass_variable(&mut this, variable: u32): bool {
val at := this.c.checker.reflected_at(variable);
return false if at == checker::no_reflection || this.c.checker.reflected_kind(at) != rk::one as u32;
this.reflected_pass(variable);
return true;
}
#! The datum holding descriptions as `core::decl`s one after another, made where
#! it is new, with the text each names among the program's data.
#! @param this The compiler.
#! @param items The descriptions' indices among the checker's.
#! @param kind The type of each, `core::decl`.
#! @returns The datum's index.
pub def lowerer::descriptions(&mut this, items: span<u32>, kind: u32): u32 {
val plain := this.checker.unalias(kind);
val index := this.layout_of(plain);
val shape := this.layout_at(index);
var bytes := vector::<u8>::new();
bytes.resize(shape.size * items.length);
var relocations := vector::<relocation>::new();
for i in 0..items.length {
val held: desc = this.checker.descs[items[i]];
val base := i * shape.size;
var name := string::new(nothing {});
var path := string::new(nothing {});
var signature := string::new(nothing {});
val decl := held.decl;
if held.param != 0 {
this.param_name(decl, held.param - 1, &mut name);
} else {
name.append(this.checker.names.text(this.checker.decls.names.p[decl]));
}
this.qualified_name(decl, &mut path);
this.signature_text(decl, &mut signature);
val file: u32 = this.checker.decls.files.p[decl];
val source := &this.checker.units[file].source;
val place := source.lines.place_of(this.node_offset(file, this.checker.decls.nodes.p[decl]));
for f in 0..shape.count {
val field := this.field_at(index, f as u32);
val field_name := this.checker.names.text(field.name);
val at := base + field.offset;
if field_name == "id" {
write_number(&mut bytes, at, this.layout_at(field.layout).size, decl as u64);
} else if field_name == "kind" {
val bit := held.param != 0 ? 0x2000u : this.checker.target_bit(decl);
write_number(&mut bytes, at, this.layout_at(field.layout).size, bit);
} else if field_name == "exported" {
write_number(&mut bytes, at, 1, held.param == 0 && this.checker.decls.has(decl, df::exported) ? 1 : 0);
} else if field_name == "generic" {
write_number(&mut bytes, at, 1, held.param == 0 && this.checker.own_param_count(decl) != 0 ? 1 : 0);
} else if field_name == "name" {
this.write_text(&mut bytes, &mut relocations, at, name.span);
} else if field_name == "path" {
this.write_text(&mut bytes, &mut relocations, at, path.span);
} else if field_name == "at" {
val inner := this.layout_at(field.layout);
for s in 0..inner.count {
val part := this.field_at(field.layout, s as u32);
val part_name := this.checker.names.text(part.name);
val within := at + part.offset;
if part_name == "path" {
this.write_text(&mut bytes, &mut relocations, within, source.path);
} else if part_name == "line" {
write_number(&mut bytes, within, this.layout_at(part.layout).size, place.line as u64);
} else if part_name == "column" {
write_number(&mut bytes, within, this.layout_at(part.layout).size, place.column as u64);
} else if part_name == "signature" {
this.write_text(&mut bytes, &mut relocations, within, signature.span);
}
}
}
}
}
val datum := this.program.data.length as u32;
bytes.append(0);
this.program.data.append(datum {
bytes = move(&mut bytes),
align = shape.align < word_size ? word_size : shape.align,
relocations = move(&mut relocations)
});
return datum;
}
#! Writes a number into bytes, as wide as it says, least significant first.
#! @param bytes Where it is written.
#! @param at The first byte.
#! @param size How many bytes it takes.
#! @param value The number.
pub def write_number(bytes: &mut vector<u8>, at: u64, size: u64, value: u64): void {
for i in 0..size {
bytes[at + i] = ((value >> (i * 8)) & 0xFF) as u8;
}
}
#! Writes a view of text into bytes: where the text is, as the address of a
#! datum holding it, and how many bytes it holds. Empty text points nowhere.
#! @param this The compiler.
#! @param bytes Where the view is written.
#! @param relocations Where the address is noted.
#! @param at The first byte of the view.
#! @param text The text.
def lowerer::write_text(&mut this, bytes: &mut vector<u8>, relocations: &mut vector<relocation>, at: u64, text: strview): void {
if text.length != 0 {
relocations.append(relocation { at = at, target = this.text_datum(text), addend = 0 });
}
write_number(bytes, at + word_size, word_size, text.length);
}
#! The name a function's parameter is declared with.
#! @param this The compiler.
#! @param decl The function's declaration.
#! @param index The parameter's position.
#! @param out Where the name is written.
def lowerer::param_name(&mut this, decl: u32, index: u32, out: &mut string): void {
val saved := this.checker.at;
this.checker.use_table(this.checker.units[this.checker.decls.files.p[decl]].table);
val node: u32 = this.checker.decls.nodes.p[decl];
val params := this.checker.decls.has(decl, df::accessor) ? this.checker.left_of(node) : this.checker.record(node, function_slot::params);
if index < this.checker.run_length(params) {
out.append(this.checker.names.text(this.checker.param_name(this.checker.run_at(params, index))));
}
this.checker.at = saved;
}
#! The name a declaration is reached by from outside its module, as
#! `sakura::analyzer::interns_keywords`.
#! @param this The compiler.
#! @param decl The declaration.
#! @param out Where the name is written.
pub def lowerer::qualified_name(&mut this, decl: u32, out: &mut string): void {
var modules := vector::<u32>::new();
var at: u32 = this.checker.decls.modules.p[decl];
var steps := 0;
while at != 0 && steps < 64 {
modules.append(at);
at = this.checker.modules.parents[at];
steps++;
}
var i := modules.length;
while i > 0 {
i--;
out.append(this.checker.names.text(this.checker.modules.names[modules[i]]));
out.append("::");
}
var holder: u32 = this.checker.decls.owners.p[decl];
if holder != 0 && this.checker.decls.kinds.p[holder] == dk::property {
holder = this.checker.decls.owners.p[holder];
}
if holder != 0 && this.checker.decls.kinds.p[holder] != dk::type_param {
out.append(this.checker.names.text(this.checker.decls.names.p[holder]));
out.append("::");
}
out.append(this.checker.names.text(this.checker.decls.names.p[decl]));
}
#! The signature a declaration is written with: its text from where it begins
#! to its body, or its first line.
#! @param this The compiler.
#! @param decl The declaration.
#! @param out Where the text is written.
pub def lowerer::signature_text(&mut this, decl: u32, out: &mut string): void {
val file: u32 = this.checker.decls.files.p[decl];
val node: u32 = this.checker.decls.nodes.p[decl];
return if node == 0;
val syntax := &this.checker.units[file].syntax;
val source := &this.checker.units[file].source;
val begin := syntax.token_offsets[syntax.start_of(node)];
val end_token := syntax.node_end(node);
val end := syntax.token_offsets[end_token] + syntax.token_length(end_token, this.checker.names);
return if end <= begin;
val whole := source.make_lexeme(begin, end - begin);
var stop := 0u;
while stop < whole.length && whole[stop] != '{' && whole[stop] != '\n' {
stop++;
}
while stop > 0 && (whole[stop - 1] == ' ' || whole[stop - 1] == '\t') {
stop--;
}
out.append(whole.substr(0, stop));
}