
    X-Ph                    "   d Z ddlmZ ddl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 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 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, ddl-m.Z. ddl/m0Z0m1Z1 dd	l2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 dd
l:m;Z; ddl<m=Z=m>Z>m?Z?m@Z@ ddlAmBZBmCZCmDZD ddlEmFZF ddlGmHZH ddlImJZJ eg df         ZKdYd!ZLdZd%ZMd[d-ZNd\d.ZOd]d0ZPd\d1ZQd^d6ZRd_d8ZS	 	 d`dad?ZT G d@ d>          ZU G dA dBeU          ZV G dC dDeU          ZWdbdFZX G dG dHeU          ZY G dI dJeU          ZZ G dK dLeZ          Z[ G dM dNeZ          Z\ G dO dPeZ          Z] G dQ dReU          Z^ G dS dTeU          Z_ G dU dVeU          Z` G dW dXeU          ZadS )czHelpers for generating for loops and comprehensions.

We special case certain kinds for loops such as "for x in range(...)"
for better efficiency.  Each for loop generator class below deals one
such special case.
    )annotations)CallableClassVar)ARG_POSCallExprDictionaryComprehension
ExpressionGeneratorExprLvalue
MemberExprNameExprRefExprSetExpr	TupleExpr	TypeAlias)
BasicBlockBranchIntegerIntOpLoadAddressLoadMemRaiseStandardErrorRegisterTupleGetTupleSetValue)RTupleRTypebool_rprimitiveint_rprimitiveis_dict_rprimitiveis_fixed_width_rtypeis_list_rprimitiveis_sequence_rprimitiveis_short_int_rprimitiveis_str_rprimitiveis_tuple_rprimitivepointer_rprimitiveshort_int_rprimitive)	IRBuilder)AssignmentTargetAssignmentTargetTuple)dict_check_size_opdict_item_iter_opdict_key_iter_opdict_next_item_opdict_next_key_opdict_next_value_opdict_value_iter_op)no_err_occurred_op)aiter_opanext_opiter_opnext_op)list_append_oplist_get_item_unsafe_opnew_list_set_item_op)stop_async_iteration_op)CFunctionDescription)
set_add_opNbuilderr*   indexr   exprr	   
body_instsGenFunc
else_instsGenFunc | Noneis_asyncboollineintreturnNonec           	        t                      }t                      }t                      }	t                      }
||	n|
}t          | ||||||          }|                     ||
           t                      }|                     |           |                                 |                     |           |                                  |             |                     |           |                                 |                     |           |	                    |           | 
                                 |4|                     |	            |             |                     |
           |                     |
           dS )a  Generate IR for a loop.

    Args:
        index: the loop index Lvalue
        expr: the expression to iterate over
        body_insts: a function that generates the body of the loop
        else_insts: a function that generates the else block instructions
    N)rF   )r   make_for_loop_generatorpush_loop_stackgoto_and_activategen_conditionactivate_block
begin_bodygen_stepgotoadd_cleanuppop_loop_stack)r?   r@   rA   rB   rD   rF   rH   
body_block
step_block
else_block
exit_blocknormal_loop_exitfor_gencondition_blocks                 Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypyc/irbuild/for_helpers.pyfor_loop_helperr_   K   s   $ JJJJ &0%;zz%j*:D8  G J
333 llOo...  :&&&JLLL j)))LL!!!()))z***
Z   :&&&&&    expr_regr   Callable[[Value], None]c                2   t          |j                  sJ |                     |          }t                      }t                      }t                      }	t                      }
t	          | |||	|d          }|                    ||d           |                     ||	           |                     |
           |                                 | 	                    |           |
                                  ||                     |j                             |                     |           |                                 |                     |
           |                    |	           |                                  | 	                    |	           dS )a}  Generate IR for a sequence iteration.

    This function only works for sequence type. Compared to for_loop_helper,
    it would feed iteration index to body_insts.

    Args:
        index: the loop index Lvalue
        expr: the expression to iterate over
        body_insts: a function that generates the body of the loop.
                    It needs a index as parameter.
    FreverseN)r$   typeget_sequence_typer   ForSequenceinitrN   rO   rP   rQ   rR   readindex_targetrS   rT   rU   rV   )r?   r@   rA   ra   rB   rH   target_typerW   rX   rZ   r]   r\   s               r^   for_loop_helper_with_indexrm      sy   & "(-00000++D11KJJJ llO'5*j$NNGLL;L666J
