
    5 f`                       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 ddl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,m-Z-m.Z. dd	l/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 dd
l7m8Z8 ddl9m:Z:m;Z;m<Z<m=Z= ddl>m?Z?m@Z@mAZA ddlBmCZC ddlDmEZE ddlFmGZG eg df         ZHdWd!ZIdXd%ZJdYd-ZKdZd.ZLdZd/ZMd[d4ZNd\d6ZO	 	 d]d^d=ZP G d> d<          ZQ G d? d@eQ          ZR G dA dBeQ          ZSd_dDZT G dE dFeQ          ZU G dG dHeQ          ZV G dI dJeV          ZW G dK dLeV          ZX G dM dNeV          ZY G dO dPeQ          ZZ G dQ dReQ          Z[ G dS dTeQ          Z\ G dU dVeQ          Z]dS )`zHelpers 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
ExpressionGeneratorExprLvalue
MemberExprRefExprSetExpr	TupleExpr	TypeAlias)

BasicBlockBranchIntegerIntOpLoadAddressLoadMem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)rC   )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>   r?   rA   rC   rE   
body_block
step_block
else_block
exit_blocknormal_loop_exitfor_gencondition_blocks                 O/var/www/equiseq/venv/lib/python3.11/site-packages/mypyc/irbuild/for_helpers.pyfor_loop_helperr\   H   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initrK   rL   rM   rN   rO   readindex_targetrP   rQ   rR   rS   )r<   r=   r>   r^   r?   rE   target_typerT   rU   rW   rZ   rY   s               r[   for_loop_helper_with_indexrj      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   rG   rH   c                z                         j                  }                    | |gj                   d S N)accept	left_exprcall_crE   )rs   er<   rk   rn   	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)rs   r   rG   rH   )len	sequencesindices	condlists	node_typer    r$   r#   rv   r<   builtin_lenrE   rj   )	r<   rk   rl   rn   rtypesequencelengthr{   rz   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            j        j        t                    }||S                                           g j                            t          t          j	        j
        j        j                            }d fd}t           ||j                                                  S )N)rl   rn   rG   rH   c                                          j                  }                     t                                        | gj                   d S ru   )rv   rw   rx   r6   rg   rE   )ry   r<   rk   list_opss    r[   gen_inner_stmtsz5translate_list_comprehension.<locals>.gen_inner_stmts   sE    NN3=))~X(>(>'BCHMMMMMr]   rG   rH   )r   r<   new_list_op_with_lengthr8   maybe_spillnew_list_oprE   listzipr~   r}   r   rC   comprehension_helperrg   )r<   rk   valloop_paramsr   r   s   ``   @r[   translate_list_comprehensionr      s    
4"?B(	  C 
""7#6#6r38#D#DEEHs3;s}clSSTTKN N N N N N N N +III<<!!!r]   c                2                                               g j                            t          t	          j        j        j        j                            }d fd}t           ||j                    
                              S )NrG   rH   c                                          j                  }                     t                                        | gj                   d S ru   )rv   rw   rx   r;   rg   rE   )ry   r<   rk   set_opss    r[   r   z4translate_set_comprehension.<locals>.gen_inner_stmts  sE    NN3=))zGLL$9$91#=sxHHHHHr]   r   )r   
