o
    8ήc                     @   s*  d Z ddlmZ ddlZddlmZ ddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ z
ddlZe ZW n eyC   dZY nw eduZi Zi Ze Zd	d
 Zdd Zdd Zdadd Zd ddZdd Zd ddZe  Z!e!j"G dd dej#Z$e!j%G dd dej&Z'dd Z(dd Z)dS )!zu
Support for CFFI. Allows checking whether objects are CFFI functions and
obtaining the pointer and numba signature.
    )BuiltinFunctionTypeN)partial)types)TypingError)	templates)numpy_supportc                 C   s,   z| t v p
t| tjW S  ty   Y dS w )NF)_ffi_instances
isinstancecffiFFI	TypeErrorobj r   C/tmp/pip-target-vg8gfxp4/lib/python/numba/core/typing/cffi_utils.pyis_ffi_instance   s
   r   c              	   C   s@   z	t | jdkW S  ty   z| tv W  Y S    Y Y dS w )z(Check whether the obj is a CFFI functionfunctionF)ffitypeofkindr   _ool_func_typesr   r   r   r   is_cffi_func'   s   r   c                 C   s    | t v rt |  S ttd| S )zY
    Get a pointer to the underlying function for a CFFI function as an
    integer.
    	uintptr_t)_ool_func_ptrintr   cast)	cffi_funcr   r   r   get_pointer1   s   r   c                   C   sV  t du ri tdtjtdtjtdtjtdtjtdtjtdtj	tdtj
td	tjtd
tjtdtjtdtjtdtjtdtj
tdtjtdtjtdtjtdtjtdtj	tdtjtdtjtdtjtdtjtdtjtdtjia t S )zc
    Lazily compute type map, as calling ffi.typeof() involves costly
    parsing of C code...
    Nboolcharshortr   longz	long longzunsigned charzunsigned shortzunsigned intzunsigned longzunsigned long longint8_tuint8_tint16_tuint16_tint32_tuint32_tint64_tuint64_tfloatdoublessize_tsize_tvoid)_cached_type_mapr   r   r   booleanr   r    intclong_longlongucharushortuintculong	ulonglongfloat_r+   intpuintpr.   r   r   r   r   	_type_map=   sZ   	
r<   Fc           	         s   t t|d t| dd}|dkrtd|dkr7| jrtd | j} fdd	| jD }tj|g|R  S |d
krM| j	}|j
dkrFtjS t |S |dkra | j	}| j}tj||fdS |dkrk|rkt| S t | }|du ryt| |S )z
    Map CFFI type to numba type.

    Parameters
    ----------
    cffi_type:
        The CFFI type to be converted.
    use_record_dtype: bool (default: False)
        When True, struct types are mapped to a NumPy Record dtype.

    use_record_dtyper    unionzNo support for CFFI unionr   z vararg function is not supportedc                    s   g | ]} |qS r   r   ).0argprimed_map_typer   r   
<listcomp>u   s    zmap_type.<locals>.<listcomp>pointerr.   array)dtypeshapestructN)r   map_typegetattrr   ellipsisresultargsr   	signatureitemr   r   voidptrCPointerlengthNestedArraymap_struct_to_record_dtyper<   get)		cffi_typer>   r   restypeargtypespointeerH   nelemrN   r   rC   r   rK   a   s2   


rK   c                 C   s   g g g t | d}d}| jD ]X\}}|jdkr!d}t|||jdkr/d}t|||jdkr=d}t||t	t
|jdd}|d	 | |d
 | |d |j ||j|j dkM }qttj||dS )z2Convert a cffi type into a NumPy Record dtype
    )namesformatsoffsetsitemsizeTz.field {!r} has bitshift, this is not supportedr   z+field {!r} has flags, this is not supportedz-field {!r} has bitsize, this is not supportedr=   r]   r^   r_   )align)r   sizeoffieldsbitshift
ValueErrorformatflagsbitsizer   as_dtyperK   typeappendoffset	alignment
from_dtypenprH   )rX   rd   
is_alignedkvmsgrH   r   r   r   rV      s0   


rV   c                 C   sF   t | p	t| }t|dddkrtdt||d}tj|t	dS )zB
    Return a Numba type for the given CFFI function pointer.
    r   r?   rJ   z!No support for CFFI struct valuesr=   )r   )
r   rW   r   r   rL   r   rK   r   ExternalFunctionPointerr   )r   r>   rX   sigr   r   r   make_function_type   s
   rw   c                   @   s   e Zd ZdZdd ZdS )FFI_from_bufferzffi.from_bufferc                 C   s   |st |dkr
d S |\}t|tjstd|f |jdvr&td|f |jdkr7|jdkr7td|f t|j}t	
||S )N   z.from_buffer() expected a buffer object, got %s)CFz<from_buffer() unsupported on non-contiguous buffers (got %s)rz   zJfrom_buffer() only supports multidimensional arrays with C layout (got %s))lenr	   r   Bufferr   layoutndimrS   rH   r   rP   )selfrO   kwsaryptrr   r   r   generic   s"   
zFFI_from_buffer.genericN)__name__
__module____qualname__keyr   r   r   r   r   rx      s    rx   c                   @   s   e Zd ZejZdd ZdS )FFIAttributec                 C   s   t tt jS )N)r   BoundFunctionrx   r   )r   r   r   r   r   resolve_from_buffer   s   z FFIAttribute.resolve_from_bufferN)r   r   r   r   r   r   r   r   r   r   r   r      s    r   c                 C   sn   t | jD ]/}t| j|}t|tr.| j|t|< | j| j|j	}t
| jd|t|< t| j qdS )zS
    Add typing for all functions in an out-of-line CFFI module to the typemap
    r   N)dirlibrL   r	   r   r   r   r   	addressofr   r   r   r   r   add)modfaddrr   r   r   register_module   s   
r   c                 C   s   t  }||| < dS )z9
    Add typing for a given CFFI type to the typemap
    N)r<   )rX   
numba_typetmr   r   r   register_type   s   r   )F)*__doc__r   r   ctypes	functoolsr   numpyrp   
numba.corenumba.core.errorsr   numba.core.typingr   numba.npr   r
   r   r   ImportError	SUPPORTEDr   r   setr   r   r   r   r/   r<   rK   rV   rw   RegistryregistryregisterAbstractTemplaterx   register_attrAttributeTemplater   r   r   r   r   r   r   <module>   sD   



$)
!