Skip to content

wake.ir.yul.switch module #

YulSwitch class #

Bases: YulStatementAbc

Represents a switch statement with the following structure:

switch <expression> {
    <cases>
}

Example

assembly {
    switch lt(i, 10)
    case 1 {
        // ...
    }
    case 2 {
        // ...
    }
    default {
        // ...
    }
}
Source code in wake/ir/yul/switch.py
class YulSwitch(YulStatementAbc):
    """
    Represents a switch statement with the following structure:

    ```solidity
    switch <expression> {
        <cases>
    }
    ```

    !!! example
        ```solidity
        assembly {
            switch lt(i, 10)
            case 1 {
                // ...
            }
            case 2 {
                // ...
            }
            default {
                // ...
            }
        }
        ```
    """

    _parent: YulBlock
    _cases: List[YulCase]
    _expression: Union[YulFunctionCall, YulIdentifier, YulLiteral]

    def __init__(self, init: IrInitTuple, switch: SolcYulSwitch, parent: YulAbc):
        super().__init__(init, switch, parent)
        if isinstance(switch.expression, SolcYulFunctionCall):
            self._expression = YulFunctionCall(init, switch.expression, self)
        elif isinstance(switch.expression, SolcYulIdentifier):
            self._expression = YulIdentifier(init, switch.expression, self)
        elif isinstance(switch.expression, SolcYulLiteral):
            self._expression = YulLiteral(init, switch.expression, self)
        else:
            assert False, f"Unexpected type: {type(switch.expression)}"
        self._cases = [YulCase(init, case, self) for case in switch.cases]

    def __iter__(self) -> Iterator[YulAbc]:
        yield self
        yield from self._expression
        for case_ in self._cases:
            yield from case_

    @property
    def parent(self) -> YulBlock:
        """
        Returns:
            Parent IR node.
        """
        return self._parent

    @property
    def cases(self) -> Tuple[YulCase, ...]:
        """
        The `default` case is optional.

        Returns:
            Tuple of cases of this switch statement in the order they appear in the source code.
        """
        return tuple(self._cases)

    @property
    def expression(self) -> Union[YulFunctionCall, YulIdentifier, YulLiteral]:
        """
        Returns:
            Expression that is evaluated to determine which case to execute.
        """
        return self._expression

    @property
    def modifies_state(
        self,
    ) -> Set[Tuple[Union[ExpressionAbc, StatementAbc, YulAbc], ModifiesStateFlag]]:
        return self._expression.modifies_state | reduce(
            or_,
            (case.modifies_state for case in self._cases),
            set(),
        )

cases: Tuple[YulCase, ...] property #

The default case is optional.

Returns:

Type Description
Tuple[YulCase, ...]

Tuple of cases of this switch statement in the order they appear in the source code.

expression: Union[YulFunctionCall, YulIdentifier, YulLiteral] property #

Returns:

Type Description
Union[YulFunctionCall, YulIdentifier, YulLiteral]

Expression that is evaluated to determine which case to execute.

parent: YulBlock property #

Returns:

Type Description
YulBlock

Parent IR node.