U
    md+                  
   @   s4  d dl 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 ZedZedZedZed	Zed
ZedZedZedZedZedZedZedZedZedZedZedZedZedZedZedZedZ edZ!edZ"edZ#G dd dZ$G d d! d!e%Z&G d"d# d#e%Z'd$d% Z(e j)dd& d'krVe(nej*Z+e
 Z,d(-e./d(d d) Z0z e+e0e,Z1e2e3e14 Z5W n< e6k
r Z7 zd*e, d+e7 Z8e6e8W 5 dZ7[7X Y nX e&e5Z5d,d- Z9d.d/ Z:G d0d1 d1e%Z;G d2d3 d3e%Z<d4d5 Z=d6d7 Z>G d8d9 d9e%Z?dS ):    N)_decode_string_is_shutting_down)get_library_namec                 C   s   t | tjfi }t|S N)typectypesZ	StructureZPOINTER)nameZnewcls r	   M/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/llvmlite/binding/ffi.py_make_opaque_ref
   s    r   ZLLVMContextZ
LLVMModuleZ	LLVMValueZLLVMTypeZLLVMExecutionEngineZLLVMPassManagerBuilderZLLVMPassManagerZLLVMTargetDataZLLVMTargetLibraryInfoZ
LLVMTargetZLLVMTargetMachineZLLVMMemoryBufferLLVMAttributeListIteratorLLVMAttributeSetIteratorLLVMGlobalsIteratorLLVMFunctionsIteratorLLVMBlocksIteratorLLVMArgumentsIteratorLLVMInstructionsIteratorLLVMOperandsIteratorLLVMTypesIteratorZLLVMObjectCacheZLLVMObjectFileZLLVMSectionIteratorc                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )	_LLVMLocka6  A Lock to guarantee thread-safety for the LLVM C-API.

    This class implements __enter__ and __exit__ for acquiring and releasing
    the lock as a context manager.

    Also, callbacks can be attached so that every time the lock is acquired
    and released the corresponding callbacks will be invoked.
    c                 C   s   t  | _g | _d S r   )	threadingRLock_lock_cblistselfr	   r	   r
   __init__2   s    
z_LLVMLock.__init__c                 C   s   | j ||f dS )zRegister callbacks that are invoked immediately after the lock is
        acquired (``acq_fn()``) and immediately before the lock is released
        (``rel_fn()``).
        N)r   appendr   acq_fnrel_fnr	   r	   r
   register8   s    z_LLVMLock.registerc                 C   s   | j ||f dS )z)Remove the registered callbacks.
        N)r   remover   r	   r	   r
   
unregister?   s    z_LLVMLock.unregisterc                 C   s$   | j   | jD ]\}}|  qd S r   )r   acquirer   r   r	   r	   r
   	__enter__D   s    
z_LLVMLock.__enter__c                 G   s$   | j D ]\}}|  q| j  d S r   )r   r   release)r   exc_detailsr   r    r	   r	   r
   __exit__J   s    z_LLVMLock.__exit__N)	__name__
__module____qualname____doc__r   r!   r#   r%   r(   r	   r	   r	   r
   r   )   s   r   c                   @   sB   e Zd ZdZdddgZdd Zdd Zed	d
 Zedd Z	dS )_lib_wrapperz{Wrap libllvmlite with a lock such that only one thread may access it at
    a time.

    This class duck-types a CDLL.
    _lib_fntabr   c                 C   s   || _ i | _t | _d S r   )r.   r/   r   r   )r   libr	   r	   r
   r   Y   s    z_lib_wrapper.__init__c                 C   sL   z| j | W S  tk
rF   t| j|}t| j|}|| j |< | Y S X d S r   )r/   KeyErrorgetattrr.   _lib_fn_wrapperr   )r   r   cfnwrappedr	   r	   r
   __getattr__^   s    
z_lib_wrapper.__getattr__c                 C   s   | j jS )zgThe name of the library passed in the CDLL constructor.

        For duck-typing a ctypes.CDLL
        )r.   _namer   r	   r	   r
   r7   h   s    z_lib_wrapper._namec                 C   s   | j jS )z]The system handle used to access the library.

        For duck-typing a ctypes.CDLL
        )r.   _handler   r	   r	   r
   r8   p   s    z_lib_wrapper._handleN)
r)   r*   r+   r,   	__slots__r   r6   propertyr7   r8   r	   r	   r	   r
   r-   Q   s   


