Skip to main content

Module generator

Module generator 

Source
Expand description

Code generator trait and output types.

Structs§

EnumVariantInfo
What the registry knows about one enum variant: the enum it belongs to and its payload shape.
EsmExport
One exportable, runtime-valued public declaration of a module: the raw emitted name plus whether it is a function (camelCased on emit). Returned by exportable_value_names.
EsmSymbol
One public symbol exposed by the per-module ESM analysis. Carries the declaring module-path and the declaration kind. The kind drives both the emitted-name transform (only a function is camelCased: get_orgetOr) and the import form each backend selects (value vs import type vs skip in JS) — see EsmDeclKind.
GeneratedCode
Output from code generation — consistent across all targets.
ImplicitEsmImport
One implicit cross-module import computed by implicit_esm_imports_for: the declaring module-path, the raw symbol name, and the declaration kind (so the backend can camelCase a function, route a type to import type, or skip a JS type-only name).
OutputFile
A single generated output file.
SourceInfo
Metadata for a source file referenced by a SourceMap.
SourceMap
Maps AIR source spans to generated code spans.
SourceMapEntry
A single source-map entry linking an AIR span to a target span.
SourceMapping
A single pointwise mapping from a position in generated code to a position in the originating Bock source.
TestArtifacts
The output of CodeGenerator::generate_tests: the transpiled test files plus an optional snippet appended to the entry file.
TraitDeclInfo
What the registry knows about one trait declaration: its declared generic parameters and its methods (each an AIRNode::FnDecl), partitioned by the caller via is_default_method.

Enums§

EsmDeclKind
The declaration kind of a public symbol exposed by the per-module ESM analysis. Each backend maps this to the right cross-module import form, because the JS and TS emitted shapes differ (a trait is a JS const mixin value but a TS interface type; an enum type name has no JS binding but is a TS type; a type alias is erased in JS but a TS type).
KeywordTarget
The codegen target whose reserved-word set an identifier is being escaped against. Mirrors the five v1 backends.
TestAssertion
A recognized Bock test assertion (expect(actual).<method>(...)).
VariantPayloadKind
The payload shape of an enum variant, as needed to lower a construction or a match arm in any target.

Constants§

DECL_ONLY_META
Metadata key marking a synthesised temp NodeKind::LetBinding as declare-only: it introduces the binding with no initialiser. The shared hoist_value_cf desugar emits these; every backend’s let emitter checks this key and emits the bare declaration (let x; / var x T / Rust deferred let mut x;) rather than a = <value> initialiser. The carried bock_air::stubs::Value::Bool is always true.
ESM_RUNTIME_PRELUDE_NAMES
Runtime-prelude value names that the JS/TS backends lower inline to tagged objects ({ _tag: "Some", _0: v }, { _tag: "Less" }, …) — NOT from a cross-module core.* import. The implicit-import pass and the public-symbol map must never route these through a core.option / core.compare import: the declaring module does not actually export them (they are compiler built-ins), so a real import would be an unresolved reference.
FUNCTIONAL_LIST_METHODS
The functional List built-in methods that take a closure argument and must be lowered to each target’s native iteration idiom (see desugared_list_functional_method).
INPLACE_LIST_MUTATORS
The DQ30 in-place List mutators, lowered natively per target via desugared_list_inplace_mutator. All are mut self (E5004-enforced like DQ18’s push/append); the per-method contracts:
MAP_METHODS
The built-in Map[K, V] methods this codegen lowers natively per target.
MUTATING_LIST_METHODS
The in-place List mutators (DQ18) lowered natively per target via desugared_list_mutating_method. These resolve in the checker to a Void return and require a mut receiver (enforced by the ownership pass), so each backend emits them in statement position as a value-less mutation:
OPTIONAL_METHODS
The built-in Optional[T] methods this codegen lowers on the tagged value.
ORDERING_VARIANTS
The three variants of the prelude Ordering enum (core.compare), in the order the comparison ladder produces them.
PRIMITIVE_BRIDGE_METHODS
The primitive trait-bridge methods this codegen lowers to a target intrinsic.
PRIMITIVE_CONVERSION_TARGETS
The set of primitive type names that can appear as the callee of a canonical primitive associated conversion (Prim.from(x) / Prim.try_from).
READ_ONLY_LIST_METHODS
The read-only / non-mutating List built-in methods this codegen lowers natively per target (see desugared_list_method). The in-place mutators are excluded: push/append lower via desugared_list_mutating_method (DQ18) and pop/remove_at/insert/reverse/set via desugared_list_inplace_mutator (DQ30).
RESULT_METHODS
The built-in Result[T, E] methods this codegen lowers on the tagged value.
SET_METHODS
The built-in Set[E] methods this codegen lowers natively per target.
STRING_METHODS
The built-in String methods this codegen lowers to each target’s native string ops.

