
    d+                        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# G d d          Z$ G d d e%          Z& G d! d"e%          Z'd# Z(e j)        dd$         d%k    re(nej*        Z+ e
            Z,d&-                    e./                    d&          d d'                   Z0	  e+e0e,          Z1 ej2         e3e14                                                    Z5n # e6$ rZ7d(e, d)e7 Z8 e6e8          dZ7[7ww xY w e&e5          Z5d* Z9d+ Z: G d, d-e%          Z; G d. d/e%          Z<d0 Z=d1 Z> G d2 d3e%          Z?dS )4    N)_decode_string_is_shutting_down)get_library_namec                 b    t          | t          j        fi           }t          j        |          S N)typectypes	StructurePOINTER)namenewclss     4lib/python3.11/site-packages/llvmlite/binding/ffi.py_make_opaque_refr   
   s)    $)+R00F>&!!!    LLVMContext
LLVMModule	LLVMValueLLVMTypeLLVMExecutionEngineLLVMPassManagerBuilderLLVMPassManagerLLVMTargetDataLLVMTargetLibraryInfo
LLVMTargetLLVMTargetMachineLLVMMemoryBufferLLVMAttributeListIteratorLLVMAttributeSetIteratorLLVMGlobalsIteratorLLVMFunctionsIteratorLLVMBlocksIteratorLLVMArgumentsIteratorLLVMInstructionsIteratorLLVMOperandsIteratorLLVMTypesIteratorLLVMObjectCacheLLVMObjectFileLLVMSectionIteratorc                   0    e Zd ZdZd Zd Zd Zd Z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                 D    t          j                    | _        g | _        d S r   )	threadingRLock_lock_cblistselfs    r   __init__z_LLVMLock.__init__2   s     _&&
r   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r1   acq_fnrel_fns      r   registerz_LLVMLock.register8   s%    
 	VV,-----r   c                 >    | j                             ||f           dS )z)Remove the registered callbacks.
        N)r/   remover5   s      r   
unregisterz_LLVMLock.unregister?   s%     	VV,-----r   c                 f    | j                                          | j        D ]\  }} |             d S r   )r.   acquirer/   r5   s      r   	__enter__z_LLVMLock.__enter__D   sB    
"l 	 	NFFFHHHH	 	r   c                 f    | j         D ]\  }} |             | j                                         d S r   )r/   r.   release)r1   exc_detailsr6   r7   s       r   __exit__z_LLVMLock.__exit__J   s>    "l 	 	NFFFHHHH
r   N)	__name__
__module____qualname____doc__r2   r8   r;   r>   rB    r   r   r*   r*   )   si           . . .. . .
      r   r*   c                   R    e Zd ZdZg dZd Zd Zed             Ze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                 H    || _         i | _        t                      | _        d S r   )rJ   rK   r*   r.   )r1   libs     r   r2   z_lib_wrapper.__init__Y   s    	[[


r   c                     	 | j         |         S # t          $ r9 t          | j        |          }t	          | j        |          }|| j         |<   |cY S w xY wr   )rK   KeyErrorgetattrrJ   _lib_fn_wrapperr.   )r1   r   cfnwrappeds       r   __getattr__z_lib_wrapper.__getattr__^   sf    	;t$$ 	 	 	$)T**C%dj#66G 'DKNNN	s    A AAc                     | j         j        S )zgThe name of the library passed in the CDLL constructor.

        For duck-typing a ctypes.CDLL
        )rJ   _namer0   s    r   rV   z_lib_wrapper._nameh   s     yr   c                     | j         j        S )z]The system handle used to access the library.

        For duck-typing a ctypes.CDLL
        )rJ   _handler0   s    r   rX   z_lib_wrapper._handlep   s     y  r   N)
rC   rD   rE   rF   	__slots__r2   rT   propertyrV   rX   rG   r   r   rI   rI   Q   s         
 ,++I! ! !
     X ! ! X! ! !r   rI   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
