U
    ,d                     @   sT   d Z ddlZddlZddlmZ G dd dZG dd dejZG dd	 d	eZdS )
z
Implement utils for supporting retargeting of dispatchers.

WARNING: Features defined in this file are experimental. The API may change
         without notice.
    N)errorsc                   @   s>   e Zd ZdZejZdd Zdd Zdd Z	dd	 Z
d
d ZdS )RetargetCachez^Cache for retargeted dispatchers.

    The cache uses the original dispatcher as the key.
    c                 C   s   |   | _d| _d| _d S )Nr   )container_type_cache	_stat_hit
_stat_missself r
   7/tmp/pip-unpacked-wheel-eu7e0c37/numba/core/retarget.py__init__   s    
zRetargetCache.__init__c                 C   s   || j |< dS )z9Save a dispatcher associated with the given key.
        N)r   )r	   	orig_dispZnew_dispr
   r
   r   
save_cache   s    zRetargetCache.save_cachec                 C   s6   | j |}|dkr$|  jd7  _n|  jd7  _|S )z9Load a dispatcher associated with the given key.
        N   )r   getr   r   )r	   r   outr
   r
   r   
load_cache   s
    zRetargetCache.load_cachec                 C   s
   | j  S )z+Returns the contents of the cache.
        )r   itemsr   r
   r
   r   r   (   s    zRetargetCache.itemsc                 C   s   | j | jdS )z0Returns stats regarding cache hit/miss.
        )hitZmiss)r   r   r   r
   r
   r   stats-   s    zRetargetCache.statsN)__name__
__module____qualname____doc__weakrefWeakKeyDictionaryr   r   r   r   r   r   r
   r
   r
   r   r      s   
r   c                   @   s,   e Zd ZdZejdd Zejdd ZdS )BaseRetargetz/Abstract base class for retargeting logic.
    c                 C   s   dS )zCheck that the retarget is compatible.

        This method does not return anything meaningful (e.g. None)
        Incompatibility is signalled via raising an exception.
        Nr
   r	   r   r
   r
   r   check_compatible6   s    zBaseRetarget.check_compatiblec                 C   s   dS )zRetargets the given dispatcher and returns a new dispatcher-like
        callable. Or, returns the original dispatcher if the the target_backend
        will not change.
        Nr
   r   r
   r
   r   retarget?   s    zBaseRetarget.retargetN)r   r   r   r   abcabstractmethodr   r   r
   r
   r
   r   r   3   s
   
r   c                   @   sN   e Zd ZdZeZdd Zeje	dddZ
ejdd Zd	d
 Zdd ZdS )BasicRetargeta  A basic retargeting implementation for a single output target.

    This class has two abstract methods/properties that subclasses must define.

    - `output_target` must return output target name.
    - `compile_retarget` must define the logic to retarget the given dispatcher.

    By default, this class uses `RetargetCache` as the internal cache. This
    can be modified by overriding the `.cache_type` class attribute.

    c                 C   s   |   | _d S )N)
cache_typecacher   r
   r
   r   r   V   s    zBasicRetarget.__init__)returnc                 C   s   dS )zhReturns the output target name.

        See numba/tests/test_retargeting.py for example usage.
        Nr
   r   r
   r
   r   output_targetY   s    zBasicRetarget.output_targetc                 C   s   dS )zkReturns the retargeted dispatcher.

        See numba/tests/test_retargeting.py for example usage.
        Nr
   r   r
   r
   r   compile_retargeta   s    zBasicRetarget.compile_retargetc                 C   s<   |j }| j}|dk	r8||kr8d| d| d}t|dS )zt
        This implementation checks that
        `self.output_target == orig_disp._required_target_backend`
        Nz2The output target does match the required target: z != .)Z_required_target_backendr&   r   ZCompilerError)r	   r   Zrequired_targetr&   mr
   r
   r   r   i   s    zBasicRetarget.check_compatiblec                 C   sR   | j }|j}|d| jkr |S ||}|dkrJ| |}||| n|}|S )zgApply retargeting to orig_disp.

        The retargeted dispatchers are cached for future use.
        Ztarget_backendN)r$   Ztargetoptionsr   r&   r   r'   r   )r	   r   r$   optscachedr   r
   r
   r   r   v   s    

zBasicRetarget.retargetN)r   r   r   r   r   r#   r   r    abstractpropertystrr&   r!   r'   r   r   r
   r
   r
   r   r"   H   s   
r"   )	r   r    r   Z
numba.corer   r   ABCr   r"   r
   r
   r
   r   <module>   s   &