o
    !&i2                     @   s
  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#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*Z.G d+d, d,e/Z0G d-d. d.e/Z1d/d0 Z2e j3dd1 d2kre2nej4Z5e0 Z6d3d4 Z7d5d6 Z8G d7d8 d8e/Z9G d9d: d:e/Z:d;d< Z;d=d> Z<G d?d@ d@e/Z=dS )A    N)_decode_string_is_shutting_down)get_library_namec                 C   s   t | tjfi }t|S N)typectypes	StructurePOINTER)nameZnewcls r   ?C:\wamp64\www\opt\env\Lib\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LLVMElementIteratorLLVMAttributeSetIteratorLLVMGlobalsIteratorLLVMFunctionsIteratorLLVMBlocksIteratorLLVMArgumentsIteratorLLVMInstructionsIteratorLLVMOperandsIteratorLLVMIncomingBlocksIteratorLLVMTypesIteratorZLLVMObjectCacheZLLVMObjectFileZLLVMSectionIteratorLLVMOrcLLJITRefLLVMOrcDylibTrackerRefZLLVMTimePassesHandlerZLLVMPipeLineTuningOptionsZLLVMModulePassManagerZLLVMFunctionPassManagerZLLVMPassBuilderc                   @   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__;   s   

z_LLVMLock.__init__c                 C      | 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   registerA   s   z_LLVMLock.registerc                 C   r#   )z)Remove the registered callbacks.
        N)r   remover%   r   r   r   
unregisterH   s   z_LLVMLock.unregisterc                 C   s$   | j   | jD ]\}}|  qd S r   )r   acquirer   r%   r   r   r   	__enter__M   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__S   s   z_LLVMLock.__exit__N)	__name__
__module____qualname____doc__r"   r(   r*   r,   r/   r   r   r   r   r   2   s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )_suppress_cleanup_errorsc                 C   s
   || _ d S r   )_context)r!   contextr   r   r   r"   [      
z!_suppress_cleanup_errors.__init__c                 C   s
   | j  S r   )r5   r,   r    r   r   r   r,   ^   r7   z"_suppress_cleanup_errors.__enter__c                 C   s(   z	| j |||W S  ty   Y d S w r   )r5   r/   PermissionError)r!   exc_type	exc_value	tracebackr   r   r   r/   a   s
   z!_suppress_cleanup_errors.__exit__N)r0   r1   r2   r"   r,   r/   r   r   r   r   r4   Z   s    r4   c                   @   sT   e Zd ZdZg dZdd Zdd Ze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_handle_fntabr   c                 C   s   d | _ i | _t | _d S r   )r=   r>   r   r   r    r   r   r   r"   p   s   z_lib_wrapper.__init__c              
   C   s   z/t ttdd t }tt|| _| j	 }W d    W d S 1 s(w   Y  W d S  t
tfyB } zt
d|d }~ww )N.r   z&Could not find/load shared object file)r4   _importlib_resources_pathr0   
rpartitionr   r   CDLLstrr=   ZLLVMPY_GetVersionInfoOSErrorAttributeError)r!   Zlib_path_er   r   r   	_load_libu   s   &
z_lib_wrapper._load_libc                 C   s   | j s|   | j S r   )r=   rH   r    r   r   r   _lib   s   z_lib_wrapper._libc                 C   sF   z| j | W S  ty"   t| j|}t| j|}|| j |< | Y S w r   )r>   KeyErrorgetattrrI   _lib_fn_wrapperr   )r!   r
   cfnwrappedr   r   r   __getattr__   s   
z_lib_wrapper.__getattr__c                 C      | j jS )zgThe name of the library passed in the CDLL constructor.

        For duck-typing a ctypes.CDLL
        )rI   _namer    r   r   r   rQ         z_lib_wrapper._namec                 C   rP   )z]The system handle used to access the library.

        For duck-typing a ctypes.CDLL
        )rI   _handler    r   r   r   rS      rR   z_lib_wrapper._handleN)r0   r1   r2   r3   	__slots__r"   rH   propertyrI   rO   rQ   rS   r   r   r   r   r<   h   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 )rL   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   rV   )r!   lockrM   r   r   r   r"      s   
z_lib_fn_wrapper.__init__c                 C   rP   r   rV   argtypesr    r   r   r   rZ         z_lib_fn_wrapper.argtypesc                 C      || j _d S r   rY   )r!   rZ   r   r   r   rZ         c                 C   rP   r   rV   restyper    r   r   r   r_      r[   z_lib_fn_wrapper.restypec                 C   r\   r   r^   )r!   r_   r   r   r   r_      r]   c                 O   s:   | j  | j|i |W  d    S 1 sw   Y  d S r   rW   )r!   argskwargsr   r   r   __call__   s   $z_lib_fn_wrapper.__call__N)r0   r1   r2   r3   rT   r"   rU   rZ   setterr_   rb   r   r   r   r   rL      s    



