o
    C2hN0                     @   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+G d&d' d'Z,G d(d) d)Z-G d*d+ d+e.Z/G d,d- d-e.Z0d.d/ Z1e j2dd0 d1kre1nej3Z4e/ Z5d2d3 Z6d4d5 Z7G d6d7 d7e.Z8G d8d9 d9e.Z9d:d; Z:d<d= Z;G d>d? d?e.Z<dS )@    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   h/var/www/html/alexa/alex_system/speach-to-text/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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)r3   appendr5   acq_fnrel_fnr   r   r   registerA   s   z_LLVMLock.registerc                 C   r7   )z)Remove the registered callbacks.
        N)r3   remover9   r   r   r   
unregisterH   s   z_LLVMLock.unregisterc                 C   s$   | j   | jD ]\}}|  qd S r   )r2   acquirer3   r9   r   r   r   	__enter__M   s   
z_LLVMLock.__enter__c                 G   s$   | j D ]\}}|  q| j  d S r   )r3   r2   release)r5   exc_detailsr:   r;   r   r   r   __exit__S   s   z_LLVMLock.__exit__N)	__name__
__module____qualname____doc__r6   r<   r>   r@   rC   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)r5   contextr   r   r   r6   [      
z!_suppress_cleanup_errors.__init__c                 C   s
   | j  S r   )rI   r@   r4   r   r   r   r@   ^   rK   z"_suppress_cleanup_errors.__enter__c                 C   s(   z	| j |||W S  ty   Y d S w r   )rI   rC   PermissionError)r5   exc_type	exc_value	tracebackr   r   r   rC   a   s
   z!_suppress_cleanup_errors.__exit__N)rD   rE   rF   r6   r@   rC   r   r   r   r   rH   Z   s    rH   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_fntabr2   c                 C   s   d | _ i | _t | _d S r   )rQ   rR   r/   r2   r4   r   r   r   r6   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)rH   _importlib_resources_pathrD   
rpartitionr   r   CDLLstrrQ   LLVMPY_GetVersionInfoOSErrorAttributeError)r5   lib_path_er   r   r   	_load_libu   s   &
z_lib_wrapper._load_libc                 C   s   | j s|   | j S r   )rQ   r^   r4   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   )rR   KeyErrorgetattrr_   _lib_fn_wrapperr2   )r5   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
        )r_   _namer4   r   r   r   rg         z_lib_wrapper._namec                 C   rf   )z]The system handle used to access the library.

        For duck-typing a ctypes.CDLL
        )r_   _handler4   r   r   r   ri      rh   z_lib_wrapper._handleN)rD   rE   rF   rG   	__slots__r6   r^   propertyr_   re   rg   ri   r   r   r   r   rP   h   s    


rP   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 )rb   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.
    r2   _cfnc                 C   s   || _ || _d S r   r2   rl   )r5   lockrc   r   r   r   r6      s   
z_lib_fn_wrapper.__init__c                 C   rf   r   rl   argtypesr4   r   r   r   rp         z_lib_fn_wrapper.argtypesc                 C      || j _d S r   ro   )r5   rp   r   r   r   rp         c                 C   rf   r   rl   restyper4   r   r   r   ru      rq   z_lib_fn_wrapper.restypec                 C   rr   r   rt   )r5   ru   r   r   r   ru      rs   c                 O   s:   | j  | j|i |W  d    S 1 sw   Y  d S r   rm   )r5   argskwargsr   r   r   __call__   s   $z_lib_fn_wrapper.__call__N)rD   rE   rF   rG   rj   r6   rk   rp   setterru   rx   r   r   r   r   rb      s    



rb   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)libr2   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   r2   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)rD   rE   rF   rG   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)r5   ownedr   r   r   r   r6     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   r4   r   r   r   close	  s   
zOutputString.closec                 C   s   | S r   r   r4   r   r   r   r@     s   zOutputString.__enter__c                 C      |    d S r   r   r5   rM   exc_valexc_tbr   r   r   rC        zOutputString.__exit__c                 C   $   | s| j d ur|    d S d S d S r   r   r5   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   )r5   sr   r   r   __str__  s
   
zOutputString.__str__c                 C   
   t | jS r   boolr   r4   r   r   r   __bool__$  rK   zOutputString.__bool__c                 C   rf   )z:Get the raw bytes of content of the char pointer.
        )r   r   r4   r   r   r   bytes)  s   zOutputString.bytes)TN)rD   rE   rF   rG   r   r   classmethodr   r6   r   r@   rC   r   r   r   r   __nonzero__rk   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)rW   r   r   r   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)r   r   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   r   _capi)r5   r   r   r   r   r6   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detachr4   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   r4   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   r4   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   r4   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__r4   r   r   r   r@   p  s   zObjectRef.__enter__c                 C   r   r   r   r   r   r   r   rC   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   r4   r   r   r   r   ~  rK   zObjectRef.__bool__c                 C   s.   t |dsdS t| jd t|jd kS )Nr   Fr   )r   r   	addressofr   )r5   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   r4   r   r   r   __hash__  s   zObjectRef.__hash__N)rD   rE   rF   rG   r   r   r   r   r6   r   r   r   rk   r   r@   rC   r   r   r   r   r   r   r   r   r   r   r   >  s$    
	
r   )=sysr   r0   importlib.resources	resourcesrz   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*   LLVMPipelineTuningOptionsRefLLVMModulePassManagerRefLLVMFunctionPassManagerRefLLVMPassBuilderRefr/   rH   objectrP   rb   r   version_infopathrT   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sp    (>">