o
    ۾i                     @   s   d Z ddlZddlmZ ddlmZ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 G d
d deZG dd deZdS )z2
Implementation of compiled C callbacks (@cfunc).
    N)cached_property)compilerregistry)	NullCacheFunctionCache)_FunctionCompiler)	signature	to_ctypes)global_compiler_lockc                   @   s   e Zd Zdd ZdS )_CFuncCompilerc                 C   s*   d|_ d|_d|_d|_|jrtd|S )NTFz&object mode not allowed in C callbacks)no_cpython_wrapperno_cfunc_wrapper
no_compileenable_pyobjectforce_pyobjectNotImplementedError)selfflags r   H/home/ubuntu/.local/lib/python3.10/site-packages/numba/core/ccallback.py_customize_flags   s   z_CFuncCompiler._customize_flagsN)__name__
__module____qualname__r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZejZejfddZ	dd Z
edd Zdd	 Zed
d Zedd Zedd Zedd Zdd Zedd Zdd Zdd ZdS )CFunczD
    A compiled C callback, as created by the @cfunc decorator.
    c                 C   s   |\}}|d u rt d|j| _t|d| j| _|| _|| _t|g|R  | _t|| j	|||d| _
d | _d | _t | _d| _d S )Nz(C callback needs an explicit return typer   )pipeline_classr   )	TypeErrorr   getattrr   __wrapped___pyfuncr   _sigr   _targetdescr	_compiler_wrapper_name_wrapper_addressr   _cache_cache_hits)r   pyfuncsiglocalsoptionsr   argsreturn_typer   r   r   __init__'   s    
zCFunc.__init__c                 C   s   t | j| _d S N)r   r    r&   r   r   r   r   enable_caching;      zCFunc.enable_cachingc                 C   sj   | j | j| jj}|d u r|  }| j | j| n|  jd7  _|j| _	|j
j| _| j	| j| _d S )N   )r&   load_overloadr!   r"   target_context_compile_uncachedsave_overloadr'   library_libraryfndescllvm_cfunc_wrapper_namer$   get_pointer_to_functionr%   )r   cresr   r   r   compile>   s   


zCFunc.compilec                 C   s   | j }| j|j|jS r/   )r!   r#   r>   r,   r-   )r   r)   r   r   r   r6   N   s   zCFunc._compile_uncachedc                 C      | j S )zG
        The process-wide symbol the C callback is exposed as.
        )r$   r0   r   r   r   native_nameT   s   zCFunc.native_namec                 C   r?   )z0
        The address of the C callback.
        )r%   r0   r   r   r   address]   s   zCFunc.addressc                 C   s   ddl }| }|d| jS )zF
        A cffi function pointer representing the C callback.
        r   Nzvoid *)cffiFFIcastrA   )r   rB   ffir   r   r   rB   d   s   z
CFunc.cffic                 C   s:   dd | j jD }t| j j}tj|g|R  }|| jS )zG
        A ctypes function object representing the C callback.
        c                 S   s   g | ]}t |qS r   r	   ).0tyr   r   r   
<listcomp>t   s    z CFunc.ctypes.<locals>.<listcomp>)r!   r,   r
   r-   ctypes	CFUNCTYPErA   )r   ctypes_argsctypes_restypefunctyper   r   r   rI   o   s   
zCFunc.ctypesc                 C   s
   | j  S )zB
        Return the LLVM IR of the C callback definition.
        )r9   get_llvm_strr0   r   r   r   inspect_llvmy   s   
zCFunc.inspect_llvmc                 C   r?   r/   )r'   r0   r   r   r   
cache_hits   s   zCFunc.cache_hitsc                 C   s   d| j f S )Nz<Numba C callback %r>)r   r0   r   r   r   __repr__   s   zCFunc.__repr__c                 O   s   | j |i |S r/   )r    )r   r,   kwargsr   r   r   __call__   r2   zCFunc.__call__N)r   r   r   __doc__r   
cpu_targetr"   r   Compilerr.   r1   r   r>   r6   propertyr@   rA   r   rB   rI   rO   rP   rQ   rS   r   r   r   r   r   !   s,    






	
r   )rT   rI   	functoolsr   
numba.corer   r   numba.core.cachingr   r   numba.core.dispatcherr   numba.core.typingr   numba.core.typing.ctypes_utilsr
   numba.core.compiler_lockr   r   objectr   r   r   r   r   <module>   s    