l0_c_emitter::CEmitter class

Defined in module l0_c_emitter (l0_c_emitter.py).

C-specific code emitter.

Responsibilities:

  • Emit C syntax (knows C keywords, syntax, conventions).
  • Name mangling for C.
  • Type emission (L0 types -> C types).
  • Statement/expression emission to C.
  • Cleanup code emission (HOW to clean, not when/why).

Does NOT:

  • Make decisions about what to emit.
  • Perform semantic analysis.
  • Manage scopes or lifetimes (queries backend for this).

Public static attributes

static Set C_KEYWORDS
static Optional analysis
static Optional current_module
static CCodeBuilder out

Public functions

auto set_analysis(self, AnalysisResult analysis) -> None
Initialize emitter with analysis data.
auto get_output(self) -> str
Get the generated C code.
auto find_variant_decl(self, str module_name, str enum_name, str variant_name) -> Optional[EnumVariant]
Look up an enum variant's AST declaration.
auto ice(self, str message, Optional node[object] = None) -> NoReturn
Raise an internal compiler error with context.
auto mangle_struct_name(self, str module_name, str struct_name) -> str
Mangle a struct name to avoid C namespace collisions.
auto mangle_enum_name(self, str module_name, str enum_name) -> str
Mangle an enum name.
auto mangle_function_name(self, str module_name, str func_name) -> str
Mangle a function name.
auto mangle_let_name(self, str module_name, str let_name) -> str
Mangle a top-level let name to C identifier.
auto mangle_identifier(self, str name) -> str
Mangle an identifier if it conflicts with C keywords or L0 names.
auto fresh_tmp(self, str kind = "tmp") -> str
Generate a unique temporary variable name.
auto emit_sizeof_type(self, Type typ) -> str
Emit C code for sizeof a given L0 type.
auto emit_ord(self, str c_enum_expr) -> str
Emit C code for ord(enum_value) intrinsic.
auto emit_type(self, Type typ) -> str
Convert an L0 Type to its C representation.
auto is_niche_nullable(self, NullableType t) -> bool
Public check for niche-optimized (pointer-shaped) nullable types.
auto emit_none_value_for_nullable(self, NullableType t) -> str
Emit C code for the L0 'null' value of a nullable type.
auto emit_some_value_for_nullable(self, NullableType t, str c_inner_expr) -> str
Emit C code for wrapping a value in 'some' for a nullable type.
auto emit_null_literal(self, Optional expected_type[Type], *bool for_initializer = False) -> str
Emit a null literal appropriate for the expected type.
auto emit_widen_int(self, str c_expr, BuiltinType src_type, BuiltinType dst_type) -> str
Emit C code for implicit integer widening.
auto emit_pointer_type(self, Type base_type) -> str
Emit C pointer type for a base type.
auto emit_enum_tag(self, EnumType enum_type, str variant_name) -> str
Emit C tag enum value for an enum variant.
auto emit_section_comment(self, str text) -> None
Emit a decorative section comment.
auto emit_module_comment(self, str module_name) -> None
Emit a module metadata comment.
auto emit_module_separator(self, str module_name) -> None
Emit a decorative module separator.
auto emit_unreachable_comment(self) -> None
Emit an unreachable code comment for debugging.
auto emit_unreachable_marker(self, str reason = "unreachable") -> None
Emit a runtime panic call for unreachable code paths.
auto emit_header(self) -> None
Emit C header boilerplate, SipHash implementation, and L0 runtime.
auto emit_line_directive(self, Node node, str current_module) -> None
Emit #line directive for debugging generated C.
auto restore_active_line_directive(self) -> None
Re-emit the current source line directive after generated helper lines.
auto emit_forward_decls(self) -> None
Emit forward declarations for all structs and enums in compilation unit.
auto emit_struct(self, str module_name, StructDecl decl, StructInfo struct_info) -> None
Emit a complete C struct definition.
auto emit_enum(self, str module_name, EnumDecl decl, EnumInfo enum_info) -> None
Emit an L0 enum as a C tagged union.
auto emit_let_declaration(self, str module_name, LetDecl decl, Type let_type, Callable[[Expr, Type], str] let_initializer_callback) -> None
Emit a single top-level let declaration as a static variable.
auto emit_top_level_let_cleanup(self) -> None
Emit final cleanup for ARC-managed top-level lets before process exit.
auto emit_function_declaration(self, str module_name, FuncDecl decl, FuncType func_type) -> None
Emit a single function declaration signature.
auto emit_function_definition_header(self, str module_name, FuncDecl decl, FuncType func_type) -> None
Emit function definition header (signature and opening brace).
auto emit_function_definition_footer(self) -> None
Emit function definition footer (closing brace).
auto emit_main_wrapper(self, str entry_module, FuncType func_type) -> None
Emit C main() wrapper that calls the L0 entry function.
auto emit_optional_wrapper_for_defined_type(self, Type inner) -> None
Emit the collected optional wrapper for a just-defined user type.
auto prepare_optional_wrappers(self) -> None
Scan all compilation unit types and collect required optional wrappers.
auto emit_optional_wrappers(self, *bool early) -> None
Emit C typedef declarations for collected optional wrapper types.
auto emit_value_cleanup(self, str c_expr, Type ty) -> None
Emit C code to clean up an owned by-value variable.
auto emit_struct_cleanup(self, str c_ptr_expr, StructType struct_type) -> None
Emit cleanup code for all owned fields in a struct.
auto emit_enum_cleanup(self, str c_ptr_expr, EnumType enum_type) -> None
Emit cleanup code for owned fields in an enum's active variant.
auto emit_int_literal(self, int value) -> str
Emit C code for an integer literal.
auto emit_byte_literal(self, str value) -> str
Emit C code for a byte literal.
auto emit_string_literal(self, str value) -> str
Emit C code for an ARC string literal.
auto emit_const_string_literal(self, str value) -> str
Emit C code for a static string literal initializer.
auto emit_bool_literal(self, bool value) -> str
Emit C code for a boolean literal expression.
auto emit_const_bool_literal(self, bool value) -> str
Emit C code for a boolean literal in a static initializer.
auto emit_var_ref(self, str c_name) -> str
Emit C code for a variable reference identifier.
auto emit_unary_op(self, str op, str c_operand) -> str
Emit C code for a unary operation.
auto emit_negated_condition(self, str c_cond) -> str
Emit a negated condition expression for control-flow lowering.
auto emit_binary_op(self, str op, str c_left, str c_right) -> str
Emit C code for a simple binary operation.
auto emit_condition_binary_op(self, str op, str c_left, str c_right) -> str
Emit a top-level binary condition for direct statement headers.
auto emit_checked_int_div(self, str c_left, str c_right) -> str
Emit C code for checked integer division runtime call.
auto emit_checked_int_mod(self, str c_left, str c_right) -> str
Emit C code for checked integer modulo runtime call.
auto emit_checked_int_mul(self, str c_left, str c_right) -> str
Emit C code for checked integer multiplication runtime call.
auto emit_checked_int_add(self, str c_left, str c_right) -> str
Emit C code for checked integer addition runtime call.
auto emit_checked_int_sub(self, str c_left, str c_right) -> str
Emit C code for checked integer subtraction runtime call.
auto emit_function_call(self, str c_func_name, str c_args) -> str
Emit C code for a function call.
auto emit_field_access(self, str c_obj, str field_name, bool is_pointer) -> str
Emit C code for field access using '.
auto emit_paren_expr(self, str c_inner) -> str
Emit C code for a parenthesized expression.
auto emit_cast(self, str c_type, str c_inner) -> str
Emit C code for a type cast.
auto emit_checked_ptr_access(self, str c_ptr_expr, str c_ptr_type, str c_required_size, str c_required_align, str access_mode = "_RT_ACCESS_READ") -> str
Emit a runtime-checked pointer access expression.
auto emit_checked_ptr_index_access(self, str c_base_expr, str c_index_expr, str c_ptr_type, str c_element_size, str c_required_align, str access_mode = "_RT_ACCESS_READ") -> str
Emit a runtime-checked indexed pointer access expression.
auto emit_checked_ptr_access_for_base(self, str c_ptr_expr, str c_base_type, str access_mode = "_RT_ACCESS_READ") -> str
Emit a runtime-checked pointer to a complete object of c_base_type.
auto emit_drop_begin_expr(self, str c_ptr_expr, str c_ptr_type, str c_required_size, str c_required_align) -> str
Emit the expression that validates and begins a generated drop.
auto emit_drop_finish_call(self, str c_ptr_expr) -> None
Emit the runtime call that completes a generated drop.
auto emit_checked_narrow_cast(self, str c_dst_type, str c_inner) -> str
Emit C code for a checked narrowing cast runtime call.
auto emit_unwrap_ptr(self, str c_dst_type, str c_inner, str type_str) -> str
Emit C code for unwrapping a pointer-shaped optional runtime check.
auto emit_unwrap_opt(self, str c_src_type, str c_inner, str type_str) -> str
Emit C code for unwrapping a value-optional runtime check.
auto emit_null_check_eq(self, str c_expr) -> str
Emit C code for null equality check (opt == null).
auto emit_null_check_ne(self, str c_expr) -> str
Emit C code for null inequality check (opt != null).
auto emit_pointer_null_check(self, str c_expr, str op) -> str
Emit C code for pointer null comparison.
auto emit_condition_pointer_null_check(self, str c_expr, str op) -> str
Emit a top-level pointer null comparison for direct statement headers.
auto emit_optional_has_value(self, str c_expr) -> str
Emit C code for reading an optional wrapper's has-value flag.
auto emit_optional_value(self, str c_expr) -> str
Emit C code for reading an optional wrapper's payload value.
auto emit_enum_tag_access(self, str c_expr) -> str
Emit C code for reading an enum tag.
auto emit_enum_payload_field_access(self, str c_expr, str variant, str field) -> str
Emit C code for reading a field from an enum payload.
auto emit_string_equals_call(self, str lhs, str rhs) -> str
Emit the runtime string-equality helper call.
auto emit_string_concat_call(self, str lhs, str rhs) -> str
Emit the runtime string-concatenation helper call.
auto emit_string_compare_call(self, str op, str lhs, str rhs) -> str
Emit the runtime string-compare helper call wrapped in a relational check.
auto emit_condition_string_compare_call(self, str op, str lhs, str rhs) -> str
Emit a top-level string relational check for direct statement headers.
auto emit_discard_expr(self, str c_expr) -> str
Emit a statement-context discard wrapper for an expression.
auto emit_deref_lvalue(self, str ptr_expr) -> str
Emit C code for a dereference lvalue: (*ptr).
auto emit_field_lvalue(self, str obj, str field, bool is_pointer) -> str
Emit C code for a field access lvalue.
auto emit_index_lvalue(self, str base, str index) -> str
Emit C code for an index lvalue: base[idx].
auto emit_struct_constructor(self, str c_struct_name, List] field_inits[Tuple[str, str]) -> str
Emit C code for a struct compound literal constructor.
auto emit_struct_static_initializer(self, List] field_inits[Tuple[str, str]) -> str
Emit a brace-only struct initializer for static storage duration.
auto emit_struct_constructor_for_type(self, StructType struct_type, List] field_inits[Tuple[str, str]) -> str
Emit a C struct constructor for an L0 struct type.
auto emit_struct_static_initializer_for_type(self, StructType struct_type, List] field_inits[Tuple[str, str]) -> str
Emit a static-storage struct initializer for an L0 struct type.
auto emit_variant_constructor(self, str c_enum_name, str variant_name, str tag_value, List] payload_inits[Tuple[str, str]) -> str
Emit C code for an enum variant tagged union literal.
auto emit_variant_static_initializer(self, str variant_name, str tag_value, List] payload_inits[Tuple[str, str]) -> str
Emit a brace-only enum tagged-union initializer for static storage duration.
auto emit_variant_constructor_for_type(self, EnumType enum_type, str variant_name, List] payload_inits[Tuple[str, str]) -> str
Emit a tagged union constructor for a given L0 enum type.
auto emit_variant_static_initializer_for_type(self, EnumType enum_type, str variant_name, List] payload_inits[Tuple[str, str]) -> str
Emit a static-storage tagged union initializer for a given L0 enum type.
auto emit_pattern_binding_init(self, str scrutinee, str variant, str field) -> str
Emit C code for accessing a variant field during pattern matching.
auto emit_expr_stmt(self, str c_expr) -> None
Emit an expression as a statement.
auto emit_return_stmt(self, Optional c_value[str]) -> None
Emit a C return statement.
auto emit_exit_switch(self) -> None
Emit a C break statement to exit a switch block.
auto emit_label(self, str label) -> None
Emit a C label followed by a null statement.
auto emit_goto(self, str label) -> None
Emit a C goto statement.
auto emit_block_start(self) -> None
Emit an opening brace and increase indentation.
auto emit_block_end(self) -> None
Emit a closing brace and decrease indentation.
auto emit_while_header(self, str c_cond) -> None
Emit a C while loop header.
auto emit_if_header(self, str c_cond) -> None
Emit a C if statement header.
auto emit_else(self) -> None
Emit a C else keyword.
auto emit_for_loop_start(self) -> None
Emit a decorative comment and opening brace for a for loop block.
auto emit_for_loop_end(self) -> None
Emit closing brace for a for loop block.
auto emit_let_decl(self, str c_type, str c_var_name, str c_init) -> None
Emit a C local variable declaration with initializer.
auto emit_assignment(self, str c_target, str c_value) -> None
Emit a simple C assignment statement.
auto emit_pointer_assignment(self, str c_ptr_name, str c_value) -> None
Emit a C assignment through a pointer.
auto emit_checked_pointer_assignment(self, str c_ptr_name, str c_base_type, str c_value) -> None
Emit an assignment through a runtime-checked object pointer.
auto emit_temp_decl(self, str c_type, str c_temp_name, str c_value) -> None
Emit a C temporary variable declaration with initializer.
auto emit_string_retain(self, str c_expr) -> None
Emit an ARC string retain runtime call.
auto emit_string_release(self, str c_expr) -> None
Emit an ARC string release runtime call.
auto emit_comment(self, str comment) -> None
Emit a C block comment.
auto emit_match_scrutinee_decl(self, str c_type, str c_expr) -> None
Emit the scrutinee declaration for a match/case statement.
auto emit_switch_start(self, str c_expr) -> None
Emit a C switch statement header and opening brace.
auto emit_match_switch_start(self, str scrutinee_name) -> None
Emit a match switch over an enum tag.
auto emit_switch_end(self) -> None
Emit a C switch statement closing brace.
auto emit_case_label(self, str c_tag_value) -> None
Emit a C case label.
auto emit_default_label(self) -> None
Emit a C default case label.
auto emit_null_assignment(self, str c_var) -> None
Emit a NULL assignment to a variable.
auto emit_alloc_obj(self, str c_ptr_type, str c_base_type, str c_temp_name) -> None
Emit a heap allocation runtime call.
auto emit_struct_init(self, str c_temp_name, str c_base_type, str c_init_str) -> None
Emit an object initialization through a pointer.
auto emit_struct_init_from_fields(self, str c_temp_name, Type base_type, List] field_inits[Tuple[str, str]) -> None
Emit struct initialization using positional field values.
auto emit_enum_variant_init(self, str c_temp_name, EnumType enum_type, str variant_name, List] payload_inits[Tuple[str, str]) -> None
Emit enum variant initialization for a heap-allocated enum.
auto emit_zero_init(self, str c_temp_name, str c_base_type) -> None
Emit zero-initialization through a pointer.
auto emit_try_check_niche(self, str c_tmp, str ret_none) -> None
Emit a NULL check for a niche-optimized optional.
auto emit_try_check_value(self, str c_tmp, str ret_none) -> None
Emit a has_value check for a value-optional.
auto emit_try_extract_value(self, str c_tmp) -> str
Emit C code to extract the inner value from an optional.