333o...:&&&Jw||G011222j)))LL!!!
###:&&&&&r`   genr
   empty_op_llbuilderCallable[[Value, int], Value]set_item_opr=   Value | Nonec                `    t          j                  dk    rt          j                  dk    rt          j        d                   dk    rډ                     j        d                   }t          |          st          |          st          |          r                     j        d                   } j	        
                    |j        d          } ||j                  d fd	}t           j        d         j        d         ||j                   S d
S )a  Generate a new tuple or list from a simple generator expression.

    Currently we only optimize for simplest generator expression, which means that
    there is no condition list in the generator and only one original sequence with
    one index is allowed.

    e.g.  (1) tuple(f(x) for x in a_list/a_tuple)
          (2) list(f(x) for x in a_list/a_tuple)
          (3) [f(x) for x in a_list/a_tuple]
    RTuple as an original sequence is not supported yet.

    Args:
        empty_op_llbuilder: A function that can generate an empty sequence op when
            passed in length. See `new_list_op_with_length` and `new_tuple_op_with_length`
            for detailed implementation.
        set_item_op: A primitive that can modify an arbitrary position of a sequence.
            The op should have three arguments:
                - Self
                - Target position
                - New Value
            See `new_list_set_item_op` and `new_tuple_set_item_op` for detailed
            implementation.
       r   T)use_pyssize_t
item_indexr   rJ   rK   c                z                         j                  }                    | |gj                   d S N)accept	left_exprcall_crH   )rv   er?   rn   rq   	target_ops     r^   set_itemz<sequence_from_generator_preallocate_helper.<locals>.set_item   s;    NN3=11{Y
A,FQQQQQr`   N)rv   r   rJ   rK   )len	sequencesindices	condlists	node_typer#   r'   r&   ry   r?   builtin_lenrH   rm   )	r?   rn   ro   rq   rtypesequencelengthr~   r}   s	   `` `    @r^   *sequence_from_generator_preallocate_helperr      sT   : 3=Q3s{#3#3q#8#8SqAQ=R=RVW=W=W!!#-"233e$$ 	(;E(B(B 	FWX]F^F^ 	~~cmA&677H_0038SW0XXF**638<<IR R R R R R R R R 'Qq)98Xsx   4r`   c                    t                     r                                 S t            j        j        t
                    }||S                                           g j                            t          t          j        j        j        j                            }d fd}t           ||j                                                  S )N)ro   rq   rJ   rK   c                                          j                  }                     t                                        | gj                   d S rx   )ry   rz   primitive_opr9   rj   rH   )r|   r?   rn   list_opss    r^   gen_inner_stmtsz5translate_list_comprehension.<locals>.gen_inner_stmts   sG    NN3=))^gll8.D.Da-H#(SSSSSr`   rJ   rK   ))raise_error_if_contains_unreachable_namesnoner   r?   new_list_op_with_lengthr;   maybe_spillnew_list_oprH   listzipr   r   r   rF   comprehension_helperrj   )r?   rn   valloop_paramsr   r   s   ``   @r^   translate_list_comprehensionr      s    0#>> ||~~ 5"?B(	  C 
""7#6#6r38#D#DEEHs3;s}clSSTTKT T T T T T T T +III<<!!!r`   'GeneratorExpr | DictionaryComprehensionc                    t          d |j        D                       r7t          t          j        d|j                  }|                     |           dS dS )zRaise a runtime error and return True if generator contains unreachable names.

    False is returned if the generator can be safely transformed without crashing.
    (It may still be unreachable!)
    c              3  R   K   | ]"}t          |t                    o|j        d u V  #d S rx   )
isinstancer   node).0ss     r^   	<genexpr>z<raise_error_if_contains_unreachable_names.<locals>.<genexpr>  s7      
K
K!:a""5qv~
K
K
K
K
K
Kr`   z+mypyc internal error: should be unreachableTF)anyr   r   RUNTIME_ERRORrH   add)r?   rn   errors      r^   r   r     s`     
K
Ks{
K
K
KKK ",9H
 

 	Et5r`   c                z    t                     r                                 S                                           g j                            t          t          j        j        j	        j
                            }d fd}t           ||j                                                  S )NrJ   rK   c                                          j                  }                     t                                        | gj                   d S rx   )ry   rz   r   r>   rj   rH   )r|   r?   rn   set_opss    r^   r   z4translate_set_comprehension.<locals>.gen_inner_stmts  sG    NN3=))Z',,w*?*?)CSXNNNNNr`   r   )r   r   r   