Traits§

CodeGenerator
The trait all per-target code generators implement.
JsTsExprEmitter
Lowers a single AIR expression to its target string, for the shared js/ts test-file builder. Implemented by a thin adapter over each backend’s private emit context so js_ts_generate_tests can reuse the exact expression lowering the runtime tree uses (function casing, enum/Optional reps, …).

Functions§

arm_body_is_statement
Returns true if a match-arm body is a statement body — either the body is itself a statement node, or it is a { ... } block whose tail is a statement node (or which has no tail at all, e.g. a block ending in a statement with no value).
assoc_fn_def
True when an impl/trait method (an bock_air::NodeKind::FnDecl) is an associated function — it does not bind a leading self receiver, so it is reached as Type.method(...) rather than value.method(...).
classify_assertion
If stmt is an expect(actual).<assertion>(expected?) chain, classify it.
collect_class_fields
Collect every class declared across modules, mapping each class name to its field names in declaration order.
collect_const_names
Pre-scan every reached module and collect the declared names of all module-scope consts.
collect_enum_variants
Pre-scan every reached module and build the EnumVariantRegistry.
collect_exported_type_names
Pre-scan every module’s top-level type declarations and collect the names of those declared public (records, enums, traits, classes). A backend that emits a declaration-merging companion (TS’s interface Target that mirrors an impl’s prototype methods) needs this: TS requires every declaration in a merged declaration to agree on export-ness, so the companion interface must be exported exactly when the target type is. Mirrors collect_generic_decls.
collect_generic_decls
Pre-scan every reached module and build the GenericDeclRegistry. Records the generic parameters declared on each top-level RecordDecl, EnumDecl, and ClassDecl. Non-generic decls are recorded with an empty parameter list (their presence still lets a backend distinguish a known concrete type from an unknown one).
collect_public_symbol_modules
Build a map from every public top-level symbol name declared across modules to the dotted declared module-path that declares it (e.g. Iterablecore.iter). Covers functions, records, enums (the type name), traits, classes, effects, type aliases, and consts.
collect_public_symbols_for_esm
Build a map from every public top-level symbol name (the raw Bock name) declared across modules to its EsmSymbol (declaring module-path + whether it is a function). Covers functions, records, enums (and each variant’s emitted Enum_Variant factory/const name), traits, classes, effects, type aliases, and consts.
collect_record_field_names
Collect every record/class field name in the module, mapped through the backend’s cased name function (to_pascal_case for Go, to_camel_case for js/ts, identity/snake for Python). Backends use the returned set with disambiguate_method_name to detect a method whose emitted name collides with a field’s emitted name.
collect_record_names
Collect the names of every record declared across modules (the names the JS/TS backends emit as classes and construct with new Name(...)).
collect_test_fns
Collect every @test-annotated top-level function across the given modules, paired with the module’s declared path (dotted, or "" if anonymous).
collect_trait_decls
Pre-scan every reached module and build the TraitDeclRegistry.
container_recv_kind
The receiver-kind annotation value, when it is one of the built-in container categories Optional, Result, Map, or Set.
derive_output_path
Derive the output path for a generated file from its source .bock path.
derives_structural_eq
True when a RecordDecl / EnumDecl node carries the checker’s bock_types::checker::DERIVE_EQ_META_KEY stamp — the type conforms to Equatable structurally (DQ29) and declares no explicit impl, so the Rust backend adds PartialEq to its #[derive(..)] list.
desugared_list_functional_method
Recognise a desugared List functional (closure-taking) method call.
desugared_list_inplace_mutator
Recognise a desugared DQ30 in-place List mutator call (pop/remove_at/insert/reverse/set).
desugared_list_method
Recognise a desugared List built-in method call.
desugared_list_mutating_method
Recognise a desugared in-place List mutator call (push/append, DQ18).
desugared_map_method
Recognise a desugared Map[K, V] built-in method call.
desugared_optional_method
Recognise a desugared Optional[T] built-in method call.
desugared_result_method
Recognise a desugared Result[T, E] built-in method call.
desugared_self_call
Recognise a desugared instance method call.
desugared_set_method
Recognise a desugared Set[E] built-in method call.
desugared_string_method
Recognise a desugared String built-in method call.
disambiguate_method_name
Disambiguate a method’s emitted name against the type’s field names.
enum_variant_value_names
The public enum-variant value names (Color_Red, …) declared in module — the names a TS per-module file must re-export in a trailing export { … }.
escape_target_keyword
Escape name (an already case-converted value identifier) against the target’s reserved-word set: a name that collides with a keyword gets a trailing _, otherwise it is returned unchanged.
esm_relative_specifier
Compute the relative ES-module import specifier from the file that hosts module from_path to the file that hosts module to_path, both keyed on their declared dotted module-paths and laid out at the mirrored path (core.optioncore/option.<ext>). The entry module is always at the build root as main.<ext> regardless of its declared path, so callers pass the empty string as from_path for the entry file.
explicitly_imported_names
Names brought into scope by module’s explicit use declarations (the imported leaf names and their aliases) — already emitted as real imports, so the implicit-import pass must skip them.
exportable_value_names
The set of JS/TS-emitted, exportable public top-level value declarations a module declares — the names the per-module path lists in a trailing export { … } (or, for functions, that the backend exports inline). Covers functions, records, enums (+ each Enum_Variant), traits, classes, effects, and consts. Type aliases are excluded: they are erased in JS (a comment, no runtime binding) and emitted as an export type alias inline in TS, so they need no trailing re-export. Runtime-prelude names are excluded (lowered inline). Each entry carries the function flag so the backend camelCases function names to match their inline export function form.
fn_decl_name
The method name of a FnDecl node, or None for a non-FnDecl.
fn_is_test
Returns true if node is a function declaration carrying the @test annotation. Matches the discovery rule used by bock test (bock-cli::test::discover_test_functions): an @test-annotated FnDecl.
hoist_value_cf
impl_has_instance_method
True when an impl block (an bock_air::NodeKind::ImplBlock) declares at least one instance method — one that binds self, or an effect operation (which is dispatched on a handler instance despite taking no self; see is_associated_impl_method).
implicit_esm_imports_for
Compute the implicit cross-module imports for module: public symbols declared in other reachable modules that module references but neither declares locally nor imports explicitly.
implicit_imports_for
Compute the implicit cross-module imports for module: public symbols declared in other reachable modules that module references but neither declares locally nor imports explicitly. Returns (module_path, name) pairs.
inherited_default_methods
Resolve, for an impl Trait for Type block, the trait default methods that the impl does not override and that must therefore be synthesized onto the target. trait_path is the ImplBlock’s trait_path; impl_methods its own methods. Returns cloned default-method FnDecl nodes, in trait-declaration order. Empty when the impl has no trait, the trait is unknown, or every default is overridden. Returns owned clones (rather than registry borrows) so a backend can iterate them while mutating its own emission buffer, without holding a borrow of the registry across the &mut self writes.
is_associated_call
True when node is a Call the lowerer classified as an associated-function call (Type.method(args) — no self prepended), via the bock_air::lower::ASSOC_CALL_META_KEY stamp.
is_associated_impl_method
True when an impl/trait method should be emitted as an associated function — assoc_fn_def holds and the method is not an effect operation.
is_bool_stringify
True when an expression node is a Bool value that must stringify to the canonical lowercase "true" / "false" (§3.5) — the checker stamped it with bock_types::checker::BOOL_STRINGIFY_META_KEY because it appears as an ${expr} interpolation part of Bool type.
is_default_method
True when fn_decl is a trait default method — one that carries a body.
is_int_arith
True when a BinaryOp { op: Div | Rem, .. } is integer division / remainder and must be lowered to DQ23’s cross-target integer semantics (§3.6) rather than the target’s native / / %.
is_list_concat
True when a BinaryOp { op: Add, left, right } is list concatenation and must be lowered to the target’s concat idiom rather than a native +.
is_target_keyword
True when name is a reserved word in the given target’s keyword set.
is_unimplemented_sealed_core_trait
True when trait_name is a compiler-provided sealed core trait (bock_types::traits::SEALED_CORE_TRAITS) that is NOT declared as a user trait in trait_decls. Such a bound is the primitive conformance and must be lowered to the target’s built-in (native ==/comparison/stringification and the built-in ordered/equality constraint) rather than referenced as a real trait/interface, which does not exist in any target. Shared by the generic-bound renderers and the method-call bridge so the two stay in lockstep.
is_user_compare
True when a BinaryOp { op: Lt | Le | Gt | Ge, .. } is an ordering comparison on a user Comparable type and must be lowered through the type’s compare(self, other) method rather than the target’s native < / <= / > / >=.
js_ts_generate_tests
Build the Vitest/Jest test file for the project’s @test functions (S7), shared by the JS and TS backends (identical apart from the file extension and the concrete emit context, both injected by the caller).
locally_declared_names
Top-level symbol names declared locally in module (item names plus each enum variant’s emitted Enum_Variant name) — the names a per-module implicit import must never shadow with a cross-module import.
loop_needs_break_label
Decide whether a loop must be given a target label so that a break/ continue inside a statement-arm match reaches the loop rather than the switch the match lowers to.
lower_blanket_into
Run the shared temp-hoist desugar over a fully-lowered, type-checked AIR module, returning the rewritten module. Idempotent on trees with no value-position diverging control flow (returns an equivalent tree).
match_has_statement_arm
Returns true if any arm of a match carries a statement body (see arm_body_is_statement). When true, backends without a statement-admitting expression form (Go, Python, JS, TS) must emit the match in statement position rather than as an expression.
match_needs_ifchain
Returns true if a match’s arms require the if/else-if-chain lowering (JS, TS, Go) rather than the value/tag switch fast-path.
merge_where_bounds_into_generics
Fold a function/impl where-clause’s trait bounds onto the matching generic params, returning an owned param list with the constraints attached inline.
module_declares_main_fn
Returns true if the given AIR module declares a top-level function named main. Used by the build pipeline to decide whether to append an entry-point invocation to the generated output of targets without a native main convention.
module_main_fn_is_async
Returns true if the given AIR module declares a top-level async fn main. Used by generate_project to select an async-aware entry invocation.
module_path_string
The declared module-path of an AIR module as a dotted string (e.g. core.option), or None if the module declares no module <path>.
module_tree_relpath
Map a module’s declared dotted path (core.option) to its relative output path in a per-module tree, with the target’s file extension (core/option.<ext>). The entry module is laid out separately (always main.<ext> at a stable location), so callers pass non-entry modules here; a module with no declared path falls back to its source-mirrored path.
node_is_statement
Returns true if node is a statement-like AIR node — one that performs control flow or mutation and yields no usable value in expression position.
ordering_variant
Returns the variant name if name is one of the prelude Ordering variants (Less/Equal/Greater), else None. The returned &'static str is the canonical spelling, suitable for emitting into target source.
param_binds_self
If param is a method parameter that binds the receiver self, return Some(is_mut) carrying its mutability; otherwise None.
primitive_bridge_call
Recognise a desugared primitive trait-bridge method call.
primitive_conversion_call
Q-prim-assoc: when node is a primitive associated-conversion call (Float.from(x) / Int.try_from(s) / String.from(c)), returns (target_prim_name, method, arg), where method is "from" or "try_from" and arg is the single source-value argument.
primitive_recv_kind
The receiver-kind annotation value, parsed into the primitive type name.
raw_recv_kind
The raw recv_kind annotation tag the checker stamped on a desugared method call node, if any.
reachable_modules
Restrict modules to those reachable from the entry module via real use edges, returned in a deterministic dependency-before-dependent order (a post-order DFS of the use graph with use targets visited in declared module-path order). The result is independent of the input slice’s order, so the emitted per-module tree is byte-stable across the per-process topo-sort shuffling described below.
registered_variant
Look up the last segment of a TypePath (the variant name) in the registry. Returns None when the path is empty or the name is not a known variant.
trait_bound_bridge_call
Recognise a desugared sealed-core-trait bridge method call on a bounded generic type variable.
trait_bound_recv_kind
The receiver-kind annotation value, parsed into the bounding trait name.
trait_uses_self_operand
True if any of trait_info’s methods reference Self in a (non-receiver) parameter type or in the return type.
user_compare_variant
Map an ordering BinOp (< / <= / > / >=) onto the Ordering variant name and whether the comparison is an equality (true) or inequality (false) against it, for lowering a user-Comparable comparison through compare:
user_eq_kind
The DQ29 equality lane a BinaryOp { op: Eq | Ne, .. } node was stamped with by the checker (bock_types::checker::USER_EQ_META_KEY), or None for an unstamped (native-equality) comparison.
value_cf_diverges
True when a value-position node is a control-flow construct whose branches diverge — at least one branch yields a value AND at least one branch exits via return/break/continue/a diverging intrinsic. These are the nodes hoist_value_cf rewrites; a construct where every branch yields a value already lowers fine via the existing expression paths and is left untouched (so value if/match/loop codegen does not regress).

Type Aliases§

EnumVariantRegistry
Maps an enum-variant name to its EnumVariantInfo. Variant names are globally unique within a v1 Bock program (no per-enum namespacing at use sites — Red, not Color.Red), so a flat map keyed by the bare variant name resolves every construction and pattern site.
GenericDeclRegistry
Maps a generic type’s declared name to its generic parameters. Built by a pre-scan of every RecordDecl/EnumDecl/ClassDecl across the reached modules.
TraitDeclRegistry
Maps a trait’s declared name to its TraitDeclInfo. Trait names are globally unique within a Bock program, so a flat map keyed by the bare name resolves every impl Trait for Type block to its trait.