
    X-Ph=                       d Z ddlmZ ddlmZ ddlmZ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)m*Z* ddl+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z:m;Z;m<Z<m=Z= d9dZ>d:dZ?d;dZ@d<d!ZAd=d"ZBd=d#ZCd>d&ZDd?d-ZEd@d/ZFdAd0ZGdBd2ZHdBd3ZIdBd4ZJdAd5ZKdAd6ZLd=d7ZMd8S )Ca  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)Callable)ARG_OPTFuncDef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calculate_arg_defaultsgen_arg_defaults)FuncInfoGeneratorClass)add_args_to_envadd_vars_to_envfinalize_env_classload_env_registersload_outer_envload_outer_envssetup_func_for_recursive_call)ExceptNonlocalControl)error_catch_opexc_matches_opraise_exception_with_tb_opreraise_exception_oprestore_exc_info_opbuilderr!   gen_func_irSCallable[[list[Register], list[BasicBlock], FuncInfo], tuple[FuncIR, Value | None]]returntuple[FuncIR, Value | None]c                4   t          |            t          |            t          |            t          |            |                     t          t          |                                |                                 \  }}}}} ||||          \  }}||fS )zAGenerate IR for generator function that returns generator object.)setup_generator_classr)   r#   r(   addr   instantiate_generator_classleave)	r3   r4   args_blocksret_typefn_infofunc_irfunc_regs	            W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypyc/irbuild/generator.pygen_generator_funcrE   9   s     '"""wWwKK27;;<<===)0&D!VXw#D&'::GXH    rA   r$   sigr   rC   Value | NoneNonec                   |                      ||j                   t          |            t          | | j        j                   |                                 }|j        }| j        j        r9t          |t                    r$|r"|j        rt          | || j        j                   t          |            t          | |j                   t!          |            |                     |j                   |                                  t)          |            | j        d         }|                                 \  }}}	}
}t/          | ||||	|j                   t3          | |||           dS )a  Generate IR based on the body of a generator function.

    Add "__next__", "__iter__" and other generator methods to the generator
    class that implements the function (each function gets a separate class).

    Return the symbol table for the body.
    )r@   N)enterr@   setup_env_for_generator_classr+   rA   generator_classtop_level_fn_infofitem	is_nested
