lang::typecheck::macro_queue
lang/src/typecheck/macro_queue.mo
The decl-level entry point — expand_decls, mirroring the Rust
reference's own expand_macros (rust/core/src/eval/macro_expand.rs):
one module's own already-parsed decl_list in, decl_list with every macro
resolved out.
Builds a macro registry directly from the decl_list list itself (bare
name -> definition, matching how [[lang/typecheck/macro_expand.mo]]'s
expand_term/resolve_quote already expect their own lookup
argument shaped), then walks the list once: Decl.def_macro_d/
Decl.decl_gen_d are macro DEFINITIONS, folded into the registry and
RETAINED in the output (they carry no runtime code, but the whole-graph
pass needs the definition to resolve a macro declared in another
module); Decl.macro_call_d is resolved against the
decl-gen registry and replaced by its expansion; every other decl
kind is kept, with its own embedded Term fields run through
expand_term (so term-position macro calls inside an ordinary
def/struct/... get resolved too).
Single-pass, not fixpoint — a deliberate, explicitly-flagged
scope-narrowing (see the macro-expansion plan's own Step 6 note):
the reference's expand_macros uses a work-queue that re-queues a
Decl.macro_call_d's own expansion result at the QUEUE FRONT, so a
decl-gen template whose own body itself contains a nested decl-
position macro call gets fully resolved, however deep. This
version expands a macro_call_d's result's own Term fields
(so a term-position macro nested inside a decl-gen template's
output still resolves) but does NOT re-scan the result for further
NESTED decl-position macro calls. std/derive.mo — the one real
corpus target this whole effort has — doesn't need that: its own
four decl-gen templates each produce exactly one
reflect_type_info! call and nothing nests inside it. Flagged
here, not silently assumed sufficient forever — genuine fixpoint
requeuing is a follow-up if a real template ever needs it.
pub def check_macro_declarations (decls : List Decl) : Option StringEvery macro DECLARATION in decls, checked; first error wins.
pub def check_macro_uses (decls : List Decl) : Option Stringpub struct DeriveHandlerReg {
dr_target : Identifier,
dr_macro : Identifier,
dr_module : ModulePath,
}One #[derive_handler <Target> ...] registration, attributed to the
module whose own decl list carries it. The graph registry itself
(DeclGenEntry) carries no module attribution and duplicate
diagnostics need it, so the validation walk below carries its own
entries.
pub def derive_handler_regs_of_decls (module : ModulePath) (decls : List Decl) : List DeriveHandlerRegOne module's own decls -> its registrations, tagged with that
module's path — the per-module step lang/module.mo's duplicate
validation folds over the whole graph with.
pub def find_duplicate_derive_target (regs : List DeriveHandlerReg) : Option (Pair DeriveHandlerReg DeriveHandlerReg)The first target two DIFFERENT registration macros both claim, as
the two conflicting registrations (their dr_targets match, their
dr_macros differ). Two sites would otherwise resolve last-wins —
silently wrong across module load order — so
lang/module.mo's expand_decls_graph turns a some here into a
compile error, mirroring the Rust host's register_derive_handler.
The SAME macro re-registering a target is idempotent, not a
conflict: the same physical file can sit in the graph under two
module paths at once (a multi-file check names the target module by
its file path while dependents import it by its mote-qualified
path), and that double-load must not error — dispatch is by macro
name, so same-name registrations are functionally identical.
pub def expand_decls_terms_res (expand : Term -> Result String Term) (decl_list : List Decl) : Result String (List Decl)pub def decls_have_live_term_call (lookup : Identifier -> Option Term) (decl_list : List Decl) : BoolWhether any decl's own terms still contain a call the whole-graph pass could expand (an intrinsic, or a macro the graph-wide registry knows). Uses the Result walk's short-circuit as the scan.
pub def expand_decls (decl_list : List Decl) : List DeclEntry point: expand every macro call (term- and decl-position) in
one module's own decl_list list. Registry is built from THIS list only
— cross-module macro imports (a macro def_macro_d/decl_gen_d
declared in a DIFFERENT, already-loaded module) are out of scope
for this single-pass version, matching the same scope-narrowing
this file's own doc comment already flags for nested decl-position
calls.
pub def derive_handler_for_target (registry : List DeclGenEntry) (target : Identifier) : Option Identifier#[derive <Target>] target name -> the macro that registered it via
#[derive_handler <Target> ...] (the entry's own name), looked up in
the graph registry — the registry-driven replacement for the old
hardcoded four-entry derive_macro_name map. Option.none for
anything unregistered, which per the section note below simply
contributes nothing.
pub def decl_type_attrs (d : Decl) : List AttributeThe attributes an attributed TYPE declaration carries — the two decl
shapes the reference's Decl::Type arm covers. NOTE the self-hosted
split the reference doesn't have: struct is its OWN Decl variant
here, where the reference represents both as one Inductive with
variant: Struct (rust/core/src/term.rs), which is why #[derive ...]
on a struct needs a parser change (ParseStruct/Struct's own
attrs slot) as well as this bridge.
pub def decl_derive_macro_names (registry : List DeclGenEntry) (d : Decl) : List IdentifierEvery macro name an attributed type declaration asks for.
pub def decl_derive_expands (registry : List DeclGenEntry) (d : Decl) : BoolWhether d is an attributed type declaration whose derive would
actually EXPAND — i.e. at least one of its macro names is in the
registry. The registry-dependence is what keeps the flag honest: an
#[derive BEq] in a file that never brings derive_beq into the
graph generates nothing, so it must not count as a graph change
(lang/module.mo's raw_changed/changed consumers would otherwise
splice an unexpanded macro_call_d into the codegen decl list).
pub def has_derive_expansion (registry : List DeclGenEntry) (decls : List Decl) : Boolpub def derive_name_ref (id : Identifier) : TermThe Term a synthesized macro call passes as its type argument —
the free (sentinel-indexed, DebugName.named) variable reference
lang/typecheck/name_subst.mo's own named_ref builds, and the
shape lang/module.mo's term_free_var_name reads the type's bare
name back out of.
pub def derive_bridge_decls (registry : List DeclGenEntry) (d : Decl) : List DeclThe decls an attributed type declaration generates — empty for every
other decl kind, and for an attributed type whose derive macros this
graph doesn't have. The type decl itself is NOT part of the result;
its own list already carries it (lang/module.mo's
decl_gen_subst_decls appends these right after it).