o
    8ήcH                     @   s   d Z ddlZddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZmZ d
d Zedd Zdd Zdd Zedd Zdd Zdd Zdd Zdd ZdS )z: Common compiler level utilities for typed dict and list.     N)ir)typescgutils)typing)
cpu_target)
Conversion)	intrinsic)TypingErrorNumbaTypeSafetyWarningc                 C   s   |  |tjS )zHelper to do (void*)ptr
    )bitcastr   	voidptr_t)builderptr r   C/tmp/pip-target-vg8gfxp4/lib/python/numba/typed/typedobjectutils.py	_as_bytes   s   r   c                 C   s*   dd }|j }t|| |||}||fS )zCast *val* to *typ*
    c                 S   s   |\}}| j ||j| |S Nnrtincrefreturn_type)contextr   	signatureargsvaltypr   r   r   codegen   s   z_cast.<locals>.codegen)instance_type_sentry_safe_cast)	typingctxr   r   r   castedsigr   r   r   _cast   s
   

r"   c           
         s  t j}ttj f\ | } fdd}dd }dd }dd }dd }|du s3|tjkr| r@|r@|  dS | rM|rM|  dS | rZ|rZ|  dS | rg|rg|  dS | rq|rqdS ttj	s||  dS d	}	t
|	 dS )
zLCheck and raise TypingError if *fromty* cannot be safely cast to *toty*
    c                     s   d} t j|  td d S )Nz1unsafe cast from {} to {}. Precision may be lost.)category)warningswarnformatr
   )mfromtytotyr   r   r%   ,   s   
z_sentry_safe_cast.<locals>.warnc                 S      t | tjS r   )
isinstancer   Integerxr   r   r   <lambda>1       z#_sentry_safe_cast.<locals>.<lambda>c                 S   r+   r   )r,   r   Floatr.   r   r   r   r0   2   r1   c                 S   r+   r   )r,   r   Complexr.   r   r   r   r0   3   r1   c                 S   r+   r   )r,   r   DictTyper.   r   r   r   r0   4   r1   Nz4cannot safely cast {} to {}. Please cast explicitly.)r   typing_contextmapr   	unliteralcan_convertr   safer,   Numberr	   r&   )
r)   r*   tyctxtbyr%   isintisfltiscmplxisdictr'   r   r(   r   r   %   s0   




r   c                 C   s,   | du rdS t | tjtjfrdS t| |S )z;Similar to _sentry_safe_cast but handle default value.
    N)r,   r   OmittedNoneTyper   )defaultvaltyr   r   r   _sentry_safe_cast_defaultN   s
   
rE   c                 C   s2   t |tjs
tddd }|j}||}||fS )z*Typing trick to cast Optional[T] to T
    zexpected an optionalc                 S   s   | j ||j|d  |d S )Nr   r   )r   r   r!   r   r   r   r   r   a   s   z_nonoptional.<locals>.codegen)r,   r   Optional	TypeErrortype)r   r   r   r    r!   r   r   r   _nonoptionalZ   s   rI   c                 C      t |}|| ||d}|jS )z<Helper to get the C list pointer in a numba containers.
    value)r   create_struct_proxydatar   r   container_tycctorconatainer_structr   r   r   _container_get_dataj      
rT   c                 C   rJ   )z.Helper to get the meminfo for a container
    rK   )r   rM   meminforO   r   r   r   _container_get_meminfor   rU   rW   c           
   	   C   s   |  sJ |j}|  }tt |g}t||d	| j
j|}t| }| j|||||jd  |  tj||d	| j
j|d}	t|	 }| j|||||	jd  |  ||	fS )Nz.numba_{}.{}_increfr   z.numba_{}.{}_decrefname)contains_nrt_meminfofe_typeget_data_type
as_pointerr   FunctionTypeVoidTyper   get_or_insert_functionr&   fndescmangled_name	IRBuilderappend_basic_blockr   r   load_from_data_pointerr   ret_voiddecref)
r   module	datamodelcontainer_element_typer[   data_ptr_ty
refct_fnty	incref_fnr   	decref_fnr   r   r   _get_incref_decrefz   s6   ro   c              	      s  |  sJ |j|  }jtjg}g  fdd}tj	||d
jj|d}|| ttd||g}tj	||d
jj|d}	t|	 }
||
|	jd }||
|	jd }j|
|tj ||g\}}|
j|jd	d
7 |
|j |
tjd W d    n1 sw   Y  |
|tjtj}|
| W d    n1 sw   Y  |
tjd |	S )Nc           	         s   t |  }j| | }ttj}t	j
}j|}|j|ji  ||}|||}||tjtj}j|| d S r   )r   rc   rd   	call_convdecode_argumentsr   r   r   booleanoperatoreqr5   resolve_value_typeget_call_typer   get_functioncastint32return_value)	fnr   r   r!   opfnopeqfnresintresargtypesr   r[   r   r   build_wrapper   s   
z!_get_equal.<locals>.build_wrapperz.numba_{}.{}_equal.wraprX       z.numba_{}.{}_equalr      T)likely)rZ   r[   r\   r]   rp   get_function_typer   ry   r   r`   r&   ra   rb   r   r^   IntTyperc   rd   re   r   call_functionrr   if_thenis_okis_noneretget_constantrx   )r   rh   ri   rj   rk   wrapfntyr   wrapfn
equal_fntyequal_fnr   lhsrhsstatusretvalr   r   r   
_get_equal   sF   
r   )__doc__rs   r$   llvmliter   
numba.corer   r   r   numba.core.registryr   numba.core.typeconvr   numba.core.extendingr   numba.core.errorsr	   r
   r   r"   r   rE   rI   rT   rW   ro   r   r   r   r   r   <module>   s*    
)
