
    sh`7                         S r SSKrSSKJrJr  SSKJrJrJr  SSK	J
r
   " S S\
5      r " S S	\5      r " S
 S\5      r " S S\5      r " S S\5      rg)z"
Support for lowering generators.
    N)Constant	IRBuilder)typesconfigcgutils)FunctionDescriptorc                   <    \ rS rSrSrSr\S 5       r\S 5       r	Sr
g)GeneratorDescriptor   z1
The descriptor for a generator's next function.
 c                 R   [        U[        R                  5      (       d   eUR                  nS/nU4nUR                  S-   nUR
                  S-   n	U " UR                  UR                  XUR                  UR                  XRR                  XbR                  XtSUR                  S9n
U
$ )z
Build a GeneratorDescriptor for the generator returned by the
function described by *fndesc*, with type *gentype*.

The generator inherits the env_name from the *fndesc*.
All emitted functions for the generator shares the same Env.
genz.nextF)argtypesmanglerinlineenv_name)
isinstancer   	Generator
yield_typequalnameunique_namenativemodnamedoctypemap	calltypeskwsr   )clsfunc_irfndescgentyper   restypeargsr   r   r   selfs              i/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/numba/core/generators.pyfrom_generator_fndesc)GeneratorDescriptor.from_generator_fndesc   s     '5??3333$$w:??W,((726==&..(::v~~w8H8Hh&//;     c                      SU R                   -   $ )zb
The LLVM name of the generator's finalizer function
(if <generator type>.has_finalizer is true).
	finalize_)mangled_namer$   s    r%   llvm_finalizer_name'GeneratorDescriptor.llvm_finalizer_name'   s     T....r(   N)__name__
__module____qualname____firstlineno____doc__	__slots__classmethodr&   propertyr-   __static_attributes__r   r(   r%   r
   r
      s4     I ( / /r(   r
   c                   d    \ rS rSrSrS r\S 5       rS rS r	S r
S rS	 rS
 rS rS rS rSrg)BaseGeneratorLower0   z-
Base support class for lowering generators.
c                    UR                   U l         UR                  U l        UR                  U l        UR                  U l        Xl        UR
                  U l        U R                  5       U l        [        R                  UR                  U R                  U R                  U R                   R                  5      U l        U R                   R                  U R                  R                  5      U l        0 U l        g N)contextr    libraryr   lowergenerator_infogeninfoget_generator_typer!   r
   r&   r   gendescget_data_packerr   
arg_packerresume_blocksr$   r?   s     r%   __init__BaseGeneratorLower.__init__5   s    }}ll}}}}
++..0*@@MM4;;dll6J6JL ,,66t{{7K7KLr(   c                 .    U R                   R                  $ r<   )r?   	call_convr,   s    r%   rK   BaseGeneratorLower.call_convE   s    zz###r(   c                 2    [         R                  " XSS5      $ )Nr      r   gep_inboundsr$   buildergenptrs      r%   get_args_ptrBaseGeneratorLower.get_args_ptrI   s    ##GQ::r(   c                 0    [         R                  " XSSSS9$ )Nr   zgen.resume_indexnamerO   rQ   s      r%   get_resume_index_ptr'BaseGeneratorLower.get_resume_index_ptrL   s    ##GQ);= 	=r(   c                 0    [         R                  " XSSSS9$ )Nr      z	gen.staterW   rO   rQ   s      r%   get_state_ptr BaseGeneratorLower.get_state_ptrP   s    ##GQ)46 	6r(   c                    UR                  U R                  5        UR                  nUR                  R	                  U R
                  U R                  U R                  /5        UR                  5         UR                  5         U R                  R                  U R
                  5      nU R                  R                  [        R                  S5      nUR                  S   nUR                  S   nUR                  S5        U R                  R                   (       aY  [#        U R                  R$                  UR&                  5       H+  u  pxU R                  R(                  R+                  X'U5        M-     U R,                  R/                  X!R&                  5      n	[1        US5      n
[2        R4                  " UXIU
/U5      nU R7                  X5      nUR                  S5        U R8                  R;                  X,5        UR=                  5         g)zq
Lower the generator's initialization function (which will fill up
the passed-by-reference generator structure).
r   rN   r\   z# low_init_func increfNz# low_init_func before return)setup_functionr    rR   r=   insert_generatorr!   rC   r>   extract_function_arguments	pre_lowerget_return_typeget_constantr   int32elementsdebug_print
enable_nrtzipr   fnargsnrtincrefrE   as_datar   r   make_anonymous_structbox_generator_structrK   return_value
post_lower)r$   r?   rR   rettyresume_indexargstystatestyargtyargvalargsval	statesval
gen_structretvals                r%   lower_init_func"BaseGeneratorLower.lower_init_funcT   s   
 	T[[)-- 	&&t||T\\(,~	7 	((* ,,T\\:||00a@">>!$23<<""!$T[[%9%95<<!H  ''? "I //))'<<@ Xt,	2274@4=4?38:

 **5=9:##G4r(   c                 P   UR                  U R                  5        UR                  SR                  U R                  R                  5      5        U R                  R
                  S   U R                  :X  d   eUR                  nUR                  nU R                  R                  U5      u  nU R                  R                  UU R                  X$5      UR                  5        U R                  X$5      U l        U R#                  X$5      U l        UR'                  S5      nUR)                  5       nUR'                  S5      nUR+                  U5        U R                  R-                  U5        UR+                  U5        UR.                  UR0                     =oR2                  S'   UR5                  UR7                  U R                   5      U5      n	U R2                  R9                  5        H  u  pU	R;                  X5        M     UR+                  U5        UR=                  U5        g)z
Lower the generator's next() function (which takes the
passed-by-reference generator structure and returns the next
yielded value).
z# lower_next_func: {0}r   generator_prologuestop_iterationN)r`   rC   rh   formatr   r   r!   rR   functionrK   get_argumentsrE   	load_intorT   rk   rY   resume_index_ptrr]   gen_state_ptrappend_basic_blocklower_function_bodyposition_at_endreturn_stop_iterationblkmapfirstblkrF   switchloaditemsadd_casebranch)r$   r?   rR   r   rS   prologueentry_block_tail
stop_blockfirst_blockr   indexblocks               r%   lower_next_func"BaseGeneratorLower.lower_next_func   s    	T\\*299$,,:R:RST||$$Q'4<<777-->> ....x8!!'"&"3"3G"D"',,	0 !% 9 9' J!//@../CD !446 001AB

