U
    ¿|eó  ã                   @   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   ú_/var/www/website-v5/atlas_env/lib/python3.8/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
   ˆ | |Ž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 krt |  S d| | jjdœ}| jD ]"}t|ƒ}t|ƒ}t||ƒ||< q&| j ¡ D ]f\}}d}d}d|krtt|ƒ}d|kr„t|ƒ}| 	d¡pš| 	d¡pšd}t
|ddƒ}t|||d�||< qTdd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3h,}| j ¡ D ]X\}	}
|	d4k�r8�q"|	 d5¡�rj|	 d5¡�rj|	|k�rjtd6|	› d7�ƒ‚t|	|
ƒ||	< �q"| j ¡ D ]\}	}
t|	|
ƒ||	< �q†t| jtjf|ƒ}|t | < | ¡ D ]d\}}t|tƒ�rÂt
||ƒ}|jdk	�rÂ|j}| | f¡}| ¡  t||t||j|j|jd�ƒ �qÂ|S )8z�
    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Ú__abs__Ú__bool__Ú__complex__Ú__contains__Ú	__float__Ú__getitem__Ú__hash__Ú	__index__Ú__int__Ú__len__Ú__setitem__Ú__str__Ú__eq__Ú__ne__Ú__ge__Ú__gt__Ú__le__Ú__lt__Ú__add__Ú__floordiv__Ú
__lshift__Ú__mod__Ú__mul__Ú__neg__Ú__pos__Ú__pow__Ú
__rshift__Ú__sub__Ú__truediv__Ú__and__Ú__or__Ú__xor__Ú__iadd__Ú__ifloordiv__Ú__ilshift__Ú__imod__Ú__imul__Ú__ipow__Ú__irshift__Ú__isub__Ú__itruediv__Ú__iand__Ú__ior__Ú__ixor__Ú__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ÚsetterZimpdctÚimpr'   Zsupported_dundersr   r    ÚsubclsÚkÚvÚpropri   Z	fast_fgetr   r   r   Ú_specialize_boxE   sº    þ
Ô.
ÿþý
ÿÿrx   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   Úunpack_tupler   rx   r{   Úadd_dynamic_addrÚidr€   ÚpyapiÚpyobjÚcall_function_objargsr   ÚIntTyper�   r   Úbox_meminfoptr_offsetÚbox_dataptr_offset)ro   Úvalr‰   ÚmeminfoÚ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 )Nrz   )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–   re   ÚdataÚ	_getvalueÚnrtÚincrefr
   r�   Úc_api_error)	ro   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_   re   )r•   r‰   r   r   r   Ú_typeof_jitclass_box   s    r¦   N)"r$   Ú	functoolsr   r   Úllvmliter   Ú
numba.corer   r   Únumba.core.decoratorsr   Únumba.core.pythonapir   r	   r
   Znumba.core.typing.typeofr   Únumba.experimental.jitclassr   Z_getter_code_templateZ_setter_code_templater   r   rZ   r[   r!   rV   rx   ÚClassInstanceTyper˜   r¥   Úregisterrg   r¦   r   r   r   r   Ú<module>   s4   
ÿÿu
 
$