Public attributes

analysis

Protected functions

auto _is_niche_nullable(self, NullableType t) -> bool
Check if a nullable type uses niche optimization (pointer-shaped).
auto _opt_key_for_type(self, Type t) -> str
Generate a unique key for an optional wrapper type name.
auto _opt_wrapper_name_for_inner(self, Type inner) -> str
Generate C typedef name for optional wrapper of given inner type.
auto _collect_opt_wrappers_from_type(self, Type t) -> None
Recursively collect optional wrapper types needed.
auto _is_early_inner(self, Type inner) -> bool
Check if an inner type wrapper can be emitted before user definitions.
auto _emit_optional_wrapper(self, str name, Type inner) -> None
Emit one collected optional wrapper typedef.
auto _emit_cleanup_by_type(self, str c_expr, Type ty) -> None
Emit recursive cleanup code for any ARC-managed data inside a type.
auto _emit_enum_value_cleanup(self, str c_expr, EnumType enum_type) -> None
Emit C cleanup code for an enum by-value variable.
auto _emit_enum_cleanup_switch(self, EnumType enum_type, str c_tag_expr, Callable[[str, str], str] field_expr_for_variant_field, *bool missing_info_is_ice) -> None
Emit a cleanup switch over the active variant of an enum value.
auto _iter_variant_cleanup_fields(self, EnumType enum_type, str variant_name, List variant_field_types[Type]) -> List[Tuple[str, Type]]
Pair variant field names with their resolved field types.
auto _enum_has_arc_data(self, EnumInfo enum_info) -> bool
Report whether any enum payload field requires ARC-aware cleanup.
auto _get_struct_info(self, StructType struct_type, *bool strict) -> Optional[StructInfo]
Look up resolved metadata for a struct type.
auto _get_enum_info(self, EnumType enum_type, *bool strict) -> Optional[EnumInfo]
Look up resolved metadata for an enum type.
auto _emit_field_cleanup(self, str field_expr, Type field_type) -> None
Emit recursive cleanup for a field.
auto _string_token_to_c_bytes_and_len(self, str value) -> tuple[str, int]
Decode an L0 string token payload and encode as C string-literal bytes.

