compiler/stage2_l0/src/c_emitter.l0
Module c_emitter
Overview Symbols grouped by source file: compiler/stage2_l0/src/c_emitter.l0
Module: c_emitter
Source: compiler/stage2_l0/src/c_emitter.l0 Language: Dea/L0
Imports / Includes
std.assertstd.hashsetstd.hashmapstd.stringstd.textstd.vectorastcodegen_optionsdriverstring_escapeutil.stringssem_contextsignaturestypes
Symbols
- cem_ice
- cem_ice_string
- cem_upper_ascii
- cem_opt_wrapper_map_free
- ccb_create
- ccb_free
- ccb_indent
- ccb_dedent
- ccb_prefix
- ccb_emit
- ccb_emit_raw
- ccb_to_string
- cem_create
- cem_reset_optional_wrappers
- cem_free
- cem_set_analysis
- cem_analysis
- cem_get_output
- cem_is_c_keyword
- cem_mangle_module
- cem_mangle_struct_name
- cem_mangle_enum_name
- cem_mangle_function_name
- cem_mangle_let_name
- cem_mangle_identifier
- cem_fresh_tmp
- cem_emit_sizeof_type
- cem_emit_ord
- cem_emit_pointer_type
- cem_emit_enum_tag
- cem_is_niche_nullable
- cem_opt_key_for_type
- cem_opt_wrapper_name_for_inner
- cem_emit_type
- cem_emit_none_value_for_nullable
- cem_emit_some_value_for_nullable
- cem_emit_null_literal
- cem_emit_widen_int
- cem_emit_section_comment
- cem_emit_module_comment
- cem_emit_module_separator
- cem_emit_unreachable_comment
- cem_emit_unreachable_marker
- cem_emit_header
- cem_module_filename
- cem_emit_line_directive
- cem_restore_active_line_directive
- cem_emit_forward_decls
- cem_find_enum_variant_info
- cem_emit_struct
- cem_emit_enum
- cem_emit_let_declaration
- cem_emit_top_level_let_cleanup
- cem_function_params
- cem_emit_function_declaration
- cem_emit_function_definition_header
- cem_emit_function_definition_footer
- cem_emit_main_wrapper
- cem_note_opt_wrapper
- cem_collect_opt_wrappers_from_type
- cem_is_early_inner
- cem_emit_optional_wrapper
- cem_emit_optional_wrapper_for_defined_type
- cem_prepare_optional_wrappers
- cem_emit_optional_wrappers
- cem_emit_int_literal
- cem_emit_bool_literal
- cem_emit_const_bool_literal
- cem_encode_c_byte_literal
- cem_emit_byte_literal
- cem_emit_string_literal
- cem_emit_const_string_literal
- cem_emit_var_ref
- cem_emit_unary_op
- cem_emit_negated_condition
- cem_emit_binary_op
- cem_emit_condition_binary_op
- cem_emit_checked_int_div
- cem_emit_checked_int_mod
- cem_emit_checked_int_mul
- cem_emit_checked_int_add
- cem_emit_checked_int_sub
- cem_emit_function_call
- cem_emit_field_access
- cem_emit_paren_expr
- cem_emit_cast
- cem_emit_checked_ptr_access
- cem_emit_checked_ptr_index_access
- cem_emit_checked_ptr_access_for_base
- cem_emit_drop_begin_expr
- cem_emit_drop_finish_call
- cem_emit_checked_narrow_cast
- cem_emit_unwrap_ptr
- cem_emit_unwrap_opt
- cem_emit_null_check_eq
- cem_emit_null_check_ne
- cem_emit_pointer_null_check
- cem_emit_condition_pointer_null_check
- cem_emit_optional_has_value
- cem_emit_optional_value
- cem_emit_enum_tag_access
- cem_emit_enum_payload_field_access
- cem_emit_string_equals_call
- cem_emit_string_concat_call
- cem_emit_string_compare_call
- cem_emit_condition_string_compare_call
- cem_emit_discard_expr
- cem_emit_deref_lvalue
- cem_emit_field_lvalue
- cem_emit_index_lvalue
- cem_emit_struct_constructor
- cem_emit_struct_static_initializer
- cem_emit_struct_constructor_for_type
- cem_emit_struct_static_initializer_for_type
- cem_emit_variant_constructor
- cem_emit_variant_static_initializer
- cem_emit_variant_constructor_for_type
- cem_emit_variant_static_initializer_for_type
- cem_emit_expr_stmt
- cem_emit_return_stmt
- cem_emit_return_void_stmt
- cem_emit_label
- cem_emit_goto
- cem_emit_block_start
- cem_emit_block_end
- cem_emit_while_header
- cem_emit_if_header
- cem_emit_else
- cem_emit_for_loop_start
- cem_emit_for_loop_end
- cem_emit_let_decl
- cem_emit_assignment
- cem_emit_temp_decl
- cem_emit_comment
- cem_emit_exit_switch
- cem_emit_pointer_assignment
- cem_emit_checked_pointer_assignment
- cem_emit_string_retain
- cem_emit_string_release
- cem_emit_match_scrutinee_decl
- cem_emit_switch_start
- cem_emit_match_switch_start
- cem_emit_switch_end
- cem_emit_case_label
- cem_emit_default_label
- cem_emit_null_assignment
- cem_emit_alloc_obj
- cem_emit_struct_init
- cem_emit_struct_init_from_fields
- cem_emit_enum_variant_init
- cem_emit_zero_init
- cem_emit_try_check_niche
- cem_emit_try_check_value
- cem_emit_try_extract_value
- cem_emit_pattern_binding_init
- cem_find_enum_decl
- cem_enum_has_arc_data
- cem_emit_value_cleanup
- cem_emit_struct_cleanup
- cem_emit_enum_cleanup
- CCodeBuilder
- CEmitter
Function cem_ice
func cem_ice(message: string)
Abort on an internal code-emission error.
Parameters:
message: ICE message to report.
Function cem_ice_string
func cem_ice_string(message: string) -> string
Abort on an internal code-emission error from a string-returning helper.
Parameters:
message: ICE message to report.
Returns: Dummy return used only to satisfy control-flow typing.
Function cem_upper_ascii
func cem_upper_ascii(text: string) -> string
Build an all-uppercase ASCII variant of an identifier-like string.
Parameters:
text: Input text.
Returns: Uppercased ASCII text.
Function cem_opt_wrapper_map_free
func cem_opt_wrapper_map_free(items: StringPtrMap*)
Free every collected optional-wrapper inner type.
Parameters:
items: Wrapper map to free.
Function ccb_create
func ccb_create() -> CCodeBuilder*
Create a new code builder.
Returns: New builder with four-space indentation.
Function ccb_free
func ccb_free(self: CCodeBuilder*)
Free a code builder.
Parameters:
self: Builder to free.
Function ccb_indent
func ccb_indent(self: CCodeBuilder*)
Increase indentation level by one.
Parameters:
self: Builder to update.
Function ccb_dedent
func ccb_dedent(self: CCodeBuilder*)
Decrease indentation level by one.
Parameters:
self: Builder to update.
Function ccb_prefix
func ccb_prefix(self: CCodeBuilder*) -> string
Build the current indentation prefix.
Parameters:
self: Builder to inspect.
Returns: Indentation string for the current level.
Function ccb_emit
func ccb_emit(self: CCodeBuilder*, line: string)
Emit one line with indentation.
Parameters:
self: Builder to update.line: Line text, or""for a blank line.
Function ccb_emit_raw
func ccb_emit_raw(self: CCodeBuilder*, line: string)
Emit one line without indentation.
Parameters:
self: Builder to update.line: Raw line text.
Function ccb_to_string
func ccb_to_string(self: CCodeBuilder*) -> string
Convert the accumulated lines to one source string with a trailing newline.
Parameters:
self: Builder to inspect.
Returns: Combined source text.
Function cem_create
func cem_create() -> CEmitter*
Create a Stage 2 C emitter.
Returns: New emitter.
Function cem_reset_optional_wrappers
func cem_reset_optional_wrappers(self: CEmitter*)
Clear every collected optional-wrapper entry.
Parameters:
self: Emitter to update.
Function cem_free
func cem_free(self: CEmitter*)
Free a Stage 2 C emitter.
Parameters:
self: Emitter to free.
Function cem_set_analysis
func cem_set_analysis(self: CEmitter*, analysis: AnalysisResult*)
Attach analysis data to an emitter.
Parameters:
self: Emitter to update.analysis: Analysis result to borrow.
Function cem_analysis
func cem_analysis(self: CEmitter*) -> AnalysisResult*
Return the borrowed analysis result.
Parameters:
self: Emitter to inspect.
Returns: Borrowed analysis result.
Function cem_get_output
func cem_get_output(self: CEmitter*) -> string
Return the generated C source text.
Parameters:
self: Emitter to inspect.
Returns: Complete source text.
Function cem_is_c_keyword
func cem_is_c_keyword(name: string) -> bool
Return whether a name conflicts with a C keyword or restricted identifier.
Parameters:
name: Identifier text to inspect.
Returns: true if the name must be mangled.
Function cem_mangle_module
func cem_mangle_module(module_name: string) -> string
Replace dots with underscores for C symbol names.
Parameters:
module_name: Module name to sanitize.
Returns: C-safe module spelling.
Function cem_mangle_struct_name
func cem_mangle_struct_name(module_name: string, struct_name: string) -> string
Mangle a struct name.
Parameters:
module_name: Defining module name.struct_name: Struct name.
Returns: Mangled C struct name.
Function cem_mangle_enum_name
func cem_mangle_enum_name(module_name: string, enum_name: string) -> string
Mangle an enum name.
Parameters:
module_name: Defining module name.enum_name: Enum name.
Returns: Mangled C enum name.
Function cem_mangle_function_name
func cem_mangle_function_name(module_name: string, func_name: string) -> string
Mangle a function name.
Parameters:
module_name: Defining module name.func_name: Function name.
Returns: Mangled C function name.
Function cem_mangle_let_name
func cem_mangle_let_name(module_name: string, let_name: string) -> string
Mangle a top-level let name.
Parameters:
module_name: Defining module name.let_name: Let name.
Returns: Mangled C identifier.
Function cem_mangle_identifier
func cem_mangle_identifier(name: string) -> string
Mangle a local identifier when needed.
Parameters:
name: Identifier to inspect.
Returns: Original or suffixed identifier.
Function cem_fresh_tmp
func cem_fresh_tmp(self: CEmitter*, kind: string) -> string
Produce a fresh temporary identifier.
Parameters:
self: Emitter to update.kind: Temp-kind prefix.
Returns: Unique temp identifier.
Function cem_emit_sizeof_type
func cem_emit_sizeof_type(typ: Type*) -> string
Emit C code for sizeof(<type>) .
Parameters:
typ: Type to measure.
Returns: Casted sizeof expression.
Function cem_emit_ord
func cem_emit_ord(c_enum_expr: string) -> string
Emit C code for ord(enum_expr) .
Parameters:
c_enum_expr: C expression producing an enum value.
Returns: Casted tag extraction expression.
Function cem_emit_pointer_type
func cem_emit_pointer_type(base_type: Type*) -> string
Emit a pointer type from a base type.
Parameters:
base_type: Base type to wrap.
Returns: Pointer type spelling.
Function cem_emit_enum_tag
func cem_emit_enum_tag(enum_type: Type*, variant_name: string) -> string
Emit the C tag constant for an enum variant.
Parameters:
enum_type: Enum type to inspect.variant_name: Variant name.
Returns: Mangled enum-tag identifier.
Function cem_is_niche_nullable
func cem_is_niche_nullable(t: Type*) -> bool
Return whether a nullable type uses pointer-shaped niche encoding.
Parameters:
t: Nullable type to inspect.
Returns: true when null lowers to NULL.
Function cem_opt_key_for_type
func cem_opt_key_for_type(t: Type*) -> string
Produce the stable wrapper-key suffix for an optional inner type.
Parameters:
t: Inner type to encode.
Returns: Stable wrapper-key suffix.
Function cem_opt_wrapper_name_for_inner
func cem_opt_wrapper_name_for_inner(inner: Type*) -> string
Return the wrapper typedef name used for a non-pointer nullable type.
Parameters:
inner: Inner type of the nullable wrapper.
Returns: Wrapper typedef name.
Function cem_emit_type
func cem_emit_type(typ: Type*) -> string
Emit an L0 type as a C type spelling.
Parameters:
typ: Type to lower.
Returns: C spelling for typ.
Function cem_emit_none_value_for_nullable
func cem_emit_none_value_for_nullable(t: Type*) -> string
Emit a none value for a non-pointer nullable type.
Parameters:
t: Nullable type to lower.
Returns: C initializer/value for null.
Function cem_emit_some_value_for_nullable
func cem_emit_some_value_for_nullable(t: Type*, c_inner_expr: string) -> string
Emit a wrapped some value for a non-pointer nullable type.
Parameters:
t: Nullable type to lower.c_inner_expr: C expression for the inner value.
Returns: Wrapper construction expression.
Function cem_emit_null_literal
func cem_emit_null_literal(expected_type: Type*, for_initializer: bool) -> string
Emit a null literal for the expected type.
Parameters:
expected_type: Expected target type.for_initializer: Whether the literal is used in static initialization.
Returns: C spelling for the literal.
Function cem_emit_widen_int
func cem_emit_widen_int(c_expr: string, src_type: Type*, dst_type: Type*) -> string
Emit a widening cast for an implicit integer conversion.
Parameters:
c_expr: Source expression.src_type: Source builtin type.dst_type: Destination builtin type.
Returns: Widened C expression.
Function cem_emit_section_comment
func cem_emit_section_comment(self: CEmitter*, text: string)
Emit a section comment.
Parameters:
self: Emitter to update.text: Comment text.
Function cem_emit_module_comment
func cem_emit_module_comment(self: CEmitter*, module_name: string)
Emit a module comment.
Parameters:
self: Emitter to update.module_name: Module name to report.
Function cem_emit_module_separator
func cem_emit_module_separator(self: CEmitter*, module_name: string)
Emit a decorative module separator.
Parameters:
self: Emitter to update.module_name: Module name to report.
Function cem_emit_unreachable_comment
func cem_emit_unreachable_comment(self: CEmitter*)
Emit an unreachable-code comment.
Parameters:
self: Emitter to update.
Function cem_emit_unreachable_marker
func cem_emit_unreachable_marker(self: CEmitter*, reason: string)
Emit an unreachable runtime marker.
Parameters:
self: Emitter to update.reason: Marker reason string.
Function cem_emit_header
func cem_emit_header(self: CEmitter*, opts: CodegenOptions*)
Emit the fixed header boilerplate for one C translation unit.
Parameters:
self: Emitter to update.opts: Codegen options to honor.
Function cem_module_filename
func cem_module_filename(self: CEmitter*, module_name: string) -> string
Return the source filename for a loaded module.
Parameters:
self: Emitter to inspect.module_name: Loaded module name.
Returns: Source filename when available.
Function cem_emit_line_directive
func cem_emit_line_directive(self: CEmitter*, line: int, current_module: string, opts: CodegenOptions*)
Emit a #line directive when enabled.
Parameters:
self: Emitter to update.line: One-based source line.current_module: Current source module.opts: Codegen options controlling line directives.
Function cem_restore_active_line_directive
func cem_restore_active_line_directive(self: CEmitter*)
Re-emit the current source line directive after generated helper lines.
Parameters:
self: Emitter to update.
Function cem_emit_forward_decls
func cem_emit_forward_decls(self: CEmitter*)
Emit forward declarations for all structs and enums in compilation order.
Parameters:
self: Emitter to update.
Function cem_find_enum_variant_info
func cem_find_enum_variant_info(enum_info: EnumInfo*, variant_name: string) -> EnumVariantInfo*?
Look up enum payload metadata by variant name.
Parameters:
enum_info: Enum metadata to inspect.variant_name: Variant name to search for.
Returns: Matching variant metadata, or null if missing.
Function cem_emit_struct
func cem_emit_struct(self: CEmitter*, module_name: string, decl: StructDecl*, struct_info: StructInfo*)
Emit a complete C struct definition.
Parameters:
self: Emitter to update.module_name: Name of the defining module.decl: Struct declaration node.struct_info: Resolved struct metadata.
Function cem_emit_enum
func cem_emit_enum(self: CEmitter*, module_name: string, decl: EnumDecl*, enum_info: EnumInfo*)
Emit an enum as a C tagged-union struct.
Parameters:
self: Emitter to update.module_name: Name of the defining module.decl: Enum declaration node.enum_info: Resolved enum metadata.
Function cem_emit_let_declaration
func cem_emit_let_declaration(self: CEmitter*, module_name: string, decl: LetDecl*, let_type: Type*, c_init: string)
Emit one top-level let declaration as a static C variable.
Parameters:
self: Emitter to update.module_name: Name of the defining module.decl: Let declaration node.let_type: Resolved let type.c_init: C initializer expression.
Function cem_emit_top_level_let_cleanup
func cem_emit_top_level_let_cleanup(self: CEmitter*)
Emit final cleanup for ARC-managed top-level lets before process exit.
Parameters:
self: Emitter to update.
Function cem_function_params
func cem_function_params(decl: FuncDecl*, func_type: Type*) -> string
Build a function parameter list.
Parameters:
decl: Function declaration node.func_type: Resolved function type.
Returns: Parameter-list text without surrounding parentheses.
Function cem_emit_function_declaration
func cem_emit_function_declaration(self: CEmitter*, module_name: string, decl: FuncDecl*, func_type: Type*)
Emit a function declaration.
Parameters:
self: Emitter to update.module_name: Name of the defining module.decl: Function declaration node.func_type: Resolved function type.
Function cem_emit_function_definition_header
func cem_emit_function_definition_header(self: CEmitter*, module_name: string, decl: FuncDecl*, func_type: Type*)
Emit a function definition header.
Parameters:
self: Emitter to update.module_name: Name of the defining module.decl: Function declaration node.func_type: Resolved function type.
Function cem_emit_function_definition_footer
func cem_emit_function_definition_footer(self: CEmitter*)
Emit a function definition footer.
Parameters:
self: Emitter to update.
Function cem_emit_main_wrapper
func cem_emit_main_wrapper(self: CEmitter*, entry_module: string, func_type: Type*)
Emit the C main() wrapper for the entry module.
Parameters:
self: Emitter to update.entry_module: Entry-module name.func_type: Resolved entry-function type.
Function cem_note_opt_wrapper
func cem_note_opt_wrapper(self: CEmitter*, inner: Type*)
Record one optional-wrapper inner type if it was not already collected.
Parameters:
self: Emitter to update.inner: Inner type to store.
Function cem_collect_opt_wrappers_from_type
func cem_collect_opt_wrappers_from_type(self: CEmitter*, t: Type*)
Recursively collect optional-wrapper types reachable from one type.
Parameters:
self: Emitter to update.t: Type to scan.
Function cem_is_early_inner
func cem_is_early_inner(inner: Type*) -> bool
Return whether a wrapper inner type can be emitted before user type definitions.
Parameters:
inner: Inner wrapped type.
Returns: true for builtin-only wrapper dependencies.
Function cem_emit_optional_wrapper
func cem_emit_optional_wrapper(self: CEmitter*, name: string, inner: Type*)
Emit one collected optional-wrapper typedef.
Parameters:
self: Emitter to update.name: Wrapper typedef name.inner: Inner wrapped type.
Function cem_emit_optional_wrapper_for_defined_type
func cem_emit_optional_wrapper_for_defined_type(self: CEmitter*, inner: Type*)
Emit the collected optional wrapper for a just-defined user type.
Parameters:
self: Emitter to update.inner: Just-defined struct or enum type.
Function cem_prepare_optional_wrappers
func cem_prepare_optional_wrappers(self: CEmitter*)
Scan analysis metadata and collect every needed optional wrapper.
Parameters:
self: Emitter to update.
Function cem_emit_optional_wrappers
func cem_emit_optional_wrappers(self: CEmitter*, early: bool)
Emit collected optional-wrapper typedefs for one phase.
Parameters:
self: Emitter to update.early:truefor builtin-only wrappers,falsefor user-defined wrappers.
Function cem_emit_int_literal
func cem_emit_int_literal(value: int) -> string
Emit an integer literal.
Parameters:
value: Integer value.
Returns: C spelling for the literal.
Function cem_emit_bool_literal
func cem_emit_bool_literal(value: bool) -> string
Emit a boolean literal.
Parameters:
value: Boolean value.
Returns: C spelling for the literal.
Function cem_emit_const_bool_literal
func cem_emit_const_bool_literal(value: bool) -> string
Emit a boolean literal for static initialization.
Parameters:
value: Boolean value.
Returns: C spelling for the static constant.
Function cem_encode_c_byte_literal
func cem_encode_c_byte_literal(value: byte) -> string
Encode one byte for a C character literal body.
Parameters:
value: Byte value.
Returns: Escaped character-literal body without surrounding quotes.
Function cem_emit_byte_literal
func cem_emit_byte_literal(value: byte) -> string
Emit a byte literal as a casted C character constant.
Parameters:
value: Byte value.
Returns: C spelling for the literal.
Function cem_emit_string_literal
func cem_emit_string_literal(value: string) -> string
Emit a decoded string literal through L0_STRING_CONST .
Parameters:
value: Decoded string bytes.
Returns: C spelling for the runtime string value.
Function cem_emit_const_string_literal
func cem_emit_const_string_literal(value: string) -> string
Emit a decoded string literal for static initialization.
Parameters:
value: Decoded string bytes.
Returns: C spelling for the static constant.
Function cem_emit_var_ref
func cem_emit_var_ref(c_name: string) -> string
Emit a variable reference expression.
Parameters:
c_name: C identifier to reference.
Returns: The identifier text.
Function cem_emit_unary_op
func cem_emit_unary_op(op: string, c_operand: string) -> string
Emit a unary operation expression.
Parameters:
op: Unary operator token.c_operand: Lowered operand expression.
Returns: Parenthesized C unary expression.
Function cem_emit_negated_condition
func cem_emit_negated_condition(c_cond: string) -> string
Emit a negated condition expression for control-flow lowering.
Parameters:
c_cond: Lowered condition expression.
Returns: Negated condition expression.
Function cem_emit_binary_op
func cem_emit_binary_op(op: string, c_left: string, c_right: string) -> string
Emit a binary operation expression.
Parameters:
op: Binary operator token.c_left: Lowered left operand.c_right: Lowered right operand.
Returns: Parenthesized C binary expression.
Function cem_emit_condition_binary_op
func cem_emit_condition_binary_op(op: string, c_left: string, c_right: string) -> string
Emit a top-level binary condition for direct statement headers.
Parameters:
op: Binary operator token.c_left: Lowered left operand.c_right: Lowered right operand.
Returns: Non-parenthesized C binary expression.
Function cem_emit_checked_int_div
func cem_emit_checked_int_div(c_left: string, c_right: string) -> string
Emit a checked integer division call.
Parameters:
c_left: Lowered left operand.c_right: Lowered right operand.
Returns: Runtime-call expression.
Function cem_emit_checked_int_mod
func cem_emit_checked_int_mod(c_left: string, c_right: string) -> string
Emit a checked integer modulo call.
Parameters:
c_left: Lowered left operand.c_right: Lowered right operand.
Returns: Runtime-call expression.
Function cem_emit_checked_int_mul
func cem_emit_checked_int_mul(c_left: string, c_right: string) -> string
Emit a checked integer multiplication call.
Parameters:
c_left: Lowered left operand.c_right: Lowered right operand.
Returns: Runtime-call expression.
Function cem_emit_checked_int_add
func cem_emit_checked_int_add(c_left: string, c_right: string) -> string
Emit a checked integer addition call.
Parameters:
c_left: Lowered left operand.c_right: Lowered right operand.
Returns: Runtime-call expression.
Function cem_emit_checked_int_sub
func cem_emit_checked_int_sub(c_left: string, c_right: string) -> string
Emit a checked integer subtraction call.
Parameters:
c_left: Lowered left operand.c_right: Lowered right operand.
Returns: Runtime-call expression.
Function cem_emit_function_call
func cem_emit_function_call(c_func_name: string, c_args: string) -> string
Emit a function-call expression.
Parameters:
c_func_name: Function expression.c_args: Comma-separated lowered arguments.
Returns: Function-call expression.
Function cem_emit_field_access
func cem_emit_field_access(c_obj: string, field_name: string, is_pointer: bool) -> string
Emit a field-access expression.
Parameters:
c_obj: Object expression.field_name: Field identifier.is_pointer: Whether the object is a pointer.
Returns: Field-access expression using . or ->.
Function cem_emit_paren_expr
func cem_emit_paren_expr(c_inner: string) -> string
Emit a parenthesized expression.
Parameters:
c_inner: Inner expression.
Returns: Parenthesized expression.
Function cem_emit_cast
func cem_emit_cast(c_type: string, c_inner: string) -> string
Emit a C cast expression.
Parameters:
c_type: Destination C type.c_inner: Inner expression.
Returns: Cast expression.
Function cem_emit_checked_ptr_access
func cem_emit_checked_ptr_access(self: CEmitter*, c_ptr_expr: string, c_ptr_type: string, c_required_size: string, c_required_align: string, access_mode: string) -> string
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.
Parameters:
self: Emitter to update.c_ptr_expr: Pointer expression.c_ptr_type: Lowered pointer type.c_required_size: Required byte extent expression.c_required_align: Required alignment expression.access_mode: Runtime access mode expression.
Returns: Checked pointer expression.
Function cem_emit_checked_ptr_index_access
func cem_emit_checked_ptr_index_access(self: CEmitter*, c_base_expr: string, c_index_expr: string, c_ptr_type: string, c_element_size: string, c_required_align: string, access_mode: string) -> string
Emit a runtime-checked indexed pointer access expression.
Parameters:
self: Emitter to update.c_base_expr: Base pointer expression.c_index_expr: Index expression.c_ptr_type: Lowered pointer type.c_element_size: Element byte extent expression.c_required_align: Required alignment expression.access_mode: Runtime access mode expression.
Returns: Checked pointer expression.
Function cem_emit_checked_ptr_access_for_base
func cem_emit_checked_ptr_access_for_base(self: CEmitter*, c_ptr_expr: string, c_base_type: string, access_mode: string) -> string
Emit a checked pointer to one complete object of the given C base type.
Parameters:
self: Emitter to update.c_ptr_expr: Pointer expression.c_base_type: Lowered pointee type.access_mode: Runtime access mode expression.
Returns: Checked pointer expression.
Function cem_emit_drop_begin_expr
func cem_emit_drop_begin_expr(c_ptr_expr: string, c_ptr_type: string, c_required_size: string, c_required_align: string) -> string
Emit the expression that validates and begins a generated drop.
Parameters:
c_ptr_expr: Pointer expression.c_ptr_type: Lowered pointer type.c_required_size: Required pointee byte extent.c_required_align: Required pointee alignment.
Returns: Checked pointer expression.
Function cem_emit_drop_finish_call
func cem_emit_drop_finish_call(self: CEmitter*, c_ptr_expr: string)
Emit the runtime call that completes a generated drop.
Parameters:
self: Emitter to update.c_ptr_expr: Checked drop pointer expression.
Function cem_emit_checked_narrow_cast
func cem_emit_checked_narrow_cast(c_dst_type: string, c_inner: string) -> string
Emit a checked narrowing cast call.
Parameters:
c_dst_type: Destination C type name.c_inner: Inner expression.
Returns: Runtime-call expression.
Function cem_emit_unwrap_ptr
func cem_emit_unwrap_ptr(c_dst_type: string, c_inner: string, type_str: string) -> string
Emit an unwrap call for pointer-shaped nullable values.
Parameters:
c_dst_type: Destination C type.c_inner: Inner nullable expression.type_str: Human-readable source type.
Returns: Unwrap expression.
Function cem_emit_unwrap_opt
func cem_emit_unwrap_opt(c_src_type: string, c_inner: string, type_str: string) -> string
Emit an unwrap call for wrapper-shaped nullable values.
Parameters:
c_src_type: Source wrapper type.c_inner: Inner nullable expression.type_str: Human-readable source type.
Returns: Unwrap expression.
Function cem_emit_null_check_eq
func cem_emit_null_check_eq(c_expr: string) -> string
Emit a null-equality check for wrapper-shaped nullable values.
Parameters:
c_expr: Wrapper expression.
Returns: Equality-check expression.
Function cem_emit_null_check_ne
func cem_emit_null_check_ne(c_expr: string) -> string
Emit a null-inequality check for wrapper-shaped nullable values.
Parameters:
c_expr: Wrapper expression.
Returns: Inequality-check expression.
Function cem_emit_pointer_null_check
func cem_emit_pointer_null_check(c_expr: string, op: string) -> string
Emit a pointer null comparison.
Parameters:
c_expr: Pointer expression.op: Comparison operator.
Returns: Comparison expression.
Function cem_emit_condition_pointer_null_check
func cem_emit_condition_pointer_null_check(c_expr: string, op: string) -> string
Emit a top-level pointer null comparison for direct statement headers.
Parameters:
c_expr: Pointer expression.op: Comparison operator.
Returns: Non-parenthesized comparison expression.
Function cem_emit_optional_has_value
func cem_emit_optional_has_value(c_expr: string) -> string
Emit access to an optional wrapper's has_value flag.
Parameters:
c_expr: Wrapper expression.
Returns: Member-access expression.
Function cem_emit_optional_value
func cem_emit_optional_value(c_expr: string) -> string
Emit access to an optional wrapper's payload value.
Parameters:
c_expr: Wrapper expression.
Returns: Member-access expression.
Function cem_emit_enum_tag_access
func cem_emit_enum_tag_access(c_expr: string) -> string
Emit access to an enum tag.
Parameters:
c_expr: Enum expression.
Returns: Member-access expression.
Function cem_emit_enum_payload_field_access
func cem_emit_enum_payload_field_access(c_expr: string, variant: string, field: string) -> string
Emit access to one enum payload field.
Parameters:
c_expr: Enum expression.variant: Active variant name.field: Field name.
Returns: Member-access expression.
Function cem_emit_string_equals_call
func cem_emit_string_equals_call(lhs: string, rhs: string) -> string
Emit the runtime string-equality helper call.
Parameters:
lhs: Left operand.rhs: Right operand.
Returns: Call expression.
Function cem_emit_string_concat_call
func cem_emit_string_concat_call(lhs: string, rhs: string) -> string
Emit the runtime string-concatenation helper call.
Parameters:
lhs: Left operand.rhs: Right operand.
Returns: Call expression.
Function cem_emit_string_compare_call
func cem_emit_string_compare_call(op: string, lhs: string, rhs: string) -> string
Emit the runtime string-compare helper call plus a relational check.
Parameters:
op: Relational operator token.lhs: Left operand.rhs: Right operand.
Returns: Parenthesized comparison expression.
Function cem_emit_condition_string_compare_call
func cem_emit_condition_string_compare_call(op: string, lhs: string, rhs: string) -> string
Emit a top-level string relational check for direct statement headers.
Parameters:
op: Relational operator token.lhs: Left operand.rhs: Right operand.
Returns: Non-parenthesized comparison expression.
Function cem_emit_discard_expr
func cem_emit_discard_expr(c_expr: string) -> string
Emit a statement-context discard wrapper for an expression.
Parameters:
c_expr: Expression to discard.
Returns: Wrapped expression.
Function cem_emit_deref_lvalue
func cem_emit_deref_lvalue(ptr_expr: string) -> string
Emit a dereference lvalue.
Parameters:
ptr_expr: Pointer expression.
Returns: Dereference lvalue text.
Function cem_emit_field_lvalue
func cem_emit_field_lvalue(obj: string, field: string, is_pointer: bool) -> string
Emit a field lvalue.
Parameters:
obj: Object expression.field: Field identifier.is_pointer: Whether the object is a pointer.
Returns: Lvalue field-access text.
Function cem_emit_index_lvalue
func cem_emit_index_lvalue(base: string, index: string) -> string
Emit an index lvalue.
Parameters:
base: Base expression.index: Index expression.
Returns: Indexed lvalue text.
Function cem_emit_struct_constructor
func cem_emit_struct_constructor(c_struct_name: string, field_inits: StringVector*) -> string
Emit a struct constructor literal.
Parameters:
c_struct_name: Mangled C struct name.field_inits: Vector of alternating field-name and field-value strings.
Returns: Compound-literal expression.
Function cem_emit_struct_static_initializer
func cem_emit_struct_static_initializer(field_inits: StringVector*) -> string
Emit a brace-only struct initializer for static storage duration.
Parameters:
field_inits: Vector of alternating field-name and field-value strings.
Returns: Brace-only initializer text.
Function cem_emit_struct_constructor_for_type
func cem_emit_struct_constructor_for_type(struct_type: Type*, field_inits: StringVector*) -> string
Emit a struct constructor for a resolved struct type.
Parameters:
struct_type: Struct type to construct.field_inits: Vector of alternating field-name and field-value strings.
Returns: Compound-literal expression.
Function cem_emit_struct_static_initializer_for_type
func cem_emit_struct_static_initializer_for_type(struct_type: Type*, field_inits: StringVector*) -> string
Emit a brace-only struct initializer for a resolved struct type.
Parameters:
struct_type: Struct type to construct.field_inits: Vector of alternating field-name and field-value strings.
Returns: Brace-only initializer text.
Function cem_emit_variant_constructor
func cem_emit_variant_constructor(c_enum_name: string, variant_name: string, tag_value: string, payload_inits: StringVector*) -> string
Emit an enum-variant constructor literal.
Parameters:
c_enum_name: Mangled C enum name.variant_name: Variant name.tag_value: Mangled tag constant.payload_inits: Vector of alternating field-name and field-value strings.
Returns: Tagged-union literal expression.
Function cem_emit_variant_static_initializer
func cem_emit_variant_static_initializer(variant_name: string, tag_value: string, payload_inits: StringVector*) -> string
Emit a brace-only enum-variant initializer for static storage duration.
Parameters:
variant_name: Variant name.tag_value: Mangled tag constant.payload_inits: Vector of alternating field-name and field-value strings.
Returns: Brace-only tagged-union initializer.
Function cem_emit_variant_constructor_for_type
func cem_emit_variant_constructor_for_type(enum_type: Type*, variant_name: string, payload_inits: StringVector*) -> string
Emit an enum-variant constructor for a resolved enum type.
Parameters:
enum_type: Enum type to construct.variant_name: Variant name.payload_inits: Vector of alternating field-name and field-value strings.
Returns: Tagged-union literal expression.
Function cem_emit_variant_static_initializer_for_type
func cem_emit_variant_static_initializer_for_type(enum_type: Type*, variant_name: string, payload_inits: StringVector*) -> string
Emit a brace-only enum-variant initializer for a resolved enum type.
Parameters:
enum_type: Enum type to construct.variant_name: Variant name.payload_inits: Vector of alternating field-name and field-value strings.
Returns: Brace-only tagged-union initializer.
Function cem_emit_expr_stmt
func cem_emit_expr_stmt(self: CEmitter*, c_expr: string)
Emit an expression statement.
Parameters:
self: Emitter to update.c_expr: Lowered expression.
Function cem_emit_return_stmt
func cem_emit_return_stmt(self: CEmitter*, c_value: string)
Emit a return statement.
Parameters:
self: Emitter to update.c_value: Lowered return value.
Function cem_emit_return_void_stmt
func cem_emit_return_void_stmt(self: CEmitter*)
Emit a return; statement.
Parameters:
self: Emitter to update.
Function cem_emit_label
func cem_emit_label(self: CEmitter*, label: string)
Emit a C label.
Parameters:
self: Emitter to update.label: Label name.
Function cem_emit_goto
func cem_emit_goto(self: CEmitter*, label: string)
Emit a goto statement.
Parameters:
self: Emitter to update.label: Target label.
Function cem_emit_block_start
func cem_emit_block_start(self: CEmitter*)
Emit a block start.
Parameters:
self: Emitter to update.
Function cem_emit_block_end
func cem_emit_block_end(self: CEmitter*)
Emit a block end.
Parameters:
self: Emitter to update.
Function cem_emit_while_header
func cem_emit_while_header(self: CEmitter*, c_cond: string)
Emit a while header.
Parameters:
self: Emitter to update.c_cond: Lowered condition expression.
Function cem_emit_if_header
func cem_emit_if_header(self: CEmitter*, c_cond: string)
Emit an if header.
Parameters:
self: Emitter to update.c_cond: Lowered condition expression.
Function cem_emit_else
func cem_emit_else(self: CEmitter*)
Emit an else keyword.
Parameters:
self: Emitter to update.
Function cem_emit_for_loop_start
func cem_emit_for_loop_start(self: CEmitter*)
Emit the Stage 1-shaped for-loop wrapper start.
Parameters:
self: Emitter to update.
Function cem_emit_for_loop_end
func cem_emit_for_loop_end(self: CEmitter*)
Emit the Stage 1-shaped for-loop wrapper end.
Parameters:
self: Emitter to update.
Function cem_emit_let_decl
func cem_emit_let_decl(self: CEmitter*, c_type: string, c_var_name: string, c_init: string)
Emit a local variable declaration.
Parameters:
self: Emitter to update.c_type: Lowered C type.c_var_name: Lowered variable name.c_init: Lowered initializer.
Function cem_emit_assignment
func cem_emit_assignment(self: CEmitter*, c_target: string, c_value: string)
Emit an assignment statement.
Parameters:
self: Emitter to update.c_target: Lowered lvalue.c_value: Lowered rvalue.
Function cem_emit_temp_decl
func cem_emit_temp_decl(self: CEmitter*, c_type: string, c_temp_name: string, c_value: string)
Emit a temporary declaration.
Parameters:
self: Emitter to update.c_type: Lowered C type.c_temp_name: Temporary identifier.c_value: Lowered initializer.
Function cem_emit_comment
func cem_emit_comment(self: CEmitter*, comment: string)
Emit a block comment.
Parameters:
self: Emitter to update.comment: Comment text.
Function cem_emit_exit_switch
func cem_emit_exit_switch(self: CEmitter*)
Emit a switch-exit break; .
Parameters:
self: Emitter to update.
Function cem_emit_pointer_assignment
func cem_emit_pointer_assignment(self: CEmitter*, c_ptr_name: string, c_value: string)
Emit an assignment through a pointer.
Parameters:
self: Emitter to update.c_ptr_name: C pointer expression.c_value: Lowered value expression.
Function cem_emit_checked_pointer_assignment
func cem_emit_checked_pointer_assignment(self: CEmitter*, c_ptr_name: string, c_base_type: string, c_value: string)
Emit an assignment through a runtime-checked object pointer.
Parameters:
self: Emitter to update.c_ptr_name: C pointer expression.c_base_type: Lowered pointee type.c_value: Lowered value expression.
Function cem_emit_string_retain
func cem_emit_string_retain(self: CEmitter*, c_expr: string)
Emit an ARC string retain call.
Parameters:
self: Emitter to update.c_expr: Lowered string expression.
Function cem_emit_string_release
func cem_emit_string_release(self: CEmitter*, c_expr: string)
Emit an ARC string release call.
Parameters:
self: Emitter to update.c_expr: Lowered string expression.
Function cem_emit_match_scrutinee_decl
func cem_emit_match_scrutinee_decl(self: CEmitter*, c_type: string, c_expr: string)
Emit the temporary scrutinee declaration used by match / case .
Parameters:
self: Emitter to update.c_type: Lowered scrutinee type.c_expr: Lowered scrutinee expression.
Function cem_emit_switch_start
func cem_emit_switch_start(self: CEmitter*, c_expr: string)
Emit a switch header.
Parameters:
self: Emitter to update.c_expr: Lowered switch expression.
Function cem_emit_match_switch_start
func cem_emit_match_switch_start(self: CEmitter*, scrutinee_name: string)
Emit the switch used for enum match .
Parameters:
self: Emitter to update.scrutinee_name: Scrutinee variable name.
Function cem_emit_switch_end
func cem_emit_switch_end(self: CEmitter*)
Emit the closing brace of a switch .
Parameters:
self: Emitter to update.
Function cem_emit_case_label
func cem_emit_case_label(self: CEmitter*, c_value: string)
Emit a case label.
Parameters:
self: Emitter to update.c_value: Lowered tag or literal.
Function cem_emit_default_label
func cem_emit_default_label(self: CEmitter*)
Emit a default: label.
Parameters:
self: Emitter to update.
Function cem_emit_null_assignment
func cem_emit_null_assignment(self: CEmitter*, c_var: string)
Emit a NULL assignment.
Parameters:
self: Emitter to update.c_var: Lowered lvalue expression.
Function cem_emit_alloc_obj
func cem_emit_alloc_obj(self: CEmitter*, c_ptr_type: string, c_base_type: string, c_temp_name: string)
Emit one heap-allocation temporary.
Parameters:
self: Emitter to update.c_ptr_type: Lowered pointer type.c_base_type: Lowered base type.c_temp_name: Temporary variable name.
Function cem_emit_struct_init
func cem_emit_struct_init(self: CEmitter*, c_temp_name: string, c_base_type: string, c_init_str: string)
Emit one pointer-target aggregate initialization.
Parameters:
self: Emitter to update.c_temp_name: Pointer temporary name.c_base_type: Lowered base type.c_init_str: Lowered initializer body without outer braces.
Function cem_emit_struct_init_from_fields
func cem_emit_struct_init_from_fields(self: CEmitter*, c_temp_name: string, base_type: Type*, field_inits: StringVector*)
Emit one pointer-target struct initialization from field pairs.
Parameters:
self: Emitter to update.c_temp_name: Pointer temporary name.base_type: Struct type being initialized.field_inits: Vector of alternating field-name and field-value strings.
Function cem_emit_enum_variant_init
func cem_emit_enum_variant_init(self: CEmitter*, c_temp_name: string, enum_type: Type*, variant_name: string, payload_inits: StringVector*)
Emit one pointer-target enum-variant initialization.
Parameters:
self: Emitter to update.c_temp_name: Pointer temporary name.enum_type: Enum type being initialized.variant_name: Active variant name.payload_inits: Vector of alternating field-name and field-value strings.
Function cem_emit_zero_init
func cem_emit_zero_init(self: CEmitter*, c_temp_name: string, c_base_type: string)
Emit zero-initialization through a pointer.
Parameters:
self: Emitter to update.c_temp_name: Pointer temporary name.c_base_type: Lowered base type.
Function cem_emit_try_check_niche
func cem_emit_try_check_niche(self: CEmitter*, c_tmp: string, ret_none: string)
Emit the null-check used by ? on niche optionals.
Parameters:
self: Emitter to update.c_tmp: Temporary variable name.ret_none: Lowerednonereturn value.
Function cem_emit_try_check_value
func cem_emit_try_check_value(self: CEmitter*, c_tmp: string, ret_none: string)
Emit the has_value check used by ? on value optionals.
Parameters:
self: Emitter to update.c_tmp: Temporary variable name.ret_none: Lowerednonereturn value.
Function cem_emit_try_extract_value
func cem_emit_try_extract_value(c_tmp: string) -> string
Emit the value extraction used by ? on value optionals.
Parameters:
c_tmp: Temporary variable name.
Returns: Lowered extraction expression.
Function cem_emit_pattern_binding_init
func cem_emit_pattern_binding_init(scrutinee: string, variant: string, field: string) -> string
Emit one match-arm payload binding initializer.
Parameters:
scrutinee: Scrutinee variable name.variant: Active variant name.field: Payload field name.
Returns: Lowered payload access.
Function cem_find_enum_decl
func cem_find_enum_decl(self: CEmitter*, module_name: string, enum_name: string) -> EnumDecl*?
Look up a loaded enum declaration by module and name.
Parameters:
self: Emitter to inspect.module_name: Defining module name.enum_name: Enum name.
Returns: Matching declaration, or null if absent.
Function cem_enum_has_arc_data
func cem_enum_has_arc_data(self: CEmitter*, enum_info: EnumInfo*, decl: EnumDecl*) -> bool
Report whether any variant field in an enum carries ARC-managed data.
Parameters:
self: Emitter to inspect.enum_info: Resolved enum metadata.decl: Parsed enum declaration.
Returns: true when cleanup code must inspect the active variant.
Function cem_emit_value_cleanup
func cem_emit_value_cleanup(self: CEmitter*, c_expr: string, ty: Type*)
Emit cleanup for a by-value expression according to its type.
Parameters:
self: Emitter to update.c_expr: Lowered value expression.ty: Type of the value.
Function cem_emit_struct_cleanup
func cem_emit_struct_cleanup(self: CEmitter*, c_ptr_expr: string, struct_type: Type*)
Emit cleanup for a pointer-to-struct value.
Parameters:
self: Emitter to update.c_ptr_expr: Lowered pointer expression.struct_type: Struct type to clean.
Function cem_emit_enum_cleanup
func cem_emit_enum_cleanup(self: CEmitter*, c_ptr_expr: string, enum_type: Type*)
Emit cleanup for a pointer-to-enum value.
Parameters:
self: Emitter to update.c_ptr_expr: Lowered pointer expression.enum_type: Enum type to clean.
Struct CCodeBuilder
Represents a line-oriented C source builder with indentation tracking.
CCodeBuilder Field lines
lines: StringVector*
CCodeBuilder Field indent_level
indent_level: int
CCodeBuilder Field indent_str
indent_str: string
CCodeBuilder Field cached_prefix
cached_prefix: string
CCodeBuilder Field cached_level
cached_level: int
Struct CEmitter
Represents the Stage 2 C emitter state.
CEmitter Field analysis
analysis: AnalysisResult*?
CEmitter Field current_module
current_module: string?
CEmitter Field out
out: CCodeBuilder*
CEmitter Field tmp_counter
tmp_counter: int
CEmitter Field active_line_directive
active_line_directive: string
CEmitter Field opt_wrappers
opt_wrappers: StringPtrMap*
CEmitter Field opt_emitted
opt_emitted: StringSet*