B
     \                 @   s   d Z ddlmZm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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 dS )z
Implement logic relating to wrapping (box) and unwrapping (unbox) instances
of jitclasses for use inside the python interpreter.
    )print_functionabsolute_import)wrapspartial)ir)typescgutils)boxunboxNativeValue)njit)exec_   )_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
    )formatr   r   )fieldtemplatefnamesourceglbls r   4lib/python3.7/site-packages/numba/jitclass/boxing.py_generate_property"   s    

r   Z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kwargs)r   r   r   wrapper<   s    z!_generate_method.<locals>.wrapper)_method_code_templater   r   r   r   )namefuncr   r   r   r   )r   r   _generate_method2   s    
r    c          
   C   s  | t krt |  S d| | jjjd}x,| jD ]"}t|}t|}t||||< q*W xt| j	 D ]f\}}d}d}d|kr|t|}d|krt|}|
dp|
dpd}t|dd}t|||d||< q\W x8| j	 D ]*\}}	|dr|dst||	||< qW t| jtjf|}
|
t | < xp|	 D ]d\}}t|tr$t|
|}|jdk	r$|j}|| f}|  t|
|t||j|j|jd q$W |
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__)_cache_specialized_boxZ
class_typeZ	class_defr"   Zstruct_generate_getter_generate_setterpropertyZjitpropsitemsr#   getattrmethods
startswithendswithr    typeZ	classnamer   ZBox
isinstancefgetcompileZdisable_compilesetattrfsetfdel)typZdctr   gettersetterZimpdctimpr%   r   r   subclskvZpropr2   Z	fast_fgetr   r   r   _specialize_boxF   sF    
r>   c                s   t j|\}}t| }jtjt|}j	|j
j}j
|d td j|}j|}	 fdd}
|
tj| |
tj|	  S )Nr      c                sD   j tj| }tj |}j| }j	|| d S )N)
contextget_constantr   uintpr   pointer_addbuilderbitcast
as_pointerZstore)member_offsetvalueoffsetptrcasted)r	   c	llvoidptrr   r   
set_member   s    z'_box_class_instance.<locals>.set_member)r   Zunpack_tuplerD   r>   r@   rA   r   rB   idZinttoptrpyapiZpyobjZcall_function_objargsr   IntTyperF   rE   r   box_meminfoptr_offsetbox_dataptr_offset)r7   valrL   meminfoZdataptrZbox_subclassedZint_addr_boxclsZbox_clsZaddr_meminfoZ	addr_datarN   r   )r	   rL   rM   r   _box_class_instance   s    rV   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@   rA   r   rB   r   rQ   rF   r   rC   rD   rE   load)rG   rI   rM   rJ   rK   )rL   rT   r   r   access_member   s
    z,_unbox_class_instance.<locals>.access_member)Zis_error)r   Zcreate_struct_proxyr@   rD   r   rR   rS   rE   rU   r0   dataZ	_getvalueZnrtZincrefr   rP   Zc_api_error)	r7   rT   rL   rX   Z
struct_clsZinstZptr_meminfoZptr_dataptrZretr   )rL   rT   r   _unbox_class_instance   s    


rZ   N)!r"   Z
__future__r   r   	functoolsr   r   Zllvmliter   Znumbar   r   Znumba.pythonapir	   r
   r   r   Z	numba.sixr    r   Z_getter_code_templateZ_setter_code_templater   r   r(   r)   r    r'   r>   ZClassInstanceTyperV   rZ   r   r   r   r   <module>   s*   
;