Skip to content

Mapping

woke.ast.ir.type_name.mapping module #

Mapping class #

Bases: TypeNameAbc

Mapping type name.

Example

A mapping type name can be used:

  • inside a VariableDeclaration:
    • mapping(address => uint) in line 1,
    • mapping(address => mapping(address => uint)) in line 8,
  • inside a UsingForDirective:
    • mapping(address => uint) in line 5,
  • inside an ArrayTypeName:
    • mapping(address => uint) in line 9,
  • inside a Mapping:
    • mapping(address => uint) in line 8.
function remove(mapping(address => uint) storage balances, address account) {
    delete balances[account];
}

using {remove} for mapping(address => uint);

contract C {
    mapping(address => mapping(address => uint)) public allowances;
    mapping(address => uint)[2] public balances;
}
Source code in woke/ast/ir/type_name/mapping.py
class Mapping(TypeNameAbc):
    """
    Mapping type name.

    !!! example
        A mapping type name can be used:

        - inside a [VariableDeclaration][woke.ast.ir.declaration.variable_declaration.VariableDeclaration]:
            - `:::solidity mapping(address => uint)` in line 1,
            - `:::solidity mapping(address => mapping(address => uint))` in line 8,
        - inside a [UsingForDirective][woke.ast.ir.meta.using_for_directive.UsingForDirective]:
            - `:::solidity mapping(address => uint)` in line 5,
        - inside an [ArrayTypeName][woke.ast.ir.type_name.array_type_name.ArrayTypeName]:
            - `:::solidity mapping(address => uint)` in line 9,
        - inside a [Mapping][woke.ast.ir.type_name.mapping.Mapping]:
            - `:::solidity mapping(address => uint)` in line 8.

        ```solidity linenums="1"
        function remove(mapping(address => uint) storage balances, address account) {
            delete balances[account];
        }

        using {remove} for mapping(address => uint);

        contract C {
            mapping(address => mapping(address => uint)) public allowances;
            mapping(address => uint)[2] public balances;
        }
        ```
    """

    _ast_node: SolcMapping
    _parent: Union[VariableDeclaration, UsingForDirective, ArrayTypeName, Mapping]

    _key_type: Union[ElementaryTypeName, UserDefinedTypeName]
    _value_type: TypeNameAbc

    def __init__(self, init: IrInitTuple, mapping: SolcMapping, parent: SolidityAbc):
        super().__init__(init, mapping, parent)
        key_type = TypeNameAbc.from_ast(init, mapping.key_type, self)
        assert isinstance(key_type, (ElementaryTypeName, UserDefinedTypeName))
        self._key_type = key_type
        self._value_type = TypeNameAbc.from_ast(init, mapping.value_type, self)

    def __iter__(self) -> Iterator[IrAbc]:
        yield self
        yield from self._key_type
        yield from self._value_type

    @property
    def parent(
        self,
    ) -> Union[VariableDeclaration, UsingForDirective, ArrayTypeName, Mapping]:
        """
        Returns:
            Parent IR node.
        """
        return self._parent

    @property
    def type(self) -> types.Mapping:
        """
        Returns:
            Type description.
        """
        t = super().type
        assert isinstance(t, types.Mapping)
        return t

    @property
    def key_type(self) -> Union[ElementaryTypeName, UserDefinedTypeName]:
        """
        Can only be:

        - an [ElementaryTypeName][woke.ast.ir.type_name.elementary_type_name.ElementaryTypeName],
        - a [UserDefinedTypeName][woke.ast.ir.type_name.user_defined_type_name.UserDefinedTypeName] of a [Contract][woke.ast.types.Contract] type,
        - a [UserDefinedTypeName][woke.ast.ir.type_name.user_defined_type_name.UserDefinedTypeName] of an [Enum][woke.ast.types.Enum] type,
        - a [UserDefinedTypeName][woke.ast.ir.type_name.user_defined_type_name.UserDefinedTypeName] of a [UserDefinedValueType][woke.ast.types.UserDefinedValueType] type.
        Returns:
            Mapping key type name.
        """
        return self._key_type

    @property
    def value_type(self) -> TypeNameAbc:
        """
        Returns:
            Mapping value type name.
        """
        return self._value_type

key_type: Union[ElementaryTypeName, UserDefinedTypeName] property #

Can only be:

Returns:

Type Description
Union[ElementaryTypeName, UserDefinedTypeName]

Mapping key type name.

parent: Union[VariableDeclaration, UsingForDirective, ArrayTypeName, Mapping] property #

Returns:

Type Description
Union[VariableDeclaration, UsingForDirective, ArrayTypeName, Mapping]

Parent IR node.

type: types.Mapping property #

Returns:

Type Description
types.Mapping

Type description.

value_type: TypeNameAbc property #

Returns:

Type Description
TypeNameAbc

Mapping value type name.