d
S )rQ   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                 "    || _         || _        d S r   r.   r\   )r1   lockrR   s      r   r2   z_lib_fn_wrapper.__init__   s    
			r   c                     | j         j        S r   r\   argtypesr0   s    r   rb   z_lib_fn_wrapper.argtypes   s    y!!r   c                     || j         _        d S r   ra   )r1   rb   s     r   rb   z_lib_fn_wrapper.argtypes   s    %	r   c                     | j         j        S r   r\   restyper0   s    r   rf   z_lib_fn_wrapper.restype   s    y  r   c                     || j         _        d S r   re   )r1   rf   s     r   rf   z_lib_fn_wrapper.restype   s    #	r   c                 ^    | j         5   | j        |i |cd d d            S # 1 swxY w Y   d S r   r^   )r1   argskwargss      r   __call__z_lib_fn_wrapper.__call__   s    Z 	. 	.49d-f--	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   "&&N)rC   rD   rE   rF   rY   r2   rZ   rb   setterrf   rk   rG   r   r   rQ   rQ   y   s          &!I   " " X" _& & _& ! ! X! ^$ $ ^$. . . . .r   rQ   c                 T    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_replrs      s#     ?7=11H<===r      )   	   .z(Could not find/load shared object file: z
 Error was: c                 F    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)rM   r.   r8   r6   r7   s     r   register_lock_callbackr{      s"     Ivv&&&&&r   c                 F    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)rM   r.   r;   rz   s     r   unregister_lock_callbackr}      s"     I(((((r   c                       e Zd ZdZdS )_DeadPointerz:
    Dummy class to make error messages more helpful.
    N)rC   rD   rE   rF   rG   r   r   r   r      s           r   r   c                       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dS )OutputStringz<
    Object for managing the char* output of LLVM APIs.
    c                 T     | 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%     sC99::::r   TNc                     ||nt          j        d           | _        t          j        | j                  | _        || _        d S r   )r	   r   _ptrbyref_as_parameter__owned)r1   ownedr   s      r   r2   zOutputString.__init__   s;     ,DD&/$2G2G	$l4955r   c                 v    | j         1| j        rt                              | j                    d | _         | `d S d S r   )r   r   rM   LLVMPY_DisposeStringr   r0   s    r   closezOutputString.close   sE    9 { 4((333DI###	 ! r   c                     | S r   rG   r0   s    r   r>   zOutputString.__enter__   s    r   c                 .    |                                   d S r   r   r1   exc_typeexc_valexc_tbs       r   rB   zOutputString.__exit__       

r   c                 X     |            s| j         |                                   d S d S d S r   r   r1   r   s     r   __del__zOutputString.__del__   s>     ! "" 	z%

	 	%%r   c                 R    | j         dS | j         j        }|J t          |          S )Nz<dead OutputString>)r   valuer   )r1   ss     r   __str__zOutputString.__str__   s0    9((IO}}}a   r   c                 *    t          | j                  S r   boolr   r0   s    r   __bool__zOutputString.__bool__      DIr   c                     | j         j        S )z:Get the raw bytes of content of the char pointer.
        )r   r   r0   s    r   byteszOutputString.bytes  s     yr   )TN)rC   rD   rE   rF   r   r   classmethodr   r2   r   r>   rB   r   r   r   r   __nonzero__rZ   r   rG   r   r   r   r      s          "\^^N	; 	; [	;   
$ $ $     ):    ! ! !   K  X  r   r   c                 X    | 't          t                              |                     S dS )z,To wrap string return-value from C-API.
    N)strr   r   r   s    r   
ret_stringr     s,     <++C00111 r   c                 H    | t                               |           j        S dS )z+To wrap bytes return-value from C-API.
    N)r   r   r   r   s    r   	ret_bytesr     s'     '',,22 r   c                       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dS )	ObjectRefzJ
    A wrapper around a ctypes pointer to a LLVM object ("resource").
    Fc                 \    |t          d          || _        || _        t          | _        d S )NzNULL pointer)
ValueErrorr   r   rM   _capi)r1   r   s     r   r2   zObjectRef.__init__*  s/    ;^,,,	!


r   c                     	 | j         s| j        s|                                  |                                  dS # |                                  w xY w)zI
        Close this object and do any required clean-up actions.
        N)_closedr   _disposedetachr0   s    r   r   zObjectRef.close1  sJ    	<    KKMMMMMDKKMMMMs	   ": Ac                 8    | j         s| `d| _         d| _        dS dS )zN
        Detach the underlying LLVM resource without disposing of it.
        TN)r   r   r   r0   s    r   r   zObjectRef.detach;  s0     | 	#DLDIII	 	r   c                     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).
        NrG   r0   s    r   r   zObjectRef._disposeD  s      r   c                     | j         S )zf
        Whether this object has been closed.  A closed object can't
        be used anymore.
        )r   r0   s    r   closedzObjectRef.closedK  s     |r   c                 f    t          | d          sJ | j        rt          | j        d          | S )Nr   z instance already closed)hasattrr   RuntimeError	__class__r0   s    r   r>   zObjectRef.__enter__S  s?    tW%%%%%< 	Qt~~~OPPPr   c                 .    |                                   d S r   r   r   s       r   rB   zObjectRef.__exit__Y  r   r   c                 X     |            s| j         |                                   d S d S d S r   r   r   s     r   r   zObjectRef.__del__\  s<      "" 	z%

	 	%%r   c                 *    t          | j                  S r   r   r0   s    r   r   zObjectRef.__bool__a  r   r   c                     t          |d          sdS t          j        | j        d                   t          j        |j        d                   k    S )Nr   Fr   )r   r	   	addressofr   )r1   others     r   __eq__zObjectRef.__eq__d  sK    uf%% 	5	!--UZ]++, 	,r   c                 n    t          t          j        | j        t          j                  j                  S r   )hashr	   r   r   c_void_pr   r0   s    r   __hash__zObjectRef.__hash__m  s#    FK	6?;;ABBBr   N)rC   rD   rE   rF   r   r   r   r   r2   r   r   r   rZ   r   r>   rB   r   r   r   r   r   r   rG   r   r   r   r   !  s          G!\^^NF           X     ):    
  , , , KC C C C Cr   r   )@sysr	   r,   importlib.resources	resourcesrn   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%   LLVMObjectCacheRefLLVMObjectFileRefLLVMSectionIteratorRefr*   objectrI   rQ   rs   version_infopath_importlib_resources_path	_lib_namejoinrC   splitpkgname_lib_handleCDLLr   r>   rM   OSErroremsgr{   r}   r   r   r   r   r   rG   r   r   <module>r      s   



      % % % % % % E E E E E E E E + + + + + +" " "
 "!-00  ..,,z**))*?@@ ,,-EFF %%&788 $$%566 ++,CDD   ..''(;<< &&'9:: ,,-HII ++,FGG &&'<== (()@AA %%&:;; (()@AA ++,FGG ''(>?? $$%899 %%&788 $$%566 ))*?@@ % % % % % % % %P%! %! %! %! %!6 %! %! %!P. . . . .f . . .D> > > !$ 0! 4 > > <;"), 
 	 ((8>>#&&qt,
-
-	++GY??K
&+cc+//1122
3
3CC    y   C
'#,, l3' ' ') ) )    6   ; ; ; ; ;6 ; ; ;|2 2 23 3 3MC MC MC MC MC MC MC MC MC MCs   /7G' 'H,G??H