new_set_oprH   r   r   r   r   r   rF   r   rj   )r?   rn   r   r   r   s   ``  @r^   translate_set_comprehensionr     s    0#>> ||~~!!'"4"4R"B"BCCGs3;s}clSSTTKO O O O O O O O +III<<   r`   r   7list[tuple[Lvalue, Expression, list[Expression], bool]]r   Callable[[], None]c                F     d fdd fd	 |           d
S )a  Helper function for list comprehensions.

    Args:
        loop_params: a list of (index, expr, [conditions]) tuples defining nested loops:
            - "index" is the Lvalue indexing that loop;
            - "expr" is the expression for the object to be iterated over;
            - "conditions" is a list of conditions, evaluated in order with short-circuiting,
                that must all be true for the loop body to be executed
        gen_inner_stmts: function to generate the IR for the body of the innermost loop
    r   r   rJ   rK   c           	     \      d         \  }}}t          || fdd|           dS )zGenerate IR for a loop.

        Given a list of (index, expression, [conditions]) tuples, generate IR
        for the nested loops the list defines.
        r   c                 ,      dd                    S Nrt    )condsloop_contentsr   s   r^   <lambda>z;comprehension_helper.<locals>.handle_loop.<locals>.<lambda>C  s    MM%QRR99 r`   N)rF   rH   )r_   )r   r@   rA   rF   r   r?   rH   r   s   `   @r^   handle_loopz)comprehension_helper.<locals>.handle_loop8  s^     (31~$tUH999999	
 	
 	
 	
 	
 	
r`   r   list[Expression]remaining_loop_paramsc                p   | D ]}                     |          }t                      t                      }}                    |||                               |           j        d                             |j                                       |           |r |          S               dS )aa  Generate the body of the loop.

        Args:
            conds: a list of conditions to be evaluated (in order, with short circuiting)
                to gate the body of the loop
            remaining_loop_params: the parameters for any further nested loops; if it's empty
                we'll instead evaluate the "gen_inner_stmts" function
        N)ry   r   add_bool_branchrQ   nonlocal_controlgen_continuerH   rO   )	r   r   condcond_val
cont_block
rest_blockr?   r   r   s	         r^   r   z+comprehension_helper.<locals>.loop_contentsI  s      	2 	2D~~d++H%/\\:<<
J##Hj*EEE"":...$R(55gtyIII%%j1111  	;4555 Or`   N)r   r   rJ   rK   )r   r   r   r   rJ   rK   r   )r?   r   r   rH   r   r   s   ` ``@@r^   r   r   '  so    "
 
 
 
 
 
 
 
"       < Kr`   r   c                b    | j         dk    p$t          | j        t                    o
| j         dk    S )Nzbuiltins.rangezsix.moves.xrange)fullnamer   r   r   )rA   s    r^   is_range_refr   j  s6    )) 	0di++ 0M//r`   FrW   r   	loop_exitnestedForGeneratorc                   |rk|                      |          }t          | |||||          }	|                     |          }
|                     |
          }|	                    ||           |	S |                     |          }t          |          rX|                      |          }|                     |          }t          | |||||          }|                    ||d           |S t          |          rV|                      |          }| 
                    |          }t          | |||||          }|                    ||           |S t          |t                    rt          |j        t                    rt!          |j                  rt#          |j                  dk    s:t#          |j                  dk    r`|                     |j        d                   ?t)          |j                  t,          hk    r t#          |j                  dk    r0t/          d          }|                      |j        d                   }n@|                      |j        d                   }|                      |j        d                   }t#          |j                  dk    rQ|                     |j        d                   }|J |dk    r&|                     d|j        d         j                   nd}t5          | |||||          }|                    |||           |S |j        j        d	k    rt#          |j                  dk    r|j        t,          gk    rt          |t8                    rjt#          |j                  dk    rR|j        d         }|j        d         }t=          | |||||          }|                    |||j        d                    |S |j        j        d
k    rt#          |j                  dk    rt)          |j                  t,          hk    rut          |t8                    r`t#          |j                  t#          |j                  k    r6t?          | |||||          }|                    |j        |j                   |S |j        j        dk    rt#          |j                  dk    r|j        t,          gk    rt          |                     |j        d                             rc|                      |j        d                   }|                     |          }t          | |||||          }|                    ||d           |S t          |t                    r<t          |j        t@                    r!|j        s|                     |j        j!                  }t          |          r|j        j"        dv r|                      |j        j!                  }d}|j        j"        dk    r'| 
                    |j        j!                  }t          }n]|j        j"        dk    r'| #                    |j        j!                  }tH          }n&| %                    |j        j!                  }tL          } || |||||          }|                    ||           |S d}t          |tN                    rddl(m)}  || |          }||}||                      |          }tU          | |||||          }|                     |          }
|                     |
          }|                    ||           |S )zReturn helper object for generating a for loop over an iterable.

    If "nested" is True, this is a nested iterator such as "e" in "enumerate(e)".
    Frd         Nrt   r   zrange() step can't be zerozbuiltins.enumeratezbuiltins.zipzbuiltins.reversedT)keysvaluesitemsr   r   )precompute_set_literal)+ry   ForAsyncIterable_analyze_iterable_item_typetype_to_rtyperi   r   r$   rg   rh   r!   get_dict_key_typeForDictionaryKeysr   r   calleer   r   r   argsextract_intset	arg_kindsr   r   r   rH   ForRanger   r   r   ForEnumerateForZipr   rA   nameget_dict_value_typeForDictionaryValuesget_dict_item_typeForDictionaryItemsr   mypyc.irbuild.expressionr   ForIterable)r?   r@   rA   rW   r   rH   rF   r   ra   	async_obj	item_type