new_set_oprE   r   r   r~   r}   r   rC   r   rg   )r<   rk   r   r   r   s   ``  @r[   translate_set_comprehensionr      s    !!'"4"4R"B"BCCGs3;s}clSSTTKI I I I I I I I +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   rG   rH   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 Nrq    )condsloop_contentsr   s   r[   <lambda>z;comprehension_helper.<locals>.handle_loop.<locals>.<lambda>&  s    MM%QRR99 r]   N)rC   rE   )r\   )r   r=   r>   rC   r   r<   rE   r   s   `   @r[   handle_loopz)comprehension_helper.<locals>.handle_loop  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)rv   r   add_bool_branchrN   nonlocal_controlgen_continuerE   rL   )	r   r   condcond_val
cont_block
rest_blockr<   r   r   s	         r[   r   z+comprehension_helper.<locals>.loop_contents,  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   rG   rH   )r   r   r   r   rG   rH   r   )r<   r   r   rE   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)fullname
isinstancenoder   )r>   s    r[   is_range_refr   M  s6    )) 	0di++ 0M//r]   FrT   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)".
    Fra         Nrq   r   zrange() step can't be zerozbuiltins.enumeratezbuiltins.zipzbuiltins.reversedT)keysvaluesitemsr   r   )precompute_set_literal)+rv   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   errorrE   ForRanger   r   r   ForEnumerateForZipr   r>   nameget_dict_value_typeForDictionaryValuesget_dict_item_typeForDictionaryItemsr   mypyc.irbuild.expressionr   ForIterable)r<   r=   r>   rT   r   rE   rC   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_objs                                  r[   rJ   rJ   U  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
   rT   r   r   rE   rF   r   rD   rG   rH   c                    || _         || _        || _        || _        |                                 r|st                      | _        d S || _        d S ru   )r<   r=   rT   rE   need_cleanupr   r   )selfr<   r=   rT   r   rE   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]   rW   c                    |                                  rO| j                            | j                   |                                  | j                            |           dS dS )z!Add post-loop cleanup, if needed.N)r   r<   rN   r   gen_cleanuprQ   )r   rW   s     r[   rR   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[   rM   zForGenerator.gen_condition        r]   c                    dS )z6Generate ops at the beginning of the body (if needed).Nr   r   s    r[   rO   zForGenerator.begin_body  r   r]   c                    dS )z/Generate stepping to the next item (if needed).Nr   r   s    r[   rP   zForGenerator.gen_step  r   r]   c                    dS )z'Generate post-loop cleanup (if needed).Nr   r   s    r[   r   zForGenerator.gen_cleanup  r   r]   r>   Value | AssignmentTargetr   c                    | j         j                             | j                             || j                  | j                  S )z>A helper to get collection length, used by several subclasses.)r<   r   rg   rE   )r   r>   s     r[   load_lenzForGenerator.load_len  s5    |#//0A0A$	0R0RTXT]^^^r]   N)r<   r'   r=   r
   rT   r   r   r   rE   rF   r   rD   rG   rH   rG   rD   )rW   r   rG   rH   r   )r>   r   rG   r   )__name__