isinstancer   add_nested_funcs_to_envr,   !create_switch_for_generator_class-add_raise_exception_blocks_to_generator_classliner'   acceptbodymaybe_add_implicit_return#populate_switch_for_generator_class	symtablesr<   add_methods_to_generator_classis_coroutiner"   )r3   rA   rG   rC   	top_levelrP   symtabler=   r>   r?   r@   s              rD   gen_generator_func_bodyr`   K   sj    MM'CLM111!'***GW_<===))++IME!Wug&&W W -	W 	&gugo6UVVV%g...1'5:FFFGNN5:%%''''000  $H)0&D!VXw"7GS$HZ[[[ 7GXx@@@@@rF   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   )rA   rP   r:   r   rN   irctorrV   rQ   callable_classcurr_env_regr   r   r   r	   )r3   rP   generator_regre   zeros        rD   r;   r;   y   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rF   r   c                .   | j                                          d}t          || j        d          }t	          | j         j                  |j        t          <   |g|_        | j	        
                    |           t          |          | j         _        |S )N_genT)is_generated)rA   namespaced_namer   module_namer   	env_class
attributesr   mroclassesappendr%   rN   )r3   namegenerator_class_irs      rD   r9   r9      s    o--//555D w':NNN3<W_=V3W3W!-001O-...&45G&H&HGO#rF   c                    |                      t          | j        j        j                             t                      }| j        j        j                            |           |                     |           d S N)	r:   r   rA   rN   switch_blockr   continuation_blocksrq   activate_block)r3   blocks     rD   rT   rT      sc    KKW_4ABBCCCLLEO#7>>uEEE5!!!!!rF   c                   | j         j        }| j         j        j        }|                     |j                   t          |j                  D ]i\  }}t                      }| 	                    |j
        t          |          d|          }|                     |||           |                     |           j|                     t          t          j        d |                     |                     t!                                 d S )Nz==)rA   rN   rP   rV   rx   rv   	enumeraterw   r   	binary_opnext_label_regr   add_bool_branchr:   r   STOP_ITERATIONr   )r3   clsrV   label
true_blockfalse_block
comparisons          rD   rZ   rZ      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rF   rV   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)rA   rN   exc_regsr   translate_is_opnone_objectr~   rx   call_cr0   r:   r   goto_and_activate)	r3   rV   r   exc_typeexc_valexc_tberror_blockok_blockr   s	            rD   rU   rU      s     /
)C<### #Hgv ,,K||H((73F3F3H3H(TXYYJJX>>>;'''NN-'6/JDQQQKKh'''''rF   arg_regslist[Register]r?   list[BasicBlock]r]   boolc                    t          | ||||          }t          | |||           t          | |||           t          | |           t	          | |||           t          | |           |rt          | |           d S d S ru   )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)r3   rA   rG   r   r?   r]   helper_fn_decls          rD   r\   r\      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 7rF   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                            |           ||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    r@   r   rN   rb   rr   rl   r   rP   rV   methods	functionsrq   rm   env_user_function)r3   r   r?   rG   rA   r   helper_fn_irs          rD   r   r      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\****6G'rF   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_methodrN   rb   r    r:   r   selfr3   rA   s     rD   r   r      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   rN   rb   r    r   r:   r   r   rP   rV   r   )r3   rA   r   rG   none_regresults         rD   r   r      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   rN   rb   r    add_argumentr   r:   r   r   readrP   rV   r   )r3   rA   r   rG   r   r   r   s          rD   r   r     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 ru    r   s   rD   <lambda>z.add_throw_to_generator_class.<locals>.<lambda>0  s    H rF   c                      S ru   r   r   s   rD   r   z.add_throw_to_generator_class.<locals>.<lambda>1  s    8 rF   N)r   rN   rb   r    r   r   r   assign_if_nullrA   rP   rV   r:   r   r   r   r   )	r3   rA   r   rG   typvaltbr   r   s	           @rD   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   rK   zbuiltins.StopIterationzgenerator ignored GeneratorExitN)$r   rN   rb   r    r   r3   push_error_handlerr   load_module_attr_by_fullnamerP   rV   r:   r   r   r   gotopop_error_handlerrx   r   r.   nonlocal_controlrq   r-   r   r/   r   BOOLr2   r   r   r1   r   r   popr   RUNTIME_ERROR)r3   rA   except_block
else_blockgenerator_exitold_excstop_iteration
exceptionsmatchesmatch_blocknon_match_blocks              rD   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     rD   r   r   t  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   rK   F)reassign)localbaser   N)rA   rP   rN   add_self_to_envrb   	add_localr   r    r   send_arg_regr   rV   self_regr*   r[   re   add_var_to_env_classr	   r   next_label_targetr&   r}   )r3   rP   r   self_targetr   r   r   exc_args           rD   rM   rM   z  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rF   N)r3   r!   r4   r5   r6   r7   )
r3   r!   rA   r$   rG   r   rC   rH   r6   rI   )r3   r!   r6   r   )r3   r!   r6   r   )r3   r!   r6   rI   )r3   r!   rV   r   r6   rI   )r3   r!   rA   r$   rG   r   r   r   r?   r   r]   r   r6   rI   )r3   r!   r   r   r?   r   rG   r   rA   r$   r6   r   )r3   r!   rA   r$   r6   rI   )
r3   r!   rA   r$   r   r   rG   r   r6   rI   )N__doc__
__future__r   typingr   
mypy.nodesr   r   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"   r#   mypyc.irbuild.contextr$   r%   mypyc.irbuild.env_classr&   r'   r(   r)   r*   r+   r,   mypyc.irbuild.nonlocalcontrolr-   mypyc.primitives.exc_opsr.   r/   r0   r1   r2   rE   r`   r;   r9   rT   rZ   rU   r\   r   r   r   r   r   r   r   rM   r   rF   rD   <module>r      s    # " " " " "       , , , , , , , , , , 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 U U U U U U U U U U : : : : : : : :                  @ ? ? ? ? ?                $+A +A +A +A\   .	 	 	 	" " " "   ( ( ( (.7 7 7 7$   >, , , ,$ $ $ $"$ $ $ $$$ $ $ $63# 3# 3# 3#l, , , ,I I I I I IrF   