§
    ƒÛgã,  ã                   óÖ   — d Z ddlmZ ddlmZmZmZ ddlmZm	Z	 ddl
mZmZmZmZmZmZmZmZ ddlmZ  G d„ d¦  «        Zd	„ Zd
„ Zd„ Zd„ Zd„ Zed„ ¦   «         Z G d„ d¦  «        ZdS )z€Utilities for defining a mutable struct.

A mutable struct is passed by reference;
hence, structref (a reference to a struct).

é    )Únjit)ÚtypesÚimputilsÚcgutils)Údefault_managerÚmodels)Úinfer_getattrÚlower_getattr_genericÚlower_setattr_genericÚboxÚunboxÚNativeValueÚ	intrinsicÚoverload)ÚAttributeTemplatec                   ó0   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ ZdS )Ú_Utilsz;Internal builder-code utils for structref definitions.
    c                 ó0   — || _         || _        || _        dS )zÈ
        Parameters
        ----------
        context :
            a numba target context
        builder :
            a llvmlite IRBuilder
        struct_type : numba.core.types.StructRef
        N)ÚcontextÚbuilderÚstruct_type)Úselfr   r   r   s       úR/granary/edev/quantum/lib/python3.11/site-packages/numba/experimental/structref.pyÚ__init__z_Utils.__init__   s    € ð ˆŒØˆŒØ&ˆÔÐÐó    c                 óz   — | j         }| j        }| j        } t          j        |¦  «        ||¦  «        }||_        |S )zKEncapsulate the MemInfo from a `StructRefPayload` in a `StructRef`
        )r   r   r   r   Úcreate_struct_proxyÚmeminfo)r   Úmir   r   r   Ústs         r   Únew_struct_refz_Utils.new_struct_ref(   sB   € ð ”,ˆØ”,ˆØÔ&ˆà5�WÔ(¨Ñ5Ô5°g¸wÑGÔGˆØˆŒ
Øˆ	r   c                 ól   — | j         }| j        }| j        } t          j        |¦  «        |||¬¦  «        S )z6Return a helper for accessing a StructRefType
        )Úvalue)r   r   r   r   r   )r   Úvalr   r   r   s        r   Úget_struct_refz_Utils.get_struct_ref3   sE   € ð ”,ˆØ”,ˆØÔ&ˆà7�wÔ*¨;Ñ7Ô7Ø�W Cð
ñ 
ô 
ð 	
r   c                 óX  — | j         }| j        }| j        }|                      |¦  «        }|j        }|j                             ||¦  «        }|                     ¦   «         }|j        |         }	|	 	                    ¦   «         }
| 
                    ||
                     ¦   «         ¦  «        }|S )zDGet the data pointer to the payload from a `StructRefType`.
        )r   r   r   r%   r   ÚnrtÚmeminfo_dataÚget_data_typeÚdata_model_managerÚget_value_typeÚbitcastÚ
as_pointer)r   r$   r   r   r   Ú	structvalr   Údata_ptrÚvaltypeÚmodelÚ
alloc_types              r   Úget_data_pointerz_Utils.get_data_pointer>   sŸ   € ð ”,ˆØ”,ˆØÔ&ˆà×'Ò'¨Ñ,Ô,ˆ	ØÔ#ˆØ”;×+Ò+¨G°WÑ=Ô=ˆà×+Ò+Ñ-Ô-ˆØÔ*¨7Ô3ˆØ×)Ò)Ñ+Ô+ˆ
Ø—?’? 8¨Z×-BÒ-BÑ-DÔ-DÑEÔEˆØˆr   c                 óÂ   — | j         }| j        }| j        }|                      |¦  «        }|                     ¦   «         } t          j        |¦  «        |||¬¦  «        }|S )zFGet a getter/setter helper for accessing a `StructRefPayload`
        )Úref)r   r   r   r3   r)   r   r   )r   r$   r   r   r   r/   r0   Údatavals           r   Úget_data_structz_Utils.get_data_structO   sm   € ð ”,ˆØ”,ˆØÔ&ˆà×(Ò(¨Ñ-Ô-ˆØ×+Ò+Ñ-Ô-ˆØ6•'Ô-¨gÑ6Ô6Ø�W (ð
ñ 
ô 
ˆð ˆr   N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r%   r3   r7   © r   r   r   r      si   € € € € € ðð ð'ð 'ð 'ð	ð 	ð 	ð	
ð 	
ð 	
ðð ð ð"ð ð ð ð r   r   c                 ó¬   ‡ — t            G ˆ fd„dt          ¦  «        ¦   «         }t          ‰ ¦  «        d„ ¦   «         }t          ‰ ¦  «        d„ ¦   «         }dS )zŽDefine attributes on `struct_typeclass`.

    Defines both setters and getters in jit-code.

    This is called directly in `register()`.
    c                   ó   •— e Zd Z” Zd„ ZdS )ú*define_attributes.<locals>.StructAttributec                 ó6   — ||j         v r|j         |         }|S d S ©N)Ú
