l0_backend_state.py

l0_backend_state.py

Module: l0_backend_state

Source: compiler/stage1_py/l0_backend_state.py Language: Python

Symbols

Namespace l0_backend_state

Class l0_backend_state::BackendState

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

Member Data l0_backend_state.BackendState.analysis

1
AnalysisResult analysis

Member Data l0_backend_state.BackendState.emitter

1
CEmitter emitter

Member Data l0_backend_state.BackendState.current_module

1
str current_module

Member Data l0_backend_state.BackendState._current_func_result

1
Type _current_func_result

Member Data l0_backend_state.BackendState._current_scope

1
ScopeContext _current_scope

Member Data l0_backend_state.BackendState._loop_cleanup_scope_stack

1
list _loop_cleanup_scope_stack

Member Data l0_backend_state.BackendState._switch_depth

1
int _switch_depth

Member Data l0_backend_state.BackendState._loop_label_stack

1
list _loop_label_stack

Member Data l0_backend_state.BackendState._label_counter

1
int _label_counter

Member Data l0_backend_state.BackendState._next_stmt_unreachable

1
bool _next_stmt_unreachable

Member Data l0_backend_state.BackendState.analysis

1
l0_backend_state.BackendState::analysis

Member Data l0_backend_state.BackendState.current_module

1
l0_backend_state.BackendState::current_module

Member Data l0_backend_state.BackendState._current_scope

1
l0_backend_state.BackendState::_current_scope

Function l0_backend_state.BackendState.__post_init__

1
l0_backend_state.BackendState.__post_init__(self)

Initialize emitter with analysis data.

Function l0_backend_state.BackendState.ice

1
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.

    Function l0_backend_state.BackendState.find_variant_decl

1
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.

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

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.

Function l0_backend_state.BackendState._fresh_label

1
str l0_backend_state.BackendState._fresh_label(self, str prefix)

Generate a unique C label name.

Parameters:

  • prefix: Prefix for the label name.

Returns: A unique label string.

Function l0_backend_state.BackendState._push_scope

1
ScopeContext l0_backend_state.BackendState._push_scope(self)

Enter a new scope.

Returns: The newly created ScopeContext.

Function l0_backend_state.BackendState._pop_scope

1
None l0_backend_state.BackendState._pop_scope(self)

Exit current scope.

Function l0_backend_state.BackendState._types_equal

1
bool l0_backend_state.BackendState._types_equal(self, Type a, Type b)

Check if two types are structurally equal.

Parameters:

  • a: First type.
  • b: Second type.

Returns: True if types are equal, False otherwise.

Function l0_backend_state.BackendState._is_int_assignable

1
bool l0_backend_state.BackendState._is_int_assignable(self, Type typ)

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.

Function l0_backend_state.BackendState._is_binary_op_enabled

1
bool l0_backend_state.BackendState._is_binary_op_enabled(self, Type typ)

Check if a type supports binary operations.

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

Parameters:

  • typ: The type to check.

Returns: True if binary operations are supported for the type.

Function l0_backend_state.BackendState._is_place_expr

1
bool l0_backend_state.BackendState._is_place_expr(self, Expr expr)

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).

Function l0_backend_state.BackendState._has_side_effects

1
bool l0_backend_state.BackendState._has_side_effects(self, Expr expr)

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

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

Parameters:

  • expr: The expression to check.

Returns: True if the expression has potential side effects.

Function l0_backend_state.BackendState._pointer_type_or_none

1
PointerType | None l0_backend_state.BackendState._pointer_type_or_none(self, Type|None ty)

Return the represented pointer type for pointer-shaped values.

Function l0_backend_state.BackendState._lookup_local_var_type

1
Type | None l0_backend_state.BackendState._lookup_local_var_type(self, str var_name)

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

Searches declared_vars (includes both locals and parameters).

Parameters:

  • var_name: The name of the variable to look up.

Returns: The variable’s Type, or None if not found.

Function l0_backend_state.BackendState._lookup_owned_local_name

1
str | None l0_backend_state.BackendState._lookup_owned_local_name(self, VarRef expr)

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

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

Parameters:

  • expr: The variable reference expression.

Returns: The mangled local name if it’s an owned binding, otherwise None.

Function l0_backend_state.BackendState._expect_expr_type

1
Type l0_backend_state.BackendState._expect_expr_type(self, Expr expr)

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

Parameters:

  • expr: The expression to look up.

Returns: The resolved Type of the expression.

Function l0_backend_state.BackendState._emit_line_directive

1
None l0_backend_state.BackendState._emit_line_directive(self, Node node)

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

Parameters:

  • node: The AST node containing span information.

    Function l0_backend_state.BackendState._scope_chain_has_cleanup

1
bool l0_backend_state.BackendState._scope_chain_has_cleanup(self)

Check if any scope in the chain has cleanup requirements.

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

Function l0_backend_state.BackendState._resolve_let_type

1
Type l0_backend_state.BackendState._resolve_let_type(self, LetStmt stmt, str module_name)

Resolve concrete type for a let declaration.

Parameters:

  • stmt: The LetStmt AST node.
  • module_name: Name of current module.

Returns: The resolved Type.

Function l0_backend_state.BackendState._resolve_type_ref

1
l0_backend_state.BackendState._resolve_type_ref(self, TypeRef tref, str module_name)

Resolve an AST TypeRef into an l0_types.Type .

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

Parameters:

  • tref: The TypeRef AST node.
  • module_name: Name of current module.

Returns: The resolved Type.

Function l0_backend_state.BackendState._lookup_symbol

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

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

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

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.

Function l0_backend_state.BackendState._is_extern_function

1
bool l0_backend_state.BackendState._is_extern_function(self, Symbol sym)

Check if a symbol is an ‘extern’ function.

Parameters:

  • sym: The symbol to check.

Returns: True if it is an extern function.

Function l0_backend_state.BackendState._int_type_size

1
l0_backend_state.BackendState._int_type_size(self, Type src_ty)

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).