
    pik3                     \   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 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'      Z- G d( d)      Z. G d* d+e/      Z0 G d, d-e/      Z1d. Z2e jf                  dd/ d0k\  re2nejh                  Z5 e0       Z6d1 Z7d2 Z8 G d3 d4e/      Z9 G d5 d6e/      Z:d7 Z;d8 Z< G d9 d:e/      Z=y);    N)_decode_string_is_shutting_down)get_library_namec                 d    t        | t        j                  fi       }t        j                  |      S N)typectypes	StructurePOINTER)namenewclss     V/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/llvmlite/binding/ffi.py_make_opaque_refr   
   s(    $))+R0F>>&!!    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                   .    e Zd ZdZd Zd Zd Zd Zd Zy)	_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                 D    t        j                         | _        g | _        y r   )	threadingRLock_lock_cblistselfs    r   __init__z_LLVMLock.__init__;   s     __&
r   c                 >    | j                   j                  ||f       y)zRegister callbacks that are invoked immediately after the lock is
        acquired (``acq_fn()``) and immediately before the lock is released
        (``rel_fn()``).
        N)r8   appendr:   acq_fnrel_fns      r   registerz_LLVMLock.registerA   s    
 	VV,-r   c                 >    | j                   j                  ||f       y)z)Remove the registered callbacks.
        N)r8   remover>   s      r   
unregisterz_LLVMLock.unregisterH   s     	VV,-r   c                 n    | j                   j                          | j                  D ]  \  }} |         y r   )r7   acquirer8   r>   s      r   	__enter__z_LLVMLock.__enter__M   s.    

"ll 	NFFH	r   c                 n    | j                   D ]  \  }} |         | j                  j                          y r   )r8   r7   release)r:   exc_detailsr?   r@   s       r   __exit__z_LLVMLock.__exit__S   s.    "ll 	NFFH	

r   N)	__name__
__module____qualname____doc__r;   rA   rD   rG   rK    r   r   r3   r3   2   s     ..
r   r3   c                       e Zd Zd Zd Zd Zy)_suppress_cleanup_errorsc                     || _         y r   )_context)r:   contexts     r   r;   z!_suppress_cleanup_errors.__init__[   s	    r   c                 6    | j                   j                         S r   )rT   rG   r9   s    r   rG   z"_suppress_cleanup_errors.__enter__^   s    }}&&((r   c                 \    	 | j                   j                  |||      S # t        $ r Y y w xY wr   )rT   rK   PermissionError)r:   exc_type	exc_value	tracebacks       r   rK   z!_suppress_cleanup_errors.__exit__a   s2    	==))(IyII 		s    	++N)rL   rM   rN   r;   rG   rK   rP   r   r   rR   rR   Z   s     )r   rR   c                   Z    e Zd ZdZg dZd Zd Zed        Zd Z	ed        Z
ed        Zy	)
_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_fntabr7   c                 >    d | _         i | _        t               | _        y r   )r^   r_   r3   r7   r9   s    r   r;   z_lib_wrapper.__init__p   s    [
r   c                    d}t         j                  d      d   }t               }t        t	        ||            5 }	 t        j                  t        |            | _         t        | j                  |              	 d d d        y # t        $ r d| d| d}t        |      t        $ r d| d| d	}t        |      w xY w# 1 sw Y   y xY w)
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 '')rL   
rpartitionr   rR   _importlib_resources_pathr	   CDLLstrr^   getattrOSErrorAttributeError)r:   test_symmod_namelib_namelib_pathmsgs         r   	_load_libz_lib_wrapper._load_libu   s    *&&s+A.#%%&?&.': ; 	#>F##);;s8}#= 3(((35	# 	#  	# B( L33;* =## cl"! ##* %#*A' cl"#!	# 	#s   C>B:B>>CC
c                 R    | j                   s| j                          | j                   S r   )r^   rq   r9   s    r   _libz_lib_wrapper._lib   s#     NNr   c                     	 | j                   |   S # t        $ r Y nw xY wt        | j                  |      }t	        | j
                  |      }|| j                   |<   |S r   )r_   KeyErrorri   rs   _lib_fn_wrapperr7   )r:   r   cfnwrappeds       r   __getattr__z_lib_wrapper.__getattr__   s]    	;;t$$ 		 dii&!$**c2#Ds    	c                 .    | j                   j                  S )zgThe name of the library passed in the CDLL constructor.

        For duck-typing a ctypes.CDLL
        )rs   _namer9   s    r   r{   z_lib_wrapper._name   s     yyr   c                 .    | j                   j                  S )z]The system handle used to access the library.

        For duck-typing a ctypes.CDLL
        )rs   _handler9   s    r   r}   z_lib_wrapper._handle   s     yy   r   N)rL   rM   rN   rO   	__slots__r;   rq   propertyrs   ry   r{   r}   rP   r   r   r]   r]   h   sY    
 3I!
