Files
sakurac/core/attribute.sakura
T

122 lines
6.0 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file attribute.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the `attribute` trait a struct declares to be written after `@`, the
#! `usage` attribute saying what each attribute may be written on, and the
#! attributes the compiler itself reads: `optional`, `intrinsic`, `nodiscard`,
#! `allow`, `inline`, `kept`, and `sealed`.
module core;
#! Marks a struct as one that may be written after `@` on a declaration, as
#! `@test def parses(): void {}`.
#!
#! It asks for no methods: a struct is an attribute only by declaring it, as
#! `struct test : attribute`, and only a struct can be one, since an attribute
#! is written as a struct literal. What it may be written on is said by
#! `@usage`.
pub trait attribute {}
#! The kinds of declaration an attribute may be written on, which `usage`
#! combines, as `target::functions | target::constants`.
pub enum target : u32 {
functions = 0x0001, #! Functions declared outside any type.
methods = 0x0002, #! Functions declared on a type, as `def list<T>::push`.
constants = 0x0004, #! Constants, including a type's own, as `const u8::top`.
variables = 0x0008, #! Variables outside any function, including a type's own.
fields = 0x0010, #! Fields of structs and unions.
structs = 0x0020, #! Structs.
unions = 0x0040, #! Unions.
enums = 0x0080, #! Enums.
members = 0x0100, #! Members of enums.
traits = 0x0200, #! Traits.
requirements = 0x0400, #! What a trait declares: its methods, properties, and constants.
aliases = 0x0800, #! Declarations of aliases, as `type id = u64`.
properties = 0x1000, #! Properties, both of whose accessors share their attributes.
parameters = 0x2000, #! Parameters of functions, written before the parameter's name.
all = 0xFFFF, #! Every kind of declaration.
}
#! Says what the attribute struct it is written on may describe.
#!
#! The compiler reads it where each attribute is written, so an attribute on a
#! declaration its usage leaves out, or a second on the same declaration, is an
#! error there. A struct without one may go anywhere, once per declaration.
@usage{ on = target::structs }
pub struct usage : attribute {
#! The kinds of declaration it may be written on.
#!
#! It is defined as a `flags32<target>` to make it explicitly convertible to
#! and from an unsigned integer. By combining targets, an attribute with a
#! non-defaulted `@usage{}` then declares what the attribute may be used on,
#! which becomes enforced by the compiler.
pub val on: flags32<target> = target::all;
#! Whether a declaration may have more than one of it.
#!
#! Multiple attributes of the same kind are typically discouraged, but allowed.
#! For example, it may be useful to an application to be able to define an
#! `@alias{ name = ... }` attribute on some object whose name can be many
#! different values.
pub val many: bool = false;
#! Whether an alias of the declaration it is written on has it too, as reflection
#! sees it.
pub val through: bool = true;
}
#! Marks a trait's method as one a type may leave out. A call reaches it where
#! the type is known to have it, and through a trait reference, the call goes to
#! the method the trait requires instead, as `at(&this)` stands in for an
#! `indexable`'s `@optional def at(&mut this, key: K): &mut V;`.
@usage{ on = target::requirements }
pub struct optional : attribute {}
#! Marks what the compiler gives, which ends at its signature: a function, as
#! `@intrinsic def size_of<T>(): u64;`, or a property whose accessors do.
@usage{ on = target::functions | target::methods | target::properties | target::structs }
pub struct intrinsic : attribute {}
#! Marks what must be handled where it is made: a function whose result each
#! call must use, or a type whose values must be, as `result<T, E>` is. A call
#! written as a statement of its own that lets such a value go is an error, and
#! `_ = f();` lets it go on purpose.
@usage{ on = target::functions | target::methods | target::requirements | target::structs | target::unions | target::enums }
pub struct nodiscard : attribute {}
#! Quiets the warnings the checker makes of a function, as of a reference kept
#! past what it refers to, where the function is right for reasons the checker
#! cannot see.
@usage{ on = target::functions | target::methods }
pub struct allow : attribute {}
#! Marks a function whose calls are each replaced by a copy of its body, at
#! every level of optimizing, `-O0` too, whatever its size. A call it cannot
#! copy stays a call: one through a trait reference, or a function value whose
#! function is not known, and the one closing a loop of calls, as a function
#! calling itself. Written on a property, it marks both accessors. A function
#! C or the compiler gives has no body to copy, so it is never `@inline`. A run
#! checking its memory copies none, so each function's locals end where its
#! frame does.
@usage{ on = target::functions | target::methods | target::properties }
pub struct inline : attribute {}
#! Marks a parameter whose argument what the function returns keeps, as a
#! `function` keeps the callable it is made of, or a thread's handle the
#! function the thread runs. What the argument refers to, the result then
#! refers to as well, so a result outliving the locals an anonymous function
#! given for it names is warned of, as the function itself would be.
@usage{ on = target::parameters }
pub struct kept : attribute {}
#! Marks a trait nothing implements, which says something of the types it is
#! given rather than asking anything of one, as
#! `@sealed pub trait closes<T> { const value := T is closeable; }`. Its
#! constants and types are its own, read through it given its type arguments,
#! as `closes::<T>::value`, and each holds a value. No type or trait names it,
#! and nothing satisfies it, so it bounds nothing and is never tested with `is`
#! or referred to.
@usage{ on = target::traits }
pub struct sealed : attribute {}