
    5 f5                       d Z ddlmZ ddlmZmZ ddlmZmZm	Z	 ddl
mZ ddlmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddl m!Z!m"Z"m#Z# dd	l$m%Z%m&Z& dd
l'm(Z(m)Z) ddl*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5m6Z6 d2dZ7d3dZ8d4dZ9d2dZ:d2dZ;d5dZ<d6d&Z=d7d(Z>d8d)Z?d9d+Z@d9d,ZAd9d-ZBd8d.ZCd8d/ZDd2d0ZEd1S ):a  Generate IR for generator functions.

A generator function is represented by a class that implements the
generator protocol and keeps track of the generator state, including
local variables.

The top-level logic for dealing with generator functions is in
mypyc.irbuild.function.
    )annotations)ARG_OPTVar)ENV_ATTR_NAMENEXT_LABEL_ATTR_NAME	SELF_NAME)ClassIR)FuncDeclFuncIRFuncSignature
RuntimeArg)NO_TRACEBACK_LINE_NO
BasicBlockBranchCallGotoInteger
MethodCallRaiseStandardErrorRegisterReturnSetAttrTupleSetUnreachableValue)	RInstanceint_rprimitiveobject_rprimitive)	IRBuildergen_arg_defaults)FuncInfoGeneratorClass)add_args_to_envfinalize_env_classload_env_registersload_outer_env)ExceptNonlocalControl)error_catch_opexc_matches_opraise_exception_with_tb_opreraise_exception_oprestore_exc_info_opbuilderr   returnNonec                    t          |            t          |            t          |            t          |            |                     t          t          |                                d S N)setup_generator_classr%   r    r$   addr   instantiate_generator_class)r-   s    M/var/www/equiseq/venv/lib/python3.11/site-packages/mypyc/irbuild/generator.pygen_generator_funcr6   4   sc    '"""wWwKK27;;<<=====    r   c                   | j         j        }|                     t          | j         j        j        j        g |j                            }| j         j        r| j         j	        j
        }n| j         j
        }|                     t          |t          ||j                             t          d          }|                     t          |t          ||j                             |S )Nr   )fn_infofitemr3   r   generator_classirctorline	is_nestedcallable_classcurr_env_regr   r   r   r   )r-   r:   generator_regrA   zeros        r5   r4   r4   <   s    O!EKKW_%D%G%LbRWR\ ] ]^^M   45B3 KK}lEJOOPPP 1::DKK&:D%*MMNNNr7   r	   c                .   | j                                          d}t          || j        d          }t	          | j         j                  |j        t          <   |g|_        | j	        
                    |           t          |          | j         _        |S )N_genT)is_generated)r9   namespaced_namer	   module_namer   	env_class
attributesr   mroclassesappendr"   r;   )r-   namegenerator_class_irs      r5   r2   r2   S   s    o--//555D w':NNN3<W_=V3W3W!-001O-...&45G&H&HGO#r7   c                    |                      t          | j        j        j                             t                      }| j        j        j                            |           |                     |           d S r1   )	r3   r   r9   r;   switch_blockr   continuation_blocksrM   activate_block)r-   blocks     r5   !create_switch_for_generator_classrU   _   sc    KKW_4ABBCCCLLEO#7>>uEEE5!!!!!r7   c                   | j         j        }| j         j        j        }|                     |j                   t          |j                  D ]i\  }}t                      }| 	                    |j
        t          |          d|          }|                     |||           |                     |           j|                     t          t          j        d |                     |                     t!                                 d S )Nz==)r9   r;   r:   r>   rS   rQ   	enumeraterR   r   	binary_opnext_label_regr   add_bool_branchr3   r   STOP_ITERATIONr   )r-   clsr>   label
true_blockfalse_block
comparisons          r5   #populate_switch_for_generator_classra   f   s    
/
)C? %D3+,,,&s'>?? , ,z ll&&s'975>>4QUVV

