B
      ›\B	  ã               @   s8   d dl mZ ddlmZmZ edƒG dd„ deƒƒZdS )é    )Úiré   )Úregister_rewriteÚRewritezbefore-inferencec               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚRewriteConstRaisesa?  
    Rewrite IR statements of the kind `raise(value)`
    where `value` is the result of instantiating an exception with
    constant arguments
    into `static_raise(exception_type, constant args)`.

    This allows lowering in nopython mode, where one can't instantiate
    exception instances from runtime data.
    c             C   s   t |tƒot|tƒS )N)Ú
isinstanceÚtypeÚ
issubclassÚ	Exception)ÚselfÚconst© r   ú:lib/python3.7/site-packages/numba/rewrites/static_raise.pyÚ_is_exception_type   s    z%RewriteConstRaises._is_exception_typec             C   sb   t |tƒr>|  |d ¡s*td|d f ƒ‚|d t|d ƒfS |  |¡rP|dfS td|f ƒ‚dS )z0
        Break down constant exception.
        r   z!unsupported exception constant %rr   N)r   Útupler   ÚNotImplementedError)r   r   r   r   r   Ú_break_constant   s    

z"RewriteConstRaises._break_constantc       
      C   sl   i  | _ }|| _xN| tj¡D ]>}|jd kr6d\}}n| |j¡}	|  |	¡\}}||f||< qW t|ƒdkS )N)NNr   )	ÚraisesÚblockZ
find_instsr   ZRaiseZ	exceptionZinfer_constantr   Úlen)
r   Zfunc_irr   ZtypemapZ	calltypesr   ÚinstÚexc_typeÚexc_argsr   r   r   r   Úmatch#   s    


zRewriteConstRaises.matchc             C   sf   | j  ¡ }| ¡  xN| j jD ]B}|| jkrT| j| \}}t |||j¡}| |¡ q| |¡ qW |S )zC
        Rewrite all matching setitems as static_setitems.
        )	r   ÚcopyÚclearZbodyr   r   ZStaticRaiseZlocÚappend)r   Z	new_blockr   r   r   Znew_instr   r   r   Úapply4   s    

zRewriteConstRaises.applyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r      s
   
r   N)Znumbar   Ú r   r   r   r   r   r   r   Ú<module>   s   