
    Ǆg`7                         d Z ddlZddlmZmZ ddlmZmZmZ ddl	m
Z
  G d de
      Z G d d	e      Z G d
 de      Z G d de      Z G d de      Zy)z"
Support for lowering generators.
    N)Constant	IRBuilder)typesconfigcgutils)FunctionDescriptorc                   4    e Zd ZdZdZed        Zed        Zy)GeneratorDescriptorz9
    The descriptor for a generator's next function.
     c                 V   t        |t        j                        sJ |j                  }dg}|f}|j                  dz   }|j
                  dz   }	 | |j                  |j                  ||	|j                  |j                  ||j                  ||j                  ||d|j                        }
|
S )a  
        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              ]/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/numba/core/generators.pyfrom_generator_fndescz)GeneratorDescriptor.from_generator_fndesc   s     '5??333$$w:??W,((726==&..(K::v~~w8H8Hh&//;     c                      d| j                   z   S )zz
        The LLVM name of the generator's finalizer function
        (if <generator type>.has_finalizer is true).
        	finalize_)mangled_namer#   s    r$   llvm_finalizer_namez'GeneratorDescriptor.llvm_finalizer_name'   s     T....r&   N)	__name__
__module____qualname____doc__	__slots__classmethodr%   propertyr+   r   r&   r$   r
   r
      s4     I ( / /r&   r
   c                   \    e Zd ZdZd Zed        Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zy)BaseGeneratorLowerz5
    Base support class for lowering generators.
    c                    |j                   | _         |j                  | _        |j                  | _        |j                  | _        || _        |j
                  | _        | j                         | _        t        j                  |j                  | j                  | j                  | j                   j                        | _        | j                   j                  | j                  j                        | _        i | _        y N)contextr   libraryr   lowergenerator_infogeninfoget_generator_typer    r
   r%   r   gendescget_data_packerr   
arg_packerresume_blocksr#   r9   s     r$   __init__zBaseGeneratorLower.__init__5   s    }}ll}}}}
++..0*@@MM4;;dll6J6JL ,,66t{{7K7KLr&   c                 .    | j                   j                  S r6   )r9   	call_convr*   s    r$   rD   zBaseGeneratorLower.call_convE   s    zz###r&   c                 2    t        j                  ||dd      S )Nr      r   gep_inboundsr#   buildergenptrs      r$   get_args_ptrzBaseGeneratorLower.get_args_ptrI   s    ##GVQ::r&   c                 6    t        j                  ||ddd      S )Nr   zgen.resume_indexnamerG   rI   s      r$   get_resume_index_ptrz'BaseGeneratorLower.get_resume_index_ptrL   s     ##GVQ);= 	=r&   c                 6    t        j                  ||ddd      S )Nr      z	gen.staterN   rG   rI   s      r$   get_state_ptrz BaseGeneratorLower.get_state_ptrP   s     ##GVQ)46 	6r&   c                    |j                  | j                         |j                  }|j                  j	                  | j
                  | j                  | j                  g       |j                          |j                          | j                  j                  | j
                        }| j                  j                  t        j                  d      }|j                  d   }|j                  d   }|j                  d       | j                  j                   rYt#        | j                  j$                  |j&                        D ],  \  }}| j                  j(                  j+                  |||       . | j,                  j/                  ||j&                        }	t1        |d      }
t3        j4                  |||	|
g|      }| j7                  ||      }|j                  d       | j8                  j;                  ||       |j=                          y)z
        Lower the generator's initialization function (which will fill up
        the passed-by-reference generator structure).
        r   rF   rR   z# low_init_func increfNz# low_init_func before return)setup_functionr   rJ   r7   insert_generatorr    r=   r8   extract_function_arguments	pre_lowerget_return_typeget_constantr   int32elementsdebug_print
enable_nrtzipr   fnargsnrtincrefr?   as_datar   r   make_anonymous_structbox_generator_structrD   return_value
post_lower)r#   r9   rJ   rettyresume_indexargstystatestyargtyargvalargsval	statesval
gen_structretvals                r$   lower_init_funcz"BaseGeneratorLower.lower_init_funcT   s   
 	T[[)-- 	&&t||T\\(,~	7 	((* ,,T\\:||00a@">>!$23<<""!$T[[%9%95<<!H @v  ''?@ //))'5<<@ Xt,	2274@'4=4?38:

 **5*=9:##GV4r&   c                 V   |j                  | j                         |j                  dj                  | j                  j                               | j                  j
                  d   | j                  k(  sJ |j                  }|j                  }| j                  j                  |      \  }| j                  j                  || j                  ||      |j                         | j                  ||      | _        | j#                  ||      | _        |j'                  d      }|j)                         }|j'                  d      }|j+                  |       | j                  j-                  |       |j+                  |       |j.                  |j0                     x}| j2                  d<   |j5                  |j7                  | j                         |      }	| j2                  j9                         D ]  \  }
}|	j;                  |
|        |j+                  |       |j=                  |       y)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)rU   r=   r]   formatr   r   r    rJ   functionrD   get_argumentsr?   	load_intorL   r`   rP   resume_index_ptrrS   gen_state_ptrappend_basic_blocklower_function_bodyposition_at_endreturn_stop_iterationblkmapfirstblkr@   switchloaditemsadd_casebranch)r#   r9   rJ   rw   rK   prologueentry_block_tail
stop_blockfirst_blockr   indexblocks               r$   lower_next_funcz"BaseGeneratorLower.lower_next_func   s    	T\\*299$,,:R:RST||$$Q'4<<777-->> ....x8!!'"&"3"3GV"D"',,	0 !% 9 9'6 J!//@../CD !446 001AB