item_rtypertyprl   for_listfor_dict	start_regend_regstep	for_rangelvalue1lvalue2for_enumeratefor_zipr   for_dict_typefor_dict_geniterable_expr_regr   set_literalfor_objs                                  r^   rM   rM   r  s      >>$''$WeZDRXYY	77==	**955
x,,,T""Dd## >>$''//55wz9dFSShU;;;$ >>$''//55$WeZDRXYYh,,,$!! Ejg&F&F E%%6	 DI!##	NNa''G,?,?	!,M,M,YDN##y00 49~~""#*1::	!..166#NN49Q<88	!..16649~~""**49Q<88'''199MM">	!@QRRR %YfUUINN9gt444 K $888DI!##7)++5),, ,EK  A%% k!nGk!nG(%YPTV\]]Mw1>>>   K N22DI!##DN##y005),, 1EK  C	NN22 WeZD&QQGLLdi000N K $777DI!##7)++&w'8'81'F'FGG , ~~dil33H!33D99K"7E:y$PVWWHMM(KM>>>O$!!  jj&I&I  RVR[  !!$+"233e$$ 	 )9=X)X)X~~dk&677H7;M{6))%778HII 1!X--%99$+:JKK 3%889IJJ 2(=%YPTV\]]Lh444&*$   ,CCCCCC,,Wd;;" +  #NN400'5*ivNNG33D99I&&y11JLL"J///Nr`   c                  R    e Zd ZdZddZddZddZddZddZddZ	ddZ
d dZdS )!r   z-Abstract base class for generating for loops.r?   r*   r@   r   rW   r   r   rH   rI   r   rG   rJ   rK   c                    || _         || _        || _        || _        |                                 r|st                      | _        d S || _        d S rx   )r?   r@   rW   rH   need_cleanupr   r   )selfr?   r@   rW   r   rH   r   s          r^   __init__zForGenerator.__init__  sY     
$	  	'v 	''\\DNNN 'DNNNr`   c                    dS )z0If this returns true, we need post-loop cleanup.Fr   r   s    r^   r   zForGenerator.need_cleanup%  s    ur`   rZ   c                    |                                  rO| j                            | j                   |                                  | j                            |           dS dS )z!Add post-loop cleanup, if needed.N)r   r?   rQ   r   gen_cleanuprT   )r   rZ   s     r^   rU   zForGenerator.add_cleanup)  sf     	*L''777Lj)))))	* 	*r`   c                    dS )z<Generate check for loop exit (e.g. exhaustion of iteration).Nr   r   s    r^   rP   zForGenerator.gen_condition0        r`   c                    dS )z6Generate ops at the beginning of the body (if needed).Nr   r   s    r^   rR   zForGenerator.begin_body3  r   r`   c                    dS )z/Generate stepping to the next item (if needed).Nr   r   s    r^   rS   zForGenerator.gen_step6  r   r`   c                    dS )z'Generate post-loop cleanup (if needed).Nr   r   s    r^   r   zForGenerator.gen_cleanup9  r   r`   rA   Value | AssignmentTargetr   c                    | j         j                             | j                             || j                  | j                  S )z>A helper to get collection length, used by several subclasses.)r?   r   rj   rH   )r   rA   s     r^   load_lenzForGenerator.load_len<  s5    |#//0A0A$	0R0RTXT]^^^r`   N)r?   r*   r@   r   rW   r   r   r   rH   rI   r   rG   rJ   rK   rJ   rG   )rZ   r   rJ   rK   r   )rA   r  rJ   r   )__name__
__module____qualname____doc__r   r   rU   rP   rR   rS   r   r  r   r`   r^   r   r   	  s        77' ' ' '2   * * * *K K K KE E E E> > > >6 6 6 6_ _ _ _ _ _r`   c                  B    e Zd ZdZddZdd
ZddZddZddZddZ	dS )r   zIGenerate IR for a for loop over an arbitrary iterable (the general case).rJ   rG   c                    dS NTr   r   s    r^   r   zForIterable.need_cleanupD  s    tr`   ra   r   rl   r   rK   c                    | j         }|                    t          |g| j                  }|                    |           |                    |          | _        || _        d S rx   )r?   r   r7   rH   r   iter_targetrl   r   ra   rl   r?   iter_regs        r^   ri   zForIterable.initH  s^     ,''(TYGGH%%%"..x88&r`   c                   | j         }| j        }|                    t          |                    | j        |          g|          | _        |                    t          | j        | j	        | j
        t          j                             d S rx   )r?   rH   r{   r8   rj   r  next_regr   r   r   rW   IS_ERRORr   r?   rH   s      r^   rP   zForIterable.gen_conditionR  sm    
 ,ywd>NPT1U1U0VX\]]F4=$.$/6?[[\\\\\r`   c                    | j         }| j        }|                    | j        | j        |          }|                    |                    | j                  ||           d S rx   r?   rH   coercer  rl   assignget_assignment_targetr@   r   r?   rH   r  s       r^   rR   zForIterable.begin_body\  Z     ,y >>$-1A4HHw44TZ@@(DQQQQQr`   c                    d S rx   r   r   s    r^   rS   zForIterable.gen_stepf      r`   c                R    | j                             t          g | j                   d S rx   r?   r{   r4   rH   r   s    r^   r   zForIterable.gen_cleanupj  s'    
 	.DI>>>>>r`   Nr  ra   r   rl   r   rJ   rK   r   
r  r  r	  r
  r   ri   rP   rR   rS   r   r   r`   r^   r   r   A  s        SS   ' ' ' '] ] ] ]R R R R   ? ? ? ? ? ?r`   r   c                  2    e Zd ZdZddZdd	Zdd
ZddZdS )r   z"Generate IR for an async for loop.ra   r   rl   r   rJ   rK   c                    | j         }|                    t          |g| j                  }|                    |           |                    |          | _        || _        t          t                    | _	        d S rx   )
r?   r{   r5   rH   r   r  rl   r   r   stop_regr  s        r^   ri   zForAsyncIterable.initu  si    
 ,>>(XJ	BBH%%%"..x88& 11r`   c                    ddl mm}  j         j        d	fd}d
 fd}d
 fd} |||fd |fgd                                t           j         j         j	        t          j
                             d S )Nr   )