Protected static attributes

static int _tmp_counter
static Optional _active_line_directive
static Dict _opt_wrappers
static Set _opt_emitted

Protected attributes

_active_line_directive

Function documentation

None l0_c_emitter::CEmitter::set_analysis(self, AnalysisResult analysis)

Initialize emitter with analysis data.

Parameters
analysis The AnalysisResult containing the compilation products.

str l0_c_emitter::CEmitter::get_output(self)

Get the generated C code.

Returns The complete generated C code string.

Optional[EnumVariant] l0_c_emitter::CEmitter::find_variant_decl(self, str module_name, str enum_name, str variant_name)

Look up an enum variant's AST declaration.

Parameters
module_name Name of the module containing the enum.
enum_name Name of the enum.
variant_name Name of the variant to find.
Returns The EnumVariant node if found, otherwise None.

NoReturn l0_c_emitter::CEmitter::ice(self, str message, Optional node[object] = None)

Raise an internal compiler error with context.

Parameters
message Descriptive error message.
node Optional AST node to provide source location information.
Exceptions
InternalCompilerError Always raised with provided context.

str l0_c_emitter::CEmitter::mangle_struct_name(self, str module_name, str struct_name)

Mangle a struct name to avoid C namespace collisions.

Parameters
module_name Module name.
struct_name L0 struct name.
Returns Mangled C struct name (e.g., "l0_module_Point").

