B
     \@                 @   s  d dl mZmZ d dlmZ d dlZd dlZd dlm	Z
 d dlmZ d dlmZ d dl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 d dlmZ d dlmZ edkrd dlmZ nd dlmZ ddlmZ G dd dej Z!G dd dej Z"e#ej$e! e#ej%e" e#ej&ej' dd Z(dZ)dd Z*G dd de+Z,dd Z-dd  Z.d!d" Z/d#d$ Z0G d%d& d&ej1Z2d'd( Z3G d)d* d*e4Z5ej6G d+d, d,ej7Z8e5j9:ej$d-d. Z;e5j9<ej$d/d. Z;d0d1 Z=e5j9>ej&e?ej@d2d3 ZAdS )4    )absolute_importprint_function)OrderedDictN)ir)types)
cpu_target)njit)	templates)default_managermodels)imputils)cgutilsutils)	PYVERSION)exec_)   r   )Sequence   )_boxc                   s   e Zd Z fddZ  ZS )InstanceModelc                sP   t |}t dt| }dt |fdt |fg}tt| ||| d S )NzOpaque.meminfodata)	r   ClassDataTypeZOpaquestrZMemInfoPointerZCPointersuperr   __init__)selfdmmfe_typZcls_data_tyZdtypemembers)	__class__ 2lib/python3.7/site-packages/numba/jitclass/base.pyr   !   s
    
zInstanceModel.__init__)__name__
__module____qualname__r   __classcell__r!   r!   )r    r"   r       s   r   c                   s   e Zd Z fddZ  ZS )InstanceDataModelc                s2   |j }dd |j D }tt| ||| d S )Nc             S   s   g | ]\}}t ||fqS r!   )_mangle_attr).0kvr!   r!   r"   
<listcomp>2   s    z.InstanceDataModel.__init__.<locals>.<listcomp>)
class_typestructitemsr   r'   r   )r   r   r   Zclstyr   )r    r!   r"   r   0   s    zInstanceDataModel.__init__)r#   r$   r%   r   r&   r!   r!   )r    r"   r'   /   s   r'   c             C   s   d|  S )zZ
    Mangle attributes.
    The resulting name does not startswith an underscore '_'.
    Zm_r!   )namer!   r!   r"   r(   ;   s    r(   z3
def ctor({args}):
    return __numba_cls_({args})
c             C   s&   t | }|j}dd | D }|S )zI
    Returns list of positional and keyword argument names in order.
    c             S   s&   g | ]\}}|j |j@ |jkr|qS r!   )ZkindZPOSITIONAL_OR_KEYWORD)r)   r*   r+   r!   r!   r"   r,   R   s    z_getargs.<locals>.<listcomp>)r   ZpysignatureZ
parametersr/   )fnsigZparamsargsr!   r!   r"   _getargsL   s    
r4   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )JitClassTypez#
    The type of any jitclass.
    c             C   sX   t |dkrtd|\}t|tr,tdd|ks<tdt| |||}|  |S )Nr   z must have exactly one base classzcannot subclass from a jitclassr-   zmissing "class_type" attr)len	TypeError
isinstancer5   AssertionErrortype__new__	_set_init)clsr0   basesdctbaseZoutclsr!   r!   r"   r;   [   s    
zJitClassType.__new__c             C   sX   | j jjd }t|dd }tjd|d}d| i}t|| |d }t|| _	dS )z
        Generate a wrapper for calling the constructor from pure Python.
        Note the wrapper will only accept positional arguments.
        r   r   Nz, )r3   Z__numba_cls_ctor)
r-   instance_typemethodsr4   _ctor_templateformatjoinr   r   _ctor)r=   initr3   Zctor_sourceZglblsrA   r!   r!   r"   r<   f   s    
zJitClassType._set_initc             C   s   t |tjr|jj| jkS dS )NF)r8   r   ZBoxZ_numba_type_r-   )r=   instancer!   r!   r"   __instancecheck__u   s    zJitClassType.__instancecheck__c             O   s   | j ||S )N)rG   )r=   r3   kwargsr!   r!   r"   __call__z   s    zJitClassType.__call__N)r#   r$   r%   __doc__r;   r<   rJ   rL   r!   r!   r!   r"   r5   W   s
   r5   c             C   sL   xF|   D ]:\}}t|ts*td|f t|tjs
td|f q
W d S )Nz#spec keys should be strings, got %rz2spec values should be Numba type instances, got %r)r/   r8   r   r7   r   ZType)specr*   r+   r!   r!   r"   _validate_spec   s    
rO   c             C   sH   t  }x<| D ]0\}}|dr8|ds8d|  | }|||< qW |S )zB
    Apply the same changes to dunder names as CPython would.
    ___)r   r/   