r-   c                   @   s\   e Zd ZdZddgZdd Zedd Zejdd Zed	d
 Z	e	jdd
 Z	dd Z
dS )r3   zWraps and duck-types a ctypes.CFUNCTYPE to provide
    automatic locking when the wrapped function is called.

    TODO: we can add methods to mark the function as threadsafe
          and remove the locking-step on call when marked.
    r   _cfnc                 C   s   || _ || _d S r   r   r;   )r   lockr4   r	   r	   r
   r      s    z_lib_fn_wrapper.__init__c                 C   s   | j jS r   r;   argtypesr   r	   r	   r
   r?      s    z_lib_fn_wrapper.argtypesc                 C   s   || j _d S r   r>   )r   r?   r	   r	   r
   r?      s    c                 C   s   | j jS r   r;   restyper   r	   r	   r
   rA      s    z_lib_fn_wrapper.restypec                 C   s   || j _d S r   r@   )r   rA   r	   r	   r
   rA      s    c              
   O   s*   | j  | j||W  5 Q R  S Q R X d S r   r<   )r   argskwargsr	   r	   r
   __call__   s    z_lib_fn_wrapper.__call__N)r)   r*   r+   r,   r9   r   r:   r?   setterrA   rD   r	   r	   r	   r
   r3   y   s   



r3   c                 C   s   t t | | S )a  Replacement implementation of `import.resources.path` to avoid
    deprecation warning following code at importlib_resources/_legacy.py
    as suggested by https://importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy

    Notes on differences from importlib.resources implementation:

    The `_common.normalize_path(resource)` call is skipped because it is an
    internal API and it is unnecessary for the use here. What it does is
    ensuring `resource` is a str and that it does not contain path separators.
    )_impresZas_filefiles)packageresourcer	   r	   r
   _importlib_resources_path_repl   s    rJ      )   	   .z(Could not find/load shared object file: z
 Error was: c                 C   s   t j| | dS )z~Register callback functions for lock acquire and release.
    *acq_fn* and *rel_fn* are callables that take no arguments.
    N)r0   r   r!   r   r    r	   r	   r
   register_lock_callback   s    rQ   c                 C   s   t j| | dS )zRemove the registered callback functions for lock acquire and release.
    The arguments are the same as used in `register_lock_callback()`.
    N)r0   r   r#   rP   r	   r	   r
   unregister_lock_callback   s    rR   c                   @   s   e Zd ZdZdS )_DeadPointerz:
    Dummy class to make error messages more helpful.
    N)r)   r*   r+   r,   r	   r	   r	   r
   rS      s   rS   c                   @   sp   e Zd ZdZe Zedd ZdddZdd	 Z	d
d Z
dd ZefddZdd Zdd ZeZedd ZdS )OutputStringz<
    Object for managing the char* output of LLVM APIs.
    c                 C   s   | t |t jdS )a%  Constructing from a pointer returned from the C-API.
        The pointer must be allocated with LLVMPY_CreateString.

        Note
        ----
        Because ctypes auto-converts *restype* of *c_char_p* into a python
        string, we must use *c_void_p* to obtain the raw pointer.
        )init)r   castc_char_p)clsptrr	   r	   r
   from_return   s    
