Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user