emit_awaittransform_try_exceptrJ   r   c                                          t          t          t          j                            }                      t          t          j        |                     S rx   )r   r   r(   r<   srcr   rf   )addrr?   rH   s    r^   except_matchz4ForAsyncIterable.gen_condition.<locals>.except_match  sF    ;;{+=?V?Z\`aabbD;;w'>'CTJJKKKr`   rK   c                                          t                              j                  g          }  |           _                            j                                        d           d S Nr   )r{   r6   rj   r  r  r  r%  false)	awaitabler?   r'  rH   r   s    r^   try_bodyz0ForAsyncIterable.gen_condition.<locals>.try_body  sf    x',,t?O2P2P1QSWXXI&Jw	4@@DMNN4='--//2>>>>>r`   c                 d                          j                                                    d S rx   )r  r%  true)r?   rH   r   s   r^   except_bodyz3ForAsyncIterable.gen_condition.<locals>.except_body  s)    NN4=',,..$?????r`   )rJ   r   r   )mypyc.irbuild.statementr'  r(  r?   rH   r   r   r%  r   rW   BOOL)r   r(  r,  r1  r4  r?   r'  rH   s   `    @@@r^   rP   zForAsyncIterable.gen_condition  s    	MLLLLLLL,y	L 	L 	L 	L 	L 	L 	L	? 	? 	? 	? 	? 	? 	? 	? 	?
	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	X,!5t[ IJDRV	
 	
 	
 	F4=$.$/6;WWXXXXXr`   c                    | j         }| j        }|                    | j        | j        |          }|                    |                    | j                  ||           d S rx   r  r  s       r^   rR   zForAsyncIterable.begin_body  r  r`   c                    d S rx   r   r   s    r^   rS   zForAsyncIterable.gen_step  r  r`   Nr!  r   r  r  r	  r
  ri   rP   rR   rS   r   r`   r^   r   r   r  ss        ,,
2 
2 
2 
2 Y  Y  Y  YDR R R R     r`   r   targetc                    t          |j                  r|                     t          ||g|          S |                     |d|gd|          S )z.Emit a potentially unsafe index into a target.__getitem__N)r#   rf   r   r:   gen_method_call)r?   r:  r@   rH   s       r^   unsafe_indexr>    sR    
 &+&& S##$;fe_dSSS&&v}ugtTRRRr`   c                  2    e Zd ZdZdd
ZddZddZddZdS )rh   zoGenerate optimized IR for a for loop over a sequence.

    Supports iterating in both forward and reverse.
    ra   r   rl   r   re   rG   rJ   rK   c                @   | j         }|| _        |                    |          | _        |st	          d          }nB|                    |                     | j                  t	          d          d| j                  }|                    |          | _	        || _
        d S )Nr   rt   -)r?   re   r   expr_targetr   	binary_opr  rH   maybe_spill_assignablerk   rl   )r   ra   rl   re   r?   	index_regs         r^   ri   zForSequence.init  s    , #..x88 	&qzzII))d.//S$) I $::9EE&r`   c                   | j         }| j        }| j        r}|                    |                    | j        |          t          d          d|          }t                      }|                    ||| j	                   |
                    |           |                     | j                  }|                    |                    | j        |          |d|          }|                    || j        | j	                   d S )Nr   z>=<)r?   rH   re   rC  rj   rk   r   r   r   r   rQ   r  rB  rW   )r   r?   rH   
comparisonsecond_checklen_regs         r^   rP   zForSequence.gen_condition  s    ,y< 
	1
 !**T.55wqzz4 J &<<L##JdnMMM""<000 -- 011&&w||D4Et'L'LgWZ\`aa