field_dict)r   ÚtypÚattrÚattrtys       r   Úgeneric_resolvez:define_attributes.<locals>.StructAttribute.generic_resolvei   s(   € Ø�s”~Ð%Ð%Øœ¨Ô-�Ø�ð &Ð%r   N)r8   r9   r:   ÚkeyrF   )Ústruct_typeclasss   €r   ÚStructAttributer?   e   s)   ø€ € € € € àˆð	ð 	ð 	ð 	ð 	r   rI   c                 ó¶   — t          | ||¦  «        }|                     |¦  «        }t          ||¦  «        }|j        |         }t	          j        | |||¦  «        S rA   )r   r7   ÚgetattrrB   r   Úimpl_ret_borrowed)	r   r   rC   r$   rD   Úutilsr6   ÚretÚ	fieldtypes	            r   Ústruct_getattr_implz.define_attributes.<locals>.struct_getattr_impln   sX   € å�w ¨Ñ-Ô-ˆØ×'Ò'¨Ñ,Ô,ˆÝ�g˜tÑ$Ô$ˆØ”N 4Ô(ˆ	ÝÔ)¨'°7¸IÀsÑKÔKÐKr   c                 ól  — |j         \  }}|\  }}t          | ||¦  «        }	|	                     |¦  «        }
|j        |         }|                      ||||¦  «        }t          |
|¦  «        }| j                             |||¦  «         | j                             |||¦  «         t          |
||¦  «         d S rA   )
Úargsr   r7   rB   ÚcastrK   r'   ÚincrefÚdecrefÚsetattr)r   r   ÚsigrR   rD   Ú	inst_typeÚval_typeÚinstancer$   rM   r6   Ú
field_typeÚcastedÚ	old_values                 r   Ústruct_setattr_implz.define_attributes.<locals>.struct_setattr_implv   sº   € à #¤Ñˆ�HØ‰ˆ�3Ý�w ¨Ñ3Ô3ˆØ×'Ò'¨Ñ1Ô1ˆàÔ)¨$Ô/ˆ
Ø—’˜g s¨H°jÑAÔAˆå˜G TÑ*Ô*ˆ	àŒ×Ò˜7 H¨fÑ5Ô5Ð5àŒ×Ò˜7 H¨iÑ8Ô8Ð8å�˜˜vÑ&Ô&Ð&Ð&Ð&r   N)r	   r   r
   r   )rH   rI   rP   r^   s   `   r   Údefine_attributesr_   ^   s›   ø€ õ ðð ð ð ð ð ð Õ+ñ ô ñ „]ðõ Ð+Ñ,Ô,ðLð Lñ -Ô,ðLõ Ð+Ñ,Ô,ð'ð 'ñ -Ô,ð'ð 'ð 'r   c                 óÎ   ‡— | t           j        u rt          dt           j        › �¦  «        ‚|j        Št	          | ¦  «        ˆfd„¦   «         }t          | ¦  «        d„ ¦   «         }dS )a˜  Define the boxing & unboxing logic for `struct_type` to `obj_class`.

    Defines both boxing and unboxing.

    - boxing turns an instance of `struct_type` into a PyObject of `obj_class`
    - unboxing turns an instance of `obj_class` into an instance of
      `struct_type` in jit-code.


    Use this directly instead of `define_proxy()` when the user does not
    want any constructor to be defined.
    úcannot register c                 ó|  •— t          |j        |j        | ¦  «        }|                     |¦  «        }|j        }t          j        t
          j        ¦  «        }|                     ||¦  «        }|j	         
                    |j	                             ‰¦  «        ¦  «        }|j	         
                    |j	                             | ¦  «        ¦  «        }	|j	                             ||	|g¦  «        }
|j	                             |¦  «         |j	                             |	¦  «         |j	                             |¦  «         |
S )z8
        Convert a raw pointer to a Python int.
        )r   r   r   r%   r   r   ÚMemInfoPointerÚvoidptrr   ÚpyapiÚunserializeÚserialize_objectÚcall_function_objargsrU   )rC   r$   ÚcrM   Ú
struct_refr   Úmip_typeÚboxed_meminfoÚctor_pyfuncÚty_pyobjÚresÚobj_ctors              €r   Úbox_struct_refz%define_boxing.<locals>.box_struct_ref›   s  ø€ õ
 �q”y !¤)¨SÑ1Ô1ˆØ×)Ò)¨#Ñ.Ô.ˆ
ØÔ$ˆåÔ'­¬Ñ6Ô6ˆØŸš˜h¨Ñ0Ô0ˆà”g×)Ò)¨!¬'×*BÒ*BÀ8Ñ*LÔ*LÑMÔMˆØ”7×&Ò& q¤w×'?Ò'?ÀÑ'DÔ'DÑEÔEˆàŒg×+Ò+Ø˜( MÐ2ñ
ô 
ˆð 	
Œ�Š�{Ñ#Ô#Ð#Ø	Œ�Š�xÑ Ô Ð Ø	Œ�Š�}Ñ%Ô%Ð%Øˆ
r   c                 ó„  — |j                              |d¦  «        }t          j        t          j        ¦  «        }|                     ||¦  «        j        }t          |j        |j	        | ¦  «        }| 
                    |¦  «        }|                     ¦   «         }|j                              |¦  «         t          |¦  «        S )NÚ_meminfo)re   Úobject_getattr_stringr   rc   rd   r   r#   r   r   r   r!   Ú	_getvaluerU   r   )	rC   Úobjri   Úmi_objrk   r   rM   rj   Úouts	            r   Úunbox_struct_refz'define_boxing.<locals>.unbox_struct_ref²   sœ   € à”×.Ò.¨s°JÑ?Ô?ˆåÔ'­¬Ñ6Ô6ˆà�WŠW�X˜vÑ&Ô&Ô,ˆå�q”y !¤)¨SÑ1Ô1ˆØ×)Ò)¨"Ñ-Ô-ˆ
Ø×"Ò"Ñ$Ô$ˆà	Œ�Š�vÑÔÐÝ˜3ÑÔÐr   N)r   Ú	StructRefÚ
ValueErrorÚ_numba_box_r   r   )r   Ú	obj_classrq   ry   rp   s       @r   Údefine_boxingr~   ‰   s�   ø€ ð •e”oÐ%Ð%ÝÐ=­E¬OÐ=Ð=Ñ>Ô>Ð>àÔ$€HåˆÑÔðð ð ð ñ Ôðõ, ˆ;ÑÔð ð  ñ Ôð ð  ð  r   c                 ór  — d                      |¦  «        }d}g }|D ]}|                     d|› d|› �¦  «         Œd|› �                      |¦  «        }d|› dt          |¦  «        › d|› d	|› d
|› d�}t          |t          ¬¦  «        }	t          ||	¦  «         |	d         }
 t          | ¦  «        |
¦  «         dS )zÙDefine the jit-code constructor for `struct_typeclass` using the
    Python type `py_class` and the required `fields`.

    Use this instead of `define_proxy()` if the user does not want boxing
    logic defined.
    z, z        zst.z = ú
z

def ctor(z/):
    struct_type = struct_typeclass(list(zip(z, [z])))
    def impl(z)):
        st = new(struct_type)
        z#
        return st
    return impl
)rH   ÚnewÚctorN)ÚjoinÚappendÚlistÚdictr�   Úexecr   )Úpy_classrH   ÚfieldsÚparamsÚindentÚinit_fields_bufÚkÚinit_fieldsÚsourceÚglbsr‚   s              r   Údefine_constructorr‘   Â   s  € ð �YŠY�vÑÔ€FØ€FØ€OØð 0ð 0ˆØ×Ò˜ Q˜˜¨1˜˜Ñ/Ô/Ð/Ð/Ø�v�-�-×$Ò$ _Ñ5Ô5€KðØ
ðð å-1°&©\¬\ðð à>Dðð ð ðð ð
 
ðð ð €Fõ Ð!1µsÐ;Ñ;Ô;€DÝˆ�ÑÔÐØ�Œ<€Dà…HˆXÑÔ�tÑÔÐÐÐr   c                 óH   — t          | ||¦  «         t          || ¦  «         dS )aû  Defines a PyObject proxy for a structref.

    This makes `py_class` a valid constructor for creating a instance of
    `struct_typeclass` that contains the members as defined by `fields`.

    Parameters
    ----------
    py_class : type
        The Python class for constructing an instance of `struct_typeclass`.
    struct_typeclass : numba.core.types.Type
        The structref type class to bind to.
    fields : Sequence[str]
        A sequence of field names.

    Returns
    -------
    None
    N)r‘   r~   )rˆ   rH   r‰   s      r   Údefine_proxyr“   â   s.   € õ& �xÐ!1°6Ñ:Ô:Ð:ÝÐ" HÑ-Ô-Ð-Ð-Ð-r   c                 ó¶   — | t           j        u rt          dt           j        › �¦  «        ‚t          j        | t
          j        ¦  «         t          | ¦  «         | S )a˜  Register a `numba.core.types.StructRef` for use in jit-code.

    This defines the data-model for lowering an instance of `struct_type`.
    This defines attributes accessor and mutator for an instance of
    `struct_type`.

    Parameters
    ----------
    struct_type : type
        A subclass of `numba.core.types.StructRef`.

    Returns
    -------
    struct_type : type
        Returns the input argument so this can act like a decorator.

    Examples
    --------

    .. code-block::

        class MyStruct(numba.core.types.StructRef):
            ...  # the simplest subclass can be empty

        numba.experimental.structref.register(MyStruct)

    ra   )r   rz   r{   r   Úregisterr   ÚStructRefModelr_   )r   s    r   r•   r•   ù   sT   € ð8 •e”oÐ%Ð%ÝÐ=­E¬OÐ=Ð=Ñ>Ô>Ð>ÝÔ˜[­&Ô*?Ñ@Ô@Ð@Ý�kÑ"Ô"Ð"ØÐr   c                 óJ   ‡‡— ddl mŠ |j        Šˆˆfd„} ‰|¦  «        }||fS )a  new(struct_type)

    A jit-code only intrinsic. Used to allocate an **empty** mutable struct.
    The fields are zero-initialized and must be set manually after calling
    the function.

    Example:

        instance = new(MyStruct)
        instance.field = field_value
    r   )Úimp_dtorc           	      ó`  •— | j         ‰                     ¦   «                  }|                     ¦   «         }|                      |¦  «        }| j                             ||                      t          j        |¦  «         ‰
