l0_backend_state::BackendState class

Defined in module l0_backend_state (l0_backend_state.py).

Canonical backend state, scopes, semantic queries, and source context.

Public static attributes

static AnalysisResult analysis
static CEmitter emitter
static str current_module

Constructors, destructors, conversion operators

__post_init__(self)
Initialize emitter with analysis data.
_resolve_type_ref(self, TypeRef tref, str module_name) protected
Resolve an AST TypeRef into an l0_types.Type.
_int_type_size(self, Type src_ty) protected
Get the byte size of an integer builtin type.

Public functions

auto ice(self, str message, *Node|None node = None) -> NoReturn
Raise an internal compiler error.
auto find_variant_decl(self, str module_name, str enum_name, str variant_name) -> EnumVariant|None
Find the EnumVariant AST node for a given variant in an enum.

Public attributes

analysis
current_module

Protected functions

auto _fresh_label(self, str prefix) -> str
Generate a unique C label name.
auto _push_scope(self) -> ScopeContext
Enter a new scope.
auto _pop_scope(self) -> None
Exit current scope.
auto _types_equal(self, Type a, Type b) -> bool
Check if two types are structurally equal.
auto _is_int_assignable(self, Type typ) -> bool
Check if a type is assignable to an integer.
auto _is_binary_op_enabled(self, Type typ) -> bool
Check if a type supports binary operations.
auto _is_place_expr(self, Expr expr) -> bool
Check if an expression refers to an existing binding.
auto _has_side_effects(self, Expr expr) -> bool
Check if the expression has side effects or contains function calls.
auto _pointer_type_or_none(self, Type|None ty) -> PointerType|None
Return the represented pointer type for pointer-shaped values.
auto _lookup_local_var_type(self, str var_name) -> Type|None
Look up a local variable's type in the current scope chain.
auto _lookup_owned_local_name(self, VarRef expr) -> str|None
Return the mangled local name when a VarRef resolves to an owned local binding.
auto _expect_expr_type(self, Expr expr) -> Type
Look up an expression's type and fail if missing.
auto _emit_line_directive(self, Node node) -> None
Emit #line directive if node has span info and context allows it.
auto _scope_chain_has_cleanup(self) -> bool
Check if any scope in the chain has cleanup requirements.
auto _resolve_let_type(self, LetStmt stmt, str module_name) -> Type
Resolve concrete type for a let declaration.
auto _lookup_symbol(self, str name, str current_module_name, list|None module_path[str] = None) -> Symbol|None
Look up a symbol in the current module's environment.
auto _is_extern_function(self, Symbol sym) -> bool
Check if a symbol is an 'extern' function.

Protected static attributes

static Type _current_func_result
static ScopeContext _current_scope
static list _loop_cleanup_scope_stack
static int _switch_depth
static list _loop_label_stack
static int _label_counter
static bool _next_stmt_unreachable

Protected attributes

_current_scope

Function documentation

l0_backend_state::BackendState::_resolve_type_ref(self, TypeRef tref, str module_name) protected

Resolve an AST TypeRef into an l0_types.Type.

Parameters
tref The TypeRef AST node.
module_name Name of current module.
Returns The resolved Type.

This is needed so "let x: int? = null;" uses the declared type (int?) instead of the initializer type (null).

l0_backend_state::BackendState::_int_type_size(self, Type src_ty) protected

Get the byte size of an integer builtin type.

Parameters
src_ty The type to check.
Returns Byte size (1 for byte, 4 for int).
Exceptions
InternalCompilerError If type is not an integer builtin.

NoReturn l0_backend_state::BackendState::ice(self, str message, *Node|None node = None)

Raise an internal compiler error.

Parameters
message The error message.
node Optional AST node associated with the error.
Exceptions
InternalCompilerError Always raised with the provided message and location.

EnumVariant|None l0_backend_state::BackendState::find_variant_decl(self, str module_name, str enum_name, str variant_name)

Find the EnumVariant AST node for a given variant in an enum.

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