DOT^LLLLLr`   c                D   | j         }| j        }t          ||                    | j        |          |                    | j        |          |          }|sJ |                    |                    | j                  |	                    || j
        |          |           d S rx   )r?   rH   r>  rj   rB  rk   r  r  r@   r  rl   )r   r?   rH   	value_boxs       r^   rR   zForSequence.begin_body  s    ,y LL)400LL*D11	
 
	  	))$*55NN9d&6==	
 	
 	
 	
 	
r`   c                   | j         }| j        }| j        sdnd}|                    t          |                    | j        |          t          |          t          j	        |          }|
                    | j        ||           d S )Nrt   r   )r?   rH   re   int_opr)   rj   rk   r   r   ADDr  )r   r?   rH   r   r   s        r^   rS   zForSequence.gen_step  s|    ,y,qq"nn LL*D11DMMI
 
 	t(#t44444r`   N)ra   r   rl   r   re   rG   rJ   rK   r   r9  r   r`   r^   rh   rh     sr         
' ' ' ' M M M M*
 
 
 
(5 5 5 5 5 5r`   rh   c                  P    e Zd ZU dZded<   ded<   ddZddZddZddZddZ	dS )ForDictionaryCommona  Generate optimized IR for a for loop over dictionary keys/values.

    The logic is pretty straightforward, we use PyDict_Next() API wrapped in
    a tuple, so that we can modify only a single register. The layout of the tuple:
      * f0: are there more items (bool)
      * f1: current offset (int)
      * f2: next key (object)
      * f3: next value (object)
    For more info see https://docs.python.org/3/c-api/dict.html#c.PyDict_Next.

    Note that for subclasses we fall back to generic PyObject_GetIter() logic,
    since they may override some iteration methods in subtly incompatible manner.
    The fallback logic is implemented in CPy.h via dynamic type check.
    zClassVar[CFunctionDescription]dict_next_opdict_iter_oprJ   rG   c                    dS r  r   r   s    r^   r   z ForDictionaryCommon.need_cleanup  	     tr`   ra   r   rl   r   rK   c                   | j         }|| _        |                    |          | _        t	          d          }|                    |          | _        |                    |                     | j                            | _        |	                    | j
        |g| j                  }|                    |          | _        d S Nr   )r?   rl   r   rB  r   rD  offset_targetr  sizer{   rS  rH   r  )r   ra   rl   r?   offsetr  s         r^   ri   zForDictionaryCommon.init#  s    ,& #..x88$;;FCC''d6F(G(GHH	 >>$"3hZKK"..x88r`   c                $   | j         }| j        }| j                             | j        |                    | j        |          |                    | j        |          g|          | _        |                    t          | j        d|                    }|
                    | j        ||           |                    t          | j        d|                    }|                    t          || j        | j        t          j                             dS )zIGet next key/value pair, set new offset, and check if we should continue.rt   r   N)r?   rH   r{   rR  rj   r  rX  
next_tupler   r   r  r   rW   r   r6  )r   r?   rH   
new_offsetshould_continues        r^   rP   z!ForDictionaryCommon.gen_condition1  s    ,y,--\\$*D117<<@RTX3Y3YZ
 
 [[$/1d!C!CDD
t):t<<<!++ht4&H&HIIF?DOT^V[YYZZZZZr`   c                    | j         }| j        }|                    t          |                    | j        |          |                    | j        |          g|           dS )zCheck that dictionary didn't change size during iteration.

        Raise RuntimeError if it is not the case to match CPython behavior.
        N)r?   rH   r{   r-   rj   rB  rY  r  s      r^   rS   zForDictionaryCommon.gen_stepB  sa    
 ,y\\$*D117<<	43P3PQ	
 	
 	
 	
 	
r`   c                R    | j                             t          g | j                   d S rx   r   r   s    r^   r   zForDictionaryCommon.gen_cleanupP  s%    .DI>>>>>r`   Nr  r!  r   )
r  r  r	  r
  __annotations__r   ri   rP   rS   r   r   r`   r^   rQ  rQ    s           10000000   
9 9 9 9[ [ [ ["
 
 
 
? ? ? ? ? ?r`   rQ  c                  "    e Zd ZdZeZeZddZdS )r   z:Generate optimized IR for a for loop over dictionary keys.rJ   rK   c                   | j         }| j        }|                    t          | j        d|                    }|                    |                    | j                  |                    || j	        |          |           d S Nr   
r?   rH   r   r   r\  r  r  r@   r  rl   )r   r?   rH   keys       r^   rR   zForDictionaryKeys.begin_body[  s}    ,y kk(4?At<<==))$*55NN3 0$77	
 	
 	
 	
 	
r`   Nr   )	r  r  r	  r
  r1   rR  r/   rS  rR   r   r`   r^   r   r   U  s8        DD#L#L

 

 

 

 

 

r`   r   c                  "    e Zd ZdZeZeZddZdS )r   z<Generate optimized IR for a for loop over dictionary values.rJ   rK   c                   | j         }| j        }|                    t          | j        d|                    }|                    |                    | j                  |                    || j	        |          |           d S rd  re  )r   r?   rH   values       r^   rR   zForDictionaryValues.begin_bodyn  s}    ,y HT_a>>??))$*55NN5$"2D99	
 	
 	
 	
 	
r`   Nr   )	r  r  r	  r
  r2   rR  r3   rS  rR   r   r`   r^   r   r   h  s8        FF%L%L

 

 

 

 

 

r`   r   c                  "    e Zd ZdZeZeZddZdS )r   z;Generate optimized IR for a for loop over dictionary items.rJ   rK   c                X   | j         }| j        }|                    t          | j        d|                    }|                    t          | j        d|                    }t          | j        t                    sJ |                    || j        j	        d         |          }|                    || j        j	        d         |          }|
                    | j                  }t          |t                    rtt          |j                  dk    r|                    d|           |                    |j        d         ||           |                    |j        d         ||           d S |                    t#          ||g|                    }|                    |||           d S )Nr   r   r   rt   z'Expected a pair for dict item iteration)r?   rH   r   r   r\  r   rl   r   r  typesr  r@   r,   r   r   r   r  r   )r   r?   rH   rf  ri  r:  rvalues          r^   rR   zForDictionaryItems.begin_body  st   ,ykk(4?At<<==HT_a>>?? $*F33333nnS$"2"8";TBBud&6&<Q&?FF..tz::f344 	16<  A%%GNNNNN6<?C666NN6<?E488888[[3,!=!=>>FNN66400000r`   Nr   )	r  r  r	  r
  r0   rR  r.   rS  rR   r   r`   r^   r   r   {  s8        EE$L$L1 1 1 1 1 1r`   r   c                  *    e Zd ZdZdd	Zdd
ZddZdS )r   z;Generate optimized IR for a for loop over an integer range.r   r   r   r   rI   rJ   rK   c                T   | j         }|| _        || _        || _        |                    |          | _        t          |j                  rt          |j                  rt          }n#t          |j                  r|j        }nt          }t          |          }|                    ||d           |                    |          | _        |                    | j                  | _        |                    | j        |                    | j        | j                  | j                   d S r.  )r?   r   r   r   r   
end_targetr%   rf   r)   r"   r    r   r  rD  rE  r  r@   rk   rj   rH   )r   r   r   r   r?   
index_typerE  s          r^   ri   zForRange.init  s   ,"	!--g66"9>22 	(7Nw|7\7\ 	( 4JJ!',// 	( JJ'JZ((	y)R000 77	BB9@9V9VW[Wa9b9bt(',,t~ty*Q*QSWS\]]]]]r`   c                   | j         }| j        }| j        dk    rdnd}|                    |                    | j        |          |                    | j        |          ||          }|                    || j        | j	                   d S )Nr   rG  >)
r?   rH   r   rC  rj   rE  rp  r   rW   r   )r   r?   rH   cmprH  s        r^   rP   zForRange.gen_condition  s    ,yY]]cc&&LL..T_d0S0SUXZ^
 

 	
DOT^LLLLLr`   c                $   | j         }| j        }t          | j        j                  rmt          | j        j                  rT|                    t          |                    | j	        |          t          | j                  t          j        |          }nC|                    |                    | j	        |          t          | j                  d|          }|                    | j	        ||           |                    | j        ||           d S )N+)r?   rH   r%   r   rf   r   rN  r)   rj   rE  r   r   r   rO  rC  r  rk   r   r?   rH   new_vals       r^   rS   zForRange.gen_step  s    ,y #4>#677 	<SL=
 =
 	 nn$T^T22	""	 GG ''T^T22GDI4F4FT G 	t~w555t('488888r`   N)r   r   r   r   r   rI   rJ   rK   r   )r  r  r	  r
  ri   rP   rS   r   r`   r^   r   r     s^        EE^ ^ ^ ^&M M M M9 9 9 9 9 9r`   r   c                  *    e Zd ZdZddZddZddZdS )	ForInfiniteCounterzAGenerate optimized IR for a for loop counting from 0 to infinity.rJ   rK   c                    | j         }t          d          }|                    |          | _        |                    | j                  | _        d S rW  )r?   r   rD  rE  r  r@   rk   )r   r?   zeros      r^   ri   zForInfiniteCounter.init  sK    , qzz 77==9@9V9VW[Wa9b9br`   c                    | j         }| j        }|                    t          |                    | j        |          t          d          t          j        |          }|	                    | j        ||           d S r   )
r?   rH   rN  r)   rj   rE  r   r   rO  r  rw  s       r^   rS   zForInfiniteCounter.gen_step  sk    ,y .. ',,t~t"D"DgajjRWR[]a
 
 	t~w55555r`   c                    | j                             | j        | j                             | j                  | j                   d S rx   )r?   r  rk   rj   rE  rH   r   s    r^   rR   zForInfiniteCounter.begin_body  s;    D-t|/@/@/P/PRVR[\\\\\r`   Nr   )r  r  r	  r
  ri   rS   rR   r   r`   r^   rz  rz    s`        KKc c c c	6 	6 	6 	6] ] ] ] ] ]r`   rz  c                  B    e Zd ZdZddZddZddZddZddZddZ	dS )r   zIGenerate optimized IR for a for loop of form "for i, x in enumerate(it)".rJ   rG   c                    dS r  r   r   s    r^   r   zForEnumerate.need_cleanup  rU  r`   index1r   index2rA   r	   rK   c           	         t          | j        || j        | j        | j        d          | _        | j                                         t          | j        ||| j        | j        | j        d          | _        d S )NTr   )	rz  r?   rW   r   rH   	index_genri   rM   main_gen)r   r  r  rA   s       r^   ri   zForEnumerate.init  sv    +L&$/4>49UY
 
 
 	/L&$[_
 
 
r`   c                8    | j                                          d S rx   )r  rP   r   s    r^   rP   zForEnumerate.gen_condition  s    ##%%%%%r`   c                j    | j                                          | j                                         d S rx   )r  rR   r  r   s    r^   rR   zForEnumerate.begin_body  s0    !!###  """""r`   c                j    | j                                          | j                                         d S rx   )r  rS   r  r   s    r^   rS   zForEnumerate.gen_step  s0    !!!     r`   c                j    | j                                          | j                                         d S rx   )r  r   r  r   s    r^   r   zForEnumerate.gen_cleanup  s0    ""$$$!!#####r`   Nr  )r  r   r  r   rA   r	   rJ   rK   r   r"  r   r`   r^   r   r     s        SS   
	
 	
 	
 	
& & & &# # # #! ! ! !$ $ $ $ $ $r`   r   c                  B    e Zd ZdZddZdd
ZddZddZddZddZ	dS )r   z?Generate IR for a for loop of form `for x, ... in zip(a, ...)`.rJ   rG   c                    dS r  r   r   s    r^   r   zForZip.need_cleanup  rU  r`   indexeslist[Lvalue]exprsr   rK   c           
     ~   t          |          t          |          k    sJ d t          t          |          dz
            D             | j        gz   | _        g | _        t          ||| j                  D ]E\  }}}t          | j        |||| j        | j	        d          }| j        
                    |           Fd S )Nc                *    g | ]}t                      S r   )r   )r   _s     r^   
<listcomp>zForZip.init.<locals>.<listcomp>  s    JJJQJLLJJJr`   rt   Tr  )r   rangerW   cond_blocksgensr   rM   r?   r   rH   append)r   r  r  r@   rA   
next_blockrn   s          r^   ri   zForZip.init  s    7||s5zz)))) KJ%Gq8H2I2IJJJdoM^^(*	'*7E4;K'L'L 	" 	"#E4)eT:t~tyY]  C IS!!!!		" 	"r`   c                    t          | j                  D ]Y\  }}|                                 |t          | j                  dz
  k     r%| j                            | j        |                    Zd S r   )	enumerater  rP   r   r?   rQ   r  )r   irn   s      r^   rP   zForZip.gen_condition$  sv    	** 	A 	AFAs3ty>>A%%%++D,<Q,?@@@	A 	Ar`   c                B    | j         D ]}|                                 d S rx   )r  rR   r   rn   s     r^   rR   zForZip.begin_body*  s0    9 	 	CNN	 	r`   c                B    | j         D ]}|                                 d S rx   )r  rS   r  s     r^   rS   zForZip.gen_step.  s,    9 	 	CLLNNNN	 	r`   c                B    | j         D ]}|                                 d S rx   )r  r   r  s     r^   r   zForZip.gen_cleanup2  s0    9 	 	COO	 	r`   Nr  )r  r  r  r   rJ   rK   r   r"  r   r`   r^   r   r     s        II   

