l0_backend_lifetime::ValueLifetime class

Defined in module l0_backend_lifetime (l0_backend_lifetime.py).

ARC materialization, value retention/cleanup, and drop operations.

Public static attributes

static OwnershipConversion convert
static BackendState state

Protected functions

auto _is_borrowed_extraction(self, Expr expr) -> bool
Check whether casts or optional extraction borrow from an existing owner.
auto _needs_arc_temp(self, Expr expr) -> bool
Check if a non-place rvalue with ARC data needs temp materialization.
auto _should_materialize_arc_temp(self, Expr expr, Type expr_type) -> bool
Check if an ARC expression should be hoisted to a cleanup temp.
auto _materialize_arc_temp(self, str c_expr, Type expr_type) -> str
Materialize an ARC rvalue into a scope-owned temporary for automatic cleanup.
auto _emit_cleanup_at_scope_exit(self, ScopeContext scope) -> None
Emit cleanup at scope exit.
auto _emit_value_cleanup(self, str c_expr, Type ty) -> None
Emit cleanup code for a by-value variable before reassignment.
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_retain_for_copied_value(self, str c_expr, Type ty) -> None
Emit retain operations for a copied owned value.
auto _emit_copy_expr_with_retains(self, str c_expr, Type ty) -> str
Materialize copied values in a temp and emit retain logic when needed.
auto _emit_drop(self, DropStmt stmt, str module_name) -> None
Emit drop statement with automatic cleanup of owned fields.

Function documentation

bool l0_backend_lifetime::ValueLifetime::_is_borrowed_extraction(self, Expr expr) protected

Check whether casts or optional extraction borrow from an existing owner.

Parameters
expr The expression to check.
Returns True for non-owner-producing cast or try chains rooted in a place.

Parentheses and non-owner-producing cast chains are ownership-transparent. Owner-producing ARC value-optional wraps stop the borrowed chain.

bool l0_backend_lifetime::ValueLifetime::_needs_arc_temp(self, Expr expr) protected

Check if a non-place rvalue with ARC data needs temp materialization.

Parameters
expr The expression to check.
Returns True if temp materialization is needed.

String literals are static constants and don't need cleanup.

bool l0_backend_lifetime::ValueLifetime::_should_materialize_arc_temp(self, Expr expr, Type expr_type) protected

Check if an ARC expression should be hoisted to a cleanup temp.

Parameters
expr The expression to check.
expr_type The type of the expression.
Returns True if the expression should be materialized into a temporary.

str l0_backend_lifetime::ValueLifetime::_materialize_arc_temp(self, str c_expr, Type expr_type) protected

Materialize an ARC rvalue into a scope-owned temporary for automatic cleanup.

Parameters
c_expr The C expression string.
expr_type The type of the expression.
Returns The name of the generated temporary variable.

None l0_backend_lifetime::ValueLifetime::_emit_cleanup_at_scope_exit(self, ScopeContext scope) protected

Emit cleanup at scope exit.

Parameters
scope The scope being exited.

Only cleans variables declared in THIS scope that have owned fields.

None l0_backend_lifetime::ValueLifetime::_emit_value_cleanup(self, str c_expr, Type ty) protected

Emit cleanup code for a by-value variable before reassignment.

Parameters
c_expr C expression for the value (e.g., "x__v", "obj.field")
ty The type of the value being cleaned up

Similar to _emit_field_cleanup, but expects c_expr to be a direct value reference (not a pointer), so uses '.' instead of '->'.

None l0_backend_lifetime::ValueLifetime::_emit_struct_cleanup(self, str c_ptr_expr, StructType struct_type) protected

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 struct type.

Recursively handles nested structs (by-value fields).

None l0_backend_lifetime::ValueLifetime::_emit_enum_cleanup(self, str c_ptr_expr, EnumType enum_type) protected

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 enum type.

Uses switch on tag to only clean up the fields that are actually present.

None l0_backend_lifetime::ValueLifetime::_emit_retain_for_copied_value(self, str c_expr, Type ty) protected

Emit retain operations for a copied owned value.

Parameters
c_expr C expression evaluating to the value.
ty The type of the value.
Exceptions
InternalCompilerError If variant decl is missing for enum types.

Used when copying from place expressions so source and destination own independent references.

str l0_backend_lifetime::ValueLifetime::_emit_copy_expr_with_retains(self, str c_expr, Type ty) protected

Materialize copied values in a temp and emit retain logic when needed.

Parameters
c_expr C expression evaluating to the value.
ty The type of the value.
Returns The name of the temporary containing the copied and retained value.

None l0_backend_lifetime::ValueLifetime::_emit_drop(self, DropStmt stmt, str module_name) protected

Emit drop statement with automatic cleanup of owned fields.

Parameters
stmt The DropStmt AST node.
module_name Name of current module.
Exceptions
InternalCompilerError If variable is undefined or not a pointer.

For structs: releases all string fields. For enums: switches on tag, releases strings in active variant. Then calls the drop-finish helper to release the memory.