o
    2g                     @  s  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 ZKdcd!d"ZLddd&d'ZMded/d0ZNdfd1d2ZOdgd4d5ZPdfd6d7ZQdhd<d=ZRdid?d@ZS	A	AdjdkdGdHZTG dIdF dFZUG dJdK dKeUZVG dLdM dMeUZWdldOdPZXG dQdR dReUZYG dSdT dTeUZZG dUdV dVeZZ[G dWdX dXeZZ\G dYdZ dZeZZ]G d[d\ d\eUZ^G d]d^ d^eUZ_G d_d` d`eUZ`G dadb dbeUZadS )mzHelpers 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              	   C  s   t  }t  }t  }	t  }
|dur|	n|
}t| ||||||d}| ||
 t  }| | |  | | |  |  | | |  | | |	| | 
  |dure| |	 |  | |
 | |
 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)rE   )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?   r@   rA   rC   rE   rG   
body_block
step_block
else_block
exit_blocknormal_loop_exitfor_gencondition_block r\   Q/home/garg/my-data/venv/lib/python3.10/site-packages/mypyc/irbuild/for_helpers.pyfor_loop_helperK   s2   






r^   expr_regr   Callable[[Value], None]c                 C  s   t |jsJ | |}t }t }t }	t }
t| |||	|d}|j||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initrL   rM   rN   rO   rP   readindex_targetrQ   rR   rS   rT   )r>   r?   r@   r_   rA   rG   target_typerU   rV   rX   r[   rZ   r\   r\   r]   for_loop_helper_with_index   s(   





rj   genr	   empty_op_llbuilderCallable[[Value, int], Value]set_item_opr<   Value | Nonec                   s   t jdkr_t jdkr_t jd dkr_ jd }t|s+t|s+t|r_ jd } j	j
|jdd}||jd fd	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   rI   rJ   c                   s&     j} | |gj d S N)accept	left_exprcall_crG   )rr   er>   rk   rn   	target_opr\   r]   set_item   s   z<sequence_from_generator_preallocate_helper.<locals>.set_itemN)rr   r   rI   rJ   )len	sequencesindices	condlists	node_typer"   r&   r%   rt   r>   builtin_lenrG   rj   )r>   rk   rl   rn   rtypesequencelengthrz   r\   rx   r]   *sequence_from_generator_preallocate_helper   s   .r   c                   s   t  r	  S t  jjtd}|d ur|S   g jt	t
jjjj}d fdd}t ||j  S )N)rl   rn   rI   rJ   c                    *     j}  t | gj d S rs   )rt   ru   rv   r8   rg   rG   rw   r>   rk   list_opsr\   r]   gen_inner_stmts      z5translate_list_comprehension.<locals>.gen_inner_stmtsrI   rJ   ))raise_error_if_contains_unreachable_namesnoner   r>   new_list_op_with_lengthr:   maybe_spillnew_list_oprG   listzipr}   r|   r~   rE   comprehension_helperrg   )r>   rk   valloop_paramsr   r\   r   r]   translate_list_comprehension   s   

r   'GeneratorExpr | DictionaryComprehensionc                 C  s6   t dd |jD rt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                 s  s$    | ]}t |to|jd u V  qd S rs   )
isinstancer   node).0sr\   r\   r]   	<genexpr>  s   " z<raise_error_if_contains_unreachable_names.<locals>.<genexpr>z+mypyc internal error: should be unreachableTF)anyr}   r   RUNTIME_ERRORrG   add)r>   rk   errorr\   r\   r]   r     s   
r   c                   sl   t  r	  S   g jttjjj	j
}d fdd}t ||j  S )NrI   rJ   c                    r   rs   )rt   ru   rv   r=   rg   rG   r   r>   rk   set_opsr\   r]   r     r   z4translate_set_comprehension.<locals>.gen_inner_stmtsr   )r   r   r   
new_set_oprG   r   r   r}   r|   r~   rE   r   rg   )r>   rk   r   r   r\   r   r]   translate_set_comprehension  s   

