
    xKg):                     .   d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZ ddlmZ  G d de      Z G d d	e      Z ed
      Zej$                  Zej(                  Zej*                  Zej,                  Zej.                  Zej0                  Zej2                  Zd Zd Zd Zd Zd Zd Z G d de      Z  G d de      Z!d!dZ"d Z#d Z$d Z% ejL                  dd      Z'ejP                  d        Z)d Z*d Z+d Z,ejP                  d"d       Z-d  Z.y)#z<
Utilities to simplify the boilerplate for native lowering.
    N)Enum)typingtypesutilscgutils)BaseRegistryLoaderc                   N    e Zd ZdZddZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zy)Registryz?
    A registry of function and attribute implementations.
    c                 X    || _         g | _        g | _        g | _        g | _        g | _        y N)name	functionsgetattrssetattrscasts	constants)selfr   s     W/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/numba/core/imputils.py__init__zRegistry.__init__   s,    	
    c                       fd}|S )a1  
        Decorate an implementation of *func* for the given argument types.
        *func* may be an actual global function object, or any
        pseudo-function supported by Numba, such as "getitem".

        The decorated implementation has the signature
        (context, builder, sig, args).
        c                 D    j                   j                  | f       | S r   )r   append)implargtysfuncr   s    r   decoratez Registry.lower.<locals>.decorate%   s!    NN!!4v"67Kr    )r   r   r   r   s   ``` r   lowerzRegistry.lower   s    	 r   c                 V     ||||      }|j                  |||j                  f       |S r   )r   	signature)r   r   tyattr	impl_list	decorator	real_impls          r   _decorate_attrzRegistry._decorate_attr*   s0    dB-	)T9+>+>?@r   c                       fd}|S )z
        Decorate an implementation of __getattr__ for type *ty* and
        the attribute *attr*.

        The decorated implementation will have the signature
        (context, builder, typ, val).
        c                 J    j                  | j                  t              S r   )r'   r   _decorate_getattrr   r#   r   r"   s    r   r   z(Registry.lower_getattr.<locals>.decorate7   %    &&tRt}}'8: :r   r   r   r"   r#   r   s   ``` r   lower_getattrzRegistry.lower_getattr/       	: r   c                 &    | j                  |d      S )a:  
        Decorate the fallback implementation of __getattr__ for type *ty*.

        The decorated implementation will have the signature
        (context, builder, typ, val, attr).  The implementation is
        called for attributes which haven't been explicitly registered
        with lower_getattr().
        N)r.   r   r"   s     r   lower_getattr_genericzRegistry.lower_getattr_generic<        !!"d++r   c                       fd}|S )z
        Decorate an implementation of __setattr__ for type *ty* and
        the attribute *attr*.

        The decorated implementation will have the signature
        (context, builder, sig, args).
        c                 J    j                  | j                  t              S r   )r'   r   _decorate_setattrr+   s    r   r   z(Registry.lower_setattr.<locals>.decorateO   r,   r   r   r-   s   ``` r   lower_setattrzRegistry.lower_setattrG   r/   r   c                 &    | j                  |d      S )a;  
        Decorate the fallback implementation of __setattr__ for type *ty*.

        The decorated implementation will have the signature
        (context, builder, sig, args, attr).  The implementation is
        called for attributes which haven't been explicitly registered
        with lower_setattr().
        N)r7   r1   s     r   lower_setattr_genericzRegistry.lower_setattr_genericT   r3   r   c                       fd}|S )z
        Decorate the implementation of implicit conversion between
        *fromty* and *toty*.

        The decorated implementation will have the signature
        (context, builder, fromty, toty, val).
        c                 F    j                   j                  | ff       | S r   )r   r   )r   fromtyr   totys    r   r   z%Registry.lower_cast.<locals>.decorateg   s#    JJtfd^45Kr   r   )r   r<   r=   r   s   ``` r   
