o
    Ù°“iƒ  ã                   @  s¬   d Z ddlmZ ddlmZ er"ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ G d
d„ deƒZedƒG dd„ deƒƒZedƒG dd„ deƒƒZdS )zAccess path classes.é    )Úannotations)ÚTYPE_CHECKING)ÚSequence)ÚObject)ÚAny)ÚIntEnumé   )Úregister_objectc                   @  s(   e Zd ZdZdZdZdZdZdZdZ	dS )	Ú
AccessKindz(Kinds of access steps in an access path.r   r   é   é   é   é   N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚATTRÚ
ARRAY_ITEMÚMAP_ITEMÚATTR_MISSINGÚARRAY_ITEM_MISSINGÚMAP_ITEM_MISSING© r   r   úG/home/ubuntu/.local/lib/python3.10/site-packages/tvm_ffi/access_path.pyr
   &   s    r
   zffi.reflection.AccessStepc                   @  s"   e Zd ZU dZded< ded< dS )Ú
AccessStepzAccess step container.ÚintÚkindr   ÚkeyN)r   r   r   r   Ú__annotations__r   r   r   r   r   1   s   
 r   zffi.reflection.AccessPathc                      s*  e Zd ZU dZded< ded< ded< erNedBd	d
„ƒZdCdd„ZdDdd„Z	dEdd„Z
dFdd„ZdDdd„ZdEdd„ZdFdd„ZdGdd„ZdHd!d"„ZdGd#d$„ZdI‡ fd&d'„ZedBd(d)„ƒZdJd+d,„ZdJd-d.„ZdKd/d0„ZdLd2d3„ZdLd4d5„ZdMd7d8„ZdMd9d:„ZdNd<d=„ZdNd>d?„ZdHd@dA„ZejZ‡  Z S )OÚ
AccessPatha6  Access path container.

    It describes how to reach a nested attribute or item
    inside a complex FFI object by recording a sequence of steps
    (attribute, array index, or map key). It is primarily used by
    diagnostics to pinpoint structural mismatches.

    Examples
    --------
    .. code-block:: python

        from tvm_ffi.access_path import AccessPath

        root = AccessPath.root()
        # Build a path equivalent to obj.layer.weight[2]
        p = root.attr("layer").attr("weight").array_item(2)
        assert isinstance(p, AccessPath)

    zObject | NoneÚparentzAccessStep | NoneÚstepr   ÚdepthÚreturnc                   C  ó   d S ©Nr   r   r   r   r   Ú_root[   s   zAccessPath._rootÚ_1r   c                C  r%   r&   r   ©Úselfr(   r   r   r   Ú_extend]   ó    zAccessPath._extendÚstrc                C  r%   r&   r   r)   r   r   r   Ú_attr^   r,   zAccessPath._attrc                C  r%   r&   r   r)   r   r   r   Ú_array_item_   r,   zAccessPath._array_itemr   c                C  r%   r&   r   r)   r   r   r   Ú	_map_item`   r,   zAccessPath._map_itemc                C  r%   r&   r   r)   r   r   r   Ú_attr_missinga   r,   zAccessPath._attr_missingc                C  r%   r&   r   r)   r   r   r   Ú_array_item_missingb   r,   zAccessPath._array_item_missingc                C  r%   r&   r   r)   r   r   r   Ú_map_item_missingc   r,   zAccessPath._map_item_missingÚboolc                C  r%   r&   r   r)   r   r   r   Ú_is_prefix_ofd   r,   zAccessPath._is_prefix_ofúSequence[AccessStep]c                C  r%   r&   r   ©r*   r   r   r   Ú	_to_stepse   r,   zAccessPath._to_stepsc                C  r%   r&   r   r)   r   r   r   Ú_path_equalf   r,   zAccessPath._path_equalÚNonec                   s   t ƒ  ¡  tdƒ‚)z>Disallow direct construction; use `AccessPath.root()` instead.zcAccessPath can't be initialized directly. Use AccessPath.root() to create a path to the root object)ÚsuperÚ__init__Ú
ValueErrorr7   ©Ú	__class__r   r   r<   j   s   
ÿzAccessPath.__init__c                   C  s   t  ¡ S )z•Create a root access path.

        Returns
        -------
        AccessPath
            A path representing the root of an object graph.

        )r    r'   r   r   r   r   Úrootr   s   
zAccessPath.rootÚotherc                 C  s   t |tƒsdS |  |¡S )z*Return whether two access paths are equal.F©Ú
isinstancer    r9   ©r*   rA   r   r   r   Ú__eq__~   s   

zAccessPath.__eq__c                 C  s   t |tƒsdS |  |¡ S )z.Return whether two access paths are not equal.TrB   rD   r   r   r   Ú__ne__„   s   
zAccessPath.__ne__c                 C  ó
   |   |¡S )aP  Check if this access path is a prefix of another access path.

        Parameters
        ----------
        other
            The access path to check if it is a prefix of this access path

        Returns
        -------
        bool
            True if this access path is a prefix of the other access path, False otherwise

        )r5   rD   r   r   r   Úis_prefix_ofŠ   ó   
zAccessPath.is_prefix_ofÚattr_keyc                 C  rG   )a  Create an access path to the attribute of the current object.

        Parameters
        ----------
        attr_key
            The key of the attribute to access

        Returns
        -------
        AccessPath
            The extended access path

        )r.   ©r*   rJ   r   r   r   Úattrš   rI   zAccessPath.attrc                 C  rG   )a  Create an access path that indicate an attribute is missing.

        Parameters
        ----------
        attr_key
            The key of the attribute to access

        Returns
        -------
        AccessPath
            The extended access path

        )r1   rK   r   r   r   Úattr_missingª   rI   zAccessPath.attr_missingÚindexc                 C  rG   )zûCreate an access path to the item of the current array.

        Parameters
        ----------
        index
            The index of the item to access

        Returns
        -------
        AccessPath
            The extended access path

        )r/   ©r*   rN   r   r   r   Ú
array_itemº   rI   zAccessPath.array_itemc                 C  rG   )a  Create an access path that indicate an array item is missing.

        Parameters
        ----------
        index
            The index of the item to access

        Returns
        -------
        AccessPath
            The extended access path

        )r2   rO   r   r   r   Úarray_item_missingÊ   rI   zAccessPath.array_item_missingr   c                 C  rG   )zõCreate an access path to the item of the current map.

        Parameters
        ----------
        key
            The key of the item to access

        Returns
        -------
        AccessPath
            The extended access path

        )r0   ©r*   r   r   r   r   Úmap_itemÚ   rI   zAccessPath.map_itemc                 C  rG   )zúCreate an access path that indicate a map item is missing.

        Parameters
        ----------
        key
            The key of the item to access

        Returns
        -------
        AccessPath
            The extended access path

        )r3   rR   r   r   r   Úmap_item_missingê   rI   zAccessPath.map_item_missingc                 C  s   |   ¡ S )z—Convert the access path to a list of access steps.

        Returns
        -------
        access_steps
            The list of access steps

        )r8   r7   r   r   r   Úto_stepsú   s   	zAccessPath.to_steps)r$   r    )r(   r   r$   r    )r(   r-   r$   r    )r(   r   r$   r    )r(   r   r$   r    )r(   r    r$   r4   )r$   r6   )r$   r:   )rA   r   r$   r4   )rA   r    r$   r4   )rJ   r-   r$   r    )rN   r   r$   r    )r   r   r$   r    )!r   r   r   r   r   r   Ústaticmethodr'   r+   r.   r/   r0   r1   r2   r3   r5   r8   r9   r<   r@   rE   rF   rH   rL   rM   rP   rQ   rS   rT   rU   r   Ú__hash__Ú__classcell__r   r   r>   r   r    >   s@   
 



















r    N)r   Ú
__future__r   Útypingr   Úcollections.abcr   Útvm_ffir   r   Úenumr   ÚcoreÚregistryr	   r
   r   r    r   r   r   r   Ú<module>   s   