Module Sarek_native_gen

val gen_expr_impl : loc:Ppxlib__.Location.t -> ctx:Sarek_native_gen_base.gen_context -> Sarek_typed_ast.texpr -> Ppxlib_ast.Ast.expression

Generate OCaml expression from typed Sarek expression.

  • parameter ctx

    Generation context with mutable vars and inline types

val gen_pattern_impl : loc:Ppxlib.location -> ctx:Sarek_native_gen_base.gen_context -> Sarek_typed_ast.tpattern -> Ppxlib.pattern

Generate pattern from typed pattern. Takes context to detect same-module types that shouldn't be qualified.

val gen_binop : loc:Ppxlib.location -> Sarek_ast.binop -> Ppxlib.expression -> Ppxlib.expression -> Sarek_types.typ -> Ppxlib.expression

Generate binary operation

val gen_unop : loc:Ppxlib.location -> Sarek_ast.unop -> Ppxlib.expression -> Sarek_types.typ -> Ppxlib.expression

Generate unary operation

val module_name_of_sarek_loc : Sarek_ast.loc -> string

Extract module name from a Sarek location (file path). For "/path/to/test_registered_variant.ml", returns "Test_registered_variant".

val gen_expr : loc:Ppxlib__.Location.t -> Sarek_typed_ast.texpr -> Ppxlib.expression

Top-level entry point for generating expressions. Starts with empty context.

val gen_expr_with_inline_types : loc:Ppxlib__.Location.t -> inline_type_names:Sarek_native_gen_base.StringSet.t -> current_module:string option -> Sarek_typed_ast.texpr -> Ppxlib.expression

Generate expression with inline types context for first-class modules.

Module Item Generation

val gen_module_fun : loc:Ppxlib__.Location.t -> string -> Sarek_typed_ast.tparam list -> Sarek_typed_ast.texpr -> Ppxlib.pattern * Ppxlib.expression

Generate a module-level function (TMFun) as a let binding.

val gen_module_const : loc:Ppxlib__.Location.t -> string -> int -> Sarek_types.typ -> Sarek_typed_ast.texpr -> Ppxlib.pattern * Ppxlib.expression

Generate a module-level constant (TMConst) as a let binding.

Type Declaration Generation

val gen_type_decl_record : loc:Ppxlib.location -> string -> (string * Sarek_types.typ * bool) list -> Ppxlib.structure_item

Generate a type declaration for a record type

val gen_type_decl_variant : loc:Ppxlib.location -> string -> (string * Sarek_types.typ option) list -> Ppxlib.structure_item

Generate a type declaration for a variant type

val gen_type_decl_item : loc:Ppxlib.location -> Sarek_typed_ast.ttype_decl -> Ppxlib.structure_item

Generate a structure item from a typed type declaration

val wrap_module_items : loc:Ppxlib__.Location.t -> Sarek_typed_ast.tmodule_item list -> Ppxlib.expression -> Ppxlib.expression

Wrap expression with module item bindings.

First-Class Module Approach for Inline Types

To avoid "type escapes its scope" errors, we use first-class modules to encapsulate inline types. The approach:

1. Generate a module signature KERNEL_TYPES with abstract types and accessors 2. Generate a concrete module implementation with the actual types 3. Transform the kernel body to use T.get_field and T.make_type accessors 4. Pass (module T : KERNEL_TYPES) as a parameter to the kernel

This keeps the concrete types hidden behind an existential type.

val gen_module_impl : loc:Ppxlib.location -> Sarek_typed_ast.ttype_decl list -> Ppxlib.module_expr

Generate a concrete module implementation for inline types (types only). Example for record type point with fields x and y: struct type point = record with fields x: float and y: float end

Note: We only generate type declarations here, not accessor functions. The accessor functions are generated as object methods by gen_types_object.

val inline_type_decls : Sarek_typed_ast.ttype_decl list -> Sarek_typed_ast.ttype_decl list

Get only inline type declarations (those without module prefix). External/registered types have qualified names like "Module.type".

val has_inline_types : Sarek_typed_ast.tkernel -> bool

Check if a kernel has inline types that need first-class module handling