str l0_c_emitter::CEmitter::mangle_enum_name(self, str module_name, str enum_name)

Mangle an enum name.

Parameters
module_name Module name.
enum_name L0 enum name.
Returns Mangled C enum name.

str l0_c_emitter::CEmitter::mangle_function_name(self, str module_name, str func_name)

Mangle a function name.

Parameters
module_name Module name.
func_name L0 function name.
Returns Mangled C function name.

str l0_c_emitter::CEmitter::mangle_let_name(self, str module_name, str let_name)

Mangle a top-level let name to C identifier.

Parameters
module_name Module name.
let_name L0 constant name.
Returns Mangled C identifier.

str l0_c_emitter::CEmitter::mangle_identifier(self, str name)

Mangle an identifier if it conflicts with C keywords or L0 names.

Parameters
name The L0 identifier to mangle.
Returns The safe C identifier.

Used for local variables, parameters, and pattern variables. Appends '__v' suffix to avoid C keyword conflicts. Also mangles names starting with '_' or 'l0_'/'L0_' to avoid clashes with runtime names.

str l0_c_emitter::CEmitter::fresh_tmp(self, str kind = "tmp")

Generate a unique temporary variable name.

Parameters
kind Category of temporary (e.g., "tmp", "ptr", "try").
Returns Unique C identifier like "l0_tmp_1", "l0_ptr_2", etc.