| |j	        ‰¦  «        ¦  «        }| j         
                    ||¦  «        }|                     ||                     ¦   «         ¦  «        }|                     t          j        |¦  «        |¦  «         |                      |‰¦  «        }	||	_        |	                     ¦   «         S rA   )r*   r)   r+   Úget_abi_sizeofr'   Úmeminfo_alloc_dtorÚget_constantr   ÚuintpÚmoduler(   r,   r-   Ústorer   Úget_null_valueÚmake_helperr   ru   )r   r   Ú	signaturerR   r1   r2   Ú
alloc_sizer   Údata_pointerÚinst_structr˜   rX   s             €€r   Úcodegenznew.<locals>.codegen-  s
  ø€ àÔ*¨9×+BÒ+BÑ+DÔ+DÔEˆØ×)Ò)Ñ+Ô+ˆ
Ø×+Ò+¨JÑ7Ô7ˆ
à”+×0Ò0ØØ× Ò ¥¤¨jÑ9Ô9ØˆH�W˜gœn¨iÑ8Ô8ñ
ô 
ˆð
 ”{×/Ò/°¸ÑAÔAˆØ—’ |°Z×5JÒ5JÑ5LÔ5LÑMÔMˆð 	�Š•gÔ,¨ZÑ8Ô8¸,ÑGÔGÐGà×)Ò)¨'°9Ñ=Ô=ˆØ%ˆÔà×$Ò$Ñ&Ô&Ð&r   )Ú numba.experimental.jitclass.baser˜   Úinstance_type)Ú	typingctxr   r¦   rW   r˜   rX   s       @@r   r�   r�     sY   øø€ ð :Ð9Ð9Ð9Ð9Ð9àÔ)€Ið'ð 'ð 'ð 'ð 'ð 'ð, ˆ)�KÑ
 Ô
 €CØ�ˆ<Ðr   c                   óR   ‡ — e Zd ZdZdZeˆ fd„¦   «         Zd„ Zed„ ¦   «         Z	ˆ xZ