__module____qualname____doc__r   r   rR   rM   rO   rP   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).rG   rD   c                    dS NTr   r   s    r[   r   zForIterable.need_cleanup'  s    tr]   r^   r   ri   r   rH   c                    | j         }|                    t          |g| j                  }|                    |           |                    |          | _        || _        d S ru   )r<   rx   r4   rE   r   iter_targetri   r   r^   ri   r<   iter_regs        r[   rf   zForIterable.init+  s\     ,>>'H:tyAAH%%%"..x88&r]   c                   | j         }| j        }|                    t          |                    | j        |          g|          | _        |                    t          | j        | j	        | j
        t          j                             d S ru   )r<   rE   rx   r5   rg   r   next_regaddr   r   rT   IS_ERRORr   r<   rE   s      r[   rM   zForIterable.gen_condition5  sm    
 ,ywd>NPT1U1U0VX\]]F4=$.$/6?[[\\\\\r]   c                    | j         }| j        }|                    | j        | j        |          }|                    |                    | j                  ||           d S ru   r<   rE   coercer  ri   assignget_assignment_targetr=   r   r<   rE   r  s       r[   rO   zForIterable.begin_body?  Z     ,y >>$-1A4HHw44TZ@@(DQQQQQr]   c                    d S ru   r   r   s    r[   rP   zForIterable.gen_stepI      r]   c                R    | j                             t          g | j                   d S ru   r<   rx   r1   rE   r   s    r[   r   zForIterable.gen_cleanupM  s'    
 	.DI>>>>>r]   Nr   r^   r   ri   r   rG   rH   r   
r   r   r   r   r   rf   rM   rO   rP   r   r   r]   r[   r   r   $  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.r^   r   ri   r   rG   rH   c                    | j         }|                    t          |g| j                  }|                    |           |                    |          | _        || _        t          t                    | _	        d S ru   )
r<   rx   r2   rE   r   r   ri   r   r   stop_regr  s        r[   rf   zForAsyncIterable.initX  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_exceptrG   r   c                                          t          t          t          j                            }                      t          t          j        |                     S ru   )r  r   r%   r9   srcr   rc   )addrr<   rE   s    r[   except_matchz4ForAsyncIterable.gen_condition.<locals>.except_matcht  sF    ;;{+=?V?Z\`aabbD;;w'>'CTJJKKKr]   rH   c                                          t                              j                  g          }  |           _                            j                                        d           d S Nr   )rx   r3   rg   r   r  r  r  false)	awaitabler<   r  rE   r   s    r[   try_bodyz0ForAsyncIterable.gen_condition.<locals>.try_bodyx  sf    x',,t?O2P2P1QSWXXI&Jw	4@@DMNN4='--//2>>>>>r]   c                 d                          j                                                    d S ru   )r  r  true)r<   rE   r   s   r[   except_bodyz3ForAsyncIterable.gen_condition.<locals>.except_body}  s)    NN4=',,..$?????r]   )rG   r   r   )mypyc.irbuild.statementr  r  r<   rE   r  r   r  r   rT   BOOL)r   r  r  r#  r&  r<   r  rE   s   `    @@@r[   rM   zForAsyncIterable.gen_conditiond  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 ru   r	  r  s       r[   rO   zForAsyncIterable.begin_body  r  r]   c                    d S ru   r   r   s    r[   rP   zForAsyncIterable.gen_step  r  r]   Nr  r   r   r   r   r   rf   rM   rO   rP   r   r]   r[   r   r   U  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    rc   rx   r7   gen_method_call)r<   r,  r=   rE   s       r[   unsafe_indexr0    sP    
 &+&& S~~5MMM&&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 )re   zoGenerate optimized IR for a for loop over a sequence.

    Supports iterating in both forward and reverse.
    r^   r   ri   r   rb   rD   rG   rH   c                @   | j         }|| _        |                    |          | _        |st	          d          }nB|                    |                     | j                  t	          d          d| j                  }|                    |          | _	        || _
        d S )Nr   rq   -)r<   rb   r   expr_targetr   	binary_opr   rE   maybe_spill_assignablerh   ri   )r   r^   ri   rb   r<   	index_regs         r[   rf   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<   rE   rb   r5  rg   rh   r   r   r   r   rN   r   r4  rT   )r   r<   rE   
comparisonsecond_checklen_regs         r[   rM   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 ru   )r<   rE   r0  rg   r4  rh   r  r  r=   r
  ri   )r   r<   rE   	value_boxs       r[   rO   zForSequence.begin_body  s    ,y LL)400LL*D11	
 
	 y 	))$*55NN9d&6==	
 	
 	
 	
 	
r]   c                   | j         }| j        }| j        sdnd}|                    t          |                    | j        |          t          |          t          j	        |          }|
                    | j        ||           d S )Nrq   r   )r<   rE   rb   int_opr&   rg   rh   r   r   ADDr  )r   r<   rE   r   r  s        r[   rP   zForSequence.gen_step  s|    ,y,qq"nn LL*D11DMMI
 
 	t(#t44444r]   N)r^   r   ri   r   rb   rD   rG   rH   r   r+  r   r]   r[   re   re     sr         
' ' ' ' M M M M*
 
 
 
(5 5 5 5 5 5r]   re   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_oprG   rD   c                    dS r   r   r   s    r[   r   z ForDictionaryCommon.need_cleanup  	     tr]   r^   r   ri   r   rH   c                   | j         }|| _        |                    |          | _        t	          d          }|                    |          | _        |                    |                     | j                            | _        |	                    | j
        |g| j                  }|                    |          | _        d S Nr   )r<   ri   r   r4  r   r6  offset_targetr   sizerx   rE  rE   r   )r   r^   ri   r<   offsetr  s         r[   rf   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.rq   r   N)r<   rE   rx   rD  rg   r   rJ  
