
    i,                         S r SSKJr  SSKJrJrJr  SSKJrJ	r	  SSK
JrJrJrJrJrJrJrJr  SSKJr   " S S5      rS	 rS
 rS rS rS r\S 5       r " S S5      rg)zUtilities 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                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
_Utils   z;Internal builder-code utils for structref definitions.
    c                 (    Xl         X l        X0l        g)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       W/var/www/html/trading/venv/lib/python3.13/site-packages/numba/experimental/structref.py__init___Utils.__init__   s     &    c                     U R                   nU R                  nU R                  n[        R                  " U5      " X#5      nXl        U$ )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_ref_Utils.new_struct_ref(   s@     ,,,,&&((5gG
	r   c                     U R                   nU R                  nU R                  n[        R                  " U5      " X#US9$ )z6Return a helper for accessing a StructRefType
        )value)r   r   r   r   r   )r   valr   r   r   s        r   get_struct_ref_Utils.get_struct_ref3   s>     ,,,,&&**;7C
 	
r   c                 Z   U R                   nU R                  nU R                  nU R                  U5      nUR                  nUR
                  R                  X65      nUR                  5       nUR                  U   n	U	R                  5       n
UR                  XzR                  5       5      nU$ )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_pointer_Utils.get_data_pointer>   s     ,,,,&&'',	##;;++G=++-**73))+
??8-B-B-DEr   c                     U R                   nU R                  nU R                  nU R                  U5      nUR	                  5       n[
        R                  " U5      " X#US9nU$ )zFGet a getter/setter helper for accessing a `StructRefPayload`
        )ref)r   r   r   r7   r-   r   r   )r   r'   r   r   r   r3   r4   datavals           r   get_data_struct_Utils.get_data_structO   s`     ,,,,&&((-++---g6(
 r   )r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   r#   r(   r7   r<   __static_attributes__ r   r   r   r      s     '		
"r   r   c                    ^  [          " U 4S jS[        5      5       n[        T 5      S 5       n[        T 5      S 5       ng)zDefine attributes on `struct_typeclass`.

Defines both setters and getters in jit-code.

This is called directly in `register()`.
c                   "   > \ rS rSr Y rS rSrg)*define_attributes.<locals>.StructAttributee   c                 D    X!R                   ;   a  UR                   U   nU$ g N)
field_dict)r   typattrattrtys       r   generic_resolve:define_attributes.<locals>.StructAttribute.generic_resolvei   s$    ~~%- &r   rD   N)r>   r?   r@   rA   keyrO   rC   )struct_typeclasss   r   StructAttributerG   e   s    	r   rS   c                     [        XU5      nUR                  U5      n[        Xd5      nUR                  U   n[        R
                  " XX5      $ rJ   )r   r<   getattrrK   r   impl_ret_borrowed)	r   r   rL   r'   rM   utilsr;   ret	fieldtypes	            r   struct_getattr_impl.define_attributes.<locals>.struct_getattr_impln   sI    w-'',g$NN4(	))'IKKr   c                 B   UR                   u  pVUu  px[        XU5      n	U	R                  U5      n
UR                  U   nU R	                  XXk5      n[        X5      nU R                  R                  XU5        U R                  R                  XU5        [        XU5        g rJ   )
argsr   r<   rK   castrU   r+   increfdecrefsetattr)r   r   sigr]   rM   	inst_typeval_typeinstancer'   rW   r;   
field_typecasted	old_values                 r   struct_setattr_impl.define_attributes.<locals>.struct_setattr_implv   s     #w3''1))$/
gHAG*	7f57i8v&r   N)r	   r   r
   r   )rR   rS   rZ   ri   s   `   r   define_attributesrk   ^   sU     +   +,L -L +,' -'r   c                    ^ U [         R                  L a  [        S[         R                   35      eUR                  m[	        U 5      U4S j5       n[        U 5      S 5       ng)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                   > [        UR                  UR                  U 5      nUR                  U5      nUR                  n[
        R                  " [
        R                  5      nUR                  Xe5      nUR                  R                  UR                  R                  T5      5      nUR                  R                  UR                  R                  U 5      5      n	UR                  R                  XU/5      n
UR                  R                  U5        UR                  R                  U	5        UR                  R                  U5        U
$ )z(
Convert a raw pointer to a Python int.
)r   r   r   r(   r    r   MemInfoPointervoidptrr   pyapiunserializeserialize_objectcall_function_objargsr`   )rL   r'   crW   
struct_refr    mip_typeboxed_meminfoctor_pyfuncty_pyobjresobj_ctors              r   box_struct_ref%define_boxing.<locals>.box_struct_ref   s    
 qyy!))S1))#.
