o
    8ήco                     @   s  d dl Z d dlmZ d dlZd dlZd dlZd dlmZ d dl	m
Z
mZmZmZmZmZmZmZmZmZmZ d dlmZmZmZmZ d dlmZmZ d dlmZ d dlm Z m!Z!m"Z" d d	l#m$Z$ d d
l%m&Z& d dlm'Z' eej(dd Z)e
ej*ej+ej+dd Z,e
ej-ej+ej+dd Z.e
ej-ej/ej/dd Z0e
ej-ej1ej1dd Z2e
ej3ej4ej4e
ej3ej5ej5dd Z6e
ej7ej4ej4e
ej7ej5ej5dd Z8dd Z9eej3e9d eej7e9d eej:dd Z;eej+ej:eej<ej:eej1ej:d d! Z=eej:ej+eej:ej1eej:ej<d"d# Z>e
ej?ej@ejAd$d% ZBe
ejCej@ejAej+d&d' ZDd(d) ZEe
eFejGd*d+ ZHe
eFeIej+d,d- ZJe
eKejGd.d/ ZLe
eKeIej+d0d1 ZMd2d3 ZNe
eOejPd4d5 ZQe
eOejPejAd6d7 ZRe
eSej+e
eTej+d8d9 ZUe
eVeIej+d:d; ZWe
ejXej+d<d= ZYeejZd>d? Z[eej\d@dA Z]eej<dBdC Z^e
e_ej+dDdE Z`e
eaejbdFdG Zce
edejedHdI Zfe
dJej+ej+dKdL Zge
ehejidMdN Zje
ekejldOdP Zme
endQdR Zoe
enejGdSdT ZpeekdUdV ZqeekdWdXdYdZ Zrd[d\ Zsd]d^ Zte
etejXe
etejud_d` Zve
esejXe
esejudadb Zwd dclxmyZymzZz d ddl{mZm|Z| e
eyej}ej~e
eyejej~dedf ZeeKdgdh ZeeFdidj Ze|dkdl Ze|dmdl Ze|dndo ZeeKdpdq ZeeFdrds Ze
ejeIej+dtdu Zeed~dvdwZeedxdy Zeedzd{ Zeed|d} ZdS )    N)reduce)ir)lower_builtinlower_getattrlower_getattr_generic
lower_castlower_constantiternext_implcall_getitercall_iternextimpl_ret_borrowedimpl_ret_untrackednumba_typeref_ctor)typingtypesutilscgutils)overload	intrinsic)
Conversion)TypingErrorLoweringErrorNumbaExperimentalFeatureWarning)literal_unroll)as_numba_type)NumbaTypeErrorc                 C   s   t | tjrdd }|S d S )Nc                 S   s   | S N valr   r   =/tmp/pip-target-vg8gfxp4/lib/python/numba/cpython/builtins.pyimpl      zol_truth.<locals>.impl)
isinstancer   Boolean)r   r!   r   r   r    ol_truth   s   r%   c                 C   s   |  tj|}||||S )z3
    Implement `x is not y` as `not (x is y)`.
    )get_functionoperatoris_not_)contextbuildersigargsis_implr   r   r    generic_is_not!   s   r/   c                 C   s`   |j \}}||kr-|jrd}t|z	| tj|}W n ty'   tj Y S w |||S tjS )z-
    Default implementation for `x is y`
    zno default `is` implementation)	r-   mutabler   r&   r'   eqNotImplementedErrorr   	false_bit)r*   r+   r,   r-   lhs_typerhs_typemsgeq_implr   r   r    
generic_is*   s   


r8   c                 C   sJ   |j \}}||kr"||d tj}||d tj}|d||S tjS )z7
    Implementation for `x is y` for Opaque types.
    r      z==)r-   ptrtointr   intp_ticmp_unsignedr3   )r*   r+   r,   r-   r4   r5   lhs_ptrrhs_ptrr   r   r    	opaque_isD   s   
r?   c                 C   sb   |\}}|j \}}| |||tj}| |||tj}	| tjttjtjtj}
|
|||	fS )z
    Implementation for `x is y` for types derived from types.Boolean
    (e.g. BooleanLiteral), and cross-checks between literal and non-literal
    booleans, to satisfy Python's behavior preserving identity for bools.
    )	r-   castr   booleanr&   r'   r1   r   	signature)r*   r+   r,   r-   arg1arg2	arg1_type	arg2_type_arg1_arg2r7   r   r   r    bool_is_implT   s   
rI   c                 C   s@   |j \}}d}|j|jkrd}ttd|}t| ||j|S Nr   r9   r-   literal_valuer   ConstantIntTyper   return_typer*   r+   r,   r-   rC   rD   r   resr   r   r    const_eq_implg      
rR   c                 C   s@   |j \}}d}|j|jkrd}ttd|}t| ||j|S rJ   rK   rP   r   r   r    const_ne_impls   rS   rT   c                    s    fdd}|S )Nc                    sL   t | tj}t |tj}|r|r fdd}|S ||A r$ fdd}|S d S )Nc                    s    S r   r   abr   r   r    r!      r"   z/gen_non_eq.<locals>.none_equality.<locals>.implc                    s     S r   r   rU   r   r   r    r!      s   r#   r   NoneType)rV   rW   a_noneb_noner!   r   r   r    none_equality   s   z!gen_non_eq.<locals>.none_equalityr   )r   r\   r   r   r    
gen_non_eq~   s   r]   TFc                 C   s4   |  }| ||||}| ||}|| ||||S )z>
    Deferred.__getattr__ => redirect to the actual type.
    )getr@   get_getattr)r*   r+   typvalueattr
inner_typer   impr   r   r    deferred_getattr   s   re   c                 C   s0   |  |||| }| j| }||| |S r   )r@   r^   data_model_managersetmake_uninitialized)r*   r+   fromtytotyr   actualmodelr   r   r    any_to_deferred   s   
rm   c                 C   s*   | j | }|||}| ||| |S r   )rf   r^   r@   )r*   r+   ri   rj   r   rl   r   r   r    deferred_to_any   s   
rn   c                 C   s0   |\}}| ||g}||}t| ||j|S r   )geploadr   rO   )r*   r+   r,   r-   base_ptridxelem_ptrrQ   r   r   r    getitem_cpointer   s   
rt   c                 C   s(   |\}}}| ||g}||| d S r   )ro   store)r*   r+   r,   r-   rq   rr   r   rs   r   r   r    setitem_cpointer   s   
rv   c           	         sX   t |t |ksJ ||ft |dksJ  fdd}t||}t||\}}|S )Nr   c                    s   | \}}|\}}j ||}|d usJ  |||} |||}ttj||}|}| ||f}	 |	||}
||
fS r   )	typing_contextunify_typesr@   r   rB   r   rA   r&   select)accumulatorra   acctyaccvtyvtycmpsiggepredrQ   r+   cmpopr*   r   r    binary_minmax   s   z do_minmax.<locals>.binary_minmax)lenzipr   )	r*   r+   argtysr-   r   r   typvalsrestyresvalr   r   r    	do_minmax   s   
r   c                 C   0   t |jd }t||d }t| |||tjS Nr   )listr-   r   unpack_tupler   r'   gtr*   r+   r,   r-   r   r   r   r    max_iterable      r   c                 C      t | ||j|tjS r   )r   r-   r'   r   r*   r+   r,   r-   r   r   r    
max_vararg      r   c                 C   r   r   )r   r-   r   r   r   r'   ltr   r   r   r    min_iterable   r   r   c                 C   r   r   )r   r-   r'   r   r   r   r   r    
min_vararg   r   r   c                 C   s   d| j f S )Nzllvm.rint.f%d)bitwidth)tpr   r   r    _round_intrinsic   s   r   c           
      C   sj   |j d }| |}|j}t||g}t||t|}|||}	|	|	| |j
}	t| ||j
|	S r   )r-   get_value_typemoduler   FunctionTyper   get_or_insert_functionr   callfptosirO   r   )
r*   r+   r,   r-   flttylltyr   fntyfnrQ   r   r   r    round_impl_unary   s   

r   c                    sN   |j d }tt|t||  fdd}| ||||}t| ||j|S )Nr   c                    s   t | s
t | r| S |dkr8|dkrd|d  }d}nd| }d}| | | }t |r0| S  || | S d|  }| | } || S )Nr      g      $@gMDg      ?)mathisinfisnan)xndigitspow1pow2y_roundr   r    round_ndigits  s   

z(round_impl_binary.<locals>.round_ndigits)	r-   r   ExternalFunctionr   r   rB   compile_internalr   rO   )r*   r+   r,   r-   r   r   rQ   r   r   r    round_impl_binary   s   
r   c                 C   s0   |j \}|\}| ||||j}t| ||j|S r   )r-   r@   rO   r   )r*   r+   r,   r-   r   r   rQ   r   r   r    int_impl"  s   r   c                 C   s   |j }|j}t|jdkr9|j\}|\}t|tjr*| ||||}t| ||j |S | ||||}	| 	|d}
n t|jdkrY|j\}}|\}	}
| ||	||}	| ||
||}
| 
||}|	|_|
|_| }t| ||j |S )Nr9   r      )rO   underlying_floatr   r-   r#   r   Complexr@   r   get_constantmake_complexrealimag	_getvalue)r*   r+   r,   r-   complex_type
float_typeargtyargrQ   r   r   realtyimagtycmplxr   r   r    complex_impl+  s(   
r   c                    sb   t |jtjr!|jj  fdd}| ||||}t| ||j|S |\}|j\}| ||||jS )z1
    Call a number class, e.g. np.int32(...)
    c                     s   t j|  dS )N)dtype)nparray)arg_hackdtr   r    fooO  s   znumber_constructor.<locals>.foo)	r#   rO   r   Arrayr   r   r   r-   r@   )r*   r+   r,   r-   r   rQ   r   valtyr   r   r    number_constructorG  s   r   c                 C      |   S r   get_dummy_valuer*   r+   r   pyvalr   r   r    constant_dummy]  s   r   c                 C   s0   |  |}| j|||t|d}|||S )N)info)get_function_pointer_typeadd_dynamic_addrget_pointerstrbitcast)r*   r+   r   r   ptrtyptrvalr   r   r    constant_function_pointerb  s
   
r   c                 C   s&   |d u r|  ||jS | ||j|S r   )make_optional_nonetypemake_optional_valuer   r   r   r    constant_optionalj  s   r   c                 C   r   )z&
    One-argument type() builtin.
    r   r   r   r   r    	type_implt  s   r   c                 C   s    |j \}|\}t| |||}|S r   )r-   r
   )r*   r+   r,   r-   r   r   itervalr   r   r    	iter_impl|  s   r   c                 C   sr   |j \}|\}t| |||}|j|| dd | j|td W d    | S 1 s0w   Y  | S )NF)likelyr   )	r-   r   if_thenr)   is_valid	call_convreturn_user_excStopIterationyielded_value)r*   r+   r,   r-   itertyr   rQ   r   r   r    	next_impl  s   
r   znot inc                 C   s"   dd }|  ||||}||S )Nc                 S   s   t || S r   )r'   containsrU   r   r   r    in_impl     znot_in.<locals>.in_impl)r   r)   )r*   r+   r,   r-   r   rQ   r   r   r    not_in  s   
r   c                 C   s0   |j \}|j}| |t|j}t| ||j|S r   )r-   rO   r   r   r   r   )r*   r+   r,   r-   r   rettyrQ   r   r   r    constsized_len  s   r   c                 C   s   |j \}t|rtjS tjS r   )r-   r   r   true_bitr3   )r*   r+   r,   r-   r   r   r   r    
sized_bool  s   r   c                 C   s    |j }| |}t| ||j |S r   )rO   get_constant_undefr   )r*   r+   r,   r-   r   rQ   r   r   r    lower_empty_tuple  s   
r   c                 C   s   |\}t | ||j|S r   )r   rO   )r*   r+   r,   r-   r   r   r   r    lower_tuple  s   r   c                 C   s6   t jt jt jt jt jt jf}t| |rdd }|S d S )Nc                 S   s   t | dkS r   )r   r   r   r   r    	bool_impl  r   z bool_sequence.<locals>.bool_impl)r   CharSeqUnicodeCharSeqDictTypeListTypeUnicodeTypeSetr#   )r   valid_typesr   r   r   r    bool_sequence  s   
	r  always)inlinec                 C   s    t | tjs
| d u rdd S d S )Nc                 S      dS NFr   r   r   r   r    <lambda>  s    zbool_none.<locals>.<lambda>rX   r   r   r   r    	bool_none  s   r  c                 C   s,   t | tjr	tjS t | tjr| jS tdNzUnsupported type)r#   r   Floatr   infIntegermaxvalr2   r`   r   r   r    get_type_max_value  s
   r  c                 C   s.   t | tjr