+,,W5 	).3ll5>>.JJ((+ T-B-B C *, ..446LEOOE) 7 	 01x r(   c                    [         R                  R                  [         R                  R                  5       U R                  R                  U R                  5      /5      n[        R                  " UR                  X R                  R                  5      nUR                  S5      n[        U5      nU R                  R                  U R                  5      nUR                  UR                  S   U5      nU R!                  XW5        g)z"
Lower the generator's finalizer.
entryr   N)llvmliteirFunctionTypeVoidTyper=   get_value_typer!   r   get_or_insert_functionmodulerC   r-   r   r   bitcastr#   lower_finalize_func_body)r$   r?   fntyr   entry_blockrR   genptrtyrS   s           r%   lower_finalize_func&BaseGeneratorLower.lower_finalize_func   s     {{''(<(<(>)-)D)DT\\)R(SU11LL$ @ @B11':K(<<..t||<q!18<%%g6r(   c                     [        U R                  R                  R                  S5      nUR                  R                  X R                  5        U R                  R                  UR                  5        g)zI
Emit a StopIteration at generator end and mark the generator exhausted.
N)r   r   typepointeerR   storerK   r   )r$   r?   indexvals      r%   return_from_generator(BaseGeneratorLower.return_from_generator   sP     D1166>>CH&;&;<,,U]];r(   c                     SU4-  nUR                   R                  U5      nUR                  R                  U5        X@R                  U'   g )Nzgenerator_resume%d)r   r   rR   r   rF   )r$   r?   r   
block_namer   s        r%   create_resumption_block*BaseGeneratorLower.create_resumption_block   sB    )UH4
11*=%%e,$)5!r(   c                     [         R                  (       a,  U R                  R                  USR	                  U5      5        g g )NzDEBUGJIT: {0})r   	DEBUG_JITr=   rh   r   )r$   rR   msgs      r%   rh   BaseGeneratorLower.debug_print   s/    LL$$Wo.D.DS.IJ r(   )rE   r=   r    r   r   rC   rA   r!   r>   r?   rF   r   N)r/   r0   r1   r2   r3   rH   r6   rK   rT   rY   r]   r}   r   r   r   r   rh   r7   r   r(   r%   r9   r9   0   sP       $ $;=6/b,!\7<*Kr(   r9   c                   *    \ rS rSrSrS rS rS rSrg)GeneratorLower   z1
Support class for lowering nopython generators.
c                 .    U R                   R                  $ r<   )r    r"   r,   s    r%   rB   !GeneratorLower.get_generator_type   s    {{"""r(   c                     U$ r<   r   )r$   r?   r{   s      r%   rp   #GeneratorLower.box_generator_struct   s    r(   c                 X   U R                  US5        U R                  R                  (       a[  U R                  X5      nU R                  R                  X5       H+  u  pEU R                  R                  R                  XU5        M-     U R                  US5        UR                  5         g)O
Lower the body of the generator's finalizer: decref all live
state variables.
z# generator: finalizez# generator: finalize endN)	rh   r=   ri   rT   rE   r   rl   decrefret_void)r$   rR   rS   args_ptrtyvals         r%   r   'GeneratorLower.lower_finalize_func_body   s    
 	"9:<<"" ((9H??//B  ''S9 C 	"=>r(   r   N)	r/   r0   r1   r2   r3   rB   rp   r   r7   r   r(   r%   r   r      s    #r(   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	PyGeneratorLower   z4
Support class for lowering object mode generators.
c           	      4   [         R                  " U R                  R                  R                  [         R
                  [         R
                  4U R                  R                  -  [         R
                  4[        U R                  R                  5      -  SS9$ )z`
Compute the actual generator type (the generator function's return
type is simply "pyobject").
T)gen_funcr   	arg_typesstate_typeshas_finalizer)
r   r   r   func_idfuncpyobject	arg_countlenrA   
state_varsr,   s    r%   rB   #PyGeneratorLower.get_generator_type   si    
 \\))..~~~~'$,,*@*@@)C0G0G,HH 	r(   c                     [         R                  " UR                  U5      nUR                  R	                  X0R
                  UR                  5      $ )z.
Box the raw *gen_struct* as a Python object.
)r   alloca_once_valuerR   pyapifrom_native_generatorr!   envarg)r$   r?   r{   gen_ptrs       r%   rp   %PyGeneratorLower.box_generator_struct  s9     ++EMM:F{{00,,UUr(   c                     UR                   R                  [        U R                  R                  R
                  S5      U R                  5        g)zW
NULL-initialize all generator state variables, to avoid spurious
decref's on cleanup.
N)rR   r   r   r   r   r   rG   s     r%   init_generator_state%PyGeneratorLower.init_generator_state	  s:    
 	HT%7%7%<%<%D%DdK ..	0r(   c                    U R                   R                  U5      nU R                  X5      nUR                  U5      nUR	                  SU[        UR                  S5      5      n[        R                  " X5         U R                  X5      n[        [        U R                  R                  5      5       Ha  n[        R                  " XSU5      n	U R                  R                  U   n
U R                   R                  XU	5      nUR!                  U5        Mc     SSS5        UR#                  5         g! , (       d  f       N= f)r   >r   N)r=   get_python_apirY   r   icmp_signedr   r   r   if_unlikelyr]   ranger   r!   r   rP   unpack_valuer   r   )r$   rR   rS   r   r   rt   need_cleanupr   state_index
state_slotr   r   s               r%   r   )PyGeneratorLower.lower_finalize_func_body  s   
 ++G444WE||$45
 **x(9(91=?   7 ..w?M$S)A)A%BC$11'23[B
\\--k:ll//ZHS!  D 8 	 87s   ;BD33
Er   N)
r/   r0   r1   r2   r3   rB   rp   r   r   r7   r   r(   r%   r   r      s    V0r(   r   c                   *    \ rS rSrSrS rS rS rSrg)
LowerYieldi-  z6
Support class for lowering a particular yield point.
c                    Xl         UR                  U l        UR                  U l        UR                  U l        U R                  R                  U l        U R                  R
                  U l        U R                  R                  U l        X l        U R                  R                  U l        X0l	        U Vs/ s H(  nUR                  R                  R                  U5      PM*     snU l        g s  snf r<   )r?   r=   rR   genlowerr!   r   r   ypinst	live_varsr@   r   r   live_var_indices)r$   r?   yield_pointr   vs        r%   rH   LowerYield.__init__2  s    
}}}}}},,!]]88 $ > >GGLL	"*3!5*3Q "'!5!5!@!@!F!Fq!I*3!5 !5s   6/C.c                    U R                   R                  S5        [        U R                  U R                  5       GH  u  p[
        R                  " U R                  U R                  SU5      nU R                  R                  U   nU R                   R                  U5      nU R                   R                  X%5        U R                   R                  U5      nU R                  R                  (       a0  U R                  R                   R#                  U R                  XF5        U R                  R%                  U R                  XFU5        GM     ['        U R(                  R*                  R,                  U R.                  R0                  5      nU R                  R3                  XpR(                  5        U R                   R                  S5        g )Nz# generator suspendr   z# generator suspend end)r?   rh   rj   r   r   r   rP   rR   r   r!   r   typeof_alloca_varloadvarr=   ri   rl   rm   
pack_valuer   r   r   r   r   r   r   )r$   r   rX   r   r   fetyper   r   s           r%   lower_yield_suspendLowerYield.lower_yield_suspendA  sB   

45!$T%:%:DNN!KK --dllD<N<N./>J))+6B
 ZZ&&t,FJJ""40**$$T*C||&&  ''b>LL##DLL":F "L" D1166>> IIOO-8%:%:;

89r(   c                    U R                   R                  U R                  U R                  R                  5        U R                  R                  S5        [        U R                  U R                  5       H  u  p[        R                  " U R                  U R                  SU5      nU R                  R                  U   nU R                  R!                  U R                  XC5      nU R                  R#                  XR5        U R                  R$                  (       d  M  U R                  R&                  R)                  U R                  XE5        M     U R                  R                  S5        g )Nz# generator resumer   z# generator resume end)r   r   r?   r   r   rh   rj   r   r   r   rP   rR   r   r!   r   r=   r   storevarri   rl   r   )r$   r   rX   r   r   r   s         r%   lower_yield_resumeLowerYield.lower_yield_resumeZ  s    --djj$))//J

34!$T%:%:DNN!KK --dllD<N<N./>J))+6B,,++DLL"ICJJ*||&&&  ''b> "L 	

78r(   )rR   r=   r   r   r!   r   r   r   r?   r   r   N)	r/   r0   r1   r2   r3   rH   r   r  r7   r   r(   r%   r   r   -  s    5:29r(   r   )r3   llvmlite.irr   r   r   
numba.corer   r   r   numba.core.funcdescr   r
   objectr9   r   r   r   r   r(   r%   <module>r	     sa     + - - 2!/, !/HbK bKH' 8:) :z;9 ;9r(   