B
     \m                 @   sT   d dl mZmZ d dlmZ d dlZddlmZ ddlm	Z	m
Z
 G dd deZdS )	    )print_functionabsolute_import)
ModuleTypeN   )ir)ConstantInferenceError
NumbaErrorc               @   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   +lib/python3.7/site-packages/numba/consts.py__init__   s    zConstantInference.__init__Nc          
   C   s   || j kr\yd| |f| j |< W n8 tk
rZ } zd|j|jff| j |< W d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   excZ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   _fail1   s    zConstantInference._failc             C   s   t |tstd|f y| j|}W n" tk
rJ   td|f Y nX y| }W n4 tk
r   t |tj	r~| 
|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defnZconstr   r   r   r   :   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 )
NZcall)r   getattrZ
build_listc                s   g | ]}j |j jd qS ))r   )r   r   r   ).0i)exprr   r   r   
<listcomp>T   s    z1ConstantInference._infer_expr.<locals>.<listcomp>Zbuild_tuplec             3   s    | ]}j |j jd V  qdS ))r   N)r   r   r   )r"   r#   )r$   r   r   r   	<genexpr>W   s    z0ConstantInference._infer_expr.<locals>.<genexpr>)opr   funcr   r   _infer_callvalue_infer_getattritemstupler   )r   r$   r(   r*   r   )r$   r   r   r    K   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   r   )r"   a)r$   r   r   r   r%   b   s    z1ConstantInference._infer_call.<locals>.<listcomp>r   ZUnreachable)
ZkwsZvarargr   slicer   typer   BaseExceptionr   AssertionError)r   r(   r$   Z_sliceZ_excr   r   )r$   r   r   r)   [   s    


zConstantInference._infer_callc             C   s(   t |ttfrt||jS | | d S )N)r   r   r0   r!   attrr   )r   r*   r$   r   r   r   r+   q   s    z ConstantInference._infer_getattr)N)__name__
__module____qualname____doc__r   r   r   r   r    r)   r+   r   r   r   r   r	      s   
	r	   )Z
__future__r   r   typesr   r
    r   errorsr   r   objectr	   r   r   r   r   <module>   s
   