tj S t | tjr| jS tdr  )r#   r   r  r   r  r  minvalr2   r  r   r   r    get_type_min_value  s
   r  c           
      C   s   |j d j}t|tjr|j}t|}|j}t	||}nLt|tj
rN|j}|dkr0t }n|dkr9t }ntdttd|}	t	|tj }nt|tjtjfrjd}t|}tjjd }t	||}t| |||S )Nr       @   +llvmlite only supports 32 and 64 bit floatsfloat{}r9   )r-   r   r#   r   r  r   r   rN   r  rM   r  	FloatType
DoubleTyper2   getattrr   formatr  
NPDatetimeNPTimedeltaint64r   
r*   r+   r,   r-   r`   bwltyr   rQ   nptyr   r   r    lower_get_type_min_value  s*   



r&  c           
      C   s   |j d j}t|tjr|j}t|}|j}t	||}nIt|tj
rM|j}|dkr0t }n|dkr9t }ntdttd|}	t	|tj}nt|tjtjfrgd}t|}tjj}t	||}t| |||S )Nr   r  r  r  r  )r-   r   r#   r   r  r   r   rN   r  rM   r  r  r  r2   r  r   r  r  r  r   r!  r   r"  r   r   r    lower_get_type_max_value  s*   



r'  )
IndexValueIndexValueType)r   register_jitablec                 C   s2   |j }|\}}t|| |}||_||_| S r   )rO   r   create_struct_proxyindexra   r   )r*   r+   r,   r-   r`   r,  ra   index_valuer   r   r    impl_index_value  s   r.  c                 C   (   t | trt |trdd }|S d S d S )Nc                 S   st   t | jrt |jr| j|jk r| S |S | S t |jr |S | j|jkr(|S | j|jkr8| j|jk r6| S |S | S r   r   r   ra   r,  indval1indval2r   r   r    min_impl(     zindval_min.<locals>.min_implr#   r)  )r2  r3  r4  r   r   r    
indval_min$     
r7  c                 C   r/  )Nc                 S   st   t | jrt |jr| j|jk r| S |S | S t |jr |S |j| jkr(|S | j|jkr8| j|jk r6| S |S | S r   r0  r1  r   r   r    max_implE  r5  zindval_max.<locals>.max_implr6  )r2  r3  r9  r   r   r    
indval_maxA  r8  r:  c                 C   s   | |kS r   r   rU   r   r   r    r  ^      r  c                 C   s   | |k S r   r   rU   r   r   r    r  _  r;  c                    s    t | tjr fdd}|S d S )Nc                    s,   t | }t|}|D ]	} ||r|}q
|S r   )iternext)iterableit
return_valr   opr   r    r!   e  s   
zmin_max_impl.<locals>.impl)r#   r   IterableType)r>  rB  r!   r   rA  r    min_max_implb  s   rD  c                 C   
   t | tS r   )rD  	less_thanr>  r   r   r    iterable_mino     
