o
    Td                     @   s   d Z ddlmZmZ ddlmZ ddlmZmZ ddl	m
Z
 ddlmZmZmZ ddlmZ ddlmZ d	Zd
ZdZdd ZeeeddZeeeddZdd Zi Zdd Zeejdd Zeejdd Zeej dd Z!dS )z
Implement logic relating to wrapping (box) and unwrapping (unbox) instances
of jitclasses for use inside the python interpreter.
    )wrapspartial)ir)typescgutils)njit)boxunboxNativeValue)typeof_impl)_boxz;
def accessor(__numba_self_):
    return __numba_self_.{0}
zN
def mutator(__numba_self_, __numba_val):
    __numba_self_.{0} = __numba_val
zL
def method(__numba_self_, *args):
    return __numba_self_.{method}(*args)
c                 C   s$   | | }i }t|| t|| S )zG
    Generate simple function that get/set a field of the instance
    )formatexecr   )fieldtemplatefnamesourceglbls r   u/home/ncw/WWW/www-new/content/articles/pi-bbp/venv/lib/python3.10/site-packages/numba/experimental/jitclass/boxing.py_generate_property!   s   

r   accessor)r   r   mutatorc                    s>   t j| d}i }t|| t|d  t| fdd}|S )zo
    Generate a wrapper for calling a method.  Note the wrapper will only
    accept positional arguments.
    methodr   c                     s    | i |S Nr   )argskwargsr   r   r   wrapper;      z!_generate_method.<locals>.wrapper)_method_code_templater   r   r   r   )namefuncr   r   r   r   r   r   _generate_method1   s   
r#   c              
   C   s  | t v rt |  S d| | jjd}| jD ]}t|}t|}t||||< q| j D ]3\}}d}d}d|v r:t|}d|v rBt|}|	dpM|	dpMd}t
|dd}t|||d||< q*h d}| j D ]&\}	}
|	d	krpqg|	d
r|	d
r|	|vrtd|	 dt|	|
||	< qg| j D ]\}	}
t|	|
||	< qt| jtjf|}|t | < | D ]/\}}t|trt
||}|jdur|j}|| f}|  t||t||j|j|jd q|S )z
    Create a subclass of Box that is specialized to the jitclass.

    This function caches the result to avoid code bloat.
    r   )	__slots___numba_type___doc__Ngetsetr&   )doc>,   __eq____ge____gt____le____lt____ne____or____abs____add____and____int____ior____len____mod____mul____neg____pos____pow____str____sub____xor____bool____hash____iadd____iand____imod____imul____ipow____isub____ixor__	__float__	__index__
__lshift__
__rshift____complex____getitem____ilshift____irshift____setitem____truediv____contains____floordiv____itruediv____ifloordiv____init____zMethod 'z' is not supported.)_cache_specialized_box
class_type	class_docstruct_generate_getter_generate_setterproperty	jit_propsitemsr'   getattrmethods
startswithendswith	TypeErrorr#   static_methodstype	classnamer   Box
isinstancefgetcompiledisable_compilesetattrfsetfdelr&   )typdctr   gettersetterimpdctimpr)   supported_dundersr!   r"   subclskvproprk   	fast_fgetr   r   r   _specialize_boxE   sd   
.


r}   c                    s   t j|\}}t| }jjjt|dd}j|jj	}j
|d td j|}j|}	 fdd}
|
tj| |
tj|	  S )Nbox_class_instance)infor      c                    sD   j tj| }tj |}j| }j	|| d S r   )
contextget_constantr   uintpr   pointer_addbuilderbitcast
as_pointerstore)member_offsetvalueoffsetptrcastedr   c	llvoidptrr   r   
set_member   s   z'_box_class_instance.<locals>.set_member)r   unpack_tupler   r}   r   add_dynamic_addridr   pyapipyobjcall_function_objargsr   IntTyper   r   box_meminfoptr_offsetbox_dataptr_offset)rq   valr   meminfodataptrbox_subclassedvoidptr_boxclsbox_clsaddr_meminfo	addr_datar   r   r   r   _box_class_instance   s    r   c           	         s    fdd}t | }| j j}|tj}|tj} j||jj	|_ j||j
j	|_
| } jj j| | t| j dS )Nc                    sL    j tj| }td }t j	|} j	
|| } j	|S )Nr   )r   r   r   r   r   r   r   r   r   r   r   load)r   r   r   r   r   r   r   r   r   access_member   s
   z,_unbox_class_instance.<locals>.access_member)is_error)r   create_struct_proxyr   r   r   r   r   r   r   rg   data	_getvaluenrtincrefr
   r   c_api_error)	rq   r   r   r   
struct_clsinstptr_meminfoptr_dataptrretr   r   r   _unbox_class_instance   s   


r   c                 C   s   t t| dS )Nr%   )ra   rg   )r   r   r   r   r   _typeof_jitclass_box   r   r   N)"r&   	functoolsr   r   llvmliter   
numba.corer   r   numba.core.decoratorsr   numba.core.pythonapir   r	   r
   numba.core.typing.typeofr   numba.experimental.jitclassr   _getter_code_template_setter_code_templater    r   r\   r]   r#   rX   r}   ClassInstanceTyper   r   registerri   r   r   r   r   r   <module>   s6    
u
 

$