#:    	   ! !r   r]   c                       e Zd ZdZddgZd Zed        Zej                  d        Zed        Z	e	j                  d        Z	d	 Z
y
)rv   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.
    r7   _cfnc                      || _         || _        y r   r7   r   )r:   lockrw   s      r   r;   z_lib_fn_wrapper.__init__   s    
	r   c                 .    | j                   j                  S r   r   argtypesr9   s    r   r   z_lib_fn_wrapper.argtypes   s    yy!!!r   c                 &    || j                   _        y r   r   )r:   r   s     r   r   z_lib_fn_wrapper.argtypes   s    %		r   c                 .    | j                   j                  S r   r   restyper9   s    r   r   z_lib_fn_wrapper.restype   s    yy   r   c                 &    || j                   _        y r   r   )r:   r   s     r   r   z_lib_fn_wrapper.restype   s    #		r   c                 j    | j                   5   | j                  |i |cd d d        S # 1 sw Y   y xY wr   r   )r:   argskwargss      r   __call__z_lib_fn_wrapper.__call__   s1    ZZ 	.499d-f-	. 	. 	.s   )2N)rL   rM   rN   rO   r~   r;   r   r   setterr   r   rP   r   r   rv   rv      su     &!I " " __& & ! ! ^^$ $.r   rv   c                 X    t        j                  t        j                  |       |z        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resources     r   _importlib_resources_path_replr      s!     ??7==1H<==r      )   
   c                 D    t         j                  j                  | |       y)z~Register callback functions for lock acquire and release.
    *acq_fn* and *rel_fn* are callables that take no arguments.
    N)libr7   rA   r?   r@   s     r   register_lock_callbackr      s     IIvv&r   c                 D    t         j                  j                  | |       y)zRemove the registered callback functions for lock acquire and release.
    The arguments are the same as used in `register_lock_callback()`.
    N)r   r7   rD   r   s     r   unregister_lock_callbackr      s     II(r   c                       e Zd ZdZy)_DeadPointerz:
    Dummy class to make error messages more helpful.
    N)rL   rM   rN   rO   rP   r   r   r   r      s    r   r   c                   r    e Zd ZdZ e       Zed        ZddZd Z	d Z
d ZefdZd	 Zd
 ZeZed        Zy)OutputStringz<
    Object for managing the char* output of LLVM APIs.
    c                 X     | t        j                  |t         j                              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ptrs     r   from_returnzOutputString.from_return  s     C9::r   Nc                     ||nt        j                  d       | _        t        j                  | j                        | _        || _        y r   )r	   r   _ptrbyref_as_parameter__owned)r:   ownedr   s      r   r;   zOutputString.__init__  s5     ,D&//$2G	$ll4995r   c                     | j                   5| j                  rt        j                  | j                          d | _         | `y y r   )r   r   r   LLVMPY_DisposeStringr   r9   s    r   closezOutputString.close  s7    99 {{((3DI#	 !r   c                     | S r   rP   r9   s    r   rG   zOutputString.__enter__  s    r   c                 $    | j                          y r   r   r:   rY   exc_valexc_tbs       r   rK   zOutputString.__exit__"      