lower_castzRegistry.lower_cast_   s    	 r   c                       fd}|S )z
        Decorate the implementation for creating a constant of type *ty*.

        The decorated implementation will have the signature
        (context, builder, ty, pyval).
        c                 D    j                   j                  | ff       | S r   )r   r   )r   r   r"   s    r   r   z)Registry.lower_constant.<locals>.decorates   s     NN!!4"-0Kr   r   )r   r"   r   s   `` r   lower_constantzRegistry.lower_constantl   s    	 r   c                 "    d| j                    dS )NzLowering Registry<>)r   r   s    r   __repr__zRegistry.__repr__x   s    #DII;a00r   N)unspecified)__name__
__module____qualname____doc__r   r   r'   r.   r2   r7   r9   r>   rA   rE   r   r   r   r
   r
      s9    
	,	,
1r   r
   c                       e Zd ZdZdZy)RegistryLoaderz6
    An incremental loader for a target registry.
    )r   r   r   r   r   N)rG   rH   rI   rJ   registry_itemsr   r   r   rL   rL   |   s     QNr   rL   builtin_registryc                 D    | |fd}nfd}|f|_         ||_        |S )Nc                      | |||      S r   r   contextbuildertypvaluer#   r&   s        r   resz_decorate_getattr.<locals>.res   s    WgsE::r   c                      | ||||      S r   r   rQ   s        r   rV   z_decorate_getattr.<locals>.res   s    WgsE4@@r   )r!   r#   r   r"   r#   rV   r&   s       @r   r*   r*      s0    I	;	A ECMCHJr   c                 b    | |fd}nfd}|t         j                  f|_        ||_        |S )Nc                      | |||      S r   r   rR   rS   sigargsr#   r&   s        r   rV   z_decorate_setattr.<locals>.res   s    WgsD99r   c                      | ||||      S r   r   r[   s        r   rV   z_decorate_setattr.<locals>.res   s    WgsD$??r   )r   Anyr!   r#   rX   s       @r   r6   r6      s6    I	:	@ OCMCHJr   c                    t        |j                  t        j                        r|j                  j                  }| j                  ||      }t        j                  ||      }|j                  |j                  |j                              5  | j                  |||      }|j                  ||       d d d        |j                  |      }|S # 1 sw Y   xY wr   )
isinstancereturn_typer   Optionaltypemake_optional_noner   alloca_once_valueif_thennot_is_nonemake_optional_valuestoreload)	rR   rS   r\   statusretval
value_typeoptional_none	retvalptroptional_values	            r   fix_returning_optionalrs      s    #//5>>2__))
227JG--g}E	__W\\&..9:$88VN MM.)4	 ;
 i(M ;:s   &CCc                 T      fd} j                   |_        t        |      |_        |S )zC
    A wrapper inserting code calling Numba-compiled *fndesc*.
    c                    | j                  |j                        }| j                  j                  ||j                  j
                  |      \  }}t        j                  ||j                        5  | j                  j                  ||       d d d        |j                  j                  k(  sJ t        | ||||      }|j                  | j                  |j                        k7  r1d}t        |j                  |j                  |j                              t!        | |j                  |      S # 1 sw Y   xY w)Nz$function returned {0} but expect {1})declare_functionmodule	call_convcall_functionrestypeargtypesr   if_unlikelyis_errorreturn_status_propagaterb   rs   rd   get_value_type	TypeErrorformatimpl_ret_new_ref)	rR   rS   r\   r]   r   rm   rn   msgfndescs	           r   impzuser_function.<locals>.imp   s    ''? **88T6>>6??DB  &//:55gvF ;&..000'#vvN;;'00AA8CCJJv{{COODEE&..&II ;:s   4D44D=)r{   r!   tuplelibs)r   r   r   s   `  r   user_functionr      s&    
