Skip to content

wake.ir.yul.variable_declaration module #

YulVariableDeclaration class #

Bases: YulStatementAbc

Represents a new variable declaration with the following structure:

let <variables> := <value>

Example

let a, b := foo() in the following example:

assembly {
    function foo() -> x, y {
        x := 1
        y := 2
    }

    let a, b := foo()
}
Source code in wake/ir/yul/variable_declaration.py
class YulVariableDeclaration(YulStatementAbc):
    """
    Represents a new variable declaration with the following structure:

    ```solidity
    let <variables> := <value>
    ```

    !!! example
        `:::solidity let a, b := foo()` in the following example:

        ```solidity
        assembly {
            function foo() -> x, y {
                x := 1
                y := 2
            }

            let a, b := foo()
        }
        ```
    """

    _parent: weakref.ReferenceType[YulBlock]
    _variables: List[YulTypedName]
    _value: Optional[Union[YulFunctionCall, YulIdentifier, YulLiteral]]

    def __init__(
        self,
        init: IrInitTuple,
        variable_declaration: SolcYulVariableDeclaration,
        parent: YulAbc,
    ):
        super().__init__(init, variable_declaration, parent)
        self._variables = [
            YulTypedName(init, variable, self)
            for variable in variable_declaration.variables
        ]
        if variable_declaration.value is None:
            self._value = None
        elif isinstance(variable_declaration.value, SolcYulFunctionCall):
            self._value = YulFunctionCall(init, variable_declaration.value, self)
        elif isinstance(variable_declaration.value, SolcYulIdentifier):
            self._value = YulIdentifier(init, variable_declaration.value, self)
        elif isinstance(variable_declaration.value, SolcYulLiteral):
            self._value = YulLiteral(init, variable_declaration.value, self)
        else:
            assert False, f"Unexpected type: {type(variable_declaration.value)}"

    def __iter__(self) -> Iterator[YulAbc]:
        yield self
        for variable in self._variables:
            yield from variable
        if self._value is not None:
            yield from self._value

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

    @property
    def children(
        self,
    ) -> Iterator[Union[YulTypedName, YulFunctionCall, YulIdentifier, YulLiteral]]:
        """
        Yields:
            Direct children of this node.
        """
        yield from self._variables
        if self._value is not None:
            yield self._value

    @property
    def variables(self) -> Tuple[YulTypedName, ...]:
        """
        Returns:
            Tuple of variables declared in this statement.
        """
        return tuple(self._variables)

    @property
    def value(self) -> Optional[Union[YulFunctionCall, YulIdentifier, YulLiteral]]:
        """
        Returns:
            Value assigned to the variables.
        """
        return self._value

    @property
    def modifies_state(
        self,
    ) -> Set[Tuple[Union[ExpressionAbc, StatementAbc, YulAbc], ModifiesStateFlag]]:
        if self._value is None:
            return set()
        return self._value.modifies_state

children: Iterator[Union[YulTypedName, YulFunctionCall, YulIdentifier, YulLiteral]] property #

Yields:

Type Description
Union[YulTypedName, YulFunctionCall, YulIdentifier, YulLiteral]

Direct children of this node.

parent: YulBlock property #

Returns:

Type Description
YulBlock

Parent IR node.

value: Optional[Union[YulFunctionCall, YulIdentifier, YulLiteral]] property #

Returns:

Type Description
Optional[Union[YulFunctionCall, YulIdentifier, YulLiteral]]

Value assigned to the variables.

variables: Tuple[YulTypedName, ...] property #

Returns:

Type Description
Tuple[YulTypedName, ...]

Tuple of variables declared in this statement.