r   c                 N     |       s| j                   | j                          y y y r   r   r:   r   s     r   __del__zOutputString.__del__%  s'     !"zz%

 & #r   c                 f    | j                   y| j                   j                  }|J t        |      S )Nz<dead OutputString>)r   valuer   )r:   ss     r   __str__zOutputString.__str__,  s1    99(IIOO}}a  r   c                 ,    t        | j                        S r   boolr   r9   s    r   __bool__zOutputString.__bool__3      DIIr   c                 .    | j                   j                  S )z:Get the raw bytes of content of the char pointer.
        )r   r   r9   s    r   byteszOutputString.bytes8  s     yyr   )TN)rL   rM   rN   rO   r   r   classmethodr   r;   r   rG   rK   r   r   r   r   __nonzero__r   r   rP   r   r   r   r     sc     "^N	; 	;
$ ): ! K r   r   c                 D    | t        t        j                  |             S y)z,To wrap string return-value from C-API.
    N)rh   r   r   r   s    r   
ret_stringr   ?  s$     <++C011 r   c                 F    | t         j                  |       j                  S y)z+To wrap bytes return-value from C-API.
    N)r   r   r   r   s    r   	ret_bytesr   F  s%     '',222 r   c                   z    e Zd ZdZdZ e       ZdZd Zd Z	d Z
d Zed        Zd Zd	 Zefd
Zd Zd ZeZd Zy)	ObjectRefzJ
    A wrapper around a ctypes pointer to a LLVM object ("resource").
    Fc                 P    |t        d      || _        || _        t        | _        y )NzNULL pointer)
ValueErrorr   r   r   _capi)r:   r   s     r   r;   zObjectRef.__init__V  s(    ;^,,	!
r   c                     	 | j                   s| j                  s| j                          | j                          y# | j                          w xY w)zI
        Close this object and do any required clean-up actions.
        N)_closedr   _disposedetachr9   s    r   r   zObjectRef.close]  s0    	<<KKMDKKMs	   (; Ac                 >    | j                   s| `d| _         d| _        yy)zN
        Detach the underlying LLVM resource without disposing of it.
        TN)r   r   r   r9   s    r   r   zObjectRef.detachg  s$     ||#DLDI r   c                      y)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).
        NrP   r9   s    r   r   zObjectRef._disposep  s    r   c                     | j                   S )zf
        Whether this object has been closed.  A closed object can't
        be used anymore.
        )r   r9   s    r   closedzObjectRef.closedw  s     ||r   c                 j    t        | d      sJ | j                  rt        | j                  d      | S )Nr   z instance already closed)hasattrr   RuntimeError	__class__r9   s    r   rG   zObjectRef.__enter__  s0    tW%%%<<t~~OPPr   c                 $    | j                          y r   r   r   s       r   rK   zObjectRef.__exit__  r   r   c                 N     |       s| j                   | j                          y y y r   r   r   s     r   r   zObjectRef.__del__  s%     "zz%

 & #r   c                 ,    t        | j                        S r   r   r9   s    r   r   zObjectRef.__bool__  r   r   c                     t        |d      syt        j                  | j                  d         t        j                  |j                  d         k(  S )Nr   Fr   )r   r	   	addressofr   )r:   others     r   __eq__zObjectRef.__eq__  sE    uf%		!-UZZ]+, 	,r   c                     t        t        j                  | j                  t        j                        j
                        S r   )hashr	   r   r   c_void_pr   r9   s    r   __hash__zObjectRef.__hash__  s&    FKK		6??;AABBr   N)rL   rM   rN   rO   r   r   r   r   r;   r   r   r   r   r   rG   rK   r   r   r   r   r   r   rP   r   r   r   r   M  sm     G!^NF   ): 
, KCr   r   )>sysr	   r5   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LLVMPassBuilderRefr3   rR   objectr]   rv   r   version_infopathrf   r   r   r   r   r   r   r   r   rP   r   r   <module>r
     sC   
   % E +"
 "-0 .,z*)*?@ ,-EF %&78 $%56 +,CD  .'(;< &'9: ,-HI &'<= +,FG &'<= ()@A %&:; ()@A +,FG '(>? -.JK $%89 %&78 $%56 )*?@ "#45)*BC +,CD /0KL +,CD -.GH %&78 % %P J!6 J!Z.f .D> !$ 0 0! 4 ? <"),, 
 n')6 ;6 ;|23MC MCr   