startswithendswith)ZclsnamerN   outr*   r+   r!   r!   r"   _fix_up_private_attr   s    rU   c             C   s   t |dd}|r| | dS )zC
    Add the required libs for the callable to allow inlining.
    libsr!   N)getattrZadd_linking_libs)contextcallrV   r!   r!   r"   _add_linking_libs   s    rZ   c                s  t |trt|}t| t| j|}i }x"tt| D ]}|	|j
 q:W tdd | D  tdd | D t fdd| D }t tB t|@ }|rtdd||dd}t| |rd	}	d| }
t|	|
x0 D ]$\}}|jd
k	rtd|qW i }x"  D ]\}}t|||< q>W i }xN D ]B\}}i }|jrt|j|d< |jrt|j|d< |||< qfW || t|||}t| j| ft||d} tj}|| | tj}|| ||  | S )a$  
    Internal function to create a jitclass.

    Args
    ----
    cls: the original class object (used as the prototype)
    spec: the structural specification contains the field types.
    class_ctor: the numba type to represent the jitclass
    builder: the internal jitclass builder
    c             s   s&   | ]\}}t |tjr||fV  qd S )N)r8   pytypesFunctionType)r)   r*   r+   r!   r!   r"   	<genexpr>   s    z&register_class_type.<locals>.<genexpr>c             s   s$   | ]\}}t |tr||fV  qd S )N)r8   property)r)   r*   r+   r!   r!   r"   r]      s    c             3   s*   | ]"\}}| kr|kr||fV  qd S )Nr!   )r)   r*   r+   )rC   propsr!   r"   r]      s    zname shadowing: {0}z, rM    z(class members are not yet supported: {0}Nzdeleter is not supported: {0}getset)r-   rM   ) r8   r   r   rO   rU   r#   reversedinspectZgetmroupdate__dict__dictr/   rb   	NameErrorrE   rF   pop_drop_ignored_attrskeysr7   fdelr   fgetfsetConstructorTemplater5   r   typing_contextZinsert_globalZtarget_contextregister)r=   rN   Z
class_ctorbuilderZclsdctZbaseclsZothersZshadowedZ	docstringmsgr   r*   r+   
jitmethodsjitpropsr?   r-   	typingctx	targetctxr!   )rC   r_   r"   register_class_type   sT    


rx   c               @   s   e Zd ZdZdd ZdS )ro   z8
    Base class for jitclass constructor templates.
    c       	      C   sX   | j j}|jd }| f| }t|}|| j||}tj	|f|j
dd   }|S )Nr   r   )keyrB   rt   Zget_reference_typer   
Dispatcherget_call_typerX   r	   	signaturer3   )	r   r3   kwsrB   rA   Z	boundargs	disp_typer2   rT   r!   r!   r"   generic   s    

zConstructorTemplate.genericN)r#   r$   r%   rM   r   r!   r!   r!   r"   ro      s   ro   c             C   st   t dddg}xL|  D ]@\}}t|tjtjfr>|| qt|dd tkr|| qW x|D ]
}| |= qbW d S )N__weakref__r$   rf   __objclass__)	rb   r/   r8   r[   BuiltinFunctionTypeBuiltinMethodTypeaddrW   object)r?   Zdropr*   r+   r!   r!   r"   rj      s    

rj   c               @   s>   e Zd ZdZe Ze Zdd Z	dd Z
dd Zdd	 Zd
S )ClassBuilderz
    A jitclass builder for a mutable jitclass.  This will register
    typing and implementation hooks to the given typing and target contexts.
    c             C   s   || _ || _|| _|| _d S )N)r-   rC   rv   rw   )r   r-   rC   rv   rw   r!   r!   r"   r     s    zClassBuilder.__init__c             C   s$   |  | j| jj | j| j dS )z7
        Register to the frontend and backend.
        N)_register_methodsclass_impl_registryr-   rB   rw   Zinstall_registry)r   r!   r!   r"   rq     s    
zClassBuilder.registerc             C   s6   x0|j D ]&}|| jkr| || | j| qW dS )zN
        Register method implementations for the given instance type.
        N)rt   implemented_methods_implement_methodr   )r   registryrB   methr!   r!   r"   r   '  s    
zClassBuilder._register_methodsc                s.   | tj ftjttj fdd}d S )Nc       	         sP   |j d }|j  }t|}| ||}|||}t| | t| ||j|S )Nr   )	r3   rt   r   rz   get_functionrZ   r   impl_ret_new_refreturn_type)	rX   rr   r2   r3   rB   methodr~   rY   rT   )attrr!   r"   imp4  s    




