Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,319 @@
|
||||
#! @file attributes.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the checking of attributes. An attribute is a struct literal of a
|
||||
#! struct declaring `attribute`, written before a declaration with `@`, whose
|
||||
#! fields are checked as any literal's are. A struct's `@usage` says what kinds
|
||||
#! of declaration its attributes may describe, and whether one may have more
|
||||
#! than one of them.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! What a struct's `@usage` allows of its attributes.
|
||||
pub struct checker::usage {
|
||||
var on: u64; #! The bits of `core::target` it may be written on.
|
||||
var many: bool; #! Whether a declaration may have more than one.
|
||||
}
|
||||
|
||||
#! Checks the attributes written before the declaration, and before each of a
|
||||
#! function's parameters.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
pub def checker::check_attributes(&mut this, id: u32): void {
|
||||
val node: u32 = this.decls.nodes.p[id];
|
||||
val attrs := this.attributes_of(node);
|
||||
if this.run_length(attrs) != 0 {
|
||||
this.check_attribute_run(id, attrs, this.target_bit(id), this.target_word(id));
|
||||
}
|
||||
return if this.kind_of(node) != nk::function;
|
||||
for ¶m in this.run_view(this.record(node, function_slot::params)) {
|
||||
val written := this.record(param, parameter_slot::attributes);
|
||||
if this.run_length(written) != 0 {
|
||||
this.check_attribute_run(id, written, this.target_named("parameters"), "parameters");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Checks a run of attributes, written on a declaration or one of its
|
||||
#! parameters: each names a struct declaring `attribute`, is a literal of it,
|
||||
#! is written on what its `@usage` allows, and once, unless it allows more.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration, or the function whose parameter they are on.
|
||||
#! @param attrs The run.
|
||||
#! @param bit The bit of `core::target` they are written on, or 0 for any.
|
||||
#! @param words What they are written on, as a note says it.
|
||||
def checker::check_attribute_run(&mut this, id: u32, attrs: u32, bit: u64, words: strview): void {
|
||||
val attribute := this.std_decl(word::attribute);
|
||||
var seen := vector::<u32>::new();
|
||||
for &attr in this.run_view(attrs) {
|
||||
continue if this.kind_of(attr) != nk::attribute;
|
||||
|
||||
# What C gives was written with attributes before `extern` sections were.
|
||||
val named := this.left_of(attr);
|
||||
val name := this.segment_name(named, this.segment_count(named) - 1);
|
||||
if (name == word::foreign || name == word::link) && this.segment_count(named) == 1
|
||||
&& this.lookup_named(name, attr, true).decl == 0 {
|
||||
this.report(attr, ec::invalid,
|
||||
"`@foreign` and `@link` are gone: C's functions, structs, and unions are declared in an `extern module`, and `extern import` names their library.");
|
||||
continue;
|
||||
}
|
||||
|
||||
val kind := this.resolve_type(this.left_of(attr));
|
||||
continue if kind == bt::error;
|
||||
if this.named_kind(kind) != dk::struct_type || (attribute != 0 && !this.names_trait(kind, attribute)) {
|
||||
var text := string::from("`", nothing {});
|
||||
val named := this.describe(kind);
|
||||
text.append(named.span);
|
||||
text.append("` is not an attribute: an attribute's type is a struct declaring `attribute`.");
|
||||
this.report_text(attr, ec::invalid, &text);
|
||||
continue;
|
||||
}
|
||||
|
||||
this.construct_struct(attr, kind, this.right_of(attr));
|
||||
val decl := this.types.first_of(kind);
|
||||
val usage := this.usage_of(decl);
|
||||
if bit != 0 && (usage.on & bit) == 0 {
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.decl_text(decl));
|
||||
text.append("` is not written on ");
|
||||
text.append(words);
|
||||
text.append(", as its `@usage` says.");
|
||||
this.report_text(attr, ec::invalid, &text);
|
||||
}
|
||||
|
||||
# What is copied where it is called has a body to copy.
|
||||
val written := this.decls.kinds.p[id];
|
||||
if name == word::inline && (written == dk::function || written == dk::property)
|
||||
&& (this.decls.has(id, df::intrinsic) || this.decls.has(id, df::foreign)) {
|
||||
this.report(attr, ec::invalid,
|
||||
"`@inline` copies a function's body where it is called, and a function C or the compiler gives has none.");
|
||||
}
|
||||
|
||||
if seen.span.contains(&decl) && !usage.many {
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.decl_text(decl));
|
||||
text.append("` is written once on a declaration, unless its `@usage` says `many = true`.");
|
||||
this.report_text(attr, ec::duplicate, &text);
|
||||
}
|
||||
seen.append(decl);
|
||||
}
|
||||
}
|
||||
|
||||
#! What a struct's `@usage` allows of its attributes, or anything, once each,
|
||||
#! where it has none.
|
||||
#! @param this The checker.
|
||||
#! @param decl The attribute struct's declaration.
|
||||
#! @returns What it may be written on, and whether more than once.
|
||||
pub def checker::usage_of(&mut this, decl: u32): checker::usage {
|
||||
var made := checker::usage { on = 0xFFFF, many = false };
|
||||
val usage := this.std_decl(word::usage);
|
||||
return made if usage == 0;
|
||||
|
||||
val saved := this.at;
|
||||
this.enter_decl(decl, 0);
|
||||
val attrs := this.attributes_of(this.decls.nodes.p[decl]);
|
||||
for &attr in this.run_view(attrs) {
|
||||
continue if this.kind_of(attr) != nk::attribute;
|
||||
val named := this.left_of(attr);
|
||||
continue if this.segment_name(named, this.segment_count(named) - 1) != word::usage;
|
||||
|
||||
val inits := this.right_of(attr);
|
||||
for &init in this.run_view(inits) {
|
||||
val value := this.left_of(init);
|
||||
this.infer_expr(value);
|
||||
continue if !this.is_known(value);
|
||||
val name := this.name_of(init);
|
||||
if name == this.names.intern(&"on") {
|
||||
made.on = this.value_at(value).magnitude;
|
||||
} else if name == this.names.intern(&"many") {
|
||||
made.many = this.value_at(value).magnitude != 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.leave(saved);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The bit of `core::target` the kind of declaration is, or 0 where std has none.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns The bit, or 0.
|
||||
pub def checker::target_bit(&mut this, id: u32): u64 {
|
||||
return this.target_named(this.target_name(id));
|
||||
}
|
||||
|
||||
#! The bit of a member of `core::target`, by its name.
|
||||
#! @param this The checker.
|
||||
#! @param name The member's name, as `parameters`.
|
||||
#! @returns The bit, or 0 where `core::target` has no such member.
|
||||
def checker::target_named(&mut this, name: strview): u64 {
|
||||
val holder := this.std_decl(this.names.intern(&"target"));
|
||||
return 0 if holder == 0;
|
||||
|
||||
val member := this.decls.member(holder, this.names.intern(&name));
|
||||
var value := constant {};
|
||||
this.resolve(holder);
|
||||
return 0 if member == 0 || !this.decl_value(member, &mut value);
|
||||
return value.magnitude;
|
||||
}
|
||||
|
||||
#! The name of the member of `core::target` the kind of declaration is.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns The name, as `functions`.
|
||||
def checker::target_name(&mut this, id: u32): strview {
|
||||
val owner: u32 = this.decls.owners.p[id];
|
||||
val in_trait := owner != 0 && this.decls.kinds.p[owner] == dk::trait_type && !this.receiver_declared(id);
|
||||
return "requirements" if in_trait;
|
||||
return match this.decls.kinds.p[id] {
|
||||
dk::function => owner != 0 ? "methods" : "functions",
|
||||
dk::variable => this.decls.has(id, df::constant) ? "constants" : "variables",
|
||||
dk::field => "fields",
|
||||
dk::struct_type => "structs",
|
||||
dk::union_type => "unions",
|
||||
dk::enum_type => "enums",
|
||||
dk::enum_member => "members",
|
||||
dk::trait_type => "traits",
|
||||
dk::alias_type => "aliases",
|
||||
dk::property => "properties",
|
||||
dk::attribute => "constants",
|
||||
else => "all"
|
||||
};
|
||||
}
|
||||
|
||||
#! The kind of declaration, as a note says it.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns The words, as `a function`.
|
||||
def checker::target_word(&mut this, id: u32): strview {
|
||||
val owner: u32 = this.decls.owners.p[id];
|
||||
val in_trait := owner != 0 && this.decls.kinds.p[owner] == dk::trait_type && !this.receiver_declared(id);
|
||||
return "what a trait declares" if in_trait;
|
||||
return match this.decls.kinds.p[id] {
|
||||
dk::function => owner != 0 ? "methods" : "functions",
|
||||
dk::variable => this.decls.has(id, df::constant) ? "constants" : "variables",
|
||||
dk::field => "fields",
|
||||
dk::struct_type => "structs",
|
||||
dk::union_type => "unions",
|
||||
dk::enum_type => "enums",
|
||||
dk::enum_member => "enum members",
|
||||
dk::trait_type => "traits",
|
||||
dk::alias_type => "aliases",
|
||||
dk::property => "properties",
|
||||
else => "this"
|
||||
};
|
||||
}
|
||||
|
||||
#! Checks a function given by C: never generic, overloaded, or a method, and of
|
||||
#! parameters and a result whose types C lays out the same way.
|
||||
#! @param this The checker.
|
||||
#! @param id The function's declaration.
|
||||
pub def checker::check_foreign(&mut this, id: u32): void {
|
||||
val node: u32 = this.decls.nodes.p[id];
|
||||
if this.decls.has(id, df::generic) || this.decls.has(id, df::method) || this.decls.overloads.p[id] != 0 {
|
||||
this.report(node, ec::invalid, "A C function is never generic, overloaded, or a method.");
|
||||
return;
|
||||
}
|
||||
|
||||
val kind: u32 = this.decls.types.p[id];
|
||||
for i in 0..this.types.part_count(kind) {
|
||||
val param := this.types.part_at(kind, i as u32);
|
||||
if !this.crosses(param, true, 0) {
|
||||
this.report_crossing(node, param);
|
||||
}
|
||||
}
|
||||
val result := this.types.first_of(kind);
|
||||
if result != bt::void_type && !this.crosses(result, false, 0) {
|
||||
this.report_crossing(node, result);
|
||||
}
|
||||
}
|
||||
|
||||
def checker::report_crossing(&mut this, node: u32, kind: u32): void {
|
||||
var text := string::from("`", nothing {});
|
||||
val named := this.describe(kind);
|
||||
text.append(named.span);
|
||||
text.append("` does not cross to C, which lays out only numbers, `bool`, enums, pointers, function pointers, and structs and C's unions of them.");
|
||||
this.report_text(node, ec::invalid, &text);
|
||||
}
|
||||
|
||||
#! Checks a function given where C's address of one is expected, which C calls
|
||||
#! back: never generic or a method, and of parameters and a result that cross.
|
||||
#! @param this The checker.
|
||||
#! @param node The node naming it.
|
||||
#! @param id The function's declaration.
|
||||
pub def checker::check_callback(&mut this, node: u32, id: u32): void {
|
||||
if this.decls.has(id, df::generic) || this.decls.has(id, df::method) || this.decls.has(id, df::intrinsic) {
|
||||
this.report(node, ec::invalid, "C calls back a function of the program that is neither generic, a method, nor `@intrinsic`.");
|
||||
return;
|
||||
}
|
||||
|
||||
val kind: u32 = this.decls.types.p[id];
|
||||
for i in 0..this.types.part_count(kind) {
|
||||
val param := this.types.part_at(kind, i as u32);
|
||||
if !this.crosses(param, true, 0) {
|
||||
this.report_crossing(node, param);
|
||||
}
|
||||
}
|
||||
val result := this.types.first_of(kind);
|
||||
if result != bt::void_type && !this.crosses(result, false, 0) {
|
||||
this.report_crossing(node, result);
|
||||
}
|
||||
}
|
||||
|
||||
#! Checks a struct or union of an `extern module`, which C lays out: never
|
||||
#! generic, and of fields whose types cross.
|
||||
#! @param this The checker.
|
||||
#! @param id The type's declaration.
|
||||
def checker::check_c_type(&mut this, id: u32): void {
|
||||
val node: u32 = this.decls.nodes.p[id];
|
||||
if this.decls.has(id, df::generic) {
|
||||
this.report(node, ec::invalid, "A struct or union of an `extern module` is laid out as C's, so it is never generic.");
|
||||
return;
|
||||
}
|
||||
|
||||
val run: u32 = this.decls.children.p[id];
|
||||
for i in 0..this.decls.list_length(run) {
|
||||
val field := this.decls.list_at(run, i as u32);
|
||||
continue if this.decls.kinds.p[field] != dk::field;
|
||||
this.resolve(field);
|
||||
val kind: u32 = this.decls.types.p[field];
|
||||
if !this.crosses(kind, false, 1) {
|
||||
this.report_crossing(this.decls.nodes.p[field], kind);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether C lays out a value of the type as Sakura does: a number, `bool`, an
|
||||
#! enum, a pointer, a function pointer, a reference where it is a parameter,
|
||||
#! and a struct, or a C union, of what crosses, with arrays within.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param parameter Whether it is a parameter's, where a reference crosses as
|
||||
#! a pointer.
|
||||
#! @param depth How deeply the search has gone, which stops a type nesting
|
||||
#! without end.
|
||||
#! @returns Whether it does.
|
||||
def checker::crosses(&mut this, kind: u32, parameter: bool, depth: u32): bool {
|
||||
return true if depth > 8 || kind == bt::error;
|
||||
val plain := this.unalias(kind);
|
||||
match this.types.kind_of(plain) {
|
||||
ty::boolean, ty::signed, ty::unsigned, ty::floating, ty::pointer => return true;
|
||||
ty::reference => return parameter;
|
||||
ty::fixed => return depth > 0 && this.crosses(this.types.first_of(plain), false, depth + 1);
|
||||
ty::named => {
|
||||
val named := this.named_kind(plain);
|
||||
return true if named == dk::enum_type || this.is_std(plain, word::cfn);
|
||||
return false if named != dk::struct_type && !(named == dk::union_type && this.decls.has(this.types.first_of(plain), df::foreign));
|
||||
val fields := this.field_types(plain);
|
||||
for &each in fields {
|
||||
return false if !this.crosses(each, false, depth + 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else => return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,401 @@
|
||||
#! @file bodies.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the checking of what each declaration holds beyond its signature:
|
||||
#! the body of a function, checked once against the bounds of its type
|
||||
#! parameters where it is generic, the values of variables and fields, and
|
||||
#! that a struct or union implements the traits it names.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! Checks what the declaration holds.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
pub def checker::check_decl(&mut this, id: u32): void {
|
||||
return if this.decls.has(id, df::checked) || this.decls.has(id, df::failed);
|
||||
this.decls.mark(id, df::checked);
|
||||
|
||||
# The attributes written before it, in its own file and scope.
|
||||
if this.decls.kinds.p[id] != dk::type_param && this.decls.kinds.p[id] != dk::builtin
|
||||
&& !this.decls.has(id, df::accessor) && this.decls.nodes.p[id] != 0 {
|
||||
val saved := this.at;
|
||||
this.enter_decl(id, 0);
|
||||
this.check_attributes(id);
|
||||
this.leave(saved);
|
||||
}
|
||||
|
||||
match this.decls.kinds.p[id] {
|
||||
dk::function => {
|
||||
this.resolve(id);
|
||||
this.check_overloads(id);
|
||||
this.check_main(id);
|
||||
if this.decls.has(id, df::foreign) {
|
||||
val saved := this.at;
|
||||
this.use_table(this.units[this.decls.files.p[id]].table);
|
||||
this.check_foreign(id);
|
||||
this.at = saved;
|
||||
}
|
||||
return if this.decls.has(id, df::intrinsic) || this.decls.has(id, df::foreign) && !this.has_body(id);
|
||||
val saved := this.at;
|
||||
this.enter_decl(id, 0);
|
||||
this.check_asked(id);
|
||||
# Its lifetimes are warned of only where its body checks, and one that
|
||||
# does not is never run while compiling.
|
||||
val errors := this.errors;
|
||||
val recorded := this.begin_lifetimes(id);
|
||||
this.check_function_body(id, this.decls.types.p[id]);
|
||||
if recorded {
|
||||
this.end_lifetimes(this.errors == errors);
|
||||
}
|
||||
if this.errors != errors {
|
||||
this.decls.mark(id, df::flawed);
|
||||
}
|
||||
this.leave(saved);
|
||||
}
|
||||
dk::variable, dk::field => {
|
||||
if this.decls.kinds.p[id] == dk::variable {
|
||||
this.checked_globals++;
|
||||
}
|
||||
val errors := this.errors;
|
||||
this.check_value(id);
|
||||
if this.errors != errors {
|
||||
this.decls.mark(id, df::flawed);
|
||||
}
|
||||
}
|
||||
dk::struct_type, dk::union_type => {
|
||||
# Its fields are part of it, and a value made of it leaving one out takes
|
||||
# the field's default, so they are checked with it.
|
||||
for &member in this.decls.list_view(this.decls.children.p[id]) {
|
||||
if this.decls.kinds.p[member] == dk::field {
|
||||
this.reach(member);
|
||||
}
|
||||
}
|
||||
this.check_implements(id);
|
||||
this.check_contains(id);
|
||||
if this.decls.has(id, df::foreign) {
|
||||
val saved := this.at;
|
||||
this.use_table(this.units[this.decls.files.p[id]].table);
|
||||
this.check_c_type(id);
|
||||
this.at = saved;
|
||||
}
|
||||
}
|
||||
dk::trait_type => {
|
||||
this.resolve_composed(id);
|
||||
this.check_sealed(id);
|
||||
}
|
||||
else => this.resolve(id);
|
||||
}
|
||||
}
|
||||
|
||||
#! Checks a function a trait asks for, which each type implementing the trait
|
||||
#! gives: one with type parameters of its own gives a default, which each type
|
||||
#! takes as its own, since no table holds a generic, and one taking no `this`
|
||||
#! has no default, since nothing within the trait names the type implementing
|
||||
#! it.
|
||||
#! @param this The checker.
|
||||
#! @param id The function's declaration, whose file is the one checked.
|
||||
def checker::check_asked(&mut this, id: u32): void {
|
||||
return if !this.trait_asks(id) || this.decls.has(id, df::accessor);
|
||||
|
||||
val node: u32 = this.decls.nodes.p[id];
|
||||
val params := this.record(node, function_slot::type_params);
|
||||
if this.run_length(params) != 0 && !this.has_body(id) {
|
||||
this.report(this.run_at(params, 0), ec::generic,
|
||||
"A trait's generic method gives a default, which each type implementing the trait takes as its own, since no table holds a generic.");
|
||||
}
|
||||
if !this.decls.has(id, df::method) && this.has_body(id) {
|
||||
this.report(node, ec::invalid,
|
||||
"Static methods of a trait have no default, since nothing within the trait names the type implementing them.");
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether a function has a body, rather than ending at its signature.
|
||||
#! @param this The checker.
|
||||
#! @param id The function's declaration.
|
||||
#! @returns Whether it has.
|
||||
pub def checker::has_body(&this, id: u32): bool {
|
||||
val syntax := &this.units[this.decls.files.p[id]].syntax;
|
||||
val node := this.decls.nodes.p[id];
|
||||
if this.decls.has(id, df::accessor) {
|
||||
return syntax.record(node, accessor_slot::body) != 0;
|
||||
}
|
||||
return syntax.record(node, function_slot::body) != 0;
|
||||
}
|
||||
|
||||
#! Checks a function's body against its type: its parameters are locals of
|
||||
#! their types, its default values are checked where its parameters are, and
|
||||
#! it returns a value on every path where its result is not `void`.
|
||||
#! @param this The checker.
|
||||
#! @param id The function's declaration.
|
||||
#! @param kind Its function type, given the type arguments it is checked with.
|
||||
pub def checker::check_function_body(&mut this, id: u32, kind: u32): void {
|
||||
val node: u32 = this.decls.nodes.p[id];
|
||||
val accessor := this.decls.has(id, df::accessor);
|
||||
val body := accessor ? this.record(node, accessor_slot::body) : this.record(node, function_slot::body);
|
||||
val params := accessor ? this.left_of(node) : this.record(node, function_slot::params);
|
||||
if body == 0 {
|
||||
if !this.decls.has(id, df::intrinsic) && !this.decls.has(id, df::foreign)
|
||||
&& this.enclosing_trait(id) == 0 {
|
||||
this.report(node, ec::invalid, "A function has a body, unless it is `@intrinsic`, given by the compiler, or C's: a function with no body is C's only in an `extern module`.");
|
||||
}
|
||||
# A trait's defaults are made where a call through it leaves them out, as
|
||||
# `C::span_of(&c)` of a `C: spannable<T>` does, so they are checked here.
|
||||
if !accessor && this.enclosing_trait(id) != 0 {
|
||||
for i in 0..this.run_length(params) {
|
||||
val param := this.run_at(params, i as u32);
|
||||
val held: u32 = (i as u32) < this.types.part_count(kind) ? this.types.part_at(kind, i as u32) : bt::error;
|
||||
val value := this.record(param, parameter_slot::value);
|
||||
if value != 0 {
|
||||
this.check_expr(value, held);
|
||||
}
|
||||
this.declare_local(param, this.param_name(param), held);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this.at.result = this.types.first_of(kind);
|
||||
val count := this.types.part_count(kind);
|
||||
# A trait's method's own body has the bounds it puts on the trait's parameters
|
||||
# and `self`, which an instance's arguments meet.
|
||||
val proved := this.proved_params.length;
|
||||
if this.at.instance == 0 {
|
||||
this.prove_outer(id);
|
||||
}
|
||||
|
||||
# A pack parameter is a local holding the pack: in the generic's own body,
|
||||
# a pack of types not known, and in an instance, the types the parameters
|
||||
# from it on were given.
|
||||
val written: u32 = this.decls.types.p[id];
|
||||
val wcount := this.types.part_count(written);
|
||||
val packed := this.decls.has(id, df::variadic) && wcount != 0
|
||||
&& this.types.is_open_pack(this.types.part_at(written, wcount - 1));
|
||||
for i in 0..this.run_length(params) {
|
||||
val param := this.run_at(params, i as u32);
|
||||
var held: u32 = i < count ? this.types.part_at(kind, i as u32) : bt::error;
|
||||
if packed && i as u32 + 1 == wcount {
|
||||
var rest := vector::<u32>::new();
|
||||
for j in i..count {
|
||||
rest.append(this.types.part_at(kind, j as u32));
|
||||
}
|
||||
# Given a variadic function's parameters, as a pattern's pack may be, the
|
||||
# pack ends in a view of the rest, which its type says as a variadic pack.
|
||||
val open := i < count && this.types.is_open_pack(this.types.part_at(kind, i as u32));
|
||||
held = open ? this.types.part_at(kind, i as u32)
|
||||
: this.types.second_of(kind) != 0 && rest.length != 0 ? this.types.variadic_pack(rest.span)
|
||||
: this.types.pack(rest.span, 0);
|
||||
} else if this.types.second_of(kind) != 0 && i as u32 + 1 == count {
|
||||
held = this.variadic_type(held);
|
||||
}
|
||||
|
||||
# A default value is checked where its parameter is, with the parameters
|
||||
# before it named, and runs at each call that leaves it out.
|
||||
val value := accessor ? 0 : this.record(param, parameter_slot::value);
|
||||
if value != 0 {
|
||||
this.check_expr(value, held);
|
||||
}
|
||||
this.declare_local(param, this.param_name(param), held);
|
||||
this.set_flag(param, rf::mutable);
|
||||
}
|
||||
|
||||
this.at.body = body;
|
||||
val how := this.check_block(body);
|
||||
this.unprove_to(proved);
|
||||
val result := this.at.result;
|
||||
if how == flow::goes_on && result != bt::void_type && result != bt::error {
|
||||
this.report(node, ec::control, "The function can end without returning a value.");
|
||||
}
|
||||
}
|
||||
|
||||
#! The type a variadic parameter is within its function: a view of its
|
||||
#! arguments, or with a pack, the pack itself.
|
||||
#! @param this The checker.
|
||||
#! @param element The type of the parameter as written: of each argument.
|
||||
#! @returns The type.
|
||||
def checker::variadic_type(&mut this, element: u32) : u32 {
|
||||
return element if this.types.kind_of(element) == ty::pack;
|
||||
return this.span_of(element);
|
||||
}
|
||||
|
||||
#! Checks the value a variable or field is declared with, against the type
|
||||
#! written for it.
|
||||
#! @param this The checker.
|
||||
#! @param id The variable's or field's declaration.
|
||||
def checker::check_value(&mut this, id: u32): void {
|
||||
this.resolve(id);
|
||||
val node: u32 = this.decls.nodes.p[id];
|
||||
val saved := this.at;
|
||||
this.enter_decl(id, 0);
|
||||
val value := this.record(node, variable_slot::value);
|
||||
if value != 0 && !this.has_flag(value, rf::checked) {
|
||||
this.check_expr(value, this.decls.types.p[id]);
|
||||
} else if value == 0 && this.decls.kinds.p[id] == dk::variable && !this.has_zero(this.decls.types.p[id], 0) {
|
||||
var text := string::from("`", nothing {});
|
||||
val named := this.describe(this.decls.types.p[id]);
|
||||
text.append(named.span);
|
||||
text.append("` has no zero value, so the variable is given a value where it is declared.");
|
||||
this.report_text(node, ec::zero_value, &text);
|
||||
}
|
||||
this.leave(saved);
|
||||
}
|
||||
|
||||
#! Checks that a struct or union implements each trait it names.
|
||||
#! @param this The checker.
|
||||
#! @param id The struct's or union's declaration.
|
||||
def checker::check_implements(&mut this, id: u32): void {
|
||||
val run := this.composed_of(id);
|
||||
val kind: u32 = this.decls.types.p[id];
|
||||
for i in 0..this.decls.list_length(run) {
|
||||
val wanted := this.decls.list_at(run, i as u32);
|
||||
continue if this.satisfies(kind, wanted);
|
||||
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.decl_text(id));
|
||||
text.append("` names `");
|
||||
val named := this.describe(wanted);
|
||||
text.append(named.span);
|
||||
text.append("` but does not implement all of it");
|
||||
val missing := this.missing_requirement(kind, wanted);
|
||||
if missing != 0 {
|
||||
text.append(": it has no `");
|
||||
text.append(this.decl_text(missing));
|
||||
text.append("` of the signature the trait asks for");
|
||||
if this.decls.kinds.p[missing] == dk::function && this.gives_defaults(missing) {
|
||||
text.append(", with a default for each parameter the trait gives one");
|
||||
}
|
||||
}
|
||||
text.append(".");
|
||||
this.report_decl(id, ec::bound, text.span);
|
||||
}
|
||||
}
|
||||
|
||||
#! The first thing a trait asks for that the type does not have, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param trait_kind The trait's type, given its arguments.
|
||||
#! @returns The declaration of what it asks for, or 0.
|
||||
def checker::missing_requirement(&mut this, kind: u32, trait_kind: u32): u32 {
|
||||
val holder := this.types.first_of(trait_kind);
|
||||
val run := this.decls.children.p[holder];
|
||||
for i in 0..this.decls.list_length(run) {
|
||||
val wanted := this.decls.list_at(run, i as u32);
|
||||
continue if this.decls.has(wanted, df::optional);
|
||||
return wanted if !this.meets(kind, trait_kind, wanted);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Reports an overload taking the same parameters as one declared before it,
|
||||
#! which no call could choose between.
|
||||
#! @param this The checker.
|
||||
#! @param id The function's declaration.
|
||||
def checker::check_overloads(&mut this, id: u32): void {
|
||||
val owner := this.decls.owners.p[id];
|
||||
val first: u32 = owner != 0 && this.decls.kinds.p[owner] != dk::property
|
||||
? this.first_member(id)
|
||||
: this.modules.decl(this.decls.modules.p[id], this.decls.names.p[id]);
|
||||
return if first == 0 || first == id || this.decls.has(id, df::accessor);
|
||||
|
||||
var walk := this.decls.walk_overloads(first);
|
||||
while true {
|
||||
val at := walk.next();
|
||||
break if at == 0 || at == id;
|
||||
this.resolve(at);
|
||||
if this.decls.kinds.p[at] == dk::function && this.same_params(at, id) {
|
||||
this.report_decl_related(id, ec::duplicate,
|
||||
"Another overload of this name takes the same parameters, so no call could choose between them.",
|
||||
at, "The other overload.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether two functions take the same parameters: of the same types, an alias
|
||||
#! being a type of its own, both static or both taking `this` alike.
|
||||
#! @param this The checker.
|
||||
#! @param a One function's declaration.
|
||||
#! @param b The other's.
|
||||
#! @returns Whether they do.
|
||||
def checker::same_params(&mut this, a: u32, b: u32): bool {
|
||||
return false if this.decls.has(a, df::method) != this.decls.has(b, df::method);
|
||||
return false if this.decls.has(a, df::mut_this) != this.decls.has(b, df::mut_this);
|
||||
return false if this.decls.has(a, df::generic) || this.decls.has(b, df::generic);
|
||||
|
||||
val ka := this.decls.types.p[a];
|
||||
val kb := this.decls.types.p[b];
|
||||
return false if this.types.part_count(ka) != this.types.part_count(kb);
|
||||
return false if (this.types.second_of(ka) != 0) != (this.types.second_of(kb) != 0);
|
||||
for i in 0..this.types.part_count(ka) {
|
||||
return false if this.types.part_at(ka, i as u32) != this.types.part_at(kb, i as u32);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Checks `main`, of the program itself, which takes nothing or the arguments
|
||||
#! as views of text, and returns nothing.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration of `main`.
|
||||
def checker::check_main(&mut this, id: u32): void {
|
||||
return if this.decls.names.p[id] != word::main || this.decls.modules.p[id] != 0 || this.decls.owners.p[id] != 0;
|
||||
|
||||
val kind: u32 = this.decls.types.p[id];
|
||||
var valid := this.types.first_of(kind) == bt::void_type && this.types.part_count(kind) <= 1;
|
||||
if valid && this.types.part_count(kind) == 1 {
|
||||
val texts := this.span_of(this.span_of(bt::uint8));
|
||||
valid = texts == bt::error || this.strip(this.types.part_at(kind, 0), 0) == this.strip(texts, 0);
|
||||
}
|
||||
if this.decls.overloads.p[id] != 0 || this.modules.decl(0, word::main) != id {
|
||||
valid = false;
|
||||
}
|
||||
return if valid;
|
||||
|
||||
this.report_decl(id, ec::invalid,
|
||||
"`main` is `def main(): void` or `def main(args: span<strview>): void`, and is not overloaded.");
|
||||
}
|
||||
|
||||
#! Reports a struct or union holding itself, other than through a pointer or a
|
||||
#! reference, which no amount of memory could hold.
|
||||
#! @param this The checker.
|
||||
#! @param id The struct's or union's declaration.
|
||||
def checker::check_contains(&mut this, id: u32): void {
|
||||
var seen := vector::<u32>::new();
|
||||
if this.holds(this.decls.types.p[id], id, &mut seen, 0) {
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.decl_text(id));
|
||||
text.append("` holds itself, which only a pointer or a reference to it can.");
|
||||
this.report_decl(id, ec::recursive, text.span);
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether a value of the type holds a value of the declaration's type, in its
|
||||
#! fields or elements, rather than through a pointer or reference.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param target The declaration looked for.
|
||||
#! @param seen The types already looked within, which are not looked within
|
||||
#! again.
|
||||
#! @param depth How deeply the search has gone.
|
||||
#! @returns Whether it does.
|
||||
def checker::holds(&mut this, kind: u32, target: u32, seen: &mut vector<u32>, depth: u32): bool {
|
||||
return false if depth > 32;
|
||||
val plain := this.unalias(kind);
|
||||
if this.types.kind_of(plain) == ty::fixed {
|
||||
return this.holds(this.types.first_of(plain), target, seen, depth + 1);
|
||||
}
|
||||
val named := this.named_kind(plain);
|
||||
return false if named != dk::struct_type && named != dk::union_type;
|
||||
|
||||
val decl := this.types.first_of(plain);
|
||||
return true if decl == target && depth > 0;
|
||||
return false if seen.span.contains(&plain);
|
||||
seen.append(plain);
|
||||
|
||||
val fields := this.field_types(plain);
|
||||
for &field in fields {
|
||||
return true if this.holds(field, target, seen, depth + 1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,608 @@
|
||||
#! @file composite.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the checking of values built from others: struct and union
|
||||
#! literals, array literals, and anonymous functions, which may take their
|
||||
#! types from the type expected of them.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! A struct or union literal, as `vec2{1, 2}`, `vec2{ y = 8 }`, or
|
||||
#! `res::<i64>{ error = 5 }`. A generic struct written without its type
|
||||
#! arguments takes them from the type expected, or from its fields' values.
|
||||
#! @param this The checker.
|
||||
#! @param node The `construct` node.
|
||||
#! @param expected The type expected, or the error type where none is.
|
||||
#! @returns The literal's type, or the error type where it is not valid.
|
||||
def checker::construct(&mut this, node: u32, expected: u32): u32 {
|
||||
val named := this.left_of(node);
|
||||
val inits := this.right_of(node);
|
||||
val decl := this.type_name_decl(named);
|
||||
if decl == 0 {
|
||||
this.check_inits(inits);
|
||||
return bt::error;
|
||||
}
|
||||
|
||||
var kind: u32 = bt::error;
|
||||
val wanted := this.unalias(expected);
|
||||
val written_args := this.segment_args(named, this.segment_count(named) - 1);
|
||||
if this.decls.has(decl, df::generic) && written_args == 0 && this.segment_count(named) == 1
|
||||
&& !this.scope_gives_args(this.segment_name(named, 0), decl) {
|
||||
if this.types.kind_of(wanted) == ty::named && this.types.first_of(wanted) == decl {
|
||||
kind = wanted;
|
||||
} else {
|
||||
kind = this.infer_struct(decl, inits, named);
|
||||
}
|
||||
} else {
|
||||
kind = this.resolve_type(named);
|
||||
}
|
||||
if kind == bt::error {
|
||||
this.check_inits(inits);
|
||||
return bt::error;
|
||||
}
|
||||
|
||||
# `void {}` is the one value of `void`, which holds nothing.
|
||||
val plain := this.unalias(kind);
|
||||
if plain == bt::void_type {
|
||||
if this.run_length(inits) != 0 {
|
||||
this.report(this.run_at(inits, 0), ec::invalid, "`void` holds nothing, so `void {}` is given no fields.");
|
||||
this.check_inits(inits);
|
||||
}
|
||||
return kind;
|
||||
}
|
||||
match this.named_kind(plain) {
|
||||
dk::struct_type => this.construct_struct(node, plain, inits);
|
||||
dk::union_type => this.construct_union(node, plain, inits);
|
||||
else => {
|
||||
this.report(named, ec::not_type, "Only a struct or a union is made with `{}`.");
|
||||
this.check_inits(inits);
|
||||
return bt::error;
|
||||
}
|
||||
}
|
||||
return kind;
|
||||
}
|
||||
|
||||
#! The declaration a type name names, without looking at its type arguments,
|
||||
#! or 0 where it names none.
|
||||
#! @param this The checker.
|
||||
#! @param node The `type_name` node.
|
||||
#! @returns The declaration, or 0.
|
||||
def checker::type_name_decl(&mut this, node: u32): u32 {
|
||||
if this.segment_count(node) == 1 {
|
||||
val name := this.segment_name(node, 0);
|
||||
val bound := this.bound(name);
|
||||
if (bound & ref_type) != 0 {
|
||||
this.set_ref(node, bound);
|
||||
return this.type_decl(bound & ref_mask);
|
||||
}
|
||||
|
||||
val decl := this.lookup_type(name, node);
|
||||
if decl == 0 || !this.is_type_decl(decl) {
|
||||
var text := string::from("No struct or union `", nothing {});
|
||||
text.append(this.names.text(name));
|
||||
text.append("` is declared where it is written.");
|
||||
this.report_text(node, ec::undefined, &text);
|
||||
return 0;
|
||||
}
|
||||
this.set_ref(node, decl);
|
||||
return decl;
|
||||
}
|
||||
|
||||
val kind := this.resolve_type(node);
|
||||
return kind == bt::error ? 0 : this.type_decl(this.unalias(kind));
|
||||
}
|
||||
|
||||
#! Whether a type around a name gives the arguments of the generic type it
|
||||
#! names, as `list<T>` gives `node` its `T` within it, which then takes no more.
|
||||
#! @param this The checker.
|
||||
#! @param name The ID of the name.
|
||||
#! @param decl The type's declaration.
|
||||
#! @returns Whether it does.
|
||||
def checker::scope_gives_args(&mut this, name: u32, decl: u32): bool {
|
||||
return false if this.bound(name) != 0 || this.own_param_count(decl) != 0;
|
||||
val found := this.in_scope(name, true);
|
||||
return found.decl == decl && this.types.kind_of(found.owner) == ty::named && this.types.part_count(found.owner) != 0;
|
||||
}
|
||||
|
||||
#! Checks the values of a literal whose type went wrong, so what is wrong
|
||||
#! within them is still reported.
|
||||
#! @param this The checker.
|
||||
#! @param run The run of `field_init` nodes.
|
||||
def checker::check_inits(&mut this, run: u32): void {
|
||||
for &each in this.run_view(run) {
|
||||
this.infer_expr(this.left_of(each));
|
||||
}
|
||||
}
|
||||
|
||||
#! The type of a generic struct whose type arguments its fields' values give.
|
||||
#! @param this The checker.
|
||||
#! @param decl The struct's declaration.
|
||||
#! @param inits The run of `field_init` nodes.
|
||||
#! @param node The node notes are placed at.
|
||||
#! @returns The struct's type, or the error type where its fields do not give
|
||||
#! its arguments.
|
||||
def checker::infer_struct(&mut this, decl: u32, inits: u32, node: u32): u32 {
|
||||
var inferred := this.begin_inference(decl, bt::error, span::<u32>::blank());
|
||||
val fields: u32 = this.decls.children.p[decl];
|
||||
for i in 0..this.run_length(inits) {
|
||||
val init := this.run_at(inits, i as u32);
|
||||
val field := this.init_field(decl, init, i as u32);
|
||||
continue if field == 0;
|
||||
|
||||
this.resolve(field);
|
||||
val value := this.left_of(init);
|
||||
if !this.deferred(value) {
|
||||
this.unify(this.decls.types.p[field], this.infer_expr(value), &mut inferred, 0);
|
||||
}
|
||||
}
|
||||
|
||||
val missing := this.unbound(&inferred);
|
||||
if missing != 0 {
|
||||
var text := string::from("Nothing gives the type of `", nothing {});
|
||||
text.append(this.decl_text(missing));
|
||||
text.append("`, so give the type arguments, as `");
|
||||
text.append(this.decl_text(decl));
|
||||
text.append("::<...>{...}`.");
|
||||
this.report_text(node, ec::generic, &text);
|
||||
return bt::error;
|
||||
}
|
||||
return this.named_of(decl, inferred.binds.span, node, true);
|
||||
}
|
||||
|
||||
#! The field a value of a literal is given for: the one it names, or the one at
|
||||
#! its place, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param decl The struct's or union's declaration.
|
||||
#! @param init The `field_init` node.
|
||||
#! @param index Its position in the literal.
|
||||
#! @returns The field's declaration, or 0.
|
||||
def checker::init_field(&mut this, decl: u32, init: u32, index: u32): u32 {
|
||||
if this.node_has(init, nf::named) {
|
||||
val found := this.find_member(this.decls.types.p[decl], this.name_of(init));
|
||||
return found.decl != 0 && this.decls.kinds.p[found.decl] == dk::field ? found.decl : 0;
|
||||
}
|
||||
|
||||
val run: u32 = this.decls.children.p[decl];
|
||||
var at: u32 = 0;
|
||||
for &field in this.decls.list_view(run) {
|
||||
continue if this.decls.kinds.p[field] != dk::field;
|
||||
return field if at == index;
|
||||
at++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! The fields of a struct literal: each value given converts to its field,
|
||||
#! and each field left out takes its default, or its zero value.
|
||||
#! @param this The checker.
|
||||
#! @param node The `construct` node.
|
||||
#! @param kind The struct's type.
|
||||
#! @param inits The run of `field_init` nodes.
|
||||
def checker::construct_struct(&mut this, node: u32, kind: u32, inits: u32): void {
|
||||
val decl := this.types.first_of(kind);
|
||||
var given := vector::<u32>::new();
|
||||
for i in 0..this.run_length(inits) {
|
||||
val init := this.run_at(inits, i as u32);
|
||||
val value := this.left_of(init);
|
||||
val field := this.init_field(decl, init, i as u32);
|
||||
if field == 0 {
|
||||
if this.node_has(init, nf::named) {
|
||||
this.report_no_member(init, kind, this.name_of(init));
|
||||
} else {
|
||||
this.report(init, ec::arguments, "The literal gives more values than the struct has fields.");
|
||||
}
|
||||
this.infer_expr(value);
|
||||
continue;
|
||||
}
|
||||
|
||||
var twice := false;
|
||||
for &each in given {
|
||||
twice = twice || each == field;
|
||||
}
|
||||
if twice {
|
||||
this.report(init, ec::duplicate, "The field is given a value twice.");
|
||||
}
|
||||
given.append(field);
|
||||
this.check_reaches(init, kind, field);
|
||||
|
||||
this.resolve(field);
|
||||
val owner: u32 = this.decls.owners.p[field];
|
||||
val held := owner == decl
|
||||
? this.instantiated(this.decls.types.p[field], decl, kind)
|
||||
: this.decls.types.p[field];
|
||||
this.check_expr(value, held);
|
||||
if this.types.kind_of(this.unalias(held)) == ty::pack {
|
||||
# A field holding a pack is given a pack of its types whole, whose values
|
||||
# that close are moved to it, as `...` moves them to a call.
|
||||
this.check_pack_moved(value, held, held);
|
||||
} else {
|
||||
this.check_owned(value, held);
|
||||
}
|
||||
this.set_ref(init, field);
|
||||
}
|
||||
|
||||
# A field left out takes its default, or its zero value, which a reference
|
||||
# or a function does not have.
|
||||
val run: u32 = this.decls.children.p[decl];
|
||||
for i in 0..this.decls.list_length(run) {
|
||||
val field := this.decls.list_at(run, i as u32);
|
||||
continue if this.decls.kinds.p[field] != dk::field;
|
||||
|
||||
var was_given := false;
|
||||
for &each in given {
|
||||
was_given = was_given || each == field;
|
||||
}
|
||||
continue if was_given || this.field_default(field) != 0;
|
||||
|
||||
this.resolve(field);
|
||||
val held := this.instantiated(this.decls.types.p[field], decl, kind);
|
||||
if !this.has_zero(held, 0) {
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.decl_text(field));
|
||||
text.append("` has no zero value, so the literal gives it a value.");
|
||||
this.report_text(node, ec::zero_value, &text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! The default value a field is declared with, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param field The field's declaration.
|
||||
#! @returns The default's node, or 0.
|
||||
pub def checker::field_default(&this, field: u32): u32 {
|
||||
return this.record_in(field, variable_slot::value);
|
||||
}
|
||||
|
||||
#! A union literal, which names the one field it holds, or none for its zero
|
||||
#! value.
|
||||
#! @param this The checker.
|
||||
#! @param node The `construct` node.
|
||||
#! @param kind The union's type.
|
||||
#! @param inits The run of `field_init` nodes.
|
||||
def checker::construct_union(&mut this, node: u32, kind: u32, inits: u32): void {
|
||||
# Naming none, it is the union's zero value.
|
||||
return if this.run_length(inits) == 0;
|
||||
if this.run_length(inits) != 1 {
|
||||
this.report(node, ec::arguments, "A union literal names the one field it holds, as `res{ error = 5 }`.");
|
||||
this.check_inits(inits);
|
||||
return;
|
||||
}
|
||||
|
||||
val decl := this.types.first_of(kind);
|
||||
val init := this.run_at(inits, 0);
|
||||
val field := this.init_field(decl, init, 0);
|
||||
if field == 0 || !this.node_has(init, nf::named) {
|
||||
this.report(init, ec::member, "A union literal names the field it holds, as `res{ error = 5 }`.");
|
||||
this.infer_expr(this.left_of(init));
|
||||
return;
|
||||
}
|
||||
|
||||
this.check_reaches(init, kind, field);
|
||||
this.resolve(field);
|
||||
this.check_expr(this.left_of(init), this.instantiated(this.decls.types.p[field], decl, kind));
|
||||
this.set_ref(init, field);
|
||||
}
|
||||
|
||||
#! Whether a type has a zero value, which a variable without a value starts at:
|
||||
#! numbers, `bool`, pointers, and enums with a member of value 0 do, and so do
|
||||
#! arrays, structs, and unions of what does, but references and functions do
|
||||
#! not, and a type parameter only where its bounds say so.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param depth How deeply the search has gone, which stops a type nesting
|
||||
#! without end.
|
||||
#! @returns Whether it has.
|
||||
pub def checker::has_zero(&mut this, kind: u32, depth: u32): bool {
|
||||
return true if depth > 8 || kind == bt::error;
|
||||
|
||||
val plain := this.unalias(kind);
|
||||
match this.types.kind_of(plain) {
|
||||
ty::reference, ty::function => return false;
|
||||
ty::param => return this.param_zeroable(plain);
|
||||
ty::pack => {
|
||||
return this.param_zeroable(plain) if this.types.is_open_pack(plain);
|
||||
for i in 0..this.types.part_count(plain) {
|
||||
return false if !this.has_zero(this.types.part_at(plain, i as u32), depth + 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
ty::fixed => return this.has_zero(this.types.first_of(plain), depth + 1);
|
||||
ty::named => {
|
||||
val decl := this.types.first_of(plain);
|
||||
match this.decls.kinds.p[decl] {
|
||||
dk::enum_type => {
|
||||
this.resolve(decl);
|
||||
val run: u32 = this.decls.children.p[decl];
|
||||
for &member in this.decls.list_view(run) {
|
||||
var value := constant {};
|
||||
if this.decl_value(member, &mut value) && value.magnitude == 0 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
dk::struct_type => {
|
||||
val run: u32 = this.decls.children.p[decl];
|
||||
for i in 0..this.decls.list_length(run) {
|
||||
val field := this.decls.list_at(run, i as u32);
|
||||
continue if this.decls.kinds.p[field] != dk::field || this.field_default(field) != 0;
|
||||
this.resolve(field);
|
||||
return false if !this.has_zero(this.instantiated(this.decls.types.p[field], decl, plain), depth + 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
dk::union_type => {
|
||||
val fields := this.field_types(plain);
|
||||
return fields.length == 0 || this.has_zero(fields[0], depth + 1);
|
||||
}
|
||||
else => return true;
|
||||
}
|
||||
}
|
||||
else => return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Whether a type parameter's bounds give it a zero value: `zeroable` does, and
|
||||
#! so do `integer`, `real`, and `pointer`, since every number and pointer has one,
|
||||
#! and `T::element` has one where `T: array` does.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type parameter's type.
|
||||
#! @returns Whether they do.
|
||||
def checker::param_zeroable(&mut this, kind: u32): bool {
|
||||
val zeroable := this.std_decl(word::zeroable);
|
||||
val integer := this.std_decl(word::integer);
|
||||
val real := this.std_decl(word::real);
|
||||
val pointer := this.std_decl(word::pointer);
|
||||
return (zeroable != 0 && this.param_bounded(kind, zeroable)) || (integer != 0 && this.param_bounded(kind, integer))
|
||||
|| (real != 0 && this.param_bounded(kind, real)) || (pointer != 0 && this.param_bounded(kind, pointer))
|
||||
|| (this.types.kind_of(kind) == ty::param && this.element_zeroable(kind));
|
||||
}
|
||||
|
||||
#! Says why a type has no zero value, for a report that it is not `zeroable`:
|
||||
#! the type itself, or the field within it that has none, followed through
|
||||
#! fields and the elements of arrays as `has_zero` follows them, as "its field
|
||||
#! `inner.r` holds `&i64`, which has no zero value".
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Why, as a note's detail.
|
||||
pub def checker::zero_lacking(&mut this, kind: u32): string {
|
||||
var fields := string::new(nothing {});
|
||||
var at := this.unalias(kind);
|
||||
for _ in 0..8 {
|
||||
if this.types.kind_of(at) == ty::fixed {
|
||||
at = this.unalias(this.types.first_of(at));
|
||||
continue;
|
||||
}
|
||||
val is_union := this.named_kind(at) == dk::union_type;
|
||||
break if this.named_kind(at) != dk::struct_type && !is_union;
|
||||
|
||||
# A union is zeroed as its first field, and a struct as each without a
|
||||
# default.
|
||||
val decl := this.types.first_of(at);
|
||||
val run: u32 = this.decls.children.p[decl];
|
||||
var lacking: u32 = 0;
|
||||
var held: u32 = 0;
|
||||
for i in 0..this.decls.list_length(run) {
|
||||
val field := this.decls.list_at(run, i as u32);
|
||||
continue if this.decls.kinds.p[field] != dk::field;
|
||||
this.resolve(field);
|
||||
val inner := this.instantiated(this.decls.types.p[field], decl, at);
|
||||
if (is_union || this.field_default(field) == 0) && !this.has_zero(inner, 0) {
|
||||
lacking = field;
|
||||
held = inner;
|
||||
}
|
||||
break if lacking != 0 || is_union;
|
||||
}
|
||||
break if lacking == 0;
|
||||
|
||||
if fields.length > 0 {
|
||||
fields.append(".");
|
||||
}
|
||||
fields.append(this.decl_text(lacking));
|
||||
at = this.unalias(held);
|
||||
}
|
||||
|
||||
val described := this.describe(at);
|
||||
val is_enum := this.named_kind(at) == dk::enum_type;
|
||||
val is_param := this.types.kind_of(at) == ty::param;
|
||||
var text := string::new(nothing {});
|
||||
if fields.length > 0 {
|
||||
text.append("its field `");
|
||||
text.append(fields.span);
|
||||
text.append("` holds `");
|
||||
text.append(described.span);
|
||||
text.append(is_enum ? "`, no member of which has the value 0"
|
||||
: is_param ? "`, which has a zero value only where its bounds say it is `zeroable`"
|
||||
: "`, which has no zero value");
|
||||
return text;
|
||||
}
|
||||
if is_enum {
|
||||
text.append("no member of `");
|
||||
text.append(described.span);
|
||||
text.append("` has the value 0");
|
||||
return text;
|
||||
}
|
||||
if is_param {
|
||||
text.append("a type parameter has a zero value only where its bounds say it is `zeroable`");
|
||||
return text;
|
||||
}
|
||||
if this.types.kind_of(at) == ty::reference || this.types.kind_of(at) == ty::function {
|
||||
text.append(this.types.kind_of(at) == ty::reference ? "a reference has no zero value" : "a function has no zero value");
|
||||
return text;
|
||||
}
|
||||
text.append("`");
|
||||
text.append(described.span);
|
||||
text.append("` has no zero value");
|
||||
return text;
|
||||
}
|
||||
|
||||
#! An array literal: a fixed array of the type its elements share, or of the
|
||||
#! type expected, or where a `span<T>` is expected, a view of itself.
|
||||
#! @param this The checker.
|
||||
#! @param node The `array` node.
|
||||
#! @param expected The type expected, or the error type where none is.
|
||||
#! @returns The array's type, or the error type where its elements share none.
|
||||
def checker::array(&mut this, node: u32, expected: u32): u32 {
|
||||
val run := this.left_of(node);
|
||||
val count := this.run_length(run);
|
||||
val wanted := this.unalias(expected);
|
||||
if this.types.kind_of(wanted) == ty::fixed {
|
||||
val element := this.types.first_of(wanted);
|
||||
for i in 0..count {
|
||||
this.check_expr(this.run_at(run, i as u32), element);
|
||||
}
|
||||
# Within a template, a length only each instance knows is theirs to check.
|
||||
if count != this.types.second_of(wanted) && this.types.part_count(wanted) == 0 {
|
||||
val words := $"The array holds {this.types.second_of(wanted)} elements, but {count} are written.";
|
||||
var text := string::from(words.span, nothing {});
|
||||
this.report_text(node, ec::mismatch, &text);
|
||||
}
|
||||
return expected;
|
||||
}
|
||||
|
||||
if this.is_std(wanted, word::span) {
|
||||
val element := this.types.part_at(wanted, 0);
|
||||
for i in 0..count {
|
||||
this.check_expr(this.run_at(run, i as u32), element);
|
||||
}
|
||||
return expected;
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
this.report(node, ec::mismatch, "An empty array takes its type from where it is written, and nothing gives it here.");
|
||||
return bt::error;
|
||||
}
|
||||
|
||||
var shared: u32 = 0;
|
||||
for i in 0..count {
|
||||
val kind := this.infer_expr(this.run_at(run, i as u32));
|
||||
if kind == bt::error {
|
||||
shared = bt::error;
|
||||
continue;
|
||||
}
|
||||
if shared == 0 {
|
||||
shared = kind;
|
||||
continue;
|
||||
}
|
||||
if shared != bt::error {
|
||||
val joined := this.common(shared, kind);
|
||||
if joined == bt::error {
|
||||
this.report_operands(this.run_at(run, i as u32), shared, kind, "are elements of one array, which share no type");
|
||||
}
|
||||
shared = joined;
|
||||
}
|
||||
}
|
||||
return bt::error if shared == bt::error;
|
||||
|
||||
val element := this.settle(shared);
|
||||
for i in 0..count {
|
||||
this.convert(this.run_at(run, i as u32), element);
|
||||
}
|
||||
return this.types.fixed(element, count);
|
||||
}
|
||||
|
||||
#! An anonymous function, whose parameter types and result may be left out
|
||||
#! where the function type expected gives them. It shares the locals of the
|
||||
#! function around it, so it is checked within that function's bindings.
|
||||
#! @param this The checker.
|
||||
#! @param node The `lambda` node.
|
||||
#! @param expected The function type expected, or the error type where none
|
||||
#! is.
|
||||
#! @returns The function type.
|
||||
def checker::lambda(&mut this, node: u32, expected: u32): u32 {
|
||||
# C calls back what it is given with nothing more, so it can't reach what a
|
||||
# lambda refers to.
|
||||
val c_wanted := this.unalias(expected);
|
||||
if this.is_std(c_wanted, word::cfn) && this.types.part_count(c_wanted) == 1 {
|
||||
this.report(node, ec::invalid, "C calls back a function of the program named where its address is expected, not a lambda, whose locals C gives it no way to reach.");
|
||||
this.lambda(node, this.types.part_at(c_wanted, 0));
|
||||
return bt::error;
|
||||
}
|
||||
|
||||
val params := this.left_of(node);
|
||||
val result := this.record(node, lambda_slot::result);
|
||||
val body := this.record(node, lambda_slot::body);
|
||||
val wanted := this.unalias(expected);
|
||||
val gives := this.types.kind_of(wanted) == ty::function;
|
||||
|
||||
var kinds := vector::<u32>::new();
|
||||
for i in 0..this.run_length(params) {
|
||||
val param := this.run_at(params, i as u32);
|
||||
val written := this.left_of(param);
|
||||
if written != 0 {
|
||||
kinds.append(this.resolve_type(written));
|
||||
} else if gives && i < this.types.part_count(wanted) {
|
||||
kinds.append(this.types.part_at(wanted, i as u32));
|
||||
} else {
|
||||
this.report(param, ec::mismatch, gives
|
||||
? "The function type expected here has fewer parameters, so give this one a type."
|
||||
: "No function type is expected here, so give the parameter a type.");
|
||||
kinds.append(bt::error);
|
||||
}
|
||||
}
|
||||
|
||||
# A lambda whose result infers a type argument takes it from its body.
|
||||
val free := this.free_result;
|
||||
this.free_result = false;
|
||||
var returns: u32 = 0;
|
||||
if result != 0 {
|
||||
returns = this.resolve_type(result);
|
||||
} else if gives && !free {
|
||||
returns = this.types.first_of(wanted);
|
||||
}
|
||||
|
||||
val saved := this.at;
|
||||
this.at.result = returns != 0 ? returns : bt::void_type;
|
||||
this.at.loops = 0;
|
||||
this.at.body = body;
|
||||
val mark := this.scope_mark();
|
||||
val scope := this.lifetime_open(node);
|
||||
for i in 0..this.run_length(params) {
|
||||
val param := this.run_at(params, i as u32);
|
||||
this.set_type(param, kinds[i]);
|
||||
this.mark_local(param);
|
||||
this.set_flag(param, rf::mutable);
|
||||
this.bind(this.name_of(param), ref_local | param);
|
||||
}
|
||||
|
||||
if this.kind_of(body) == nk::block {
|
||||
val leaves := this.check_block(body) != flow::goes_on;
|
||||
if returns == 0 {
|
||||
returns = bt::void_type;
|
||||
} else if returns != bt::void_type && !leaves {
|
||||
this.report(node, ec::control, "The function can end without returning a value.");
|
||||
}
|
||||
} else if returns == 0 {
|
||||
# What the body gives is taken as a binding takes it: what a reference
|
||||
# reaches, unless `&` is written.
|
||||
returns = this.settle_expr(body);
|
||||
if this.takes_value(body, returns) {
|
||||
returns = this.types.first_of(this.unalias(returns));
|
||||
this.convert(body, returns);
|
||||
this.check_owned(body, returns);
|
||||
}
|
||||
} else if returns == bt::void_type {
|
||||
this.settle_expr(body);
|
||||
} else {
|
||||
this.check_expr(body, returns);
|
||||
}
|
||||
|
||||
# A body that is a value is what the function returns.
|
||||
if this.kind_of(body) != nk::block {
|
||||
this.lifetime_event(node, lifetime_kind_of_event::returned);
|
||||
}
|
||||
this.lifetime_close(scope);
|
||||
this.unbind_to(mark);
|
||||
this.at.result = saved.result;
|
||||
this.at.loops = saved.loops;
|
||||
this.at.body = saved.body;
|
||||
# One whose parameter has no type was reported, and is nothing a call can take.
|
||||
for &kind in kinds.span {
|
||||
return bt::error if kind == bt::error;
|
||||
}
|
||||
return this.types.function(kinds.span, returns, false);
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
#! @file constants.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the constants the checker works out while checking: the values of
|
||||
#! literals, and of operators applied to constants, which the length of a
|
||||
#! fixed array, the value of an enum member, and whether a constant fits its
|
||||
#! type need. An integer is kept as its magnitude and sign, so every value of
|
||||
#! both `i64` and `u64` is one.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! A constant integer of the magnitude and sign, where zero is never negative.
|
||||
#! @param magnitude Its magnitude.
|
||||
#! @param negative Whether it is below zero.
|
||||
#! @returns The constant.
|
||||
pub def constant::integer(magnitude: u64, negative: bool): constant {
|
||||
return constant { word = magnitude, negative = negative && magnitude != 0 };
|
||||
}
|
||||
|
||||
#! A constant float.
|
||||
#! @param real Its value.
|
||||
#! @returns The constant.
|
||||
pub def constant::real_of(real: f64): constant {
|
||||
var held := real;
|
||||
return constant { word = ((&mut held) as *mut u64)[0], floating = true };
|
||||
}
|
||||
|
||||
#! A constant `bool`, as a magnitude of 0 or 1.
|
||||
#! @param value Its value.
|
||||
#! @returns The constant.
|
||||
pub def constant::truth(value: bool): constant {
|
||||
return constant { word = value ? 1 : 0 };
|
||||
}
|
||||
|
||||
#! An integer's magnitude, or a `bool` as 0 or 1, or 0 for a float.
|
||||
#! @param this The constant.
|
||||
#! @returns The magnitude.
|
||||
pub val constant::magnitude: u64 { get => this.floating ? 0 : this.word; }
|
||||
|
||||
#! A float's value, or 0 for an integer.
|
||||
#! @param this The constant.
|
||||
#! @returns The value.
|
||||
pub val constant::real: f64 {
|
||||
get {
|
||||
return 0.0 if !this.floating;
|
||||
var held := this.word;
|
||||
return ((&mut held) as *mut f64)[0];
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether two constants are the same value, a float's bits and all, as
|
||||
#! interning them asks.
|
||||
#! @param this The one constant.
|
||||
#! @param other The other.
|
||||
#! @returns Whether they are.
|
||||
pub def constant::same(&this, other: &constant): bool {
|
||||
return this.word == other.word && this.negative == other.negative && this.floating == other.floating;
|
||||
}
|
||||
|
||||
#! The constant as a float.
|
||||
#! @param this The constant.
|
||||
#! @returns The float, which an integer converts to.
|
||||
pub def constant::as_real(&this): f64 {
|
||||
return this.real if this.floating;
|
||||
val magnitude := this.magnitude as f64;
|
||||
return this.negative ? -magnitude : magnitude;
|
||||
}
|
||||
|
||||
#! The constant's 64 bits, as two's complement where it is negative.
|
||||
#! @param this The constant.
|
||||
#! @returns The bits.
|
||||
pub def constant::bits(&this): u64 {
|
||||
return this.negative ? ~this.magnitude + 1 : this.magnitude;
|
||||
}
|
||||
|
||||
#! The constant of the 64 bits, read as signed where `signed` says.
|
||||
#! @param bits The bits.
|
||||
#! @param signed Whether they are two's complement.
|
||||
#! @returns The constant.
|
||||
pub def constant::from_bits(bits: u64, signed: bool): constant {
|
||||
if signed && (bits >> 63) != 0 {
|
||||
return constant::integer(~bits + 1, true);
|
||||
}
|
||||
return constant::integer(bits, false);
|
||||
}
|
||||
|
||||
#! The integer an integer literal writes, in decimal or with `0x`, `0o`, or
|
||||
#! `0b`, without its `u`, or false where it is past 64 bits.
|
||||
#! @param this The checker.
|
||||
#! @param text The literal as written.
|
||||
#! @param out Where the integer is written.
|
||||
#! @returns Whether it fits in 64 bits.
|
||||
pub def checker::read_integer(&this, text: strview, out: &mut constant): bool {
|
||||
var at := 0u;
|
||||
var base := 10u;
|
||||
var limit := text.length;
|
||||
if limit > 0 && text[limit - 1] == 'u' {
|
||||
limit--;
|
||||
}
|
||||
if limit > 2 && text[0] == '0' {
|
||||
val prefix := text[1];
|
||||
if prefix == 'x' { base = 16; at = 2; }
|
||||
if prefix == 'o' { base = 8; at = 2; }
|
||||
if prefix == 'b' { base = 2; at = 2; }
|
||||
}
|
||||
|
||||
var magnitude := 0u;
|
||||
while at < limit {
|
||||
val c := text[at];
|
||||
var digit := 0u;
|
||||
if c >= '0' && c <= '9' {
|
||||
digit = (c - '0') as u64;
|
||||
} else if c >= 'a' && c <= 'f' {
|
||||
digit = (c - 'a') as u64 + 10;
|
||||
} else if c >= 'A' && c <= 'F' {
|
||||
digit = (c - 'A') as u64 + 10;
|
||||
} else {
|
||||
at++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if magnitude > (0xFFFFFFFFFFFFFFFFu - digit) / base {
|
||||
return false;
|
||||
}
|
||||
magnitude = magnitude * base + digit;
|
||||
at++;
|
||||
}
|
||||
|
||||
out.word = magnitude;
|
||||
out.negative = false;
|
||||
out.floating = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The float a float literal writes, without its `f`.
|
||||
#! @param this The checker.
|
||||
#! @param text The literal as written.
|
||||
#! @returns The float.
|
||||
pub def checker::read_real(&this, text: strview): f64 {
|
||||
var limit := text.length;
|
||||
if limit > 0 && text[limit - 1] == 'f' {
|
||||
limit--;
|
||||
}
|
||||
|
||||
return match real::parse::<f64>(text.substr(0, limit)) {
|
||||
value(read) => read,
|
||||
else => 0.0
|
||||
};
|
||||
}
|
||||
|
||||
#! The code point a character literal writes, from its escape where it is one.
|
||||
#! @param this The checker.
|
||||
#! @param text The literal as written, with its quotes.
|
||||
#! @returns The code point.
|
||||
pub def checker::read_character(&this, text: strview): u64 {
|
||||
return 0 if text.length < 3;
|
||||
|
||||
val inner := text.substr(1, text.length - 2);
|
||||
if inner[0] == '\\' && inner.length >= 2 {
|
||||
return match inner[1] {
|
||||
'n' => 10,
|
||||
't' => 9,
|
||||
'r' => 13,
|
||||
'0' => 0,
|
||||
'x' => this.read_hex(inner.substr(2, 2)),
|
||||
else => inner[1] as u64
|
||||
};
|
||||
}
|
||||
|
||||
# UTF-8: the lead byte's bits, then six of each byte continuing it.
|
||||
val lead := inner[0];
|
||||
return lead as u64 if lead < 0x80;
|
||||
|
||||
val count: u64 = lead >= 0xF0 ? 3 : lead >= 0xE0 ? 2 : 1;
|
||||
var code: u64 = (lead & (0x3F >> count as u8)) as u64;
|
||||
for i in 1..(count + 1) {
|
||||
break if i >= inner.length;
|
||||
code = (code << 6) | (inner[i] & 0x3F) as u64;
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
def checker::read_hex(&this, text: strview): u64 {
|
||||
var value := 0u;
|
||||
for &c in text {
|
||||
var digit := 0u;
|
||||
if c >= '0' && c <= '9' { digit = (c - '0') as u64; }
|
||||
else if c >= 'a' && c <= 'f' { digit = (c - 'a') as u64 + 10; }
|
||||
else if c >= 'A' && c <= 'F' { digit = (c - 'A') as u64 + 10; }
|
||||
value = value * 16 + digit;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
#! Whether the constant is within the range of the type, as a signed or
|
||||
#! unsigned integer of its bits, a float, or a constant of no type.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param value The constant.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::in_range(&this, kind: u32, value: &constant): bool {
|
||||
val bits := this.types.first_of(kind);
|
||||
match this.types.kind_of(kind) {
|
||||
ty::signed => {
|
||||
return true if value.floating;
|
||||
val top: u64 = 1u << (bits - 1) as u64;
|
||||
return value.negative ? value.magnitude <= top : value.magnitude < top;
|
||||
}
|
||||
ty::unsigned => {
|
||||
return true if value.floating;
|
||||
return false if value.negative;
|
||||
return bits == 64 || value.magnitude < (1u << bits as u64);
|
||||
}
|
||||
ty::untyped_uint => return !value.negative;
|
||||
else => return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The constant an operator gives of two constants, where it gives one: false
|
||||
#! where it overflows 64 bits, divides by zero, or is no operator on constants.
|
||||
#! @param this The checker.
|
||||
#! @param operator The operator.
|
||||
#! @param a The left constant.
|
||||
#! @param b The right constant.
|
||||
#! @param signed Whether they are signed, which division, remainder,
|
||||
#! comparisons, and shifts care about.
|
||||
#! @param out Where the constant given is written.
|
||||
#! @returns Whether it gives one.
|
||||
pub def checker::fold_binary(&this, operator: tk, a: &constant, b: &constant, signed: bool, out: &mut constant): bool {
|
||||
if a.floating || b.floating {
|
||||
val x := a.as_real();
|
||||
val y := b.as_real();
|
||||
match operator {
|
||||
tk::plus => { out = constant::real_of(x + y); return true; }
|
||||
tk::minus => { out = constant::real_of(x - y); return true; }
|
||||
tk::star => { out = constant::real_of(x * y); return true; }
|
||||
tk::slash => { out = constant::real_of(x / y); return true; }
|
||||
tk::modulo => { out = constant::real_of(x % y); return true; }
|
||||
tk::equals => { out = constant::truth(x == y); return true; }
|
||||
tk::noteq => { out = constant::truth(x != y); return true; }
|
||||
tk::less => { out = constant::truth(x < y); return true; }
|
||||
tk::lesseq => { out = constant::truth(x <= y); return true; }
|
||||
tk::more => { out = constant::truth(x > y); return true; }
|
||||
tk::moreeq => { out = constant::truth(x >= y); return true; }
|
||||
else => return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
match operator {
|
||||
tk::plus => return this.fold_add(a, b.magnitude, b.negative, out);
|
||||
tk::minus => return this.fold_add(a, b.magnitude, !b.negative, out);
|
||||
tk::star => {
|
||||
if a.magnitude != 0 && b.magnitude > 0xFFFFFFFFFFFFFFFFu / a.magnitude {
|
||||
return false;
|
||||
}
|
||||
out = constant::integer(a.magnitude * b.magnitude, a.negative != b.negative);
|
||||
return true;
|
||||
}
|
||||
tk::slash => {
|
||||
return false if b.magnitude == 0;
|
||||
out = constant::integer(a.magnitude / b.magnitude, a.negative != b.negative);
|
||||
return true;
|
||||
}
|
||||
tk::modulo => {
|
||||
return false if b.magnitude == 0;
|
||||
out = constant::integer(a.magnitude % b.magnitude, a.negative);
|
||||
return true;
|
||||
}
|
||||
tk::bitand => { out = constant::from_bits(a.bits() & b.bits(), a.negative || b.negative); return true; }
|
||||
tk::bitor => { out = constant::from_bits(a.bits() | b.bits(), a.negative || b.negative); return true; }
|
||||
tk::bitxor => { out = constant::from_bits(a.bits() ^ b.bits(), a.negative != b.negative); return true; }
|
||||
tk::lshift => {
|
||||
return false if b.negative || b.magnitude >= 64;
|
||||
val shifted := a.magnitude << b.magnitude;
|
||||
return false if (shifted >> b.magnitude) != a.magnitude;
|
||||
out = constant::integer(shifted, a.negative);
|
||||
return true;
|
||||
}
|
||||
tk::rshift => {
|
||||
return false if b.negative;
|
||||
if b.magnitude >= 64 {
|
||||
out = constant::integer(a.negative ? 1 : 0, a.negative);
|
||||
return true;
|
||||
}
|
||||
out = a.negative
|
||||
? constant::from_bits(((a.bits() as i64) >> b.magnitude as i64) as u64, true)
|
||||
: constant::integer(a.magnitude >> b.magnitude, false);
|
||||
return true;
|
||||
}
|
||||
tk::equals => { out = constant::truth(this.compare_constants(a, b) == 0); return true; }
|
||||
tk::noteq => { out = constant::truth(this.compare_constants(a, b) != 0); return true; }
|
||||
tk::less => { out = constant::truth(this.compare_constants(a, b) < 0); return true; }
|
||||
tk::lesseq => { out = constant::truth(this.compare_constants(a, b) <= 0); return true; }
|
||||
tk::more => { out = constant::truth(this.compare_constants(a, b) > 0); return true; }
|
||||
tk::moreeq => { out = constant::truth(this.compare_constants(a, b) >= 0); return true; }
|
||||
tk::logand => { out = constant::truth(a.magnitude != 0 && b.magnitude != 0); return true; }
|
||||
tk::logor => { out = constant::truth(a.magnitude != 0 || b.magnitude != 0); return true; }
|
||||
else => return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
def checker::fold_add(&this, a: &constant, magnitude: u64, negative: bool, out: &mut constant): bool {
|
||||
if a.negative == negative {
|
||||
return false if a.magnitude > 0xFFFFFFFFFFFFFFFFu - magnitude;
|
||||
out = constant::integer(a.magnitude + magnitude, negative);
|
||||
return true;
|
||||
}
|
||||
|
||||
out = a.magnitude >= magnitude
|
||||
? constant::integer(a.magnitude - magnitude, a.negative)
|
||||
: constant::integer(magnitude - a.magnitude, negative);
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Below zero where `a` is less than `b`, zero where they are equal, and above
|
||||
#! zero where `a` is more.
|
||||
#! @param this The checker.
|
||||
#! @param a One constant.
|
||||
#! @param b The other.
|
||||
#! @returns Their order.
|
||||
pub def checker::compare_constants(&this, a: &constant, b: &constant): i32 {
|
||||
if a.floating || b.floating {
|
||||
val x := a.as_real();
|
||||
val y := b.as_real();
|
||||
return x < y ? -1 : x > y ? 1 : 0;
|
||||
}
|
||||
|
||||
return -1 if a.negative && !b.negative;
|
||||
return 1 if !a.negative && b.negative;
|
||||
if a.magnitude == b.magnitude {
|
||||
return 0;
|
||||
}
|
||||
val less := a.magnitude < b.magnitude;
|
||||
return (less != a.negative) ? -1 : 1;
|
||||
}
|
||||
@@ -0,0 +1,895 @@
|
||||
#! @file convert.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains when a value of one type is taken where another is expected, and
|
||||
#! how well it fits there, which overloads are chosen by: a type exactly, then
|
||||
#! a constant taking the type expected, then any other conversion, then
|
||||
#! becoming a union. Also contains the type two values share, as both sides of
|
||||
#! an operator or both branches of a ternary must.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! How a value fits where a type is expected, best first.
|
||||
pub enum fit : u8 {
|
||||
exact, #! It is the type.
|
||||
read, #! It is a reference, read through to what it refers to, which is the type.
|
||||
constant, #! It is a constant, which takes the type.
|
||||
convert, #! It converts: widening, an alias, a view lent, or reading through a reference.
|
||||
unite, #! It becomes a union holding it.
|
||||
none, #! It does not fit.
|
||||
overflow, #! It is a constant outside the range of the type.
|
||||
}
|
||||
|
||||
#! The declaration of `core`'s root of the name, which the language relies on,
|
||||
#! or 0 where it declares none.
|
||||
#! @param this The checker.
|
||||
#! @param word The ID of the name.
|
||||
#! @returns The declaration, or 0.
|
||||
pub def checker::std_decl(&mut this, word: u32): u32 {
|
||||
if this.known.get(&word) is some(got) {
|
||||
return got;
|
||||
}
|
||||
|
||||
val core := this.modules.find(0, word::core);
|
||||
val found := core == 0 ? 0 : this.modules.decl(core, word);
|
||||
this.known.set(word, found);
|
||||
return found;
|
||||
}
|
||||
|
||||
#! The type of `core`'s root of the name given the arguments, or the error type
|
||||
#! where it declares none.
|
||||
#! @param this The checker.
|
||||
#! @param word The ID of the name.
|
||||
#! @param args The type arguments, or none.
|
||||
#! @returns The type, or the error type.
|
||||
pub def checker::std_type(&mut this, word: u32, args: span<u32>): u32 {
|
||||
val decl := this.std_decl(word);
|
||||
return bt::error if decl == 0;
|
||||
|
||||
this.resolve(decl);
|
||||
return this.types.named(decl, args) if args.length != 0;
|
||||
return this.decls.types.p[decl];
|
||||
}
|
||||
|
||||
#! A `span<T>` of the element.
|
||||
#! @param this The checker.
|
||||
#! @param element The element's type.
|
||||
#! @returns The span's type, or the error type where `core` declares no `span`.
|
||||
pub def checker::span_of(&mut this, element: u32): u32 {
|
||||
var args := vector::<u32>::new();
|
||||
args.append(element);
|
||||
return this.std_type(word::span, args.span);
|
||||
}
|
||||
|
||||
#! The declaration a type is of: a named type's, a built-in type's, or a type
|
||||
#! parameter's, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns The declaration, or 0.
|
||||
pub def checker::type_decl(&this, kind: u32): u32 {
|
||||
return match this.types.kind_of(kind) {
|
||||
ty::named, ty::param => this.types.first_of(kind),
|
||||
else => kind < bt::count as u32 ? this.builtin_of[kind] : 0
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether the type is the named type of `core`'s root of the name, of any
|
||||
#! arguments.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param word The ID of the name.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::is_std(&mut this, kind: u32, word: u32): bool {
|
||||
return this.types.kind_of(kind) == ty::named && this.types.first_of(kind) == this.std_decl(word)
|
||||
&& this.std_decl(word) != 0;
|
||||
}
|
||||
|
||||
#! Whether a type is `core`'s vector or mask, `simd<L[N]>` or `mask<L[N]>`, which an
|
||||
#! executable holds in vector registers.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::is_lanes_type(&mut this, kind: u32): bool {
|
||||
val plain := this.unalias(kind);
|
||||
return this.is_std(plain, word::simd) || this.is_std(plain, word::mask);
|
||||
}
|
||||
|
||||
#! The type an alias stands for, given its arguments.
|
||||
#! @param this The checker.
|
||||
#! @param kind The alias's type.
|
||||
#! @returns The type it stands for.
|
||||
pub def checker::alias_target(&mut this, kind: u32): u32 {
|
||||
if this.types.kind_of(kind) == ty::matched {
|
||||
return this.reduce_match(kind) if this.choosable(kind);
|
||||
val chosen := this.proved_choice(kind);
|
||||
return chosen != 0 ? chosen : kind;
|
||||
}
|
||||
val decl := this.types.first_of(kind);
|
||||
this.resolve(decl);
|
||||
return this.instantiated(this.decls.targets.p[decl], decl, kind);
|
||||
}
|
||||
|
||||
#! The type with the type parameters of the declaration replaced by the type
|
||||
#! arguments of `kind`, a named type of it.
|
||||
#! @param this The checker.
|
||||
#! @param inner The type written within the declaration, as a field's.
|
||||
#! @param decl The generic declaration.
|
||||
#! @param kind The named type of it, whose arguments replace its parameters.
|
||||
#! @returns The type with them replaced.
|
||||
pub def checker::instantiated(&mut this, inner: u32, decl: u32, kind: u32): u32 {
|
||||
val params: u32 = this.decls.params.p[decl];
|
||||
return inner if params == 0 || !this.types.is_open(inner);
|
||||
|
||||
val from := this.decls.list_of(params);
|
||||
val args := this.types.parts_of(kind);
|
||||
return this.substitute(inner, from.span, args.span);
|
||||
}
|
||||
|
||||
#! Whether the type is an alias.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::is_alias(&this, kind: u32): bool {
|
||||
return this.types.kind_of(kind) == ty::named
|
||||
&& this.decls.kinds.p[this.types.first_of(kind)] == dk::alias_type;
|
||||
}
|
||||
|
||||
#! The type with every alias it is followed to what it stands for, and every
|
||||
#! `match const` choosing by a type it knows to the type it chooses.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns The type at the end of its aliases, or the type itself where it is
|
||||
#! none, or the error type where they do not end.
|
||||
pub def checker::unalias(&mut this, kind: u32): u32 {
|
||||
# Nearly every type given is no alias, which its flags say once asked, so
|
||||
# that is told first, and the rest are followed apart.
|
||||
return kind if (this.types.flags.p[kind] & (tf::asked as u8 | tf::alias as u8)) == tf::asked as u8;
|
||||
val follows := this.follows;
|
||||
return follows(this, kind);
|
||||
}
|
||||
|
||||
# The type at the end of the aliases a type is followed through, as `unalias`
|
||||
# gives it, for a type not yet asked of or that is an alias. Those not ended in
|
||||
# as many steps as `max_aliases` says never end, as a `match const` choosing
|
||||
# by itself does, and are reported.
|
||||
def checker::follow_aliases(&mut this, kind: u32): u32 {
|
||||
var at := kind;
|
||||
var steps := 0u;
|
||||
while this.aliases(at) {
|
||||
if steps == checker::max_aliases {
|
||||
this.report_endless(kind);
|
||||
return bt::error;
|
||||
}
|
||||
at = this.alias_target(at);
|
||||
steps++;
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
#! The most aliases, and types `match const` chooses, a type is followed through.
|
||||
pub const checker::max_aliases: u64 = 32;
|
||||
|
||||
#! Whether the type is an alias, as `is_alias` says, or a type `match const`
|
||||
#! chooses once it knows the type it chooses by, or within a generic's own body
|
||||
#! where what is proved decides it, worked out once and kept in its flags, as
|
||||
#! nearly every type `unalias` is given is neither.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Whether it is.
|
||||
def checker::aliases(&mut this, kind: u32): bool {
|
||||
val flags := this.types.flags.p[kind];
|
||||
return (flags & tf::alias as u8) != 0 if (flags & tf::asked as u8) != 0;
|
||||
# One within a generic's own body is chosen where what is proved of the type
|
||||
# parameter it chooses by decides it, which changes as checking goes, so what
|
||||
# it is is never kept.
|
||||
if this.types.kind_of(kind) == ty::matched && !this.choosable(kind) {
|
||||
return this.proved_choice(kind) != 0;
|
||||
}
|
||||
val alias := this.is_alias(kind) || this.choosable(kind);
|
||||
this.types.flags.p[kind] = flags | tf::asked as u8 | (alias ? tf::alias as u8 : 0);
|
||||
return alias;
|
||||
}
|
||||
|
||||
#! The kind of declaration a named type is of, or `none`.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns The kind of its declaration, or `none` where it is not named.
|
||||
pub def checker::named_kind(&this, kind: u32): dk {
|
||||
return this.types.kind_of(kind) == ty::named
|
||||
? this.decls.kinds.p[this.types.first_of(kind)]
|
||||
: dk::none;
|
||||
}
|
||||
|
||||
#! The integer type an enum is held as.
|
||||
#! @param this The checker.
|
||||
#! @param kind The enum's type.
|
||||
#! @returns The integer type.
|
||||
pub def checker::enum_integer(&mut this, kind: u32): u32 {
|
||||
# An enum's members may name earlier ones while it resolves, and its integer
|
||||
# type is known before them.
|
||||
val decl := this.types.first_of(kind);
|
||||
if this.decls.states.p[decl] != ds::resolving {
|
||||
this.resolve(decl);
|
||||
}
|
||||
val integer: u32 = this.decls.targets.p[decl];
|
||||
return integer == 0 ? bt::int32 : integer;
|
||||
}
|
||||
|
||||
#! What the values of an enum's members are flipped by to be ordered as words
|
||||
#! without a sign: the sign bit where the enum is held signed, and nothing
|
||||
#! otherwise.
|
||||
#! @param this The checker.
|
||||
#! @param kind The enum's type.
|
||||
#! @returns The bits to flip.
|
||||
pub def checker::member_flip(&mut this, kind: u32): u64 {
|
||||
return this.types.is_signed(this.unalias(this.enum_integer(this.unalias(kind)))) ? 0x8000000000000000u : 0;
|
||||
}
|
||||
|
||||
#! The values of an enum's members, each once, from the least, as a range of
|
||||
#! its members counts through them: each a word flipped by `member_flip`, so
|
||||
#! the words order as the values do when compared without a sign.
|
||||
#! @param this The checker.
|
||||
#! @param kind The enum's type.
|
||||
#! @returns The words.
|
||||
pub def checker::member_order(&mut this, kind: u32): vector<u64> {
|
||||
val plain := this.unalias(kind);
|
||||
val flip := this.member_flip(plain);
|
||||
val run: u32 = this.decls.children.p[this.types.first_of(plain)];
|
||||
var values := vector::<u64>::new();
|
||||
for i in 0..this.decls.list_length(run) {
|
||||
val member := this.decls.list_at(run, i as u32);
|
||||
var held := constant {};
|
||||
continue if this.decls.kinds.p[member] != dk::enum_member || !this.decl_value(member, &mut held);
|
||||
values.append(held.bits() ^ flip);
|
||||
}
|
||||
values.sort();
|
||||
|
||||
# A member given an earlier one's value is another name for it.
|
||||
var made := vector::<u64>::new();
|
||||
for each in values {
|
||||
if made.length == 0 || made[made.length - 1] != each {
|
||||
made.append(each);
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The types of the fields of a struct or union type, given its arguments.
|
||||
#! @param this The checker.
|
||||
#! @param kind The struct or union type.
|
||||
#! @returns The type of each field, in order.
|
||||
pub def checker::field_types(&mut this, kind: u32): vector<u32> {
|
||||
var made := vector::<u32>::new();
|
||||
val decl := this.types.first_of(kind);
|
||||
val run: u32 = this.decls.children.p[decl];
|
||||
for i in 0..this.decls.list_length(run) {
|
||||
val field := this.decls.list_at(run, i as u32);
|
||||
continue if this.decls.kinds.p[field] != dk::field;
|
||||
this.resolve(field);
|
||||
made.append(this.instantiated(this.decls.types.p[field], decl, kind));
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! How the value of the node fits where the type is expected.
|
||||
#! @param this The checker.
|
||||
#! @param node The value's node.
|
||||
#! @param to The type expected.
|
||||
#! @returns How it fits.
|
||||
pub def checker::fits_node(&mut this, node: u32, to: u32): fit {
|
||||
val value := this.value_at(node);
|
||||
val from := this.type_at(node);
|
||||
val how := this.fits(from, to, &value, this.is_known(node), 0);
|
||||
return how if how != fit::none || this.choices == 0;
|
||||
return this.fits_unchosen(from, to, &value, this.is_known(node));
|
||||
}
|
||||
|
||||
#! How a value of the type fits where `to` is expected, where `value` is its
|
||||
#! value when `known`.
|
||||
#! @param this The checker.
|
||||
#! @param from The value's type.
|
||||
#! @param to The type expected.
|
||||
#! @param value The value, where it is known while compiling.
|
||||
#! @param known Whether it is.
|
||||
#! @param depth How deeply the search has gone, which stops a type nesting
|
||||
#! without end.
|
||||
#! @returns How it fits.
|
||||
pub def checker::fits(&mut this, from: u32, to: u32, value: &constant, known: bool, depth: u32): fit {
|
||||
return fit::exact if from == to || from == bt::error || to == bt::error;
|
||||
return fit::none if depth > 8;
|
||||
# Within a template, a pack's types fit each other, as its values did before
|
||||
# they were reached at an index: each instance checks the types it gives.
|
||||
val pack := this.pack_of_param(from);
|
||||
return fit::exact if pack != 0 && pack == this.pack_of_param(to);
|
||||
|
||||
val target := this.unalias(to);
|
||||
val source := this.unalias(from);
|
||||
# A type parameter known to have a shape, as `T: array` or `T is vector` says,
|
||||
# is one with that shape, and converts as it does.
|
||||
if this.types.kind_of(source) == ty::param || this.types.kind_of(target) == ty::param {
|
||||
val from_shape := this.param_shape(source);
|
||||
return fit::exact if from_shape != 0 && this.unalias(from_shape) == target;
|
||||
if from_shape != 0 && this.types.kind_of(this.unalias(from_shape)) != ty::param {
|
||||
val how := this.fits(from_shape, to, value, known, depth + 1);
|
||||
return how if how != fit::none;
|
||||
}
|
||||
val to_shape := this.param_shape(target);
|
||||
return fit::exact if to_shape != 0 && this.unalias(to_shape) == source;
|
||||
}
|
||||
val goal := this.types.kind_of(target);
|
||||
match this.types.kind_of(source) {
|
||||
ty::untyped_int, ty::untyped_char => {
|
||||
if goal == ty::signed || goal == ty::unsigned {
|
||||
return known && !this.in_range(target, value) ? fit::overflow : fit::constant;
|
||||
}
|
||||
if goal == ty::untyped_uint {
|
||||
return known && value.negative ? fit::overflow : fit::constant;
|
||||
}
|
||||
return fit::constant if goal == ty::floating || goal == ty::untyped_int
|
||||
|| goal == ty::untyped_float || this.numeric_param(target);
|
||||
return this.unites(source, target, value, known, depth);
|
||||
}
|
||||
ty::untyped_uint => {
|
||||
if goal == ty::unsigned {
|
||||
return known && !this.in_range(target, value) ? fit::overflow : fit::constant;
|
||||
}
|
||||
return fit::constant if this.numeric_param(target);
|
||||
return this.unites(source, target, value, known, depth);
|
||||
}
|
||||
ty::untyped_float, ty::untyped_float32 => {
|
||||
return fit::constant if goal == ty::floating || this.numeric_param(target);
|
||||
return this.unites(source, target, value, known, depth);
|
||||
}
|
||||
ty::untyped_bool => return goal == ty::boolean ? fit::constant : this.unites(source, target, value, known, depth);
|
||||
ty::untyped_text => {
|
||||
return fit::constant if this.is_std(target, word::string) || this.is_text_view(target);
|
||||
return this.unites(source, target, value, known, depth);
|
||||
}
|
||||
ty::null_type => {
|
||||
return fit::constant if goal == ty::pointer || this.is_std(target, word::cfn);
|
||||
return this.unites(source, target, value, known, depth);
|
||||
}
|
||||
ty::empty => return fit::constant;
|
||||
else => {}
|
||||
}
|
||||
|
||||
# What only differs by aliases converts, anywhere within the types.
|
||||
return fit::convert if source == target || this.strip(source, 0) == this.strip(target, 0);
|
||||
|
||||
val skind := this.types.kind_of(source);
|
||||
if this.types.is_numeric(source) && this.types.is_numeric(target) {
|
||||
val sbits := this.types.first_of(source);
|
||||
val tbits := this.types.first_of(target);
|
||||
return fit::convert if skind == goal && tbits > sbits;
|
||||
return fit::convert if skind == ty::unsigned && goal == ty::signed && tbits > sbits;
|
||||
return fit::none;
|
||||
}
|
||||
|
||||
# An enum is its integer type wherever one is expected.
|
||||
if this.named_kind(source) == dk::enum_type && this.types.is_integer(target) {
|
||||
val integer := this.enum_integer(source);
|
||||
return integer == target || this.fits(integer, target, value, false, depth + 1) == fit::convert
|
||||
? fit::convert
|
||||
: fit::none;
|
||||
}
|
||||
|
||||
if (skind == ty::reference || skind == ty::pointer) && skind == goal {
|
||||
val inner_from := this.types.first_of(source);
|
||||
val inner_to := this.types.first_of(target);
|
||||
val writes := this.types.second_of(source) != 0;
|
||||
val wants := this.types.second_of(target) != 0;
|
||||
if !wants || writes {
|
||||
return fit::convert if this.unalias(inner_from) == this.unalias(inner_to);
|
||||
|
||||
# A reference to a constant, as `&200`, refers to a copy of it of the type
|
||||
# referred to.
|
||||
if this.types.is_untyped(inner_from) {
|
||||
val how := this.fits(inner_from, inner_to, value, false, depth + 1);
|
||||
return how == fit::none || how == fit::overflow ? how : fit::constant;
|
||||
}
|
||||
|
||||
# A reference to a value converts to a reference to a trait it satisfies.
|
||||
if skind == ty::reference && this.named_kind(inner_to) == dk::trait_type
|
||||
&& this.satisfies(inner_from, inner_to) {
|
||||
return fit::convert;
|
||||
}
|
||||
}
|
||||
|
||||
# A reference to a reference is otherwise read through to the one it holds,
|
||||
# where that converts, as a generic given a reference for `T` reads a `T`
|
||||
# through a `&mut T`.
|
||||
if skind == ty::reference && this.types.kind_of(this.unalias(inner_from)) == ty::reference {
|
||||
val read := this.fits(inner_from, target, value, false, depth + 1);
|
||||
return read == fit::none || read == fit::overflow ? read : fit::convert;
|
||||
}
|
||||
return fit::none;
|
||||
}
|
||||
|
||||
# A union holds a reference as it holds anything else, before it is read.
|
||||
if this.named_kind(target) == dk::union_type {
|
||||
val united := this.unites(source, target, value, known, depth);
|
||||
return united if united != fit::none;
|
||||
}
|
||||
|
||||
# A plain reference is read through where its value is expected.
|
||||
if skind == ty::reference && goal != ty::reference {
|
||||
val read := this.fits(this.types.first_of(source), target, value, false, depth + 1);
|
||||
return read == fit::none || read == fit::overflow ? read : fit::convert;
|
||||
}
|
||||
|
||||
# A view lends a read-only one, and a string lends a view of its text.
|
||||
if this.is_std(target, word::span) {
|
||||
if this.is_std(source, word::slice) && this.types.part_at(source, 0) == this.types.part_at(target, 0) {
|
||||
return fit::convert;
|
||||
}
|
||||
if this.is_std(source, word::string) && this.is_text_view(target) {
|
||||
return fit::convert;
|
||||
}
|
||||
}
|
||||
|
||||
# Bytes become a string by being copied into memory the string owns, so it
|
||||
# outlives whatever they were read from.
|
||||
if this.is_std(target, word::string) && this.is_byte_view(source) {
|
||||
return fit::convert;
|
||||
}
|
||||
|
||||
if skind == ty::function && goal == ty::function {
|
||||
return this.same_function(source, target) ? fit::convert : fit::none;
|
||||
}
|
||||
|
||||
return this.unites(source, target, value, known, depth);
|
||||
}
|
||||
|
||||
#! Whether the type is a view of bytes, read-only or writable.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::is_byte_view(&mut this, kind: u32): bool {
|
||||
val plain := this.unalias(kind);
|
||||
return (this.is_std(plain, word::span) || this.is_std(plain, word::slice))
|
||||
&& this.unalias(this.types.part_at(plain, 0)) == bt::uint8;
|
||||
}
|
||||
|
||||
#! The type with every alias within it followed to what it stands for, so
|
||||
#! types differing only by aliases are the same once stripped.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param depth How deeply the search has gone, which stops a type nesting
|
||||
#! without end.
|
||||
#! @returns The type stripped.
|
||||
pub def checker::strip(&mut this, kind: u32, depth: u32): u32 {
|
||||
return kind if depth > 16 || this.types.is_untyped(kind);
|
||||
val plain := this.unalias(kind);
|
||||
match this.types.kind_of(plain) {
|
||||
ty::reference, ty::pointer, ty::fixed => {
|
||||
val inner := this.strip(this.types.first_of(plain), depth + 1);
|
||||
if this.types.part_count(plain) == 0 {
|
||||
return this.types.intern(this.types.kind_of(plain), inner, this.types.second_of(plain), span::<u32>::blank());
|
||||
}
|
||||
val length := this.types.parts_of(plain);
|
||||
return this.types.intern(this.types.kind_of(plain), inner, this.types.second_of(plain), length.span);
|
||||
}
|
||||
ty::named, ty::function => {
|
||||
return plain if this.types.part_count(plain) == 0 && this.types.kind_of(plain) == ty::named;
|
||||
var parts := vector::<u32>::new();
|
||||
for i in 0..this.types.part_count(plain) {
|
||||
parts.append(this.strip(this.types.part_at(plain, i as u32), depth + 1));
|
||||
}
|
||||
val first := this.types.kind_of(plain) == ty::function
|
||||
? this.strip(this.types.first_of(plain), depth + 1)
|
||||
: this.types.first_of(plain);
|
||||
return this.types.intern(this.types.kind_of(plain), first, this.types.second_of(plain), parts.span);
|
||||
}
|
||||
else => return plain;
|
||||
}
|
||||
return plain;
|
||||
}
|
||||
|
||||
#! Whether the type is a view of bytes, which text read in place is: a
|
||||
#! `span<u8>`, or a `strview`.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::is_text_view(&mut this, kind: u32): bool {
|
||||
val target := this.unalias(kind);
|
||||
return this.is_std(target, word::span) && this.unalias(this.types.part_at(target, 0)) == bt::uint8;
|
||||
}
|
||||
|
||||
#! Whether the type is a type parameter that numbers may stand in for.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Whether it is one.
|
||||
def checker::numeric_param(&mut this, kind: u32): bool {
|
||||
return this.types.kind_of(kind) == ty::param && this.param_has_numeric(this.types.first_of(kind));
|
||||
}
|
||||
|
||||
#! Whether function types only differ by aliases.
|
||||
#! @param this The checker.
|
||||
#! @param a One function type.
|
||||
#! @param b The other.
|
||||
#! @returns Whether they do.
|
||||
def checker::same_function(&mut this, a: u32, b: u32): bool {
|
||||
return false if this.types.part_count(a) != this.types.part_count(b);
|
||||
return false if this.strip(this.types.first_of(a), 0) != this.strip(this.types.first_of(b), 0);
|
||||
for i in 0..this.types.part_count(a) {
|
||||
# Aliases are looked through wherever they are, so `&strview` is `&span<u8>`,
|
||||
# and a variadic parameter is the view it takes, so `def(i64...)` is
|
||||
# `def(span<i64>)`.
|
||||
val x := this.strip(this.param_taken(a, i as u32), 0);
|
||||
val y := this.strip(this.param_taken(b, i as u32), 0);
|
||||
return false if x != y;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The type a function takes a parameter as: its own, or of a variadic one, a
|
||||
#! view of what it gathers.
|
||||
#! @param this The checker.
|
||||
#! @param kind The function type.
|
||||
#! @param index The parameter's position.
|
||||
#! @returns The type.
|
||||
def checker::param_taken(&mut this, kind: u32, index: u32): u32 {
|
||||
val part := this.types.part_at(kind, index);
|
||||
return part if this.types.second_of(kind) == 0 || index + 1 != this.types.part_count(kind);
|
||||
return this.span_of(part);
|
||||
}
|
||||
|
||||
#! How a value fits a union: as the field of its own type, or else the one
|
||||
#! field it converts to.
|
||||
#! @param this The checker.
|
||||
#! @param from The value's type.
|
||||
#! @param to The union's type.
|
||||
#! @param value The value, where it is known while compiling.
|
||||
#! @param known Whether it is.
|
||||
#! @param depth How deeply the search has gone.
|
||||
#! @returns How it fits: `unite`, or `none`.
|
||||
def checker::unites(&mut this, from: u32, to: u32, value: &constant, known: bool, depth: u32): fit {
|
||||
# A type a generic's own body has not chosen yet, which every other way of
|
||||
# fitting leaves to this, fits where each way it can be chosen does, and is
|
||||
# fitted by what fits each.
|
||||
if this.choices != 0 && (this.types.kind_of(from) == ty::matched || this.types.kind_of(to) == ty::matched) && from != to {
|
||||
return this.fits_ways(from, to, value, known, depth);
|
||||
}
|
||||
return fit::none if this.named_kind(to) != dk::union_type;
|
||||
|
||||
# A constant goes to the field of the type it would be where nothing else is
|
||||
# expected, before any other it fits.
|
||||
val fields := this.field_types(to);
|
||||
val settled := this.types.is_untyped(from) ? this.settle(from) : from;
|
||||
var exact := 0;
|
||||
var fitting := 0;
|
||||
for &each in fields {
|
||||
if each == from || each == settled {
|
||||
exact++;
|
||||
continue;
|
||||
}
|
||||
val how := this.fits(from, each, value, known, depth + 1);
|
||||
if how != fit::none && how != fit::overflow && how != fit::unite {
|
||||
fitting++;
|
||||
}
|
||||
}
|
||||
return fit::unite if exact == 1 || (exact == 0 && fitting == 1);
|
||||
return fit::none;
|
||||
}
|
||||
|
||||
#! The field of a union a value of the type becomes, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param from The value's type.
|
||||
#! @param to The union's type.
|
||||
#! @returns The field's declaration, or 0.
|
||||
pub def checker::united_field(&mut this, from: u32, to: u32): u32 {
|
||||
val decl := this.types.first_of(to);
|
||||
val run: u32 = this.decls.children.p[decl];
|
||||
val settled := this.types.is_untyped(from) ? this.settle(from) : from;
|
||||
var found: u32 = 0;
|
||||
var none_value := constant {};
|
||||
for i in 0..this.decls.list_length(run) {
|
||||
val field := this.decls.list_at(run, i as u32);
|
||||
continue if this.decls.kinds.p[field] != dk::field;
|
||||
this.resolve(field);
|
||||
val kind := this.instantiated(this.decls.types.p[field], decl, to);
|
||||
return field if kind == from || kind == settled;
|
||||
val how := this.fits(from, kind, &none_value, false, 1);
|
||||
if how != fit::none && how != fit::overflow && how != fit::unite && found == 0 {
|
||||
found = field;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
#! Converts the node's value to the type, reporting where it does not fit.
|
||||
#! @param this The checker.
|
||||
#! @param node The value's node.
|
||||
#! @param to The type expected.
|
||||
#! @returns Whether it fits.
|
||||
pub def checker::convert(&mut this, node: u32, to: u32): bool {
|
||||
return true if to == bt::error;
|
||||
|
||||
val from := this.type_at(node);
|
||||
val how := this.fits_node(node, to);
|
||||
match how {
|
||||
fit::exact => return true;
|
||||
fit::none => {
|
||||
# A reference written as one is a pointer at what it refers to where one
|
||||
# is expected, which keeps nothing alive.
|
||||
if this.lends_pointer(node, from, to) {
|
||||
this.refuse_lent_temporary(node);
|
||||
this.set_convert(node, to);
|
||||
return true;
|
||||
}
|
||||
# A function named where another function type is expected is called as
|
||||
# that type asks, by a function made for it.
|
||||
if this.adapts(node, from, to) {
|
||||
this.set_convert(node, to);
|
||||
this.set_flag(node, rf::adapted);
|
||||
return true;
|
||||
}
|
||||
this.report_mismatch(node, from, to);
|
||||
return false;
|
||||
}
|
||||
fit::overflow => {
|
||||
var text := string::from("The constant is outside the range of `", nothing {});
|
||||
val named := this.describe(to);
|
||||
text.append(named.span);
|
||||
text.append("`.");
|
||||
this.report_text(node, ec::overflow, &text);
|
||||
return false;
|
||||
}
|
||||
else => {
|
||||
this.set_convert(node, to);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Whether a function named as a value, of the type `from`, is called as the
|
||||
#! function type `to` asks, as a function of `to`'s type made for it calls it:
|
||||
#! each of `to`'s parameters given as a call gives an argument, those after them
|
||||
#! given their defaults, and what it gives converted to what `to` gives. A
|
||||
#! function type matching it exactly takes it as it is.
|
||||
#! @param this The checker.
|
||||
#! @param node The node naming it.
|
||||
#! @param from Its type.
|
||||
#! @param to The function type expected.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::adapts(&mut this, node: u32, from: u32, to: u32): bool {
|
||||
return false if this.kind_of(node) != nk::name && this.kind_of(node) != nk::path;
|
||||
val source := this.unalias(from);
|
||||
val target := this.unalias(to);
|
||||
return false if this.types.kind_of(source) != ty::function || this.types.kind_of(target) != ty::function;
|
||||
return false if this.types.second_of(source) != 0 || this.types.second_of(target) != 0;
|
||||
return false if source == target || this.strip(source, 0) == this.strip(target, 0);
|
||||
|
||||
val ref := this.ref_at(node);
|
||||
return false if ref == 0 || (ref & (ref_local | ref_type)) != 0;
|
||||
return this.adapt_cost(ref, source, target) >= 0;
|
||||
}
|
||||
|
||||
#! How far a function of the type `from` is from being called as the function
|
||||
#! type `to` asks, as `adapts` says it is: 0 for each parameter given as it is,
|
||||
#! 1 for one lent or read through, and 2 for one converted, or -1 where it is not
|
||||
#! called so. A call chooses between overloads by it.
|
||||
#! @param this The checker.
|
||||
#! @param decl The function's declaration, whose defaults it is given.
|
||||
#! @param from Its type.
|
||||
#! @param to The function type expected.
|
||||
#! @returns How far, or -1.
|
||||
pub def checker::adapt_cost(&mut this, decl: u32, from: u32, to: u32): i32 {
|
||||
val source := this.unalias(from);
|
||||
val target := this.unalias(to);
|
||||
return -1 if this.types.kind_of(source) != ty::function || this.types.kind_of(target) != ty::function;
|
||||
return -1 if this.types.second_of(source) != 0 || this.types.second_of(target) != 0;
|
||||
|
||||
# Only a function of the program is called so, with the defaults it declares.
|
||||
val kind_of: dk = this.decls.kinds.p[decl];
|
||||
return -1 if kind_of != dk::function && kind_of != dk::property;
|
||||
return -1 if kind_of == dk::function && (this.decls.has(decl, df::foreign) || this.decls.has(decl, df::intrinsic)
|
||||
|| this.decls.has(decl, df::variadic));
|
||||
|
||||
val given := this.types.part_count(target);
|
||||
val takes := this.types.part_count(source);
|
||||
val required: u32 = kind_of == dk::function ? this.required_params(decl) : takes;
|
||||
return -1 if given > takes || given < required;
|
||||
var cost: i32 = 0;
|
||||
for i in 0..given {
|
||||
val wanted := this.types.part_at(target, i as u32);
|
||||
val param := this.types.part_at(source, i as u32);
|
||||
return -1 if !this.passes(wanted, param);
|
||||
if this.unalias(wanted) != this.unalias(param) {
|
||||
cost += this.unalias(this.reached(wanted)) == this.unalias(this.reached(param)) ? 1 : 2;
|
||||
}
|
||||
}
|
||||
|
||||
# What a call around it still infers from what it gives, as `U` of `map`,
|
||||
# takes whatever it gives.
|
||||
return cost if this.free_result;
|
||||
val none := constant {};
|
||||
val gives := this.types.first_of(source);
|
||||
val wants := this.types.first_of(target);
|
||||
if this.types.kind_of(this.unalias(gives)) == ty::reference && this.types.kind_of(this.unalias(wants)) != ty::reference
|
||||
&& this.closes(this.reached(gives)) {
|
||||
return -1;
|
||||
}
|
||||
val how := this.fits(gives, wants, &none, false, 0);
|
||||
return -1 if how == fit::none || how == fit::overflow;
|
||||
return cost + (how == fit::exact ? 0 : 1);
|
||||
}
|
||||
|
||||
#! Whether a value of the type `given` is given for a parameter of the type
|
||||
#! `param` as a call gives it: a value lent where the parameter is a reference,
|
||||
#! a reference read through to one of the type asked, and what a reference
|
||||
#! reaches copied where the parameter takes a value, where that closes nothing.
|
||||
#! @param this The checker.
|
||||
#! @param given The type given.
|
||||
#! @param param The parameter's type.
|
||||
#! @returns Whether it is.
|
||||
def checker::passes(&mut this, given: u32, param: u32): bool {
|
||||
val none := constant {};
|
||||
val wanted := this.unalias(param);
|
||||
val held := this.unalias(given);
|
||||
if this.types.kind_of(wanted) == ty::reference && this.types.kind_of(held) != ty::reference {
|
||||
val how := this.fits(held, this.types.first_of(wanted), &none, false, 0);
|
||||
return how == fit::exact || how == fit::convert;
|
||||
}
|
||||
if this.types.kind_of(wanted) != ty::reference && this.types.kind_of(held) == ty::reference
|
||||
&& this.closes(this.reached(held)) {
|
||||
return false;
|
||||
}
|
||||
val how := this.fits(held, wanted, &none, false, 0);
|
||||
return how == fit::exact || how == fit::convert;
|
||||
}
|
||||
|
||||
#! Whether a value is a reference written as one, `&x` or `&mut x`, given where
|
||||
#! a pointer at what it refers to is expected, which it becomes: `&T` a `*T`, and
|
||||
#! `&mut T` a `*mut T` or a `*T`. Nothing else becomes a pointer without `as`,
|
||||
#! so taking an address is always written.
|
||||
#! @param this The checker.
|
||||
#! @param node The value's node.
|
||||
#! @param from Its type.
|
||||
#! @param to The type expected.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::lends_pointer(&mut this, node: u32, from: u32, to: u32): bool {
|
||||
return false if !this.lent(node);
|
||||
val source := this.unalias(from);
|
||||
val target := this.unalias(to);
|
||||
return false if this.types.kind_of(source) != ty::reference || this.types.kind_of(target) != ty::pointer;
|
||||
return false if this.types.second_of(target) != 0 && this.types.second_of(source) == 0;
|
||||
val inner_from := this.types.first_of(source);
|
||||
val inner_to := this.types.first_of(target);
|
||||
# A reference to a constant, as `&10`, refers to a copy of it of the type
|
||||
# pointed at.
|
||||
if this.types.is_untyped(inner_from) {
|
||||
val none := constant {};
|
||||
val how := this.fits(inner_from, inner_to, &none, false, 0);
|
||||
return how != fit::none && how != fit::overflow;
|
||||
}
|
||||
return this.unalias(inner_from) == this.unalias(inner_to);
|
||||
}
|
||||
|
||||
#! Whether a reference written as one refers to a value that is no place, as
|
||||
#! `&10` or `&make()`, which lives to the end of its statement, or as long as a
|
||||
#! name a reference binds.
|
||||
#! @param this The checker.
|
||||
#! @param node The value's node.
|
||||
#! @returns Whether it does.
|
||||
pub def checker::lends_temporary(&mut this, node: u32): bool {
|
||||
if this.kind_of(node) == nk::ternary {
|
||||
val chosen := this.chosen_arm(node);
|
||||
return this.lends_temporary(chosen) if chosen != 0;
|
||||
return this.lends_temporary(this.record(node, if_slot::then)) || this.lends_temporary(this.record(node, if_slot::other));
|
||||
}
|
||||
return false if this.kind_of(node) != nk::reference;
|
||||
val operand := this.left_of(node);
|
||||
return !this.is_place(operand) && this.types.kind_of(this.unalias(this.type_at(operand))) != ty::reference;
|
||||
}
|
||||
|
||||
#! Reports a pointer kept past its statement at a value that ends with it: a
|
||||
#! pointer keeps nothing alive, as a reference bound to a name does.
|
||||
#! @param this The checker.
|
||||
#! @param node The value's node: a reference written as one, or one cast to a
|
||||
#! pointer.
|
||||
pub def checker::refuse_lent_temporary(&mut this, node: u32): void {
|
||||
val lent := this.kind_of(node) == nk::cast ? this.left_of(node) : node;
|
||||
return if !this.lends_temporary(lent);
|
||||
this.report(node, ec::invalid,
|
||||
"A pointer keeps nothing alive, and this one points at a value that ends with its statement: keep the value in a variable first, or refer to it, as a reference bound to a name keeps it.");
|
||||
}
|
||||
|
||||
#! Reports a value of one type where another is expected.
|
||||
#! @param this The checker.
|
||||
#! @param node The value's node.
|
||||
#! @param from Its type.
|
||||
#! @param to The type expected.
|
||||
pub def checker::report_mismatch(&mut this, node: u32, from: u32, to: u32): void {
|
||||
var text := string::from("Expected `", nothing {});
|
||||
val want := this.describe(to);
|
||||
val given := this.describe(from);
|
||||
text.append(want.span);
|
||||
text.append("`, but the value is `");
|
||||
text.append(given.span);
|
||||
text.append("`.");
|
||||
this.report_text(node, ec::mismatch, &text);
|
||||
}
|
||||
|
||||
#! The type a value of the type becomes where nothing is expected of it: a
|
||||
#! number constant its default type, and text a `strview`, or a `string` where
|
||||
#! std declares no `strview`.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns The type it settles as.
|
||||
pub def checker::settle(&mut this, kind: u32): u32 {
|
||||
if this.types.kind_of(kind) == ty::reference && this.types.is_untyped(this.types.first_of(kind)) {
|
||||
return this.types.reference(this.settle(this.types.first_of(kind)), this.types.second_of(kind) != 0);
|
||||
}
|
||||
# A literal is a view of the bytes the program was written with, or a `string`
|
||||
# where std declares no `strview`.
|
||||
if this.types.kind_of(kind) == ty::untyped_text {
|
||||
val view := this.std_type(word::strview, span::<u32>::blank());
|
||||
return view != bt::error ? view : this.std_type(word::string, span::<u32>::blank());
|
||||
}
|
||||
if this.types.kind_of(kind) == ty::null_type || this.types.kind_of(kind) == ty::empty {
|
||||
return kind;
|
||||
}
|
||||
return this.types.settled(kind);
|
||||
}
|
||||
|
||||
#! The type two values share, as the operands of an operator or the branches
|
||||
#! of a ternary, or the error type where they share none.
|
||||
#! @param this The checker.
|
||||
#! @param a One value's type.
|
||||
#! @param b The other's.
|
||||
#! @returns The type they share, or the error type.
|
||||
pub def checker::common(&mut this, a: u32, b: u32): u32 {
|
||||
return a if a == b;
|
||||
return b if a == bt::error;
|
||||
return a if b == bt::error;
|
||||
|
||||
val none := constant {};
|
||||
val a_untyped := this.types.is_untyped(a);
|
||||
val b_untyped := this.types.is_untyped(b);
|
||||
if a_untyped && b_untyped {
|
||||
val a_float := this.types.is_float(a);
|
||||
val b_float := this.types.is_float(b);
|
||||
return a if a_float && !b_float;
|
||||
return b if b_float && !a_float;
|
||||
return a if this.types.kind_of(a) == ty::untyped_uint;
|
||||
return b if this.types.kind_of(b) == ty::untyped_uint;
|
||||
return a if this.types.is_integer(a) && this.types.is_integer(b);
|
||||
return bt::error;
|
||||
}
|
||||
|
||||
if a_untyped {
|
||||
val how := this.fits(a, b, &none, false, 0);
|
||||
return how == fit::none ? bt::error : b;
|
||||
}
|
||||
if b_untyped {
|
||||
val how := this.fits(b, a, &none, false, 0);
|
||||
return how == fit::none ? bt::error : a;
|
||||
}
|
||||
|
||||
val forward := this.fits(a, b, &none, false, 0);
|
||||
val backward := this.fits(b, a, &none, false, 0);
|
||||
return b if forward == fit::convert && backward == fit::none;
|
||||
return a if backward == fit::convert && forward == fit::none;
|
||||
return a if forward != fit::none && backward != fit::none;
|
||||
return bt::error;
|
||||
}
|
||||
|
||||
#! The pack a type parameter stands for, or for one of whose types it stands,
|
||||
#! as `Ts` or `Ts...[0]`.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns The pack parameter's declaration, or 0 where the type is neither.
|
||||
def checker::pack_of_param(&mut this, kind: u32): u32 {
|
||||
return 0 if this.types.kind_of(kind) != ty::param || this.element_ins.length == 0;
|
||||
val decl := this.types.first_of(kind);
|
||||
for &each in this.element_ins {
|
||||
return this.types.first_of(each.pack) if each.param == decl;
|
||||
return decl if this.types.first_of(each.pack) == decl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,653 @@
|
||||
#! @file declare.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the declaring of what each file declares: its modules, its
|
||||
#! imports, and every declaration outside a function, with the members of
|
||||
#! types and the type parameters of generics. A declaration made through a
|
||||
#! type, as `vec2::length`, waits until every file is declared, since its type
|
||||
#! may be declared in any of them.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! Declares what the file declares.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
pub def checker::declare_unit(&mut this, file: u32): void {
|
||||
this.use_table(this.units[file].table);
|
||||
this.at.home = 0;
|
||||
this.at.scope = 0;
|
||||
|
||||
val decls := this.left_of(this.units[file].root);
|
||||
var active := true;
|
||||
var begun := false;
|
||||
var sections := false;
|
||||
var first_library: u32 = 0;
|
||||
this.external = false;
|
||||
for &node in this.run_view(decls) {
|
||||
match this.kind_of(node) {
|
||||
nk::import_decl, nk::extern_import => {
|
||||
if begun {
|
||||
this.report(node, ec::invalid, "Imports come before every other declaration.");
|
||||
}
|
||||
if this.kind_of(node) == nk::import_decl {
|
||||
this.pending_imports.append(file_node { file = file, node = node });
|
||||
} else if this.right_of(node) == 0 || this.target_holds(this.right_of(node)) {
|
||||
first_library = first_library == 0 ? node : first_library;
|
||||
this.add_library(file, node);
|
||||
}
|
||||
}
|
||||
nk::module_decl, nk::extern_module => {
|
||||
begun = true;
|
||||
this.at.home = this.module_path(this.left_of(node), true);
|
||||
this.modules.named[this.at.home] = true;
|
||||
active = this.right_of(node) == 0 || this.target_holds(this.right_of(node));
|
||||
this.external = this.kind_of(node) == nk::extern_module;
|
||||
if this.external {
|
||||
sections = true;
|
||||
this.check_section(node);
|
||||
}
|
||||
}
|
||||
else => {
|
||||
begun = true;
|
||||
if active {
|
||||
this.declare_top(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
this.external = false;
|
||||
|
||||
if first_library != 0 && !sections {
|
||||
this.warn(first_library, ec::invalid,
|
||||
"Nothing in this file is C's, so nothing uses the libraries it imports: C's functions are declared in an `extern module`.");
|
||||
}
|
||||
}
|
||||
|
||||
#! Checks that an `extern module` says which systems it is for, since a library
|
||||
#! is one system's and C's calling convention is the system's too.
|
||||
#! @param this The checker.
|
||||
#! @param node The `extern_module` node.
|
||||
def checker::check_section(&mut this, node: u32): void {
|
||||
val condition := this.right_of(node);
|
||||
if condition == 0 || !this.names_system(condition) {
|
||||
this.report(node, ec::invalid, "An `extern module` says which systems it's for: `when os == ...`, `arch == ...`, or both.");
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether a condition on the target names `os` or `arch` anywhere.
|
||||
#! @param this The checker.
|
||||
#! @param node The condition's node.
|
||||
#! @returns Whether it does.
|
||||
def checker::names_system(&this, node: u32): bool {
|
||||
return match this.kind_of(node) {
|
||||
nk::unary => this.names_system(this.left_of(node)),
|
||||
nk::binary => this.token_kind(node) == tk::logand || this.token_kind(node) == tk::logor
|
||||
? this.names_system(this.left_of(node)) || this.names_system(this.right_of(node))
|
||||
: this.name_of(this.left_of(node)) == word::os || this.name_of(this.left_of(node)) == word::arch,
|
||||
else => false
|
||||
};
|
||||
}
|
||||
|
||||
#! Notes a library a file's C functions come from. On Windows each function is
|
||||
#! listed under its library, so a file names one there.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @param node The `extern_import` node.
|
||||
def checker::add_library(&mut this, file: u32, node: u32): void {
|
||||
if this.names.text(this.target.os) == "windows" {
|
||||
for i in 0..this.libraries.length {
|
||||
if this.libraries[i].file == file {
|
||||
this.report(node, ec::invalid,
|
||||
"A file names one library on Windows, which lists each C function under its library: declare the functions of another in a file of their own.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.libraries.append(file_node { file = file, node = node });
|
||||
}
|
||||
|
||||
#! Marks what an `extern module` declares as C's: a function without a body, a
|
||||
#! struct, or a union. Whatever else it declares is ordinary Sakura.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @param kind Its kind.
|
||||
#! @param node Its node.
|
||||
def checker::mark_external(&mut this, id: u32, kind: dk, node: u32): void {
|
||||
if kind == dk::struct_type || kind == dk::union_type
|
||||
|| kind == dk::function && this.record(node, function_slot::body) == 0 {
|
||||
this.decls.mark(id, df::foreign);
|
||||
}
|
||||
}
|
||||
|
||||
#! The module a path names, made where `make` says and it is new, or 0 where
|
||||
#! it names none.
|
||||
#! @param this The checker.
|
||||
#! @param path The `path` node.
|
||||
#! @param make Whether a module it names is made where it is new.
|
||||
#! @returns The module, or 0.
|
||||
pub def checker::module_path(&mut this, path: u32, make: bool): u32 {
|
||||
var at: u32 = 0;
|
||||
for i in 0..this.segment_count(path) {
|
||||
val name := this.segment_name(path, i as u32);
|
||||
if make {
|
||||
at = this.modules.child(at, name);
|
||||
continue;
|
||||
}
|
||||
|
||||
at = this.modules.find(at, name);
|
||||
return 0 if at == 0;
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
#! Declares a declaration at the top level, or those of the branch of a `when`
|
||||
#! the target takes.
|
||||
#! @param this The checker.
|
||||
#! @param node The top-level node.
|
||||
def checker::declare_top(&mut this, node: u32): void {
|
||||
match this.kind_of(node) {
|
||||
nk::when_block => {
|
||||
val chosen := this.when_branch(node);
|
||||
return if chosen == 0;
|
||||
|
||||
val decls := this.left_of(chosen);
|
||||
for &node in this.run_view(decls) {
|
||||
this.declare_top(node);
|
||||
}
|
||||
}
|
||||
nk::error => {}
|
||||
else => {
|
||||
this.decls.top = node;
|
||||
this.declare_decl(node, 0);
|
||||
this.decls.top = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! The block of a `when` the target takes, following `else when`, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param node The `when_block` node.
|
||||
#! @returns The block, or 0.
|
||||
pub def checker::when_branch(&mut this, node: u32): u32 {
|
||||
var at := node;
|
||||
while at != 0 && this.kind_of(at) == nk::when_block {
|
||||
return this.record(at, if_slot::then) if this.target_holds(this.left_of(at));
|
||||
at = this.record(at, if_slot::other);
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
#! Whether the target meets the condition of a `when`, reporting a key or a
|
||||
#! value it does not know.
|
||||
#! @param this The checker.
|
||||
#! @param node The condition's node.
|
||||
#! @returns Whether it does.
|
||||
pub def checker::target_holds(&mut this, node: u32): bool {
|
||||
match this.kind_of(node) {
|
||||
nk::unary => return !this.target_holds(this.left_of(node));
|
||||
nk::binary => {
|
||||
val operator := this.token_kind(node);
|
||||
if operator == tk::logand {
|
||||
val left := this.target_holds(this.left_of(node));
|
||||
return this.target_holds(this.right_of(node)) && left;
|
||||
}
|
||||
if operator == tk::logor {
|
||||
val left := this.target_holds(this.left_of(node));
|
||||
return this.target_holds(this.right_of(node)) || left;
|
||||
}
|
||||
|
||||
val key := this.name_of(this.left_of(node));
|
||||
val value := this.name_of(this.right_of(node));
|
||||
val held := this.target_value(this.left_of(node), key, this.right_of(node), value);
|
||||
return operator == tk::equals ? held : !held;
|
||||
}
|
||||
else => return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Whether the target's value of the key is the value, reporting a key or a
|
||||
#! value no target has.
|
||||
#! @param this The checker.
|
||||
#! @param key_node The node of the key, which a note is placed at.
|
||||
#! @param key The ID of the key, as `os`.
|
||||
#! @param value_node The node of the value.
|
||||
#! @param value The ID of the value, as `linux`.
|
||||
#! @returns Whether it is.
|
||||
def checker::target_value(&mut this, key_node: u32, key: u32, value_node: u32, value: u32): bool {
|
||||
val text := this.names.text(value);
|
||||
if key == word::os {
|
||||
if text != "linux" && text != "macos" && text != "windows" && text != "freebsd" {
|
||||
this.report(value_node, ec::invalid, "`os` is `linux`, `macos`, `windows`, or `freebsd`.");
|
||||
}
|
||||
return value == this.target.os;
|
||||
}
|
||||
|
||||
if key == word::arch {
|
||||
if text != "x86_64" && text != "aarch64" {
|
||||
this.report(value_node, ec::invalid, "`arch` is `x86_64` or `aarch64`.");
|
||||
}
|
||||
return value == this.target.arch;
|
||||
}
|
||||
|
||||
if key == word::backend {
|
||||
if text != "vm" && text != "native" {
|
||||
this.report(value_node, ec::invalid, "`backend` is `vm` or `native`.");
|
||||
}
|
||||
return value == this.target.backend;
|
||||
}
|
||||
|
||||
this.report(key_node, ec::invalid, "The target keys are `os`, `arch`, and `backend`.");
|
||||
return false;
|
||||
}
|
||||
|
||||
#! The kind of declaration a node declares.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The kind.
|
||||
def checker::decl_kind(&this, node: u32): dk {
|
||||
return match this.kind_of(node) {
|
||||
nk::function => dk::function,
|
||||
nk::variable => dk::variable,
|
||||
nk::field => dk::field,
|
||||
nk::property => dk::property,
|
||||
nk::struct_decl => dk::struct_type,
|
||||
nk::union_decl => dk::union_type,
|
||||
nk::trait_decl => dk::trait_type,
|
||||
nk::enumeration => dk::enum_type,
|
||||
nk::alias => dk::alias_type,
|
||||
nk::assoc_type => dk::assoc_type,
|
||||
nk::enumerator => dk::enum_member,
|
||||
else => dk::none
|
||||
};
|
||||
}
|
||||
|
||||
#! The receiver a declaration is made through, as `vec2` of `vec2::length`, or
|
||||
#! 0.
|
||||
#! @param this The checker.
|
||||
#! @param node The declaration's node.
|
||||
#! @returns The receiver's `type_name`, or 0.
|
||||
def checker::receiver_of(&this, node: u32): u32 {
|
||||
return match this.kind_of(node) {
|
||||
nk::function, nk::variable, nk::property, nk::struct_decl, nk::union_decl,
|
||||
nk::trait_decl, nk::enumeration, nk::alias => this.record(node, decl_slot::receiver),
|
||||
else => 0
|
||||
};
|
||||
}
|
||||
|
||||
#! The run of a declaration's own type parameters, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param node The declaration's node.
|
||||
#! @returns The run, or 0.
|
||||
def checker::own_params_of(&this, node: u32): u32 {
|
||||
return match this.kind_of(node) {
|
||||
nk::function, nk::struct_decl, nk::union_decl, nk::trait_decl, nk::alias => this.record(node, decl_slot::type_params),
|
||||
else => 0
|
||||
};
|
||||
}
|
||||
|
||||
#! The run of the attributes written on a declaration, or on one of a
|
||||
#! function's parameters, in the table of the declaration's file, or 0.
|
||||
#! @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 run, or 0.
|
||||
pub def checker::attributes_at(&this, id: u32, param: u32): u32 {
|
||||
val node: u32 = this.decls.nodes.p[id];
|
||||
return this.attributes_of(node) if param == 0;
|
||||
return 0 if this.kind_of(node) != nk::function || this.decls.has(id, df::accessor);
|
||||
val params := this.record(node, function_slot::params);
|
||||
return param <= this.run_length(params) ? this.record(this.run_at(params, param - 1), parameter_slot::attributes) : 0;
|
||||
}
|
||||
|
||||
#! The run of a declaration's attributes, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param node The declaration's node.
|
||||
#! @returns The run, or 0.
|
||||
pub def checker::attributes_of(&this, node: u32): u32 {
|
||||
return match this.kind_of(node) {
|
||||
nk::function, nk::variable, nk::field, nk::property, nk::struct_decl, nk::union_decl,
|
||||
nk::trait_decl, nk::enumeration, nk::alias, nk::assoc_type => this.record(node, decl_slot::attributes),
|
||||
nk::enumerator => this.left_of(node),
|
||||
else => 0
|
||||
};
|
||||
}
|
||||
|
||||
#! Declares the declaration, as a member of `owner` where it is given, or
|
||||
#! else in the module, or waiting for its receiver.
|
||||
#! @param this The checker.
|
||||
#! @param node The declaration's node.
|
||||
#! @param owner The type it is a member of, or 0.
|
||||
#! @returns Its ID, or 0 where it declares nothing.
|
||||
pub def checker::declare_decl(&mut this, node: u32, owner: u32): u32 {
|
||||
val kind := this.decl_kind(node);
|
||||
return 0 if kind == dk::none;
|
||||
|
||||
val id := this.decls.add(kind, this.name_of(node), this.at.file, node, this.at.home);
|
||||
val flags := this.node_flags(node);
|
||||
if (flags & nf::exported as u32) != 0 { this.decls.mark(id, df::exported); }
|
||||
if (flags & nf::mutable as u32) != 0 { this.decls.mark(id, df::mutable); }
|
||||
if (flags & nf::constant as u32) != 0 { this.decls.mark(id, df::constant); }
|
||||
if kind == dk::assoc_type && (flags & nf::packed as u32) != 0 { this.decls.mark(id, df::packed); }
|
||||
# An alias of a `match const`, or of `C ? A : B`, stands for the type it
|
||||
# chooses, which is told from what it is written as, before it is resolved.
|
||||
if kind == dk::alias_type
|
||||
&& (this.kind_of(this.right_of(node)) == nk::type_match || this.kind_of(this.right_of(node)) == nk::type_cond) {
|
||||
this.decls.mark(id, df::chooses);
|
||||
this.choosing_aliases++;
|
||||
}
|
||||
this.mark_attributes(id, node);
|
||||
if this.external && owner == 0 {
|
||||
this.mark_external(id, kind, node);
|
||||
}
|
||||
|
||||
if kind == dk::function {
|
||||
this.mark_signature(id, node);
|
||||
}
|
||||
|
||||
# A trait's members and the fields of a struct belong to it as they are
|
||||
# declared. Anything else declared through a type waits for its type.
|
||||
if owner != 0 {
|
||||
this.add_member(owner, id);
|
||||
} else if this.receiver_of(node) != 0 {
|
||||
this.pending.append(id);
|
||||
} else {
|
||||
this.add_global(id);
|
||||
}
|
||||
|
||||
val own := this.declare_params(this.own_params_of(node), id);
|
||||
this.record_shape(id, this.own_params_of(node));
|
||||
# A function's type parameters are inferred from its arguments, and an alias is
|
||||
# the type it is written as, so only a struct, union, or trait takes a pattern.
|
||||
if kind == dk::function || kind == dk::alias_type {
|
||||
val run := this.own_params_of(node);
|
||||
for ¶m in this.run_view(run) {
|
||||
val written := this.kind_of(param);
|
||||
if written == nk::type_pattern || written == nk::array_pattern {
|
||||
this.report(param, ec::generic, "Only a struct, union, or trait takes a pattern parameter, as `def(Ts...): R` or `L[N]`.");
|
||||
}
|
||||
}
|
||||
}
|
||||
# The names of generic functions, which an argument naming one waits by.
|
||||
if kind == dk::function && own.length != 0 {
|
||||
val name := this.name_of(node);
|
||||
if this.generic_names.length <= name as u64 {
|
||||
this.generic_names.resize(name as u64 + 1, false);
|
||||
}
|
||||
this.generic_names[name] = true;
|
||||
}
|
||||
if this.receiver_of(node) == 0 {
|
||||
this.set_params(id, own.span);
|
||||
} else {
|
||||
this.decls.params.p[id] = this.decls.make_list(own.span);
|
||||
}
|
||||
|
||||
this.declare_members(id, node);
|
||||
return id;
|
||||
}
|
||||
|
||||
#! Gives the declaration its own type parameters, which make it generic.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @param own Its own type parameters' declarations.
|
||||
def checker::set_params(&mut this, id: u32, own: span<u32>): void {
|
||||
this.decls.params.p[id] = this.decls.make_list(own);
|
||||
if own.length != 0 {
|
||||
this.decls.mark(id, df::generic);
|
||||
}
|
||||
}
|
||||
|
||||
#! Declares the type parameters of the run, each owned by the declaration.
|
||||
#! @param this The checker.
|
||||
#! @param run The run of `type_param` nodes.
|
||||
#! @param owner The declaration they belong to.
|
||||
#! @returns Their declarations.
|
||||
def checker::declare_params(&mut this, run: u32, owner: u32): vector<u32> {
|
||||
var made := vector::<u32>::new();
|
||||
for &node in this.run_view(run) {
|
||||
# A pattern, `def(Ts...): R`, declares the type parameters it names, those of
|
||||
# its parameters and then its result's.
|
||||
if this.kind_of(node) == nk::type_pattern {
|
||||
val named := this.declare_params(this.left_of(node), owner);
|
||||
made.extend(named.span);
|
||||
if this.right_of(node) != 0 {
|
||||
val result := this.decls.add(dk::type_param, this.name_of(this.right_of(node)), this.at.file, this.right_of(node), this.at.home);
|
||||
this.decls.own(result, owner);
|
||||
made.append(result);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
# A pattern, `L[N]`, declares its element's type parameter, and its length's
|
||||
# constant one.
|
||||
if this.kind_of(node) == nk::array_pattern {
|
||||
val element := this.decls.add(dk::type_param, this.name_of(this.left_of(node)), this.at.file, this.left_of(node), this.at.home);
|
||||
this.decls.own(element, owner);
|
||||
made.append(element);
|
||||
if this.right_of(node) != 0 {
|
||||
val length := this.decls.add(dk::type_param, this.name_of(this.right_of(node)), this.at.file, this.right_of(node), this.at.home);
|
||||
this.decls.own(length, owner);
|
||||
this.decls.mark(length, df::valued);
|
||||
made.append(length);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
val id := this.decls.add(dk::type_param, this.name_of(node), this.at.file, node, this.at.home);
|
||||
this.decls.own(id, owner);
|
||||
# A constant parameter stands for a value, whose type it resolves with its bounds.
|
||||
if this.kind_of(node) == nk::const_param {
|
||||
this.decls.mark(id, df::valued);
|
||||
} else if this.node_has(node, nf::packed) {
|
||||
this.decls.mark(id, df::packed);
|
||||
# A function's variadic parameter takes a pack, and a struct, union, or
|
||||
# trait takes one for what it holds or asks for to pass on.
|
||||
val kind: dk = this.decls.kinds.p[owner];
|
||||
if kind != dk::function && kind != dk::struct_type && kind != dk::union_type && kind != dk::trait_type {
|
||||
this.report(node, ec::generic, "Only a function, struct, union, or trait takes a pack.");
|
||||
}
|
||||
}
|
||||
made.append(id);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Declares the fields of a struct or union, the members of a trait, the
|
||||
#! members of an enum, and the accessors of a property.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @param node Its node.
|
||||
def checker::declare_members(&mut this, id: u32, node: u32): void {
|
||||
val kind := this.kind_of(node);
|
||||
if kind != nk::struct_decl && kind != nk::union_decl && kind != nk::trait_decl
|
||||
&& kind != nk::enumeration && kind != nk::property {
|
||||
return;
|
||||
}
|
||||
|
||||
var children := vector::<u32>::new();
|
||||
val members := this.right_of(node);
|
||||
for &member in this.run_view(members) {
|
||||
if kind == nk::property {
|
||||
children.append(this.declare_accessor(id, member));
|
||||
continue;
|
||||
}
|
||||
|
||||
val made := this.declare_decl(member, id);
|
||||
if made != 0 {
|
||||
children.append(made);
|
||||
}
|
||||
}
|
||||
this.decls.children.p[id] = this.decls.make_list(children.span);
|
||||
}
|
||||
|
||||
#! Declares an accessor of a property, a function of the property's name taking
|
||||
#! `this`, which is no member of its own: reading or writing the property
|
||||
#! calls it.
|
||||
#! @param this The checker.
|
||||
#! @param property The property's declaration.
|
||||
#! @param node The `accessor` node.
|
||||
#! @returns The accessor's declaration.
|
||||
def checker::declare_accessor(&mut this, property: u32, node: u32): u32 {
|
||||
val id := this.decls.add(dk::function, this.decls.names.p[property], this.at.file, node, this.at.home);
|
||||
this.decls.own(id, property);
|
||||
this.decls.mark(id, df::accessor);
|
||||
this.decls.mark(id, df::method);
|
||||
if this.name_of(node) == this.names.intern(&"set") {
|
||||
this.decls.mark(id, df::setter);
|
||||
}
|
||||
if this.decls.has(property, df::exported) || this.node_has(node, nf::exported) {
|
||||
this.decls.mark(id, df::exported);
|
||||
}
|
||||
if this.decls.has(property, df::inline) {
|
||||
this.decls.mark(id, df::inline);
|
||||
}
|
||||
|
||||
val params := this.left_of(node);
|
||||
if this.run_length(params) != 0 && this.node_has(this.run_at(params, 0), nf::mutable) {
|
||||
this.decls.mark(id, df::mut_this);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
#! Marks what the attributes the compiler reads say of the declaration.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @param node Its node.
|
||||
def checker::mark_attributes(&mut this, id: u32, node: u32): void {
|
||||
val attrs := this.attributes_of(node);
|
||||
for &attr in this.run_view(attrs) {
|
||||
continue if this.kind_of(attr) != nk::attribute;
|
||||
|
||||
val named := this.left_of(attr);
|
||||
val name := this.segment_name(named, this.segment_count(named) - 1);
|
||||
if name == word::intrinsic {
|
||||
this.decls.mark(id, df::intrinsic);
|
||||
} else if name == word::optional {
|
||||
this.decls.mark(id, df::optional);
|
||||
} else if name == word::nodiscard {
|
||||
this.decls.mark(id, df::nodiscard);
|
||||
} else if name == word::allow {
|
||||
this.decls.mark(id, df::allows);
|
||||
} else if name == word::inline {
|
||||
this.decls.mark(id, df::inline);
|
||||
} else if name == word::sealed && this.decls.kinds.p[id] == dk::trait_type {
|
||||
this.decls.mark(id, df::sealed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Marks what a function's parameters say of it: whether it takes `this`, and
|
||||
#! `&mut this`, and whether its last parameter is variadic.
|
||||
#! @param this The checker.
|
||||
#! @param id The function's declaration.
|
||||
#! @param node Its node.
|
||||
def checker::mark_signature(&mut this, id: u32, node: u32): void {
|
||||
val params := this.record(node, function_slot::params);
|
||||
val count := this.run_length(params);
|
||||
return if count == 0;
|
||||
|
||||
# `&this` takes the value a method is called on, as does `this` written with
|
||||
# a reference to the method's type, `this: &mut T`.
|
||||
val first := this.run_at(params, 0);
|
||||
if this.name_of(first) == names::this_id {
|
||||
val written := this.left_of(first);
|
||||
if written == 0 {
|
||||
this.decls.mark(id, df::method);
|
||||
if this.node_has(first, nf::mutable) {
|
||||
this.decls.mark(id, df::mut_this);
|
||||
}
|
||||
} else if this.kind_of(written) == nk::type_ref {
|
||||
this.decls.mark(id, df::method);
|
||||
if this.node_has(written, nf::mutable) {
|
||||
this.decls.mark(id, df::mut_this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if this.node_has(this.run_at(params, count - 1), nf::variadic) {
|
||||
this.decls.mark(id, df::variadic);
|
||||
}
|
||||
}
|
||||
|
||||
#! Declares the declaration in its module, as an overload of the functions of
|
||||
#! its name there, or reporting a name already taken.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
def checker::add_global(&mut this, id: u32): void {
|
||||
val home: u32 = this.decls.modules.p[id];
|
||||
val name: u32 = this.decls.names.p[id];
|
||||
val first := this.modules.decl(home, name);
|
||||
if first == 0 {
|
||||
this.modules.set_decl(home, name, id);
|
||||
return;
|
||||
}
|
||||
|
||||
if this.decls.kinds.p[first] == dk::function && this.decls.kinds.p[id] == dk::function {
|
||||
this.decls.chain(first, id);
|
||||
return;
|
||||
}
|
||||
|
||||
this.report_duplicate(id, first);
|
||||
}
|
||||
|
||||
#! Makes the declaration a member of the type, as an overload of the functions
|
||||
#! of its name, or reporting a name already taken. A field or a property is
|
||||
#! reached through a value, and a type declared through the type through the
|
||||
#! type, so those may share a name, as `kind` and `token::kind`, or a property
|
||||
#! `iterator` and `list<T>::iterator`, the type it gives. One declared outside
|
||||
#! `core` on `core`'s type, of a name `core` gives it a member or type of too, is
|
||||
#! kept apart from `core`'s.
|
||||
#! @param this The checker.
|
||||
#! @param owner The type's declaration.
|
||||
#! @param id The member's declaration.
|
||||
def checker::add_member(&mut this, owner: u32, id: u32): void {
|
||||
val name: u32 = this.decls.names.p[id];
|
||||
val types := this.decls.nests(id);
|
||||
val same := this.decls.member_in(owner, name, types);
|
||||
val other := this.decls.member_in(owner, name, !types);
|
||||
|
||||
# A member or type of `core`'s type declared outside `core`, of a name `core`
|
||||
# gives it too, is kept apart and chosen over `core`'s where it is seen, so
|
||||
# `core` adding one later never refuses a program or takes its calls.
|
||||
val core_id := this.in_core(this.decls.modules.p[id]);
|
||||
if !core_id && ((same != 0 && this.in_core(this.decls.modules.p[same]))
|
||||
|| (other != 0 && this.in_core(this.decls.modules.p[other]))) {
|
||||
val first := this.decls.added_member(owner, name);
|
||||
if first == 0 || (this.decls.kinds.p[first] == dk::function && this.decls.kinds.p[id] == dk::function) {
|
||||
this.decls.add_added(owner, id);
|
||||
return;
|
||||
}
|
||||
this.decls.own(id, owner);
|
||||
this.report_duplicate(id, first);
|
||||
return;
|
||||
}
|
||||
# `core`'s, declared after them, takes their place.
|
||||
if core_id && this.decls.added_member(owner, name) == 0 {
|
||||
if same != 0 && !this.in_core(this.decls.modules.p[same]) {
|
||||
this.decls.set_aside(owner, name, types);
|
||||
this.add_member(owner, id);
|
||||
return;
|
||||
}
|
||||
if other != 0 && !this.in_core(this.decls.modules.p[other]) {
|
||||
this.decls.set_aside(owner, name, !types);
|
||||
this.add_member(owner, id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
val value := this.decls.kinds.p[types ? other : id];
|
||||
val shares := other == 0 || value == dk::field || value == dk::property;
|
||||
if shares && (same == 0 || (this.decls.kinds.p[same] == dk::function && this.decls.kinds.p[id] == dk::function)) {
|
||||
this.decls.add_member(owner, id);
|
||||
return;
|
||||
}
|
||||
|
||||
this.decls.own(id, owner);
|
||||
this.report_duplicate(id, same != 0 ? same : other);
|
||||
}
|
||||
|
||||
def checker::report_duplicate(&mut this, id: u32, first: u32): void {
|
||||
this.report_decl_related(id, ec::duplicate, "The name is already declared here.", first, "First declared here.");
|
||||
this.decls.mark(id, df::failed);
|
||||
}
|
||||
@@ -0,0 +1,583 @@
|
||||
#! @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.
|
||||
composed = 0x200000, #! A struct, union, or trait whose traits, those `composed` holds the run of, are worked out, or being worked out.
|
||||
flawed = 0x400000, #! A function or constant whose body or value checking found an error in, which is never run.
|
||||
chooses = 0x800000, #! An alias of a `match const`, which stands for the type it chooses, as `type strip_pointer<T> = match const T { ... }`.
|
||||
sealed = 0x1000000, #! A trait nothing implements, whose constants and types are its own: `@sealed`.
|
||||
}
|
||||
|
||||
#! 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<dk>; #! The kind of each declaration.
|
||||
pub var names: column<u32>; #! The name of each, by its ID.
|
||||
pub var files: column<u32>; #! The file each is declared in.
|
||||
pub var nodes: column<u32>; #! The node declaring each.
|
||||
pub var modules: column<u32>; #! The module each belongs to.
|
||||
pub var owners: column<u32>; #! The type a member belongs to, or the declaration a type parameter belongs to.
|
||||
pub var types: column<u32>; #! What a variable holds, a function's type, or the type a type declares.
|
||||
pub var overloads: column<u32>; #! The next declaration of the same name and place.
|
||||
pub var children: column<u32>; #! The run of a type's fields, enum members, or trait members in `lists`, or a type parameter's bounds.
|
||||
|
||||
var flags: column<u32>; #! The flags of each, as `df` says.
|
||||
var states: column<ds>; #! How far each is resolved.
|
||||
var params: column<u32>; #! The run of a declaration's type parameters in `lists`, its owner's first.
|
||||
pub var targets: column<u32>; #! What an alias stands for, an enum's integer type, or the default of a type a trait asks for.
|
||||
var tops: column<u32>; #! 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<u32>; #! 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::<dk>::new(),
|
||||
names = column::<u32>::new(),
|
||||
files = column::<u32>::new(),
|
||||
nodes = column::<u32>::new(),
|
||||
modules = column::<u32>::new(),
|
||||
owners = column::<u32>::new(),
|
||||
types = column::<u32>::new(),
|
||||
flags = column::<u32>::new(),
|
||||
states = column::<ds>::new(),
|
||||
overloads = column::<u32>::new(),
|
||||
params = column::<u32>::new(),
|
||||
children = column::<u32>::new(),
|
||||
targets = column::<u32>::new(),
|
||||
tops = column::<u32>::new(),
|
||||
lists = runs::<u32>::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>): 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<u32> {
|
||||
var made := vector::<u32>::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<u32> {
|
||||
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<vector<u32>>; #! 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::<vector<u32>>::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::<u32>::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.span.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<u32> {
|
||||
return (key as u64) < this.lists.length ? this.lists[key].span : span::<u32>::blank();
|
||||
}
|
||||
|
||||
#! 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<u32>; #! The name of each module, by its ID.
|
||||
pub var parents: vector<u32>; #! The module each is within.
|
||||
pub var named: vector<bool>; #! 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::<u32>::new(),
|
||||
parents = vector::<u32>::new(),
|
||||
named = vector::<bool>::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;
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
#! @file describe.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how the checker writes types and declarations as notes spell them,
|
||||
#! as `&mut list<int>` or `def(u8): bool`.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! The type, as notes spell it.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Its spelling, as `list<i64>`.
|
||||
pub def checker::describe(&this, kind: u32): string {
|
||||
var out := string::new(nothing {});
|
||||
this.describe_into(kind, &mut out, 0);
|
||||
return out;
|
||||
}
|
||||
|
||||
def checker::describe_into(&this, kind: u32, out: &mut string, depth: u32): void {
|
||||
if depth > 16 {
|
||||
out.append("...");
|
||||
return;
|
||||
}
|
||||
|
||||
val first := this.types.first_of(kind);
|
||||
match this.types.kind_of(kind) {
|
||||
ty::error => out.append("?");
|
||||
ty::void_type => out.append("void");
|
||||
ty::null_type => out.append("null");
|
||||
ty::boolean => out.append("bool");
|
||||
ty::untyped_bool => out.append("bool");
|
||||
ty::untyped_int => out.append("integer constant");
|
||||
ty::untyped_uint => out.append("unsigned integer constant");
|
||||
ty::untyped_char => out.append("character constant");
|
||||
ty::untyped_float => out.append("float constant");
|
||||
ty::untyped_float32 => out.append("float constant");
|
||||
ty::untyped_text => out.append("text");
|
||||
ty::empty => out.append("empty array element");
|
||||
ty::signed => out.append(first == 64 ? "int" : first == 32 ? "i32" : first == 16 ? "i16" : "i8");
|
||||
ty::unsigned => out.append(first == 64 ? "uint" : first == 32 ? "u32" : first == 16 ? "u16" : "u8");
|
||||
ty::floating => out.append(first == 64 ? "float" : "f32");
|
||||
ty::reference, ty::pointer => {
|
||||
out.append(this.types.kind_of(kind) == ty::reference ? "&" : "*");
|
||||
if this.types.second_of(kind) != 0 {
|
||||
out.append("mut ");
|
||||
}
|
||||
this.describe_into(first, out, depth + 1);
|
||||
}
|
||||
ty::fixed => {
|
||||
this.describe_into(first, out, depth + 1);
|
||||
if this.types.part_count(kind) != 0 {
|
||||
out.append("[");
|
||||
this.describe_into(this.types.part_at(kind, 0), out, depth + 1);
|
||||
out.append("]");
|
||||
} else {
|
||||
val count := $"[{this.types.second_of(kind)}]";
|
||||
out.append(count.span);
|
||||
}
|
||||
}
|
||||
# A constant is written as its value, as `4` of `buffer<u8, 4>`.
|
||||
ty::value => this.describe_known(this.types.second_of(kind), out, depth + 1);
|
||||
ty::function => {
|
||||
# A pack parameter says `...` of itself.
|
||||
out.append("def(");
|
||||
this.describe_parts(kind, out, depth);
|
||||
val count := this.types.part_count(kind);
|
||||
if this.types.second_of(kind) != 0 && (count == 0 || !this.types.is_open_pack(this.types.part_at(kind, count - 1))) {
|
||||
out.append("...");
|
||||
}
|
||||
out.append("): ");
|
||||
this.describe_into(first, out, depth + 1);
|
||||
}
|
||||
# A pack among the type arguments is written as the types it gives, as
|
||||
# `box<i64, bool, u8>` of `box<A, Ts...>`.
|
||||
ty::named => {
|
||||
if this.qualified_names {
|
||||
this.module_names(first, out);
|
||||
}
|
||||
if this.owned_names || this.qualified_names {
|
||||
this.owner_names(first, out, 0);
|
||||
}
|
||||
out.append(this.names.text(this.decls.names.p[first]));
|
||||
if this.types.part_count(kind) != 0 {
|
||||
out.append("<");
|
||||
if this.has_patterns(first) {
|
||||
this.describe_written(kind, out, depth);
|
||||
} else {
|
||||
this.describe_args(this.types.parts_view(kind), out, depth + 1);
|
||||
}
|
||||
out.append(">");
|
||||
}
|
||||
}
|
||||
ty::param => {
|
||||
# A type a bound asks of a type parameter is named as it is written, as
|
||||
# `T::target`, as a constant asked so is.
|
||||
val owner: u32 = this.decls.owners.p[first];
|
||||
if owner != 0 && this.decls.kinds.p[owner] == dk::trait_type && !this.decls.has(first, df::valued) {
|
||||
for &each in this.stand_ins {
|
||||
continue if each.param != first;
|
||||
this.describe_into(each.base, out, depth + 1);
|
||||
out.append("::");
|
||||
break;
|
||||
}
|
||||
}
|
||||
out.append(this.names.text(this.decls.names.p[first]));
|
||||
}
|
||||
# A type `match const` chooses, before it is chosen, says what it chooses by.
|
||||
ty::matched => {
|
||||
# `C ? A : B` chooses `A` where `C` is `true`, and `B` otherwise, and a part
|
||||
# of `C` testing a type says what it tests, as `T is reference ? A : B`.
|
||||
val pattern := this.types.part_count(kind) == 5 ? this.types.part_at(kind, 1) : 0;
|
||||
val written := first < this.units.length as u32
|
||||
&& (this.units[first].syntax.node_kinds[this.types.second_of(kind)] == nk::type_cond
|
||||
|| this.units[first].syntax.node_kinds[this.types.second_of(kind)] == nk::ternary);
|
||||
if this.types.part_count(kind) == 5 && this.types.part_at(kind, 3) == 0 && (written || this.is_truth(pattern)) {
|
||||
this.describe_into(this.types.part_at(kind, 0), out, depth + 1);
|
||||
if !this.is_truth(pattern) {
|
||||
out.append(this.types.kind_of(pattern) == ty::value ? " == " : " is ");
|
||||
this.describe_into(pattern, out, depth + 1);
|
||||
}
|
||||
out.append(" ? ");
|
||||
this.describe_into(this.types.part_at(kind, 2), out, depth + 1);
|
||||
out.append(" : ");
|
||||
this.describe_into(this.types.part_at(kind, 4), out, depth + 1);
|
||||
} else {
|
||||
out.append("match const ");
|
||||
this.describe_into(this.types.part_at(kind, 0), out, depth + 1);
|
||||
}
|
||||
}
|
||||
ty::pack => {
|
||||
if this.types.part_count(kind) == 0 && first != 0 {
|
||||
out.append(this.names.text(this.decls.names.p[first]));
|
||||
out.append("...");
|
||||
} else {
|
||||
# A variadic function's pack of parameters ends in the one taking the rest.
|
||||
out.append("(");
|
||||
this.describe_parts(kind, out, depth);
|
||||
if this.types.second_of(kind) != 0 {
|
||||
out.append("...");
|
||||
}
|
||||
out.append(")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! The type, with the module of each type it names, as `path_of` spells it:
|
||||
#! `std::containers::dict<core::string, game::item>`, which no two types share.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Its spelling.
|
||||
pub def checker::describe_path(&mut this, kind: u32): string {
|
||||
val saved := this.qualified_names;
|
||||
this.qualified_names = true;
|
||||
var out := this.describe(kind);
|
||||
this.qualified_names = saved;
|
||||
return out;
|
||||
}
|
||||
|
||||
#! Writes the modules a declaration is in, outermost first, as `std::io::`.
|
||||
#! @param this The checker.
|
||||
#! @param decl The declaration.
|
||||
#! @param out Where they are written.
|
||||
def checker::module_names(&this, decl: u32, out: &mut string): void {
|
||||
var modules := vector::<u32>::new();
|
||||
var at: u32 = this.decls.modules.p[decl];
|
||||
while at != 0 && modules.length < 64 {
|
||||
modules.append(at);
|
||||
at = this.modules.parents[at];
|
||||
}
|
||||
var i := modules.length;
|
||||
while i > 0 {
|
||||
i--;
|
||||
out.append(this.names.text(this.modules.names[modules[i]]));
|
||||
out.append("::");
|
||||
}
|
||||
}
|
||||
|
||||
#! Writes the types a type is declared through, as `token::` of `token::kind`.
|
||||
#! @param this The checker.
|
||||
#! @param decl The type's declaration.
|
||||
#! @param out Where they are written.
|
||||
#! @param depth How many have been written.
|
||||
def checker::owner_names(&this, decl: u32, out: &mut string, depth: u32): void {
|
||||
val owner: u32 = this.decls.owners.p[decl];
|
||||
return if owner == 0 || depth > 8 || !this.decls.nests(decl) || this.decls.kinds.p[owner] == dk::builtin;
|
||||
this.owner_names(owner, out, depth + 1);
|
||||
out.append(this.names.text(this.decls.names.p[owner]));
|
||||
out.append("::");
|
||||
}
|
||||
|
||||
def checker::describe_parts(&this, kind: u32, out: &mut string, depth: u32): void {
|
||||
for i in 0..this.types.part_count(kind) {
|
||||
if i != 0 {
|
||||
out.append(", ");
|
||||
}
|
||||
this.describe_into(this.types.part_at(kind, i as u32), out, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
#! The name of the declaration, as notes spell it.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns The name.
|
||||
pub def checker::decl_text(&this, id: u32): strview {
|
||||
return this.names.text(this.decls.names.p[id]);
|
||||
}
|
||||
|
||||
#! What the declaration is, in a word or two, as "function" or "struct".
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns The word.
|
||||
pub def checker::decl_word(&this, id: u32): strview {
|
||||
return match this.decls.kinds.p[id] {
|
||||
dk::builtin => "type",
|
||||
dk::function => "function",
|
||||
dk::variable => this.decls.has(id, df::constant) ? "constant" : "variable",
|
||||
dk::field => "field",
|
||||
dk::struct_type => "struct",
|
||||
dk::union_type => "union",
|
||||
dk::enum_type => "enum",
|
||||
dk::enum_member => "enum member",
|
||||
dk::trait_type => "trait",
|
||||
dk::alias_type => "type alias",
|
||||
dk::type_param => "type parameter",
|
||||
dk::assoc_type => "type",
|
||||
dk::property => "property",
|
||||
dk::attribute => "constant",
|
||||
else => "declaration"
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
#! @file evaluating.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what the checker asks of the program's evaluator, which runs code
|
||||
#! while checking, where a value must be known that folding does not give: an
|
||||
#! array's length a function gives, the condition of an `if const`, a local
|
||||
#! constant's value, a range's end for `for const`, a constant type argument,
|
||||
#! or the text of `compile_error`. The analyzer has no evaluator of its own: the
|
||||
#! program's machine is one, which whoever checks a program to run or build it
|
||||
#! gives the checker before checking.
|
||||
#!
|
||||
#! What is asked for is lowered with what it calls, each checked as lowering
|
||||
#! reaches it, and run, unless what it calls had an error checking it, which is
|
||||
#! never run. A checker given no evaluator, as one answering an editor's
|
||||
#! queries, knows what folding gives and nothing more.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! What the checker asks the evaluator for.
|
||||
pub enum evaluation_kind : u8 {
|
||||
value, #! The value of an expression: `table` and the node.
|
||||
local, #! A local constant's value: `table` and its `variable` node.
|
||||
condition, #! Whether the condition of an `if const` holds: `table` and the `if_const` node.
|
||||
text, #! The text `compile_error` reports: `table` and the `call` node.
|
||||
}
|
||||
|
||||
#! What the checker asks the evaluator to work out, and where.
|
||||
pub struct evaluation_ask {
|
||||
pub var what: evaluation_kind; #! What it is.
|
||||
pub var table: u32; #! The result table the node was checked in.
|
||||
pub var node: u32; #! The node.
|
||||
}
|
||||
|
||||
#! What the evaluator worked out of what was asked.
|
||||
pub struct evaluation_answer {
|
||||
pub var worked: bool; #! Whether it was worked out.
|
||||
pub var noted: bool; #! Where it was not, whether the evaluator noted why, so the checker notes nothing more.
|
||||
pub var scalar: bool; #! Whether the value is a number, `bool`, character, or member of an enum, which `value` holds.
|
||||
pub var value: constant; #! The value, where it is one of those, or whether a condition holds.
|
||||
pub var text: string; #! The text `compile_error` reports, where that was asked for.
|
||||
}
|
||||
|
||||
#! What the checker keeps of where checking is while the evaluator runs, which
|
||||
#! checks what checking reached first: what one body's checking holds that
|
||||
#! another's must begin without.
|
||||
struct checker::outside {
|
||||
var at: checker::frame; #! Where checking was.
|
||||
var quiet: u32; #! Whether notes were left out.
|
||||
var undecided: u32; #! How many undecided `if const` and `for const` were around it.
|
||||
var waits_for: u32; #! The call that might wait for its parameter's type.
|
||||
var free_result: bool; #! Whether a lambda takes its result from its body.
|
||||
var walked: u64; #! How many places loops walked.
|
||||
}
|
||||
|
||||
#! Asks the program's evaluator for what only running gives, once what checking
|
||||
#! reached is checked.
|
||||
#! @param this The checker.
|
||||
#! @param what What is asked for.
|
||||
#! @param node The node it is asked of, in the table being written.
|
||||
#! @param out Where the answer is written.
|
||||
#! @returns Whether it was worked out.
|
||||
pub def checker::ask_evaluator(&mut this, what: evaluation_kind, node: u32, out: &mut evaluation_answer): bool {
|
||||
out.worked = false;
|
||||
out.noted = false;
|
||||
return false if this.quiet != 0 || this.waits_for_instance();
|
||||
# An expression checking found an error in is never run.
|
||||
return false if what == evaluation_kind::value && this.type_at(node) == bt::error;
|
||||
val run := this.evaluator else { return false; };
|
||||
val ask := evaluation_ask { what = what, table = this.table_node_of(node).table, node = node };
|
||||
|
||||
val held := this.step_outside();
|
||||
run(&ask, out);
|
||||
this.step_back(&held);
|
||||
return out.worked;
|
||||
}
|
||||
|
||||
#! Keeps where checking is and begins as checking a declaration of its own does,
|
||||
#! so what is checked meanwhile, as what an evaluation lowers, sees nothing of
|
||||
#! the body being checked.
|
||||
#! @param this The checker.
|
||||
#! @returns What was kept, which `step_back` puts back.
|
||||
def checker::step_outside(&mut this): checker::outside {
|
||||
val made := checker::outside {
|
||||
at = this.at,
|
||||
quiet = this.quiet,
|
||||
undecided = this.undecided,
|
||||
waits_for = this.waits_for,
|
||||
free_result = this.free_result,
|
||||
walked = this.walked.length
|
||||
};
|
||||
this.quiet = 0;
|
||||
this.undecided = 0;
|
||||
this.waits_for = 0;
|
||||
this.free_result = false;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Puts back where checking was, as `step_outside` kept it.
|
||||
#! @param this The checker.
|
||||
#! @param held What was kept.
|
||||
def checker::step_back(&mut this, held: &checker::outside): void {
|
||||
this.at = held.at;
|
||||
this.quiet = held.quiet;
|
||||
this.undecided = held.undecided;
|
||||
this.waits_for = held.waits_for;
|
||||
this.free_result = held.free_result;
|
||||
this.walked.resize(held.walked);
|
||||
}
|
||||
|
||||
#! Asks the evaluator for the value of an expression of a number, `bool`,
|
||||
#! character, or enum, and records it at the node where it was worked out. One
|
||||
#! reading a local only the program running gives a value is not asked for.
|
||||
#! @param this The checker.
|
||||
#! @param node The expression.
|
||||
#! @param out Where the answer is written.
|
||||
#! @returns Whether its value is known now.
|
||||
pub def checker::run_value(&mut this, node: u32, out: &mut evaluation_answer): bool {
|
||||
return false if this.runtime_local_read(node) != 0;
|
||||
if this.ask_evaluator(evaluation_kind::value, node, out) && out.scalar {
|
||||
this.set_value(node, out.value);
|
||||
}
|
||||
return this.is_known(node);
|
||||
}
|
||||
|
||||
#! The constant made to fit an integer type of its bits, wrapping as the
|
||||
#! program running wraps.
|
||||
#! @param this The checker.
|
||||
#! @param value The constant.
|
||||
#! @param kind The integer type.
|
||||
#! @returns The constant wrapped to its bits.
|
||||
def checker::wrap_constant(&mut this, value: constant, kind: u32): constant {
|
||||
val plain := this.unalias(kind);
|
||||
val tkd := this.types.kind_of(plain);
|
||||
return value if value.floating || (tkd != ty::signed && tkd != ty::unsigned);
|
||||
val bits := this.types.first_of(plain);
|
||||
var raw := value.bits();
|
||||
if bits < 64 {
|
||||
raw &= (1u << bits as u64) - 1;
|
||||
if tkd == ty::signed && (raw >> (bits - 1) as u64) != 0 {
|
||||
raw |= ~((1u << bits as u64) - 1);
|
||||
}
|
||||
}
|
||||
return constant::from_bits(raw, tkd == ty::signed);
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,847 @@
|
||||
#! @file generics.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains generics: inferring the type arguments of a call from what it is
|
||||
#! given, and the instances of generic functions. A generic's body is checked
|
||||
#! once against its bounds, then again for each instance the program uses,
|
||||
#! over the template's own nodes with a result table of the instance's, so the
|
||||
#! template is never copied. An instance is made where a call gives a generic
|
||||
#! types holding no type parameter, and its body is checked once every
|
||||
#! declaration has been.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! What inferring type arguments works out: the type of each parameter of the
|
||||
#! declaration, its owner's first, and whether each was only suggested by a
|
||||
#! constant, which a typed value given for it overrules.
|
||||
pub struct checker::inferred {
|
||||
var params: vector<u32>; #! The declarations of the type parameters.
|
||||
var binds: vector<u32>; #! The type of each, or `unknown_type` where nothing gave it yet.
|
||||
var soft: vector<bool>; #! Whether each was only suggested by a constant.
|
||||
var outer: vector<u32>; #! The parameters a trait's default names beside its own: the trait's, and `self`.
|
||||
var given: vector<u32>; #! The type each of those is given by the value it is called on.
|
||||
}
|
||||
|
||||
#! Begins inferring the declaration's type arguments, with those of its owner
|
||||
#! and those written given.
|
||||
#! @param this The checker.
|
||||
#! @param decl The generic declaration.
|
||||
#! @param owner The type it is a member of, given its arguments, or the
|
||||
#! error type.
|
||||
#! @param written The type arguments written for its own parameters.
|
||||
#! @returns What is known so far.
|
||||
pub def checker::begin_inference(&mut this, decl: u32, owner: u32, written: span<u32>): checker::inferred {
|
||||
var made := checker::inferred {
|
||||
params = vector::<u32>::new(),
|
||||
binds = vector::<u32>::new(),
|
||||
soft = vector::<bool>::new(),
|
||||
outer = vector::<u32>::new(),
|
||||
given = vector::<u32>::new()
|
||||
};
|
||||
|
||||
val params := this.decls.list_view(this.decls.params.p[decl]);
|
||||
made.params.extend(params);
|
||||
made.binds.resize(params.length, checker::unknown_type);
|
||||
made.soft.resize(params.length, false);
|
||||
|
||||
var at: u64 = 0;
|
||||
if owner != bt::error && this.types.kind_of(owner) == ty::named {
|
||||
for &part in this.types.parts_view(owner) {
|
||||
break if at >= made.binds.length;
|
||||
made.binds[at] = part;
|
||||
at++;
|
||||
}
|
||||
}
|
||||
# Written type arguments fill a pack with whatever the parameters before it
|
||||
# leave, as `show_all::<i64, bool>`.
|
||||
for i in 0..written.length {
|
||||
break if at >= made.binds.length;
|
||||
val param := made.params[at];
|
||||
if this.decls.has(param, df::packed) {
|
||||
made.binds[at] = this.gather_pack(written, i, 0);
|
||||
break;
|
||||
}
|
||||
made.binds[at] = written[i];
|
||||
at++;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Binds a pack parameter to the types of the arguments a call gives it from
|
||||
#! `from` on, each spread pack already its own types.
|
||||
#! @param this The checker.
|
||||
#! @param pack The pack parameter's type.
|
||||
#! @param slots The types of the call's arguments.
|
||||
#! @param from The first argument the pack takes.
|
||||
#! @param into What is inferred, which the pack's types are written to.
|
||||
pub def checker::bind_pack(&mut this, pack: u32, slots: span<u32>, from: u64, into: &mut checker::inferred): void {
|
||||
var types := vector::<u32>::new();
|
||||
for i in from..slots.length {
|
||||
types.append(slots[i] == 0 ? bt::error : this.settle(slots[i]));
|
||||
}
|
||||
|
||||
val decl := this.types.first_of(pack);
|
||||
for i in 0..into.params.length {
|
||||
if into.params[i] == decl && (into.binds[i] == checker::unknown_type || into.soft[i]) {
|
||||
into.binds[i] = this.types.pack(types.span, 0);
|
||||
into.soft[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Infers type parameters a type holds from the type given where it is
|
||||
#! expected, as `T` of `&T` from a `&i64`.
|
||||
#! @param this The checker.
|
||||
#! @param pattern The type written, holding the parameters.
|
||||
#! @param actual The type given.
|
||||
#! @param into What is inferred.
|
||||
#! @param depth How deeply the search has gone, which stops a type nesting
|
||||
#! without end.
|
||||
pub def checker::unify(&mut this, pattern: u32, actual: u32, into: &mut checker::inferred, depth: u32): void {
|
||||
return if depth > 16 || !this.types.is_open(pattern) || actual == bt::error;
|
||||
|
||||
# A reference is read through to what it refers to wherever a type that is no
|
||||
# reference is wanted, so `through(r)` takes the pointer `r` refers to.
|
||||
val shape := this.types.kind_of(pattern);
|
||||
if shape != ty::param && shape != ty::pack && shape != ty::reference {
|
||||
actual = this.reached(actual);
|
||||
}
|
||||
|
||||
match shape {
|
||||
ty::param => {
|
||||
val decl := this.types.first_of(pattern);
|
||||
# A constant parameter is given a constant, and a type parameter a type.
|
||||
val given := this.types.kind_of(actual);
|
||||
val constant := given == ty::value || (given == ty::param && this.decls.has(this.types.first_of(actual), df::valued));
|
||||
return if this.decls.has(decl, df::valued) != constant;
|
||||
for i in 0..into.params.length {
|
||||
continue if into.params[i] != decl;
|
||||
|
||||
if this.types.is_untyped(actual) {
|
||||
if into.binds[i] == checker::unknown_type {
|
||||
into.binds[i] = this.settle(actual);
|
||||
into.soft[i] = true;
|
||||
} else if into.soft[i] && this.types.is_float(actual) && !this.types.is_float(into.binds[i]) {
|
||||
# An integer constant and a float constant share the float.
|
||||
into.binds[i] = this.settle(actual);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if into.binds[i] == checker::unknown_type || into.soft[i] {
|
||||
into.binds[i] = actual;
|
||||
into.soft[i] = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
ty::reference, ty::pointer => {
|
||||
# A type parameter known to have a shape, as `T: pointer` says, gives what
|
||||
# it is a shape of, as `through(p)` takes `T::target` from a `T`.
|
||||
var plain := this.unalias(actual);
|
||||
if this.types.kind_of(plain) == ty::param {
|
||||
plain = this.shaped_plain(plain);
|
||||
}
|
||||
if this.types.kind_of(plain) == this.types.kind_of(pattern) {
|
||||
this.unify(this.types.first_of(pattern), this.types.first_of(plain), into, depth + 1);
|
||||
} else if this.types.kind_of(pattern) == ty::reference {
|
||||
this.unify(this.types.first_of(pattern), actual, into, depth + 1);
|
||||
}
|
||||
}
|
||||
ty::fixed => {
|
||||
var plain := this.unalias(this.reached_value(actual));
|
||||
if this.types.kind_of(plain) == ty::param {
|
||||
plain = this.shaped_plain(plain);
|
||||
}
|
||||
if this.types.kind_of(plain) == ty::fixed {
|
||||
this.unify(this.types.first_of(pattern), this.types.first_of(plain), into, depth + 1);
|
||||
# A length that is a constant parameter is given the length of the array.
|
||||
if this.types.part_count(pattern) != 0 {
|
||||
val length := this.types.part_at(pattern, 0);
|
||||
if this.types.part_count(plain) != 0 {
|
||||
this.unify(length, this.types.part_at(plain, 0), into, depth + 1);
|
||||
} else if this.types.kind_of(length) == ty::param && this.decls.has(this.types.first_of(length), df::valued) {
|
||||
val of := this.constant_type(length);
|
||||
val tkd := this.types.kind_of(this.unalias(of));
|
||||
if tkd == ty::signed || tkd == ty::unsigned {
|
||||
val count := this.known_number(constant::integer(this.types.second_of(plain) as u64, false), of);
|
||||
this.unify(length, this.known_type(count), into, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ty::function => {
|
||||
val plain := this.unalias(actual);
|
||||
return if this.types.kind_of(plain) != ty::function;
|
||||
# A pack among the parameters, as `Ts` of `def(Ts...): R`, takes the types of
|
||||
# those after the ones before it, and is variadic where the function is.
|
||||
val count := this.types.part_count(pattern);
|
||||
val given := this.types.part_count(plain);
|
||||
if this.types.second_of(pattern) != 0 && count != 0 && this.types.is_open_pack(this.types.part_at(pattern, count - 1)) {
|
||||
return if given + 1 < count;
|
||||
for i in 0..count - 1 {
|
||||
this.unify(this.types.part_at(pattern, i as u32), this.types.part_at(plain, i as u32), into, depth + 1);
|
||||
}
|
||||
this.bind_param(this.types.first_of(this.types.part_at(pattern, count - 1)), this.types.params_pack(plain, count - 1), into);
|
||||
this.unify(this.types.first_of(pattern), this.types.first_of(plain), into, depth + 1);
|
||||
return;
|
||||
}
|
||||
if given == count {
|
||||
for i in 0..count {
|
||||
this.unify(this.types.part_at(pattern, i as u32), this.types.part_at(plain, i as u32), into, depth + 1);
|
||||
}
|
||||
this.unify(this.types.first_of(pattern), this.types.first_of(plain), into, depth + 1);
|
||||
}
|
||||
}
|
||||
# A pack whose types are not known takes those of the pack given.
|
||||
ty::pack => {
|
||||
if this.types.is_open_pack(pattern) && this.types.kind_of(actual) == ty::pack {
|
||||
this.bind_param(this.types.first_of(pattern), actual, into);
|
||||
} else if !this.types.is_open_pack(pattern) && this.types.kind_of(actual) == ty::pack
|
||||
&& this.types.part_count(actual) == this.types.part_count(pattern) {
|
||||
for i in 0..this.types.part_count(pattern) {
|
||||
this.unify(this.types.part_at(pattern, i as u32), this.types.part_at(actual, i as u32), into, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
ty::named => {
|
||||
var plain := this.reached_value(actual);
|
||||
if this.types.kind_of(plain) == ty::named && this.types.first_of(plain) != this.types.first_of(pattern) {
|
||||
plain = this.unalias(plain);
|
||||
}
|
||||
# A value of a type implementing a generic trait gives its parameters
|
||||
# through what implements the trait's members.
|
||||
val wanted := this.types.first_of(pattern);
|
||||
if this.decls.kinds.p[wanted] == dk::trait_type && this.named_kind(plain) != dk::trait_type
|
||||
&& this.types.kind_of(plain) != ty::param {
|
||||
this.unify_named(pattern, plain, into, depth);
|
||||
this.unify_members(pattern, plain, into, depth);
|
||||
return;
|
||||
}
|
||||
if this.types.kind_of(plain) == ty::named && this.types.first_of(plain) == this.types.first_of(pattern) {
|
||||
for i in 0..this.types.part_count(pattern) {
|
||||
break if i >= this.types.part_count(plain);
|
||||
this.unify(this.types.part_at(pattern, i as u32), this.types.part_at(plain, i as u32), into, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
|
||||
#! Gives a type parameter, or a pack, the type inferred for it, where nothing
|
||||
#! but a constant has given it one yet.
|
||||
#! @param this The checker.
|
||||
#! @param decl The parameter's declaration.
|
||||
#! @param given The type it is given.
|
||||
#! @param into What is inferred.
|
||||
def checker::bind_param(&mut this, decl: u32, given: u32, into: &mut checker::inferred): void {
|
||||
for i in 0..into.params.length {
|
||||
continue if into.params[i] != decl;
|
||||
if into.binds[i] == checker::unknown_type || into.soft[i] {
|
||||
into.binds[i] = given;
|
||||
into.soft[i] = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#! Infers type parameters a generic trait holds from a type naming it, as the
|
||||
#! type names it: `x : into<int>` gives the `T` of `into<T>` as `int`. A
|
||||
#! member's types may be what the trait's arguments ask for, as `F::arg`, which
|
||||
#! say nothing of the argument itself, so what the type names is read first.
|
||||
#! @param this The checker.
|
||||
#! @param pattern The trait's type, holding the parameters.
|
||||
#! @param kind The type given.
|
||||
#! @param into What is inferred.
|
||||
#! @param depth How deeply the search has gone.
|
||||
def checker::unify_named(&mut this, pattern: u32, kind: u32, into: &mut checker::inferred, depth: u32): void {
|
||||
return if depth > 16;
|
||||
val holder := this.types.first_of(pattern);
|
||||
val named := this.implemented(this.reached_value(kind), holder);
|
||||
return if named == this.decls.types.p[holder] || this.types.first_of(named) != holder;
|
||||
for i in 0..this.types.part_count(pattern) {
|
||||
break if i >= this.types.part_count(named);
|
||||
this.unify(this.types.part_at(pattern, i as u32), this.types.part_at(named, i as u32), into, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
#! Infers type parameters a generic trait holds from a type implementing it,
|
||||
#! through what implements each of the trait's members, as `count::into(&this):
|
||||
#! int` gives the `T` of `into<T>`, and through what implements those of the
|
||||
#! traits it composes.
|
||||
#! @param this The checker.
|
||||
#! @param pattern The trait's type, holding the parameters.
|
||||
#! @param kind The type given.
|
||||
#! @param into What is inferred.
|
||||
#! @param depth How deeply the search has gone.
|
||||
def checker::unify_members(&mut this, pattern: u32, kind: u32, into: &mut checker::inferred, depth: u32): void {
|
||||
return if depth > 16;
|
||||
val holder := this.types.first_of(pattern);
|
||||
val run: u32 = this.decls.children.p[holder];
|
||||
for i in 0..this.decls.list_length(run) {
|
||||
val wanted := this.decls.list_at(run, i as u32);
|
||||
val kind_of: dk = this.decls.kinds.p[wanted];
|
||||
continue if kind_of != dk::function && kind_of != dk::variable;
|
||||
val found := this.find_member(kind, this.decls.names.p[wanted]);
|
||||
# A method the compiler gives a built-in type, as a function's `invoke`, says
|
||||
# what it takes and gives as a declared one does.
|
||||
if found.decl == 0 && kind_of == dk::function && this.decls.has(wanted, df::method) {
|
||||
val given := this.builtin_method(kind, this.decls.names.p[wanted]);
|
||||
if given != 0 {
|
||||
this.resolve(wanted);
|
||||
val asked := this.trait_in_terms(this.decls.types.p[wanted], holder, pattern, kind);
|
||||
this.unify(this.past_receiver(asked), this.past_receiver(given), into, depth + 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
continue if found.decl == 0 || this.enclosing_trait(found.decl) != 0;
|
||||
this.resolve(wanted);
|
||||
val asked := this.trait_in_terms(this.decls.types.p[wanted], holder, pattern, kind);
|
||||
if kind_of == dk::variable {
|
||||
continue if this.decls.kinds.p[found.decl] != dk::variable;
|
||||
this.resolve(found.decl);
|
||||
this.unify(asked, this.instantiated(this.decls.types.p[found.decl], this.type_decl(found.owner), found.owner), into, depth + 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
# Of overloads, one of the shape asked for.
|
||||
val count := this.types.part_count(asked);
|
||||
var walk := this.decls.walk_overloads(found.decl);
|
||||
while true {
|
||||
val at := walk.next();
|
||||
break if at == 0;
|
||||
if this.decls.kinds.p[at] == dk::function && this.decls.has(at, df::method) == this.decls.has(wanted, df::method)
|
||||
&& this.member_seen(at) {
|
||||
val given := this.method_type(at, found.owner);
|
||||
if this.types.kind_of(given) == ty::function && this.types.part_count(given) == count {
|
||||
val skip: u64 = this.decls.has(wanted, df::method) ? 1 : 0;
|
||||
for p in skip..count as u64 {
|
||||
this.unify(this.types.part_at(asked, p as u32), this.types.part_at(given, p as u32), into, depth + 1);
|
||||
}
|
||||
this.unify(this.types.first_of(asked), this.types.first_of(given), into, depth + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val composed := this.composed_of(holder);
|
||||
for i in 0..this.decls.list_length(composed) {
|
||||
val inner := this.trait_in_terms(this.decls.list_at(composed, i as u32), holder, pattern, kind);
|
||||
this.unify_members(inner, kind, into, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
#! A method's type without its receiver: the function its other parameters and
|
||||
#! its result make, variadic where the method is.
|
||||
#! @param this The checker.
|
||||
#! @param method The method's type.
|
||||
#! @returns The function's type.
|
||||
def checker::past_receiver(&mut this, method: u32): u32 {
|
||||
var parts := vector::<u32>::new();
|
||||
for i in 1..this.types.part_count(method) {
|
||||
parts.append(this.types.part_at(method, i as u32));
|
||||
}
|
||||
return this.types.function(parts.span, this.types.first_of(method), this.types.second_of(method) != 0);
|
||||
}
|
||||
|
||||
#! What the type arguments inferred so far give those their bounds name, as `C`,
|
||||
#! where `C: iterable<T>`, gives `T` the type of the elements it lends. A bound
|
||||
#! may name a parameter another bound then gives more of, so the bounds are read
|
||||
#! again until they give nothing new.
|
||||
#! @param this The checker.
|
||||
#! @param into What is inferred.
|
||||
#! @param depth How many times the bounds have been read.
|
||||
pub def checker::unify_bounds(&mut this, into: &mut checker::inferred, depth: u32): void {
|
||||
return if depth > 4 || this.unbound(into) == 0;
|
||||
val before := this.unbound_count(into);
|
||||
for i in 0..into.params.length {
|
||||
val param: u32 = into.params[i];
|
||||
val given: u32 = into.binds[i];
|
||||
continue if given == checker::unknown_type || into.soft[i] || this.decls.has(param, df::valued);
|
||||
this.resolve(param);
|
||||
val bounds: u32 = this.decls.children.p[param];
|
||||
for j in 0..this.decls.list_length(bounds) {
|
||||
val bound := this.outer_terms(this.decls.list_at(bounds, j as u32), into);
|
||||
if this.types.is_open(bound) {
|
||||
this.unify_trait(bound, given, into, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
if this.unbound_count(into) != before {
|
||||
this.unify_bounds(into, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
#! A type a trait's default names, with the trait's parameters and `self` given
|
||||
#! the types the value it is called on gives them, leaving its own parameters.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param into What is inferred.
|
||||
#! @returns The type in those terms.
|
||||
pub def checker::outer_terms(&mut this, kind: u32, into: &checker::inferred): u32 {
|
||||
return kind if into.outer.length == 0;
|
||||
return this.substitute(kind, into.outer.span, into.given.span);
|
||||
}
|
||||
|
||||
#! Gives what is inferred for a trait's default compiled for each type the
|
||||
#! terms its bounds are read in: the trait's parameters, given the trait the
|
||||
#! value it is called on implements, and `self`, given the value's type.
|
||||
#! @param this The checker.
|
||||
#! @param method The default's declaration.
|
||||
#! @param through The type of the value it is called on, read through its
|
||||
#! references.
|
||||
#! @param into What is inferred.
|
||||
pub def checker::give_outer(&mut this, method: u32, through: u32, into: &mut checker::inferred): void {
|
||||
val holder := this.enclosing_trait(method);
|
||||
return if holder == 0;
|
||||
val plain := this.unalias(through);
|
||||
val trait_kind := this.trait_reached(method, plain);
|
||||
val params: u32 = this.decls.params.p[holder];
|
||||
for i in 0..this.decls.list_length(params) {
|
||||
into.outer.append(this.decls.list_at(params, i as u32));
|
||||
into.given.append(i < this.types.part_count(trait_kind) ? this.types.part_at(trait_kind, i as u32) : bt::error);
|
||||
}
|
||||
into.outer.append(this.self_decl(holder));
|
||||
into.given.append(plain);
|
||||
}
|
||||
|
||||
#! How many type parameters nothing has given a type yet.
|
||||
#! @param this The checker.
|
||||
#! @param inferred What was inferred.
|
||||
#! @returns How many.
|
||||
def checker::unbound_count(&this, inferred: &checker::inferred): u32 {
|
||||
var count: u32 = 0;
|
||||
for i in 0..inferred.binds.length {
|
||||
count += inferred.binds[i] == checker::unknown_type ? 1 : 0;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
#! Infers the arguments of a trait a type implements from how it implements it:
|
||||
#! each type the trait asks for is the one the type gives, which the bounds it is
|
||||
#! asked with say more of, and each method's parameters and result are those of
|
||||
#! the type's method.
|
||||
#! @param this The checker.
|
||||
#! @param pattern The trait, as the template writes it.
|
||||
#! @param actual The type implementing it.
|
||||
#! @param into What is inferred.
|
||||
#! @param depth How deeply the search has gone.
|
||||
def checker::unify_trait(&mut this, pattern: u32, actual: u32, into: &mut checker::inferred, depth: u32): void {
|
||||
return if depth > 8 || actual == bt::error;
|
||||
val plain := this.unalias(pattern);
|
||||
return if this.named_kind(plain) != dk::trait_type;
|
||||
val asked := this.types.first_of(plain);
|
||||
this.resolve(asked);
|
||||
this.unify_named(plain, actual, into, depth);
|
||||
|
||||
# The trait's own parameters are the pattern's arguments, and `self` the type.
|
||||
var from := vector::<u32>::new();
|
||||
var to := vector::<u32>::new();
|
||||
val params: u32 = this.decls.params.p[asked];
|
||||
for i in 0..this.decls.list_length(params) {
|
||||
from.append(this.decls.list_at(params, i as u32));
|
||||
to.append(i < this.types.part_count(plain) ? this.types.part_at(plain, i as u32) : bt::error);
|
||||
}
|
||||
from.append(this.self_decl(asked));
|
||||
to.append(actual);
|
||||
|
||||
val members: u32 = this.decls.children.p[asked];
|
||||
for i in 0..this.decls.list_length(members) {
|
||||
val member := this.decls.list_at(members, i as u32);
|
||||
if this.decls.kinds.p[member] == dk::assoc_type {
|
||||
val given := this.member_type(actual, this.decls.names.p[member]);
|
||||
continue if given == bt::error;
|
||||
this.resolve(member);
|
||||
val bounds: u32 = this.decls.children.p[member];
|
||||
for j in 0..this.decls.list_length(bounds) {
|
||||
val bound := this.substitute(this.decls.list_at(bounds, j as u32), from.span, to.span);
|
||||
this.unify_trait(bound, given, into, depth + 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
continue if this.decls.kinds.p[member] != dk::function;
|
||||
val found := this.find_member(this.reached_value(actual), this.decls.names.p[member]);
|
||||
# A method the compiler gives a built-in type, as a function's `invoke`.
|
||||
if found.decl == 0 && this.decls.has(member, df::method) {
|
||||
val given := this.builtin_method(this.reached_value(actual), this.decls.names.p[member]);
|
||||
if given != 0 {
|
||||
this.resolve(member);
|
||||
val want := this.substitute(this.decls.types.p[member], from.span, to.span);
|
||||
this.unify(this.past_receiver(want), this.past_receiver(given), into, depth + 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
continue if found.decl == 0 || this.decls.kinds.p[found.decl] != dk::function;
|
||||
this.resolve(member);
|
||||
val want := this.substitute(this.decls.types.p[member], from.span, to.span);
|
||||
val have := this.member_function_type(found.decl, found.owner);
|
||||
this.unify(want, have, into, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
#! What a value of the type is, read through a reference.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns What it reaches, or the type itself.
|
||||
def checker::reached_value(&mut this, kind: u32): u32 {
|
||||
return this.types.kind_of(kind) == ty::reference ? this.types.first_of(kind) : kind;
|
||||
}
|
||||
|
||||
#! The first type parameter nothing gave a type, or 0 where every one has one.
|
||||
#! @param this The checker.
|
||||
#! @param inferred What was inferred.
|
||||
#! @returns The type parameter's declaration, or 0.
|
||||
pub def checker::unbound(&this, inferred: &checker::inferred): u32 {
|
||||
for i in 0..inferred.params.length {
|
||||
return inferred.params[i] if inferred.binds[i] == checker::unknown_type;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! What a type parameter is inferred as while nothing has given it a type.
|
||||
pub const checker::unknown_type: u32 = 0xFFFFFFFF;
|
||||
|
||||
#! The type with the inferred type parameters replaced by their types.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param inferred What was inferred.
|
||||
#! @returns The type with them replaced.
|
||||
pub def checker::apply(&mut this, kind: u32, inferred: &checker::inferred): u32 {
|
||||
return this.substitute(kind, inferred.params.span, inferred.binds.span);
|
||||
}
|
||||
|
||||
#! The type with the type parameters inferred so far replaced by their types,
|
||||
#! and those nothing has given a type yet left as they are.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param inferred What was inferred.
|
||||
#! @returns The type with the known ones replaced.
|
||||
pub def checker::apply_known(&mut this, kind: u32, inferred: &checker::inferred): u32 {
|
||||
var from := vector::<u32>::new();
|
||||
var to := vector::<u32>::new();
|
||||
for i in 0..inferred.params.length {
|
||||
continue if inferred.binds[i] == checker::unknown_type;
|
||||
from.append(inferred.params[i]);
|
||||
to.append(inferred.binds[i]);
|
||||
}
|
||||
return this.substitute(kind, from.span, to.span);
|
||||
}
|
||||
|
||||
#! Whether a type holds a type parameter, so it belongs to a template rather
|
||||
#! than an instance.
|
||||
#! @param this The checker.
|
||||
#! @param kinds The types.
|
||||
#! @returns Whether any does.
|
||||
pub def checker::any_open(&this, kinds: span<u32>): bool {
|
||||
for &each in kinds {
|
||||
return true if this.types.is_open(each);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! How deeply type arguments nest within the type.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param depth How deeply it already nests.
|
||||
#! @returns How deeply its arguments nest.
|
||||
def checker::nesting(&this, kind: u32, depth: u32): u32 {
|
||||
return depth if depth > checker::max_nesting;
|
||||
|
||||
var deepest := depth;
|
||||
match this.types.kind_of(kind) {
|
||||
ty::reference, ty::pointer, ty::fixed => {
|
||||
return this.nesting(this.types.first_of(kind), depth + 1);
|
||||
}
|
||||
ty::function, ty::named, ty::pack => {
|
||||
for &part in this.types.parts_view(kind) {
|
||||
val inner := this.nesting(part, depth + 1);
|
||||
deepest = inner > deepest ? inner : deepest;
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
return deepest;
|
||||
}
|
||||
|
||||
#! The instance of the generic function given the type arguments, made where it
|
||||
#! is new and waiting for its body to be checked.
|
||||
#! @param this The checker.
|
||||
#! @param decl The generic function.
|
||||
#! @param args Its type arguments, its owner's first.
|
||||
#! @param node The node a note is placed at, where too many instances are
|
||||
#! made.
|
||||
#! @returns The instance, or 0 where the arguments hold type parameters, or the
|
||||
#! program has made as many instances as it may.
|
||||
pub def checker::instance_of(&mut this, decl: u32, args: span<u32>, node: u32): u32 {
|
||||
return 0 if this.any_open(args) || args.length == 0;
|
||||
for &each in args {
|
||||
return 0 if each == bt::error;
|
||||
}
|
||||
|
||||
val pack := this.types.pack(args, 0);
|
||||
val key := pair_key(decl, pack);
|
||||
if this.instanced.get(&key) is some(got) {
|
||||
return got;
|
||||
}
|
||||
|
||||
if this.instances.length as u32 > checker::max_instances {
|
||||
this.report(node, ec::limit, "The program makes more instances of generics than the checker allows.");
|
||||
return 0;
|
||||
}
|
||||
if this.nesting(pack, 0) > checker::max_nesting {
|
||||
this.report(node, ec::limit, "The type arguments nest too deeply, as a generic instantiating itself with ever larger types does.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
this.resolve(decl);
|
||||
val params: u32 = this.decls.params.p[decl];
|
||||
var from := this.decls.list_of(params);
|
||||
|
||||
val id := this.instances.length as u32;
|
||||
this.instances.append(instance {
|
||||
decl = decl,
|
||||
args = pack,
|
||||
kind = this.substitute(this.decls.types.p[decl], from.span, args),
|
||||
made_file = node != 0 ? this.at.file : 0,
|
||||
made_node = node,
|
||||
made_in = node != 0 ? this.at.instance : 0
|
||||
});
|
||||
this.instanced.set(key, id);
|
||||
this.wants.append(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
#! The instance of a trait's default method a type takes. A default of a trait
|
||||
#! that is only a bound is compiled for each type taking it, with the trait's
|
||||
#! parameters bound to its arguments, `self` to the type, and its own type
|
||||
#! parameters to those the call gives. Any other is compiled for the trait,
|
||||
#! given its arguments, with `self` the trait itself, called through as a
|
||||
#! reference to the trait is, and a default of a trait without parameters is
|
||||
#! then its own template.
|
||||
#! @param this The checker.
|
||||
#! @param method The default's declaration.
|
||||
#! @param trait_kind The trait declaring it, given its arguments.
|
||||
#! @param self_kind The type taking it.
|
||||
#! @param own The method's own type arguments, which a default
|
||||
#! compiled for each type takes after `self`.
|
||||
#! @returns The instance, or 0 where it is its own template or its arguments
|
||||
#! are not known.
|
||||
pub def checker::default_instance(&mut this, method: u32, trait_kind: u32, self_kind: u32, own: span<u32>): u32 {
|
||||
val holder := this.enclosing_trait(method);
|
||||
return 0 if holder == 0 || holder == method || this.types.kind_of(trait_kind) != ty::named;
|
||||
val params: u32 = this.decls.params.p[holder];
|
||||
# A constant's default is worked out for each type taking it, as a default
|
||||
# compiled for each type is.
|
||||
val typed := this.typed_default(method) || this.constant_default(method);
|
||||
return 0 if !typed && this.decls.list_length(params) == 0;
|
||||
var args := this.types.parts_of(trait_kind);
|
||||
if typed {
|
||||
return 0 if own.length != this.decls.list_length(this.decls.params.p[method]);
|
||||
args.append(self_kind);
|
||||
args.extend(own);
|
||||
}
|
||||
return 0 if args.length == 0 || this.any_open(args.span);
|
||||
for &each in args {
|
||||
return 0 if each == bt::error;
|
||||
}
|
||||
|
||||
val pack := this.types.pack(args.span, 0);
|
||||
val key := pair_key(method, pack);
|
||||
if this.instanced.get(&key) is some(got) {
|
||||
return got;
|
||||
}
|
||||
if this.instances.length as u32 > checker::max_instances {
|
||||
this.report(this.decls.nodes.p[method], ec::limit, "The program makes more instances of generics than the checker allows.");
|
||||
return 0;
|
||||
}
|
||||
if this.nesting(pack, 0) > checker::max_nesting {
|
||||
this.report(this.decls.nodes.p[method], ec::limit, "The type arguments nest too deeply, as a generic instantiating itself with ever larger types does.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
this.resolve(method);
|
||||
var from := vector::<u32>::new();
|
||||
var to := vector::<u32>::new();
|
||||
val trait_args := this.types.part_count(trait_kind);
|
||||
for i in 0..this.decls.list_length(params) {
|
||||
from.append(this.decls.list_at(params, i as u32));
|
||||
to.append(i < trait_args ? this.types.part_at(trait_kind, i as u32) : bt::error);
|
||||
}
|
||||
from.append(this.self_decl(holder));
|
||||
to.append(typed ? self_kind : trait_kind);
|
||||
if typed {
|
||||
from.extend(this.decls.list_view(this.decls.params.p[method]));
|
||||
to.extend(own);
|
||||
}
|
||||
|
||||
val id := this.instances.length as u32;
|
||||
this.instances.append(instance {
|
||||
decl = method,
|
||||
args = pack,
|
||||
kind = this.substitute(this.decls.types.p[method], from.span, to.span)
|
||||
});
|
||||
this.instanced.set(key, id);
|
||||
this.wants.append(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
#! The instance of a sealed trait's constant its arguments give, checked as a
|
||||
#! generic type's constant is, with the trait's parameters bound to them and
|
||||
#! each of its types the one they give.
|
||||
#! @param this The checker.
|
||||
#! @param decl The constant's declaration.
|
||||
#! @param owner The trait, given its arguments.
|
||||
#! @param node The node a note is placed at, where too many instances are
|
||||
#! made.
|
||||
#! @returns The instance, or 0 where the arguments hold type parameters, or the
|
||||
#! program has made as many instances as it may.
|
||||
pub def checker::sealed_instance(&mut this, decl: u32, owner: u32, node: u32): u32 {
|
||||
val args := this.types.parts_of(owner);
|
||||
return 0 if args.length == 0 || this.any_open(args.span);
|
||||
for &each in args.span {
|
||||
return 0 if each == bt::error;
|
||||
}
|
||||
|
||||
val pack := this.types.pack(args.span, 0);
|
||||
val key := pair_key(decl, pack);
|
||||
if this.instanced.get(&key) is some(got) {
|
||||
return got;
|
||||
}
|
||||
if this.instances.length as u32 > checker::max_instances {
|
||||
this.report(node, ec::limit, "The program makes more instances of generics than the checker allows.");
|
||||
return 0;
|
||||
}
|
||||
if this.nesting(pack, 0) > checker::max_nesting {
|
||||
this.report(node, ec::limit, "The type arguments nest too deeply, as a generic instantiating itself with ever larger types does.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
this.resolve(decl);
|
||||
val kind := this.sealed_given(this.decls.types.p[decl], owner, 0);
|
||||
val id := this.instances.length as u32;
|
||||
this.instances.append(instance {
|
||||
decl = decl,
|
||||
args = pack,
|
||||
kind = kind,
|
||||
made_file = node != 0 ? this.at.file : 0,
|
||||
made_node = node,
|
||||
made_in = node != 0 ? this.at.instance : 0
|
||||
});
|
||||
this.instanced.set(key, id);
|
||||
this.wants.append(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
#! The first node of a declaration's nodes, which are one run of indexes, the
|
||||
#! parser having made them as it went, the declaration's own last.
|
||||
#! @param this The checker.
|
||||
#! @param decl The declaration.
|
||||
#! @returns The node.
|
||||
pub def checker::first_node(&mut this, decl: u32): u32 {
|
||||
val held := this.decl_bases.at_or_zero(decl);
|
||||
return held if held != 0;
|
||||
|
||||
val saved := this.at;
|
||||
this.use_table(this.units[this.decls.files.p[decl]].table);
|
||||
val node: u32 = this.decls.nodes.p[decl];
|
||||
var lowest := node;
|
||||
var todo := vector::<u32>::new();
|
||||
todo.append(node);
|
||||
while todo.length != 0 {
|
||||
val at: u32 = todo.last;
|
||||
todo.truncate();
|
||||
lowest = at < lowest ? at : lowest;
|
||||
this.syn().children(at, &mut todo);
|
||||
}
|
||||
this.at = saved;
|
||||
|
||||
this.decl_bases.put(decl, lowest);
|
||||
return lowest;
|
||||
}
|
||||
|
||||
#! Checks the bodies of the instances the program uses, including those the
|
||||
#! bodies checked here use in turn.
|
||||
#! @param this The checker.
|
||||
pub def checker::check_wanted(&mut this): void {
|
||||
while this.wants_checked < this.wants.length {
|
||||
val id: u32 = this.wants[this.wants_checked];
|
||||
this.wants_checked++;
|
||||
this.check_instance(id);
|
||||
}
|
||||
}
|
||||
|
||||
#! Checks an instance's body over its template's nodes, with a result table of
|
||||
#! its own and the type parameters bound to its arguments, or of a variable of a
|
||||
#! generic type, its value.
|
||||
#! @param this The checker.
|
||||
#! @param id The instance's index.
|
||||
pub def checker::check_instance(&mut this, id: u32): void {
|
||||
val held: instance = this.instances[id];
|
||||
val decl := held.decl;
|
||||
return if held.table != 0 || this.decls.has(decl, df::failed);
|
||||
|
||||
val first := this.first_node(decl);
|
||||
val last: u32 = this.decls.nodes.p[decl];
|
||||
val table := this.add_table(this.decls.files.p[decl], first, last - first + 1, id);
|
||||
this.instances[id].table = table;
|
||||
|
||||
val saved := this.at;
|
||||
val errors := this.errors;
|
||||
this.enter_decl(decl, held.args);
|
||||
this.use_table(table);
|
||||
this.at.instance = id;
|
||||
if this.decls.kinds.p[decl] == dk::variable {
|
||||
this.check_variable_instance(id, decl, held.kind);
|
||||
} else {
|
||||
this.check_function_body(decl, held.kind);
|
||||
}
|
||||
this.leave(saved);
|
||||
this.instances[id].flawed = this.errors != errors;
|
||||
}
|
||||
|
||||
#! Checks an instance now, where what reads it needs it checked, as a constant
|
||||
#! is for its value, as checking a declaration of its own begins.
|
||||
#! @param this The checker.
|
||||
#! @param id The instance's index.
|
||||
pub def checker::check_instance_now(&mut this, id: u32): void {
|
||||
return if this.instances[id].table != 0;
|
||||
# Notes are its own, said now, not held with those of cases checked around it.
|
||||
val held := this.step_outside();
|
||||
val repeating := this.repeating;
|
||||
this.repeating = 0;
|
||||
this.check_instance(id);
|
||||
this.repeating = repeating;
|
||||
this.step_back(&held);
|
||||
}
|
||||
|
||||
#! Checks the instance of a variable or constant of a generic type: its value,
|
||||
#! as of the type its type arguments give, and of a constant, works out what
|
||||
#! the value is, which reading it reads. Each is a global of the program.
|
||||
#! @param this The checker.
|
||||
#! @param id The instance's index.
|
||||
#! @param decl The variable's declaration.
|
||||
#! @param kind The instance's type.
|
||||
def checker::check_variable_instance(&mut this, id: u32, decl: u32, kind: u32): void {
|
||||
this.checked_globals++;
|
||||
val node: u32 = this.decls.nodes.p[decl];
|
||||
this.set_type(node, kind);
|
||||
val value := this.record(node, variable_slot::value);
|
||||
if value != 0 {
|
||||
this.check_expr(value, kind);
|
||||
this.set_flag(value, rf::checked);
|
||||
if this.decls.has(decl, df::constant) {
|
||||
this.instances[id].value = this.known_or_run(value, kind);
|
||||
}
|
||||
} else if !this.has_zero(kind, 0) {
|
||||
var text := string::from("`", nothing {});
|
||||
val named := this.describe(kind);
|
||||
text.append(named.span);
|
||||
text.append("` has no zero value, so the variable is given a value where it is declared.");
|
||||
this.report_text(node, ec::zero_value, &text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,650 @@
|
||||
#! @file incremental.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains checking again after an edit. While a declaration is checked, what
|
||||
#! it depends on is recorded: each declaration it resolves, and each name it
|
||||
#! looks up outside any scope, found or not. Once a file is parsed again, which
|
||||
#! keeps the nodes of the top-level declarations an edit did not touch, the
|
||||
#! checker keeps what it worked out of those, declares what is new, lets go of
|
||||
#! what is gone, and checks again only what depends on what changed. A changed
|
||||
#! declaration of the same kind, module, and name keeps its ID, so what other
|
||||
#! files worked out of it stays.
|
||||
|
||||
import std::containers;
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! Records that the declaration being checked depends on the declaration.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration depended on.
|
||||
pub def checker::depend(&mut this, id: u32): void {
|
||||
val from := this.top_decl(this.at.decl);
|
||||
return if from == 0 || id == 0 || from == id;
|
||||
this.reach(id);
|
||||
return if !this.tracks;
|
||||
this.add_dependency(from, id);
|
||||
}
|
||||
|
||||
#! Records that checking reached a declaration, which is checked in its turn,
|
||||
#! where it was not reached before.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
pub def checker::reach(&mut this, id: u32): void {
|
||||
if id as u64 >= this.depended.length {
|
||||
this.depended.resize(this.decls.count > id as u64 ? this.decls.count : id as u64 + 1);
|
||||
}
|
||||
return if this.depended[id];
|
||||
this.depended[id] = true;
|
||||
this.queued.append(id);
|
||||
}
|
||||
|
||||
#! Whether any declaration depends on a declaration, as resolving it.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns Whether one does.
|
||||
pub def checker::is_depended(&this, id: u32): bool {
|
||||
return (id as u64) < this.depended.length && this.depended[id];
|
||||
}
|
||||
|
||||
#! Records that the declaration being checked looked up the name outside any
|
||||
#! scope, so a declaration of the name made later may be what it means.
|
||||
#! @param this The checker.
|
||||
#! @param name The ID of the name.
|
||||
pub def checker::depend_name(&mut this, name: u32): void {
|
||||
return if !this.tracks;
|
||||
val from := this.top_decl(this.at.decl);
|
||||
return if from == 0;
|
||||
this.add_dependency(from, name | checker::named_dependency);
|
||||
}
|
||||
|
||||
#! What a dependency on a name, rather than a declaration, has set.
|
||||
pub const checker::named_dependency: u32 = 0x80000000;
|
||||
|
||||
def checker::add_dependency(&mut this, from: u32, to: u32): void {
|
||||
val key := pair_key(from, to);
|
||||
return if !this.dep_seen.insert(key, 1);
|
||||
this.dep_from.append(from);
|
||||
this.dep_to.append(to);
|
||||
}
|
||||
|
||||
#! The top-level declaration a declaration was declared with: itself, or the
|
||||
#! type a field or member is of, or the declaration a type parameter is of.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns The top-level declaration.
|
||||
pub def checker::top_decl(&this, id: u32): u32 {
|
||||
var at := id;
|
||||
var steps := 0;
|
||||
while at != 0 && steps < 8 {
|
||||
val node: u32 = this.decls.nodes.p[at];
|
||||
val top: u32 = this.decls.tops.p[at];
|
||||
return at if top == 0 || node == top;
|
||||
at = this.decls.owners.p[at];
|
||||
steps++;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
#! A top-level declaration of a file, and the module it is declared in.
|
||||
struct checker::placed {
|
||||
var node: u32; #! The declaration's node.
|
||||
var home: u32; #! The module it is declared in.
|
||||
var external: bool; #! Whether it is in an `extern module`.
|
||||
}
|
||||
|
||||
#! The top-level declarations the file declares, in the branches of `when` the
|
||||
#! target takes, each with the module it is declared in, noting the libraries
|
||||
#! its `extern import`s name anew.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @returns Each declaration's node and module, in order.
|
||||
def checker::placements(&mut this, file: u32): vector<checker::placed> {
|
||||
var kept := vector::<file_node>::new();
|
||||
for &each in this.libraries {
|
||||
if each.file != file {
|
||||
kept.append(each);
|
||||
}
|
||||
}
|
||||
this.libraries = move(&mut kept);
|
||||
|
||||
var made := vector::<checker::placed>::new();
|
||||
val decls := this.left_of(this.units[file].root);
|
||||
var home: u32 = 0;
|
||||
var active := true;
|
||||
var external := false;
|
||||
for &node in this.run_view(decls) {
|
||||
match this.kind_of(node) {
|
||||
nk::import_decl => {}
|
||||
nk::extern_import => {
|
||||
if this.right_of(node) == 0 || this.target_holds(this.right_of(node)) {
|
||||
this.add_library(file, node);
|
||||
}
|
||||
}
|
||||
nk::module_decl, nk::extern_module => {
|
||||
home = this.module_path(this.left_of(node), true);
|
||||
active = this.right_of(node) == 0 || this.target_holds(this.right_of(node));
|
||||
external = this.kind_of(node) == nk::extern_module;
|
||||
if external {
|
||||
this.check_section(node);
|
||||
}
|
||||
}
|
||||
else => {
|
||||
if active {
|
||||
this.place_top(node, home, external, &mut made);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
def checker::place_top(&mut this, node: u32, home: u32, external: bool, into: &mut vector<checker::placed>): void {
|
||||
match this.kind_of(node) {
|
||||
nk::when_block => {
|
||||
val chosen := this.when_branch(node);
|
||||
return if chosen == 0;
|
||||
val decls := this.left_of(chosen);
|
||||
for &node in this.run_view(decls) {
|
||||
this.place_top(node, home, external, into);
|
||||
}
|
||||
}
|
||||
nk::error => {}
|
||||
else => into.append(checker::placed { node = node, home = home, external = external });
|
||||
}
|
||||
}
|
||||
|
||||
#! The key a top-level declaration is matched by across an edit: its kind,
|
||||
#! module, and name, each whole, so no two declarations differing in any share
|
||||
#! one.
|
||||
struct checker::match_key : hashable, equatable {
|
||||
var kind: dk; #! Its kind.
|
||||
var home: u32; #! The module it is declared in.
|
||||
var name: u32; #! The ID of its name.
|
||||
}
|
||||
|
||||
#! Hashes the key, as the dictionary of keys asks.
|
||||
#! @param this The key.
|
||||
#! @returns Its hash code.
|
||||
pub def checker::match_key::hash(&this): u64 {
|
||||
return mix(pair_key(this.home, this.name), this.kind as u64);
|
||||
}
|
||||
|
||||
#! Whether two keys are of the same kind, module, and name.
|
||||
#! @param this The key.
|
||||
#! @param other The other key.
|
||||
#! @returns Whether they are.
|
||||
pub def checker::match_key::equals(&this, other: &checker::match_key): bool {
|
||||
return this.kind == other.kind && this.home == other.home && this.name == other.name;
|
||||
}
|
||||
|
||||
#! Checks the file again once it is parsed again, as `syntax::reparse` does,
|
||||
#! checking again only what the edit affects.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @param source The file's text as it was edited, which its tokens are of
|
||||
#! now.
|
||||
#! @param root The file's `document` node, as parsing it again gave it.
|
||||
#! @returns How many top-level declarations were checked again.
|
||||
pub def checker::update(&mut this, file: u32, source: &source, root: u32): u32 {
|
||||
val saved := this.at;
|
||||
this.units[file].source = &source;
|
||||
this.units[file].root = root;
|
||||
|
||||
# Parsing the file whole again made every node anew, so none of the old ones
|
||||
# is kept, however their indexes fall.
|
||||
val whole := this.units[file].syntax.parses != this.units[file].parses;
|
||||
this.units[file].parses = this.units[file].syntax.parses;
|
||||
|
||||
# The nodes kept keep their results where they are. Those an edit added, past
|
||||
# the file's last, have a table of their own, and a file parsed whole again a
|
||||
# table of all of them.
|
||||
val count := this.units[file].syntax.node_count as u32;
|
||||
val last: rtable = this.tables.p[this.units[file].table];
|
||||
if whole {
|
||||
val fresh := this.add_table(file, 0, count, 0);
|
||||
this.units[file].table = fresh;
|
||||
this.units[file].segments.clear();
|
||||
this.units[file].segments.append(fresh);
|
||||
} else if count > last.base + last.count {
|
||||
val fresh := this.add_table(file, last.base + last.count, count - last.base - last.count, 0);
|
||||
this.units[file].table = fresh;
|
||||
this.units[file].segments.append(fresh);
|
||||
}
|
||||
this.use_table(this.units[file].table);
|
||||
|
||||
# The top-level declarations the file has now, and those it had.
|
||||
val placed := this.placements(file);
|
||||
var now := dict::<u32, u32>::new(); # Each top-level node now, with its module.
|
||||
for &each in placed {
|
||||
now.set(each.node, each.home);
|
||||
}
|
||||
|
||||
var kept := dict::<u32, u32>::new(); # The top-level nodes kept, and their declarations.
|
||||
var gone := vector::<u32>::new(); # The top-level declarations whose nodes are gone.
|
||||
for &decl in this.decls.by_file.of(file) {
|
||||
continue if this.decls.kinds.p[decl] == dk::none || this.decls.kinds.p[decl] == dk::builtin;
|
||||
val top: u32 = this.decls.tops.p[decl];
|
||||
continue if top == 0 || this.decls.nodes.p[decl] != top;
|
||||
if !whole && now.contains(&top) && this.decl_kind(top) == this.decls.kinds.p[decl] {
|
||||
if !kept.contains(&top) {
|
||||
kept.set(top, decl);
|
||||
}
|
||||
} else {
|
||||
gone.append(decl);
|
||||
}
|
||||
}
|
||||
|
||||
# A top-level declaration gone whose kind, module, and name a new one has
|
||||
# keeps its ID, so what other files worked out of it stays.
|
||||
var reusable := dict::<checker::match_key, u32>::new();
|
||||
for &decl in gone {
|
||||
val key := checker::match_key {
|
||||
kind = this.decls.kinds.p[decl as u32],
|
||||
home = this.decls.modules.p[decl as u32],
|
||||
name = this.decls.names.p[decl as u32]
|
||||
};
|
||||
if !reusable.contains(&key) {
|
||||
reusable.set(key, decl);
|
||||
}
|
||||
}
|
||||
|
||||
var changed := vector::<u32>::new();
|
||||
var olds := dict::<u32, u32>::new(); # The type each reused declaration had.
|
||||
for &decl in gone {
|
||||
this.forget_members(decl, file);
|
||||
}
|
||||
|
||||
this.pending.clear();
|
||||
this.at.scope = 0;
|
||||
var made := vector::<u32>::new();
|
||||
for &each in placed {
|
||||
continue if kept.contains(&each.node);
|
||||
this.at.home = each.home;
|
||||
val kind := this.decl_kind(each.node);
|
||||
val name := this.name_of(each.node);
|
||||
val key := checker::match_key { kind = kind, home = each.home, name = name };
|
||||
if reusable.contains(&key) {
|
||||
val decl: u32 = reusable[key];
|
||||
reusable.remove(&key);
|
||||
olds.set(decl, this.decls.types.p[decl]);
|
||||
this.external = each.external;
|
||||
this.redeclare(decl, each.node, each.home);
|
||||
this.external = false;
|
||||
made.append(decl);
|
||||
continue;
|
||||
}
|
||||
|
||||
this.decls.top = each.node;
|
||||
this.external = each.external;
|
||||
val decl := this.declare_decl(each.node, 0);
|
||||
this.external = false;
|
||||
this.decls.top = 0;
|
||||
if decl != 0 {
|
||||
made.append(decl);
|
||||
changed.append(decl);
|
||||
}
|
||||
}
|
||||
|
||||
# What is gone and not reused leaves every scope it was in.
|
||||
for &each in gone {
|
||||
val decl: u32 = each;
|
||||
continue if olds.contains(&decl);
|
||||
this.forget(decl);
|
||||
changed.append(decl);
|
||||
}
|
||||
|
||||
this.relink(file);
|
||||
this.forget_caches();
|
||||
|
||||
# Checking again: what is new or reused, then whatever depends on what
|
||||
# changed, and on what that changes in turn.
|
||||
var checked := hashset::<u32>::new();
|
||||
var queue := vector::<u32>::new();
|
||||
queue.extend(made.span);
|
||||
var at: u64 = 0;
|
||||
var rounds := 0;
|
||||
while rounds < 64 {
|
||||
rounds++;
|
||||
while at < queue.length {
|
||||
val decl: u32 = queue[at];
|
||||
at++;
|
||||
continue if checked.contains(&decl) || this.decls.kinds.p[decl] == dk::none;
|
||||
checked.insert(decl);
|
||||
|
||||
val before := olds.get_or(&decl, this.decls.types.p[decl]);
|
||||
this.check_again(decl);
|
||||
val after: u32 = this.decls.types.p[decl];
|
||||
if !olds.contains(&decl) || before != after || this.is_type_decl(decl) {
|
||||
changed.append(decl);
|
||||
}
|
||||
}
|
||||
|
||||
# Whatever depends on what changed is checked again.
|
||||
val queued := queue.length;
|
||||
var names := hashset::<u32>::new();
|
||||
var ids := hashset::<u32>::new();
|
||||
for &decl in changed {
|
||||
ids.insert(decl);
|
||||
names.insert(this.decls.names.p[decl as u32]);
|
||||
}
|
||||
changed.clear();
|
||||
for i in 0..this.dep_from.length {
|
||||
val to: u32 = this.dep_to[i];
|
||||
val from: u32 = this.dep_from[i];
|
||||
val hit := (to & checker::named_dependency) != 0
|
||||
? names.contains(&(to & ~checker::named_dependency))
|
||||
: ids.contains(&to);
|
||||
if hit && !checked.contains(&from) {
|
||||
queue.append(from);
|
||||
}
|
||||
}
|
||||
break if queue.length == queued;
|
||||
}
|
||||
|
||||
this.check_reached();
|
||||
this.recount_notes();
|
||||
this.at = saved;
|
||||
return checked.length as u32;
|
||||
}
|
||||
|
||||
#! Lets go of what a top-level declaration declared within it: its members,
|
||||
#! its type parameters, and what the checker made of it, which declaring it
|
||||
#! again makes anew.
|
||||
#! @param this The checker.
|
||||
#! @param decl The top-level declaration.
|
||||
#! @param file The file it is declared in.
|
||||
def checker::forget_members(&mut this, decl: u32, file: u32): void {
|
||||
val top: u32 = this.decls.tops.p[decl];
|
||||
for &each in this.decls.by_file.of(file) {
|
||||
continue if each == decl || this.decls.tops.p[each] != top;
|
||||
this.forget(each);
|
||||
}
|
||||
this.decls.children.p[decl] = 0;
|
||||
}
|
||||
|
||||
#! Lets go of a declaration: it leaves the module or type declaring it, and is
|
||||
#! no longer anything.
|
||||
#! @param this The checker.
|
||||
#! @param decl The declaration.
|
||||
def checker::forget(&mut this, decl: u32): void {
|
||||
val kind: dk = this.decls.kinds.p[decl];
|
||||
return if kind == dk::none;
|
||||
|
||||
val owner: u32 = this.decls.owners.p[decl];
|
||||
val name: u32 = this.decls.names.p[decl];
|
||||
if owner != 0 && (kind != dk::type_param) {
|
||||
this.leave_type(owner, decl);
|
||||
} else if kind != dk::type_param {
|
||||
val home: u32 = this.decls.modules.p[decl];
|
||||
val key := pair_key(home, name);
|
||||
this.unchain(this.modules.decl(home, name), decl, key, chained::modules);
|
||||
}
|
||||
|
||||
this.decls.kinds.p[decl] = dk::none;
|
||||
this.decls.overloads.p[decl] = 0;
|
||||
this.decls.tops.p[decl] = 0;
|
||||
}
|
||||
|
||||
#! Takes a member out of its type's members, or out of the types declared
|
||||
#! through its type.
|
||||
#! @param this The checker.
|
||||
#! @param owner The type's declaration.
|
||||
#! @param decl The member's declaration.
|
||||
def checker::leave_type(&mut this, owner: u32, decl: u32): void {
|
||||
val name: u32 = this.decls.names.p[decl];
|
||||
val key := pair_key(owner, name);
|
||||
val added := this.decls.added_member(owner, name);
|
||||
if added != 0 && !this.in_core(this.decls.modules.p[decl]) {
|
||||
this.unchain(added, decl, key, chained::added);
|
||||
return;
|
||||
}
|
||||
if this.decls.nests(decl) {
|
||||
if this.decls.member_in(owner, name, true) == decl {
|
||||
this.decls.nested.remove(&key);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.unchain(this.decls.member_in(owner, name, false), decl, key, chained::members);
|
||||
}
|
||||
|
||||
#! Which dictionary holds the first of a chain of overloads by its key.
|
||||
enum chained : u8 {
|
||||
members, #! A type's members, `decls.members`.
|
||||
added, #! Those declared outside `core` on `core`'s type, `decls.added`.
|
||||
modules, #! A module's, `modules.decls`.
|
||||
}
|
||||
|
||||
#! Takes the declaration out of the chain of overloads beginning with `first`,
|
||||
#! which the dictionary holds by the key.
|
||||
#! @param this The checker.
|
||||
#! @param first The first of the overloads, or 0.
|
||||
#! @param decl The declaration to take out.
|
||||
#! @param key The key the dictionary holds the first by.
|
||||
#! @param held Which dictionary that is.
|
||||
def checker::unchain(&mut this, first: u32, decl: u32, key: u64, held: chained): void {
|
||||
return if first == 0;
|
||||
if first == decl {
|
||||
val next: u32 = this.decls.overloads.p[decl];
|
||||
var dict := match held {
|
||||
chained::members => &mut this.decls.members,
|
||||
chained::added => &mut this.decls.added,
|
||||
chained::modules => &mut this.modules.decls
|
||||
};
|
||||
if next == 0 { dict.remove(&key); } else { dict.set(key, next); }
|
||||
return;
|
||||
}
|
||||
|
||||
var walk := this.decls.walk_overloads(first, 4096);
|
||||
while true {
|
||||
val at := walk.next();
|
||||
break if at == 0;
|
||||
if this.decls.overloads.p[at] == decl {
|
||||
val after: u32 = this.decls.overloads.p[decl];
|
||||
this.decls.overloads.p[at] = after;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Declares a top-level declaration again, at its new node, keeping its ID: it
|
||||
#! leaves where it was declared and is declared there again, with its members
|
||||
#! and type parameters made anew.
|
||||
#! @param this The checker.
|
||||
#! @param decl The declaration.
|
||||
#! @param node Its new node.
|
||||
#! @param home The module it is declared in.
|
||||
def checker::redeclare(&mut this, decl: u32, node: u32, home: u32): void {
|
||||
val kind: dk = this.decls.kinds.p[decl];
|
||||
val owner: u32 = this.decls.owners.p[decl];
|
||||
val name: u32 = this.decls.names.p[decl];
|
||||
if owner != 0 {
|
||||
this.leave_type(owner, decl);
|
||||
} else {
|
||||
val key := pair_key(this.decls.modules.p[decl], name);
|
||||
this.unchain(this.modules.decl(this.decls.modules.p[decl], name), decl, key, chained::modules);
|
||||
}
|
||||
|
||||
this.decls.renew(decl, node, home);
|
||||
this.decl_bases.put(decl, 0);
|
||||
this.decl_values.put(decl, 0);
|
||||
|
||||
val flags := this.node_flags(node);
|
||||
if (flags & nf::exported as u32) != 0 { this.decls.mark(decl, df::exported); }
|
||||
if (flags & nf::mutable as u32) != 0 { this.decls.mark(decl, df::mutable); }
|
||||
if (flags & nf::constant as u32) != 0 { this.decls.mark(decl, df::constant); }
|
||||
this.mark_attributes(decl, node);
|
||||
if this.external {
|
||||
this.mark_external(decl, kind, node);
|
||||
}
|
||||
if kind == dk::function {
|
||||
this.mark_signature(decl, node);
|
||||
}
|
||||
|
||||
this.decls.top = node;
|
||||
if this.receiver_of(node) != 0 {
|
||||
this.pending.append(decl);
|
||||
} else {
|
||||
this.add_global(decl);
|
||||
}
|
||||
val own := this.declare_params(this.own_params_of(node), decl);
|
||||
this.record_shape(decl, this.own_params_of(node));
|
||||
if this.receiver_of(node) == 0 {
|
||||
this.set_params(decl, own.span);
|
||||
} else {
|
||||
this.decls.params.p[decl] = this.decls.make_list(own.span);
|
||||
}
|
||||
this.declare_members(decl, node);
|
||||
this.decls.top = 0;
|
||||
}
|
||||
|
||||
#! Links what was declared anew: the file's imports, the declarations made
|
||||
#! through types, and the types of new type declarations.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
def checker::relink(&mut this, file: u32): void {
|
||||
var imports := vector::<u32>::new();
|
||||
val decls := this.left_of(this.units[file].root);
|
||||
for &node in this.run_view(decls) {
|
||||
continue if this.kind_of(node) != nk::import_decl;
|
||||
val found := this.module_path(this.left_of(node), false);
|
||||
if found == 0 {
|
||||
this.report(node, ec::undefined, "No file declares this module.");
|
||||
continue;
|
||||
}
|
||||
imports.append(found);
|
||||
}
|
||||
val run := this.decls.make_list(imports.span);
|
||||
this.units[file].imports = run;
|
||||
|
||||
this.attach_pending();
|
||||
this.pending.clear();
|
||||
this.give_accessors_params();
|
||||
this.name_types();
|
||||
}
|
||||
|
||||
#! Lets go of what was worked out of types and traits, which what changed may
|
||||
#! have changed, to be worked out again as it is needed.
|
||||
#! @param this The checker.
|
||||
def checker::forget_caches(&mut this): void {
|
||||
this.satisfied.clear();
|
||||
this.types.forget();
|
||||
this.only_bounds.clear();
|
||||
this.outers.clear();
|
||||
this.composed.resize(0);
|
||||
for id in 1..this.decls.count {
|
||||
this.decls.flags.p[id] &= ~(df::composed as u32);
|
||||
}
|
||||
this.known.clear();
|
||||
this.worked_again.clear();
|
||||
this.forget_paths();
|
||||
}
|
||||
|
||||
#! Forgets what the paths an argument's checking looked at name, which checking
|
||||
#! again finds anew.
|
||||
#! @param this The checker.
|
||||
def checker::forget_paths(&mut this): void {
|
||||
this.named_ids.clear();
|
||||
this.named_paths.resize(1);
|
||||
this.chosen_arms.clear();
|
||||
}
|
||||
|
||||
#! Checks a top-level declaration again: its notes are let go of, what it and
|
||||
#! what it declares worked out is let go of, and it is resolved and checked as
|
||||
#! though for the first time, with the instances of it the program uses.
|
||||
#! @param this The checker.
|
||||
#! @param decl The top-level declaration.
|
||||
def checker::check_again(&mut this, decl: u32): void {
|
||||
val top: u32 = this.decls.tops.p[decl];
|
||||
val file: u32 = this.decls.files.p[decl];
|
||||
this.forget_notes(decl);
|
||||
|
||||
var family := vector::<u32>::new();
|
||||
for &each in this.decls.by_file.of(file) {
|
||||
if each == decl || (top != 0 && this.decls.tops.p[each] == top && this.decls.kinds.p[each] != dk::none) {
|
||||
family.append(each);
|
||||
}
|
||||
}
|
||||
|
||||
for &each in family {
|
||||
this.decls.states.p[each as u32] = ds::unresolved;
|
||||
this.decls.flags.p[each as u32] &= ~(df::checked as u32 | df::failed as u32);
|
||||
}
|
||||
this.clear_results(decl);
|
||||
this.forget_paths();
|
||||
for &each in family {
|
||||
this.resolve(each);
|
||||
}
|
||||
for &each in family {
|
||||
this.check_decl(each);
|
||||
}
|
||||
|
||||
# The instances of what is checked again are checked again with it.
|
||||
for i in 1..this.instances.length {
|
||||
val held: instance = this.instances[i];
|
||||
continue if !family.span.contains(&held.decl);
|
||||
var from := this.decls.list_of(this.decls.params.p[held.decl]);
|
||||
var args := this.types.parts_of(held.args);
|
||||
val kind := this.substitute(this.decls.types.p[held.decl], from.span, args.span);
|
||||
this.instances[i].kind = kind;
|
||||
this.instances[i].table = 0;
|
||||
this.instances[i].value = 0;
|
||||
this.wants.append(i as u32);
|
||||
}
|
||||
}
|
||||
|
||||
#! Lets go of what was worked out of a declaration's nodes in its file's tables.
|
||||
#! @param this The checker.
|
||||
#! @param decl The top-level declaration.
|
||||
def checker::clear_results(&mut this, decl: u32) : void {
|
||||
val file: u32 = this.decls.files.p[decl];
|
||||
val first := this.first_node(decl);
|
||||
val last: u32 = this.decls.nodes.p[decl];
|
||||
for node in first..(last + 1) {
|
||||
val n := node as u32;
|
||||
val table := this.file_table_of(file, n);
|
||||
continue if table == checker::no_table;
|
||||
val held: rtable = this.tables.p[table];
|
||||
this.drop_results(held.offset as u64 + (n - held.base) as u64);
|
||||
val key := table_node { table = table, node = n };
|
||||
this.reflections.remove(&key);
|
||||
this.lengths.remove(&key);
|
||||
this.called.remove(&key);
|
||||
}
|
||||
}
|
||||
|
||||
#! Lets go of the notes the checker made checking the declaration, or what it
|
||||
#! declares.
|
||||
#! @param this The checker.
|
||||
#! @param decl The top-level declaration.
|
||||
def checker::forget_notes(&mut this, decl: u32): void {
|
||||
# Those kept are moved down over those let go of, which are closed as they
|
||||
# are written over, and those left past the last kept are closed with them.
|
||||
var kept: u64 = 0;
|
||||
for i in 0..this.sink.notes.length {
|
||||
val origin := this.sink.notes[i].origin;
|
||||
continue if origin != 0 && this.top_decl(origin) == decl;
|
||||
if kept != i {
|
||||
this.sink.notes[kept] = move(&mut this.sink.notes[i]);
|
||||
}
|
||||
kept++;
|
||||
}
|
||||
while this.sink.notes.length > kept {
|
||||
this.sink.notes.truncate();
|
||||
}
|
||||
}
|
||||
|
||||
#! Counts the notes the checker made, which the most it makes bounds, and
|
||||
#! whether the program is valid, once some are made again.
|
||||
#! @param this The checker.
|
||||
def checker::recount_notes(&mut this): void {
|
||||
var count: u32 = 0;
|
||||
for i in 0..this.sink.notes.length {
|
||||
if this.sink.notes[i].origin != 0 {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
this.sink.count = count;
|
||||
this.failed = count != 0;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,677 @@
|
||||
#! @file lifetimes.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the lifetime warnings: where a function keeps a reference past what
|
||||
#! it refers to, as the checker is sure of it. A reference, a view, or an
|
||||
#! anonymous function refers to the locals it names, which end where their
|
||||
#! scopes do, and to a value a statement makes, which ends with the statement
|
||||
#! unless a name is bound to it. Nothing keeps what they refer to alive, so one
|
||||
#! that outlives it dangles, which is the program's mistake, as in C++.
|
||||
#!
|
||||
#! The checker is sure of what it sees written: a value refers to a local where
|
||||
#! it is `&` of the local's own storage, a `val` bound to such a value, or a
|
||||
#! `var` no assignment changes, an anonymous function naming the local, a
|
||||
#! struct, union, or array literal holding one, or a call keeping one. A call
|
||||
#! keeps an argument where the function says so of its parameter, with
|
||||
#! `@kept`, as `function::new` and `thread::spawn` do, or where the parameter's
|
||||
#! type is a type parameter of the function's own that its result's type
|
||||
#! holds, as an adapter holding the function it is given does. It is sure the
|
||||
#! value outlives the local where it is returned from the function declaring
|
||||
#! the local, or written into a global, into what a parameter refers or points
|
||||
#! to, or into a local of a scope around the local's. A value a statement makes
|
||||
#! outlives it where it is returned, written anywhere, or held in a literal a
|
||||
#! name is bound to. What else a call does with what it is given, the checker
|
||||
#! does not follow, and says nothing of.
|
||||
#!
|
||||
#! What the warnings need is recorded as the checker checks a function's body,
|
||||
#! rather than by walking it again: the scope each local is declared in, and
|
||||
#! each value returned, written, or bound to a name, with the scopes around it.
|
||||
#! Once the body checks, each is looked at in the order it is written. Only what
|
||||
#! checking decides is recorded: not what is within an `if const` or `for const`
|
||||
#! only evaluating or an instance decides, nor a pass of `for const`, whose
|
||||
#! results are its own.
|
||||
#!
|
||||
#! Only functions that are no generic's are recorded, so each warning is said
|
||||
#! once, and `@allow` quiets those of a function.
|
||||
|
||||
import std::containers;
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! What a value refers to, as far as the checker is sure.
|
||||
enum lifetime_kind : u8 {
|
||||
none, #! Nothing it is sure of.
|
||||
local, #! A local, as `local` says.
|
||||
temporary, #! A value its statement makes.
|
||||
}
|
||||
|
||||
#! What a value refers to, the local where it is one, and the node of the
|
||||
#! value saying so: the `&`, the slice, the anonymous function, or the `val`
|
||||
#! named, within any literal holding it.
|
||||
struct lifetime_ref {
|
||||
var kind: lifetime_kind; #! What it is.
|
||||
var local: u32; #! The node declaring the local, where it is one.
|
||||
var at: u32; #! The node saying so, which a warning is made at.
|
||||
}
|
||||
|
||||
#! What the checker records of the bodies it checks for the warnings, and what
|
||||
#! looking at a body's record holds. A body's check may start another's, as
|
||||
#! evaluating a constant checks the function it calls, so each body checked
|
||||
#! has a session of its own on a stack, and what it records follows what the
|
||||
#! sessions around it recorded.
|
||||
struct lifetime_walk {
|
||||
var sessions: vector<lifetime_session>; #! The bodies being recorded, innermost last.
|
||||
var opened: vector<lifetime_scope>; #! The scopes entered while recording, the first standing for none.
|
||||
var events: vector<lifetime_event>; #! What was returned, written, or bound while recording.
|
||||
var notes: keyed<table_node>; #! The scope each local is declared in, by its table and node.
|
||||
var current: u32; #! The innermost scope entered, of the innermost session.
|
||||
var table: u32; #! The table the innermost session's body is checked in, outside which nothing is recorded.
|
||||
|
||||
# Where looking at an event is: the scopes around it, outermost first, and
|
||||
# the function and anonymous functions among them.
|
||||
var scopes: vector<u32>; #! The scopes' nodes.
|
||||
var functions: vector<u32>; #! The functions' nodes.
|
||||
}
|
||||
|
||||
#! A body being recorded: the function, and where its record begins.
|
||||
struct lifetime_session {
|
||||
var decl: u32; #! The function's declaration.
|
||||
var table: u32; #! The table its body is checked in.
|
||||
var scopes: u32; #! Where its scopes begin in `opened`.
|
||||
var events: u32; #! Where its events begin in `events`.
|
||||
var current: u32; #! The innermost scope of the session around it, as it was.
|
||||
var outer: u32; #! The table of the session around it.
|
||||
}
|
||||
|
||||
#! A scope entered while recording: a function, block, loop, `if`, arm, or
|
||||
#! anonymous function.
|
||||
struct lifetime_scope {
|
||||
var node: u32; #! The node opening it.
|
||||
var parent: u32; #! The scope around it, in `opened`, or 0.
|
||||
var function: bool; #! Whether it is a function's or an anonymous function's.
|
||||
}
|
||||
|
||||
#! What a value returned, written, or bound is, as an event of a record.
|
||||
enum lifetime_kind_of_event : u8 {
|
||||
returned, #! A `return`, or the value an anonymous function's body is.
|
||||
written, #! An assignment.
|
||||
bound, #! A local declared with a value.
|
||||
}
|
||||
|
||||
#! A value returned, written, or bound, and the scope it was checked in.
|
||||
struct lifetime_event {
|
||||
var node: u32; #! The `return_stmt`, `lambda`, `assign`, or `variable` node.
|
||||
var kind: lifetime_kind_of_event; #! Which it is.
|
||||
var scope: u32; #! The innermost scope around it, in `opened`.
|
||||
}
|
||||
|
||||
#! Where a local is declared: its scope, and the function or anonymous
|
||||
#! function that scope is in.
|
||||
struct lifetime_note {
|
||||
var scope: u32; #! The scope's node, or 0 where nothing was recorded of it.
|
||||
var owner: u32; #! The function's node.
|
||||
}
|
||||
|
||||
#! Makes a walk, which records nothing until a body begins.
|
||||
#! @returns The walk.
|
||||
def lifetime_walk::new(): lifetime_walk {
|
||||
var made := lifetime_walk {
|
||||
sessions = vector::<lifetime_session>::new(),
|
||||
opened = vector::<lifetime_scope>::new(),
|
||||
events = vector::<lifetime_event>::new(),
|
||||
notes = keyed::<table_node>::new(),
|
||||
scopes = vector::<u32>::new(),
|
||||
functions = vector::<u32>::new()
|
||||
};
|
||||
made.opened.append(lifetime_scope {});
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Where the local was declared, as the session looked at recorded it.
|
||||
#! @param this The walk.
|
||||
#! @param local The node declaring the local.
|
||||
#! @returns The note, or nothing noted where none was recorded.
|
||||
def lifetime_walk::note_of(&this, local: u32): lifetime_note {
|
||||
val at := this.notes.get_or(&table_node { table = this.table, node = local }, 0);
|
||||
return lifetime_note {} if at == 0;
|
||||
var owner := at;
|
||||
while owner != 0 && !this.opened[owner].function {
|
||||
owner = this.opened[owner].parent;
|
||||
}
|
||||
return lifetime_note { scope = this.opened[at].node, owner = this.opened[owner].node };
|
||||
}
|
||||
|
||||
#! Whether what is checked now is recorded: within a body a session records,
|
||||
#! in its own table, where checking decides it.
|
||||
#! @param this The checker.
|
||||
#! @returns Whether it is.
|
||||
@inline
|
||||
def checker::recording(&this): bool {
|
||||
return this.lifetimes.sessions.length != 0 && this.undecided == 0 && this.at.table == this.lifetimes.table;
|
||||
}
|
||||
|
||||
#! Begins recording a function's body, unless it is a generic's, which each
|
||||
#! instance would warn of again, or is `@allow`.
|
||||
#! @param this The checker.
|
||||
#! @param id The function's declaration, whose body is about to be checked.
|
||||
#! @returns Whether it is recorded, which `end_lifetimes` ends.
|
||||
pub def checker::begin_lifetimes(&mut this, id: u32): bool {
|
||||
return false if this.decls.has(id, df::generic) || this.decls.has(id, df::allows) || this.decls.has(id, df::accessor);
|
||||
val node: u32 = this.decls.nodes.p[id];
|
||||
return false if node == 0 || this.kind_of(node) != nk::function || this.at.instance != 0;
|
||||
val walk := &mut this.lifetimes;
|
||||
if walk.sessions.length == 0 {
|
||||
walk.notes.clear();
|
||||
}
|
||||
walk.sessions.append(lifetime_session {
|
||||
decl = id,
|
||||
table = this.at.table,
|
||||
scopes = walk.opened.length as u32,
|
||||
events = walk.events.length as u32,
|
||||
current = walk.current,
|
||||
outer = walk.table
|
||||
});
|
||||
walk.table = this.at.table;
|
||||
walk.current = 0;
|
||||
_ = this.lifetime_open(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Ends the record of the body last begun, warning of what it keeps past what
|
||||
#! it refers to where it checked.
|
||||
#! @param this The checker.
|
||||
#! @param checks Whether the body checked, without errors.
|
||||
pub def checker::end_lifetimes(&mut this, checks: bool): void {
|
||||
if checks {
|
||||
this.lifetime_warn();
|
||||
}
|
||||
val walk := &mut this.lifetimes;
|
||||
val held: lifetime_session = walk.sessions.last;
|
||||
walk.opened.resize(held.scopes as u64);
|
||||
walk.events.resize(held.events as u64);
|
||||
walk.current = held.current;
|
||||
walk.table = held.outer;
|
||||
walk.sessions.truncate();
|
||||
}
|
||||
|
||||
#! Enters the scope a node opens, where what is checked is recorded.
|
||||
#! @param this The checker.
|
||||
#! @param node The function, block, loop, `if`, arm, or anonymous function.
|
||||
#! @returns The scope it was within, which `lifetime_close` goes back to.
|
||||
def checker::lifetime_open(&mut this, node: u32): u32 {
|
||||
val walk := &mut this.lifetimes;
|
||||
val outer := walk.current;
|
||||
return outer if !this.recording();
|
||||
val kind := this.kind_of(node);
|
||||
walk.current = walk.opened.length as u32;
|
||||
walk.opened.append(lifetime_scope { node = node, parent = outer, function = kind == nk::function || kind == nk::lambda });
|
||||
return outer;
|
||||
}
|
||||
|
||||
#! Leaves the scope last entered.
|
||||
#! @param this The checker.
|
||||
#! @param outer The scope it was within, as `lifetime_open` gave it.
|
||||
def checker::lifetime_close(&mut this, outer: u32): void {
|
||||
this.lifetimes.current = outer;
|
||||
}
|
||||
|
||||
#! Makes a node a local, noting the scope it is declared in where what is
|
||||
#! checked is recorded.
|
||||
#! @param this The checker.
|
||||
#! @param node The node declaring the local.
|
||||
pub def checker::mark_local(&mut this, node: u32): void {
|
||||
this.set_flag(node, rf::local);
|
||||
if this.recording() {
|
||||
this.lifetimes.notes.set(table_node { table = this.at.table, node = node }, this.lifetimes.current);
|
||||
}
|
||||
}
|
||||
|
||||
#! Records a value returned, written, or bound, where what is checked is.
|
||||
#! @param this The checker.
|
||||
#! @param node The `return_stmt`, `lambda`, `assign`, or `variable` node.
|
||||
#! @param kind Which it is.
|
||||
def checker::lifetime_event(&mut this, node: u32, kind: lifetime_kind_of_event): void {
|
||||
return if !this.recording();
|
||||
this.lifetimes.events.append(lifetime_event { node = node, kind = kind, scope = this.lifetimes.current });
|
||||
}
|
||||
|
||||
#! An event's place in the order its body is written: after what it holds, and
|
||||
#! before what follows it.
|
||||
struct lifetime_order {
|
||||
var end: u32; #! The last token of its node.
|
||||
var start: u32; #! Its node's token.
|
||||
var index: u32; #! The event, in `events`.
|
||||
}
|
||||
|
||||
#! Warns of what the body last begun keeps past what it refers to, looking at
|
||||
#! each value it returned, wrote, or bound, in the order they are written, once
|
||||
#! each however many times checking reached it.
|
||||
#! @param this The checker.
|
||||
def checker::lifetime_warn(&mut this): void {
|
||||
val first: u32 = this.lifetimes.sessions.last.events;
|
||||
var order := vector::<lifetime_order>::new();
|
||||
for i in first..this.lifetimes.events.length as u32 {
|
||||
val node := this.lifetimes.events[i].node;
|
||||
order.append(lifetime_order { end = this.syn().node_end(node), start = this.token_of(node), index = i });
|
||||
}
|
||||
order.sort_by(def(a: &lifetime_order, b: &lifetime_order): bool {
|
||||
return a.end < b.end || a.end == b.end && (a.start > b.start || a.start == b.start && a.index < b.index);
|
||||
});
|
||||
|
||||
var last: u32 = 0;
|
||||
for i in 0..order.length {
|
||||
val held: lifetime_event = this.lifetimes.events[order[i].index];
|
||||
continue if i != 0 && held.node == last;
|
||||
last = held.node;
|
||||
|
||||
# The scopes around it, outermost first.
|
||||
val walk := &mut this.lifetimes;
|
||||
walk.scopes.clear();
|
||||
walk.functions.clear();
|
||||
var depth: u64 = 0;
|
||||
var at := held.scope;
|
||||
while at != 0 {
|
||||
depth++;
|
||||
at = walk.opened[at].parent;
|
||||
}
|
||||
walk.scopes.resize(depth);
|
||||
at = held.scope;
|
||||
while at != 0 {
|
||||
depth--;
|
||||
walk.scopes[depth] = walk.opened[at].node;
|
||||
at = walk.opened[at].parent;
|
||||
}
|
||||
for i in 0..walk.scopes.length {
|
||||
val scope := walk.scopes[i];
|
||||
val kind := this.kind_of(scope);
|
||||
if kind == nk::function || kind == nk::lambda {
|
||||
walk.functions.append(scope);
|
||||
}
|
||||
}
|
||||
|
||||
match held.kind {
|
||||
lifetime_kind_of_event::returned => {
|
||||
val value := this.kind_of(held.node) == nk::lambda ? this.record(held.node, lambda_slot::body) : this.left_of(held.node);
|
||||
this.lifetime_returned(walk, value);
|
||||
}
|
||||
lifetime_kind_of_event::written => {
|
||||
if this.token_kind(held.node) == tk::assign {
|
||||
this.lifetime_written(walk, this.left_of(held.node), this.right_of(held.node));
|
||||
}
|
||||
}
|
||||
lifetime_kind_of_event::bound => {
|
||||
# A name bound to a value its statement makes refers to it as long as
|
||||
# the name lives, but a literal holding a reference to one does not.
|
||||
val value := this.record(held.node, variable_slot::value);
|
||||
if value != 0 && this.kind_of(value) != nk::reference {
|
||||
val got := this.lifetime_of(walk, value, 0);
|
||||
if got.kind == lifetime_kind::temporary {
|
||||
this.warn(got.at, ec::lifetime,
|
||||
"The value referred to is made in the statement, and ends with it, so the value holding a reference to it outlives it. Bound to a name, as `val p := &point { x = 1 };`, a value lives as long as the name.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Warns where a value returned refers to a local of the function returning it,
|
||||
#! or to a value the `return` makes.
|
||||
#! @param this The checker.
|
||||
#! @param walk The walk.
|
||||
#! @param value The value returned, or 0.
|
||||
def checker::lifetime_returned(&mut this, walk: &mut lifetime_walk, value: u32): void {
|
||||
return if value == 0 || walk.functions.length == 0;
|
||||
val held := this.lifetime_of(walk, value, 0);
|
||||
if held.kind == lifetime_kind::temporary {
|
||||
this.warn(held.at, ec::lifetime, "The value returned refers to a value made in the statement, which ends with it.");
|
||||
return;
|
||||
}
|
||||
return if held.kind != lifetime_kind::local || walk.note_of(held.local).owner != walk.functions.last;
|
||||
var text := string::from("The value returned refers to `", nothing {});
|
||||
text.append(this.names.text(this.local_name(held.local)));
|
||||
text.append("`, which ends as the function returns.");
|
||||
this.warn_text(held.at, ec::lifetime, &text, held.local);
|
||||
}
|
||||
|
||||
#! Warns where a value written to a place refers to a local the place outlives,
|
||||
#! or to a value the statement makes.
|
||||
#! @param this The checker.
|
||||
#! @param walk The walk.
|
||||
#! @param target The place written.
|
||||
#! @param value The value written.
|
||||
def checker::lifetime_written(&mut this, walk: &mut lifetime_walk, target: u32, value: u32): void {
|
||||
return if this.kind_of(target) == nk::name && this.names.text(this.name_of(target)) == "_";
|
||||
val held := this.lifetime_of(walk, value, 0);
|
||||
if held.kind == lifetime_kind::temporary {
|
||||
this.warn(held.at, ec::lifetime,
|
||||
"The value referred to is made in the statement, and ends with it, so the reference written here outlives it. Bound to a name, as `val p := &point { x = 1 };`, a value lives as long as the name.");
|
||||
return;
|
||||
}
|
||||
return if held.kind != lifetime_kind::local;
|
||||
|
||||
# The local or global whose storage the place is within, and whether it is
|
||||
# reached through a reference or pointer.
|
||||
var at := target;
|
||||
var through := false;
|
||||
var steps := 0;
|
||||
while steps < 64 {
|
||||
steps++;
|
||||
val kind := this.kind_of(at);
|
||||
if kind == nk::member || kind == nk::index || kind == nk::slice {
|
||||
val object := this.left_of(at);
|
||||
val held_type := this.types.kind_of(this.unalias(this.type_at(object)));
|
||||
if kind == nk::member {
|
||||
through = through || held_type == ty::reference || held_type == ty::pointer;
|
||||
} else {
|
||||
through = through || held_type != ty::fixed;
|
||||
}
|
||||
at = object;
|
||||
continue;
|
||||
}
|
||||
if kind == nk::unary && this.token_kind(at) == tk::star {
|
||||
through = true;
|
||||
at = this.left_of(at);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return if this.kind_of(at) != nk::name;
|
||||
val ref := this.ref_at(at);
|
||||
return if ref == 0 || (ref & ref_type) != 0;
|
||||
|
||||
var outlives := false;
|
||||
var into := string::new(nothing {});
|
||||
if (ref & ref_local) == 0 {
|
||||
# A global outlives every local.
|
||||
outlives = this.decls.kinds.p[ref & ref_mask] == dk::variable;
|
||||
into.append("a global");
|
||||
} else {
|
||||
val root := ref & ref_mask;
|
||||
if through {
|
||||
# What a parameter refers or points to is the caller's, which outlives
|
||||
# every local of the function.
|
||||
val root_type := this.types.kind_of(this.unalias(this.type_at(root)));
|
||||
outlives = this.kind_of(root) == nk::param && (root_type == ty::reference || root_type == ty::pointer);
|
||||
into.append("what `");
|
||||
into.append(this.names.text(this.local_name(root)));
|
||||
into.append("` refers to");
|
||||
} else {
|
||||
# A local of a scope around the local's outlives it.
|
||||
val outer := this.lifetime_scope(walk, root);
|
||||
val inner := this.lifetime_scope(walk, held.local);
|
||||
outlives = outer >= 0 && inner >= 0 && outer < inner;
|
||||
into.append("`");
|
||||
into.append(this.names.text(this.local_name(root)));
|
||||
into.append("`");
|
||||
}
|
||||
}
|
||||
return if !outlives;
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.names.text(this.local_name(held.local)));
|
||||
text.append("` ends where its scope does, so the reference written to ");
|
||||
text.append(into.span);
|
||||
text.append(" outlives it.");
|
||||
this.warn_text(held.at, ec::lifetime, &text, held.local);
|
||||
}
|
||||
|
||||
#! Where among the scopes the walk is within a local is declared.
|
||||
#! @param this The checker.
|
||||
#! @param walk The walk.
|
||||
#! @param local The node declaring the local.
|
||||
#! @returns The index of its scope, or -1 where it is in none of them.
|
||||
def checker::lifetime_scope(&this, walk: &lifetime_walk, local: u32): i64 {
|
||||
val scope := walk.note_of(local).scope;
|
||||
return -1 if scope == 0;
|
||||
for i in 0..walk.scopes.length {
|
||||
return i as i64 if walk.scopes[i] == scope;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
#! What a value refers to, as far as the checker is sure.
|
||||
#! @param this The checker.
|
||||
#! @param walk The walk.
|
||||
#! @param node The value.
|
||||
#! @param depth How many `val`s it was followed through.
|
||||
#! @returns What it refers to.
|
||||
def checker::lifetime_of(&mut this, walk: &lifetime_walk, node: u32, depth: u32): lifetime_ref {
|
||||
val none := lifetime_ref { kind = lifetime_kind::none };
|
||||
return none if node == 0 || depth > 8;
|
||||
match this.kind_of(node) {
|
||||
nk::reference => {
|
||||
val inner := this.left_of(node);
|
||||
if this.is_place(inner) {
|
||||
val root := this.lifetime_root(inner);
|
||||
return root != 0 ? lifetime_ref { kind = lifetime_kind::local, local = root, at = node } : none;
|
||||
}
|
||||
# `&` of a reference refers to what it does, which is not known.
|
||||
val given := this.types.kind_of(this.unalias(this.type_at(inner)));
|
||||
return given == ty::reference ? none : lifetime_ref { kind = lifetime_kind::temporary, at = node };
|
||||
}
|
||||
nk::slice => {
|
||||
val object := this.left_of(node);
|
||||
val held := this.types.kind_of(this.unalias(this.type_at(object)));
|
||||
return none if held != ty::fixed || !this.is_place(object);
|
||||
val root := this.lifetime_root(object);
|
||||
return root != 0 ? lifetime_ref { kind = lifetime_kind::local, local = root, at = node } : none;
|
||||
}
|
||||
nk::name => {
|
||||
# A `val` refers to what its value does, as does a `var` no assignment
|
||||
# changes, and one bound to a value its statement makes refers to it as
|
||||
# long as it lives.
|
||||
val ref := this.ref_at(node);
|
||||
return none if (ref & ref_local) == 0;
|
||||
val local := ref & ref_mask;
|
||||
return none if this.kind_of(local) != nk::variable;
|
||||
return none if this.node_has(local, nf::mutable) && !this.lifetime_unwritten(walk, local);
|
||||
val value := this.record(local, variable_slot::value);
|
||||
return none if value == 0;
|
||||
val held := this.lifetime_of(walk, value, depth + 1);
|
||||
if held.kind == lifetime_kind::temporary {
|
||||
return this.kind_of(value) == nk::reference ? lifetime_ref { kind = lifetime_kind::local, local = local, at = node } : none;
|
||||
}
|
||||
return held.kind == lifetime_kind::none ? none : lifetime_ref { kind = held.kind, local = held.local, at = node };
|
||||
}
|
||||
nk::lambda => {
|
||||
# An anonymous function refers to the locals around it that it names.
|
||||
var held := this.lifetime_captured(walk, node);
|
||||
held.at = node;
|
||||
return held;
|
||||
}
|
||||
nk::call => {
|
||||
# A call refers to what each argument it keeps does.
|
||||
val args := this.right_of(node);
|
||||
for i in 0..this.run_length(args) {
|
||||
continue if !this.call_keeps(node, i as u32);
|
||||
val held := this.lifetime_of(walk, this.run_at(args, i as u32), depth + 1);
|
||||
return held if held.kind != lifetime_kind::none;
|
||||
}
|
||||
return none;
|
||||
}
|
||||
nk::fallback => {
|
||||
# `x else y` refers to what `x` does where `x` holds a value.
|
||||
return this.lifetime_of(walk, this.left_of(node), depth);
|
||||
}
|
||||
nk::construct => {
|
||||
for &each in this.run_view(this.right_of(node)) {
|
||||
val held := this.lifetime_of(walk, this.left_of(each), depth);
|
||||
return held if held.kind != lifetime_kind::none;
|
||||
}
|
||||
return none;
|
||||
}
|
||||
nk::array => {
|
||||
for &each in this.run_view(this.left_of(node)) {
|
||||
val held := this.lifetime_of(walk, each, depth);
|
||||
return held if held.kind != lifetime_kind::none;
|
||||
}
|
||||
return none;
|
||||
}
|
||||
else => return none;
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether no assignment of the body being looked at writes a local whole.
|
||||
#! @param this The checker.
|
||||
#! @param walk The walk.
|
||||
#! @param local The node declaring the local.
|
||||
#! @returns Whether none does.
|
||||
def checker::lifetime_unwritten(&this, walk: &lifetime_walk, local: u32): bool {
|
||||
val first: u32 = walk.sessions.last.events;
|
||||
for i in first..walk.events.length as u32 {
|
||||
val held: lifetime_event = walk.events[i];
|
||||
continue if held.kind != lifetime_kind_of_event::written;
|
||||
val target := this.left_of(held.node);
|
||||
continue if this.kind_of(target) != nk::name;
|
||||
val ref := this.ref_at(target);
|
||||
return false if (ref & ref_local) != 0 && (ref & ref_mask) == local;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Whether what a call gives back keeps one of its arguments: the parameter it
|
||||
#! is given for is `@kept`, or its type is a type parameter of the function's
|
||||
#! own that the result's type holds.
|
||||
#! @param this The checker.
|
||||
#! @param call The `call` node.
|
||||
#! @param arg The argument's index among those written.
|
||||
#! @returns Whether it keeps it.
|
||||
def checker::call_keeps(&mut this, call: u32, arg: u32): bool {
|
||||
val callee := this.left_of(call);
|
||||
val ref := this.ref_at(callee);
|
||||
return false if ref == 0 || (ref & (ref_local | ref_type)) != 0;
|
||||
val decl := ref & ref_mask;
|
||||
return false if this.decls.kinds.p[decl] != dk::function || this.decls.nodes.p[decl] == 0;
|
||||
# A method called on a value takes it as `this`, the first parameter.
|
||||
val object := this.kind_of(callee) == nk::member && this.decls.has(decl, df::method);
|
||||
val index := arg + (object ? 1u : 0u);
|
||||
|
||||
val saved := this.at;
|
||||
this.use_table(this.units[this.decls.files.p[decl]].table);
|
||||
val node: u32 = this.decls.nodes.p[decl];
|
||||
val params := this.record(node, function_slot::params);
|
||||
var keeps := false;
|
||||
if this.kind_of(node) == nk::function && index < this.run_length(params) {
|
||||
val param := this.run_at(params, index);
|
||||
for &attr in this.run_view(this.record(param, parameter_slot::attributes)) {
|
||||
continue if this.kind_of(attr) != nk::attribute;
|
||||
val named := this.left_of(attr);
|
||||
keeps = keeps || this.segment_name(named, this.segment_count(named) - 1) == word::kept;
|
||||
}
|
||||
# A type parameter of its own the result's type holds.
|
||||
val written := this.left_of(param);
|
||||
if !keeps && written != 0 && this.kind_of(written) == nk::type_name && this.segment_count(written) == 1 {
|
||||
val name := this.segment_name(written, 0);
|
||||
var own := false;
|
||||
for &each in this.run_view(this.record(node, function_slot::type_params)) {
|
||||
own = own || this.name_of(each) == name;
|
||||
}
|
||||
keeps = own && this.type_names(this.record(node, function_slot::result), name);
|
||||
}
|
||||
}
|
||||
this.at = saved;
|
||||
return keeps;
|
||||
}
|
||||
|
||||
#! Whether a type written names a type, as a type parameter, anywhere within it.
|
||||
#! @param this The checker.
|
||||
#! @param node The type written, or 0.
|
||||
#! @param name The name's ID.
|
||||
#! @returns Whether it does.
|
||||
def checker::type_names(&mut this, node: u32, name: u32): bool {
|
||||
return false if node == 0;
|
||||
var todo := vector::<u32>::new();
|
||||
todo.append(node);
|
||||
var seen := 0;
|
||||
while todo.length != 0 && seen < 4096 {
|
||||
seen++;
|
||||
val at: u32 = todo.last;
|
||||
todo.truncate();
|
||||
return true if this.kind_of(at) == nk::type_name && this.segment_count(at) == 1 && this.segment_name(at, 0) == name;
|
||||
this.syn().children(at, &mut todo);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! The local an anonymous function names that a function around it declares,
|
||||
#! the one declared first where it names several.
|
||||
#! @param this The checker.
|
||||
#! @param walk The walk.
|
||||
#! @param node The `lambda` node.
|
||||
#! @returns The local, or nothing it is sure of.
|
||||
def checker::lifetime_captured(&mut this, walk: &lifetime_walk, node: u32): lifetime_ref {
|
||||
var todo := vector::<u32>::new();
|
||||
this.syn().children(node, &mut todo);
|
||||
var seen := 0;
|
||||
var first: u32 = 0;
|
||||
while todo.length != 0 && seen < 4096 {
|
||||
seen++;
|
||||
val at: u32 = todo.last;
|
||||
todo.truncate();
|
||||
if this.kind_of(at) == nk::name {
|
||||
val ref := this.ref_at(at);
|
||||
if (ref & ref_local) != 0 {
|
||||
val local := ref & ref_mask;
|
||||
val owner := walk.note_of(local).owner;
|
||||
if owner != 0 && walk.functions.span.contains(&owner)
|
||||
&& (first == 0 || this.node_offset_of(local) < this.node_offset_of(first)) {
|
||||
first = local;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.syn().children(at, &mut todo);
|
||||
}
|
||||
return first != 0 ? lifetime_ref { kind = lifetime_kind::local, local = first } : lifetime_ref { kind = lifetime_kind::none };
|
||||
}
|
||||
|
||||
#! The local whose own storage a place is within, past fields and the elements
|
||||
#! of fixed arrays it holds as values. A local that is a reference names what
|
||||
#! it refers to, so a place within it is within no local the checker knows.
|
||||
#! @param this The checker.
|
||||
#! @param node The place.
|
||||
#! @returns The node declaring the local, or 0 where the place is not within one.
|
||||
def checker::lifetime_root(&mut this, node: u32): u32 {
|
||||
var at := node;
|
||||
var steps := 0;
|
||||
while steps < 64 {
|
||||
steps++;
|
||||
val kind := this.kind_of(at);
|
||||
if kind == nk::member || kind == nk::index {
|
||||
val object := this.left_of(at);
|
||||
val held := this.types.kind_of(this.unalias(this.type_at(object)));
|
||||
return 0 if kind == nk::member && (held == ty::reference || held == ty::pointer);
|
||||
return 0 if kind == nk::index && held != ty::fixed;
|
||||
if kind == nk::member {
|
||||
val ref := this.ref_at(at);
|
||||
return 0 if ref == 0 || (ref & (ref_local | ref_type)) != 0 || this.decls.kinds.p[ref] != dk::field;
|
||||
}
|
||||
at = object;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return 0 if this.kind_of(at) != nk::name;
|
||||
val ref := this.ref_at(at);
|
||||
return 0 if (ref & ref_local) == 0;
|
||||
val held := this.types.kind_of(this.unalias(this.type_at(ref & ref_mask)));
|
||||
return held == ty::reference ? 0 : ref & ref_mask;
|
||||
}
|
||||
|
||||
#! Where in the file a node's token begins.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The token's offset.
|
||||
def checker::node_offset_of(&this, node: u32): u32 {
|
||||
val syntax := &this.syn();
|
||||
return syntax.token_offsets[syntax.node_tokens[node]];
|
||||
}
|
||||
|
||||
#! The name a local is declared with.
|
||||
#! @param this The checker.
|
||||
#! @param local The node declaring it.
|
||||
#! @returns The name's ID.
|
||||
def checker::local_name(&this, local: u32): u32 {
|
||||
return this.name_of(local);
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
#! @file link.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the linking of what every file declared: each file's imports are
|
||||
#! found among the modules declared, and each declaration made through a type,
|
||||
#! as `vec2::length` or `list<T>::iter`, becomes a member of its type, with the
|
||||
#! type parameters its receiver names. A nested type may itself wait for its
|
||||
#! type, so those whose types are not yet members wait for another pass.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! Links the imports and the declarations made through types.
|
||||
#! @param this The checker.
|
||||
pub def checker::link(&mut this): void {
|
||||
var imports := vector::<vector<u32>>::new();
|
||||
for _ in 0..this.units.length {
|
||||
imports.append(vector::<u32>::new());
|
||||
}
|
||||
|
||||
for i in 0..this.pending_imports.length {
|
||||
val each := this.pending_imports[i];
|
||||
val file := each.file;
|
||||
val node := each.node;
|
||||
this.use_table(this.units[file].table);
|
||||
|
||||
val found := this.module_path(this.left_of(node), false);
|
||||
if found == 0 {
|
||||
this.report(node, ec::undefined, "No file declares this module.");
|
||||
continue;
|
||||
}
|
||||
imports[file].append(found);
|
||||
}
|
||||
|
||||
for file in 0..this.units.length {
|
||||
val run := this.decls.make_list(imports[file].span);
|
||||
this.units[file].imports = run;
|
||||
}
|
||||
|
||||
this.attach_pending();
|
||||
this.give_accessors_params();
|
||||
}
|
||||
|
||||
#! Makes each declaration made through a type a member of it. Each pass attaches
|
||||
#! what it can. One whose type is a nested type not yet attached waits for the
|
||||
#! next, and what is left when a pass attaches nothing names no type.
|
||||
#! @param this The checker.
|
||||
def checker::attach_pending(&mut this): void {
|
||||
var waiting := vector::<u32>::new();
|
||||
waiting.extend(this.pending.span);
|
||||
|
||||
var progress := true;
|
||||
while progress && waiting.length != 0 {
|
||||
progress = false;
|
||||
var later := vector::<u32>::new();
|
||||
for &id in waiting {
|
||||
if this.attach(id, false) {
|
||||
progress = true;
|
||||
} else {
|
||||
later.append(id);
|
||||
}
|
||||
}
|
||||
waiting = move(&mut later);
|
||||
}
|
||||
|
||||
for &id in waiting {
|
||||
this.attach(id, true);
|
||||
}
|
||||
}
|
||||
|
||||
#! Gives each accessor its property's type parameters, which its receiver named.
|
||||
#! @param this The checker.
|
||||
def checker::give_accessors_params(&mut this): void {
|
||||
for id in 1..this.decls.count {
|
||||
continue if !this.decls.has(id as u32, df::accessor) || this.decls.kinds.p[id] == dk::none;
|
||||
val params: u32 = this.decls.params.p[this.decls.owners.p[id]];
|
||||
this.decls.params.p[id] = params;
|
||||
if params != 0 {
|
||||
this.decls.mark(id as u32, df::generic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Makes the declaration a member of the type its receiver names.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @param last Whether this is the last chance, which reports a receiver
|
||||
#! naming no type rather than waiting.
|
||||
#! @returns Whether it was attached, or reported.
|
||||
def checker::attach(&mut this, id: u32, last: bool): bool {
|
||||
val saved := this.at;
|
||||
this.use_table(this.units[this.decls.files.p[id]].table);
|
||||
this.at.home = this.decls.modules.p[id];
|
||||
# A receiver is a path from the module, which no type around it shortens.
|
||||
this.at.scope = 0;
|
||||
|
||||
val node: u32 = this.decls.nodes.p[id];
|
||||
val receiver := this.receiver_of(node);
|
||||
this.decls.top = this.decls.tops.p[id];
|
||||
val owner := this.receiver_decl(receiver, last);
|
||||
if owner == 0 {
|
||||
this.at = saved;
|
||||
this.decls.top = 0;
|
||||
if last {
|
||||
this.decls.mark(id, df::failed);
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
# What a trait asks for is declared within it. Through it, outside it, only
|
||||
# functions of its own are, which take no `this`.
|
||||
if this.decls.kinds.p[owner] == dk::trait_type
|
||||
&& (this.decls.kinds.p[id] != dk::function || this.decls.has(id, df::method)) {
|
||||
this.report(receiver, ec::invalid,
|
||||
"A trait has no methods, associated variables, or nested types declared outside it. What it asks for is declared within it, and outside it only functions of its own, which take no `this`, as `def real::parse<T>(text: strview)`.");
|
||||
this.decls.mark(id, df::failed);
|
||||
this.at = saved;
|
||||
this.decls.top = 0;
|
||||
return true;
|
||||
}
|
||||
# A function of the trait's own takes a name it does not ask for.
|
||||
if this.decls.kinds.p[owner] == dk::trait_type {
|
||||
val asked := this.decls.member(owner, this.decls.names.p[id]);
|
||||
if asked != 0 && this.decls.owners.p[asked] == owner && !this.receiver_declared(asked) {
|
||||
this.report(node, ec::duplicate, "The trait already asks for a member of this name.");
|
||||
this.decls.mark(id, df::failed);
|
||||
this.at = saved;
|
||||
this.decls.top = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
# Each segment of the receiver names the type parameters of the type it
|
||||
# names, as `T` of `box<T>::get`, which the declaration takes first.
|
||||
var params := vector::<u32>::new();
|
||||
var level := owner;
|
||||
for step in 0..this.segment_count(receiver) {
|
||||
val index := this.segment_count(receiver) - 1 - step as u32;
|
||||
val named := this.segment_args(receiver, index);
|
||||
val made := this.declare_params(named, id);
|
||||
# A `where` bounding the type's parameters further makes the method one only
|
||||
# the types meeting it have.
|
||||
for &written in this.run_view(named) {
|
||||
if this.kind_of(written) == nk::type_param && this.left_of(written) != 0 {
|
||||
this.decls.mark(id, df::bounding);
|
||||
}
|
||||
}
|
||||
for k in 0..made.length {
|
||||
params.insert_at(k, made[k]);
|
||||
}
|
||||
|
||||
# Each stands for a parameter of the type, whose bounds it takes.
|
||||
val wanted := this.own_param_count(level);
|
||||
val owned: u32 = this.decls.params.p[level];
|
||||
val total := this.decls.list_length(owned);
|
||||
for k in 0..made.length {
|
||||
if k < wanted as u64 && total >= wanted {
|
||||
val source := this.decls.list_at(owned, total - wanted + k as u32);
|
||||
val param: u32 = made[k];
|
||||
this.mirrors.put(param, source);
|
||||
# A constant parameter of the type is named a constant here too.
|
||||
if this.decls.has(source, df::valued) {
|
||||
this.decls.mark(param, df::valued);
|
||||
} else if this.kind_of(this.decls.nodes.p[param]) == nk::const_param {
|
||||
this.report(this.decls.nodes.p[param], ec::generic,
|
||||
"A type parameter of the type is named here without `const`.");
|
||||
}
|
||||
}
|
||||
}
|
||||
if made.length as u32 != wanted && level != 0 {
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.decl_text(level));
|
||||
val words := $"` takes {wanted} type parameters, which a declaration through it names, as `box<T>::get`.";
|
||||
text.append(words.span);
|
||||
this.report_text(this.segment_node(receiver, index), ec::generic, &text);
|
||||
}
|
||||
level = this.decls.owners.p[level];
|
||||
break if level == 0 || this.decls.kinds.p[level] == dk::none;
|
||||
}
|
||||
|
||||
val own: u32 = this.decls.params.p[id];
|
||||
params.extend(this.decls.list_view(own));
|
||||
this.decls.params.p[id] = this.decls.make_list(params.span);
|
||||
if params.length != 0 {
|
||||
this.decls.mark(id, df::generic);
|
||||
}
|
||||
|
||||
this.add_member(owner, id);
|
||||
this.decls.top = 0;
|
||||
this.at = saved;
|
||||
return true;
|
||||
}
|
||||
|
||||
#! How many type parameters a type declares of its own, not counting those of
|
||||
#! the type it is nested in.
|
||||
#! @param this The checker.
|
||||
#! @param id The type's declaration.
|
||||
#! @returns How many.
|
||||
pub def checker::own_param_count(&this, id: u32): u32 {
|
||||
return 0 if id == 0 || this.decls.kinds.p[id] == dk::builtin;
|
||||
|
||||
val saved_file: u32 = this.decls.files.p[id];
|
||||
val syntax := &this.units[saved_file].syntax;
|
||||
val node: u32 = this.decls.nodes.p[id];
|
||||
val kind := syntax.node_kinds[node];
|
||||
return 0 if kind != nk::struct_decl && kind != nk::union_decl && kind != nk::trait_decl
|
||||
&& kind != nk::alias && kind != nk::function;
|
||||
|
||||
# A pattern, `def(Ts...): R` or `L[N]`, declares one for each type parameter it
|
||||
# names.
|
||||
var made: u32 = 0;
|
||||
for &each in syntax.run_view(syntax.record(node, decl_slot::type_params)) {
|
||||
if syntax.node_kinds[each] == nk::array_pattern {
|
||||
made += syntax.node_rights[each] != 0 ? 2 : 1;
|
||||
continue;
|
||||
}
|
||||
if syntax.node_kinds[each] != nk::type_pattern {
|
||||
made++;
|
||||
continue;
|
||||
}
|
||||
val inner := syntax.node_lefts[each];
|
||||
made += syntax.run_length(inner) + (syntax.node_rights[each] != 0 ? 1 : 0);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The type declaration a receiver names, or 0 where it names none yet.
|
||||
#! @param this The checker.
|
||||
#! @param receiver The receiver's `type_name` node.
|
||||
#! @param last Whether this is the last chance, which reports a receiver
|
||||
#! naming no type.
|
||||
#! @returns The declaration, or 0.
|
||||
def checker::receiver_decl(&mut this, receiver: u32, last: bool): u32 {
|
||||
var at: u32 = 0;
|
||||
var home: u32 = 0;
|
||||
var in_module := false;
|
||||
for i in 0..this.segment_count(receiver) {
|
||||
val name := this.segment_name(receiver, i as u32);
|
||||
if i == 0 {
|
||||
at = this.lookup_global(name, 0);
|
||||
if at == 0 {
|
||||
home = this.lookup_module(name);
|
||||
in_module = home != 0;
|
||||
}
|
||||
} else if in_module {
|
||||
at = this.modules.decl(home, name);
|
||||
if at == 0 {
|
||||
home = this.modules.find(home, name);
|
||||
in_module = home != 0;
|
||||
} else {
|
||||
in_module = false;
|
||||
}
|
||||
} else {
|
||||
at = this.decls.path_member(at, name);
|
||||
}
|
||||
|
||||
if at == 0 && !in_module {
|
||||
if last {
|
||||
this.report(this.segment_node(receiver, i as u32), ec::not_type,
|
||||
"A declaration is made through a type, and this names none.");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0 if in_module;
|
||||
if !this.is_type_decl(at) {
|
||||
if last {
|
||||
this.report(receiver, ec::not_type, "A declaration is made through a type, and this names none.");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
#! Whether the declaration declares a type.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns Whether it does.
|
||||
pub def checker::is_type_decl(&this, id: u32): bool {
|
||||
return match this.decls.kinds.p[id] {
|
||||
dk::builtin, dk::struct_type, dk::union_type, dk::enum_type, dk::trait_type,
|
||||
dk::alias_type, dk::type_param, dk::assoc_type => true,
|
||||
else => false
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,507 @@
|
||||
#! @file lookup.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how names are found where they are written. A name is first what
|
||||
#! a scope binds it to, then what the types around it give, as `kind` within
|
||||
#! `token` is `token::kind`, then a declaration of the module it is written in,
|
||||
#! or of the modules holding that one, nearest first, then of the modules its
|
||||
#! file imports, then a built-in type, then a declaration of `core`'s root. The program, where the files declaring
|
||||
#! no module are, holds no module, so a module never names what it declares. A name a type gives that the module or
|
||||
#! a module holding it declares too is ambiguous, as are two imports declaring
|
||||
#! the name, unless they declare the same thing.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! What the name refers to where it is written, or 0 where nothing declares it.
|
||||
#! @param this The checker.
|
||||
#! @param name The ID of the name.
|
||||
#! @param node Where the name is written, for reporting it ambiguous, or 0.
|
||||
#! @returns A local or type parameter with its bit of `ref_local` or `ref_type`,
|
||||
#! a declaration, or 0.
|
||||
pub def checker::lookup(&mut this, name: u32, node: u32): u32 {
|
||||
val bound := this.bound(name);
|
||||
return bound if bound != 0;
|
||||
return this.lookup_global(name, node);
|
||||
}
|
||||
|
||||
#! The declaration the name refers to outside any scope, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param name The ID of the name.
|
||||
#! @param node Where the name is written, for reporting it ambiguous, or 0.
|
||||
#! @returns The declaration, or 0.
|
||||
pub def checker::lookup_global(&mut this, name: u32, node: u32): u32 {
|
||||
return this.lookup_named(name, node, false).decl;
|
||||
}
|
||||
|
||||
#! The declaration the name refers to outside any scope where only a type is
|
||||
#! meant, as in a type, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param name The ID of the name.
|
||||
#! @param node Where the name is written, for reporting it ambiguous, or 0.
|
||||
#! @returns The declaration, or 0.
|
||||
pub def checker::lookup_type(&mut this, name: u32, node: u32): u32 {
|
||||
return this.lookup_named(name, node, true).decl;
|
||||
}
|
||||
|
||||
#! The declaration the name refers to outside any scope, and where a type
|
||||
#! around it gives it, that type, given the arguments it has there.
|
||||
#! @param this The checker.
|
||||
#! @param name The ID of the name.
|
||||
#! @param node Where the name is written, for reporting it ambiguous, or 0.
|
||||
#! @param types Whether only a type is meant, so what else a type around it
|
||||
#! gives is passed over, as a member `u8` of an enum is in `u8`.
|
||||
#! @returns The declaration and the type giving it, or no declaration.
|
||||
pub def checker::lookup_named(&mut this, name: u32, node: u32, types: bool): checker::found {
|
||||
this.depend_name(name);
|
||||
val scoped := this.in_scope(name, types);
|
||||
var home := this.at.home;
|
||||
var steps := 0;
|
||||
while steps < 64 {
|
||||
val found := this.modules.decl(home, name);
|
||||
if found != 0 {
|
||||
if scoped.decl != 0 && scoped.decl != found && node != 0 {
|
||||
this.report_ambiguous(node, name, scoped.decl, found);
|
||||
}
|
||||
return scoped.decl != 0 ? scoped : checker::found { decl = found };
|
||||
}
|
||||
# The program is where the files declaring no module are, so it holds no
|
||||
# module: a module of one segment is within none.
|
||||
break if home == 0 || this.modules.parents[home] == 0;
|
||||
home = this.modules.parents[home];
|
||||
steps++;
|
||||
}
|
||||
return scoped if scoped.decl != 0;
|
||||
|
||||
var found: u32 = 0;
|
||||
var hidden: u32 = 0;
|
||||
val imports: u32 = this.units[this.at.file].imports;
|
||||
for &imported in this.decls.list_view(imports) {
|
||||
val each := this.modules.decl(imported, name);
|
||||
continue if each == 0;
|
||||
if !this.visible(each) {
|
||||
hidden = each;
|
||||
continue;
|
||||
}
|
||||
|
||||
if found != 0 && found != each {
|
||||
if node != 0 {
|
||||
var text := string::from("More than one imported module declares `", nothing {});
|
||||
text.append(this.names.text(name));
|
||||
text.append("`, so write the module it is of, as `io::println`.");
|
||||
this.report_text(node, ec::ambiguous, &text);
|
||||
}
|
||||
return checker::found { decl = found };
|
||||
}
|
||||
found = each;
|
||||
}
|
||||
|
||||
return checker::found { decl = found } if found != 0;
|
||||
if this.builtins.get(&name) is some(got) {
|
||||
return checker::found { decl = got };
|
||||
}
|
||||
|
||||
# Last, the names of `core`'s root, which every file sees, and which anything
|
||||
# declared or imported hides.
|
||||
val core := this.modules.find(0, word::core);
|
||||
if core != 0 && core != this.at.home {
|
||||
val each := this.modules.decl(core, name);
|
||||
if each != 0 {
|
||||
return checker::found { decl = each } if this.visible(each);
|
||||
hidden = hidden != 0 ? hidden : each;
|
||||
}
|
||||
}
|
||||
|
||||
# A declaration only hidden from where it is named is found, so what names
|
||||
# it is told it is not `pub` rather than that it does not exist.
|
||||
return checker::found { decl = hidden };
|
||||
}
|
||||
|
||||
#! What the types around where checking is give the name, innermost first.
|
||||
#! Within a type, and what is declared through it, its nested types and
|
||||
#! associated variables, and an enum's members, are named without it, as
|
||||
#! `kind` is `token::kind` within `token`. A field or method belongs to a value,
|
||||
#! so it is named through one, and a type's functions are named through the
|
||||
#! type, since they are often named for the free functions they implement, as
|
||||
#! `vector<T>::decode` calls `decode::<T>`.
|
||||
#! @param this The checker.
|
||||
#! @param name The ID of the name.
|
||||
#! @param types Whether only a type is meant.
|
||||
#! @returns The member and the type giving it, or no member.
|
||||
pub def checker::in_scope(&mut this, name: u32, types: bool): checker::found {
|
||||
var kind := this.at.scope;
|
||||
var steps := 0;
|
||||
while kind != 0 && steps < 16 {
|
||||
steps++;
|
||||
val decl := this.type_decl(kind);
|
||||
break if decl == 0;
|
||||
val member := this.scope_member(decl, name, types);
|
||||
return checker::found { decl = member, owner = kind } if member != 0;
|
||||
|
||||
# A type nested in another is named within that one, whose arguments are
|
||||
# the first of its own.
|
||||
val outer: u32 = this.decls.owners.p[decl];
|
||||
break if outer == 0 || !this.gives_scope(outer);
|
||||
kind = this.scope_type(outer, kind);
|
||||
}
|
||||
return checker::found {};
|
||||
}
|
||||
|
||||
#! The member of the name a type gives the names within it, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param decl The type's declaration.
|
||||
#! @param name The ID of the name.
|
||||
#! @param types Whether only a type is meant.
|
||||
#! @returns The member, or 0.
|
||||
def checker::scope_member(&mut this, decl: u32, name: u32, types: bool): u32 {
|
||||
val nested := this.decls.member_in(decl, name, true);
|
||||
if nested != 0 {
|
||||
return this.visible(nested) ? nested : 0;
|
||||
}
|
||||
|
||||
# A sealed trait gives the names of its types too.
|
||||
val sealed := this.is_sealed(decl);
|
||||
val member := types && !sealed ? 0 : this.decls.member_in(decl, name, false);
|
||||
return 0 if member == 0 || !this.visible(member);
|
||||
return match this.decls.kinds.p[member] {
|
||||
dk::variable, dk::enum_member => types ? 0 : member,
|
||||
dk::assoc_type => sealed ? member : 0,
|
||||
else => 0
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether a type gives the names within it: a struct, union, or enum, or a
|
||||
#! built-in type. A trait gives its members `self` and the types it asks for.
|
||||
#! @param this The checker.
|
||||
#! @param decl The type's declaration.
|
||||
#! @returns Whether it does.
|
||||
pub def checker::gives_scope(&this, decl: u32): bool {
|
||||
return match this.decls.kinds.p[decl] {
|
||||
dk::struct_type, dk::union_type, dk::enum_type, dk::builtin => true,
|
||||
else => false
|
||||
};
|
||||
}
|
||||
|
||||
#! The type giving the names within a declaration: the type itself, or the
|
||||
#! one it is a member of or is declared through, given the arguments it is
|
||||
#! checked with, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @param args The pack of its type arguments, or 0 where it is checked as
|
||||
#! itself.
|
||||
#! @returns The type, or 0.
|
||||
pub def checker::scope_of(&mut this, id: u32, args: u32): u32 {
|
||||
var owner: u32 = this.decls.owners.p[id];
|
||||
var holder := id;
|
||||
match this.decls.kinds.p[id] {
|
||||
dk::type_param => return owner == 0 ? 0 : this.scope_of(owner, 0);
|
||||
dk::struct_type, dk::union_type, dk::enum_type => return this.own_scope(id, id, args);
|
||||
dk::field, dk::enum_member => return owner != 0 && this.gives_scope(owner) ? this.decls.types.p[owner] : 0;
|
||||
dk::function, dk::variable, dk::property, dk::alias_type, dk::trait_type, dk::assoc_type => {}
|
||||
else => return 0;
|
||||
}
|
||||
|
||||
# A sealed trait gives its constants and types the names of the others, of
|
||||
# the trait given the same arguments.
|
||||
if this.decls.kinds.p[id] == dk::trait_type && this.is_sealed(id) {
|
||||
return args == 0 ? this.decls.types.p[id] : this.types.named(id, this.types.parts_of(args).span);
|
||||
}
|
||||
if (this.decls.kinds.p[id] == dk::variable || this.decls.kinds.p[id] == dk::assoc_type || this.decls.kinds.p[id] == dk::function)
|
||||
&& this.is_sealed(owner) {
|
||||
return this.decls.types.p[owner] if args == 0;
|
||||
var given := vector::<u32>::new();
|
||||
for i in 0..this.decls.list_length(this.decls.params.p[owner]) {
|
||||
given.append(i < this.types.part_count(args) ? this.types.part_at(args, i as u32) : bt::error);
|
||||
}
|
||||
return this.types.named(owner, given.span);
|
||||
}
|
||||
# An accessor is checked as a member of its property's type.
|
||||
if owner != 0 && this.decls.kinds.p[owner] == dk::property {
|
||||
holder = owner;
|
||||
owner = this.decls.owners.p[owner];
|
||||
}
|
||||
return 0 if owner == 0 || !this.gives_scope(owner);
|
||||
return this.own_scope(owner, holder, args);
|
||||
}
|
||||
|
||||
#! A type, given the first of the arguments of a declaration within it, which
|
||||
#! names the type's parameters first.
|
||||
#! @param this The checker.
|
||||
#! @param decl The type's declaration.
|
||||
#! @param holder The declaration within it.
|
||||
#! @param args The pack of the holder's type arguments, or 0 where it is
|
||||
#! checked as itself.
|
||||
#! @returns The type.
|
||||
def checker::own_scope(&mut this, decl: u32, holder: u32, args: u32): u32 {
|
||||
return this.decls.types.p[decl] if this.decls.kinds.p[decl] == dk::builtin;
|
||||
val count := this.decls.list_length(this.decls.params.p[decl]);
|
||||
val run: u32 = this.decls.params.p[holder];
|
||||
var given := vector::<u32>::new();
|
||||
for i in 0..count {
|
||||
if args != 0 && i < this.types.part_count(args) {
|
||||
given.append(this.types.part_at(args, i as u32));
|
||||
} else if i < this.decls.list_length(run) {
|
||||
given.append(this.decls.types.p[this.decls.list_at(run, i as u32)]);
|
||||
} else {
|
||||
given.append(bt::error);
|
||||
}
|
||||
}
|
||||
return this.types.named(decl, given.span);
|
||||
}
|
||||
|
||||
#! A type a nested one is declared through, given the first of the nested
|
||||
#! one's arguments.
|
||||
#! @param this The checker.
|
||||
#! @param outer The outer type's declaration.
|
||||
#! @param inner The nested type, given its arguments.
|
||||
#! @returns The outer type.
|
||||
def checker::scope_type(&mut this, outer: u32, inner: u32): u32 {
|
||||
return this.decls.types.p[outer] if this.decls.kinds.p[outer] == dk::builtin;
|
||||
val count := this.decls.list_length(this.decls.params.p[outer]);
|
||||
var given := vector::<u32>::new();
|
||||
for i in 0..count {
|
||||
given.append(this.types.kind_of(inner) == ty::named && i < this.types.part_count(inner)
|
||||
? this.types.part_at(inner, i as u32)
|
||||
: bt::error);
|
||||
}
|
||||
return this.types.named(outer, given.span);
|
||||
}
|
||||
|
||||
#! Reports a name both a type around it and its module give, which only a
|
||||
#! path tells apart.
|
||||
#! @param this The checker.
|
||||
#! @param node Where the name is written.
|
||||
#! @param name The ID of the name.
|
||||
#! @param scoped What the type gives it.
|
||||
#! @param other What the module gives it.
|
||||
def checker::report_ambiguous(&mut this, node: u32, name: u32, scoped: u32, other: u32): void {
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.names.text(name));
|
||||
text.append("` names both `");
|
||||
var first := this.path_text(scoped);
|
||||
text.append(first.span);
|
||||
if this.decls.modules.p[other] == 0 && this.decls.owners.p[other] == 0 {
|
||||
text.append("` and the program's own `");
|
||||
text.append(this.names.text(name));
|
||||
text.append("`, so write `");
|
||||
text.append(first.span);
|
||||
text.append("` for the first, or rename one of them.");
|
||||
} else {
|
||||
text.append("` and `");
|
||||
var second := this.path_text(other);
|
||||
text.append(second.span);
|
||||
text.append("`, so write the path of the one meant.");
|
||||
}
|
||||
this.report_text(node, ec::ambiguous, &text);
|
||||
}
|
||||
|
||||
#! The path a declaration is written as from anywhere, as `token::kind` or
|
||||
#! `std::io::println`. One declared outside any module has only its name.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns The path.
|
||||
pub def checker::path_text(&this, id: u32): string {
|
||||
var made := string::new(nothing {});
|
||||
val owner: u32 = this.decls.owners.p[id];
|
||||
if owner != 0 && this.decls.kinds.p[id] != dk::type_param {
|
||||
made = this.path_text(owner);
|
||||
made.append("::");
|
||||
} else {
|
||||
var modules := vector::<u32>::new();
|
||||
var at: u32 = this.decls.modules.p[id];
|
||||
var steps := 0;
|
||||
while at != 0 && steps < 64 {
|
||||
modules.append(at);
|
||||
at = this.modules.parents[at];
|
||||
steps++;
|
||||
}
|
||||
var i := modules.length;
|
||||
while i > 0 {
|
||||
i--;
|
||||
made.append(this.names.text(this.modules.names[modules[i]]));
|
||||
made.append("::");
|
||||
}
|
||||
}
|
||||
made.append(this.names.text(this.decls.names.p[id]));
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Reports a name nothing declares where it is used. A module sees the names of
|
||||
#! those holding it without an import, so where no file of one was read, as a
|
||||
#! library's root left out, the note says so.
|
||||
#! @param this The checker.
|
||||
#! @param node The node naming it.
|
||||
#! @param name The ID of the name.
|
||||
pub def checker::report_undeclared(&mut this, node: u32, name: u32): void {
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.names.text(name));
|
||||
text.append("` is not declared where it is used.");
|
||||
var at: u32 = this.at.home == 0 ? 0 : this.modules.parents[this.at.home];
|
||||
var steps := 0;
|
||||
while at != 0 && steps < 64 {
|
||||
if !this.modules.named[at] {
|
||||
text.append(" No file of `");
|
||||
text.append(this.module_text(at).span);
|
||||
text.append("`, which holds this file's module and whose names it sees, was read: is one missing?");
|
||||
break;
|
||||
}
|
||||
at = this.modules.parents[at];
|
||||
steps++;
|
||||
}
|
||||
this.report_text(node, ec::undefined, &text);
|
||||
}
|
||||
|
||||
#! A module's path, as `std::io`.
|
||||
#! @param this The checker.
|
||||
#! @param id The module.
|
||||
#! @returns The path.
|
||||
pub def checker::module_text(&this, id: u32): string {
|
||||
var made := string::new(nothing {});
|
||||
var at := id;
|
||||
var steps := 0;
|
||||
while at != 0 && steps < 64 {
|
||||
var each := string::from(this.names.text(this.modules.names[at]), nothing {});
|
||||
if made.length != 0 {
|
||||
each.append("::");
|
||||
each.append(made.span);
|
||||
}
|
||||
made = move(&mut each);
|
||||
at = this.modules.parents[at];
|
||||
steps++;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The module the name refers to where it is written: one within the module it
|
||||
#! is written in or a module holding it, or the last segment of an import.
|
||||
#! @param this The checker.
|
||||
#! @param name The ID of the name.
|
||||
#! @returns The module, or 0.
|
||||
pub def checker::lookup_module(&mut this, name: u32): u32 {
|
||||
var home := this.at.home;
|
||||
var steps := 0;
|
||||
while steps < 64 {
|
||||
val found := this.modules.find(home, name);
|
||||
return found if found != 0;
|
||||
break if home == 0;
|
||||
home = this.modules.parents[home];
|
||||
steps++;
|
||||
}
|
||||
|
||||
val imports: u32 = this.units[this.at.file].imports;
|
||||
for &imported in this.decls.list_view(imports) {
|
||||
return imported if this.modules.names[imported] == name;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Whether a path whose first name names both a module and a type names the
|
||||
#! type: where the module declares nothing of the path's next name, as
|
||||
#! `uri::parse` where the module `uri` declares the type `uri`, whose function
|
||||
#! `parse` is.
|
||||
#! @param this The checker.
|
||||
#! @param home The module the first name names.
|
||||
#! @param node The `path` or `type_name` node, of two segments or more.
|
||||
#! @returns Whether the type is named.
|
||||
pub def checker::type_over_module(&mut this, home: u32, node: u32): bool {
|
||||
val next := this.segment_name(node, 1);
|
||||
return false if this.modules.decl(home, next) != 0 || this.modules.find(home, next) != 0;
|
||||
val found := this.lookup_named(this.segment_name(node, 0), 0, true);
|
||||
return found.decl != 0 && this.is_type_decl(found.decl);
|
||||
}
|
||||
|
||||
#! Whether the declaration can be named where checking is: it is `pub`, or
|
||||
#! declared in the module checked or one holding it.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns Whether it can.
|
||||
pub def checker::visible(&this, id: u32): bool {
|
||||
return true if this.decls.has(id, df::exported) || this.decls.kinds.p[id] == dk::builtin;
|
||||
|
||||
# A property is seen where an accessor of it is `pub`, as `pub get`.
|
||||
if this.decls.kinds.p[id] == dk::property {
|
||||
val run: u32 = this.decls.children.p[id];
|
||||
for &each in this.decls.list_view(run) {
|
||||
return true if this.decls.has(each, df::exported);
|
||||
}
|
||||
}
|
||||
|
||||
# Enum members, and what a trait asks for, are as visible as their type. A
|
||||
# field takes its own `pub`, as a method does.
|
||||
val owner: u32 = this.decls.owners.p[id];
|
||||
val kind: dk = this.decls.kinds.p[id];
|
||||
if owner != 0 && (kind == dk::enum_member
|
||||
|| (this.decls.kinds.p[owner] == dk::trait_type && !this.receiver_declared(id))) {
|
||||
return this.visible(owner);
|
||||
}
|
||||
|
||||
var home: u32 = this.decls.modules.p[id];
|
||||
return this.modules.within(this.at.home, home);
|
||||
}
|
||||
|
||||
#! Whether a trait asks for the declaration: it is declared within the trait,
|
||||
#! or is an accessor of a property that is. A function declared through a
|
||||
#! trait, outside it, as `def real::parse<T>`, is the trait's own instead,
|
||||
#! called through the trait's name as any type's static function is.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns Whether it does.
|
||||
pub def checker::trait_asks(&this, id: u32): bool {
|
||||
var at := id;
|
||||
var owner: u32 = this.decls.owners.p[id];
|
||||
if owner != 0 && this.decls.kinds.p[owner] == dk::property {
|
||||
at = owner;
|
||||
owner = this.decls.owners.p[owner];
|
||||
}
|
||||
return owner != 0 && this.decls.kinds.p[owner] == dk::trait_type && !this.receiver_declared(at);
|
||||
}
|
||||
|
||||
#! Whether a trait's default is compiled for each type taking it, with `self`
|
||||
#! that type, as the type's own methods are: one of a trait that is only a
|
||||
#! bound, which no table calls through. Any other default is compiled once,
|
||||
#! taking `this` as a reference to the trait.
|
||||
#! @param this The checker.
|
||||
#! @param id The method's declaration.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::typed_default(&mut this, id: u32): bool {
|
||||
return false if this.decls.kinds.p[id] != dk::function || !this.trait_asks(id) || !this.has_body(id);
|
||||
val holder := this.enclosing_trait(id);
|
||||
return holder != 0 && holder != id && this.bound_only(holder);
|
||||
}
|
||||
|
||||
#! Whether a declaration is a constant a trait asks for and gives a default of,
|
||||
#! which a type naming the trait and giving none of its own takes, worked out
|
||||
#! with `self` that type. A sealed trait's constants are its own instead.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::constant_default(&this, id: u32): bool {
|
||||
return this.decls.kinds.p[id] == dk::variable && this.trait_asks(id)
|
||||
&& !this.is_sealed(this.decls.owners.p[id]) && this.record_in(id, variable_slot::value) != 0;
|
||||
}
|
||||
|
||||
#! Whether a constant a trait declares is one constant of the program, rather
|
||||
#! than one for each type taking it or each list of arguments the trait is
|
||||
#! given: one of a sealed trait without type parameters.
|
||||
#! @param this The checker.
|
||||
#! @param decl The constant's declaration.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::sealed_alone(&this, decl: u32): bool {
|
||||
val holder: u32 = this.decls.owners.p[decl];
|
||||
return this.is_sealed(holder) && this.decls.list_length(this.decls.params.p[holder]) == 0;
|
||||
}
|
||||
|
||||
#! Whether a declaration was made through a type, outside it, rather than
|
||||
#! within it, so it takes its own `pub`.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @returns Whether it was.
|
||||
pub def checker::receiver_declared(&this, id: u32): bool {
|
||||
val syntax := &this.units[this.decls.files.p[id]].syntax;
|
||||
val node: u32 = this.decls.nodes.p[id];
|
||||
val kind := syntax.node_kinds[node];
|
||||
return false if kind != nk::function && kind != nk::variable && kind != nk::property;
|
||||
return syntax.record(node, decl_slot::receiver) != 0;
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
#! @file nodes.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how the checker reads the nodes of the file it is checking: their
|
||||
#! kinds, tokens, slots, records, and runs, and the names and text of tokens.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! The syntax of the file being checked.
|
||||
#! @param this The checker.
|
||||
#! @returns The syntax.
|
||||
pub def checker::syn(&this): &syntax {
|
||||
return this.units[this.at.file].syntax;
|
||||
}
|
||||
|
||||
#! The source of the file being checked.
|
||||
#! @param this The checker.
|
||||
#! @returns The source.
|
||||
pub def checker::src(&this): &source {
|
||||
return this.units[this.at.file].source;
|
||||
}
|
||||
|
||||
#! The kind of a node of the file being checked.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns Its kind.
|
||||
pub def checker::kind_of(&this, node: u32): nk {
|
||||
return this.at.kinds[node];
|
||||
}
|
||||
|
||||
#! The token a node of the file being checked says what it is by.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The token's index.
|
||||
pub def checker::token_of(&this, node: u32): u32 {
|
||||
return this.at.tokens[node];
|
||||
}
|
||||
|
||||
#! A node's first slot, which its kind's shape says the meaning of.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The slot.
|
||||
pub def checker::left_of(&this, node: u32): u32 {
|
||||
return this.at.lefts[node];
|
||||
}
|
||||
|
||||
#! A node's second slot, which its kind's shape says the meaning of.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The slot.
|
||||
pub def checker::right_of(&this, node: u32): u32 {
|
||||
return this.at.rights[node];
|
||||
}
|
||||
|
||||
#! The name the node's token is, as a declaration's name or a `name` node's.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The name's ID, or 0 where its token is no word.
|
||||
pub def checker::name_of(&this, node: u32): u32 {
|
||||
return this.at.names[this.at.tokens[node]];
|
||||
}
|
||||
|
||||
#! The name a parameter is known by: its own, or of one an accessor implies,
|
||||
#! whose token is the `get` or `set`, `this`, or a `set`'s `value`.
|
||||
#! @param this The checker.
|
||||
#! @param node The parameter's node, or any other.
|
||||
#! @returns The ID of the name.
|
||||
pub def checker::param_name(&this, node: u32): u32 {
|
||||
return this.name_of(node) if this.kind_of(node) != nk::param || !this.node_has(node, nf::implied);
|
||||
return this.name_of(node) == names::set_id && !this.node_has(node, nf::mutable) ? names::value_id : names::this_id;
|
||||
}
|
||||
|
||||
#! The kind of the node's token.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The kind.
|
||||
pub def checker::token_kind(&this, node: u32): tk {
|
||||
return this.at.tkinds[this.at.tokens[node]];
|
||||
}
|
||||
|
||||
#! The text of the node's token, as it is written.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The text, which views the file's source.
|
||||
pub def checker::text_of(&this, node: u32): strview {
|
||||
val syntax := &this.syn();
|
||||
val token := this.token_of(node);
|
||||
return this.src().make_lexeme(syntax.token_offsets[token], syntax.token_length(token, this.names));
|
||||
}
|
||||
|
||||
#! Writes an expression as it is written, from its first token to its last, as
|
||||
#! `v.items` or `b != 0`.
|
||||
#! @param this The checker.
|
||||
#! @param node The expression.
|
||||
#! @param out Where it is written.
|
||||
pub def checker::written_text(&this, node: u32, out: &mut string): void {
|
||||
val syntax := &this.syn();
|
||||
# A node's own token may come after those of what it was parsed from, as an
|
||||
# operator's comes after its left operand, so the first is the least of all.
|
||||
var first: u32 = syntax.node_tokens[node];
|
||||
var walk := node_walk::new(node);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val token := syntax.node_tokens[at];
|
||||
first = token < first ? token : first;
|
||||
walk.enter(syntax, at);
|
||||
}
|
||||
val last := syntax.node_end(node);
|
||||
val begin := syntax.token_offsets[first];
|
||||
val end := syntax.token_offsets[last] + syntax.token_length(last, this.names);
|
||||
return if end <= begin;
|
||||
out.append(this.src().make_lexeme(begin, end - begin));
|
||||
}
|
||||
|
||||
#! The slot of the node's record at the index, in whichever of its slots its
|
||||
#! shape says holds the record.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param index The slot's position in the record.
|
||||
#! @returns The slot, or 0 where the node has no record.
|
||||
pub def checker::record(&this, node: u32, index: u32): u32 {
|
||||
val shape := shapes[this.at.kinds[node] as u64];
|
||||
asserts(index < shape.record.length, "The node's record has the slot.");
|
||||
val at: u32 = shape.left == slot::extra ? this.at.lefts[node] : this.at.rights[node];
|
||||
return this.at.extras[at + index];
|
||||
}
|
||||
|
||||
#! The flags of the node, wherever its shape keeps them.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The flags, as `nf` says, or 0.
|
||||
pub def checker::node_flags(&this, node: u32): u32 {
|
||||
return this.syn().flags_at(node);
|
||||
}
|
||||
|
||||
#! Whether a node has a flag.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param flag The flag.
|
||||
#! @returns Whether it has it.
|
||||
pub def checker::node_has(&this, node: u32, flag: nf): bool {
|
||||
return (this.node_flags(node) & flag as u32) != 0;
|
||||
}
|
||||
|
||||
#! How many nodes a run of the file being checked holds.
|
||||
#! @param this The checker.
|
||||
#! @param run Where the run begins.
|
||||
#! @returns How many nodes it holds.
|
||||
pub def checker::run_length(&this, run: u32): u32 {
|
||||
return this.at.extras[run];
|
||||
}
|
||||
|
||||
#! A node of a run, by its position.
|
||||
#! @param this The checker.
|
||||
#! @param run Where the run begins.
|
||||
#! @param index The position, below its length.
|
||||
#! @returns The node.
|
||||
pub def checker::run_at(&this, run: u32, index: u32): u32 {
|
||||
return this.at.extras[run + 1 + index];
|
||||
}
|
||||
|
||||
#! The nodes of a run of the file being checked, where they are, which checking
|
||||
#! never moves.
|
||||
#! @param this The checker.
|
||||
#! @param run Where the run begins.
|
||||
#! @returns The nodes.
|
||||
pub def checker::run_view(&this, run: u32): span<u32> {
|
||||
return span::<u32>::over(this.at.extras + run + 1, this.at.extras[run] as u64);
|
||||
}
|
||||
|
||||
#! How many segments a `type_name` or `path` has.
|
||||
#! @param this The checker.
|
||||
#! @param node The `type_name` or `path` node.
|
||||
#! @returns How many.
|
||||
pub def checker::segment_count(&this, node: u32): u32 {
|
||||
val run := this.left_of(node);
|
||||
return run == 0 ? 1 : this.run_length(run);
|
||||
}
|
||||
|
||||
#! The name of a segment of a `type_name` or `path`.
|
||||
#! @param this The checker.
|
||||
#! @param node The `type_name` or `path` node.
|
||||
#! @param index The segment's position.
|
||||
#! @returns The ID of its name.
|
||||
pub def checker::segment_name(&this, node: u32, index: u32): u32 {
|
||||
val run := this.left_of(node);
|
||||
return run == 0 ? this.name_of(node) : this.name_of(this.run_at(run, index));
|
||||
}
|
||||
|
||||
#! The node of a segment, where it holds type arguments, or the `type_name`
|
||||
#! itself for one of a single segment.
|
||||
#! @param this The checker.
|
||||
#! @param node The `type_name` or `path` node.
|
||||
#! @param index The segment's position.
|
||||
#! @returns The node.
|
||||
pub def checker::segment_node(&this, node: u32, index: u32): u32 {
|
||||
val run := this.left_of(node);
|
||||
return run == 0 ? node : this.run_at(run, index);
|
||||
}
|
||||
|
||||
#! The run of type arguments a segment is given, or 0.
|
||||
#! @param this The checker.
|
||||
#! @param node The `type_name` or `path` node.
|
||||
#! @param index The segment's position.
|
||||
#! @returns The run, or 0.
|
||||
pub def checker::segment_args(&this, node: u32, index: u32): u32 {
|
||||
val run := this.left_of(node);
|
||||
return run == 0 ? 0 : this.left_of(this.run_at(run, index));
|
||||
}
|
||||
@@ -0,0 +1,320 @@
|
||||
#! @file notes.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how the checker reports what is wrong: at a node of the file being
|
||||
#! checked, or of another, with the instance of a generic it was found in and
|
||||
#! a related place where there is one. A note holds a copy of its text, made
|
||||
#! once the note is admitted, so text made for a note that is left out, as one
|
||||
#! made where what is checked is quiet, is let go of with what made it.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! Reports what is wrong at the node of the file being checked.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param code What kind of mistake it is.
|
||||
#! @param detail What the note says.
|
||||
pub def checker::report(&mut this, node: u32, code: ec, detail: strview): void {
|
||||
this.report_in(this.at.file, node, code, detail);
|
||||
}
|
||||
|
||||
#! Reports what is wrong at the node, with text made for it.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param code What kind of mistake it is.
|
||||
#! @param text What the note says, which the note copies.
|
||||
pub def checker::report_text(&mut this, node: u32, code: ec, text: &string): void {
|
||||
this.report_in(this.at.file, node, code, text.span);
|
||||
}
|
||||
|
||||
#! Reports what is wrong at the node of the file.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @param node The node.
|
||||
#! @param code What kind of mistake it is.
|
||||
#! @param detail What the note says.
|
||||
pub def checker::report_in(&mut this, file: u32, node: u32, code: ec, detail: strview): void {
|
||||
this.report_place(file, node, code, detail, 0, 0, 0, "");
|
||||
}
|
||||
|
||||
#! Reports what is wrong at the declaration, in its own file, wherever checking
|
||||
#! is.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @param code What kind of mistake it is.
|
||||
#! @param detail What the note says.
|
||||
pub def checker::report_decl(&mut this, id: u32, code: ec, detail: strview): void {
|
||||
this.report_in(this.decls.files.p[id], this.decls.nodes.p[id], code, detail);
|
||||
}
|
||||
|
||||
#! Reports what is wrong at the node of the file, and a place it relates to,
|
||||
#! where it says something of one: to whatever hears the checker's notes, or in
|
||||
#! its notes.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @param node The node.
|
||||
#! @param code What kind of mistake it is.
|
||||
#! @param detail What the note says.
|
||||
#! @param related_file The file of the place it relates to.
|
||||
#! @param related_offset Where that place begins.
|
||||
#! @param related_length How many bytes it covers.
|
||||
#! @param related What the note says of it, or empty where it relates
|
||||
#! to none.
|
||||
def checker::report_place(
|
||||
&mut this,
|
||||
file: u32,
|
||||
node: u32,
|
||||
code: ec,
|
||||
detail: strview,
|
||||
related_file: u32,
|
||||
related_offset: u32,
|
||||
related_length: u32,
|
||||
related: strview
|
||||
): void {
|
||||
return if this.quiet != 0;
|
||||
# A body checked once for each case it holds in says each note once, which is
|
||||
# held until it ends.
|
||||
val key := ((file as u64) << 40) | ((node as u64) << 8) | (code as u64);
|
||||
if this.repeating != 0 {
|
||||
for &mut each in this.held_notes {
|
||||
continue if each.key != key;
|
||||
if each.serial != this.case_serial {
|
||||
each.serial = this.case_serial;
|
||||
each.passes++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.failed = true;
|
||||
this.errors++;
|
||||
return if !this.sink.admits();
|
||||
|
||||
val syntax := &this.units[file].syntax;
|
||||
val token := syntax.node_tokens[node];
|
||||
var made := note {
|
||||
file = file,
|
||||
offset = syntax.token_offsets[token],
|
||||
length = syntax.token_length(token, this.names),
|
||||
code = code,
|
||||
detail = string::from(detail, nothing {}),
|
||||
instance = this.instance_text(),
|
||||
related_file = related_file,
|
||||
related_offset = related_offset,
|
||||
related_length = related_length,
|
||||
related = string::from(related, nothing {}),
|
||||
origin = this.at.decl
|
||||
};
|
||||
if related.length == 0 {
|
||||
this.relate_instance(&mut made);
|
||||
}
|
||||
if this.repeating != 0 {
|
||||
this.held_notes.append(checker::held_note {
|
||||
key = key, serial = this.case_serial, passes = 1, made = move(&mut made), text = string::copy_of(&this.case_text)
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.sink.give(&mut made);
|
||||
}
|
||||
|
||||
#! Relates a note made in an instance to where what is written made it, where
|
||||
#! it relates to nothing else.
|
||||
#! @param this The checker.
|
||||
#! @param made The note.
|
||||
def checker::relate_instance(&mut this, made: &mut note): void {
|
||||
return if this.at.instance == 0;
|
||||
val at := this.instance_made(this.at.instance);
|
||||
return if at.node == 0;
|
||||
val syntax := &this.units[at.file].syntax;
|
||||
val token := this.made_token(at);
|
||||
made.related_file = at.file;
|
||||
made.related_offset = syntax.token_offsets[token];
|
||||
made.related_length = syntax.token_length(token, this.names);
|
||||
made.related = string::from("The instance is made here.", nothing {});
|
||||
}
|
||||
|
||||
#! Warns of what is likely wrong at the node of the file being checked, which
|
||||
#! never stops a program, nor marks what is checked as failed.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param code What kind of mistake it is about.
|
||||
#! @param detail What the note says.
|
||||
pub def checker::warn(&mut this, node: u32, code: ec, detail: strview): void {
|
||||
this.warn_place(node, code, detail, 0);
|
||||
}
|
||||
|
||||
#! Warns of what is likely wrong at the node, with text made for it, and the
|
||||
#! node declaring what it is about, where one is given.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param code What kind of mistake it is about.
|
||||
#! @param text What the note says, which the note copies.
|
||||
#! @param related The node declaring what it is about, in the same file, or 0.
|
||||
pub def checker::warn_text(&mut this, node: u32, code: ec, text: &string, related: u32): void {
|
||||
this.warn_place(node, code, text.span, related);
|
||||
}
|
||||
|
||||
#! Warns of what is likely wrong at the node: to whatever hears the checker's
|
||||
#! notes, or in its notes.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param code What kind of mistake it is about.
|
||||
#! @param detail What the note says.
|
||||
#! @param related The node declaring what it is about, in the same file, or 0.
|
||||
def checker::warn_place(&mut this, node: u32, code: ec, detail: strview, related: u32): void {
|
||||
return if this.quiet != 0 || !this.sink.admits();
|
||||
val file := this.at.file;
|
||||
val syntax := &this.units[file].syntax;
|
||||
val token := syntax.node_tokens[node];
|
||||
var made := note {
|
||||
file = file,
|
||||
offset = syntax.token_offsets[token],
|
||||
length = syntax.token_length(token, this.names),
|
||||
code = code,
|
||||
detail = string::from(detail, nothing {}),
|
||||
warning = true,
|
||||
instance = this.instance_text(),
|
||||
origin = this.at.decl
|
||||
};
|
||||
if related != 0 {
|
||||
val other := syntax.node_tokens[related];
|
||||
made.related_file = file;
|
||||
made.related_offset = syntax.token_offsets[other];
|
||||
made.related_length = syntax.token_length(other, this.names);
|
||||
made.related = string::from("It is declared here.", nothing {});
|
||||
} else {
|
||||
this.relate_instance(&mut made);
|
||||
}
|
||||
this.sink.give(&mut made);
|
||||
}
|
||||
|
||||
#! Reports what is wrong at the node, and the place of another it relates to,
|
||||
#! as where a name was first declared.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param code What kind of mistake it is.
|
||||
#! @param detail What the note says.
|
||||
#! @param file The file of the other node.
|
||||
#! @param other The other node.
|
||||
#! @param related What the note says of it.
|
||||
pub def checker::report_related(
|
||||
&mut this,
|
||||
node: u32,
|
||||
code: ec,
|
||||
detail: strview,
|
||||
file: u32,
|
||||
other: u32,
|
||||
related: strview
|
||||
): void {
|
||||
this.report_related_in(this.at.file, node, code, detail, file, other, related);
|
||||
}
|
||||
|
||||
#! Reports what is wrong at the declaration, in its own file, wherever checking
|
||||
#! is, and the place of another declaration it relates to, as where the name
|
||||
#! was first declared.
|
||||
#! @param this The checker.
|
||||
#! @param id The declaration.
|
||||
#! @param code What kind of mistake it is.
|
||||
#! @param detail What the note says.
|
||||
#! @param other The other declaration.
|
||||
#! @param related What the note says of it.
|
||||
pub def checker::report_decl_related(&mut this, id: u32, code: ec, detail: strview, other: u32, related: strview): void {
|
||||
this.report_related_in(this.decls.files.p[id], this.decls.nodes.p[id], code, detail,
|
||||
this.decls.files.p[other], this.decls.nodes.p[other], related);
|
||||
}
|
||||
|
||||
#! Reports what is wrong at the node of the file, and the place of another node
|
||||
#! it relates to.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @param node The node.
|
||||
#! @param code What kind of mistake it is.
|
||||
#! @param detail What the note says.
|
||||
#! @param other_file The file of the other node.
|
||||
#! @param other The other node.
|
||||
#! @param related What the note says of it.
|
||||
def checker::report_related_in(
|
||||
&mut this,
|
||||
file: u32,
|
||||
node: u32,
|
||||
code: ec,
|
||||
detail: strview,
|
||||
other_file: u32,
|
||||
other: u32,
|
||||
related: strview
|
||||
): void {
|
||||
val syntax := &this.units[other_file].syntax;
|
||||
val token := syntax.node_tokens[other];
|
||||
this.report_place(file, node, code, detail, other_file, syntax.token_offsets[token], syntax.token_length(token, this.names), related);
|
||||
}
|
||||
|
||||
#! What notes made in the instance being checked say of it, as "In `max<u8>`",
|
||||
#! with where what is written made it, as ", made at main.sakura:4:12", or
|
||||
#! nothing outside an instance. An instance an instance made is made by what
|
||||
#! made that one, so the place is in the program's own code where it can be.
|
||||
#! @param this The checker.
|
||||
#! @returns The text, or empty text.
|
||||
def checker::instance_text(&mut this): string {
|
||||
return string::new(nothing {}) if this.at.instance == 0;
|
||||
|
||||
val held: instance = this.instances[this.at.instance];
|
||||
var text := string::from("In `", nothing {});
|
||||
# A variable of a generic type is written through the type, as
|
||||
# `counter<u8>::count`.
|
||||
val variable := this.decls.kinds.p[held.decl] == dk::variable && this.decls.owners.p[held.decl] != 0;
|
||||
text.append(this.names.text(this.decls.names.p[variable ? this.decls.owners.p[held.decl] : held.decl]));
|
||||
text.append("<");
|
||||
# A pack's types are written as the arguments they are, as `show_all<i64, bool>`.
|
||||
this.describe_args(this.types.parts_view(held.args), &mut text);
|
||||
text.append(">");
|
||||
if variable {
|
||||
text.append("::");
|
||||
text.append(this.names.text(this.decls.names.p[held.decl]));
|
||||
}
|
||||
text.append("`");
|
||||
val made := this.instance_made(this.at.instance);
|
||||
if made.node != 0 {
|
||||
val source := &this.units[made.file].source;
|
||||
val syntax := &this.units[made.file].syntax;
|
||||
val place := source.lines.place_of(syntax.token_offsets[this.made_token(made)]);
|
||||
text.append($", made at {source.path}:{place.line}:{place.column}");
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
#! The token a note says an instance was made at: of the function a call
|
||||
#! calls, where a call made it, as `max` of `max::<u8>(a, b)`, or else of the
|
||||
#! node.
|
||||
#! @param this The checker.
|
||||
#! @param made The file and node, as `instance_made` gives them.
|
||||
#! @returns The token's index.
|
||||
def checker::made_token(&this, made: file_node): u32 {
|
||||
val syntax := &this.units[made.file].syntax;
|
||||
val node: u32 = syntax.node_kinds[made.node] == nk::call && syntax.node_lefts[made.node] != 0
|
||||
? syntax.node_lefts[made.node] : made.node;
|
||||
return syntax.node_tokens[node];
|
||||
}
|
||||
|
||||
#! Where what is written made an instance: the node first asking for it, or for
|
||||
#! the outermost instance whose checking made it, or none where nothing written
|
||||
#! asked for it.
|
||||
#! @param this The checker.
|
||||
#! @param id The instance.
|
||||
#! @returns The file and node, or node 0.
|
||||
def checker::instance_made(&this, id: u32): file_node {
|
||||
var at := id;
|
||||
while this.instances[at].made_in != 0 && this.instances[this.instances[at].made_in].made_node != 0 {
|
||||
at = this.instances[at].made_in;
|
||||
}
|
||||
return file_node { file = this.instances[at].made_file, node = this.instances[at].made_node };
|
||||
}
|
||||
|
||||
#! Counts what the checker met and does not check yet, which it lets through
|
||||
#! rather than reporting, so a program using it is not wrongly refused.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
pub def checker::unsupported(&mut this, node: u32): void {
|
||||
this.skipped++;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,342 @@
|
||||
#! @file ownership.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the rules of owning values: a value of a type that closes, as a
|
||||
#! `string`, has one owner, so it is never copied out of the place holding it
|
||||
#! without saying so; and the warning where a loop's body changes whole what the
|
||||
#! loop walks, which may move what the loop reads next.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! Whether values of the type are closed where their scope ends: the type
|
||||
#! names `owning`, or holds a value that does in its fields or
|
||||
#! elements. A reference or pointer holds nothing of its own.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Whether they are.
|
||||
pub def checker::closes(&mut this, kind: u32): bool {
|
||||
return false if kind == bt::error || this.types.is_untyped(kind);
|
||||
val flags := this.types.flags.p[kind];
|
||||
return (flags & tf::closes as u8) != 0 if (flags & tf::weighed as u8) != 0;
|
||||
# Weighed as closing nothing while it is worked out, so a type holding
|
||||
# itself through a pointer is met again as one that does not.
|
||||
this.types.flags.p[kind] = flags | tf::weighed as u8;
|
||||
|
||||
val plain := this.unalias(kind);
|
||||
var made := false;
|
||||
match this.types.kind_of(plain) {
|
||||
ty::fixed => made = this.closes(this.types.first_of(plain));
|
||||
ty::named => {
|
||||
val decl := this.types.first_of(plain);
|
||||
val kind_of: dk = this.decls.kinds.p[decl];
|
||||
if kind_of == dk::struct_type || kind_of == dk::union_type {
|
||||
val owning := this.std_decl(word::owning);
|
||||
made = owning != 0 && this.names_trait(plain, owning) && this.has_close(plain);
|
||||
if !made {
|
||||
val fields := this.field_types(plain);
|
||||
for &each in fields {
|
||||
made = made || this.closes(each);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ty::param => {
|
||||
val owning := this.std_decl(word::owning);
|
||||
made = owning != 0 && this.param_bounded(plain, owning);
|
||||
}
|
||||
# A pack held by a field closes what of it closes, and one whose types are
|
||||
# not known yet what its bounds say does.
|
||||
ty::pack => {
|
||||
if this.types.is_open_pack(plain) {
|
||||
val owning := this.std_decl(word::owning);
|
||||
made = owning != 0 && this.param_bounded(plain, owning);
|
||||
} else {
|
||||
for i in 0..this.types.part_count(plain) {
|
||||
made = made || this.closes(this.types.part_at(plain, i as u32));
|
||||
}
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
|
||||
if made {
|
||||
this.types.flags.p[kind] |= tf::closes as u8;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Whether the type has the `close(&mut this): void` of `owning`.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Whether it has.
|
||||
def checker::has_close(&mut this, kind: u32): bool {
|
||||
val found := this.find_member(kind, word::close);
|
||||
return false if found.decl == 0 || this.decls.kinds.p[found.decl] != dk::function;
|
||||
var walk := this.decls.walk_overloads(found.decl);
|
||||
while true {
|
||||
val at := walk.next();
|
||||
break if at == 0;
|
||||
continue if !this.member_seen(at);
|
||||
this.resolve(at);
|
||||
if this.decls.has(at, df::mut_this) && this.types.part_count(this.decls.types.p[at]) == 1 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Reports a value that closes copied out of the place holding it, where it is
|
||||
#! taken as a value of the type `to`. A new value, as a call gives, is moved.
|
||||
#! What a reference refers to is copied where it is read through as a whole, and
|
||||
#! its owner still holds it.
|
||||
#! @param this The checker.
|
||||
#! @param node The value's node.
|
||||
#! @param to The type it is taken as.
|
||||
pub def checker::check_owned(&mut this, node: u32, to: u32): void {
|
||||
return if node == 0 || to == bt::error;
|
||||
return if this.types.kind_of(this.unalias(to)) == ty::reference;
|
||||
val kind := this.type_at(node);
|
||||
if this.types.kind_of(kind) == ty::reference {
|
||||
return if !this.closes(to);
|
||||
var text := string::from("`", nothing {});
|
||||
val named := this.describe(to);
|
||||
text.append(named.span);
|
||||
text.append("` is closed where its scope ends, so it has one owner, which still holds what the reference refers to. Refer to it with `&` or `&mut`, or copy or move it by name.");
|
||||
this.report_text(node, ec::owned, &text);
|
||||
return;
|
||||
}
|
||||
# The field of a union is read as a copy, but the union still owns what it
|
||||
# holds and closes it, even where the union is only a call's result.
|
||||
if this.union_field(node) && this.closes(kind) && this.closes(to) {
|
||||
var text := string::from("`", nothing {});
|
||||
val named := this.describe(kind);
|
||||
text.append(named.span);
|
||||
text.append("` is closed where its scope ends, so the union holding it owns it and it cannot be copied out. Name it with `match` by `&` or `&mut`, and copy or move it by name.");
|
||||
this.report_text(node, ec::owned, &text);
|
||||
return;
|
||||
}
|
||||
# Taken as a view, as a string lends a `span<u8>` of itself, it is not copied.
|
||||
return if !this.is_place(node) || !this.closes(kind) || !this.closes(to);
|
||||
|
||||
var text := string::from("`", nothing {});
|
||||
val named := this.describe(kind);
|
||||
text.append(named.span);
|
||||
text.append("` is closed where its scope ends, so it has one owner and is not copied out of the place holding it. Refer to it with `&` or `&mut`, or copy or move it by name.");
|
||||
this.report_text(node, ec::owned, &text);
|
||||
}
|
||||
|
||||
#! Checks a pack given whole, as `...args` gives it to a call, or a literal to a
|
||||
#! field holding a pack: each value that closes is moved to where it goes,
|
||||
#! which takes it by value, leaving the pack's place empty, so the pack is a
|
||||
#! variadic parameter, which the function owns, a new value's, as a call's
|
||||
#! result's, or a place that can be written.
|
||||
#! @param this The checker.
|
||||
#! @param node The pack's node.
|
||||
#! @param pack The pack's type.
|
||||
#! @param to The pack of the types its values go to.
|
||||
pub def checker::check_pack_moved(&mut this, node: u32, pack: u32, to: u32): void {
|
||||
val plain := this.unalias(this.reached(pack));
|
||||
return if this.types.kind_of(plain) != ty::pack || this.types.kind_of(to) != ty::pack;
|
||||
return if this.kind_of(node) == nk::name || !this.closes(plain);
|
||||
var moves := false;
|
||||
for j in 0..this.types.part_count(plain) {
|
||||
val each := this.types.part_at(plain, j as u32);
|
||||
val goes := j < this.types.part_count(to) ? this.types.part_at(to, j as u32) : bt::error;
|
||||
moves = moves || (this.closes(each) && this.types.kind_of(this.unalias(goes)) != ty::reference);
|
||||
}
|
||||
return if !moves || !this.is_place(node) || this.is_writable(node);
|
||||
var text := string::from("`", nothing {});
|
||||
val named := this.describe(plain);
|
||||
text.append(named.span);
|
||||
text.append("` holds what is closed where its scope ends, which is moved out of the pack, leaving it empty, so the pack is one that can be written: a `var`'s field, or one reached through `&mut`.");
|
||||
this.report_text(node, ec::owned, &text);
|
||||
}
|
||||
|
||||
#! Whether a node reads a field of a union, as `r.value` does of a `result`.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns Whether it does.
|
||||
def checker::union_field(&mut this, node: u32): bool {
|
||||
return false if this.kind_of(node) != nk::member;
|
||||
val held := this.unalias(this.reached_value(this.type_at(this.left_of(node))));
|
||||
return this.named_kind(held) == dk::union_type;
|
||||
}
|
||||
|
||||
#! Whether a place is part of what a pointer points at, as `p[0]` and
|
||||
#! `(*p).next` are, which the compiler never closes, since a pointer owns
|
||||
#! nothing.
|
||||
#! @param this The checker.
|
||||
#! @param node The place's node.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::pointed_at(&mut this, node: u32): bool {
|
||||
var at := node;
|
||||
var steps := 0;
|
||||
while at != 0 && steps < 64 {
|
||||
steps++;
|
||||
val kind := this.kind_of(at);
|
||||
return false if kind != nk::member && kind != nk::index && !(kind == nk::unary && this.token_kind(at) == tk::star);
|
||||
at = this.left_of(at);
|
||||
return true if this.types.kind_of(this.unalias(this.type_at(at))) == ty::pointer;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Whether a node reads a property, `x.name` or `x.(d)` of one, whose value its
|
||||
#! getter makes, as a call's makes one, rather than being a place.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns Whether it does.
|
||||
pub def checker::reads_property(&this, node: u32): bool {
|
||||
val kind := this.kind_of(node);
|
||||
return false if kind != nk::member && kind != nk::member_of;
|
||||
val ref := this.ref_at(node);
|
||||
return false if ref == 0 || (ref & (ref_local | ref_type)) != 0;
|
||||
val decl: dk = this.decls.kinds.p[ref];
|
||||
return decl == dk::property || (decl == dk::function && this.decls.has(ref, df::accessor));
|
||||
}
|
||||
|
||||
#! The place a loop walks what it reads from, which changing whole may move or
|
||||
#! release: what is iterated, or what it is lent from, as `v` of a slice of it,
|
||||
#! `v[1:]`, of a property of it giving a view or an iterator, `v.span`, and of
|
||||
#! its `iter()`. What another method or property gives may hold nothing of the
|
||||
#! value, so it is no place the loop is sure to walk. It is 0 where that is no
|
||||
#! place `same_place` tells apart, or where nothing moves what the loop reads: a
|
||||
#! fixed array, a view, or an iterator, which the loop and its body advance
|
||||
#! alike.
|
||||
#! @param this The checker.
|
||||
#! @param iterated The node of what the loop iterates.
|
||||
#! @returns The place's node, or 0.
|
||||
pub def checker::walked_place(&mut this, iterated: u32): u32 {
|
||||
var at := iterated;
|
||||
var steps := 0;
|
||||
while steps < 16 {
|
||||
steps++;
|
||||
if this.kind_of(at) == nk::slice || (this.reads_property(at) && this.lends_walk(this.type_at(at))) {
|
||||
at = this.left_of(at);
|
||||
continue;
|
||||
}
|
||||
if this.kind_of(at) == nk::call {
|
||||
val callee := this.left_of(at);
|
||||
val decl := this.ref_at(at);
|
||||
if this.kind_of(callee) == nk::member && decl != 0 && (decl & (ref_local | ref_type)) == 0
|
||||
&& this.decls.kinds.p[decl] == dk::function && this.decls.has(decl, df::method)
|
||||
&& this.decls.names.p[decl] == word::iter {
|
||||
at = this.left_of(callee);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return 0 if !this.plain_place(at) || this.lends_walk(this.type_at(at));
|
||||
return 0 if this.types.kind_of(this.unalias(this.reached(this.type_at(at)))) != ty::named;
|
||||
return at;
|
||||
}
|
||||
|
||||
#! Whether a value of a type walks what something else holds without owning it:
|
||||
#! a view, or an iterator, as what `v.span` and `v.iter()` give.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns Whether it does.
|
||||
def checker::lends_walk(&mut this, kind: u32): bool {
|
||||
val held := this.unalias(this.reached(kind));
|
||||
return this.is_std(held, word::span) || this.is_std(held, word::slice)
|
||||
|| (this.types.kind_of(held) == ty::named && this.find_member(held, word::next).decl != 0);
|
||||
}
|
||||
|
||||
#! Whether a node is a place written with names, fields, `*`, and indexes that
|
||||
#! are names or constants alone, which `same_place` tells apart.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns Whether it is.
|
||||
def checker::plain_place(&mut this, node: u32): bool {
|
||||
var at := node;
|
||||
var steps := 0;
|
||||
while steps < 32 {
|
||||
steps++;
|
||||
match this.kind_of(at) {
|
||||
nk::name, nk::path => {
|
||||
val ref := this.ref_at(at);
|
||||
return ref != 0 && (ref & ref_type) == 0;
|
||||
}
|
||||
nk::member => {
|
||||
val ref := this.ref_at(at);
|
||||
return false if ref == 0 || (ref & (ref_local | ref_type)) != 0 || this.decls.kinds.p[ref] != dk::field;
|
||||
}
|
||||
nk::index => {
|
||||
val at_index := this.right_of(at);
|
||||
return false if !this.is_known(at_index) && (this.kind_of(at_index) != nk::name || this.ref_at(at_index) == 0);
|
||||
}
|
||||
nk::unary => {
|
||||
return false if this.token_kind(at) != tk::star;
|
||||
}
|
||||
else => return false;
|
||||
}
|
||||
at = this.left_of(at);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Whether two places are written alike: the same local or global, its same
|
||||
#! fields, through `*` alike, at indexes of the same names or constants.
|
||||
#! @param this The checker.
|
||||
#! @param a One place's node.
|
||||
#! @param b The other's.
|
||||
#! @returns Whether they are.
|
||||
def checker::same_place(&mut this, a: u32, b: u32): bool {
|
||||
var x := a;
|
||||
var y := b;
|
||||
var steps := 0;
|
||||
while steps < 32 {
|
||||
steps++;
|
||||
val kind := this.kind_of(x);
|
||||
return false if kind != this.kind_of(y);
|
||||
match kind {
|
||||
nk::name, nk::path => return this.ref_at(x) != 0 && this.ref_at(x) == this.ref_at(y);
|
||||
nk::member => {
|
||||
return false if this.ref_at(x) == 0 || this.ref_at(x) != this.ref_at(y);
|
||||
}
|
||||
nk::index => {
|
||||
val i := this.right_of(x);
|
||||
val j := this.right_of(y);
|
||||
if this.is_known(i) && this.is_known(j) {
|
||||
return false if this.value_at(i).magnitude != this.value_at(j).magnitude;
|
||||
} else {
|
||||
return false if this.kind_of(i) != nk::name || this.kind_of(j) != nk::name
|
||||
|| this.ref_at(i) == 0 || this.ref_at(i) != this.ref_at(j);
|
||||
}
|
||||
}
|
||||
nk::unary => {
|
||||
return false if this.token_kind(x) != tk::star || this.token_kind(y) != tk::star;
|
||||
}
|
||||
else => return false;
|
||||
}
|
||||
x = this.left_of(x);
|
||||
y = this.left_of(y);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Warns where a loop around the node walks the place it is, which changing
|
||||
#! whole, as a method taking `&mut this` does, `&mut` of it, or assigning it,
|
||||
#! may move or release, so what the loop reads next may be gone. What changes
|
||||
#! what it holds in place, as `v[i] = x` or `v.first = x`, moves nothing, and
|
||||
#! is not warned of; nor is what changes what holds it, which may not change it.
|
||||
#! @param this The checker.
|
||||
#! @param node The node of what is changed.
|
||||
pub def checker::check_walked(&mut this, node: u32): void {
|
||||
return if this.walked.length == 0;
|
||||
for i in 0..this.walked.length {
|
||||
val held := this.walked[i];
|
||||
continue if (held >> 32) as u32 != this.at.table;
|
||||
val place := (held & 0xFFFFFFFF) as u32;
|
||||
continue if !this.same_place(place, node);
|
||||
var text := string::from("`", nothing {});
|
||||
this.written_text(place, &mut text);
|
||||
text.append("` is walked by the loop around this, and changing it whole, as this may, can move or release what the loop reads next. Walk a copy of it, or its indexes, to change it.");
|
||||
this.warn_text(node, ec::lifetime, &text, 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,632 @@
|
||||
#! @file patterns.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how the type arguments written for a generic type are given to its
|
||||
#! type parameters: one each, a pack the arguments after those before it, and a
|
||||
#! pattern parameter, `def(Ts...): R`, the parts of the function type it is
|
||||
#! given, as `invokable<def(i64, bool): u8>` gives `Ts` the pack `(i64, bool)`
|
||||
#! and `R` the type `u8`. A declaration's type parameters are those its
|
||||
#! patterns name, so everything past writing its arguments sees only them.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! What marks a pattern among the entries of a written shape, as `record_shape`
|
||||
#! makes them: 0 for a type parameter written plainly, 1 for a pack, and for a
|
||||
#! pattern this mark, with how many parameter types it names shifted left by 2,
|
||||
#! 2 where the last of them is a pack, and 1 where it names its result.
|
||||
pub const checker::pattern_mark: u32 = 0x80000000;
|
||||
|
||||
#! What marks a pattern of a fixed array, `L[N]`, beside `pattern_mark`, which
|
||||
#! names its element's type and its length.
|
||||
pub const checker::array_mark: u32 = 0x40000000;
|
||||
|
||||
#! Records the written shape of a declaration's own type parameters where a
|
||||
#! pattern is among them, while the file declaring it is the one checked, so
|
||||
#! its type arguments are given their parameters from any file.
|
||||
#! @param this The checker.
|
||||
#! @param decl The declaration.
|
||||
#! @param run The run of its own type parameters as written.
|
||||
pub def checker::record_shape(&mut this, decl: u32, run: u32): void {
|
||||
var entries := vector::<u32>::new();
|
||||
var any := false;
|
||||
for &each in this.run_view(run) {
|
||||
if this.kind_of(each) == nk::type_pattern {
|
||||
val params := this.left_of(each);
|
||||
val count := this.run_length(params);
|
||||
val packed := count != 0 && this.node_has(this.run_at(params, count - 1), nf::packed);
|
||||
entries.append(checker::pattern_mark | (count << 2) | (packed ? 2 : 0) | (this.right_of(each) != 0 ? 1 : 0));
|
||||
any = true;
|
||||
} else if this.kind_of(each) == nk::array_pattern {
|
||||
entries.append(checker::pattern_mark | checker::array_mark);
|
||||
any = true;
|
||||
} else {
|
||||
entries.append(this.kind_of(each) == nk::type_param && this.node_has(each, nf::packed) ? 1 : 0);
|
||||
}
|
||||
}
|
||||
if any {
|
||||
this.shapes.set(decl, this.decls.make_list(entries.span));
|
||||
}
|
||||
}
|
||||
|
||||
#! The written shape of a declaration's own type parameters: as recorded where
|
||||
#! a pattern is among them, or else one entry for each, a pack's 1.
|
||||
#! @param this The checker.
|
||||
#! @param decl The declaration.
|
||||
#! @returns The entries, as `pattern_mark` says.
|
||||
def checker::shape_of(&this, decl: u32): vector<u32> {
|
||||
if this.shapes.get(&decl) is some(got) {
|
||||
return this.decls.list_of(got);
|
||||
}
|
||||
var made := vector::<u32>::new();
|
||||
val params: u32 = this.decls.params.p[decl];
|
||||
val wanted := this.decls.list_length(params);
|
||||
val own := this.own_param_count(decl);
|
||||
for k in (wanted >= own ? wanted - own : 0)..wanted {
|
||||
made.append(this.decls.has(this.decls.list_at(params, k as u32), df::packed) ? 1 : 0);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! How many type parameters an entry of a written shape names: a pattern one
|
||||
#! for each of its parameter types and one for its result, or of a fixed array
|
||||
#! its element's and its length's, and anything else itself.
|
||||
#! @param this The checker.
|
||||
#! @param entry The entry.
|
||||
#! @returns How many.
|
||||
def checker::named_count(&this, entry: u32): u32 {
|
||||
return 1 if (entry & checker::pattern_mark) == 0;
|
||||
return 2 if (entry & checker::array_mark) != 0;
|
||||
return ((entry & ~checker::pattern_mark) >> 2) + (entry & 1);
|
||||
}
|
||||
|
||||
#! Whether a declaration is written with a pattern among its own type
|
||||
#! parameters.
|
||||
#! @param this The checker.
|
||||
#! @param decl The declaration.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::has_patterns(&this, decl: u32): bool {
|
||||
return this.shapes.contains(&decl);
|
||||
}
|
||||
|
||||
#! The type parameter a type argument written at a place is given to: one of a
|
||||
#! type the declaration is within, one of its own, or the pack taking the rest,
|
||||
#! or 0 for a pattern, whose type parameters are given the parts of what is
|
||||
#! written.
|
||||
#! @param this The checker.
|
||||
#! @param decl The generic declaration.
|
||||
#! @param index The place of the type argument among those written.
|
||||
#! @returns The type parameter's declaration, or 0.
|
||||
pub def checker::param_written_at(&this, decl: u32, index: u32): u32 {
|
||||
val params: u32 = this.decls.params.p[decl];
|
||||
val wanted := this.decls.list_length(params);
|
||||
var shape := this.shape_of(decl);
|
||||
var own: u32 = 0;
|
||||
for &each in shape {
|
||||
own += this.named_count(each);
|
||||
}
|
||||
val lead: u32 = wanted >= own ? wanted - own : 0;
|
||||
return index < wanted ? this.decls.list_at(params, index) : 0 if index < lead;
|
||||
|
||||
var at := lead;
|
||||
for i in 0..shape.length {
|
||||
val each := shape[i];
|
||||
val packs := each == 1;
|
||||
if i as u32 == index - lead || (packs && index - lead > i as u32) {
|
||||
return (each & checker::pattern_mark) != 0 || at >= wanted ? 0 : this.decls.list_at(params, at);
|
||||
}
|
||||
at += this.named_count(each);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Gives the type arguments written for a generic type to its type parameters,
|
||||
#! after those of any type it is declared within, which are given one each: one
|
||||
#! each to those written plainly, the rest to a pack, and to a pattern the parts
|
||||
#! of the function type it is given.
|
||||
#! @param this The checker.
|
||||
#! @param decl The generic type's declaration.
|
||||
#! @param args The type arguments as written, those of the types it is
|
||||
#! declared within first.
|
||||
#! @param node The node notes are placed at.
|
||||
#! @param into What each of its type parameters is given, in order.
|
||||
#! @returns Whether they were given, which notes say why not where not.
|
||||
pub def checker::bind_written(&mut this, decl: u32, args: span<u32>, node: u32, into: &mut vector<u32>): bool {
|
||||
val params: u32 = this.decls.params.p[decl];
|
||||
val wanted := this.decls.list_length(params);
|
||||
var shape := this.shape_of(decl);
|
||||
var own: u32 = 0;
|
||||
for &each in shape {
|
||||
own += this.named_count(each);
|
||||
}
|
||||
# A type declared within a generic one takes that one's parameters first.
|
||||
val lead: u32 = wanted >= own ? wanted - own : 0;
|
||||
val count := shape.length as u32;
|
||||
val last_packs := count != 0 && shape[count as u64 - 1] == 1;
|
||||
val written := lead + count;
|
||||
|
||||
var at: u64 = 0;
|
||||
for i in 0..lead {
|
||||
break if at >= args.length;
|
||||
into.append(args[at]);
|
||||
at++;
|
||||
}
|
||||
for i in 0..count {
|
||||
val each := shape[i];
|
||||
if each == 1 {
|
||||
# A pack takes the type arguments after those before it.
|
||||
val pack := this.gather_pack(args, at, node);
|
||||
return false if pack == bt::error;
|
||||
into.append(pack);
|
||||
at = args.length;
|
||||
continue;
|
||||
}
|
||||
break if at >= args.length;
|
||||
if (each & checker::array_mark) != 0 {
|
||||
return false if !this.destructure_array(decl, args[at], node, into);
|
||||
} else if (each & checker::pattern_mark) != 0 {
|
||||
return false if !this.destructure(each, decl, args[at], node, into);
|
||||
} else {
|
||||
if this.types.kind_of(args[at]) == ty::pack {
|
||||
this.report(node, ec::generic, "A pack is passed on with `...` only for a pack parameter, as `Ts` of `box<Ts...>`.");
|
||||
return false;
|
||||
}
|
||||
into.append(args[at]);
|
||||
}
|
||||
at++;
|
||||
}
|
||||
|
||||
if at != args.length || into.length != wanted as u64 {
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.decl_text(decl));
|
||||
val words := last_packs
|
||||
? $"` takes at least {written - 1} type arguments, but is given {args.length}."
|
||||
: $"` takes {written} type arguments, but is given {args.length}.";
|
||||
text.append(words.span);
|
||||
this.report_text(node, ec::generic, &text);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Gives a pattern parameter's type parameters the parts of the function type it
|
||||
#! is given: those of its parameters before a pack each take one of the
|
||||
#! function's parameters, the pack the rest, as a variadic function's are where
|
||||
#! it is one, and its result the function's.
|
||||
#! @param this The checker.
|
||||
#! @param pattern The pattern's entry in its declaration's written shape.
|
||||
#! @param decl The generic type's declaration.
|
||||
#! @param arg The type the pattern is given.
|
||||
#! @param node The node notes are placed at.
|
||||
#! @param into What the pattern's type parameters are given, in order.
|
||||
#! @returns Whether the type fits the pattern, which a note says why not where
|
||||
#! not.
|
||||
def checker::destructure(&mut this, pattern: u32, decl: u32, arg: u32, node: u32, into: &mut vector<u32>): bool {
|
||||
val count := (pattern & ~checker::pattern_mark) >> 2;
|
||||
val packed := (pattern & 2) != 0;
|
||||
val result := (pattern & 1) != 0;
|
||||
val fixed := packed ? count - 1 : count;
|
||||
|
||||
# The address of a C function is given as the function type it is of, and a
|
||||
# type parameter that is a `signature`, as `F`, its `F::params` and `F::result`.
|
||||
val plain := this.signature_function(arg);
|
||||
val bare := this.unalias(arg);
|
||||
if plain == 0 && this.types.kind_of(bare) == ty::param {
|
||||
val sig := this.std_decl(word::signature);
|
||||
if sig != 0 && this.satisfies(bare, this.decls.types.p[sig]) {
|
||||
if fixed != 0 || !packed {
|
||||
var text := string::from("`", nothing {});
|
||||
val named := this.describe(arg);
|
||||
text.append(named.span);
|
||||
text.append("` is a signature whose parameters are not known yet, so the pattern of `");
|
||||
text.append(this.decl_text(decl));
|
||||
text.append("` takes it only where it names them as one pack, as `def(Ts...): R`.");
|
||||
this.report_text(node, ec::generic, &text);
|
||||
return false;
|
||||
}
|
||||
into.append(this.projected(bare, this.trait_member(sig, word::params), this.decls.types.p[sig]));
|
||||
if result {
|
||||
into.append(this.projected(bare, this.trait_member(sig, word::result), this.decls.types.p[sig]));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if plain == 0 {
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.decl_text(decl));
|
||||
text.append("` is given a function type where it takes `def(...)`, as `def(i64): i64`, and this is `");
|
||||
val named := this.describe(arg);
|
||||
text.append(named.span);
|
||||
text.append("`.");
|
||||
this.report_text(node, ec::generic, &text);
|
||||
return false;
|
||||
}
|
||||
|
||||
# The parameters taking one value each, before any taking the rest.
|
||||
val parts := this.types.part_count(plain);
|
||||
val variadic := this.types.second_of(plain) != 0 && parts != 0;
|
||||
val singles := variadic ? parts - 1 : parts;
|
||||
val open := variadic && this.types.is_open_pack(this.types.part_at(plain, parts - 1));
|
||||
if (packed && singles < fixed) || (!packed && (variadic || singles != fixed)) || (packed && open && singles != fixed) {
|
||||
var text := string::from("`", nothing {});
|
||||
val named := this.describe(arg);
|
||||
text.append(named.span);
|
||||
text.append(open && singles > fixed
|
||||
? "` ends in a pack whose types are not known yet, which a pattern's pack takes only alone."
|
||||
: "` does not have the parameters the pattern of `");
|
||||
if !(open && singles > fixed) {
|
||||
text.append(this.decl_text(decl));
|
||||
text.append(packed
|
||||
? "` asks for: at least as many before any taking the rest as it names before its pack."
|
||||
: "` asks for: as many as it names, none taking the rest.");
|
||||
}
|
||||
this.report_text(node, ec::generic, &text);
|
||||
return false;
|
||||
}
|
||||
|
||||
for k in 0..fixed {
|
||||
into.append(this.types.part_at(plain, k as u32));
|
||||
}
|
||||
if packed {
|
||||
into.append(open ? this.types.part_at(plain, parts - 1) : this.types.params_pack(plain, fixed));
|
||||
}
|
||||
if result {
|
||||
into.append(this.types.first_of(plain));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Gives a pattern parameter, `L[N]`, the parts of the fixed array it is given:
|
||||
#! its element's type to `L`, and its length to `N`, as a `u64`.
|
||||
#! @param this The checker.
|
||||
#! @param decl The generic type's declaration.
|
||||
#! @param arg The type the pattern is given.
|
||||
#! @param node The node notes are placed at.
|
||||
#! @param into What the pattern's type parameters are given, in order.
|
||||
#! @returns Whether the type fits the pattern, which a note says why not where
|
||||
#! not.
|
||||
def checker::destructure_array(&mut this, decl: u32, arg: u32, node: u32, into: &mut vector<u32>): bool {
|
||||
val plain := this.unalias(arg);
|
||||
if this.types.kind_of(plain) != ty::fixed {
|
||||
var text := string::from("`", nothing {});
|
||||
text.append(this.decl_text(decl));
|
||||
text.append("` is given a fixed array where it takes `L[N]`, as `f32[4]`, and this is `");
|
||||
val named := this.describe(arg);
|
||||
text.append(named.span);
|
||||
text.append("`.");
|
||||
this.report_text(node, ec::generic, &text);
|
||||
return false;
|
||||
}
|
||||
into.append(this.types.first_of(plain));
|
||||
# A length only each instance knows is given as it is.
|
||||
if this.types.part_count(plain) != 0 {
|
||||
into.append(this.types.part_at(plain, 0));
|
||||
} else {
|
||||
into.append(this.known_type(this.known_number(constant::integer(this.types.second_of(plain) as u64, false), bt::uint64)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Writes the type arguments of a type declared with patterns as they are written,
|
||||
#! a function type for each pattern, as `invokable<def(i64): u8>`.
|
||||
#! @param this The checker.
|
||||
#! @param kind The named type.
|
||||
#! @param out What it is written into.
|
||||
#! @param depth How deeply the writing has gone.
|
||||
def checker::describe_written(&this, kind: u32, out: &mut string, depth: u32): void {
|
||||
val decl := this.types.first_of(kind);
|
||||
var shape := this.shape_of(decl);
|
||||
val total := this.types.part_count(kind);
|
||||
var own: u32 = 0;
|
||||
for &each in shape {
|
||||
own += this.named_count(each);
|
||||
}
|
||||
val lead: u32 = total >= own ? total - own : 0;
|
||||
|
||||
var at: u32 = 0;
|
||||
var written := false;
|
||||
for i in 0..lead {
|
||||
this.describe_arg(this.types.part_at(kind, at), out, depth + 1, &mut written);
|
||||
at++;
|
||||
}
|
||||
for i in 0..shape.length {
|
||||
val each := shape[i];
|
||||
break if at >= total;
|
||||
if (each & checker::pattern_mark) == 0 {
|
||||
this.describe_arg(this.types.part_at(kind, at), out, depth + 1, &mut written);
|
||||
at++;
|
||||
continue;
|
||||
}
|
||||
out.append(written ? ", " : "");
|
||||
written = true;
|
||||
|
||||
# A fixed array's pattern, as the array it was given the parts of.
|
||||
if (each & checker::array_mark) != 0 {
|
||||
this.describe_into(this.types.part_at(kind, at), out, depth + 1);
|
||||
out.append("[");
|
||||
if at + 1 < total {
|
||||
this.describe_into(this.types.part_at(kind, at + 1), out, depth + 1);
|
||||
}
|
||||
out.append("]");
|
||||
at += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
# A signature `F` given whole is written as itself.
|
||||
val count := (each & ~checker::pattern_mark) >> 2;
|
||||
val packed := (each & 2) != 0;
|
||||
if count == 1 && packed && (each & 1) != 0 && at + 1 < total {
|
||||
val given := this.signature_given(this.types.part_at(kind, at), this.types.part_at(kind, at + 1));
|
||||
if given != 0 {
|
||||
this.describe_into(given, out, depth + 1);
|
||||
at += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
# The function type the pattern's type parameters were given the parts of.
|
||||
out.append("def(");
|
||||
for k in 0..count {
|
||||
break if at >= total;
|
||||
out.append(k != 0 ? ", " : "");
|
||||
val part := this.types.part_at(kind, at);
|
||||
if packed && k + 1 == count && this.types.is_known_pack(part) {
|
||||
for j in 0..this.types.part_count(part) {
|
||||
out.append(j != 0 ? ", " : "");
|
||||
this.describe_into(this.types.part_at(part, j as u32), out, depth + 1);
|
||||
}
|
||||
if this.types.second_of(part) != 0 {
|
||||
out.append("...");
|
||||
}
|
||||
} else {
|
||||
this.describe_into(part, out, depth + 1);
|
||||
}
|
||||
at++;
|
||||
}
|
||||
out.append("): ");
|
||||
if (each & 1) != 0 && at < total {
|
||||
this.describe_into(this.types.part_at(kind, at), out, depth + 1);
|
||||
at++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Writes type arguments as notes spell them, each after a comma but the
|
||||
#! first: a pack whose types are known as the types it gives, as `i64, bool`
|
||||
#! of a pack given for `Ts...`, and an empty one as none.
|
||||
#! @param this The checker.
|
||||
#! @param args The type arguments.
|
||||
#! @param out What they are written into.
|
||||
#! @param depth How deeply the writing has gone, at the arguments.
|
||||
pub def checker::describe_args(&this, args: span<u32>, out: &mut string, depth: u32 = 0): void {
|
||||
var written := false;
|
||||
for &arg in args {
|
||||
this.describe_arg(arg, out, depth, &mut written);
|
||||
}
|
||||
}
|
||||
|
||||
#! Writes a type argument after those already written: a pack whose types are
|
||||
#! known as the types it gives, or another type as it is.
|
||||
#! @param this The checker.
|
||||
#! @param arg The type argument.
|
||||
#! @param out What it is written into.
|
||||
#! @param depth How deeply the writing has gone, at the argument.
|
||||
#! @param written Whether a type argument is written before it, which it sets.
|
||||
pub def checker::describe_arg(&this, arg: u32, out: &mut string, depth: u32, written: &mut bool): void {
|
||||
if !this.types.is_known_pack(arg) {
|
||||
out.append(written ? ", " : "");
|
||||
this.describe_into(arg, out, depth);
|
||||
written = true;
|
||||
return;
|
||||
}
|
||||
for &part in this.types.parts_view(arg) {
|
||||
out.append(written ? ", " : "");
|
||||
this.describe_into(part, out, depth);
|
||||
written = true;
|
||||
}
|
||||
}
|
||||
|
||||
#! The type of a value a pack holds: its type, or for the last of a variadic
|
||||
#! function's pack, the view of the rest it holds.
|
||||
#! @param this The checker.
|
||||
#! @param pack The pack's type.
|
||||
#! @param index The value's place.
|
||||
#! @returns The type.
|
||||
pub def checker::pack_value_type(&mut this, pack: u32, index: u32): u32 {
|
||||
val part := this.types.part_at(pack, index);
|
||||
return this.types.second_of(pack) != 0 && index + 1 == this.types.part_count(pack) ? this.span_of(part) : part;
|
||||
}
|
||||
|
||||
#! The function type a signature is: a function type itself, or the one the
|
||||
#! address of a C function, `cfn<def(...): R>`, is of.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @returns The function type, or 0 where the type is no signature.
|
||||
pub def checker::signature_function(&mut this, kind: u32): u32 {
|
||||
val plain := this.unalias(kind);
|
||||
return plain if this.types.kind_of(plain) == ty::function;
|
||||
if this.is_std(plain, word::cfn) && this.types.part_count(plain) != 0 {
|
||||
val inner := this.unalias(this.types.part_at(plain, 0));
|
||||
return inner if this.types.kind_of(inner) == ty::function;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! What `signature` asks of a type it decides: a function's `result`, and its
|
||||
#! `params`, the pack of its parameters' types, variadic where it is.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param asked The declaration of the type `signature` asks for.
|
||||
#! @returns The type, or 0 where the type is no signature or the trait asking
|
||||
#! is not `signature`.
|
||||
pub def checker::signature_part(&mut this, kind: u32, asked: u32): u32 {
|
||||
val holder: u32 = this.decls.owners.p[asked];
|
||||
return 0 if holder == 0 || this.decls.names.p[holder] != word::signature || this.std_decl(word::signature) != holder;
|
||||
val function := this.signature_function(kind);
|
||||
return 0 if function == 0;
|
||||
return this.types.first_of(function) if this.decls.names.p[asked] == word::result;
|
||||
return this.parameters_of(function, 0) if this.decls.names.p[asked] == word::params;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! What a trait the compiler decides asks of a type it decides: what `signature`
|
||||
#! asks of a function, what a pointer points at or a reference refers to, its
|
||||
#! `target`, an array's `element`, and the `args` a generic type is given.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param asked The declaration of the type the trait asks for.
|
||||
#! @returns The type, or 0 where the trait asking is none of these, or the type
|
||||
#! is not one it decides.
|
||||
pub def checker::decided_part(&mut this, kind: u32, asked: u32): u32 {
|
||||
val holder: u32 = this.decls.owners.p[asked];
|
||||
return 0 if holder == 0;
|
||||
val trait_name: u32 = this.decls.names.p[holder];
|
||||
return 0 if this.std_decl(trait_name) != holder;
|
||||
return this.signature_part(kind, asked) if trait_name == word::signature;
|
||||
|
||||
val plain := this.unalias(kind);
|
||||
val name: u32 = this.decls.names.p[asked];
|
||||
val of := this.types.kind_of(plain);
|
||||
if name == word::target && ((trait_name == word::pointer && of == ty::pointer) || (trait_name == word::reference && of == ty::reference)) {
|
||||
return this.types.first_of(plain);
|
||||
}
|
||||
if name == word::element && trait_name == word::array && of == ty::fixed {
|
||||
return this.types.first_of(plain);
|
||||
}
|
||||
if name == word::args && trait_name == word::generic && this.generic_instance(plain) {
|
||||
return this.type_args_pack(plain);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Whether a type is a generic type given its type arguments: a named type of
|
||||
#! a declaration with type parameters, its own or its owner's.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type, with no alias.
|
||||
#! @returns Whether it is.
|
||||
pub def checker::generic_instance(&this, kind: u32): bool {
|
||||
return this.types.kind_of(kind) == ty::named && this.types.part_count(kind) != 0;
|
||||
}
|
||||
|
||||
#! The pack of a generic type's type arguments, in the order they are written,
|
||||
#! with what a pack parameter was given in its place. Where that is a pack whose
|
||||
#! types are not known, it is that pack where it is the only argument, since
|
||||
#! where the types after it go is not known until it is.
|
||||
#! @param this The checker.
|
||||
#! @param kind The generic type, with no alias.
|
||||
#! @returns The pack, or 0 where it cannot be one.
|
||||
pub def checker::type_args_pack(&mut this, kind: u32): u32 {
|
||||
var items := vector::<u32>::new();
|
||||
val count := this.types.part_count(kind);
|
||||
for i in 0..count {
|
||||
val part := this.types.part_at(kind, i as u32);
|
||||
if this.types.kind_of(part) != ty::pack {
|
||||
items.append(part);
|
||||
continue;
|
||||
}
|
||||
if this.types.is_open_pack(part) {
|
||||
return count == 1 ? part : 0;
|
||||
}
|
||||
items.extend(this.types.parts_view(part));
|
||||
}
|
||||
return this.types.pack(items.span, 0);
|
||||
}
|
||||
|
||||
#! The value of a constant a trait the compiler decides asks of a type it
|
||||
#! decides: whether a pointer or reference writes its target, `mutable`, and how
|
||||
#! many elements an array holds, `length`.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type.
|
||||
#! @param asked The declaration of the constant the trait asks for.
|
||||
#! @param value Where the value is written.
|
||||
#! @returns Whether the constant is one of these and the type decides it.
|
||||
pub def checker::decided_value(&mut this, kind: u32, asked: u32, value: &mut constant): bool {
|
||||
return false if asked == 0 || kind == bt::error || this.decls.kinds.p[asked] != dk::variable;
|
||||
val holder: u32 = this.decls.owners.p[asked];
|
||||
return false if holder == 0;
|
||||
val trait_name: u32 = this.decls.names.p[holder];
|
||||
return false if this.std_decl(trait_name) != holder;
|
||||
|
||||
val plain := this.unalias(kind);
|
||||
val name: u32 = this.decls.names.p[asked];
|
||||
val of := this.types.kind_of(plain);
|
||||
if name == word::mutable && ((trait_name == word::pointer && of == ty::pointer) || (trait_name == word::reference && of == ty::reference)) {
|
||||
value = constant::truth(this.types.second_of(plain) == 1);
|
||||
return true;
|
||||
}
|
||||
# Within a template, a length only each instance knows is no constant there.
|
||||
if name == word::length && trait_name == word::array && of == ty::fixed && this.types.part_count(plain) == 0 {
|
||||
value = constant::integer(this.types.second_of(plain) as u64, false);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! The constant one of a type parameter's bounds, or those an `if const` the
|
||||
#! instance decided gave it, asks for of the name, where the bound is a trait the
|
||||
#! compiler decides and the type given decides it, as `T::mutable` where
|
||||
#! `T: pointer` is given `*mut u8`.
|
||||
#! @param this The checker.
|
||||
#! @param kind The type given.
|
||||
#! @param param The type parameter's declaration.
|
||||
#! @param name The ID of the name.
|
||||
#! @returns The constant's declaration, or 0.
|
||||
pub def checker::decided_constant_of_bounds(&mut this, kind: u32, param: u32, name: u32): u32 {
|
||||
this.resolve(param);
|
||||
var bounds := vector::<u32>::new();
|
||||
val run: u32 = this.decls.children.p[param];
|
||||
for i in 0..this.decls.list_length(run) {
|
||||
bounds.append(this.decls.list_at(run, i as u32));
|
||||
}
|
||||
for i in 0..this.proved_params.length {
|
||||
if this.proved_params[i] == param {
|
||||
bounds.append(this.proved_bounds[i]);
|
||||
}
|
||||
}
|
||||
for &bound in bounds {
|
||||
continue if this.types.kind_of(bound) != ty::named;
|
||||
val member := this.trait_member(this.types.first_of(bound), name);
|
||||
var value := constant {};
|
||||
return member if this.decided_value(kind, member, &mut value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! The pack of a function type's parameter types from `from` on, variadic where
|
||||
#! the function is, or where they are only a pack whose types are not known, as
|
||||
#! those of `def(Ts...): R`, that pack.
|
||||
#! @param this The checker.
|
||||
#! @param function The function type.
|
||||
#! @param from The first parameter the pack holds.
|
||||
#! @returns The pack.
|
||||
pub def checker::parameters_of(&mut this, function: u32, from: u32): u32 {
|
||||
val count := this.types.part_count(function);
|
||||
if this.types.second_of(function) != 0 && count == from + 1 && this.types.is_open_pack(this.types.part_at(function, from)) {
|
||||
return this.types.part_at(function, from);
|
||||
}
|
||||
return this.types.params_pack(function, from);
|
||||
}
|
||||
|
||||
#! The type `F` whose `params` and `result` stand in for what a pattern's type
|
||||
#! parameters were given, where they do, so a type it was given `F` is written
|
||||
#! with it, as `invokable<F>`.
|
||||
#! @param this The checker.
|
||||
#! @param pack What the pattern's pack was given.
|
||||
#! @param result What its result was given.
|
||||
#! @returns `F`, or 0.
|
||||
def checker::signature_given(&this, pack: u32, result: u32): u32 {
|
||||
return 0 if !this.types.is_open_pack(pack) || this.types.kind_of(result) != ty::param;
|
||||
var base: u32 = 0;
|
||||
for &each in this.stand_ins {
|
||||
if each.param == this.types.first_of(pack) && this.decls.names.p[each.asked] == word::params {
|
||||
base = each.base;
|
||||
}
|
||||
}
|
||||
return 0 if base == 0;
|
||||
for &each in this.stand_ins {
|
||||
return base if each.param == this.types.first_of(result) && each.base == base && this.decls.names.p[each.asked] == word::result;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,528 @@
|
||||
#! @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);
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,415 @@
|
||||
#! @file results.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the result tables, which hold what the checker works out of each
|
||||
#! node: its type, what it refers to, and its flags, with the constant it is
|
||||
#! and the type it converts to where it has them. A file has a table of all of
|
||||
#! its nodes, and an instance of a generic a table of its template's nodes, so
|
||||
#! the same node has the results of each instance it is checked in.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! What a node's flags hold.
|
||||
pub enum rf : u16 {
|
||||
place = 0x0001, #! It names a place: a variable, parameter, field, element, or what a reference or pointer reaches.
|
||||
writable = 0x0002, #! The place can be written.
|
||||
known = 0x0004, #! Its value is known while compiling, and is in `r_values`.
|
||||
local = 0x0008, #! It declares a local, whose type is the node's type.
|
||||
mutable = 0x0010, #! The local it declares can be written.
|
||||
checked = 0x0020, #! It has been checked, which a node reached twice is checked once for.
|
||||
borrowed = 0x0040, #! A reference to it was taken for a parameter, as `&` before it would.
|
||||
leaves = 0x0080, #! A statement after which the block never goes on: it returns, breaks, or continues.
|
||||
method = 0x0100, #! A member that is a method, which a call gives `this`.
|
||||
narrowed = 0x0200, #! A binding of what an `is` test or match arm names.
|
||||
reflected = 0x0400, #! It gives descriptions of reflection, which are in `reflections`.
|
||||
invokes = 0x0800, #! A call of a value of no function type, through its `invoke`, whose `this` the callee is.
|
||||
valued = 0x1000, #! A reference taken as the value it reaches, as a binding, a pattern, `for`, or inference takes one not written with `&`.
|
||||
relent = 0x2000, #! `&` of a reference, or of a field holding one, which is that reference, since references never stack.
|
||||
adapted = 0x4000, #! A function named as a value of another function type, which a function made for it calls it as, as a call would.
|
||||
}
|
||||
|
||||
#! What a node's reference holds: a declaration, or with one of these bits, the
|
||||
#! node declaring a local, or a type a type parameter is bound to.
|
||||
pub const ref_local: u32 = 0x80000000; #! The node declaring a local, in the bits of `ref_mask`.
|
||||
pub const ref_type: u32 = 0x40000000; #! A type a type parameter is bound to, in the bits of `ref_mask`.
|
||||
pub const ref_mask: u32 = 0x3FFFFFFF; #! The bits holding the declaration, node, or type.
|
||||
|
||||
#! Adds a result table of `count` nodes from `base` of the file.
|
||||
#! @param this The checker.
|
||||
#! @param file The file whose nodes it holds results of.
|
||||
#! @param base The first node it holds.
|
||||
#! @param count How many nodes it holds.
|
||||
#! @param instance The instance it is of, or 0 for the file's own.
|
||||
#! @returns Its index.
|
||||
pub def checker::add_table(&mut this, file: u32, base: u32, count: u32, instance: u32): u32 {
|
||||
val index := this.tables.length as u32;
|
||||
this.tables.append(rtable {
|
||||
file = file,
|
||||
base = base,
|
||||
count = count,
|
||||
offset = this.r_types.length as u32,
|
||||
instance = instance
|
||||
});
|
||||
|
||||
this.resize_results(this.r_types.length + count as u64);
|
||||
return index;
|
||||
}
|
||||
|
||||
#! Makes each result column hold a number of nodes' results, the nodes added
|
||||
#! holding none.
|
||||
#! @param this The checker.
|
||||
#! @param total How many nodes' results each holds after.
|
||||
def checker::resize_results(&mut this, total: u64): void {
|
||||
this.r_types.resize(total);
|
||||
this.r_refs.resize(total);
|
||||
this.r_flags.resize(total);
|
||||
}
|
||||
|
||||
#! Copies a node's results, at one row of the results, to another.
|
||||
#! @param this The checker.
|
||||
#! @param from The row to copy from.
|
||||
#! @param to The row to copy to.
|
||||
def checker::copy_results(&mut this, from: u64, to: u64): void {
|
||||
this.r_types.p[to] = this.r_types.p[from];
|
||||
this.r_refs.p[to] = this.r_refs.p[from];
|
||||
this.r_flags.p[to] = this.r_flags.p[from];
|
||||
copy_row(&mut this.r_converts, from, to);
|
||||
copy_row(&mut this.r_values, from, to);
|
||||
}
|
||||
|
||||
# Copies what a map holds of one row to another, or takes what it holds of the
|
||||
# other where it holds nothing of the one.
|
||||
def copy_row(rows: &mut keymap, from: u64, to: u64): void {
|
||||
if rows.get(&from) is some(held) {
|
||||
rows.set(to, held);
|
||||
} else {
|
||||
rows.remove(&to);
|
||||
}
|
||||
}
|
||||
|
||||
#! Lets go of a node's results, at a row of the results.
|
||||
#! @param this The checker.
|
||||
#! @param at The row.
|
||||
def checker::drop_results(&mut this, at: u64): void {
|
||||
this.r_types.p[at] = 0;
|
||||
this.r_refs.p[at] = 0;
|
||||
this.r_flags.p[at] = 0;
|
||||
this.r_converts.remove(&at);
|
||||
this.r_values.remove(&at);
|
||||
}
|
||||
|
||||
#! Adds a result table of the nodes from `base`, within the one being written,
|
||||
#! whose nodes it holds but for its own: what a body checked once for each of
|
||||
#! several things, as `for const`, writes for each.
|
||||
#! @param this The checker.
|
||||
#! @param base The first node it holds.
|
||||
#! @param count How many nodes it holds.
|
||||
#! @returns Its index.
|
||||
pub def checker::add_child_table(&mut this, base: u32, count: u32): u32 {
|
||||
val index := this.add_table(this.at.file, base, count, this.tables.p[this.at.table].instance);
|
||||
this.tables.p[index].parent = this.at.table;
|
||||
return index;
|
||||
}
|
||||
|
||||
#! Makes the table the one results are read from and written to.
|
||||
#! @param this The checker.
|
||||
#! @param table The table's index.
|
||||
pub def checker::use_table(&mut this, table: u32): void {
|
||||
val held: rtable = this.tables.p[table];
|
||||
this.at.table = table;
|
||||
this.at.file = held.file;
|
||||
this.at.base = held.base;
|
||||
this.at.offset = held.offset;
|
||||
this.at.count = held.count;
|
||||
|
||||
val syntax := &this.units[held.file].syntax;
|
||||
this.at.kinds = syntax.node_kinds.span.data;
|
||||
this.at.tokens = syntax.node_tokens.span.data;
|
||||
this.at.lefts = syntax.node_lefts.span.data;
|
||||
this.at.rights = syntax.node_rights.span.data;
|
||||
this.at.extras = syntax.extras.span.data;
|
||||
this.at.names = syntax.token_names.span.data;
|
||||
this.at.tkinds = syntax.token_kinds.span.data;
|
||||
}
|
||||
|
||||
#! The table holding the node's results: the one being written, or the one it
|
||||
#! was made within that holds the node, or, outside an instance, the one of
|
||||
#! the file's own that does, or `no_table`.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The table's index, or `no_table`.
|
||||
def checker::holding(&this, node: u32): u32 {
|
||||
return this.at.table if node >= this.at.base && node - this.at.base < this.at.count;
|
||||
|
||||
var table: u32 = this.tables.p[this.at.table].parent;
|
||||
var steps := 0;
|
||||
while table != 0 && steps < 32 {
|
||||
val held: rtable = this.tables.p[table];
|
||||
return table if node >= held.base && node - held.base < held.count;
|
||||
table = held.parent;
|
||||
steps++;
|
||||
}
|
||||
return checker::no_table if this.tables.p[this.at.table].instance != 0;
|
||||
return this.file_table_of(this.at.file, node);
|
||||
}
|
||||
|
||||
#! The table of a file's own holding a node's results: of the nodes it was
|
||||
#! parsed with, or of those an edit added.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @param node The node.
|
||||
#! @returns The table's index, or `no_table` where the node is past the file's.
|
||||
pub def checker::file_table_of(&this, file: u32, node: u32): u32 {
|
||||
val segments := &this.units[file].segments;
|
||||
var low: u64 = 0;
|
||||
var high := segments.length;
|
||||
while low < high {
|
||||
val middle := (low + high) / 2;
|
||||
val held: rtable = this.tables.p[segments[middle]];
|
||||
if node < held.base {
|
||||
high = middle;
|
||||
} else if node - held.base >= held.count {
|
||||
low = middle + 1;
|
||||
} else {
|
||||
return segments[middle];
|
||||
}
|
||||
}
|
||||
return checker::no_table;
|
||||
}
|
||||
|
||||
#! Where a node's results are among its file's own, in the result vectors.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @param node The node.
|
||||
#! @returns The index of its results, or past their end where no table of the
|
||||
#! file holds it.
|
||||
pub def checker::file_row(&this, file: u32, node: u32): u64 {
|
||||
val table := this.file_table_of(file, node);
|
||||
return this.r_types.length if table == checker::no_table;
|
||||
val held: rtable = this.tables.p[table];
|
||||
return held.offset as u64 + (node - held.base) as u64;
|
||||
}
|
||||
|
||||
#! What says no table holds a node.
|
||||
pub const checker::no_table: u32 = 0xFFFFFFFF;
|
||||
|
||||
#! Where the node's results are in the result vectors, or past their end where
|
||||
#! no table being written holds the node. Every read and write of a result
|
||||
#! finds it here, so it is copied where it is called.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The index of its results.
|
||||
@inline
|
||||
def checker::slot(&this, node: u32): u64 {
|
||||
if node >= this.at.base && node - this.at.base < this.at.count {
|
||||
return this.at.offset as u64 + (node - this.at.base) as u64;
|
||||
}
|
||||
return this.slot_elsewhere(node);
|
||||
}
|
||||
|
||||
#! Where the results are of a node the table being written does not hold, as
|
||||
#! `slot` says, which is apart so `slot` stays small where it is copied.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The index of its results.
|
||||
def checker::slot_elsewhere(&this, node: u32): u64 {
|
||||
val table := this.holding(node);
|
||||
return this.r_types.length if table == checker::no_table;
|
||||
val held: rtable = this.tables.p[table];
|
||||
return held.offset as u64 + (node - held.base) as u64;
|
||||
}
|
||||
|
||||
#! The type checking gave a node.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns Its type, or 0 where it has none, or no table being written holds
|
||||
#! it.
|
||||
@inline
|
||||
pub def checker::type_at(&this, node: u32): u32 {
|
||||
val at := this.slot(node);
|
||||
return at < this.r_types.length ? this.r_types.p[at] : 0;
|
||||
}
|
||||
|
||||
#! Records the type of a node.
|
||||
#! @param this The checker.
|
||||
#! @param node The node, whose type is not recorded where no table being
|
||||
#! written holds it.
|
||||
#! @param kind The type.
|
||||
@inline
|
||||
pub def checker::set_type(&mut this, node: u32, kind: u32): void {
|
||||
val at := this.slot(node);
|
||||
if at < this.r_types.length {
|
||||
this.r_types.p[at] = kind;
|
||||
}
|
||||
}
|
||||
|
||||
#! What a node refers to.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns A declaration, a local or a type with its bit of `ref_local` or
|
||||
#! `ref_type`, or 0.
|
||||
@inline
|
||||
pub def checker::ref_at(&this, node: u32): u32 {
|
||||
val at := this.slot(node);
|
||||
return at < this.r_refs.length ? this.r_refs.p[at] : 0;
|
||||
}
|
||||
|
||||
#! Records what a node refers to.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param ref A declaration, or a local or a type with its bit.
|
||||
@inline
|
||||
pub def checker::set_ref(&mut this, node: u32, ref: u32): void {
|
||||
val at := this.slot(node);
|
||||
if at < this.r_refs.length {
|
||||
this.r_refs.p[at] = ref;
|
||||
}
|
||||
}
|
||||
|
||||
#! The flags of a node, as `rf` says.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns Its flags, or 0.
|
||||
@inline
|
||||
pub def checker::flags_at(&this, node: u32): u32 {
|
||||
val at := this.slot(node);
|
||||
return at < this.r_flags.length ? this.r_flags.p[at] as u32 : 0;
|
||||
}
|
||||
|
||||
#! Whether a node has a flag.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param flag The flag.
|
||||
#! @returns Whether it has it.
|
||||
@inline
|
||||
pub def checker::has_flag(&this, node: u32, flag: rf): bool {
|
||||
return (this.flags_at(node) & flag as u32) != 0;
|
||||
}
|
||||
|
||||
#! Gives a node a flag.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param flag The flag.
|
||||
@inline
|
||||
pub def checker::set_flag(&mut this, node: u32, flag: rf): void {
|
||||
val at := this.slot(node);
|
||||
if at < this.r_flags.length {
|
||||
this.r_flags.p[at] |= flag as u16;
|
||||
}
|
||||
}
|
||||
|
||||
#! Takes a flag from a node.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param flag The flag.
|
||||
@inline
|
||||
pub def checker::clear_flag(&mut this, node: u32, flag: rf): void {
|
||||
val at := this.slot(node);
|
||||
if at < this.r_flags.length {
|
||||
this.r_flags.p[at] &= ~(flag as u16);
|
||||
}
|
||||
}
|
||||
|
||||
#! The node as the table holding its results holds it, for the results kept
|
||||
#! apart from the tables: the one being written, or one around it.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns Its table and node.
|
||||
pub def checker::table_node_of(&this, node: u32): table_node {
|
||||
val table := this.holding(node);
|
||||
return table_node { table = table == checker::no_table ? this.at.table : table, node = node };
|
||||
}
|
||||
|
||||
#! Records that the node's value converts to the type, which is not its own.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param kind The type it converts to.
|
||||
pub def checker::set_convert(&mut this, node: u32, kind: u32): void {
|
||||
val at := this.slot(node);
|
||||
if at < this.r_types.length {
|
||||
if kind != 0 {
|
||||
this.r_converts.set(at, kind);
|
||||
} else {
|
||||
this.r_converts.remove(&at);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.converts.set(this.table_node_of(node), kind);
|
||||
}
|
||||
|
||||
#! The type the node's value becomes where it is used: what it converts to, or
|
||||
#! its own type.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns The type.
|
||||
pub def checker::converted(&this, node: u32): u32 {
|
||||
val at := this.slot(node);
|
||||
if at < this.r_types.length {
|
||||
return this.r_converts.get_or(&at, this.r_types.p[at]);
|
||||
}
|
||||
val key := this.table_node_of(node);
|
||||
return this.converts.get_or(&key, this.type_at(node));
|
||||
}
|
||||
|
||||
#! Records that the node's value is known while compiling.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @param value Its value.
|
||||
pub def checker::set_value(&mut this, node: u32, value: constant): void {
|
||||
val id := this.value_known(value);
|
||||
val at := this.slot(node);
|
||||
if at < this.r_types.length {
|
||||
this.r_values.set(at, id);
|
||||
this.r_flags.p[at] |= rf::known as u16;
|
||||
return;
|
||||
}
|
||||
this.values.set(this.table_node_of(node), id);
|
||||
this.set_flag(node, rf::known);
|
||||
}
|
||||
|
||||
#! The ID among `knowns` of a number, `bool`, character, member of an enum, or
|
||||
#! float, of no type in particular, as the values of nodes and declarations
|
||||
#! are kept, interned, so each value is held once however many nodes are it:
|
||||
#! most are 0, 1, or a small number.
|
||||
#! @param this The checker.
|
||||
#! @param value The value.
|
||||
#! @returns Its ID.
|
||||
pub def checker::value_known(&mut this, value: constant): u32 {
|
||||
val hash := mix(mix(value.word, value.negative ? 1 : 0), value.floating ? 1 : 0);
|
||||
var walk := this.value_index.walk(hash);
|
||||
var at := this.value_index.next(&mut walk);
|
||||
while at != 0 {
|
||||
return at - 1 if this.knowns.p[at - 1].value.same(&value);
|
||||
at = this.value_index.next(&mut walk);
|
||||
}
|
||||
val id := this.intern_known(known { kind = value.floating ? kk::real : kk::number, value = value }, span::<u32>::blank(), "");
|
||||
this.value_index.add(hash, id);
|
||||
return id;
|
||||
}
|
||||
|
||||
#! Whether the node's value is known while compiling.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns Whether it is.
|
||||
@inline
|
||||
pub def checker::is_known(&this, node: u32): bool {
|
||||
return this.has_flag(node, rf::known);
|
||||
}
|
||||
|
||||
#! The node's value, where it is known, or a zero constant: constant 0, which
|
||||
#! stands for none.
|
||||
#! @param this The checker.
|
||||
#! @param node The node.
|
||||
#! @returns Its value.
|
||||
pub def checker::value_at(&this, node: u32): constant {
|
||||
# A node a table holds says whether it has a value, which most have none of.
|
||||
val at := this.slot(node);
|
||||
if at < this.r_types.length {
|
||||
return (this.r_flags.p[at] & rf::known as u16) != 0 ? this.knowns.p[this.r_values.get_or(&at, 0)].value : constant {};
|
||||
}
|
||||
val key := this.table_node_of(node);
|
||||
return this.knowns.p[this.values.get_or(&key, 0)].value;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#! @file scopes.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the bindings of names to the locals, parameters, and type
|
||||
#! parameters they refer to. They are one stack, and each name keeps the
|
||||
#! binding of it nearest the top, so finding what a name refers to takes one
|
||||
#! read, and a scope ends by cutting the stack back to where it began.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! Where the bindings are now, which a scope ending cuts them back to.
|
||||
#! @param this The checker.
|
||||
#! @returns The mark.
|
||||
pub def checker::scope_mark(&this): u32 {
|
||||
return this.bind_names.length as u32;
|
||||
}
|
||||
|
||||
#! Binds the name to what it refers to, hiding what it referred to before
|
||||
#! until the scope ends. `_` is never bound, since nothing reads it.
|
||||
#! @param this The checker.
|
||||
#! @param name The ID of the name.
|
||||
#! @param value What it refers to: a local or type parameter with its bit of
|
||||
#! `ref_local` or `ref_type`.
|
||||
pub def checker::bind(&mut this, name: u32, value: u32): void {
|
||||
return if name == names::underscore_id || name == 0;
|
||||
|
||||
if this.innermost.length <= name as u64 {
|
||||
this.innermost.resize(name as u64 + 1);
|
||||
}
|
||||
|
||||
this.bind_names.append(name);
|
||||
this.bind_values.append(value);
|
||||
this.bind_prev.append(this.innermost[name]);
|
||||
this.innermost[name] = this.bind_names.length as u32 - 1;
|
||||
}
|
||||
|
||||
#! Ends the scopes begun since the mark.
|
||||
#! @param this The checker.
|
||||
#! @param mark Where the bindings were, as `scope_mark` gave it.
|
||||
pub def checker::unbind_to(&mut this, mark: u32): void {
|
||||
# Each name is given back what it hid from the top down, so a name bound more
|
||||
# than once since the mark is given back what it was bound to before.
|
||||
var top := this.bind_names.length;
|
||||
return if top <= mark as u64;
|
||||
while top > mark as u64 {
|
||||
top--;
|
||||
val name: u32 = this.bind_names[top];
|
||||
this.innermost[name] = this.bind_prev[top];
|
||||
}
|
||||
this.bind_names.resize(mark as u64);
|
||||
this.bind_values.resize(mark as u64);
|
||||
this.bind_prev.resize(mark as u64);
|
||||
}
|
||||
|
||||
#! What the name is bound to, or 0 where it is bound to nothing.
|
||||
#! @param this The checker.
|
||||
#! @param name The ID of the name.
|
||||
#! @returns What it is bound to, or 0.
|
||||
pub def checker::bound(&this, name: u32): u32 {
|
||||
return 0 if name as u64 >= this.innermost.length;
|
||||
|
||||
# What the body checked around a declaration checked within it binds is no
|
||||
# binding of the declaration's.
|
||||
val at: u32 = this.innermost[name];
|
||||
return 0 if at < this.at.marks;
|
||||
return this.bind_values[at];
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,583 @@
|
||||
#! @file types.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the type table, which gives every type the checker meets an ID of
|
||||
#! its own. A type is interned by its kind, two numbers, and its parts, so two
|
||||
#! types are the same exactly where their IDs are, and the parts of each are a
|
||||
#! run of one vector, as the runs of a syntax are.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! The kinds of type.
|
||||
pub enum ty : u8 {
|
||||
error, #! What a mistake gives, which fits anywhere, so a mistake is reported once.
|
||||
void_type, #! `void`, which a function giving nothing gives.
|
||||
null_type, #! The type of `null`, which becomes any pointer.
|
||||
boolean, #! `bool`.
|
||||
signed, #! `first` is its bits.
|
||||
unsigned, #! `first` is its bits.
|
||||
floating, #! `first` is its bits.
|
||||
untyped_int, #! An integer constant, an `i64` unless another number type is expected.
|
||||
untyped_uint, #! An integer written with `u`, a `uint` unless another unsigned type is expected.
|
||||
untyped_char, #! A character, a `u32` unless another integer type is expected.
|
||||
untyped_float, #! A float constant, an `f64` unless another float type is expected.
|
||||
untyped_float32, #! A float constant written with `f`, an `f32` unless an `f64` is expected.
|
||||
untyped_bool, #! A constant `true` or `false`.
|
||||
untyped_text, #! Text, a `span<u8>` or a `string`, whichever is expected.
|
||||
empty, #! The element of `[]`, which becomes any element.
|
||||
reference, #! `first` is what it refers to, and `second` is 1 where it writes.
|
||||
pointer, #! `first` is what it points at, and `second` is 1 where it writes.
|
||||
fixed, #! `first` is its element, and `second` how many it holds, or within a template its one part, a constant only each instance knows.
|
||||
function, #! `first` is its result, `parts` its parameters, and `second` 1 where the last is variadic.
|
||||
named, #! `first` is its declaration, and `parts` its type arguments.
|
||||
param, #! `first` is the declaration of the type parameter it is.
|
||||
pack, #! `parts` are its types, or `first` is the declaration of the pack it is, while unknown.
|
||||
value, #! A constant type argument: `first` is the type of its value, `second` its ID among the checker's known values, and `parts` the constant parameters it is worked out from, within a template.
|
||||
matched, #! `match const T { ... }` as a type, while `T` holds a type parameter: `parts` are `T`, then a pattern and the type it gives for each of the arms' patterns, a pattern of 0 being `else`, and `first` and `second` the file and node it was written at.
|
||||
}
|
||||
|
||||
#! What a type's `flags` hold: what is so of it, which interning it works out,
|
||||
#! and what the checker worked out of it and keeps, which `forget` lets go of.
|
||||
pub enum tf : u8 {
|
||||
open = 0x01, #! It holds a type parameter anywhere within.
|
||||
holds = 0x02, #! It holds a constant, or a fixed array whose length is one, anywhere within.
|
||||
asked = 0x04, #! Whether it is an alias was worked out, which `alias` says.
|
||||
alias = 0x08, #! It is an alias.
|
||||
weighed = 0x10, #! Whether values of it close was worked out, which `closes` says.
|
||||
closes = 0x20, #! Values of it close, or hold what does.
|
||||
reported = 0x40, #! A `match const` type no arm of which fits, or that is not chosen within as many steps as aliases are followed, was reported.
|
||||
}
|
||||
|
||||
#! Lets go of what the checker worked out of each type and kept in its flags,
|
||||
#! which a change may have changed.
|
||||
#! @param this The type table.
|
||||
pub def type_table::forget(&mut this): void {
|
||||
val kept := tf::open as u8 | tf::holds as u8;
|
||||
for id in 0..this.flags.length {
|
||||
this.flags.p[id] &= kept;
|
||||
}
|
||||
}
|
||||
|
||||
#! The IDs of the types every program has, which the type table interns first
|
||||
#! and in this order.
|
||||
pub enum bt : u32 {
|
||||
#! The error type.
|
||||
error,
|
||||
#! `void`.
|
||||
void_type,
|
||||
#! The type of `null`.
|
||||
null_type,
|
||||
#! `bool`.
|
||||
boolean,
|
||||
#! `i8`.
|
||||
int8,
|
||||
#! `i16`.
|
||||
int16,
|
||||
#! `i32`.
|
||||
int32,
|
||||
#! `i64`.
|
||||
int64,
|
||||
#! `u8`.
|
||||
uint8,
|
||||
#! `u16`.
|
||||
uint16,
|
||||
#! `u32`.
|
||||
uint32,
|
||||
#! `u64`.
|
||||
uint64,
|
||||
#! `f32`.
|
||||
float32,
|
||||
#! `f64`.
|
||||
float64,
|
||||
#! An integer constant.
|
||||
untyped_int,
|
||||
#! An integer constant written with `u`.
|
||||
untyped_uint,
|
||||
#! A character.
|
||||
untyped_char,
|
||||
#! A float constant.
|
||||
untyped_float,
|
||||
#! A float constant written with `f`.
|
||||
untyped_float32,
|
||||
#! A constant `true` or `false`.
|
||||
untyped_bool,
|
||||
#! Text written as a literal.
|
||||
untyped_text,
|
||||
#! The element of `[]`.
|
||||
empty,
|
||||
#! How many built-in types there are, which is the ID of the first other type.
|
||||
count,
|
||||
}
|
||||
|
||||
#! Every type, by its ID, a field in a column of its own.
|
||||
pub struct type_table {
|
||||
var kinds: column<ty>; #! The kind of each type.
|
||||
var firsts: column<u32>; #! The first number of each type, which its kind says the meaning of.
|
||||
var seconds: column<u32>; #! The second number of each type.
|
||||
var parts: column<u32>; #! The run of each type's parts in `runs`, 0 where it has none.
|
||||
pub var flags: column<u8>; #! What is so of each type, as `tf` says: what it holds, and what was worked out of it.
|
||||
var runs: column<u32>; #! The runs of parts, each a count and then the parts.
|
||||
var index: id_index; #! The types of each hash, which interning finds them by.
|
||||
var scratch: column<u32>; #! The parts of the types substituting is making, those of each inner one past those of the ones around it.
|
||||
}
|
||||
|
||||
#! Makes a type table holding the types every program has.
|
||||
#! @returns The table, in which the IDs of `bt` are those types.
|
||||
pub def type_table::new(): type_table {
|
||||
var made := type_table {
|
||||
kinds = column::<ty>::new(),
|
||||
firsts = column::<u32>::new(),
|
||||
seconds = column::<u32>::new(),
|
||||
parts = column::<u32>::new(),
|
||||
flags = column::<u8>::new(),
|
||||
runs = column::<u32>::new(),
|
||||
index = id_index::new(),
|
||||
scratch = column::<u32>::new()
|
||||
};
|
||||
|
||||
made.runs.append(0);
|
||||
val none := span::<u32>::blank();
|
||||
made.intern(ty::error, 0, 0, none);
|
||||
made.intern(ty::void_type, 0, 0, none);
|
||||
made.intern(ty::null_type, 0, 0, none);
|
||||
made.intern(ty::boolean, 8, 0, none);
|
||||
made.intern(ty::signed, 8, 0, none);
|
||||
made.intern(ty::signed, 16, 0, none);
|
||||
made.intern(ty::signed, 32, 0, none);
|
||||
made.intern(ty::signed, 64, 0, none);
|
||||
made.intern(ty::unsigned, 8, 0, none);
|
||||
made.intern(ty::unsigned, 16, 0, none);
|
||||
made.intern(ty::unsigned, 32, 0, none);
|
||||
made.intern(ty::unsigned, 64, 0, none);
|
||||
made.intern(ty::floating, 32, 0, none);
|
||||
made.intern(ty::floating, 64, 0, none);
|
||||
made.intern(ty::untyped_int, 0, 0, none);
|
||||
made.intern(ty::untyped_uint, 0, 0, none);
|
||||
made.intern(ty::untyped_char, 0, 0, none);
|
||||
made.intern(ty::untyped_float, 0, 0, none);
|
||||
made.intern(ty::untyped_float32, 0, 0, none);
|
||||
made.intern(ty::untyped_bool, 0, 0, none);
|
||||
made.intern(ty::untyped_text, 0, 0, none);
|
||||
made.intern(ty::empty, 0, 0, none);
|
||||
expects(made.kinds.length == bt::count as u64);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! How many types there are.
|
||||
#! @param this The type table.
|
||||
#! @returns The number of types, which is one more than the newest's ID.
|
||||
pub val type_table::count: u64 { get => this.kinds.length; }
|
||||
|
||||
#! The ID of the type of the kind, numbers, and parts, made where it is new.
|
||||
#! @param this The type table.
|
||||
#! @param kind The type's kind.
|
||||
#! @param first Its first number, which its kind says the meaning of.
|
||||
#! @param second Its second number.
|
||||
#! @param items Its parts, in order.
|
||||
#! @returns The type's ID.
|
||||
pub def type_table::intern(&mut this, kind: ty, first: u32, second: u32, items: span<u32>): u32 {
|
||||
var hash := mix(mix(mix(items.length, kind as u64), first as u64), second as u64);
|
||||
for &item in items {
|
||||
hash = mix(hash, item as u64);
|
||||
}
|
||||
|
||||
var walk := this.index.walk(hash);
|
||||
var at := this.index.next(&mut walk);
|
||||
while at != 0 {
|
||||
val id := at - 1;
|
||||
if this.kinds.p[id] == kind && this.firsts.p[id] == first && this.seconds.p[id] == second
|
||||
&& this.same_parts(id, items) {
|
||||
return id;
|
||||
}
|
||||
at = this.index.next(&mut walk);
|
||||
}
|
||||
|
||||
# A type is open where it is a type parameter, or holds one.
|
||||
var open := kind == ty::param || (kind == ty::pack && items.length == 0 && first != 0);
|
||||
if kind == ty::reference || kind == ty::pointer || kind == ty::fixed || kind == ty::function
|
||||
|| kind == ty::value {
|
||||
open = open || this.is_open(first);
|
||||
}
|
||||
# A type holds a constant where it is one, is a fixed array whose length is one,
|
||||
# or holds either, which only substituting within it may work out.
|
||||
var held := kind == ty::value || (kind == ty::fixed && items.length != 0);
|
||||
if kind == ty::reference || kind == ty::pointer || kind == ty::fixed || kind == ty::function {
|
||||
held = held || this.holds_value(first);
|
||||
}
|
||||
for &item in items {
|
||||
open = open || this.is_open(item as u32);
|
||||
held = held || this.holds_value(item as u32);
|
||||
}
|
||||
|
||||
var run: u32 = 0;
|
||||
if items.length != 0 {
|
||||
run = this.runs.length as u32;
|
||||
this.runs.append(items.length as u32);
|
||||
this.runs.extend(items);
|
||||
}
|
||||
|
||||
val id := this.kinds.length as u32;
|
||||
this.kinds.append(kind);
|
||||
this.firsts.append(first);
|
||||
this.seconds.append(second);
|
||||
this.parts.append(run);
|
||||
this.flags.append((open ? tf::open as u8 : 0) | (held ? tf::holds as u8 : 0));
|
||||
this.index.add(hash, id);
|
||||
return id;
|
||||
}
|
||||
|
||||
def type_table::same_parts(&this, id: u32, items: span<u32>): bool {
|
||||
val run: u32 = this.parts.p[id];
|
||||
val count: u32 = run == 0 ? 0 : this.runs.p[run];
|
||||
return false if count as u64 != items.length;
|
||||
for i in 0..items.length {
|
||||
return false if this.runs.p[run as u64 + 1 + i] != items[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The kind of a type.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Its kind.
|
||||
pub def type_table::kind_of(&this, id: u32): ty { return this.kinds.p[id]; }
|
||||
#! The first number of a type, which its kind says the meaning of.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns The number.
|
||||
pub def type_table::first_of(&this, id: u32): u32 { return this.firsts.p[id]; }
|
||||
#! The second number of a type, which its kind says the meaning of.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns The number.
|
||||
pub def type_table::second_of(&this, id: u32): u32 { return this.seconds.p[id]; }
|
||||
#! Whether a type holds a type parameter anywhere within, so it stands for other
|
||||
#! types once the parameter is known.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it holds one.
|
||||
pub def type_table::is_open(&this, id: u32): bool { return (this.flags.p[id] & tf::open as u8) != 0; }
|
||||
#! Whether a type holds a constant, or a fixed array whose length is one,
|
||||
#! anywhere within.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it holds one.
|
||||
pub def type_table::holds_value(&this, id: u32): bool { return (this.flags.p[id] & tf::holds as u8) != 0; }
|
||||
|
||||
#! How many parts the type has: the parameters of a function, the type
|
||||
#! arguments of a named type, or the types of a pack.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns How many parts it has.
|
||||
pub def type_table::part_count(&this, id: u32): u32 {
|
||||
val run: u32 = this.parts.p[id];
|
||||
return run == 0 ? 0 : this.runs.p[run];
|
||||
}
|
||||
|
||||
#! A part of a type, by its position.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @param index The part's position, below `part_count`.
|
||||
#! @returns The part's ID.
|
||||
pub def type_table::part_at(&this, id: u32, index: u32): u32 {
|
||||
return this.runs.p[this.parts.p[id] as u64 + 1 + index as u64];
|
||||
}
|
||||
|
||||
#! The parts of the type, where they are.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns The IDs of its parts, in order, a view only good until a type with
|
||||
#! parts is interned, so a loop that may intern one walks them by
|
||||
#! `part_at`, or copies them by `parts_of`.
|
||||
pub def type_table::parts_view(&this, id: u32): span<u32> {
|
||||
val run: u32 = this.parts.p[id];
|
||||
return span::<u32>::over(this.runs.p + run + 1, run == 0 ? 0 : this.runs.p[run] as u64);
|
||||
}
|
||||
|
||||
#! The parts of the type, copied, since interning may move where they are.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns The IDs of its parts, in order.
|
||||
pub def type_table::parts_of(&this, id: u32): vector<u32> {
|
||||
var made := vector::<u32>::new();
|
||||
made.extend(this.parts_view(id));
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The type of a reference.
|
||||
#! @param this The type table.
|
||||
#! @param referent The type it refers to.
|
||||
#! @param mutable Whether it writes, as `&mut` does.
|
||||
#! @returns The reference's ID.
|
||||
pub def type_table::reference(&mut this, referent: u32, mutable: bool): u32 {
|
||||
return this.intern(ty::reference, referent, mutable ? 1 : 0, span::<u32>::blank());
|
||||
}
|
||||
|
||||
#! The type of a pointer.
|
||||
#! @param this The type table.
|
||||
#! @param pointee The type it points at.
|
||||
#! @param mutable Whether it writes, as `*mut` does.
|
||||
#! @returns The pointer's ID.
|
||||
pub def type_table::pointer(&mut this, pointee: u32, mutable: bool): u32 {
|
||||
return this.intern(ty::pointer, pointee, mutable ? 1 : 0, span::<u32>::blank());
|
||||
}
|
||||
|
||||
#! The type of a fixed array.
|
||||
#! @param this The type table.
|
||||
#! @param element The type of its elements.
|
||||
#! @param count How many it holds.
|
||||
#! @returns The array's ID.
|
||||
pub def type_table::fixed(&mut this, element: u32, count: u32): u32 {
|
||||
return this.intern(ty::fixed, element, count, span::<u32>::blank());
|
||||
}
|
||||
|
||||
#! The type of a fixed array whose length only each instance of a template knows.
|
||||
#! @param this The type table.
|
||||
#! @param element The type of its elements.
|
||||
#! @param length The constant parameter or value it holds as many as.
|
||||
#! @returns The array's ID.
|
||||
pub def type_table::fixed_of(&mut this, element: u32, length: u32): u32 {
|
||||
var parts := vector::<u32>::new();
|
||||
parts.append(length);
|
||||
return this.intern(ty::fixed, element, 0, parts.span);
|
||||
}
|
||||
|
||||
#! The type of a function.
|
||||
#! @param this The type table.
|
||||
#! @param params The types of its parameters, in order.
|
||||
#! @param result The type it gives.
|
||||
#! @param variadic Whether its last parameter takes any number of values.
|
||||
#! @returns The function's ID.
|
||||
pub def type_table::function(&mut this, params: span<u32>, result: u32, variadic: bool): u32 {
|
||||
return this.intern(ty::function, result, variadic ? 1 : 0, params);
|
||||
}
|
||||
|
||||
#! The type a declaration names, given its type arguments.
|
||||
#! @param this The type table.
|
||||
#! @param decl The declaration of the struct, union, enum, trait, or alias.
|
||||
#! @param args Its type arguments, or none where it is not generic.
|
||||
#! @returns The type's ID.
|
||||
pub def type_table::named(&mut this, decl: u32, args: span<u32>): u32 {
|
||||
return this.intern(ty::named, decl, 0, args);
|
||||
}
|
||||
|
||||
#! The type a type parameter is, while what it stands for is not known.
|
||||
#! @param this The type table.
|
||||
#! @param decl The type parameter's declaration.
|
||||
#! @returns The type's ID.
|
||||
pub def type_table::param(&mut this, decl: u32): u32 {
|
||||
return this.intern(ty::param, decl, 0, span::<u32>::blank());
|
||||
}
|
||||
|
||||
#! The pack of the types, or while they are unknown, the pack of the parameter.
|
||||
#! @param this The type table.
|
||||
#! @param items The types the pack holds, or none while they are unknown.
|
||||
#! @param decl The declaration of the pack parameter, while its types are
|
||||
#! unknown.
|
||||
#! @returns The pack's ID.
|
||||
pub def type_table::pack(&mut this, items: span<u32>, decl: u32): u32 {
|
||||
return this.intern(ty::pack, items.length == 0 ? decl : 0, 0, items);
|
||||
}
|
||||
|
||||
#! The pack of the parameters of a variadic function, whose last takes any number
|
||||
#! of values of its type, as `(str, i64...)` of `def(str, i64...): void`. Spread
|
||||
#! into a function's parameters, it makes the function variadic.
|
||||
#! @param this The type table.
|
||||
#! @param items The types of the parameters, the last that of the values it
|
||||
#! takes.
|
||||
#! @returns The pack's ID.
|
||||
pub def type_table::variadic_pack(&mut this, items: span<u32>): u32 {
|
||||
return this.intern(ty::pack, 0, 1, items);
|
||||
}
|
||||
|
||||
#! The pack of a function type's parameters, variadic where it is.
|
||||
#! @param this The type table.
|
||||
#! @param id The function type's ID.
|
||||
#! @param from The first parameter the pack holds.
|
||||
#! @returns The pack's ID.
|
||||
pub def type_table::params_pack(&mut this, id: u32, from: u32): u32 {
|
||||
var items := vector::<u32>::new();
|
||||
for i in from..this.part_count(id) {
|
||||
items.append(this.part_at(id, i as u32));
|
||||
}
|
||||
return this.seconds.p[id] != 0 && items.length != 0 ? this.variadic_pack(items.span) : this.pack(items.span, 0);
|
||||
}
|
||||
|
||||
#! Whether the type is an integer of a known size, or an integer constant.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_integer(&this, id: u32): bool {
|
||||
return match this.kinds.p[id] {
|
||||
ty::signed, ty::unsigned, ty::untyped_int, ty::untyped_uint, ty::untyped_char => true,
|
||||
else => false
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether a type is a float of a known size, or a float constant.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_float(&this, id: u32): bool {
|
||||
return match this.kinds.p[id] {
|
||||
ty::floating, ty::untyped_float, ty::untyped_float32 => true,
|
||||
else => false
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether a type is a number: an integer or a float, of a known size or a
|
||||
#! constant.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_numeric(&this, id: u32): bool {
|
||||
return this.is_integer(id) || this.is_float(id);
|
||||
}
|
||||
|
||||
#! Whether a type is `bool`, or a constant `true` or `false`.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_boolean(&this, id: u32): bool {
|
||||
val kind: ty = this.kinds.p[id];
|
||||
return kind == ty::boolean || kind == ty::untyped_bool;
|
||||
}
|
||||
|
||||
#! Whether the type is a constant's, which takes the type expected of it.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_untyped(&this, id: u32): bool {
|
||||
return match this.kinds.p[id] {
|
||||
ty::untyped_int, ty::untyped_uint, ty::untyped_char, ty::untyped_float,
|
||||
ty::untyped_float32, ty::untyped_bool, ty::untyped_text, ty::empty, ty::null_type => true,
|
||||
else => false
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether the type is a signed integer, of a known size or not.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_signed(&this, id: u32): bool {
|
||||
val kind: ty = this.kinds.p[id];
|
||||
return kind == ty::signed || kind == ty::untyped_int;
|
||||
}
|
||||
|
||||
#! The numeric type an untyped one becomes where nothing else is expected.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns The type it becomes, or the type itself where it is not untyped.
|
||||
pub def type_table::settled(&this, id: u32): u32 {
|
||||
return match this.kinds.p[id] {
|
||||
ty::untyped_int => bt::int64,
|
||||
ty::untyped_uint => bt::uint64,
|
||||
ty::untyped_char => bt::uint32,
|
||||
ty::untyped_float => bt::float64,
|
||||
ty::untyped_float32 => bt::float32,
|
||||
ty::untyped_bool => bt::boolean,
|
||||
else => id
|
||||
};
|
||||
}
|
||||
|
||||
#! The type with each type parameter `from` names replaced by the type `to`
|
||||
#! gives at the same index. A type holding no parameter is itself.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @param from The type parameters to replace.
|
||||
#! @param to The types to replace each with.
|
||||
#! @returns The type made.
|
||||
pub def type_table::substitute(&mut this, id: u32, from: span<u32>, to: span<u32>): u32 {
|
||||
return id if !this.is_open(id) || from.length == 0;
|
||||
|
||||
val kind: ty = this.kinds.p[id];
|
||||
val first: u32 = this.firsts.p[id];
|
||||
match kind {
|
||||
ty::param => {
|
||||
for i in 0..from.length {
|
||||
return to[i] if from[i] == first && i < to.length;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
ty::reference, ty::pointer => {
|
||||
val inner := this.substitute(first, from, to);
|
||||
return this.intern(kind, inner, this.seconds.p[id], span::<u32>::blank());
|
||||
}
|
||||
# A length only each instance knows is given its parameter's argument, which the
|
||||
# checker then works out.
|
||||
ty::fixed => {
|
||||
val inner := this.substitute(first, from, to);
|
||||
val mark := this.scratch.length;
|
||||
for i in 0..this.part_count(id) {
|
||||
val part := this.substitute(this.part_at(id, i as u32), from, to);
|
||||
this.scratch.append(part);
|
||||
}
|
||||
return this.intern_scratch(kind, inner, this.seconds.p[id], mark);
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
|
||||
# A pack parameter stands for the pack it is given, whose types it spreads into
|
||||
# the parts around it, as `Ts` in `def(Ts...): void`.
|
||||
if kind == ty::pack && this.part_count(id) == 0 {
|
||||
for i in 0..from.length {
|
||||
return to[i] if from[i] == first && i < to.length;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
# A pack given for a pack parameter among a function's parameters becomes a
|
||||
# parameter for each of its types, so the function is no longer variadic,
|
||||
# unless the pack is a variadic function's parameters. A named type holds the
|
||||
# pack as the one argument it is, as `Ts` of `box<Ts...>`. The parts are put
|
||||
# on the scratch column past those of the substitutions around this one, each
|
||||
# part's own substitution taking what it put there off again, so they lie
|
||||
# together where interning them reads them.
|
||||
val mark := this.scratch.length;
|
||||
var spread := false;
|
||||
var rest: u32 = 0;
|
||||
for i in 0..this.part_count(id) {
|
||||
val written: u32 = this.part_at(id, i as u32);
|
||||
val part := this.substitute(written, from, to);
|
||||
val known_pack := this.kinds.p[part] == ty::pack && this.firsts.p[part] == 0;
|
||||
if known_pack && this.kinds.p[written] == ty::pack && kind == ty::function {
|
||||
this.scratch.extend(this.parts_view(part));
|
||||
spread = true;
|
||||
rest = this.seconds.p[part];
|
||||
continue;
|
||||
}
|
||||
this.scratch.append(part);
|
||||
}
|
||||
|
||||
val result := kind == ty::function ? this.substitute(first, from, to) : first;
|
||||
val second: u32 = kind == ty::function && spread ? rest : this.seconds.p[id];
|
||||
return this.intern_scratch(kind, result, second, mark);
|
||||
}
|
||||
|
||||
# Interns a type whose parts are those on the scratch column from `mark`, then
|
||||
# takes them off it.
|
||||
def type_table::intern_scratch(&mut this, kind: ty, first: u32, second: u32, mark: u64): u32 {
|
||||
val made := this.intern(kind, first, second, span::<u32>::over(this.scratch.p + mark, this.scratch.length - mark));
|
||||
this.scratch.resize(mark);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Whether the type is a pack whose types are not yet known: a pack parameter.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_open_pack(&this, id: u32): bool {
|
||||
return this.kinds.p[id] == ty::pack && this.part_count(id) == 0 && this.firsts.p[id] != 0;
|
||||
}
|
||||
|
||||
#! Whether the type is a pack whose types are known: one a pack parameter was
|
||||
#! given, which is written as the types it gives.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_known_pack(&this, id: u32): bool {
|
||||
return this.kinds.p[id] == ty::pack && !this.is_open_pack(id);
|
||||
}
|
||||
Reference in new issue
Block a user