o
    yik3                     @   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)namenewcls r   W/home/ubuntu/maya3_transcribe/venv/lib/python3.10/site-packages/llvmlite/binding/ffi.py_make_opaque_ref
   s   
r   LLVMContext
LLVMModule	LLVMValueLLVMTypeLLVMExecutionEngineLLVMPassManagerBuilderLLVMPassManagerLLVMTargetDataLLVMTargetLibraryInfo
LLVMTargetLLVMTargetMachineLLVMMemoryBufferLLVMAttributeListIteratorLLVMElementIteratorLLVMAttributeSetIteratorLLVMGlobalsIteratorLLVMFunctionsIteratorLLVMBlocksIteratorLLVMArgumentsIteratorLLVMInstructionsIteratorLLVMOperandsIteratorLLVMIncomingBlocksIteratorLLVMTypesIteratorLLVMObjectCacheLLVMObjectFileLLVMSectionIteratorLLVMOrcLLJITRefLLVMOrcDylibTrackerRefLLVMTimePassesHandlerLLVMPipeLineTuningOptionsLLVMModulePassManagerLLVMFunctionPassManager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)r4   appendr6   acq_fnrel_fnr   r   r   registerA   s   z_LLVMLock.registerc                 C   r8   )z)Remove the registered callbacks.
        N)r4   remover:   r   r   r   
unregisterH   s   z_LLVMLock.unregisterc                 C   s$   | j   | jD ]\}}|  qd S r   )r3   acquirer4   r:   r   r   r   	__enter__M   s   
z_LLVMLock.__enter__c                 G   s$   | j D ]\}}|  q| j  d S r   )r4   r3   release)r6   exc_detailsr;   r<   r   r   r   __exit__S   s   z_LLVMLock.__exit__N)	__name__
__module____qualname____doc__r7   r=   r?   rA   rD   r   r   r   r   r0   2   s    r0   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)r6   contextr   r   r   r7   [      
z!_suppress_cleanup_errors.__init__c                 C   s
   | j  S r   )rJ   rA   r5   r   r   r   rA   ^   rL   z"_suppress_cleanup_errors.__enter__c                 C   s(   z	| j |||W S  ty   Y d S w r   )rJ   rD   PermissionError)r6   exc_type	exc_value	tracebackr   r   r   rD   a   s
   z!_suppress_cleanup_errors.__exit__N)rE   rF   rG   r7   rA   rD   r   r   r   r   rI   Z   s    rI   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_fntabr3   c                 C   s   d | _ i | _t | _d S r   )rR   rS   r0   r3   r5   r   r   r   r7   p   s   z_lib_wrapper.__init__c              	   C   s   d}t dd }t }tt||B}ztt|| _t	| j|  W n' t
y9   d| d| d}t
| tyL   d| d| d	}t
|w W d    d S 1 sXw   Y  d S )
NLLVMPY_GetVersionInfo.r   z(Could not find/load shared object file 'z' from resource location: 'z'. This could mean that the library literally cannot be found, but may also mean that the permissions are incorrect or that a dependency of/a symbol in the library could not be resolved.z-During testing of symbol lookup, the symbol 'z%' could not be found in the library '')rE   
rpartitionr   rI   _importlib_resources_pathr   CDLLstrrR   getattrOSErrorAttributeError)r6   test_symmod_namelib_namelib_pathmsgr   r   r   	_load_libu   s2   "z_lib_wrapper._load_libc                 C   s   | j s|   | j S r   )rR   rc   r5   r   r   r   _lib   s   z_lib_wrapper._libc                 C   sF   z| j | W S  ty   Y nw t| j|}t| j|}|| j |< |S r   )rS   KeyErrorr[   rd   _lib_fn_wrapperr3   )r6   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
        )rd   _namer5   r   r   r   rk         z_lib_wrapper._namec                 C   rj   )z]The system handle used to access the library.

        For duck-typing a ctypes.CDLL
        )rd   _handler5   r   r   r   rm      rl   z_lib_wrapper._handleN)rE   rF   rG   rH   	__slots__r7   rc   propertyrd   ri   rk   rm   r   r   r   r   rQ   h   s    

rQ   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 )rf   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.
    r3   _cfnc                 C   s   || _ || _d S r   r3   rp   )r6   lockrg   r   r   r   r7      s   
