B
     \                 @   sL   d dl mZmZ ddlmZmZ G dd deZedG dd deZd	S )
    )irerrors   )register_rewriteRewritec               @   s&   e Zd ZdZdZd	ddZdd ZdS )
Macroar  
    A macro object is expanded to a function call

    Args
    ----
    name: str
        Name of this Macro
    func: function
        Function that evaluates the macro expansion.
    callable: bool
        True if the macro is callable from Python code
        (``func`` is then a Python callable returning the desired IR node).
        False if the macro is not callable
        (``func`` is then the name of a backend-specific function name
         specifying the function to call at runtime).
    argnames: list
        If ``callable`` is True, this holds a list of the names of arguments
        to the function.
    )namefunccallableargnamesFNc             C   s   || _ || _|| _|| _d S )N)r   r	   r
   r   )selfr   r	   r
   r    r   4lib/python3.7/site-packages/numba/rewrites/macros.py__init__   s    zMacro.__init__c             C   s   d| j | jf S )Nz<macro %s -> %s>)r   r	   )r   r   r   r   __repr__"   s    zMacro.__repr__)FN)__name__
__module____qualname____doc__	__slots__r   r   r   r   r   r   r      s   
r   zbefore-inferencec               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ExpandMacrosz4
    Expand lookups and calls of Macro objects.
    c       
   	   C   s&  || _ i  | _}x|jD  ]}t|tjr|j}t|tjr|jdkrt|j	tj
ry||j	}W n tjk
r~   wY nX t|tr|jst| ||||j}	|	||< qt|tjr|jdkry||j}W n tjk
r   wY nX t|tr|js| ||j}	|	||< qW t|dkS )zR
        Look for potential macros for expand and store their expansions.
        callgetattrr   )blockrewritesbody
isinstancer   AssignvalueExpropr	   ZVarinfer_constantr   ConstantInferenceErrorr   r
   AssertionError_expand_callable_macroloctarget_expand_non_callable_macrolen)
r   func_irr   ZtypemapZ	calltypesr   instZrhsZconstnew_exprr   r   r   match,   s2    




zExpandMacros.matchc             C   s,   t j|j|jdd}t jj|dd|d}|S )zO
        Return the IR expression of expanding the non-callable macro.
        r   )args)r	   r-   kwsr%   )r   Z	Intrinsicr   r	   r   r   )r   macror%   Zintrr+   r   r   r   r'   O   s    

z'ExpandMacros._expand_non_callable_macroc          
      s  |j s
t fdd|jD }i }xR|jD ]H\}}y |||< W q* tjk
rp   dj||d}	t|	Y q*X q*W y|j	||}
W nP t
k
r } z2t|}	d}|j|d}d||	}t|W dd}~X Y nX |
dk	st|j|
_tjj|
|j|j|d	}|S )
zG
        Return the IR expression of expanding the macro call.
        c                s   g | ]}  |jqS r   )r!   r   ).0arg)r)   r   r   
<listcomp>_   s    z7ExpandMacros._expand_callable_macro.<locals>.<listcomp>z-Argument {name!r} must be a constant at {loc})r   r%   z Macro expansion failed at {line})linez{0}:
{1}N)r	   r-   r.   r%   )r
   r#   r-   r.   r!   r   r"   format
ValueErrorr	   	ExceptionstrZ
MacroErrorr%   r   r   r   )r   r)   r   r/   r%   r-   r.   kvmsgresulteZheadfmtheadZnewmsgr+   r   )r)   r   r$   X   s0    
z#ExpandMacros._expand_callable_macroc             C   sB   | j }| j}x0|jD ]&}t|tjr|j|kr||j |_qW |S )z<
        Apply the expansions computed in .match().
        )r   r   r   r   r   r   r   )r   r   r   r*   r   r   r   applyz   s    zExpandMacros.applyN)r   r   r   r   r,   r'   r$   r>   r   r   r   r   r   &   s
   #	"r   N)	Znumbar   r    r   r   objectr   r   r   r   r   r   <module>   s   !