str l0_c_emitter::CEmitter::emit_sizeof_type(self, Type typ)

Emit C code for sizeof a given L0 type.

Parameters
typ The type to measure.
Returns A C expression like "((l0_int)sizeof(l0_int))".

str l0_c_emitter::CEmitter::emit_ord(self, str c_enum_expr)

Emit C code for ord(enum_value) intrinsic.

Parameters
c_enum_expr C expression evaluating to an enum value.
Returns A C expression that extracts the tag field and casts to l0_int.

str l0_c_emitter::CEmitter::emit_type(self, Type typ)

Convert an L0 Type to its C representation.

Parameters
typ The L0 Type to convert.
Returns A C type string (e.g., "l0_int", "struct l0_main_Point*").
Exceptions
InternalCompilerError If the type kind is unknown or unsupported.

bool l0_c_emitter::CEmitter::is_niche_nullable(self, NullableType t)

Public check for niche-optimized (pointer-shaped) nullable types.

Parameters
t The NullableType to check.
Returns True if the type is represented as a nullable pointer in C.

str l0_c_emitter::CEmitter::emit_none_value_for_nullable(self, NullableType t)

Emit C code for the L0 'null' value of a nullable type.

Parameters
t The NullableType.
Returns C code string representing the 'none' state.

str l0_c_emitter::CEmitter::emit_some_value_for_nullable(self, NullableType t, str c_inner_expr)

Emit C code for wrapping a value in 'some' for a nullable type.

Parameters
t The NullableType.
c_inner_expr C expression for the inner value.
Returns C code string representing the 'some' state.

str l0_c_emitter::CEmitter::emit_null_literal(self, Optional expected_type[Type], *bool for_initializer = False)

Emit a null literal appropriate for the expected type.

Parameters
expected_type The type expected in this context.
for_initializer If True, uses C initializer syntax ({0}).
Returns C code for the null value.
Exceptions
InternalCompilerError If expected type is not nullable or a pointer.

str l0_c_emitter::CEmitter::emit_widen_int(self, str c_expr, BuiltinType src_type, BuiltinType dst_type)

Emit C code for implicit integer widening.

Parameters
c_expr C expression evaluating to the source value.
src_type The smaller source type.
dst_type The larger destination type.
Returns C code string for the widening cast.
Exceptions
InternalCompilerError If the widening path is unsupported.

str l0_c_emitter::CEmitter::emit_pointer_type(self, Type base_type)

Emit C pointer type for a base type.

Parameters
base_type The L0 type to point to.
Returns C type string (e.g., "l0_int*").

str l0_c_emitter::CEmitter::emit_enum_tag(self, EnumType enum_type, str variant_name)

Emit C tag enum value for an enum variant.

Parameters
enum_type The L0 enum type.
variant_name The name of the variant.
Returns The mangled C enum tag identifier.

None l0_c_emitter::CEmitter::emit_section_comment(self, str text)

Emit a decorative section comment.

Parameters
text The comment text.

None l0_c_emitter::CEmitter::emit_module_comment(self, str module_name)

Emit a module metadata comment.

Parameters
module_name The name of the module.

None l0_c_emitter::CEmitter::emit_module_separator(self, str module_name)

Emit a decorative module separator.

Parameters
module_name The name of the module.

None l0_c_emitter::CEmitter::emit_unreachable_marker(self, str reason = "unreachable")

Emit a runtime panic call for unreachable code paths.

Parameters
reason Description of why the path is unreachable.

None l0_c_emitter::CEmitter::emit_line_directive(self, Node node, str current_module)

Emit #line directive for debugging generated C.

Parameters
node AST node with span information.
current_module Name of the current module.

None l0_c_emitter::CEmitter::emit_struct(self, str module_name, StructDecl decl, StructInfo struct_info)

Emit a complete C struct definition.

Parameters
module_name Name of the module.
decl StructDecl AST node.
struct_info Resolved type information for the struct.

None l0_c_emitter::CEmitter::emit_enum(self, str module_name, EnumDecl decl, EnumInfo enum_info)

Emit an L0 enum as a C tagged union.

Parameters
module_name Name of the module.
decl EnumDecl AST node.
enum_info Resolved type information for the enum.
Exceptions
InternalCompilerError If variant information is missing.