z_lib_fn_wrapper.__init__c                 C   rj   r   rp   argtypesr5   r   r   r   rt         z_lib_fn_wrapper.argtypesc                 C      || j _d S r   rs   )r6   rt   r   r   r   rt         c                 C   rj   r   rp   restyper5   r   r   r   ry      ru   z_lib_fn_wrapper.restypec                 C   rv   r   rx   )r6   ry   r   r   r   ry      rw   c                 O   s:   | j  | j|i |W  d    S 1 sw   Y  d S r   rq   )r6   argskwargsr   r   r   __call__   s   $z_lib_fn_wrapper.__call__N)rE   rF   rG   rH   rn   r7   ro   rt   setterry   r|   r   r   r   r   rf      s    



rf   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   r      )   
   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)libr3   r=   r;   r<   r   r   r   register_lock_callback      r   c                 C   r   )zRemove the registered callback functions for lock acquire and release.
    The arguments are the same as used in `register_lock_callback()`.
    N)r   r3   r?   r   r   r   r   unregister_lock_callback   r   r   c                   @   s   e Zd ZdZdS )_DeadPointerz:
    Dummy class to make error messages more helpful.
    N)rE   rF   rG   rH   r   r   r   r   r      s    r   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   r   _ptrbyref_as_parameter__owned)r6   ownedr   r   r   r   r7     s   
zOutputString.__init__c                 C   s.   | j d ur| jrt| j  d | _ | `d S d S r   )r   r   r   LLVMPY_DisposeStringr   r5   r   r   r   close  s   
zOutputString.closec                 C   s   | S r   r   r5   r   r   r   rA     s   zOutputString.__enter__c                 C      |    d S r   r   r6   rN   exc_valexc_tbr   r   r   rD   "     zOutputString.__exit__c                 C   $   | s| j d ur|    d S d S d S r   r   r6   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   )r6   sr   r   r   __str__,  s
   
zOutputString.__str__c                 C   
   t | jS r   boolr   r5   r   r   r   __bool__3  rL   zOutputString.__bool__c                 C   rj   )z:Get the raw bytes of content of the char pointer.
        )r   r   r5   r   r   r   bytes8  s   zOutputString.bytes)TN)rE   rF   rG   rH   r   r   classmethodr   r7   r   rA   rD   r   r   r   r   __nonzero__ro   r   r   r   r   r   r     s    

r   c                 C   s   | durt t| S dS )z,To wrap string return-value from C-API.
    N)rZ   r   r   r   r   r   r   
ret_string?  s   r   c                 C   s   | dur
t | jS dS )z+To wrap bytes return-value from C-API.
    N)r   r   r   r   r   r   r   	ret_bytesF  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   r   _capi)r6   r   r   r   r   r7   V  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detachr5   r   r   r   r   ]  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   r5   r   r   r   r   g  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   r5   r   r   r   r   p  s    zObjectRef._disposec                 C   s   | j S )zf
        Whether this object has been closed.  A closed object can't
        be used anymore.
        )r   r5   r   r   r   closedw  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__r5   r   r   r   rA     s   zObjectRef.__enter__c                 C   r   r   r   r   r   r   r   rD     r   zObjectRef.__exit__c                 C   r   r   r   r   r   r   r   r     s
   
zObjectRef.__del__c                 C   r   r   r   r5   r   r   r   r     rL   zObjectRef.__bool__c                 C   s.   t |dsdS t| jd t|jd kS )Nr   Fr   )r   r   	addressofr   )r6   otherr   r   r   __eq__  s
   
zObjectRef.__eq__c                 C   s   t t| jtjjS r   )hashr   r   r   c_void_pr   r5   r   r   r   __hash__  s   zObjectRef.__hash__N)rE   rF   rG   rH   r   r   r   r   r7   r   r   r   ro   r   rA   rD   r   r   r   r   r   r   r   r   r   r   r   M  s$    
	
r   )>sysr   r1   importlib.resources	resourcesr~   llvmlite.binding.commonr   r   llvmlite.utilsr   r   LLVMContextRefLLVMModuleRefLLVMValueRefLLVMTypeRefLLVMExecutionEngineRefLLVMPassManagerBuilderRefLLVMPassManagerRefLLVMTargetDataRefLLVMTargetLibraryInfoRefLLVMTargetRefLLVMTargetMachineRefLLVMMemoryBufferRefr   r   r   r   r   r    r!   r"   r#   r$   r%   LLVMObjectCacheRefLLVMObjectFileRefLLVMSectionIteratorRefr)   r*   LLVMTimePassesHandlerRefLLVMPipelineTuningOptionsRefLLVMModulePassManagerRefLLVMFunctionPassManagerRefLLVMPassBuilderRefr0   rI   objectrQ   rf   r   version_infopathrX   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sr    (M">