+,,W5 	).3ll5>>.JJd((+ T-B-B C *, ..446 	*LE5OOE5)	* 	 01x r&   c                    t         j                  j                  t         j                  j                         | j                  j                  | j                        g      }t        j                  |j                  || j                  j                        }|j                  d      }t        |      }| j                  j                  | j                        }|j                  |j                  d   |      }| j!                  ||       y)z2
        Lower the generator's finalizer.
        entryr   N)llvmliteirFunctionTypeVoidTyper7   get_value_typer    r   get_or_insert_functionmoduler=   r+   r|   r   bitcastr"   lower_finalize_func_body)r#   r9   fntyrw   entry_blockrJ   genptrtyrK   s           r$   lower_finalize_funcz&BaseGeneratorLower.lower_finalize_func   s     {{''(<(<(>)-)D)DT\\)R(SU11LL$ @ @B11':K(<<..t||<q!18<%%gv6r&   c                     t        | j                  j                  j                  d      }|j                  j                  || j                         | j                  j                  |j                         y)zY
        Emit a StopIteration at generator end and mark the generator exhausted.
        N)r   rz   typepointeerJ   storerD   r   )r#   r9   indexvals      r$   return_from_generatorz(BaseGeneratorLower.return_from_generator   sR     D1166>>CHd&;&;<,,U]];r&   c                     d|fz  }|j                   j                  |      }|j                  j                  |       || j                  |<   y )Nzgenerator_resume%d)rw   r|   rJ   r~   r@   )r#   r9   r   