None l0_c_emitter::CEmitter::emit_let_declaration(self, str module_name, LetDecl decl, Type let_type, Callable[[Expr, Type], str] let_initializer_callback)

Emit a single top-level let declaration as a static variable.

Parameters
module_name Module containing the let.
decl Let declaration AST node.
let_type Resolved type of the let.
let_initializer_callback Callback to emit initializer expression string.

None l0_c_emitter::CEmitter::emit_function_declaration(self, str module_name, FuncDecl decl, FuncType func_type)

Emit a single function declaration signature.

Parameters
module_name Name of the module.
decl FuncDecl AST node.
func_type Resolved signature of the function.

None l0_c_emitter::CEmitter::emit_function_definition_header(self, str module_name, FuncDecl decl, FuncType func_type)

Emit function definition header (signature and opening brace).

Parameters
module_name Name of the module.
decl FuncDecl AST node.
func_type Resolved signature of the function.

None l0_c_emitter::CEmitter::emit_main_wrapper(self, str entry_module, FuncType func_type)

Emit C main() wrapper that calls the L0 entry function.

Parameters
entry_module Name of the entry module.
func_type Resolved signature of the L0 main function.

None l0_c_emitter::CEmitter::emit_optional_wrapper_for_defined_type(self, Type inner)

Emit the collected optional wrapper for a just-defined user type.

Parameters
inner Just-defined struct or enum type.

None l0_c_emitter::CEmitter::emit_optional_wrappers(self, *bool early)

Emit C typedef declarations for collected optional wrapper types.

Parameters
early If True, emit wrappers for builtins only. If False, emit wrappers for user-defined structs/enums.

None l0_c_emitter::CEmitter::emit_value_cleanup(self, str c_expr, Type ty)

Emit C code to clean up an owned by-value variable.

Parameters
c_expr C expression evaluating to the value.
ty The L0 Type of the value.

None l0_c_emitter::CEmitter::emit_struct_cleanup(self, str c_ptr_expr, StructType struct_type)

Emit cleanup code for all owned fields in a struct.

Parameters
c_ptr_expr C expression evaluating to a pointer to the struct.
struct_type The L0 struct type.

None l0_c_emitter::CEmitter::emit_enum_cleanup(self, str c_ptr_expr, EnumType enum_type)

Emit cleanup code for owned fields in an enum's active variant.

Parameters
c_ptr_expr C expression evaluating to a pointer to the enum.
enum_type The L0 enum type.

str l0_c_emitter::CEmitter::emit_int_literal(self, int value)

Emit C code for an integer literal.

Parameters
value The integer value.
Returns C literal string (handles INT32_MIN edge case).

str l0_c_emitter::CEmitter::emit_byte_literal(self, str value)

Emit C code for a byte literal.

Parameters
value The byte literal character.
Returns C casted character literal.

str l0_c_emitter::CEmitter::emit_string_literal(self, str value)

Emit C code for an ARC string literal.

Parameters
value The L0 string token payload.
Returns C L0_STRING_CONST expression.

str l0_c_emitter::CEmitter::emit_const_string_literal(self, str value)

Emit C code for a static string literal initializer.

Parameters
value The L0 string token payload.
Returns C L0_STRING_CONST macro expression.

str l0_c_emitter::CEmitter::emit_bool_literal(self, bool value)

Emit C code for a boolean literal expression.

Parameters
value The boolean value.
Returns "1" for true, "0" for false.

str l0_c_emitter::CEmitter::emit_const_bool_literal(self, bool value)

Emit C code for a boolean literal in a static initializer.

Parameters
value The boolean value.
Returns "true" or "false".

str l0_c_emitter::CEmitter::emit_var_ref(self, str c_name)

Emit C code for a variable reference identifier.

Parameters
c_name Mangled C identifier.
Returns The identifier string.

str l0_c_emitter::CEmitter::emit_unary_op(self, str op, str c_operand)

Emit C code for a unary operation.

Parameters
op C unary operator string.
c_operand C expression for the operand.
Returns C unary expression string.

str l0_c_emitter::CEmitter::emit_binary_op(self, str op, str c_left, str c_right)

Emit C code for a simple binary operation.

Parameters
op C binary operator string.
c_left C expression for left operand.
c_right C expression for right operand.
Returns C binary expression string.

str l0_c_emitter::CEmitter::emit_function_call(self, str c_func_name, str c_args)

Emit C code for a function call.

Parameters
c_func_name C identifier or expression for the function.
c_args Comma-separated C expression string for arguments.
Returns C function call expression string.

str l0_c_emitter::CEmitter::emit_field_access(self, str c_obj, str field_name, bool is_pointer)

Emit C code for field access using '.

Parameters
c_obj C expression for the object.
field_name The field identifier.
is_pointer True if the object is a pointer.
Returns C field access expression string.

' or '->'.

str l0_c_emitter::CEmitter::emit_paren_expr(self, str c_inner)

Emit C code for a parenthesized expression.

Parameters
c_inner The inner C expression string.
Returns Parenthesized expression string.