rL   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.
    )_impresas_filefiles)packageresourcer   r   r   _importlib_resources_path_repl   s   ri      )   
   c                 C      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)libr   r(   r&   r'   r   r   r   register_lock_callback      rp   c                 C   rm   )zRemove the registered callback functions for lock acquire and release.
    The arguments are the same as used in `register_lock_callback()`.
    N)rn   r   r*   ro   r   r   r   unregister_lock_callback   rq   rr   c                   @   s   e Zd ZdZdS )_DeadPointerz:
    Dummy class to make error messages more helpful.
    N)r0   r1   r2   r3   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 ur|nt d | _t | j| _|| _d S r   )r   rw   _ptrbyref_as_parameter__owned)r!   Zownedru   r   r   r   r"     s   
zOutputString.__init__c                 C   s.   | j d ur| jrt| j  d | _ | `d S d S r   )r{   r~   rn   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      |    d S r   r   r!   r9   exc_valexc_tbr   r   r   r/        zOutputString.__exit__c                 C   $   | s| j d ur|    d S d S d S r   r   r!   r   r   r   r   __del__  s
   
zOutputString.__del__c                 C   s*   | j d u rdS | j j}|d usJ t|S )Nz<dead OutputString>)r{   valuer   )r!   sr   r   r   __str__  s
   
zOutputString.__str__c                 C   
   t | jS r   boolr{   r    r   r   r   __bool__$  r7   zOutputString.__bool__c                 C   rP   )z:Get the raw bytes of content of the char pointer.
        )r{   r   r    r   r   r   bytes)  s   zOutputString.bytes)TN)r0   r1   r2   r3   rs   r}   classmethodrz   r"   r   r,   r/   r   r   r   r   __nonzero__rU   r   r   r   r   r   rt      s    

rt   c                 C   s   | durt t| S dS )z,To wrap string return-value from C-API.
    N)rC   rt   rz   ry   r   r   r   
ret_string0  s   r   c                 C   s   | dur
t | jS dS )z+To wrap bytes return-value from C-API.
    N)rt   rz   r   r   r   r   r   	ret_bytes7  s   r   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 u rt d|| _|| _t| _d S )NzNULL pointer)
ValueErrorr{   r}   rn   Z_capi)r!   ry   r   r   r   r"   G  s
   
zObjectRef.__init__c                 C   sJ   z| j s| js|   W |   dS W |   dS W |   dS |   w )zI
        Close this object and do any required clean-up actions.
        N)_closedr~   _disposedetachr    r   r   r   r   N  s   
zObjectRef.closec                 C   s   | j s| `d| _ d| _dS dS )zN
        Detach the underlying LLVM resource without disposing of it.
        TN)r   r}   r{   r    r   r   r   r   X  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   r   a  s    zObjectRef._disposec                 C   s   | j S )zf
        Whether this object has been closed.  A closed object can't
        be used anymore.
        )r   r    r   r   r   closedh  s   zObjectRef.closedc                 C   s(   t | dsJ | jrtd| jf | S )Nr   z%s instance already closed)hasattrr   RuntimeError	__class__r    r   r   r   r,   p  s   zObjectRef.__enter__c                 C   r   r   r   r   r   r   r   r/   v  r   zObjectRef.__exit__c                 C   r   r   r   r   r   r   r   r   y  s
   
zObjectRef.__del__c                 C   r   r   r   r    r   r   r   r   ~  r7   zObjectRef.__bool__c                 C   s.   t |dsdS t| jd t|jd kS )Nr{   Fr   )r   r   	addressofr{   )r!   otherr   r   r   __eq__  s
   
zObjectRef.__eq__c                 C   s   t t| jtjjS r   )hashr   rv   r{   c_void_pr   r    r   r   r   __hash__  s   zObjectRef.__hash__N)r0   r1   r2   r3   r   rs   r}   r~   r"   r   r   r   rU   r   r,   r/   r   r   r   r   r   r   r   r   r   r   r   >  s$    
	
r   )>sysr   r   importlib.resources	resourcesrd   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   r   r   ZLLVMObjectCacheRefZLLVMObjectFileRefZLLVMSectionIteratorRefr   r   ZLLVMTimePassesHandlerRefZLLVMPipelineTuningOptionsRefZLLVMModulePassManagerRefZLLVMFunctionPassManagerRefZLLVMPassBuilderRefr   r4   objectr<   rL   ri   version_infopathr@   rn   rp   rr   rs   rt   r   r   r   r   r   r   r   <module>   sr    (>">