JDDD{++++KK"#5#DdDQQRRRKKr7   r>   intc                   | j         j        }|j        J |j        \  }}}t                      }t                      }|                     ||                                 d|          }|                     |||           |                     |           |                     t          |||g|           | 
                    t                                 |                     |           dS )zAdd error handling blocks to a generator class.

    Generates blocks to check if error flags are set while calling the
    helper method for generator functions, and raises an exception if
    those flags are set.
    Nzis not)r9   r;   exc_regsr   translate_is_opnone_objectrZ   rS   call_cr*   r3   r   goto_and_activate)	r-   r>   r\   exc_typeexc_valexc_tberror_blockok_blockr`   s	            r5   -add_raise_exception_blocks_to_generator_classrn   u   s     /
)C<### #Hgv ,,K||H((73F3F3H3H(TXYYJJX>>>;'''NN-'6/JDQQQKKh'''''r7   r9   r!   sigr   arg_regslist[Register]blockslist[BasicBlock]is_coroutineboolc                    t          | ||||          }t          | |||           t          | |||           t          | |           t	          | |||           t          | |           |rt          | |           d S d S r1   )add_helper_to_generator_classadd_next_to_generator_classadd_send_to_generator_classadd_iter_to_generator_classadd_throw_to_generator_classadd_close_to_generator_classadd_await_to_generator_class)r-   r9   ro   rp   rr   rt   helper_fn_decls          r5   add_methods_to_generator_classr      s     37HfcSZ[[N.#FFF.#FFF111 '>3GGG '222 7$Wg666667 7r7   r
   c           
        t          t          t          t                    t          dt                    t          dt                    t          dt                    t          dt                    f|j                  }t          d|j        j        j        | j	        |          }t          ||||j        j        |j        j                  }||j        j        j        d<   | j                            |           |S )zRGenerates a helper method for a generator class, called by '__next__' and 'throw'.typevalue	tracebackarg__mypyc_generator_helper__)traceback_name)r   r   r   r   ret_typer
   r;   r<   rN   rH   r   r:   r>   methods	functionsrM   )r-   rp   rr   ro   r9   r~   helper_fn_irs          r5   rw   rw      s     y"344v011w 122{$566u/00	
 		 	C $g&=&@&EwGZ\_ N &'-*<W]M_  L HTG&'CD\***r7   c                    |                      |j        j        dt          |          5  |                     t          |                                                      ddd           dS # 1 swxY w Y   dS )z6Generates the '__iter__' method for a generator class.__iter__Nenter_methodr;   r<   r   r3   r   selfr-   r9   s     r5   rz   rz      s    			g58*FWY`	a	a , ,F7<<>>**+++, , , , , , , , , , , , , , , , , ,   5A**A.1A.fn_declc                |   |                      |j        j        dt          |          5  |                                 }|                     t          ||                                 ||||g|j        j	                            }|                     t          |                     ddd           dS # 1 swxY w Y   dS )z6Generates the '__next__' method for a generator class.__next__N)r   r;   r<   r   rf   r3   r   r   r:   r>   r   )r-   r9   r   ro   none_regresults         r5   rx   rx      s     
		g58*FWY`	a	a 
$ 
$&&((8XxH" 
 
 	F6NN###
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$s   A<B11B58B5c                   |                      |j        j        dt          |          5  |                     dt                    }|                                 }|                     t          ||                                 |||| 	                    |          g|j
        j                            }|                     t          |                     ddd           dS # 1 swxY w Y   dS )z2Generates the 'send' method for a generator class.sendr   N)r   r;   r<   r   add_argumentrf   r3   r   r   readr:   r>   r   )r-   r9   r   ro   r   r   r   s          r5   ry   ry      s    
		g58&BSU\	]	] $ $""5*;<<&&((8Xw||C?P?PQ" 
 
 	F6NN###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $s   B*CC#&C#c                N   |                      |j        j        dt          |          5  |                     dt                    }|                     dt          t
                    }|                     dt          t
                    }|                                 |                     |fd| j        j	        j
                   |                     |fd| j        j	        j
                   |                     t          ||                                 |                     |          |                     |          |                     |          g|j	        j
                            }|                     t          |                     ddd           dS # 1 swxY w Y   dS )z3Generates the 'throw' method for a generator class.throwr   r   r   c                      S r1    r   s   r5   <lambda>z.add_throw_to_generator_class.<locals>.<lambda>   s    H r7   c                      S r1   r   r   s   r5   r   z.add_throw_to_generator_class.<locals>.<lambda>   s    8 r7   N)r   r;   r<   r   r   r   rf   assign_if_nullr9   r:   r>   r3   r   r   r   r   )	r-   r9   r   ro   typvaltbr   r   s	           @r5   r{   r{      s    
		g58'CTV]	^	^ $ $""6+<==""7,=wGG!!+/@'JJ
 &&((s$4$4$4$4go6K6PQQQr#3#3#3#3W_5J5OPPP c!2!2GLL4E4Ew||TVGWGWYab" 
 
 	F6NN###)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $s   E$FF!Fc                   |                      |j        j        dt          |          5  t	                      t	                      }}| j                            |           |                     t	                                 |                     d|j	        j
                  }|                     t          |                                 d||                                 |                                 g                     |                     |           | j                                         |                     |           |                     t&          g |j	        j
                  }| j                            t-          | j        d         |                     |                     d|j	        j
                  }|                     t/          ||g|j	        j
                            }|                     t0          |g|j	        j
                  }t	                      t	                      }
}	|                     t3          ||	|
t2          j                             |                     |	           |                     t6          |                     |          g|j	        j
                   |                     t;          |                                                      |                     |
           |                     t<          g t>                     |                     tA                                 | j        !                                 |                     |           |                     tE          tD          j#        d|j	        j
                             |                     tA                                 ddd           dS # 1 swxY w Y   dS )z7Generates the '__close__' method for a generator class.closezbuiltins.GeneratorExitr   zbuiltins.StopIterationzgenerator ignored GeneratorExitN)$r   r;   r<   r   r   r-   push_error_handlerrh   load_module_attr_by_fullnamer:   r>   r3   r   r   rf   gotopop_error_handlerrS   rg   r(   nonlocal_controlrM   r'   r   r)   r   BOOLr,   r   r   r+   r   r   popr   RUNTIME_ERROR)r-   r9   except_block
else_blockgenerator_exitold_excstop_iteration
exceptionsmatchesmatch_blocknon_match_blocks              r5   r|   r|      su   			g58'CTV]	^	^ 1# 1##-<<j**<888!!*,,/// ==$gm&8
 
 	!4!4!6!68K8K8M8MN 	
 	
 	
 	Z   ))+++|,,,..W]5GHH ''!'":2">HH	
 	
 	
 !==$gm&8
 
 [[>>*JGML^!_!_``
..*w}?QRR'1||Z\\_F7K&+NNOOO{+++*W\\'-B-B,CW]EWXXXF7..0011222///+R1EFFFKMM""" $$&&&z***"01" 	
 	
 	
 	KMM"""c1# 1# 1# 1# 1# 1# 1# 1# 1# 1# 1# 1# 1# 1# 1# 1# 1# 1#s   NOOOc                    |                      |j        j        dt          |          5  |                     t          |                                                      ddd           dS # 1 swxY w Y   dS )z7Generates the '__await__' method for a generator class.	__await__Nr   r   s     r5   r}   r}   5  s    			g58+GXZa	b	b , ,F7<<>>**+++, , , , , , , , , , , , , , , , , ,r   c                B   | j         j        }| j         j        }|                     |j                  }|                     t          d          t          d          }|                     t          d          t          d          }|                     t          d          t          d          }|                     t          d          t          d          }|||f|_        ||_	        | 
                    ||j                  |_        t          | |j        | j        d                   |_        |                     t          t"                    t$          |d	          |_        t)          | d|d
           | 
                    |j        |j                  |_        dS )z0Populates the environment for a generator class.r   T)is_argr   r   r   r   F)reassign)localbaser   N)r9   r:   r;   add_self_to_envr<   	add_localr   r   rd   send_arg_regr   r>   self_regr&   	symtablesrA   add_var_to_env_classr   r   next_label_targetr#   rY   )r-   r:   r\   self_targetri   rj   rk   exc_args           r5   setup_env_for_generator_classr   ;  sk   O!E
/
)C))#&11K   V.? MMHG.?MMGs;//1B4PPFE

,=dKKGgv.CLC<<UZ88CL%gs|W=Nr=RSSC
 $88 !!>3 9  C G5sUCCCC !c&;UZHHCr7   N)r-   r   r.   r/   )r-   r   r.   r   )r-   r   r.   r	   )r-   r   r>   rb   r.   r/   )r-   r   r9   r!   ro   r   rp   rq   rr   rs   rt   ru   r.   r/   )r-   r   rp   rq   rr   rs   ro   r   r9   r!   r.   r
   )r-   r   r9   r!   r.   r/   )
r-   r   r9   r!   r   r
   ro   r   r.   r/   )F__doc__
__future__r   
mypy.nodesr   r   mypyc.commonr   r   r   mypyc.ir.class_irr	   mypyc.ir.func_irr
   r   r   r   mypyc.ir.opsr   r   r   r   r   r   r   r   r   r   r   r   r   r   mypyc.ir.rtypesr   r   r   mypyc.irbuild.builderr   r    mypyc.irbuild.contextr!   r"   mypyc.irbuild.env_classr#   r$   r%   r&   mypyc.irbuild.nonlocalcontrolr'   mypyc.primitives.exc_opsr(   r)   r*   r+   r,   r6   r4   r2   rU   ra   rn   r   rw   rz   rx   ry   r{   r|   r}   r   r   r7   r5   <module>r      sT    # " " " " " # # # # # # # # G G G G G G G G G G % % % % % % H H H H H H H H H H H H                                 I H H H H H H H H H = = = = = = = = : : : : : : : :            @ ? ? ? ? ?             > > > >   .	 	 	 	" " " "   ( ( ( (.7 7 7 7$   :, , , ,$ $ $ $"$ $ $ $$$ $ $ $63# 3# 3# 3#l, , , ,I I I I I Ir7   