Skip to content

functions

Classes:

FunctionVisitorMixin

Bases: VisitorMixinBase

Methods:

visit

visit(node: FunctionReturn) -> None
Source code in src/irx/builder/lowering/functions.py
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
@VisitorCore.visit.dispatch
def visit(self, node: astx.FunctionReturn) -> None:
    """
    title: Visit FunctionReturn nodes.
    parameters:
      node:
        type: astx.FunctionReturn
    """
    return_resolution = self._semantic_return_resolution(node)
    if return_resolution.returns_void:
        self._llvm.ir_builder.ret_void()
        return

    if node.value is not None:
        self.visit_child(node.value)
        retval = require_lowered_value(
            safe_pop(self.result_stack),
            node=node.value,
            context="return expression",
        )
    else:
        retval = None

    if retval is None:
        raise_lowering_internal_error(
            "return expression did not lower to a value",
            node=node,
        )

    retval = self._cast_ast_value(
        retval,
        source_type=self._resolved_ast_type(node.value),
        target_type=return_resolution.expected_type,
    )
    fn_return_type = (
        self._llvm.ir_builder.function.function_type.return_type
    )
    if is_int_type(fn_return_type) and fn_return_type.width == 1:
        if is_int_type(retval.type) and retval.type.width != 1:
            retval = self._llvm.ir_builder.trunc(retval, ir.IntType(1))
    self._llvm.ir_builder.ret(retval)