This is needed to get field names when binding pattern variables, since pattern variables are positional, but we need to access fields by name.

str l0_backend_state::BackendState::_fresh_label(self, str prefix) protected

Generate a unique C label name.

Parameters
prefix Prefix for the label name.
Returns A unique label string.

ScopeContext l0_backend_state::BackendState::_push_scope(self) protected

Enter a new scope.

Returns The newly created ScopeContext.

None l0_backend_state::BackendState::_pop_scope(self) protected

Exit current scope.

Exceptions
InternalCompilerError If there is no current scope to pop.

bool l0_backend_state::BackendState::_types_equal(self, Type a, Type b) protected

Check if two types are structurally equal.

Parameters
a First type.
b Second type.
Returns True if types are equal, False otherwise.

bool l0_backend_state::BackendState::_is_int_assignable(self, Type typ) protected

Check if a type is assignable to an integer.

Parameters
typ The type to check.
Returns True if it's an 'int' or 'byte' builtin type.

bool l0_backend_state::BackendState::_is_binary_op_enabled(self, Type typ) protected

Check if a type supports binary operations.

Parameters
typ The type to check.
Returns True if binary operations are supported for the type.

Currently only int, byte, and bool support binary operations.

bool l0_backend_state::BackendState::_is_place_expr(self, Expr expr) protected

Check if an expression refers to an existing binding.

Parameters
expr The expression to check.
Returns True if expr refers to an existing binding (retain on copy). False if expr produces a fresh value (ownership transfer, no retain).

bool l0_backend_state::BackendState::_has_side_effects(self, Expr expr) protected

Check if the expression has side effects or contains function calls.

Parameters
expr The expression to check.
Returns True if the expression has potential side effects.

Such expressions should be evaluated once and cached in a temporary to avoid multiple evaluation when used in contexts like assignment with ARC operations.

Type|None l0_backend_state::BackendState::_lookup_local_var_type(self, str var_name) protected

Look up a local variable's type in the current scope chain.

Parameters
var_name The name of the variable to look up.
Returns The variable's Type, or None if not found.

Searches declared_vars (includes both locals and parameters).

str|None l0_backend_state::BackendState::_lookup_owned_local_name(self, VarRef expr) protected

Return the mangled local name when a VarRef resolves to an owned local binding.

Parameters
expr The variable reference expression.
Returns The mangled local name if it's an owned binding, otherwise None.

Parameters are local VarRefs but are not owned by the callee, so they do not appear in owned_vars and return None.

Type l0_backend_state::BackendState::_expect_expr_type(self, Expr expr) protected

Look up an expression's type and fail if missing.

Parameters
expr The expression to look up.
Returns The resolved Type of the expression.
Exceptions
InternalCompilerError If the type is missing from the analysis.

None l0_backend_state::BackendState::_emit_line_directive(self, Node node) protected

Emit #line directive if node has span info and context allows it.

Parameters
node The AST node containing span information.

bool l0_backend_state::BackendState::_scope_chain_has_cleanup(self) protected

Check if any scope in the chain has cleanup requirements.

Returns True if any scope has a with-cleanup or owned ARC variables.

Type l0_backend_state::BackendState::_resolve_let_type(self, LetStmt stmt, str module_name) protected

Resolve concrete type for a let declaration.

Parameters
stmt The LetStmt AST node.
module_name Name of current module.
Returns The resolved Type.
Exceptions
InternalCompilerError If type cannot be inferred.

Symbol|None l0_backend_state::BackendState::_lookup_symbol(self, str name, str current_module_name, list|None module_path[str] = None) protected

Look up a symbol in the current module's environment.

Parameters
name The name of the symbol.
current_module_name Name of current module.
module_path Optional module path for qualified names.
Returns The resolved Symbol, or None if not found.

This is used to determine which module a function is defined in so we can generate the correct mangled name.

bool l0_backend_state::BackendState::_is_extern_function(self, Symbol sym) protected

Check if a symbol is an 'extern' function.

Parameters
sym The symbol to check.
Returns True if it is an extern function.