r   r   7list[tuple[Lvalue, Expression, list[Expression], bool]]r   Callable[[], None]c                   s0   d fddd fd
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   rI   rJ   c              	     s6   d \}} }t || fdd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                     s    dd  S Nrp   r\   r\   )condsloop_contentsr   r\   r]   <lambda>C      z;comprehension_helper.<locals>.handle_loop.<locals>.<lambda>N)rE   rG   )r^   )r   r?   r@   rE   )r>   rG   r   )r   r   r]   handle_loop8  s   
z)comprehension_helper.<locals>.handle_loopr   list[Expression]remaining_loop_paramsc                   sn   | D ])}  |}t t }} |||  |  jd  |j  | q|r2|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)rt   r   add_bool_branchrO   nonlocal_controlgen_continuerG   rM   )r   r   condcond_val
cont_block
rest_block)r>   r   r   r\   r]   r   I  s   


z+comprehension_helper.<locals>.loop_contentsN)r   r   rI   rJ   )r   r   r   r   rI   rJ   r\   )r>   r   r   rG   r\   )r>   r   r   rG   r   r]   r   '  s   r   r   c                 C  s    | j dkpt| jto| j dkS )Nzbuiltins.rangezsix.moves.xrange)fullnamer   r   r   )r@   r\   r\   r]   is_range_refj  s   
r   FrU   r   	loop_exitnestedForGeneratorc                 C  s  |r"|  |}t| |||||}	| |}
| |
}|	|| |	S | |}t|rH|  |}| |}t| |||||}|j||dd |S t	|rg|  |}| 
|}t| |||||}||| |S t|trt|jtrt|jrt|jdkst|jdkr| |jd durt|jthkrt|jdkrtd}|  |jd }n|  |jd }|  |jd }t|jdkr| |jd }|dusJ |dkr| d|jd j nd}t| |||||}|||| |S |jjd	kr8t|jdkr8|jtgkr8t|tr8t|jdkr8|jd }|jd }t| |||||}||||jd  |S |jjd
krtt|jdkrtt|jthkrtt|trtt|jt|jkrtt| |||||}||j|j |S |jjdkrt|jdkr|jtgkrt| |jd r|  |jd }| |}t| |||||}|j||d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!}t$}n	| %|jj!}t&}|| |||||}||| |S d}t|t'r8ddl(m)} || |}|dur8|}|du rB|  |}t*| |||||}| |}
| |
}||| |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)".
    Fra         Nrp   r   zrange() step can't be zerozbuiltins.enumeratezbuiltins.zipzbuiltins.reversedT)keysvaluesitemsr   r   )precompute_set_literal)+rt   ForAsyncIterable_analyze_iterable_item_typetype_to_rtyperf   r   r#   rd   re   r    get_dict_key_typeForDictionaryKeysr   r   calleer   r   r{   argsextract_intset	arg_kindsr   r   r   rG   ForRanger   r   r   ForEnumerateForZipr   r@   nameget_dict_value_typeForDictionaryValuesget_dict_item_typeForDictionaryItemsr   mypyc.irbuild.expressionr   ForIterable)r>   r?   r@   rU   r   rG   rE   r   r_   	async_obj	item_type
item_rtypertypri   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_objr\   r\   r]   rK   r  s   







"


"