$$''6h0gg))!''*B*B8*LM77&&qww'?'?'DEgg++M2
 	
{#	x 	}%
r   c                    UR                   R                  US5      n[        R                  " [        R                  5      nUR                  XC5      R                  n[        UR                  UR                  U 5      nUR                  U5      nUR                  5       nUR                   R                  U5        [        U5      $ )N_meminfo)rq   object_getattr_stringr   ro   rp   r   r&   r   r   r   r#   	_getvaluer`   r   )	rL   objru   mi_objrw   r!   rW   rv   outs	            r   unbox_struct_ref'define_boxing.<locals>.unbox_struct_ref   s    ..sJ?''6WWX&,,qyy!))S1))"-
""$	v3r   N)r   	StructRef
ValueError_numba_box_r   r   )r   	obj_classr}   r   r|   s       @r   define_boxingr      sd     eoo%+EOO+<=>>$$H , ;   r   c                 *   SR                  U5      nSn/ nU H  nUR                  SU SU 35        M     SU 3R                  U5      nSU S[        U5       SU S	U S
U S3n[        U[        S9n	[        X5        U	S   n
[        U 5      " U
5        g)zDefine 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
)rR   newctorN)joinappendlistdictr   execr   )py_classrR   fieldsparamsindentinit_fields_bufkinit_fieldssourceglbsr   s              r   define_constructorr      s     YYvFFOQCs1#/ vh-$$_5K

 --1&\N#fX FX 		 F !1s;D<DXtr   c                 2    [        XU5        [        X5        g)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   rR   r   s      r   define_proxyr      s    & x6:"-r   c                     U [         R                  L a  [        S[         R                   35      e[        R                  " U [
        R                  5        [        U 5        U $ )aP  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)

rm   )r   r   r   r   registerr   StructRefModelrk   )r   s    r   r   r      sL    8 eoo%+EOO+<=>>[&*?*?@k"r   c                 N   ^^ SSK Jm  UR                  mUU4S jnT" U5      nX24$ )znew(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           	      (  > U R                   TR                  5          nUR                  5       nU R                  U5      nU R                  R                  UU R                  [        R                  U5      T
" XR                  T5      5      nU R                  R                  X5      nUR                  XR                  5       5      nUR                  [        R                  " U5      U5        U R!                  UT5      n	Xyl        U	R%                  5       $ rJ   )r.   r-   r/   get_abi_sizeofr+   meminfo_alloc_dtorget_constantr   uintpmoduler,   r0   r1   storer   get_null_valuemake_helperr    r   )r   r   	signaturer]   r5   r6   
alloc_sizer    data_pointerinst_structr   rc   s             r   codegennew.<locals>.codegen-  s    **9+B+B+DE))+
++J7
++00  j9Wnni8

 {{//A|5J5J5LM 	g,,Z8,G))'9=%$$&&r   ) numba.experimental.jitclass.baser   instance_type)	typingctxr   r   rb   r   rc   s       @@r   r   r     s,     :))I', K
 C<r   c                   P   ^  \ rS rSrSrSr\U 4S j5       rS r\	S 5       r
SrU =r$ )StructRefProxyiG  zA PyObject proxy to the Numba allocated structref data structure.

Notes
-----

* Subclasses should not define ``__init__``.
* Subclasses can override ``__new__``.
)_typer   c                 >   > [         TU ]  U 5      nXl        X#l        U$ )zCalled 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   r   )clstyr!   re   	__class__s       r   r   StructRefProxy._numba_box_R  s#    " 7?3'r   c                 t   ^   T R                   nU" U6 $ ! [         a    [        U 4S j5       nUT l          N'f = f)zConstruct 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                     > T" U 6 $ rJ   rD   )r]   r   s    r   r   $StructRefProxy.__new__.<locals>.ctors  s    Dz!r   )_StructRefProxy__numba_ctorAttributeErrorr   )r   r]   r   s   `  r   r   StructRefProxy.__new__h  sL    		$##D T{  	$" "  $C	$s     77c                     U R                   $ )z^Returns the Numba type instance for this structref instance.

Subclasses should NOT override.
)r   )r   s    r   _numba_type_StructRefProxy._numba_type_z  s     zzr   rD   )r>   r?   r@   rA   rB   	__slots__classmethodr   r   propertyr   rC   __classcell__)r   s   @r   r   r   G  s:     &I *$  r   r   N)rB   numbar   
numba.corer   r   r   numba.core.datamodelr   r   numba.core.extendingr	   r
   r   r   r   r   r   r   numba.core.typing.templatesr   r   rk   r   r   r   r   r   r   rD   r   r   <module>r      su     / / 8	 	 	 :D DN('V6 r@.. F ' 'T9 9r   