block_namer   s        r$   create_resumption_blockz*BaseGeneratorLower.create_resumption_block   sD    )UH4
11*=%%e,$)5!r&   c                 |    t         j                  r,| j                  j                  |dj	                  |             y y )NzDEBUGJIT: {0})r   	DEBUG_JITr7   r]   rv   )r#   rJ   msgs      r$   r]   zBaseGeneratorLower.debug_print   s/    LL$$Wo.D.DS.IJ r&   N)r,   r-   r.   r/   rB   r2   rD   rL   rP   rS   rr   r   r   r   r   r]   r   r&   r$   r4   r4   0   sP       $ $;=6/b,!\7<*Kr&   r4   c                   "    e Zd ZdZd Zd Zd Zy)GeneratorLowerz9
    Support class for lowering nopython generators.
    c                 .    | j                   j                  S r6   )r   r!   r*   s    r$   r<   z!GeneratorLower.get_generator_type   s    {{"""r&   c                     |S r6   r   )r#   r9   rp   s      r$   re   z#GeneratorLower.box_generator_struct   s    r&   c                 R   | j                  |d       | j                  j                  r]| j                  ||      }| j                  j                  ||      D ],  \  }}| j                  j                  j                  |||       . | j                  |d       |j                          y)g
        Lower the body of the generator's finalizer: decref all live
        state variables.
        z# generator: finalizez# generator: finalize endN)	r]   r7   r^   rL   r?   r   ra   decrefret_void)r#   rJ   rK   args_ptrtyvals         r$   r   z'GeneratorLower.lower_finalize_func_body   s    
 	"9:<<"" ((&9H??//B :C  ''S9: 	"=>r&   N)r,   r-   r.   r/   r<   re   r   r   r&   r$   r   r      s    #r&   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)PyGeneratorLowerz<
    Support class for lowering object mode generators.
    c           	      8   t        j                  | j                  j                  j                  t         j
                  t         j
                  f| j                  j                  z  t         j
                  ft        | j                  j                        z  d      S )zx
        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lenr;   
state_varsr*   s    r$   r<   z#PyGeneratorLower.get_generator_type   si    
 \\))..~~~~'$,,*@*@@)C0G0G,HH 	r&   c                     t        j                  |j                  |      }|j                  j	                  || j
                  |j                        S )z>
        Box the raw *gen_struct* as a Python object.
        )r   alloca_once_valuerJ   pyapifrom_native_generatorr    envarg)r#   r9   rp   gen_ptrs       r$   re   z%PyGeneratorLower.box_generator_struct  s;     ++EMM:F{{00$,,UUr&   c                     |j                   j                  t        | j                  j                  j
                  d      | j                         y)zo
        NULL-initialize all generator state variables, to avoid spurious
        decref's on cleanup.
        N)rJ   r   r   r{   r   r   rA   s     r$   init_generator_statez%PyGeneratorLower.init_generator_state	  s:    
 	HT%7%7%<%<%D%DdK ..	0r&   c                    | j                   j                  |      }| j                  ||      }|j                  |      }|j	                  d|t        |j                  d            }t        j                  ||      5  | j                  ||      }t        t        | j                  j                              D ]a  }t        j                  ||d|      }	| j                  j                  |   }
| j                   j                  ||
|	      }|j!                  |       c 	 ddd       |j#                          y# 1 sw Y   xY w)r   >r   N)r7   get_python_apirP   r   icmp_signedr   r   r   if_unlikelyrS   ranger   r    r   rH   unpack_valuer   r   )r#   rJ   rK   r   rz   ri   need_cleanupr{   state_index
state_slotr   r   s               r$   r   z)PyGeneratorLower.lower_finalize_func_body  s   
 ++G444WfE||$45
 **x(9(91=?   ,7 	" ..w?M$S)A)A%BC "$11'=23[B
\\--k:ll//ZHS!"	" 		" 	"s   <BD55D>N)r,   r-   r.   r/   r<   re   r   r   r   r&   r$   r   r      s    V0r&   r   c                   "    e Zd ZdZd Zd Zd Zy)
LowerYieldz>
    Support class for lowering a particular yield point.
    c                    || _         |j                  | _        |j                  | _        |j                  | _        | j                  j                  | _        | j                  j
                  | _        | j                  j                  | _        || _        | j                  j                  | _        || _	        |D cg c]'  }|j                  j                  j                  |      ) c}| _        y c c}w r6   )r9   r7   rJ   genlowerr    r{   rz   ypinst	live_varsr:   r   r   live_var_indices)r#   r9   yield_pointr   vs        r$   rB   zLowerYield.__init__2  s    
}}}}}},,!]]88 $ > >GGLL	"*3!5%& "'!5!5!@!@!F!Fq!I !5 !5s   9,C.c                    | j                   j                  d       t        | j                  | j                        D ]  \  }}t        j                  | j                  | j                  d|      }| j                  j                  |   }| j                   j                  |      }| j                   j                  ||       | j                   j                  |      }| j                  j                  r1| j                  j                   j#                  | j                  ||       | j                  j%                  | j                  |||        t'        | j(                  j*                  j,                  | j.                  j0                        }| j                  j3                  || j(                         | j                   j                  d       y )Nz# generator suspendr   z# generator suspend end)r9   r]   r_   r   r   r   rH   rJ   r{   r    r   typeof_alloca_varloadvarr7   r^   ra   rb   
pack_valuer   rz   r   r   r   r   r   )r#   r   rO   r   r   fetyper   r   s           r$   lower_yield_suspendzLowerYield.lower_yield_suspendA  sR   

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

89r&   c                    | j                   j                  | j                  | j                  j                         | j                  j                  d       t        | j                  | j                        D ]  \  }}t        j                  | j                  | j                  d|      }| j                  j                  |   }| j                  j!                  | j                  ||      }| j                  j#                  ||       | j                  j$                  s| j                  j&                  j)                  | j                  ||        | j                  j                  d       y )Nz# generator resumer   z# generator resume end)r   r   r9   r   r   r]   r_   r   r   r   rH   rJ   r{   r    r   r7   r   storevarr^   ra   r   )r#   r   rO   r   r   r   s         r$   lower_yield_resumezLowerYield.lower_yield_resumeZ  s   --djj$))//J

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

78r&   N)r,   r-   r.   r/   rB   r   r   r   r&   r$   r   r   -  s    5:29r&   r   )r/   llvmlite.irr   r   r   
numba.corer   r   r   numba.core.funcdescr   r
   objectr4   r   r   r   r   r&   r$   <module>r      sa     + - - 2!/, !/HbK bKH' 8:) :z;9 ;9r&   