lang::meta_env

lang/src/meta_env.mo

The environment macro meta-defs are evaluated in (meta_eval_invoke, reached from meta! and reflect_type_info!): the defs reachable from the meta-def names, elaborated, plus every non-def declaration. Building it from the whole graph type-checked the entire program in post-load only to evaluate a few derive/format helpers.

A def whose elaboration fails is kept in place with a body lowering rejects (poisoned_decl), so a meta-def that reaches it fails with the elaboration error instead of evaluating the unelaborated body, and a def pulled in only by name and never lowered costs nothing.

Reachability is by name and over-approximates: a def is filed under its bare name and under the segment after its last . (an opened IO.println is referenced as println), and every def sharing a key is kept. A reference the keys miss leaves its def out, and lowering then fails on the unresolved name. Defs keep their original relative order, so lowering's first-match lookup picks the same def it would in the whole graph. The closure is taken over ELABORATED bodies, because elaboration is what introduces the dictionary and instance-method references a class call resolves to.

defref_key#
pub def ref_key (id : Identifier) : String

The name a reference or a def is matched by: its spelling after the last ::.

defterm_ref_names#
pub def term_ref_names (t : Term) (acc : List String) : List String

Every global name t references: free variables, macro variables, constructors and match-case constructor names.

defdecl_def#
pub def decl_def (d : Decl) : Option Def

The def a declaration carries, looking through a scoped_open_d.

defdecl_ref_names#
pub def decl_ref_names (d : Decl) (acc : List String) : List String

Every name a declaration's def references (type and body); a non-def references nothing here.

defelaboration_failed_marker#
pub def elaboration_failed_marker : String

The prefix of the name a poisoned def's body references; lowering reports it as an unresolved name, carrying the elaboration error.

defpoisoned_decl#
pub def poisoned_decl (d : Decl) (err : String) : Decl

d with its def's body replaced by a reference lowering cannot resolve, naming the def and the elaboration error.

defmeta_env_decls#
pub def meta_env_decls (elab : Decl -> Result String Decl) (decls : List Decl) (roots : List String) : List Decl

decls narrowed to every non-def plus the defs reachable from roots, each reachable def passed through elab exactly once (poisoned when it fails), in the original order.

defdecl_all_ref_names#
pub def decl_all_ref_names (d : Decl) (acc : List String) : List String

Every name a declaration mentions anywhere it can spell a meta-def: a def or macro body, a macro call's arguments, a decl-gen template.

defdecls_all_ref_names#
pub def decls_all_ref_names (ds : List Decl) (acc : List String) : List String