rH  c                 C   rE  r   )rD  greater_thanrG  r   r   r    iterable_maxt  rI  rK  c                 C   sz   |j }dd }tj|j}t|t||}t|dkr*| 	 | 
|||f}n| 	 | 
||df}| ||||S )ae  Redirect constructor implementation to `numba_typeref_ctor(cls, *args)`,
    which should be overloaded by the type's implementation.

    For example:

        d = Dict()

    `d` will be typed as `TypeRef[DictType]()`.  Thus, it will call into this
    implementation.  We need to redirect the lowering to a function
    named ``numba_typeref_ctor``.
    c                 W   s   t | g|R  S r   )r   )clsr-   r   r   r    	call_ctor  s   z%redirect_type_ctor.<locals>.call_ctorr   r   )rO   r   Tuple
from_typesr-   r   rB   TypeRefr   r   
make_tupler   )r*   r+   r,   r-   rL  rM  	ctor_argsr   r   r    redirect_type_ctory  s   rS  c                    s   d }t |tjrd}nt |tjrd}nt |tjrd}|d ur)dj| }t|t | tjjtj	tj
tjtjfr<t nt | tjjrFt nd S d fdd	}|S )	N)strings )bytesrW   )	bytearrayrW   z/sum() can't sum {} [use {}''.join(seq) instead]r   c                    s   |} | D ]}|| }q|S r   r   )r>  startr|   r   iteratorr   r    r!     s   
zol_sum.<locals>.implr   )r#   r   r  Bytes	ByteArrayr  r   
containers_HomogeneousTupleListr  r   	RangeTyper<  _HeterogeneousTupler   )r>  rX  errorr6   r!   r   rY  r    ol_sum  s&   
rd  c                 G   s   dd }|S )Nc                 w   s&    t |g|R  D ]}| | V  q	d S r   )r   )funcr>  r-   r   r   r   r    r!     s   zol_map.<locals>.implr   )re  r>  r-   r!   r   r   r    ol_map  s   rf  c                 C   s,   | d u s
t | tjrdd }|S dd }|S )Nc                 s   s    |D ]}|r
|V  qd S r   r   re  r>  r   r   r   r    r!     s   zol_filter.<locals>.implc                 s   s    |D ]	}| |r|V  qd S r   r   rg  r   r   r    r!     s   rX   )re  r>  r!   r   r   r    	ol_filter  s
   	rh  c                 C   s@  dd }dd }t | }t|tjrd| d}t|tjtjtjtjtj	tj
