Skip to content

wake.ir.meta.modifier_invocation module #

ModifierInvocation class #

Bases: SolidityAbc

Warning

Also represents a base constructor invocation.

Example

in the following code:

import Initializable from "@openzeppelin/contracts/proxy/utils/Initializable.sol";

contract MyContract is ERC20, Initializable {
    constructor() ERC20("MyToken", "MTK") initializer {
        // ...
    }
}

Source code in wake/ir/meta/modifier_invocation.py
class ModifierInvocation(SolidityAbc):
    """
    !!! warning
        Also represents a base constructor invocation.
    !!! example
        - `:::solidity ERC20("MyToken", "MTK")` ([ModifierInvocationKind.BASE_CONSTRUCTOR_SPECIFIER][wake.ir.enums.ModifierInvocationKind.BASE_CONSTRUCTOR_SPECIFIER]),
        - `:::solidity initializer` ([ModifierInvocationKind.MODIFIER_INVOCATION][wake.ir.enums.ModifierInvocationKind.MODIFIER_INVOCATION])

        in the following code:
        ```solidity
        import Initializable from "@openzeppelin/contracts/proxy/utils/Initializable.sol";

        contract MyContract is ERC20, Initializable {
            constructor() ERC20("MyToken", "MTK") initializer {
                // ...
            }
        }
        ```
    """

    _ast_node: SolcModifierInvocation
    _parent: weakref.ReferenceType[FunctionDefinition]

    _kind: Optional[ModifierInvocationKind]
    _modifier_name: Union[Identifier, IdentifierPath]
    _arguments: Optional[List[ExpressionAbc]]

    def __init__(
        self,
        init: IrInitTuple,
        modifier_invocation: SolcModifierInvocation,
        parent: SolidityAbc,
    ):
        super().__init__(init, modifier_invocation, parent)
        self._kind = None
        if isinstance(modifier_invocation.modifier_name, SolcIdentifier):
            self._modifier_name = Identifier(
                init, modifier_invocation.modifier_name, self
            )
        elif isinstance(modifier_invocation.modifier_name, SolcIdentifierPath):
            self._modifier_name = IdentifierPath(
                init, modifier_invocation.modifier_name, self
            )

        if modifier_invocation.arguments is None:
            self._arguments = None
        else:
            self._arguments = [
                ExpressionAbc.from_ast(init, argument, self)
                for argument in modifier_invocation.arguments
            ]

        self._reference_resolver.register_post_process_callback(self._post_process)

    def _post_process(self, callback_params: CallbackParams):
        from ..declarations.contract_definition import ContractDefinition
        from ..declarations.modifier_definition import ModifierDefinition

        referenced_declaration = self.modifier_name.referenced_declaration
        if isinstance(referenced_declaration, ModifierDefinition):
            self._kind = ModifierInvocationKind.MODIFIER_INVOCATION
        elif isinstance(referenced_declaration, ContractDefinition):
            self._kind = ModifierInvocationKind.BASE_CONSTRUCTOR_SPECIFIER
        else:
            assert False, f"Unexpected declaration type: {referenced_declaration}"

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

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

    @property
    def children(self) -> Iterator[Union[Identifier, IdentifierPath, ExpressionAbc]]:
        """
        Yields:
            Direct children of this node.
        """
        yield self._modifier_name
        if self._arguments is not None:
            yield from self._arguments

    @property
    def kind(self) -> ModifierInvocationKind:
        """
        Returns:
            Kind of the modifier invocation.
        """
        assert self._kind is not None
        return self._kind

    @property
    def modifier_name(self) -> Union[Identifier, IdentifierPath]:
        """
        The returned IR node holds a reference to the [ModifierDefinition][wake.ir.declarations.modifier_definition.ModifierDefinition] declaration of the modifier being invoked in the case of the [ModifierInvocationKind.MODIFIER_INVOCATION][wake.ir.enums.ModifierInvocationKind.MODIFIER_INVOCATION] kind.
        In the case of the [ModifierInvocationKind.BASE_CONSTRUCTOR_SPECIFIER][wake.ir.enums.ModifierInvocationKind.BASE_CONSTRUCTOR_SPECIFIER] kind, the returned IR node holds a reference to the [ContractDefinition][wake.ir.declarations.contract_definition.ContractDefinition] whose constructor is being invoked.
        !!! example
            `ERC20` and `initializer` in the following code:
            ```solidity
            constructor() ERC20("MyToken", "MTK") initializer {
                // ...
            }
            ```

        Until Solidity 0.8.0, modifiers were referenced in [ModifierInvocations][wake.ir.meta.modifier_invocation.ModifierInvocation]
        using [Identifiers][wake.ir.expressions.identifier.Identifier]. Version 0.8.0 started using [IdentifierPaths][wake.ir.meta.identifier_path.IdentifierPath] instead.

        Returns:
            IR node representing the name of the modifier.
        """
        return self._modifier_name

    @property
    def arguments(self) -> Optional[Tuple[ExpressionAbc, ...]]:
        """
        !!! warning
            Is `None` when there are no round brackets after the modifier name.
            ```solidity
            constructor() initializer {}
            ```

            Is an empty list when there are round brackets but no arguments.
            ```solidity
            constructor() initializer() {}
            ```
        !!! example
            `:::solidity "MyToken"` and `:::solidity "MTK"` in the following code:
            ```solidity
            constructor() ERC20("MyToken", "MTK") initializer {
                // ...
            }
            ```

        Returns:
            Arguments of the base constructor or modifier invocation (if any).
        """
        if self._arguments is None:
            return None
        return tuple(self._arguments)

arguments: Optional[Tuple[ExpressionAbc, ...]] property #

Warning

Is None when there are no round brackets after the modifier name.

constructor() initializer {}

Is an empty list when there are round brackets but no arguments.

constructor() initializer() {}

Example

"MyToken" and "MTK" in the following code:

constructor() ERC20("MyToken", "MTK") initializer {
    // ...
}

Returns:

Type Description
Optional[Tuple[ExpressionAbc, ...]]

Arguments of the base constructor or modifier invocation (if any).

children: Iterator[Union[Identifier, IdentifierPath, ExpressionAbc]] property #

Yields:

Type Description
Union[Identifier, IdentifierPath, ExpressionAbc]

Direct children of this node.

kind: ModifierInvocationKind property #

Returns:

Type Description
ModifierInvocationKind

Kind of the modifier invocation.

modifier_name: Union[Identifier, IdentifierPath] property #

The returned IR node holds a reference to the ModifierDefinition declaration of the modifier being invoked in the case of the ModifierInvocationKind.MODIFIER_INVOCATION kind. In the case of the ModifierInvocationKind.BASE_CONSTRUCTOR_SPECIFIER kind, the returned IR node holds a reference to the ContractDefinition whose constructor is being invoked.

Example

ERC20 and initializer in the following code:

constructor() ERC20("MyToken", "MTK") initializer {
    // ...
}

Until Solidity 0.8.0, modifiers were referenced in ModifierInvocations using Identifiers. Version 0.8.0 started using IdentifierPaths instead.

Returns:

Type Description
Union[Identifier, IdentifierPath]

IR node representing the name of the modifier.

parent: FunctionDefinition property #

Returns:

Type Description
FunctionDefinition

Parent IR node.