z+ClassBuilder._implement_method.<locals>.imp)lowerr   ClassInstanceTypeVarArgAny)r   r   r   r   r!   )r   r"   r   3  s    zClassBuilder._implement_methodN)r#   r$   r%   rM   r   ZRegistryr   rb   r   r   rq   r   r   r!   r!   r!   r"   r     s   r   c               @   s   e Zd ZejZdd ZdS )ClassAttributec                s    j krj   S  jkrZj  }t|G  fdddtj}t|S  jkrj  }|d }t|j	fi }|j
S d S )Nc                   s$   e Zd Zj fZfddZdS )z6ClassAttribute.generic_resolve.<locals>.MethodTemplatec                s&   ft | } | j||}| S )N)tupler{   rX   Z	as_method)r   r3   r}   r2   )r~   rI   r!   r"   r   S  s    z>ClassAttribute.generic_resolve.<locals>.MethodTemplate.genericN)r#   r$   r%   ry   r   r!   )r   r~   rI   r   r!   r"   MethodTemplateP  s   
r   ra   )r.   rt   r   rz   r	   AbstractTemplateZBoundFunctionru   r{   rX   r   )r   rI   r   r   r   Zimpdctgetterr2   r!   )r   r~   rI   r   r"   generic_resolveF  s    







zClassAttribute.generic_resolveN)r#   r$   r%   r   r   ry   r   r!   r!   r!   r"   r   B  s   r   c          	   C   s   ||j krT| j|||d}|j}| j|| |d}t| ||j | t|t|S ||j	kr|j	| d }t
d|}	t|}
|
| j|gi }	| |
|	}|||g}t| | t| ||	j|S td|dS )z4
    Generic getattr() for @jitclass instances.
    )value)refra   Nzattribute {0!r} not implemented)r.   make_helperr   make_data_helperget_data_typer   Zimpl_ret_borrowedrW   r(   ru   r	   r|   r   rz   r{   rp   r   rZ   r   r   NotImplementedErrorrE   )rX   rr   typr   r   instdata_pointerr   r   r2   Z
dispatcherrY   rT   r!   r!   r"   	attr_implc  s$    



r   c             C   s   |j \}}|\}}||jkr| j|||d}	|	j}
| j|| |
d}|j| }t|t|}t|t|| | j	
||| | j	||| nj||jkr|j| d }t|}|| j||fi }| ||}||||f t| | ntd|dS )z4
    Generic setattr() for @jitclass instances.
    )r   )r   rb   zattribute {0!r} not implementedN)r3   r.   r   r   r   r   rW   r(   setattrnrtZincrefdecrefru   r   rz   r{   rp   r   rZ   r   rE   )rX   rr   r2   r3   r   r   Zvaltytargetvalr   Zdata_ptrr   Z	attr_typeZoldvaluesetterr~   rY   r!   r!   r"   r     s*    




c             C   s   |  tj}|  tj}tt |||g}d|j}|j	||d}|j
rt| }| }	|  |	}
||jd |
 }| j||	|d}| j||	|  |  |S )Nz	_Dtor.{0})r0   r   )r   )Zget_value_typer   Zvoidptruintpllvmirr\   ZVoidTyperE   r0   Zget_or_insert_functionZis_declarationZ	IRBuilderZappend_basic_blockr   bitcastr3   
as_pointerr   r   r   	_getvalueZret_void)rX   modulerB   Z	llvoidptrZllsizeZ
dtor_ftypefnameZdtor_fnrr   Zalloc_fe_type
alloc_typeZptrr   r!   r!   r"   imp_dtor  s     

r   c             C   s   |j }| | }| |}| j|| tj|t| |j	|}| j
||}||| }|t|| | ||}	||	_||	_|j f|j }
|jd }t|}| |tj|
 }t| | |	 gt| }||| |	 }t| |||S )z7
    Generic constructor (__new__) for jitclasses.
    r   )r   r   Zget_abi_sizeofr   Zmeminfo_alloc_dtorZget_constantr   r   r   r   Zmeminfo_datar   r   Zstorer   Zget_null_valuer   r   r   r3   rt   rz   r   ZvoidrZ   r   listr   r   )rX   rr   r2   r3   Zinst_typr   Z
alloc_sizer   r   Zinst_structZinit_sigrH   r~   rY   ZrealargsZretr!   r!   r"   	ctor_impl  s0    





r   )BZ
__future__r   r   collectionsr   r   r[   rd   Zllvmliter   r   ZnumbaZnumba.targets.registryr   r   Znumba.typingr	   Znumba.datamodelr
   r   Znumba.targetsr   r   r   Znumba.configr   Z	numba.sixr   Zcollections.abcr   r`   r   ZStructModelr   r'   rq   r   r   Z	ClassTypeZOpaqueModelr(   rD   r4   r:   r5   rO   rU   rZ   rx   r   ro   rj   r   r   Zinfer_getattrZAttributeTemplater   r   Zlower_getattr_genericr   Zlower_setattr_genericr   r   r   r   r   r!   r!   r!   r"   <module>   sN   *		O4
 '