rK   c                   @  s`   e Zd ZdZd$ddZd%ddZd&ddZd'ddZd'ddZd'ddZ	d'ddZ
d(d!d"Zd#S ))r   z-Abstract base class for generating for loops.r>   r)   r?   r
   rU   r   r   rG   rH   r   rF   rI   rJ   c                 C  s:   || _ || _|| _|| _|  r|st | _d S || _d S rs   )r>   r?   rU   rG   need_cleanupr   r   )selfr>   r?   rU   r   rG   r   r\   r\   r]   __init__  s   	
zForGenerator.__init__c                 C     dS )z0If this returns true, we need post-loop cleanup.Fr\   r   r\   r\   r]   r   %     zForGenerator.need_cleanuprX   c                 C  s2   |   r| j| j |   | j| dS dS )z!Add post-loop cleanup, if needed.N)r   r>   rO   r   gen_cleanuprR   )r   rX   r\   r\   r]   rS   )  s
   zForGenerator.add_cleanupc                 C  r   )z<Generate check for loop exit (e.g. exhaustion of iteration).Nr\   r   r\   r\   r]   rN   0      zForGenerator.gen_conditionc                 C  r   )z6Generate ops at the beginning of the body (if needed).Nr\   r   r\   r\   r]   rP   3  r   zForGenerator.begin_bodyc                 C  r   )z/Generate stepping to the next item (if needed).Nr\   r   r\   r\   r]   rQ   6  r   zForGenerator.gen_stepc                 C  r   )z'Generate post-loop cleanup (if needed).Nr\   r   r\   r\   r]   r   9  r   zForGenerator.gen_cleanupr@   Value | AssignmentTargetr   c                 C  s   | j j | j || j| jS )z>A helper to get collection length, used by several subclasses.)r>   r   rg   rG   )r   r@   r\   r\   r]   load_len<  s   zForGenerator.load_lenN)r>   r)   r?   r
   rU   r   r   r   rG   rH   r   rF   rI   rJ   rI   rF   )rX   r   rI   rJ   r   )r@   r   rI   r   )__name__
__module____qualname____doc__r   r   rS   rN   rP   rQ   r   r   r\   r\   r\   r]   r   	  s    






c                   @  L   e Zd ZdZdddZdddZdddZdddZdddZdddZ	dS )r   zIGenerate IR for a for loop over an arbitrary iterable (the general case).rI   rF   c                 C  r   NTr\   r   r\   r\   r]   r   D  r   zForIterable.need_cleanupr_   r   ri   r   rJ   c                 C  s8   | j }|t|g| j}|| ||| _|| _d S rs   )r>   rv   r6   rG   r   iter_targetri   r   r_   ri   r>   iter_regr\   r\   r]   rf   H  s
   

zForIterable.initc                 C  sH   | j }| j}|t|| j|g|| _|t| j| j	| j
tj d S rs   )r>   rG   rv   r7   rg   r   next_regr   r   r   rU   IS_ERRORr   r>   rG   r\   r\   r]   rN   R  s    zForIterable.gen_conditionc                 C  8   | j }| j}|| j| j|}||| j|| d S rs   r>   rG   coercer   ri   assignget_assignment_targetr?   r   r>   rG   r   r\   r\   r]   rP   \     zForIterable.begin_bodyc                 C     d S rs   r\   r   r\   r\   r]   rQ   f  r   zForIterable.gen_stepc                 C     | j tg | j d S rs   r>   rv   r3   rG   r   r\   r\   r]   r   j  s   zForIterable.gen_cleanupNr   r_   r   ri   r   rI   rJ   r   
r   r   r   r   r   rf   rN   rP   rQ   r   r\   r\   r\   r]   r   A  s    







r   c                   @  s8   e Zd ZdZddd	Zdd
dZdddZdddZdS )r   z"Generate IR for an async for loop.r_   r   ri   r   rI   rJ   c                 C  sB   | j }|t|g| j}|| ||| _|| _tt| _	d S rs   )
r>   rv   r4   rG   r   r   ri   r   r   stop_regr   r\   r\   r]   rf   u  s   
zForAsyncIterable.initc                   s   ddl mm} j jd fdd}d fdd	}d fd
d}| ||fd |fgd   tjjj	tj
 d S )Nr   )
emit_awaittransform_try_exceptrI   r   c                    s&     tttj}   ttj| S rs   )r   r   r'   r;   srcr   rc   )addr)r>   rG   r\   r]   except_match  s   z4ForAsyncIterable.gen_condition.<locals>.except_matchrJ   c                    s>     t jg}  | _ j  d d S Nr   )rv   r5   rg   r   r   r  r  false)	awaitabler>   r  rG   r   r\   r]   try_body  s   z0ForAsyncIterable.gen_condition.<locals>.try_bodyc                     s     j   d S rs   )r  r  truer\   )r>   rG   r   r\   r]   except_body  s   z3ForAsyncIterable.gen_condition.<locals>.except_body)rI   r   r   )mypyc.irbuild.statementr  r  r>   rG   r   r   r  r   rU   BOOL)r   r  r  r  r  r\   r  r]   rN     s    zForAsyncIterable.gen_conditionc                 C  r   rs   r  r  r\   r\   r]   rP     r  zForAsyncIterable.begin_bodyc                 C  r  rs   r\   r   r\   r\   r]   rQ     r   zForAsyncIterable.gen_stepNr
  r   r   r   r   r   rf   rN   rP   rQ   r\   r\   r\   r]   r   r  s    


"
r   targetc                 C  s0   t |jr| t||g|S | |d|gd|S )z.Emit a potentially unsafe index into a target.__getitem__N)r"   rc   rv   r9   gen_method_call)r>   r  r?   rG   r\   r\   r]   unsafe_index  s   
r  c                   @  s8   e Zd ZdZdd
dZdddZdddZdddZdS )re   zoGenerate optimized IR for a for loop over a sequence.

    Supports iterating in both forward and reverse.
    r_   r   ri   r   rb   rF   rI   rJ   c                 C  sZ   | j }|| _||| _|std}n|| | jtdd| j}||| _	|| _
d S )Nr   rp   -)r>   rb   r   expr_targetr   	binary_opr   rG   maybe_spill_assignablerh   ri   )r   r_   ri   rb   r>   	index_regr\   r\   r]   rf     s   

zForSequence.initc                 C  s   | j }| j}| jr(||| j|tdd|}t }|||| j	 |
| | | j}||| j||d|}||| j| j	 d S )Nr   z>=<)r>   rG   rb   r"  rg   rh   r   r   r   r   rO   r   r!  rU   )r   r>   rG   
comparisonsecond_checklen_regr\   r\   r]   rN     s   
zForSequence.gen_conditionc                 C  s\   | j }| j}t||| j||| j||}|sJ ||| j|	|| j
|| d S rs   )r>   rG   r  rg   r!  rh   r  r  r?   r  ri   )r   r>   rG   	value_boxr\   r\   r]   rP     s   
zForSequence.begin_bodyc                 C  sP   | j }| j}| jsdnd}|t|| j|t|tj	|}|
| j|| d S )Nrp   r   )r>   rG   rb   int_opr(   rg   rh   r   r   ADDr  )r   r>   rG   r   r   r\   r\   r]   rQ     s   zForSequence.gen_stepN)r_   r   ri   r   rb   rF   rI   rJ   r   r  r\   r\   r\   r]   re     s    


re   c                   @  sT   e Zd ZU dZded< ded< dddZdddZdddZdddZd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_oprI   rF   c                 C  r   r   r\   r   r\   r\   r]   r        z ForDictionaryCommon.need_cleanupr_   r   ri   r   rJ   c                 C  sd   | j }|| _||| _td}||| _|| | j| _|	| j
|g| j}||| _d S Nr   )r>   ri   r   r!  r   r#  offset_targetr   sizerv   r.  rG   r   )r   r_   ri   r>   offsetr   r\   r\   r]   rf   #  s   zForDictionaryCommon.initc                 C  s   | 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.rp   r   N)r>   rG   rv   r-  rg   r   r1  
next_tupler   r   r  r   rU   r   r  )r   r>   rG   
new_offsetshould_continuer\   r\   r]   rN   1  s   z!ForDictionaryCommon.gen_conditionc                 C  s6   | 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>   rG   rv   r,   rg   r!  r2  r   r\   r\   r]   rQ   B  s   zForDictionaryCommon.gen_stepc                 C  r  rs   r	  r   r\   r\   r]   r   P  s   zForDictionaryCommon.gen_cleanupNr   r
  r   )
r   r   r   r   __annotations__r   rf   rN   rQ   r   r\   r\   r\   r]   r,    s   
 



r,  c                   @  "   e Zd ZdZeZeZdddZdS )r   z:Generate optimized IR for a for loop over dictionary keys.rI   rJ   c                 C  F   | j }| j}|t| jd|}||| j||| j	|| d S Nr   
r>   rG   r   r   r4  r  r  r?   r  ri   )r   r>   rG   keyr\   r\   r]   rP   [     
zForDictionaryKeys.begin_bodyNr   )	r   r   r   r   r0   r-  r.   r.  rP   r\   r\   r\   r]   r   U  
    r   c                   @  r8  )r   z<Generate optimized IR for a for loop over dictionary values.rI   rJ   c                 C  r9  r:  r;  )r   r>   rG   valuer\   r\   r]   rP   n  r=  zForDictionaryValues.begin_bodyNr   )	r   r   r   r   r1   r-  r2   r.  rP   r\   r\   r\   r]   r   h  r>  r   c                   @  r8  )r   z;Generate optimized IR for a for loop over dictionary items.rI   rJ   c                 C  s   | j }| j}|t| jd|}|t| jd|}t| jts"J ||| jj	d |}||| jj	d |}|
| j}t|trft|jdkrP|d| ||jd || ||jd || d S |t||g|}|||| d S )Nr   r   r   rp   z'Expected a pair for dict item iteration)r>   rG   r   r   r4  r   ri   r   r  typesr  r?   r+   r{   r   r   r  r   )r   r>   rG   r<  r?  r  rvaluer\   r\   r]   rP     s   
zForDictionaryItems.begin_bodyNr   )	r   r   r   r   r/   r-  r-   r.  rP   r\   r\   r\   r]   r   {  r>  r   c                   @  s.   e Zd ZdZdd	d
ZdddZdddZdS )r   z;Generate optimized IR for a for loop over an integer range.r   r   r   r   rH   rI   rJ   c                 C  s   | 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$   rc   r(   r!   r   r   r  r#  r$  r  r?   rh   rg   rG   )r   r   r   r   r>   
index_typer$  r\   r\   r]   rf     s   
"zForRange.initc                 C  sX   | j }| j}| jdkrdnd}||| j||| j|||}||| j| j	 d S )Nr   r%  >)
r>   rG   r   r"  rg   r$  rB  r   rU   r   )r   r>   rG   cmpr&  r\   r\   r]   rN     s   zForRange.gen_conditionc                 C  s   | j }| j}t| jjr%t| jjr%|t|| j	|t
| jtj|}n||| j	|t
| jd|}|| j	|| || j|| d S )N+)r>   rG   r$   r   rc   r   r*  r(   rg   r$  r   r   r   r+  r"  r  rh   r   r>   rG   new_valr\   r\   r]   rQ     s"   	zForRange.gen_stepN)r   r   r   r   r   rH   rI   rJ   r   )r   r   r   r   rf   rN   rQ   r\   r\   r\   r]   r     s
    


r   c                   @  s$   e Zd ZdZd	ddZd	ddZdS )
ForInfiniteCounterzAGenerate optimized IR for a for loop counting from 0 to infinity.rI   rJ   c                 C  s>   | j }td}||| _|| j| _|| j|| j d S r0  )	r>   r   r#  r$  r  r?   rh   r  rG   )r   r>   zeror\   r\   r]   rf     s
   zForInfiniteCounter.initc                 C  sR   | j }| j}|t|| j|tdtj|}|	| j|| |	| j
|| d S r   )r>   rG   r*  r(   rg   r$  r   r   r+  r  rh   rG  r\   r\   r]   rQ     s   zForInfiniteCounter.gen_stepNr   )r   r   r   r   rf   rQ   r\   r\   r\   r]   rI    s    
	rI  c                   @  sL   e Zd ZdZdddZdddZdddZdddZdddZdddZ	dS )r   zIGenerate optimized IR for a for loop of form "for i, x in enumerate(it)".rI   rF   c                 C  r   r   r\   r   r\   r\   r]   r     r/  zForEnumerate.need_cleanupindex1r
   index2r@   r   rJ   c              	   C  sL   t | j|| j| j| jdd| _| j  t| j||| j| j| jdd| _d S )NTr   )	rI  r>   rU   r   rG   	index_genrf   rK   main_gen)r   rK  rL  r@   r\   r\   r]   rf     s   
zForEnumerate.initc                 C  s   | j   d S rs   )rO  rN   r   r\   r\   r]   rN     s   zForEnumerate.gen_conditionc                 C     | j   | j  d S rs   )rN  rP   rO  r   r\   r\   r]   rP        
zForEnumerate.begin_bodyc                 C  rP  rs   )rN  rQ   rO  r   r\   r\   r]   rQ     rQ  zForEnumerate.gen_stepc                 C  rP  rs   )rN  r   rO  r   r\   r\   r]   r   
  rQ  zForEnumerate.gen_cleanupNr   )rK  r
   rL  r
   r@   r   rI   rJ   r   r  r\   r\   r\   r]   r     s    




r   c                   @  r   )r   z?Generate IR for a for loop of form `for x, ... in zip(a, ...)`.rI   rF   c                 C  r   r   r\   r   r\   r\   r]   r     r/  zForZip.need_cleanupindexeslist[Lvalue]exprsr   rJ   c              
   C  s   t |t |ks
J dd tt |d D | jg | _g | _t||| jD ]\}}}t| j|||| j| j	dd}| j
| q&d S )Nc                 S  s   g | ]}t  qS r\   )r   )r   _r\   r\   r]   
<listcomp>  r   zForZip.init.<locals>.<listcomp>rp   TrM  )r{   rangerU   cond_blocksgensr   rK   r>   r   rG   append)r   rR  rT  r?   r@   
next_blockrk   r\   r\   r]   rf     s   $zForZip.initc                 C  sD   t | jD ]\}}|  |t| jd k r| j| j|  qd S r   )	enumeraterY  rN   r{   r>   rO   rX  )r   irk   r\   r\   r]   rN   #  s   zForZip.gen_conditionc                 C     | j D ]}|  qd S rs   )rY  rP   r   rk   r\   r\   r]   rP   )     

zForZip.begin_bodyc                 C  r^  rs   )rY  rQ   r_  r\   r\   r]   rQ   -  r`  zForZip.gen_stepc                 C  r^  rs   )rY  r   r_  r\   r\   r]   r   1  r`  zForZip.gen_cleanupNr   )rR  rS  rT  r   rI   rJ   r   r  r\   r\   r\   r]   r     s    




r   )r>   r)   r?   r
   r@   r   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   )r>   r)   r?   r
   r@   r   r_   r   rA   r`   rG   rH   rI   rJ   )
r>   r)   rk   r	   rl   rm   rn   r<   rI   ro   )r>   r)   rk   r	   rI   r   )r>   r)   rk   r   rI   rF   )
r>   r)   r   r   r   r   rG   rH   rI   rJ   )r@   r   rI   rF   )FF)r>   r)   r?   r
   r@   r   rU   r   r   r   rG   rH   rE   rF   r   rF   rI   r   )
r>   r)   r  r   r?   r   rG   rH   rI   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.   r/   r0   r1   r2   mypyc.primitives.exc_opsr3   mypyc.primitives.generic_opsr4   r5   r6   r7   mypyc.primitives.list_opsr8   r9   r:   mypyc.primitives.misc_opsr;   mypyc.primitives.registryr<   mypyc.primitives.set_opsr=   rB   r^   rj   r   r   r   r   r   r   rK   r   r   r   r  re   r,  r   r   r   r   rI  r   r   r\   r\   r\   r]   <module>   sR    84<$	

>
1
0



C 81
@NJ9$