zOutputString.from_returnTNc                 C   s0   |d k	r|nt d | _t | j| _|| _d S r   )r   rW   _ptrZbyref_as_parameter__owned)r   ZownedrU   r	   r	   r
   r      s    zOutputString.__init__c                 C   s*   | j d k	r&| jrt| j  d | _ | `d S r   )r[   r]   r0   ZLLVMPY_DisposeStringr\   r   r	   r	   r
   close   s
    
zOutputString.closec                 C   s   | S r   r	   r   r	   r	   r
   r%      s    zOutputString.__enter__c                 C   s   |    d S r   r^   r   exc_typeexc_valexc_tbr	   r	   r
   r(      s    zOutputString.__exit__c                 C   s   | s| j d k	r|    d S r   r_   r   r   r	   r	   r
   __del__   s    
zOutputString.__del__c                 C   s*   | j d krdS | j j}|d k	s"tt|S )Nz<dead OutputString>)r[   valueAssertionErrorr   )r   sr	   r	   r
   __str__   s
    
zOutputString.__str__c                 C   s
   t | jS r   boolr[   r   r	   r	   r
   __bool__  s    zOutputString.__bool__c                 C   s   | j jS )z:Get the raw bytes of content of the char pointer.
        )r[   rf   r   r	   r	   r
   bytes  s    zOutputString.bytes)TN)r)   r*   r+   r,   rS   r\   classmethodrZ   r   r^   r%   r(   r   re   ri   rl   __nonzero__r:   rm   r	   r	   r	   r
   rT      s   

rT   c                 C   s   | dk	rt t| S dS )z,To wrap string return-value from C-API.
    N)strrT   rZ   rY   r	   r	   r
   
ret_string  s    rr   c                 C   s   | dk	rt | jS dS )z+To wrap bytes return-value from C-API.
    N)rT   rZ   rm   rq   r	   r	   r
   	ret_bytes  s    rs   c                   @   s   e Zd ZdZdZe ZdZdd Zdd Z	dd Z
d	d
 Zedd Zdd Zdd ZefddZdd Zdd ZeZdd ZdS )	ObjectRefzJ
    A wrapper around a ctypes pointer to a LLVM object ("resource").
    Fc                 C   s&   |d krt d|| _|| _t| _d S )NzNULL pointer)
ValueErrorr[   r\   r0   Z_capi)r   rY   r	   r	   r
   r   *  s
    zObjectRef.__init__c                 C   s(   z| js| js|   W 5 |    X dS )zI
        Close this object and do any required clean-up actions.
        N)detach_closedr]   _disposer   r	   r	   r
   r^   1  s    zObjectRef.closec                 C   s   | j s| `d| _ d| _dS )zN
        Detach the underlying LLVM resource without disposing of it.
        TN)rw   r\   r[   r   r	   r	   r
   rv   ;  s    zObjectRef.detachc                 C   s   dS )z
        Dispose of the underlying LLVM resource.  Should be overriden
        by subclasses.  Automatically called by close(), __del__() and
        __exit__() (unless the resource has been detached).
        Nr	   r   r	   r	   r
   rx   D  s    zObjectRef._disposec                 C   s   | j S )zf
        Whether this object has been closed.  A closed object can't
        be used anymore.
        )rw   r   r	   r	   r
   closedK  s    zObjectRef.closedc                 C   s(   t | dst| jr$td| jf | S )Nr^   z%s instance already closed)hasattrrg   rw   RuntimeError	__class__r   r	   r	   r
   r%   S  s    zObjectRef.__enter__c                 C   s   |    d S r   r_   r`   r	   r	   r
   r(   Y  s    zObjectRef.__exit__c                 C   s   | s| j d k	r|    d S r   r_   rd   r	   r	   r
   re   \  s    
zObjectRef.__del__c                 C   s
   t | jS r   rj   r   r	   r	   r
   rl   a  s    zObjectRef.__bool__c                 C   s.   t |dsdS t| jd t|jd kS )Nr[   Fr   )rz   r   Z	addressofr[   )r   otherr	   r	   r
   __eq__d  s
    
zObjectRef.__eq__c                 C   s   t t| jtjjS r   )hashr   rV   r[   Zc_void_prf   r   r	   r	   r
   __hash__m  s    zObjectRef.__hash__N)r)   r*   r+   r,   rw   rS   r\   r]   r   r^   rv   rx   r:   ry   r%   r(   r   re   rl   r~   ro   r   r	   r	   r	   r
   rt   !  s"   
	
rt   )@sysr   r   importlib.resources	resourcesrF   Zllvmlite.binding.commonr   r   Zllvmlite.utilsr   r   ZLLVMContextRefZLLVMModuleRefZLLVMValueRefZLLVMTypeRefZLLVMExecutionEngineRefZLLVMPassManagerBuilderRefZLLVMPassManagerRefZLLVMTargetDataRefZLLVMTargetLibraryInfoRefZLLVMTargetRefZLLVMTargetMachineRefZLLVMMemoryBufferRefr   r   r   r   r   r   r   r   r   ZLLVMObjectCacheRefZLLVMObjectFileRefZLLVMSectionIteratorRefr   objectr-   r3   rJ   version_infopathZ_importlib_resources_pathZ	_lib_namejoinr)   splitpkgnameZ_lib_handleCDLLrp   r%   r0   OSErroremsgrQ   rR   rS   rT   rr   rs   rt   r	   r	   r	   r
   <module>   sp   (("
>