U
    ,d                     @   sD   d dl mZ d dlZd dlmZmZ d dlmZ G dd deZ	dS )    )
ModuleTypeN)ConstantInferenceError
NumbaError)irc                   @   sJ   e Zd ZdZdd ZdddZdd Zd	d
 Zdd Zdd Z	dd Z
dS )ConstantInferencez
    A constant inference engine for a given interpreter.
    Inference inspects the IR to try and compute a compile-time constant for
    a variable.

    This shouldn't be used directly, instead call Interpreter.infer_constant().
    c                 C   s   t || _i | _d S N)weakrefproxy_func_ir_cache)selfZfunc_ir r   5/tmp/pip-unpacked-wheel-eu7e0c37/numba/core/consts.py__init__   s    zConstantInference.__init__Nc              
   C   s   || j kr\zd| |f| j |< W n8 tk
rZ } zd|j|jff| j |< W 5 d}~X Y nX | j | \}}|rr|S |\}}t|tr||d|in|| dS )z
        Infer a constant value for the given variable *name*.
        If no value can be inferred, numba.errors.ConstantInferenceError
        is raised.
        TFNloc)r   	_do_inferr   	__class__args
issubclassr   )r   namer   excsuccessvalr   r   r   r   infer_constant   s    
(
z ConstantInference.infer_constantc                 C   s   t d|f d dd S )Nz'Constant inference not possible for: %sr   )r   )r   r   r   r   r   _fail/   s     zConstantInference._failc                 C   s   t |tstd|f z| j|}W n" tk
rJ   td|f Y nX z| }W n8 tk
r   t |tj	r| 
| Y S | | Y nX |S )Nz'infer_constant() called with non-str %rzno single definition for %r)
isinstancestr	TypeErrorr
   Zget_definitionKeyErrorr   r   r   ZExpr_infer_exprr   )r   r   Zdefnconstr   r   r   r   8   s"    

zConstantInference._do_inferc                    s    j dkr*j jj jd}| S  j dkrTj jj jd}| S  j dkrt fdd jD S  j dkrt	 fdd	 jD S 
  d S )
Ncallr   getattrZ
build_listc                    s   g | ]}j |j jd qS r   r   r   r   .0iexprr   r   r   
<listcomp>R   s     z1ConstantInference._infer_expr.<locals>.<listcomp>Zbuild_tuplec                 3   s    | ]}j |j jd V  qdS )r   Nr%   r&   r)   r   r   	<genexpr>U   s     z0ConstantInference._infer_expr.<locals>.<genexpr>)opr   funcr   r   _infer_callvalue_infer_getattritemstupler   )r   r*   r.   r0   r   r)   r   r    I   s    



zConstantInference._infer_exprc                    s    j s jr  |tfk}t|to2t|t}|s<|rv fdd jD }|r^|| S |rj||fS dsvt	d  d S )Nc                    s   g | ]}j |j jd qS r$   r%   )r'   ar)   r   r   r+   `   s     z1ConstantInference._infer_call.<locals>.<listcomp>r   ZUnreachable)
kwsvarargr   slicer   typer   BaseExceptionr   AssertionError)r   r.   r*   Z_sliceZ_excr   r   r)   r   r/   Y   s    

zConstantInference._infer_callc                 C   s@   t |ttfr2zt||jW S  tk
r0   Y nX | | d S r   )r   r   r8   r#   attrAttributeErrorr   )r   r0   r*   r   r   r   r1   o   s    z ConstantInference._infer_getattr)N)__name__
__module____qualname____doc__r   r   r   r   r    r/   r1   r   r   r   r   r   	   s   
	r   )
typesr   r   Znumba.core.errorsr   r   Z
numba.corer   objectr   r   r   r   r   <module>   s   