Files
sakurac/analyzer/checker/reflection.sakura
T
2026-12-06 13:52:57 -06:00

529 lines
21 KiB
Plaintext

#! @file reflection.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains reflection: what a program reads of its own declarations while
#! compiling. A description names a declaration, or a function's parameter,
#! and an expression giving one, or a list of them, as `declarations()` or
#! `params_of(d)`, has them as a result of its own. What they say that `if
#! const` decides by, as `d.kind` or how many there are, is known while
#! checking, and `for const` walks a list, checking its body once for each.
module sakura::analyzer;
#! What reflection describes: a declaration, or one of a function's parameters,
#! and where it is in a generic type, the instance it was reached through, as
#! `page<int>` of `decl_of::<page<int>>()` and of each of its members.
pub struct desc {
pub var decl: u32; #! The declaration.
pub var param: u32; #! One more than the index of the parameter it is, or 0 for the declaration itself.
var owner: u32; #! The instance whose arguments the generic type's parameters take, or 0.
var next: u32; #! The description of the same declaration through another instance, or `no_reflection`.
}
#! The kinds of what an expression of reflection gives.
pub enum rk : u32 {
one = 1, #! A description.
list = 2, #! A list of descriptions.
}
#! The description of the declaration, or of its parameter, made where it is
#! new.
#! @param this The checker.
#! @param decl The declaration.
#! @param param One more than the index of the parameter, or 0 for the
#! declaration itself.
#! @returns The description's index in `descs`.
pub def checker::describe_decl(&mut this, decl: u32, param: u32): u32 {
return this.describe_owned(decl, param, 0);
}
#! The description of the declaration, or of its parameter, reached through the
#! instance of the generic type it is in, made where it is new.
#! @param this The checker.
#! @param decl The declaration.
#! @param param One more than the index of the parameter, or 0 for the
#! declaration itself.
#! @param owner The instance it is reached through, or 0.
#! @returns The description's index in `descs`.
pub def checker::describe_owned(&mut this, decl: u32, param: u32, owner: u32): u32 {
val key := pair_key(decl, param);
var first := checker::no_reflection;
if this.desc_ids.get(&key) is some(got) {
first = got;
}
var at := first;
while at != checker::no_reflection {
val held: desc = this.descs[at];
return at if held.owner == owner;
at = held.next;
}
val id := this.descs.length as u32;
this.descs.append(desc { decl = decl, param = param, owner = owner, next = first });
this.desc_ids.set(key, id);
return id;
}
#! The instance a member of the description's declaration is reached through:
#! the description's own, where it describes a generic type through one.
#! @param this The checker.
#! @param held The description.
#! @returns The instance, or 0.
def checker::member_owner(&mut this, held: desc): u32 {
return 0 if held.owner == 0 || this.type_decl(held.owner) != held.decl;
return held.owner;
}
#! Records that the node gives the descriptions, one or a list of them.
#! @param this The checker.
#! @param node The node.
#! @param kind Whether it gives one or a list.
#! @param items The descriptions' indices in `descs`.
pub def checker::set_reflected(&mut this, node: u32, kind: rk, items: span<u32>): void {
this.reflected.push(kind as u32);
for &each in items {
this.reflected.push(each);
}
this.reflections.set(this.table_node_of(node), this.reflected.end());
this.set_flag(node, rf::reflected);
}
#! Where the descriptions the node gives are recorded, or `no_reflection`.
#! @param this The checker.
#! @param node The node.
#! @returns Their run in `reflected`, or `no_reflection`.
pub def checker::reflected_at(&this, node: u32): u32 {
# A node a table holds says whether it gives any, which most give none of.
val at := this.slot(node);
if at < this.r_flags.length && (this.r_flags.p[at] & rf::reflected as u16) == 0 {
return checker::no_reflection;
}
val key := this.table_node_of(node);
return this.reflections.get_or(&key, checker::no_reflection);
}
#! What says a node gives no descriptions, and a description has no other
#! instance.
pub const checker::no_reflection: u32 = 0xFFFFFFFF;
#! Whether what a node gives is one description or a list, as `rk` says.
#! @param this The checker.
#! @param at Its run in `reflected`.
#! @returns The `rk`, as an integer.
pub def checker::reflected_kind(&this, at: u32): u32 { return this.reflected.at(at, 0); }
#! How many descriptions a node gives.
#! @param this The checker.
#! @param at Their run in `reflected`.
#! @returns How many.
pub def checker::reflected_count(&this, at: u32): u32 { return (this.reflected.length(at) - 1) as u32; }
#! One of the descriptions a node gives.
#! @param this The checker.
#! @param at Their run in `reflected`.
#! @param index Its position, below their count.
#! @returns The description's index in `descs`.
pub def checker::reflected_item(&this, at: u32, index: u32): u32 { return this.reflected.at(at, index as u64 + 1); }
#! The one description the node gives, or `no_reflection`.
#! @param this The checker.
#! @param node The node.
#! @returns The description's index in `descs`, or `no_reflection`.
pub def checker::reflected_one(&this, node: u32): u32 {
val at := this.reflected_at(node);
return checker::no_reflection if at == checker::no_reflection || this.reflected_kind(at) != rk::one as u32;
return this.reflected_item(at, 0);
}
#! Whether the call is of an intrinsic of reflection, by the name it is.
#! @param this The checker.
#! @param decl The function called.
#! @returns Whether it is.
pub def checker::is_reflection(&mut this, decl: u32): bool {
return false if !this.decls.has(decl, df::intrinsic);
val name := this.names.text(this.decls.names.p[decl]);
return name == "declarations" || name == "decl_of" || name == "members_of" || name == "owner_of"
|| name == "attributes_of" || name == "attached" || name == "value_of" || name == "params_of"
|| name == "name_of" || name == "path_of";
}
#! A call of an intrinsic of reflection.
#! @param this The checker.
#! @param node The `call` node.
#! @param callee The node of what is called.
#! @param decl The intrinsic's declaration.
#! @param written The run of the type arguments written, as `T` of
#! `decl_of::<T>()`.
#! @param args The arguments' nodes.
#! @param expected The type expected of what it gives, or the error type where
#! none is.
#! @returns The type it gives.
pub def checker::reflect_call(&mut this, node: u32, callee: u32, decl: u32, written: u32, args: span<u32>, expected: u32): u32 {
val name := this.names.text(this.decls.names.p[decl]);
this.resolve(decl);
val result := this.types.first_of(this.decls.types.p[decl]);
this.set_ref(callee, decl);
if name == "value_of" {
return bt::error if args.length != 1;
this.infer_expr(args[0]);
val one := this.described_arg(args[0], "`value_of`");
if one == checker::no_reflection {
return bt::error;
}
# What reflection was given, it reaches, as an attribute consents to.
val held: desc = this.descs[one];
return bt::error if held.param != 0;
val saved := this.consent;
this.consent = held.decl;
val made := this.decl_value_of(node, held.decl, expected);
this.consent = saved;
return made;
}
if name == "declarations" {
var items := vector::<u32>::new();
for id in 1..this.decls.count {
if this.reflectable(id as u32) {
items.append(this.describe_decl(id as u32, 0));
}
}
this.set_reflected(node, rk::list, items.span);
return result;
}
if name == "decl_of" || name == "name_of" || name == "path_of" {
val kind := this.run_length(written) == 1 ? this.resolve_type(this.run_at(written, 0)) : bt::error;
# The type named is kept as the callee's, which lowering spells.
if name == "name_of" || name == "path_of" {
this.set_type(callee, kind);
}
val plain := this.unalias(kind);
val target := this.type_decl(plain);
# A type no declaration makes, as a reference or a number, has none to give.
if name == "decl_of" && target == 0 && kind != bt::error && !this.types.is_open(kind) {
var text := string::from("`decl_of` takes a type a declaration makes, as a struct, union, enum, or trait, and `", nothing {});
val named := this.describe(kind);
text.append(named.span);
text.append("` is none: `name_of` and `path_of` name any type.");
this.report_text(node, ec::invalid, &text);
return result;
}
if name == "decl_of" && target != 0 && !this.types.is_open(kind) {
# An instance of a generic type gives its members the types it takes.
val owner: u32 = this.decls.params.p[target] != 0 ? plain : 0;
var items := vector::<u32>::new();
items.append(this.describe_owned(target, 0, owner));
this.set_reflected(node, rk::one, items.span);
}
return result;
}
# What the rest take is a description.
for &each in args {
this.infer_expr(each);
}
var what := string::from("`", nothing {});
what.append(name);
what.append("`");
val one := args.length == 0 ? checker::no_reflection : this.described_arg(args[0], what.span);
# What `attached` gives is a view of the attribute's type, which is known even
# where the declaration is not, as in a generic's own body.
if one == checker::no_reflection && name == "attached" {
val wanted := this.run_length(written) == 1 ? this.resolve_type(this.run_at(written, 0)) : bt::error;
return wanted == bt::error ? bt::error : this.span_of(wanted);
}
return result if one == checker::no_reflection;
val held: desc = this.descs[one];
if name == "params_of" {
var items := vector::<u32>::new();
if held.param == 0 && this.decls.kinds.p[held.decl] == dk::function {
this.resolve(held.decl);
for i in 0..this.types.part_count(this.decls.types.p[held.decl]) {
items.append(this.describe_owned(held.decl, i as u32 + 1, held.owner));
}
}
this.set_reflected(node, rk::list, items.span);
return result;
}
if name == "members_of" {
var items := vector::<u32>::new();
if held.param == 0 {
val owner := this.member_owner(held);
val run: u32 = this.decls.children.p[held.decl];
for &member in this.decls.list_view(run) {
items.append(this.describe_owned(member, 0, owner));
}
for &member in this.decls.by_owner.of(held.decl) {
if this.decls.owners.p[member] == held.decl && this.receiver_declared(member) && this.visible(member) {
items.append(this.describe_owned(member, 0, owner));
}
}
}
this.set_reflected(node, rk::list, items.span);
return result;
}
if name == "owner_of" {
val owner: u32 = held.param != 0 ? held.decl : this.decls.owners.p[held.decl];
if owner != 0 {
val through: u32 = held.owner != 0 && this.type_decl(held.owner) == owner ? held.owner : 0;
var items := vector::<u32>::new();
items.append(this.describe_owned(owner, 0, through));
this.set_reflected(node, rk::one, items.span);
}
return result;
}
if name == "attached" {
# A span of the values of the attributes of the type given, of which how
# many there are is known.
val wanted := this.run_length(written) == 1 ? this.resolve_type(this.run_at(written, 0)) : bt::error;
var count := 0u;
val kinds := this.attribute_types(held.decl, held.param);
for &each in kinds {
if each == wanted { count++; }
}
this.set_known_length(node, count);
return wanted == bt::error ? bt::error : this.span_of(wanted);
}
if name == "attributes_of" {
var items := vector::<u32>::new();
val made := this.attribute_decls(held.decl, held.param);
for &each in made {
items.append(this.describe_decl(each, 0));
}
this.set_reflected(node, rk::list, items.span);
return result;
}
return result;
}
#! The attributes written on a declaration, or on one of a function's
#! parameters, in the order they are written, each a declaration of its own, as
#! `attributes_of` gives them: a constant of the attribute's type, named as it
#! is written, as `@tag`, whose value is the attribute's. Each is made the
#! first time it is asked for.
#! @param this The checker.
#! @param id The declaration.
#! @param param One more than the index of the parameter, or 0 for the
#! declaration itself.
#! @returns The attributes' declarations.
def checker::attribute_decls(&mut this, id: u32, param: u32 = 0): vector<u32> {
var made := vector::<u32>::new();
val kinds := this.attribute_types(id, param);
val file: u32 = this.decls.files.p[id];
val saved := this.at;
this.use_table(this.units[file].table);
val attrs := this.attributes_at(id, param);
var at: u64 = 0;
for &attr in this.run_view(attrs) {
continue if this.kind_of(attr) != nk::attribute;
val kind := at < kinds.length ? kinds[at] : bt::error;
at++;
continue if kind == bt::error;
val key := file_node { file = file, node = attr };
if this.attr_decls.get(&key) is some(got) {
made.append(got);
continue;
}
var name := string::from("@", nothing {});
val named := this.describe(kind);
name.append(named.span);
val decl := this.decls.add(dk::attribute, this.names.intern(&name.span), file, attr, 0);
this.decls.types.p[decl] = kind;
this.decls.states.p[decl] = ds::resolved;
this.decls.mark(decl, df::constant);
this.decls.mark(decl, df::checked);
this.attr_decls.set(key, decl);
made.append(decl);
}
this.at = saved;
return made;
}
#! The description an argument of reflection gives, reporting one giving none
#! where checking is not within a generic's own body, which each instance
#! checks.
#! @param this The checker.
#! @param arg The argument's node.
#! @param what The intrinsic, as a note names it.
#! @returns The description's index in `descs`, or `no_reflection`.
def checker::described_arg(&mut this, arg: u32, what: strview): u32 {
val one := this.reflected_one(arg);
if one == checker::no_reflection && !this.waits_for_instance() && this.type_at(arg) != bt::error {
var text := string::from(what, nothing {});
text.append(" takes a declaration known while compiling, as the variable of `for const d in declarations()` is.");
this.report_text(arg, ec::constant, &text);
}
return one;
}
#! Records how many values a view an expression of reflection gives holds,
#! which its `length` is known as.
#! @param this The checker.
#! @param node The expression.
#! @param count How many values the view holds.
def checker::set_known_length(&mut this, node: u32, count: u64): void {
this.lengths.set(this.table_node_of(node), count as u32);
}
#! Whether reflection lists the declaration among `declarations()`: it is
#! declared outside any function, and the module checking can name it, or it
#! carries an attribute whose type that module declares.
#! @param this The checker.
#! @param id The declaration.
#! @returns Whether it does.
def checker::reflectable(&mut this, id: u32): bool {
val kind: dk = this.decls.kinds.p[id];
match kind {
dk::function, dk::variable, dk::struct_type, dk::union_type, dk::enum_type,
dk::trait_type, dk::alias_type, dk::property => {}
else => return false;
}
return false if this.decls.has(id, df::accessor) || this.decls.nodes.p[id] == 0;
val owner: u32 = this.decls.owners.p[id];
return false if owner != 0 && !this.receiver_declared(id);
return true if this.visible(id);
val kinds := this.attribute_types(id);
for &each in kinds {
val decl := this.type_decl(each);
return true if decl != 0 && this.decls.modules.p[decl] == this.at.home;
}
return false;
}
#! The types of the attributes written before the declaration, or before one of
#! a function's parameters, resolved where the declaration is.
#! @param this The checker.
#! @param id The declaration.
#! @param param One more than the index of the parameter, or 0 for the
#! declaration itself.
#! @returns The attributes' types, in the order they are written.
pub def checker::attribute_types(&mut this, id: u32, param: u32 = 0): vector<u32> {
var made := vector::<u32>::new();
val saved := this.at;
this.enter_decl(id, 0);
val attrs := this.attributes_at(id, param);
for &attr in this.run_view(attrs) {
continue if this.kind_of(attr) != nk::attribute;
made.append(this.quiet_type(this.left_of(attr)));
}
this.leave(saved);
return made;
}
#! The type a type name names, without reporting what is wrong with it, which
#! checking the declaration it is written on reports.
#! @param this The checker.
#! @param node The `type_name` node.
#! @returns The type, or the error type.
def checker::quiet_type(&mut this, node: u32): u32 {
val name := this.segment_name(node, this.segment_count(node) - 1);
val decl := this.segment_count(node) == 1 ? this.lookup_type(name, 0) : 0;
return this.decls.types.p[decl] if decl != 0 && this.is_type_decl(decl) && this.decls.params.p[decl] == 0;
# What it names wrongly is reported where its declaration's attributes are
# checked, once.
this.quiet++;
val kind := this.resolve_type(node);
this.quiet--;
return kind;
}
#! What a description says of itself, read as a field of it: its `kind`, as
#! the bit of `core::target`, whether it is `exported` or `generic`, which are
#! known, and its `name` and `path`.
#! @param this The checker.
#! @param node The `member` node, which takes what the field is known to be.
#! @param one The description's index in `descs`.
#! @param name The ID of the field's name.
pub def checker::reflected_field(&mut this, node: u32, one: u32, name: u32): void {
val held: desc = this.descs[one];
val text := this.names.text(name);
if text == "kind" {
val bit := held.param != 0 ? 0x2000u : this.target_bit(held.decl);
this.set_value(node, constant::integer(bit, false));
} else if text == "exported" {
this.set_value(node, constant::truth(held.param == 0 && this.decls.has(held.decl, df::exported)));
} else if text == "generic" {
val own := held.param == 0 && this.own_param_count(held.decl) != 0;
this.set_value(node, constant::truth(own));
}
}
#! The type of what a description describes: a function's own type, or what a
#! variable, field, parameter, or property holds.
#! @param this The checker.
#! @param one The description's index in `descs`.
#! @returns The type.
pub def checker::reflected_type(&mut this, one: u32): u32 {
val held: desc = this.descs[one];
this.resolve(held.decl);
var kind: u32 = this.decls.types.p[held.decl];
if held.owner != 0 {
kind = this.instantiated(kind, this.type_decl(held.owner), held.owner);
}
if held.param != 0 {
return held.param <= this.types.part_count(kind) ? this.types.part_at(kind, held.param - 1) : bt::error;
}
return kind;
}
#! `for const d in declarations()`, or over any list of descriptions: its body
#! is checked once for each, with `d` describing it, each with a result table
#! of its own.
#! @param this The checker.
#! @param node The `for_stmt` node.
#! @param variable The node of the loop's variable.
#! @param list The list's run in `reflected`.
#! @param kind The type of the descriptions, `core::decl`.
pub def checker::unroll_reflection(&mut this, node: u32, variable: u32, list: u32, kind: u32): void {
val body := this.right_of(node);
val outer := this.at.table;
val count := this.reflected_count(list);
var element: u32 = bt::error;
if this.is_std(this.unalias(kind), word::span) {
element = this.types.part_at(this.unalias(kind), 0);
}
for i in 0..count {
val item := this.reflected_item(list, i as u32);
val table := this.add_child_table(variable, body - variable + 1);
this.use_table(table);
val mark := this.scope_mark();
this.declare_local(variable, this.name_of(variable), element);
var items := vector::<u32>::new();
items.append(item);
this.set_reflected(variable, rk::one, items.span);
this.at.loops++;
this.check_block(body);
this.at.loops--;
this.unbind_to(mark);
this.use_table(outer);
}
}
#! Whether checking is within a generic's own body, where what reflection says
#! of its type parameters waits for each instance.
#! @param this The checker.
#! @returns Whether it is.
pub def checker::waits_for_instance(&mut this): bool {
return false if this.at.instance != 0 || this.at.decl == 0 || this.at.given;
return true if this.decls.has(this.at.decl, df::generic);
# A default compiled for each type taking it is checked as a template too, as
# is a constant of a trait, worked out for each type or list of arguments,
# but of a sealed trait without parameters, which is one constant.
val decl := this.at.decl;
return !this.sealed_alone(decl) if this.decls.kinds.p[decl] == dk::variable && this.trait_asks(decl);
return this.decls.kinds.p[decl] == dk::function && this.typed_default(decl);
}