l0_c_types::CTypes class

Defined in module l0_c_types (l0_c_types.py).

C type representation and optional-wrapper collection/emission.

Public static attributes

static CNames names
static CEmitterState state

Public functions

auto emit_sizeof_type(self, Type typ) -> str
Emit C code for sizeof a given L0 type.
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, Type|None expected_type, *bool for_initializer = False) -> str
Emit a null literal appropriate for the expected type.
auto emit_pointer_type(self, Type base_type) -> str
Emit C pointer type for a base type.
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.

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.

Protected static attributes

static dict _opt_wrappers
static set _opt_emitted

Function documentation

str l0_c_types::CTypes::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_types::CTypes::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_types::CTypes::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_types::CTypes::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_types::CTypes::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_types::CTypes::emit_null_literal(self, Type|None expected_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_types::CTypes::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*").

None l0_c_types::CTypes::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_types::CTypes::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_types::CTypes::_emit_optional_wrapper(self, str name, Type inner) protected

Emit one collected optional wrapper typedef.

Parameters
name Wrapper typedef name.
inner Inner wrapped type.