str l0_c_emitter::CEmitter::emit_cast(self, str c_type, str c_inner)

Emit C code for a type cast.

Parameters
c_type Target C type string.
c_inner Expression to cast.
Returns C cast expression string.

str l0_c_emitter::CEmitter::emit_checked_ptr_access(self, str c_ptr_expr, str c_ptr_type, str c_required_size, str c_required_align, str access_mode = "_RT_ACCESS_READ")

Emit a runtime-checked pointer access expression.

Declares one static per-call-site cache slot so the runtime validates repeated accesses from the same site with a single range check.

str l0_c_emitter::CEmitter::emit_pointer_null_check(self, str c_expr, str op)

Emit C code for pointer null comparison.

Parameters
c_expr C expression for the pointer.
op Comparison operator (e.g., "==", "!=").
Returns C comparison expression string.

str l0_c_emitter::CEmitter::emit_struct_constructor(self, str c_struct_name, List] field_inits[Tuple[str, str])

Emit C code for a struct compound literal constructor.

Parameters
c_struct_name Mangled C struct name.
field_inits List of (field_name, c_value) tuples.
Returns C compound literal: (struct name){ .f1 = v1, .f2 = v2 }.

str l0_c_emitter::CEmitter::emit_variant_constructor(self, str c_enum_name, str variant_name, str tag_value, List] payload_inits[Tuple[str, str])

Emit C code for an enum variant tagged union literal.

Parameters
c_enum_name Mangled C enum name.
variant_name Name of the variant.
tag_value C tag value.
payload_inits List of (field_name, c_value) tuples for payload.
Returns C tagged union literal string.

str l0_c_emitter::CEmitter::emit_pattern_binding_init(self, str scrutinee, str variant, str field)

Emit C code for accessing a variant field during pattern matching.

Parameters
scrutinee Name of the scrutinee variable.
variant Name of the variant being matched.
field Name of the payload field being extracted.
Returns C field access expression string.

None l0_c_emitter::CEmitter::emit_expr_stmt(self, str c_expr)

Emit an expression as a statement.

Parameters
c_expr C expression string.

None l0_c_emitter::CEmitter::emit_return_stmt(self, Optional c_value[str])

Emit a C return statement.

Parameters
c_value Optional C expression string to return.

None l0_c_emitter::CEmitter::emit_label(self, str label)

Emit a C label followed by a null statement.

Parameters
label C label identifier.

None l0_c_emitter::CEmitter::emit_goto(self, str label)

Emit a C goto statement.

Parameters
label Target C label identifier.

None l0_c_emitter::CEmitter::emit_while_header(self, str c_cond)

Emit a C while loop header.

Parameters
c_cond C expression for the loop condition.

None l0_c_emitter::CEmitter::emit_if_header(self, str c_cond)

Emit a C if statement header.

Parameters
c_cond C expression for the condition.

None l0_c_emitter::CEmitter::emit_let_decl(self, str c_type, str c_var_name, str c_init)

Emit a C local variable declaration with initializer.

Parameters
c_type C type string.
c_var_name C identifier.
c_init C initializer expression string.

None l0_c_emitter::CEmitter::emit_assignment(self, str c_target, str c_value)

Emit a simple C assignment statement.

Parameters
c_target C lvalue expression.
c_value C expression for the value.

None l0_c_emitter::CEmitter::emit_pointer_assignment(self, str c_ptr_name, str c_value)

Emit a C assignment through a pointer.

Parameters
c_ptr_name C expression evaluating to a pointer.
c_value C expression for the value.

None l0_c_emitter::CEmitter::emit_temp_decl(self, str c_type, str c_temp_name, str c_value)

Emit a C temporary variable declaration with initializer.

Parameters
c_type C type string.
c_temp_name C identifier for temporary.
c_value C initializer expression string.

None l0_c_emitter::CEmitter::emit_string_retain(self, str c_expr)

Emit an ARC string retain runtime call.

Parameters
c_expr C expression evaluating to an l0_string.

None l0_c_emitter::CEmitter::emit_string_release(self, str c_expr)

Emit an ARC string release runtime call.

Parameters
c_expr C expression evaluating to an l0_string.

None l0_c_emitter::CEmitter::emit_comment(self, str comment)

Emit a C block comment.

Parameters
comment The comment text.

None l0_c_emitter::CEmitter::emit_match_scrutinee_decl(self, str c_type, str c_expr)

Emit the scrutinee declaration for a match/case statement.

Parameters
c_type C type string of scrutinee.
c_expr C expression for scrutinee value.

None l0_c_emitter::CEmitter::emit_switch_start(self, str c_expr)

Emit a C switch statement header and opening brace.

Parameters
c_expr C expression to switch on.

None l0_c_emitter::CEmitter::emit_match_switch_start(self, str scrutinee_name)

Emit a match switch over an enum tag.

Parameters
scrutinee_name Identifier of the scrutinee variable.

None l0_c_emitter::CEmitter::emit_case_label(self, str c_tag_value)

Emit a C case label.