J" OOCMT{CHJr   c                 2      fd}t        |      |_        |S )zD
    A wrapper inserting code calling Numba-compiled *gendesc*.
    c                     | j                  |j                        }| j                  j                  ||j                  j
                  |      \  }}||fS r   )rv   rw   rx   ry   rz   r{   )rR   rS   r\   r]   r   rm   rn   gendescs          r   r   zuser_generator.<locals>.imp   sU    ''@ **88T7??G,<,<dD v~r   )r   r   )r   r   r   s   `  r   user_generatorr      s    
 T{CHJr   c                     fd}|S )zl
    Decorator a given class as implementing *iterator_type*
    (by providing an `iternext()` method).
    c                       j                   t        t        j                         fd       } t	        d      |        S )Nc                 6    |\  } | ||      } || ||      S r   r   )	rR   rS   r\   r]   resultrU   iterobjclsiternexts	          r   iternext_wrapperz8iterator_impl.<locals>.wrapper.<locals>.iternext_wrapper   s)    HU'7E2GGWgv>>r   r   )r   iternext_implRefTypeBORROWEDlower_builtin)r   r   r   iterator_types   ` @r   wrapperziterator_impl.<locals>.wrapper   sB    <<	w''	(	? 
)	?
 	1j-01AB
r   r   )iterable_typer   r   s    ` r   iterator_implr      s     Nr   c                   :    e Zd ZdZdZd Zd Zd
dZd Zd Z	d Z
y	)_IternextResultzb
    A result wrapper for iteration, passed by iternext_impl() into the
    wrapped function.
    _context_builder_pairobjc                 .    || _         || _        || _        y r   r   )r   rR   rS   pairobjs       r   r   z_IternextResult.__init__  s    r   c                 v    | j                   j                  t        j                  d      | j                  _        y)z1
        Mark the iterator as exhausted.
        FNr   get_constantr   booleanr   secondrD   s    r   set_exhaustedz_IternextResult.set_exhausted	  s%      $}}99%--Or   c                     |dv r*| j                   j                  t        j                  |      }|| j                  _        y)z
        Mark the iterator as valid according to *is_valid* (which must
        be either a Python boolean or a LLVM inst).
        )FTNr   )r   is_valids     r   	set_validz_IternextResult.set_valid  s2    
 }$}}11%--JH'r   c                 &    || j                   _        y)zP
        Mark the iterator as yielding the given *value* (a LLVM inst).
        Nr   first)r   rU   s     r   yield_z_IternextResult.yield_  s     $r   c                     | j                   j                  | j                  t        j                  | j
                  j                        S )z>
        Return whether the iterator is marked valid.
        )r   get_argument_valuer   r   r   r   r   rD   s    r   r   z_IternextResult.is_valid  s7     }}//05040D0DF 	Fr   c                 .    | j                   j                  S )z>
        Return the iterator's yielded value, if any.
        r   rD   s    r   yielded_valuez_IternextResult.yielded_value&  s     }}"""r   N)T)rG   rH   rI   rJ   	__slots__r   r   r   r   r   r   r   r   r   r   r      s.     5I 
P($F#r   r   c                   "    e Zd ZdZ	 dZ	 dZ	 dZy)r   z&
    Enumerate the reference type
             N)rG   rH   rI   rJ   NEWr   	UNTRACKEDr   r   r   r   r   ,  s+     C H Ir   r   c                 ^      t         D cg c]  }| c}vrt        d       fd}|S c c}w )a  
    Wrap the given iternext() implementation so that it gets passed
    an _IternextResult() object easing the returning of the iternext()
    result pair.

    ref_type: a numba.targets.imputils.RefType value, the reference type used is
    that specified through the RefType enum.

    The wrapped function will be called with the following signature:
        (context, builder, sig, args, iternext_result)
    z3ref_type must be an enum member of imputils.RefTypec                       fd}|S )Nc                 R   |j                   }| j                  ||      } | |||t        | ||             t        j                  k(  rt
        }n?t        j                  k(  rt        }n%t        j                  k(  rt        }nt        d       || |||j                               S )NzUnknown ref_type encountered)rb   make_helperr   r   r   r   r   impl_ret_borrowedr   impl_ret_untracked
ValueError	_getvalue)	rR   rS   r\   r]   	pair_typer   impl_retr   ref_types	          r   r   z-iternext_impl.<locals>.outer.<locals>.wrapperM  s    I))'9=G'3':<7;;&+W---,W...- !?@@GW$-w/@/@/BD Dr   r   )r   r   r   s   ` r   outerziternext_impl.<locals>.outerL  s    	D r   )r   r   )r   xr   s   `  r   r   r   =  s9     7+7a7++NOO" L) ,s   	*c                 z    t        j                  |j                  |      }| j                  d|      } |||f      S )z
    Call the `getiter()` implementation for the given *iterable_type*
    of value *val*, and return the corresponding LLVM inst.
    getiter)r   r!   r   get_function)rR   rS   r   valgetiter_siggetiter_impls         r   call_getiterr   `  s=    
 ""=#>#>NK''	;?L#((r   c                    |j                   }t        j                  |t        j                        }t	        j
                  ||      }| j                  d|      } |||f      }| j                  |||      }t        | ||      S )z
    Call the `iternext()` implementation for the given *iterator_type*
    of value *val*, and return a convenience _IternextResult() object
    reflecting the results.
    r   )	