" 
" 
" 
"A A A A           r`   r   )r?   r*   r@   r   rA   r	   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   )r?   r*   r@   r   rA   r	   ra   r   rB   rb   rH   rI   rJ   rK   )
r?   r*   rn   r
   ro   rp   rq   r=   rJ   rr   )r?   r*   rn   r
   rJ   r   )r?   r*   rn   r   rJ   rG   )
r?   r*   r   r   r   r   rH   rI   rJ   rK   )rA   r   rJ   rG   )FF)r?   r*   r@   r   rA   r	   rW   r   r   r   rH   rI   rF   rG   r   rG   rJ   r   )
r?   r*   r:  r   r@   r   rH   rI   rJ   r   )br
  
__future__r   typingr   r   
mypy.nodesr   r   r   r	   r
   r   r   r   r   r   r   r   mypyc.ir.opsr   r   r   r   r   r   r   r   r   r   r   mypyc.ir.rtypesr   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   mypyc.irbuild.builderr*   mypyc.irbuild.targetsr+   r,   mypyc.primitives.dict_opsr-   r.   r/   r0   r1   r2   r3   mypyc.primitives.exc_opsr4   mypyc.primitives.generic_opsr5   r6   r7   r8   mypyc.primitives.list_opsr9   r:   r;   mypyc.primitives.misc_opsr<   mypyc.primitives.registryr=   mypyc.primitives.set_opsr>   rC   r_   rm   r   r   r   r   r   r   rM   r   r   r   r>  rh   rQ  r   r   r   r   rz  r   r   r   r`   r^   <module>r     s    # " " " " " % % % % % % % %                                                                                  , + + + + + I I I I I I I I                  8 7 7 7 7 7 M M M M M M M M M M M M c c c c c c c c c c = = = = = = : : : : : : / / / / / /
2t8
;' ;' ;' ;'|.' .' .' .'b- - - -`" " " "4   (! ! ! !@ @ @ @F    T T T T Tn5_ 5_ 5_ 5_ 5_ 5_ 5_ 5_p.? .? .? .? .?, .? .? .?b= = = = =| = = =@S S S SK5 K5 K5 K5 K5, K5 K5 K5\G? G? G? G? G?, G? G? G?T
 
 
 
 
+ 
 
 
&
 
 
 
 
- 
 
 
&1 1 1 1 1, 1 1 1<69 69 69 69 69| 69 69 69r] ] ] ] ] ] ] ]4!$ !$ !$ !$ !$< !$ !$ !$H$ $ $ $ $\ $ $ $ $ $r`   