tjtjtjtjtjtjtjtjtjtjf}t||sMd| d}t|d	}tj|td
 |}t|tjrc|jd }t|tjsl|f}|D ]}t|tjr||jd }	nt|tjr|}	n|j}	ttjttjttjtj	fttj
ttjttji}
|	|
v rt||
|	 r|  S qnt|tj r|	tjtjfvrd}t|t!||	u r|  S |  S t |	}||kr|  S t|tjrt|tjr|j|j"jkr|  S t|tj#r|jd tj$krt||j%st|tjrt|tjr|  S qn|S )Nc                 S   r
  )NTr   vartypsr   r   r    	true_impl  r"   z ol_isinstance.<locals>.true_implc                 S   r
  r  r   ri  r   r   r    
false_impl  r"   z!ol_isinstance.<locals>.false_implz1isinstance cannot handle optional types. Found: ""z1isinstance() does not support variables of type "z".z>Use of isinstance() detected. This is an experimental feature.)categoryr   zLNumba type classes (except numba.typed.* container types) are not supported.)&r   r#   r   Optionalr   Numberr\  ra  r  LiteralStrKeyDictr`  r  rN  UniTupler  Function	ClassTyper  ClassInstanceTyperY   r   warningswarnr   keyrV  rangedictr   tuple	BaseTuplerg   rP  r   instance_type	Container	undefined	__class__)rj  rk  rl  rm  var_tyr6   supported_var_tyt_typsr`   ry  types_not_registered	numba_typr   r   r    ol_isinstance  s   




r  r[  )r   	functoolsr   numpyr   r'   rw  llvmliter   numba.core.imputilsr   r   r   r   r   r	   r
   r   r   r   r   
numba.corer   r   r   r   numba.core.extendingr   r   numba.core.typeconvr   numba.core.errorsr   r   r   numba.misc.specialr   numba.core.typing.asnumbatyper   r   truthr%   is_notAnyr/   r(   r8   Opaquer?   r$   rI   r1   LiteralIntegerLiteralrR   nerT   r]   DeferredTypere   rp  rm   rn   getitemCPointerr  rt   setitemrv   r   maxr}  r   VarArgr   minr   r   r   roundr  r   r   intfloatr   complexr   NumberClassr   Dummyr   ExternalFunctionPointerr   r   r   r   r<  rC  r   r=  IteratorTyper   r   r   
ConstSizedr   boolSizedr   r|  r   r   r  r  r  r  DTyper&  r'  numba.core.typing.builtinsr(  r)  numba.extendingr*  intpTypeuintpr.  r7  r:  rJ  rF  rD  rH  rK  rP  rS  sumrd  maprf  filterrh  r#   r  r   r   r   r    <module>   s   4





	
	











%






	




















	





'