yield_typer   Pairr   r   r!   r   r   r   )	rR   rS   r   r   itemtyr   iternext_sigr   r   s	            r   call_iternextr   j  sx     %%F

65==1I##I}=L((\BM
#
(C!!'9c:G7GW55r   c                     	 | j                  t        t        j                  t        j
                  |            } |||f      S # t        $ r Y yw xY w)z^
    Call len() on the given value.  Return None if len() isn't defined on
    this type.
    N)r   lenr   r!   r   intpNotImplementedError)rR   rS   r"   r   len_impls        r   call_lenr   y  sO    
)''V-=-=ejj"-NO #((  s   8A 	AA_ForIterLoop)rU   do_breakc              #     	K   |j                   }t        | ||      }j                  d      }j                  d      		fd}j                  |       j	                  |      5  t        | ||      }j                  j                  |j                               d      5  j                  	       ddd       t        |j                         |       j                  |       ddd       j                  	       | j                  r| j                  j                  ||       yy# 1 sw Y   zxY w# 1 sw Y   QxY ww)z
    Simulate a for loop on the given iterable.  Yields a namedtuple with
    the given members:
    - `value` is the value being yielded
    - `do_break` is a callable to early out of the loop
    zfor_iter.bodyzfor_iter.endc                  (    j                          y r   )branch)bb_endrS   s   r   r   zfor_iter.<locals>.do_break  s    vr   F)likelyN)r   r   append_basic_blockr   
goto_blockr   rg   rh   r   r   r   position_at_end
enable_nrtnrtdecref)
rR   rS   r   r   r   itervalbb_bodyr   rV   r   s
    `       @r   for_iterr     s     "//M7G]C@G((9G''7F NN7			G	$GWmWE__W\\#,,.9%_HNN6" I3,,.99w 
% F#7M7;  IH 
%	$s7   A%E)?D?(D3:5D?/AE3D<	8D??EEc                     |S )z5
    The implementation returns a new reference.
    r   ctxrS   rettyrets       r   r   r     	     Jr   c                 X    | j                   r| j                  j                  |||       |S )z
    The implementation returns a borrowed reference.
    This function automatically incref so that the implementation is
    returning a new reference.
    )r   r   increfr   s       r   r   r     s$     ~~ws+Jr   c                     |S )z.
    The return type is not a NRT object.
    r   r   s       r   r   r     r   r   c              #      K   ddl m} | j                  }|j                  ||       | _        	 d || _        y# || _        w xY ww)z7
    Temporarily change the context's error model.
    r   )callconvN)
numba.corer   error_modelcreate_error_model)rR   
model_namer   old_error_models       r   force_error_modelr     sC     
 $))O"55j'JG.-os   *A9 A	AAc                      t        d      )zPA stub for use internally by Numba when a call is emitted
    on a TypeRef.
    z%This function should not be executed.)r   )r]   kwargss     r   numba_typeref_ctorr     s     E
FFr   r   )numpy)/rJ   collections
contextlibinspect	functoolsenumr   r   r   r   r   r   numba.core.typing.templatesr   objectr
   rL   rN   r   r   r.   r2   r7   r9   r>   rA   r*   r6   rs   r   r   r   r   r   r   r   r   r   
namedtupler   contextmanagerr   r   r   r   r   r   r   r   r   <module>r     sM  
      4 4 :i1v i1XQ' Q ./  && ..(>>  ..(>> ((
!006",-#f -#^d " F)6
) &{%%n&;= < << . .Gr   