next_tupler  r   r  r   rT   r   r(  )r   r<   rE   
new_offsetshould_continues        r[   rM   z!ForDictionaryCommon.gen_condition  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<   rE   rx   r*   rg   r4  rK  r  s      r[   rP   zForDictionaryCommon.gen_step%  sa    
 ,y\\$*D117<<	43P3PQ	
 	
 	
 	
 	
r]   c                R    | j                             t          g | j                   d S ru   r  r   s    r[   r   zForDictionaryCommon.gen_cleanup3  s%    .DI>>>>>r]   Nr   r  r   )
r   r   r   r   __annotations__r   rf   rM   rP   r   r   r]   r[   rC  rC    s           10000000   
9 9 9 9[ [ [ ["
 
 
 
? ? ? ? ? ?r]   rC  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.rG   rH   c                   | j         }| j        }|                    t          | j        d|                    }|                    |                    | j                  |                    || j	        |          |           d S Nr   
r<   rE   r  r   rN  r  r  r=   r
  ri   )r   r<   rE   keys       r[   rO   zForDictionaryKeys.begin_body>  s}    ,y kk(4?At<<==))$*55NN3 0$77	
 	
 	
 	
 	
r]   Nr   )	r   r   r   r   r.   rD  r,   rE  rO   r   r]   r[   r   r   8  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.rG   rH   c                   | j         }| j        }|                    t          | j        d|                    }|                    |                    | j                  |                    || j	        |          |           d S rV  rW  )r   r<   rE   values       r[   rO   zForDictionaryValues.begin_bodyQ  s}    ,y HT_a>>??))$*55NN5$"2D99	
 	
 	
 	
 	
r]   Nr   )	r   r   r   r   r/   rD  r0   rE  rO   r   r]   r[   r   r   K  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.rG   rH   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   rq   z'Expected a pair for dict item iteration)r<   rE   r  r   rN  r   ri   r   r
  typesr  r=   r)   r|   r   r   r  r   )r   r<   rE   rX  r[  r,  rvalues          r[   rO   zForDictionaryItems.begin_bodyd  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   r-   rD  r+   rE  rO   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   rF   rG   rH   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"   rc   r&   r   r   r   r  r6  r7  r  r=   rh   rg   rE   )r   r   r   r   r<   
index_typer7  s          r[   rf   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   r9  >)
r<   rE   r   r5  rg   r7  rb  r   rT   r   )r   r<   rE   cmpr:  s        r[   rM   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<   rE   r"   r   rc   r   r@  r&   rg   r7  r   r   r   rA  r5  r  rh   r   r<   rE   new_vals       r[   rP   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   rF   rG   rH   r   )r   r   r   r   rf   rM   rP   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S )ForInfiniteCounterzAGenerate optimized IR for a for loop counting from 0 to infinity.rG   rH   c                    | j         }t          d          }|                    |          | _        |                    | j                  | _        |                    | j        || j                   d S rI  )	r<   r   r6  r7  r  r=   rh   r  rE   )r   r<   zeros      r[   rf   zForInfiniteCounter.init  sf    , qzz 77==9@9V9VW[Wa9b9bt($	:::::r]   c                .   | j         }| j        }|                    t          |                    | j        |          t          d          t          j        |          }|	                    | j        ||           |	                    | j
        ||           d S r   )r<   rE   r@  r&   rg   r7  r   r   rA  r  rh   ri  s       r[   rP   zForInfiniteCounter.gen_step  s    ,y .. ',,t~t"D"DgajjRWR[]a
 
 	t~w555t('488888r]   Nr   )r   r   r   r   rf   rP   r   r]   r[   rl  rl    sB        KK; ; ; ;
9 
9 
9 
9 
9 
9r]   rl  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)".rG   rD   c                    dS r   r   r   s    r[   r   zForEnumerate.need_cleanup  rG  r]   index1r
   index2r>   r   rH   c           	         t          | j        || j        | j        | j        d          | _        | j                                         t          | j        ||| j        | j        | j        d          | _        d S )NTr   )	rl  r<   rT   r   rE   	index_genrf   rJ   main_gen)r   rr  rs  r>   s       r[   rf   zForEnumerate.init  sv    +L&$/4>49UY
 
 
 	/L&$[_
 
 
r]   c                8    | j                                          d S ru   )rw  rM   r   s    r[   rM   zForEnumerate.gen_condition  s    ##%%%%%r]   c                j    | j                                          | j                                         d S ru   )rv  rO   rw  r   s    r[   rO   zForEnumerate.begin_body  s0    !!###  """""r]   c                j    | j                                          | j                                         d S ru   )rv  rP   rw  r   s    r[   rP   zForEnumerate.gen_step  s0    !!!     r]   c                j    | j                                          | j                                         d S ru   )rv  r   rw  r   s    r[   r   zForEnumerate.gen_cleanup  s0    ""$$$!!#####r]   Nr   )rr  r
   rs  r
   r>   r   rG   rH   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, ...)`.rG   rD   c                    dS r   r   r   s    r[   r   zForZip.need_cleanup  rG  r]   indexeslist[Lvalue]exprsr   rH   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   ).0_s     r[   
<listcomp>zForZip.init.<locals>.<listcomp>  s    JJJQJLLJJJr]   rq   Tru  )r|   rangerT   cond_blocksgensr   rJ   r<   r   rE   append)r   r~  r  r=   r>   
next_blockrk   s          r[   rf   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  rM   r|   r<   rN   r  )r   irk   s      r[   rM   zForZip.gen_condition  sv    	** 	A 	AFAs3ty>>A%%%++D,<Q,?@@@	A 	Ar]   c                B    | j         D ]}|                                 d S ru   )r  rO   r   rk   s     r[   rO   zForZip.begin_body  s0    9 	 	CNN	 	r]   c                B    | j         D ]}|                                 d S ru   )r  rP   r  s     r[   rP   zForZip.gen_step  s,    9 	 	CLLNNNN	 	r]   c                B    | j         D ]}|                                 d S ru   )r  r   r  s     r[   r   zForZip.gen_cleanup  s0    9 	 	COO	 	r]   Nr   )r~  r  r  r   rG   rH   r   r  r   r]   r[   r   r     s        II   

" 
" 
" 
"A A A A           r]   r   )r<   r'   r=   r
   r>   r   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   )r<   r'   r=   r
   r>   r   r^   r   r?   r_   rE   rF   rG   rH   )
r<   r'   rk   r	   rl   rm   rn   r:   rG   ro   )r<   r'   rk   r	   rG   r   )
r<   r'   r   r   r   r   rE   rF   rG   rH   )r>   r   rG   rD   )FF)r<   r'   r=   r
   r>   r   rT   r   r   r   rE   rF   rC   rD   r   rD   rG   r   )
r<   r'   r,  r   r=   r   rE   rF   rG   r   )^r   
__future__r   typingr   r   
mypy.nodesr   r   r   r	   r
   r   r   r   r   r   mypyc.ir.opsr   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-   r.   r/   r0   mypyc.primitives.exc_opsr1   mypyc.primitives.generic_opsr2   r3   r4   r5   mypyc.primitives.list_opsr6   r7   r8   mypyc.primitives.misc_opsr9   mypyc.primitives.registryr:   mypyc.primitives.set_opsr;   r@   r\   rj   r   r   r   r   r   rJ   r   r   r   r0  re   rC  r   r   r   r   rl  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- - - -`" " " ".	! 	! 	! 	!@ @ @ @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9 9 9 9 9 9 9 92!$ !$ !$ !$ !$< !$ !$ !$H$ $ $ $ $\ $ $ $ $ $r]   