Skip to content

wake.ir.statements.block module #

Block class #

Bases: StatementAbc

Block statements group multiple statements into a single block.

Example

Lines 3-5 in the following code:

1
2
3
4
5
6
contract Foo {
    function bar(uint a, uint b) public pure returns(uint)
    {
        return a + b;
    }
}

Source code in wake/ir/statements/block.py
class Block(StatementAbc):
    """
    Block statements group multiple statements into a single block.

    !!! example
        Lines 3-5 in the following code:
        ```solidity linenums="1"
        contract Foo {
            function bar(uint a, uint b) public pure returns(uint)
            {
                return a + b;
            }
        }
        ```
    """

    _ast_node: SolcBlock
    _parent: weakref.ReferenceType[
        Union[
            Block,
            DoWhileStatement,
            ForStatement,
            IfStatement,
            UncheckedBlock,
            WhileStatement,  # statements
            FunctionDefinition,
            ModifierDefinition,  # declarations
            TryCatchClause,  # meta
        ]
    ]

    _statements: List[StatementAbc]

    def __init__(self, init: IrInitTuple, block: SolcBlock, parent: SolidityAbc):
        super().__init__(init, block, parent)
        self._statements = [
            StatementAbc.from_ast(init, statement, self)
            for statement in block.statements
        ]

    def __iter__(self) -> Iterator[IrAbc]:
        yield self
        if self._statements is not None:
            for statement in self._statements:
                yield from statement

    @property
    def parent(
        self,
    ) -> Union[
        Block,
        DoWhileStatement,
        ForStatement,
        IfStatement,
        UncheckedBlock,
        WhileStatement,
        FunctionDefinition,
        ModifierDefinition,
        TryCatchClause,
    ]:
        """
        Returns:
            Parent IR node.
        """
        return super().parent

    @property
    def children(self) -> Iterator[StatementAbc]:
        """
        Yields:
            Direct children of this node.
        """
        yield from self._statements

    @property
    def statements(self) -> Tuple[StatementAbc, ...]:
        """
        Can be empty.

        Returns:
            Statements in the block.
        """
        return tuple(self._statements)

    @property
    @weak_self_lru_cache(maxsize=2048)
    def modifies_state(
        self,
    ) -> Set[Tuple[Union[ExpressionAbc, StatementAbc, YulAbc], ModifiesStateFlag]]:
        if self.statements is None:
            return set()
        return reduce(
            or_,
            (statement.modifies_state for statement in self.statements),
            set(),
        )

    def statements_iter(self) -> Iterator[StatementAbc]:
        yield self
        if self._statements is not None:
            for statement in self._statements:
                yield from statement.statements_iter()

children: Iterator[StatementAbc] property #

Yields:

Type Description
StatementAbc

Direct children of this node.

parent: Union[Block, DoWhileStatement, ForStatement, IfStatement, UncheckedBlock, WhileStatement, FunctionDefinition, ModifierDefinition, TryCatchClause] property #

Returns:

Type Description
Union[Block, DoWhileStatement, ForStatement, IfStatement, UncheckedBlock, WhileStatement, FunctionDefinition, ModifierDefinition, TryCatchClause]

Parent IR node.

statements: Tuple[StatementAbc, ...] property #

Can be empty.

Returns:

Type Description
Tuple[StatementAbc, ...]

Statements in the block.