a
    
d                     @   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   k/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/numba/experimental/jitclass/boxing.py_generate_property!   s    

r   accessor)r   r   Z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;   s    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 ]f\}}d}d}d|v rtt|}d|v rt|}|	dp|	dpd}t
|dd}t|||d||< qTh d}| j D ]R\}	}
|	d	krq|	d
r|	d
r|	|vrtd|	 dt|	|
||	< q| j D ]\}	}
t|	|
||	< q,t| jtjf|}|t | < | D ]d\}}t|trht
||}|jdurh|j}|| f}|  t||t||j|j|jd qh|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>,   __str____lt____len____ipow____gt__
__lshift____ilshift____le____pow____ge____add____mul____pos____or____itruediv____truediv____bool__	__index____abs____sub____irshift____floordiv____iand____isub____and____xor____iadd____ior____setitem____getitem____mod____hash__
__rshift____neg____imod____ixor____eq____int__	__float____imul____complex____contains____ifloordiv____ne____init____zMethod 'z' is not supported.)_cache_specialized_boxZ
class_typeZ	class_docstruct_generate_getter_generate_setterpropertyZ	jit_propsitemsr%   getattrmethods
startswithendswith	TypeErrorr!   Zstatic_methodstype	classnamer   Box
isinstancefgetcompileZdisable_compilesetattrfsetfdelr$   )typdctr   gettersetterZimpdctimpr'   Zsupported_dundersr   r    subclskvpropre   Z	fast_fgetr   r   r   _specialize_boxE   sb    
.
rs   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 )NZ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   Zunpack_tuplerz   rs   rv   Zadd_dynamic_addridr{   pyapiZpyobjZcall_function_objargsr   IntTyper|   r   box_meminfoptr_offsetbox_dataptr_offset)rj   valr   meminfoZdataptrZbox_subclassedZvoidptr_boxclsZbox_clsZaddr_meminfoZ	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 )Nru   )rv   rw   r   rx   r   r   r|   r   ry   rz   r{   load)r~   r   r   r   r   r   r   r   r   access_member   s
    z,_unbox_class_instance.<locals>.access_member)Zis_error)r   Zcreate_struct_proxyrv   rz   r   r   r   r{   r   ra   dataZ	_getvalueZnrtZincrefr
   r   Zc_api_error)	rj   r   r   r   Z
struct_clsinstZptr_meminfoZptr_dataptrretr   r   r   _unbox_class_instance   s    


r   c                 C   s   t t| dS )Nr#   )r\   ra   )r   r   r   r   r   _typeof_jitclass_box   s    r   N)"r$   	functoolsr   r   Zllvmliter   Z
numba.corer   r   Znumba.core.decoratorsr   Znumba.core.pythonapir   r	   r
   Znumba.core.typing.typeofr   Znumba.experimental.jitclassr   Z_getter_code_templateZ_setter_code_templater   r   rX   rY   r!   rV   rs   ZClassInstanceTyper   r   registerrc   r   r   r   r   r   <module>   s4   
u
 
$
