a
    –§»a  ã                   @   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
mZ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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)ÚboxÚunboxÚNativeValue)Únjitz;
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   ú\/home/yuhw/wc/pochoir/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   Úwrapper9   s    z!_generate_method.<locals>.wrapper)Ú_method_code_templater   r   r
   r   )ÚnameÚfuncr   r   r   r   r   r   Ú_generate_method/   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 r„t|ƒ}| 	d¡pš| 	d¡pšd}t
|ddƒ}t|||d�||< qT| j ¡ D ]:\}}	|dksò|d	ksò| d
¡rò| d
¡sÆt||	ƒ||< qÆ| j ¡ D ]\}}	t||	ƒ||< �qddlm}
 t| j|
jf|ƒ}|t | < | ¡ D ]d\}}t|tƒ�rTt
||ƒ}|jdu�rT|j}| | f¡}| ¡  t||t||j|j|jd�ƒ �qT|S )z�
    Create a subclass of Box that is specialized to the jitclass.

    This function caches the result to avoid code bloat.
    r   )Ú	__slots__Z_numba_type_Ú__doc__NÚgetÚsetr!   )ÚdocÚ__getitem__Ú__setitem__Ú__r   ©Ú_box)Ú_cache_specialized_boxZ
class_typeZ	class_docÚstructÚ_generate_getterÚ_generate_setterÚpropertyZ	jit_propsÚitemsr"   ÚgetattrÚmethodsÚ
startswithÚendswithr   Zstatic_methodsÚnumba.experimental.jitclassr)   ÚtypeÚ	classnameZBoxÚ
isinstanceÚfgetÚcompileZdisable_compileÚsetattrÚfsetÚfdelr!   )ÚtypÚdctr   ÚgetterÚsetterZimpdctÚimpr$   r   r   r)   ÚsubclsÚkÚvÚpropr8   Z	fast_fgetr   r   r   Ú_specialize_boxC   sZ    þ
ÿÿ
ÿÿrF   c                    s´   t  ˆj|¡\}}t| ƒ}ˆjjˆjt|ƒdd�}ˆj |ˆjj	¡}ˆj 
|d¡‰ t d¡ ¡ ‰ˆj |ˆ¡}ˆj |ˆ¡}	‡ ‡‡fdd„}
ddlm} |
|j|ƒ |
|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_memberr   r(   )r   Zunpack_tuplerM   rF   rI   Zadd_dynamic_addrÚidrN   ÚpyapiZpyobjZcall_function_objargsr   ÚIntTyperO   r4   r)   Úbox_meminfoptr_offsetÚbox_dataptr_offset)r=   ÚvalrW   ÚmeminfoZdataptrZbox_subclassedZvoidptr_boxclsZbox_clsZaddr_meminfoZ	addr_datarY   r)   r   rV   r   Ú_box_class_instance†   s"    ýra   c           
         sœ   ‡ ‡fdd„}t  | ¡}|ˆ jˆ jƒ}ddlm} ||jƒ}||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 )NrH   )rI   rJ   r   rK   r   r\   rO   r   rL   rM   rN   Úload)rQ   rS   rX   rT   rU   ©rW   r_   r   r   Úaccess_memberª   s
    z,_unbox_class_instance.<locals>.access_memberr   r(   )Zis_error)r   Zcreate_struct_proxyrI   rM   r4   r)   r]   r^   rN   r`   r5   ÚdataZ	_getvalueZnrtZincrefr	   r[   Zc_api_error)
r=   r_   rW   rd   Z
struct_clsÚinstr)   Zptr_meminfoZptr_dataptrÚretr   rc   r   Ú_unbox_class_instance¨   s    


rh   N)r!   Ú	functoolsr   r   Zllvmliter   Z
numba.corer   r   Znumba.core.pythonapir   r   r	   Znumbar
   Z_getter_code_templateZ_setter_code_templater   r   r,   r-   r   r*   rF   ZClassInstanceTypera   rh   r   r   r   r   Ú<module>   s,   
ÿÿC
!