Parameters
c_tag_value The constant C tag identifier or literal.

None l0_c_emitter::CEmitter::emit_null_assignment(self, str c_var)

Emit a NULL assignment to a variable.

Parameters
c_var C lvalue expression.

None l0_c_emitter::CEmitter::emit_alloc_obj(self, str c_ptr_type, str c_base_type, str c_temp_name)

Emit a heap allocation runtime call.

Parameters
c_ptr_type C pointer type string.
c_base_type C base object type string.
c_temp_name Name of temporary to hold the pointer.

None l0_c_emitter::CEmitter::emit_struct_init(self, str c_temp_name, str c_base_type, str c_init_str)

Emit an object initialization through a pointer.

Parameters
c_temp_name Name of the pointer variable.
c_base_type C base object type string.
c_init_str C compound initializer body.

None l0_c_emitter::CEmitter::emit_struct_init_from_fields(self, str c_temp_name, Type base_type, List] field_inits[Tuple[str, str])

Emit struct initialization using positional field values.

Parameters
c_temp_name Name of the pointer variable.
base_type L0 base object type.
field_inits List of (field_name, c_value) tuples.

None l0_c_emitter::CEmitter::emit_enum_variant_init(self, str c_temp_name, EnumType enum_type, str variant_name, List] payload_inits[Tuple[str, str])

Emit enum variant initialization for a heap-allocated enum.

Parameters
c_temp_name Name of the pointer variable.
enum_type L0 enum type.
variant_name Name of the active variant.
payload_inits List of (field_name, c_value) tuples for payload.

None l0_c_emitter::CEmitter::emit_zero_init(self, str c_temp_name, str c_base_type)

Emit zero-initialization through a pointer.

Parameters
c_temp_name Name of the pointer variable.
c_base_type C base object type string.

None l0_c_emitter::CEmitter::emit_try_check_niche(self, str c_tmp, str ret_none)

Emit a NULL check for a niche-optimized optional.

Parameters
c_tmp C identifier of the temporary holding the optional.
ret_none C expression for the 'none' return value.

None l0_c_emitter::CEmitter::emit_try_check_value(self, str c_tmp, str ret_none)

Emit a has_value check for a value-optional.

Parameters
c_tmp C identifier of the temporary holding the optional.
ret_none C expression for the 'none' return value.

str l0_c_emitter::CEmitter::emit_try_extract_value(self, str c_tmp)

Emit C code to extract the inner value from an optional.

Parameters
c_tmp C identifier of the temporary holding the optional.
Returns C expression string for the extracted value.

None l0_c_emitter::CEmitter::_emit_optional_wrapper(self, str name, Type inner) protected

Emit one collected optional wrapper typedef.

Parameters
name Wrapper typedef name.
inner Inner wrapped type.

None l0_c_emitter::CEmitter::_emit_cleanup_by_type(self, str c_expr, Type ty) protected

Emit recursive cleanup code for any ARC-managed data inside a type.

Parameters
c_expr C expression evaluating to the by-value object to clean up.
ty The L0 type describing c_expr.

None l0_c_emitter::CEmitter::_emit_enum_cleanup_switch(self, EnumType enum_type, str c_tag_expr, Callable[[str, str], str] field_expr_for_variant_field, *bool missing_info_is_ice) protected

Emit a cleanup switch over the active variant of an enum value.

Parameters
enum_type Enum type whose payload cleanup is being emitted.
c_tag_expr C expression yielding the enum tag to switch on.
field_expr_for_variant_field Callback that maps variant and field names to the corresponding C lvalue expression.
missing_info_is_ice Whether missing enum metadata should raise an internal compiler error.

List[Tuple[str, Type]] l0_c_emitter::CEmitter::_iter_variant_cleanup_fields(self, EnumType enum_type, str variant_name, List variant_field_types[Type]) protected

Pair variant field names with their resolved field types.

Parameters
enum_type Enum type owning the variant.
variant_name Variant whose fields are being cleaned up.
variant_field_types Resolved payload field types for the variant.
Returns A list of "(field_name, field_type)" pairs, or an empty list when the AST declaration cannot be matched to the resolved payload types.

bool l0_c_emitter::CEmitter::_enum_has_arc_data(self, EnumInfo enum_info) protected

Report whether any enum payload field requires ARC-aware cleanup.

Parameters
enum_info Resolved enum metadata to inspect.
Returns True if any variant payload contains ARC-managed data.

Optional[StructInfo] l0_c_emitter::CEmitter::_get_struct_info(self, StructType struct_type, *bool strict) protected

Look up resolved metadata for a struct type.

Parameters
struct_type Struct type whose metadata is needed.
strict Whether a missing entry should raise an internal compiler error.
Returns The matching StructInfo when available, otherwise None.

Optional[EnumInfo] l0_c_emitter::CEmitter::_get_enum_info(self, EnumType enum_type, *bool strict) protected

Look up resolved metadata for an enum type.

Parameters
enum_type Enum type whose metadata is needed.
strict Whether a missing entry should raise an internal compiler error.
Returns The matching EnumInfo when available, otherwise None.