S )ÚStructRefProxyz¸A PyObject proxy to the Numba allocated structref data structure.

    Notes
    -----

    * Subclasses should not define ``__init__``.
    * Subclasses can override ``__new__``.
    )Ú_typers   c                 óf   •— t          ¦   «                              | ¦  «        }||_        ||_        |S )aO  Called by boxing logic, the conversion of Numba internal
        representation into a PyObject.

        Parameters
        ----------
        ty :
            a Numba type instance.
        mi :
            a wrapped MemInfoPointer.

        Returns
        -------
        instance :
             a StructRefProxy instance.
        )ÚsuperÚ__new__r¬   rs   )ÚclsÚtyr   rZ   Ú	__class__s       €r   r|   zStructRefProxy._numba_box_R  s.   ø€ õ" ‘7”7—?’? 3Ñ'Ô'ˆØˆŒØˆÔØˆr   c                 ór   ‡ — 	 ‰ j         }n)# t          $ r t          ˆ fd„¦   «         }|‰ _         Y nw xY w ||Ž S )zÄConstruct a new instance of the structref.

        This takes positional-arguments only due to limitation of the compiler.
        The arguments are mapped to ``cls(*args)`` in jit-code.
        c                  ó   •—  ‰| Ž S rA   r<   )rR   r°   s    €r   r‚   z$StructRefProxy.__new__.<locals>.ctors  s   ø€ à�s˜D�zÐ!r   )Ú_StructRefProxy__numba_ctorÚAttributeErrorr   )r°   rR   r‚   s   `  r   r¯   zStructRefProxy.__new__h  sn   ø€ ð		$àÔ#ˆDˆDøÝð 	$ð 	$ð 	$åð"ð "ð "ð "ñ ŒTð"ð  $ˆCÔÐÐð	$øøøð ˆt�Tˆ{Ðs   ƒ ‹#1°1c                 ó   — | j         S )znReturns the Numba type instance for this structref instance.

        Subclasses should NOT override.
        )r¬   )r   s    r   Ú_numba_type_zStructRefProxy._numba_type_z  s   € ð ŒzÐr   )r8   r9   r:   r;   Ú	__slots__Úclassmethodr|   r¯   Úpropertyr¸   Ú__classcell__)r²   s   @r   r«   r«   G  s}   ø€ € € € € ðð ð &€Iàðð ð ð ñ „[ðð*ð ð ð$ ðð ñ „Xðð ð ð ð r   r«   N)r;   Únumbar   Ú
numba.corer   r   r   Únumba.core.datamodelr   r   Únumba.core.extendingr	   r
   r   r   r   r   r   r   Únumba.core.typing.templatesr   r   r_   r~   r‘   r“   r•   r�   r«   r<   r   r   ú<module>rÂ      sŸ  ððð ð Ð Ð Ð Ð Ð Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð :Ð 9Ð 9Ð 9Ð 9Ð 9ðDð Dð Dð Dð Dñ Dô Dð DðN('ð ('ð ('ðV6 ð 6 ð 6 ðrð ð ð@.ð .ð .ð. ð  ð  ðF ð'ð 'ñ „ð'ðT9ð 9ð 9ð 9ð 9ñ 9ô 9ð 9ð 9ð 9r   