#! @file reflect.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains compile-time reflection: `decl`, which describes a declaration of #! the program, and the intrinsics that find declarations, walk what they #! declare, read their attributes, reach what they name, and report errors while #! compiling. module core; #! Describes a declaration of the program, as the compiler found it. #! #! A `decl` is plain data, made only by the intrinsics below, the way #! `call_site()` makes a `site`. Walked by `for const`, each one is known while #! compiling, so `value_of(d)` and `x.(d)` reach what it declares as though its #! name were written. pub struct decl { pub val id: u32; #! Which declaration. Meaningful only while compiling. pub val kind: target; #! What it is, as the bit `@usage` checks attributes against. pub val name: strview; #! As written: `interns_keywords`, `push`, `red`. pub val path: strview; #! Qualified: `sakura::analyzer::interns_keywords`. pub val at: site; #! Where it is declared. pub val exported: bool; #! Whether it is `pub`. pub val generic: bool; #! Whether it has type parameters of its own. } #! Gives every declaration outside a function in the program, including methods, #! a type's own constants and variables, and nested types, in the order the #! files were given and then the order they are written in. #! #! Only those the calling module could name are given, and those carrying an #! attribute whose type the calling module declares, since writing that #! attribute is consent for the module owning it to find and use what it marks. #! @returns The declarations, which are known while compiling. @intrinsic pub def declarations(): span; #! Gives the declaration of a type, as `decl_of::>()`. #! @tparam T The type, which is an instance where it is generic. #! @returns Its declaration. @intrinsic pub def decl_of(): decl; #! Gives what a type declares: the fields of a struct or union, with the fields #! of a spread in its place, the members of an enum, or what a trait requires, #! and then the methods, properties, constants, variables, and nested types #! declared on it. #! @param d The declaration of a type. #! @returns The members, in the order they are declared. @intrinsic pub def members_of(d: decl): span; #! Gives the type a field, member, method, or a type's own declaration belongs #! to. #! @param d A declaration belonging to a type. #! @returns The declaration of the type it belongs to. @intrinsic pub def owner_of(d: decl): decl; #! Gives the declarations of every attribute written on a declaration, in the #! order they are written, each a constant whose value `value_of` reads. #! @param d The declaration the attributes are written on. #! @returns The attributes, each a constant whose value `value_of` reads. @intrinsic pub def attributes_of(d: decl): span; #! Gives the values of the `@A` attributes written on a declaration: often none, #! and at most one unless the usage of `A` allows `many`. An alias also has #! those of what it aliases, when the usage of `A` says `through`. #! @tparam A The type of attribute to look for. #! @param d The declaration the attributes are written on. #! @returns Their values, which are known while compiling. @intrinsic pub def attached(d: decl): span where A: attribute; #! Stands for what a declaration names, as though its name were written here: a #! function, a constant, a member of an enum, a global variable, or an #! attribute's value. #! #! A generic function is given its type arguments after the call, as #! `value_of(d)::`. #! @param d The declaration, which must be known while compiling. #! @remarks Its type is that of what it names, rather than the `void` it is #! declared with. @intrinsic pub def value_of(d: decl): void; #! Gives the parameters of a function, including `this`, in the order they are #! declared. `type_of(p)` is the type of each, written wherever a type is, as #! `var fixture: type_of(params_of(d)[1]);`. Anything but a function has none. #! @param d The declaration of a function. #! @returns Its parameters, or none where it is not a function. @intrinsic pub def params_of(d: decl): span; #! Gives a type's name as errors spell it, without the modules of the types it #! names, as `list` or `&mut position`. #! @tparam T The type to name. #! @returns Its name, which is known while compiling. @intrinsic pub def name_of(): strview; #! Gives a type's name with the module of each type it names, as #! `std::containers::dict` or `&mut game::position`, #! which no two types share, as `decl.path` qualifies `decl.name`. #! @tparam T The type to name. #! @returns Its name, which is known while compiling. @intrinsic pub def path_of(): strview; #! Reports an error while compiling, where checking reaches the call, which a #! branch of `if const` that is not taken never does. #! @param text What the error says, as text known while compiling, which may #! be worked out, as interpolated text is. #! @param at Where the error is, which a library gives as the `at` of a #! declaration it was handed, to point at its user's code. @intrinsic pub def compile_error(text: strview, at: site = call_site()): void;