
    hi                       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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/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8 dd	l9m:Z:m;Z; dd
l<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC ddlDmEZEmFZF ddlGmHZHmIZImJZJmKZK ddlLmMZMmNZNmOZO ddlPmQZQ ddlRmSZS ddlTmUZU ddlVmWZW ddlXmYZY eg df         ZZd^d$Z[d_d(Z\d`d0Z]dad1Z^dbd3Z_dad4Z`dcd9Zaddd;Zb	 	 dedfdBZc G dC dA          Zd G dD dEed          Ze G dF dGed          Zf G dH dIed          ZgdgdKZh G dL dMed          Zi G dN dOed          Zj G dP dQej          Zk G dR dSej          Zl G dT dUej          Zm G dV dWed          Zn G dX dYed          Zo G dZ d[ed          Zp G d\ d]ed          ZqdS )hzHelpers 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)	ERR_NEVER
BasicBlockBranchIntegerIntOpLoadAddressLoadErrorValueLoadMem
MethodCallRaiseStandardErrorRegisterTupleGetTupleSetValue)	RInstanceRTupleRTypebool_rprimitivec_pyssize_t_rprimitiveint_rprimitiveis_dict_rprimitiveis_fixed_width_rtypeis_immutable_rprimitiveis_list_rprimitiveis_sequence_rprimitiveis_short_int_rprimitiveis_str_rprimitiveis_tuple_rprimitiveobject_pointer_rprimitiveobject_rprimitivepointer_rprimitiveshort_int_rprimitive)	IRBuilder)GENERATOR_HELPER_NAME)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propagate_if_error_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_op)str_get_item_unsafe_op)tuple_get_item_unsafe_opNbuilderr2   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)rR   )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)rK   rL   rM   rN   rP   rR   rT   
body_block
step_block
else_block
exit_blocknormal_loop_exitfor_gencondition_blocks                 i/var/www/html/Pagina-Ingenieria-Requisitos/venv/lib/python3.11/site-packages/mypyc/irbuild/for_helpers.pyfor_loop_helperrk   V   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initrZ   r[   r\   r]   r^   readindex_targetr_   r`   ra   rb   )rK   rL   rM   rm   rN   rT   target_typerc   rd   rf   ri   rh   s               rj   for_loop_helper_with_indexry      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!!!
###:&&&&&rl   genr
   empty_op_llbuilderCallable[[Value, int], Value]set_item_oprG   Value | Nonec                "    t          j                  dk    rt          j                  dk    rt          j        d                   dk    r                     j        d                   }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/a_str/a_bytes)
          (2) list(f(x) for x in a_list/a_tuple/a_str/a_bytes)
          (3) [f(x) for x in a_list/a_tuple/a_str/a_bytes]
    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   rV   rW   c                z                         j                  }                    | |gj                   d S N)accept	left_exprcall_crT   )r   erK   rz   r}   	target_ops     rj   set_itemz<sequence_from_generator_preallocate_helper.<locals>.set_item   s;    NN3=11{Y
A,FQQQQQrl   N)r   r   rV   rW   )len	sequencesindices	condlists	node_typer*   r   rK   builtin_lenrT   ry   )	rK   rz   r{   r}   rtypesequencelengthr   r   s	   `` `    @rj   *sequence_from_generator_preallocate_helperr      s3   : 3=Q3s{#3#3q#8#8SqAQ=R=RVW=W=W!!#-"233!%(( 	~~cmA&677H_0038SW0XXF**638<<IR R R R R R R R R 'Qq)98Xsx   4rl   c                    t                     r                                 S t            j        j        t
                    }||S                                           g j                            t          t          j        j        j        j                            }d fd}t           ||j                                                  S )N)r{   r}   rV   rW   c                                          j                  }                     t                                        | gj                   d S r   )r   r   primitive_oprC   rv   rT   )r   rK   rz   list_opss    rj   gen_inner_stmtsz5translate_list_comprehension.<locals>.gen_inner_stmts  sG    NN3=))^gll8.D.Da-H#(SSSSSrl   rV   rW   ))raise_error_if_contains_unreachable_namesnoner   rK   new_list_op_with_lengthrE   maybe_spillnew_list_oprT   listzipr   r   r   rR   comprehension_helperrv   )rK   rz   valloop_paramsr   r   s   ``   @rj   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<<!!!rl   '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 r   )
isinstancer   node).0ss     rj   	<genexpr>z<raise_error_if_contains_unreachable_names.<locals>.<genexpr>  s7      
K
K!:a""5qv~
K
K
K
K
K
Krl   z+mypyc internal error: should be unreachableTF)anyr   r   RUNTIME_ERRORrT   add)rK   rz   errors      rj   r   r     s`     
K
Ks{
K
K
KKK ",9H
 

 	Et5rl   c                z    t                     r                                 S                                           g j                            t          t          j        j        j	        j
                            }d fd}t           ||j                                                  S )NrV   rW   c                                          j                  }                     t                                        | gj                   d S r   )r   r   r   rH   rv   rT   )r   rK   rz   set_opss    rj   r   z4translate_set_comprehension.<locals>.gen_inner_stmts*  sG    NN3=))Z',,w*?*?)CSXNNNNNrl   r   )r   r   r   
new_set_oprT   r   r   r   r   r   rR   r   rv   )rK   rz   r   r   r   s   ``  @rj   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<<   rl   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   rV   rW   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 Nr    )condsloop_contentsr   s   rj   <lambda>z;comprehension_helper.<locals>.handle_loop.<locals>.<lambda>N  s    MM%QRR99 rl   N)rR   rT   )rk   )r   rL   rM   rR   r   rK   rT   r   s   `   @rj   handle_loopz)comprehension_helper.<locals>.handle_loopC  s^     (31~$tUH999999	
 	
 	
 	
 	
 	
rl   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)r   r   add_bool_branchr]   nonlocal_controlgen_continuerT   r[   )	r   r   condcond_val
cont_block
rest_blockrK   r   r   s	         rj   r   z+comprehension_helper.<locals>.loop_contentsT  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rl   N)r   r   rV   rW   )r   r   r   r   rV   rW   r   )rK   r   r   rT   r   r   s   ` ``@@rj   r   r   2  so    "
 
 
 
 
 
 
 
"       < Krl   r   c                b    | j         dk    p$t          | j        t                    o
| j         dk    S )Nzbuiltins.rangezsix.moves.xrange)fullnamer   r   r   )rM   s    rj   is_range_refr   u  s6    )) 	0di++ 0M//rl   Frc   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)}  || |          }||}||                      |          }|j*        }t          |tV                    r4|j,        -                    t\                    rt_          | |||||          }nta          | |||||          }|                     |          }
|                     |
          }|                    ||           |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)".
    Frp         Nr   r   zrange() step can't be zerozbuiltins.enumeratezbuiltins.zipzbuiltins.reversedT)keysvaluesitemsr   r   )precompute_set_literal)1r   ForAsyncIterable_analyze_iterable_item_typetype_to_rtyperu   r   r*   rs   rt   r&   get_dict_key_typeForDictionaryKeysr   r   calleer   r   r   argsextract_intset	arg_kindsr   r   r   rT   ForRanger   r   r   ForEnumerateForZipr   rM   nameget_dict_value_typeForDictionaryValuesget_dict_item_typeForDictionaryItemsr   mypyc.irbuild.expressionr   rr   r    class_ir
has_methodr3   ForNativeGeneratorForIterable) rK   rL   rM   rc   r   rT   rR   r   rm   	async_obj	item_type
item_rtypertyprx   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itfor_objs                                    rj   rY   rY   }  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		B"i   SR[%;%;<Q%R%R S$WeZDRXYY guj)T6RR33D99I&&y11JLL"J///Nrl   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.rK   r2   rL   r   rc   r   r   rT   rU   r   rS   rV   rW   c                    || _         || _        || _        || _        |                                 r|st                      | _        d S || _        d S r   )rK   rL   rc   rT   need_cleanupr   r   )selfrK   rL   rc   r   rT   r   s          rj   __init__zForGenerator.__init__  sY     
$	  	'v 	''\\DNNN 'DNNNrl   c                    dS )z0If this returns true, we need post-loop cleanup.Fr   r	  s    rj   r  zForGenerator.need_cleanup8  s    url   rf   c                    |                                  rO| j                            | j                   |                                  | j                            |           dS dS )z!Add post-loop cleanup, if needed.N)r  rK   r]   r   gen_cleanupr`   )r	  rf   s     rj   ra   zForGenerator.add_cleanup<  sf     	*L''777Lj)))))	* 	*rl   c                    dS )z<Generate check for loop exit (e.g. exhaustion of iteration).Nr   r  s    rj   r\   zForGenerator.gen_conditionC        rl   c                    dS )z6Generate ops at the beginning of the body (if needed).Nr   r  s    rj   r^   zForGenerator.begin_bodyF  r  rl   c                    dS )z/Generate stepping to the next item (if needed).Nr   r  s    rj   r_   zForGenerator.gen_stepI  r  rl   c                    dS )z'Generate post-loop cleanup (if needed).Nr   r  s    rj   r  zForGenerator.gen_cleanupL  r  rl   rM   Value | AssignmentTargetr   c                    | j         j                             | j                             || j                  | j        d          S )z>A helper to get collection length, used by several subclasses.Tr   )rK   r   rv   rT   )r	  rM   s     rj   load_lenzForGenerator.load_lenO  sA    |#//LdDI..	 0 
 
 	
rl   N)rK   r2   rL   r   rc   r   r   r   rT   rU   r   rS   rV   rW   rV   rS   )rf   r   rV   rW   r   )rM   r  rV   r   )__name__
__module____qualname____doc__r
  r  ra   r\   r^   r_   r  r  r   rl   rj   r   r     s        77' ' ' '2   * * * *K K K KE E E E> > > >6 6 6 6
 
 
 
 
 
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 IR for a for loop over an arbitrary iterable (the general case).rV   rS   c                    dS NTr   r  s    rj   r  zForIterable.need_cleanupY      trl   rm   r   rx   r"   rW   c                    | j         }|                    t          |g| j                  }|                    |           |                    |          | _        || _        d S r   )rK   r   rA   rT   r   iter_targetrx   r	  rm   rx   rK   iter_regs        rj   ru   zForIterable.init]  s^     ,''(TYGGH%%%"..x88&rl   c                   | j         }| j        }|                    t          |                    | j        |          g|          | _        |                    t          | j        | j	        | j
        t          j                             d S r   )rK   rT   r   rB   rv   r!  next_regr   r   r   rc   IS_ERRORr	  rK   rT   s      rj   r\   zForIterable.gen_conditiong  sm    
 ,ywd>NPT1U1U0VX\]]F4=$.$/6?[[\\\\\rl   c                    | j         }| j        }|                    | j        | j        |          }|                    |                    | j                  ||           d S r   rK   rT   coercer%  rx   assignget_assignment_targetrL   r	  rK   rT   r%  s       rj   r^   zForIterable.begin_bodyq  Z     ,y >>$-1A4HHw44TZ@@(DQQQQQrl   c                    d S r   r   r  s    rj   r_   zForIterable.gen_step{      rl   c                R    | j                             t          g | j                   d S r   rK   r   r=   rT   r  s    rj   r  zForIterable.gen_cleanup  s'    
 	.DI>>>>>rl   Nr  rm   r   rx   r"   rV   rW   r   
r  r  r  r  r  ru   r\   r^   r_   r  r   rl   rj   r   r   V  s        SS   ' ' ' '] ] ] ]R R R R   ? ? ? ? ? ?rl   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   z3Generate IR for a for loop over a native generator.rV   rS   c                    dS r  r   r  s    rj   r  zForNativeGenerator.need_cleanup  r  rl   rm   r   rx   r"   rW   c                V    | j         }|                    |          | _        || _        d S r   )rK   r   r!  rx   )r	  rm   rx   rK   s       rj   ru   zForNativeGenerator.init  s/     ,"..x88&rl   c           	        | j         }| j        }t          t                    | _        |                    t          t          d                    }|                    | j        ||           |                    t          t          | j                            }|
                                }t          |                    | j                  t          |||||g|          }t          |_        |                    |          | _        |                    t%          | j        | j        | j        t$          j                             d S )NT)	undefines)rK   rT   r   r/   return_valuer   r   r+  r   r.   none_objectr   rv   r!  r3   r   
error_kindr%  r   r   rc   r&  )r	  rK   rT   errptrnnhelper_calls          rj   r\   z ForNativeGenerator.gen_condition  s   ,y$%677kk.):dKKKLLt(#t444 kk+&?ARSSTT  "" LL)**,ABBPRTWCXZ^
 
 "+K00F4=$.$/6?[[\\\\\rl   c                    | j         }| j        }|                    | j        | j        |          }|                    |                    | j                  ||           d S r   r)  r-  s       rj   r^   zForNativeGenerator.begin_body  r.  rl   c                    d S r   r   r  s    rj   r_   zForNativeGenerator.gen_step  r0  rl   c                ^    | j                             t          | j        g| j                   d S r   )rK   r   r>   r:  rT   r  s    rj   r  zForNativeGenerator.gen_cleanup  s-     	!!"7$:K9LdiXXXXXrl   Nr  r3  r   r4  r   rl   rj   r   r     s        ==   ' ' ' '] ] ] ].R R R R   Y Y Y Y Y Yrl   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.rm   r   rx   r"   rV   rW   c                    | j         }|                    t          |g| j                  }|                    |           |                    |          | _        || _        t          t                    | _	        d S r   )
rK   r   r?   rT   r   r!  rx   r   r#   stop_regr"  s        rj   ru   zForAsyncIterable.init  si    
 ,>>(XJ	BBH%%%"..x88& 11rl   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_exceptrV   r   c                                          t          t          t          j                            }                      t          t          j        | d                    S )NT)borrow)r   r   r0   rF   srcr   rr   )addrrK   rT   s    rj   except_matchz4ForAsyncIterable.gen_condition.<locals>.except_match  sL    ;;{+=?V?Z\`aabbD;;w'>'CTRVWWWXXXrl   rW   c                                          t                              j                  g          }  |           _                            j                                        d           d S Nr   )r   r@   rv   r!  r%  r+  rF  false)	awaitablerK   rH  rT   r	  s    rj   try_bodyz0ForAsyncIterable.gen_condition.<locals>.try_body  sf    x',,t?O2P2P1QSWXXI&Jw	4@@DMNN4='--//2>>>>>rl   c                 d                          j                                                    d S r   )r+  rF  true)rK   rT   r	  s   rj   except_bodyz3ForAsyncIterable.gen_condition.<locals>.except_body  s)    NN4=',,..$?????rl   )rV   r   r   )mypyc.irbuild.statementrH  rI  rK   rT   r   r   rF  r   rc   BOOL)r	  rI  rN  rS  rV  rK   rH  rT   s   `    @@@rj   r\   zForAsyncIterable.gen_condition  s    	MLLLLLLL,y	Y 	Y 	Y 	Y 	Y 	Y 	Y	? 	? 	? 	? 	? 	? 	? 	? 	?
	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	X,!5t[ IJDRV	
 	
 	
 	F4=$.$/6;WWXXXXXrl   c                    | j         }| j        }|                    | j        | j        |          }|                    |                    | j                  ||           d S r   r)  r-  s       rj   r^   zForAsyncIterable.begin_body  r.  rl   c                    d S r   r   r  s    rj   r_   zForAsyncIterable.gen_step  r0  rl   Nr3  r   )r  r  r  r  ru   r\   r^   r_   r   rl   rj   r   r     ss        ,,
2 
2 
2 
2 Y  Y  Y  YDR R R R     rl   r   targetc                b   t          |j                  r|                     t          ||g|          S t	          |j                  r|                     t          ||g|          S t          |j                  r|                     t          ||g|          S | 	                    |d|gd|          S )z.Emit a potentially unsafe index into a target.__getitem__N)
r)   rr   r   rD   r-   r   rJ   r,   rI   gen_method_call)rK   r[  rL   rT   s       rj   unsafe_indexr_     s    
 &+&& S##$;fe_dSSS	V[	)	) S~~6NNN	6;	'	' S~~4vuotLLL&&v}ugtTRRRrl   c                  >    e Zd ZU dZded<   ddZddZddZddZdS )rt   zoGenerate optimized IR for a for loop over a sequence.

    Supports iterating in both forward and reverse.
    zValue | AssignmentTarget | None
length_regrm   r   rx   r"   rq   rS   rV   rW   c                N   t          |j                  s
J |            | j        }|| _        |                    |          | _        t          |j                  r3|                    |                     | j                            | _        nd | _        |st          dt                    }nW| j        |                    | j                  }n|                     | j                  }|j                            |d          }|                    |          | _        || _        d S )Nr   r   )r*   rr   rK   rq   r   expr_targetr(   r  ra  r   r$   rv   int_submaybe_spill_assignablerw   rx   )r	  rm   rx   rq   rK   	index_reglen_vals          rj   ru   zForSequence.init  s   %hm44>>h>>>, #..x88"8=11 	#%11$--@P2Q2QRRDOO #DO 	<&q*@AAII*!,,t77--(899//;;I#::9EE&rl   c                H   | j         }| j        }| j        r}|                    |                    | j        |          t          d          d|          }t                      }|                    ||| j	                   |
                    |           | j        |                     | j                  }n|                    | j        |          }|                    |                    | j        |          |d|          }|                    || j        | j	                   d S )Nr   z>=<)rK   rT   rq   	binary_oprv   rw   r   r   r   r   r]   ra  r  rc  rc   )r	  rK   rT   
comparisonsecond_checklen_regs         rj   r\   zForSequence.gen_condition1  s   ,y< 
	1
 !**T.55wqzz4 J &<<L##JdnMMM""<000?" mmD$455GG ll4?D99G&&w||D4Et'L'LgWZ\`aa

DOT^LLLLLrl   c                D   | j         }| j        }t          ||                    | j        |          |                    | j        |          |          }|sJ |                    |                    | j                  |	                    || j
        |          |           d S r   )rK   rT   r_  rv   rc  rw   r+  r,  rL   r*  rx   )r	  rK   rT   	value_boxs       rj   r^   zForSequence.begin_bodyJ  s    ,y LL)400LL*D11	
 
	  	))$*55NN9d&6==	
 	
 	
 	
 	
rl   c                    | j         }| j        }| j        sdnd}|j                             |                    | j        |          |          }|                    | j        ||           d S )Nr   r   )rK   rT   rq   int_addrv   rw   r+  )r	  rK   rT   r   r   s        rj   r_   zForSequence.gen_step^  sg    ,y,qq"o%%gll43Dd&K&KTRRt(#t44444rl   N)rm   r   rx   r"   rq   rS   rV   rW   r   )	r  r  r  r  __annotations__ru   r\   r^   r_   r   rl   rj   rt   rt     s          
 0///' ' ' '4M M M M2
 
 
 
(5 5 5 5 5 5rl   rt   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_oprV   rS   c                    dS r  r   r  s    rj   r  z ForDictionaryCommon.need_cleanupz  	     trl   rm   r   rx   r"   rW   c                   | j         }|| _        |                    |          | _        t	          d          }|                    |          | _        |                    |                     | j                            | _        |	                    | j
        |g| j                  }|                    |          | _        d S Nr   )rK   rx   r   rc  r   re  offset_targetr  sizer   rv  rT   r!  )r	  rm   rx   rK   offsetr#  s         rj   ru   zForDictionaryCommon.init  s    ,& #..x88$;;FCC''d6F(G(GHH	 >>$"3hZKK"..x88rl   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.r   r   N)rK   rT   r   ru  rv   r!  r{  
next_tupler   r   r+  r   rc   r   rX  )r	  rK   rT   
new_offsetshould_continues        rj   r\   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Zrl   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)rK   rT   r   r6   rv   rc  r|  r'  s      rj   r_   zForDictionaryCommon.gen_step  sa    
 ,y\\$*D117<<	43P3PQ	
 	
 	
 	
 	
rl   c                R    | j                             t          g | j                   d S r   r2  r  s    rj   r  zForDictionaryCommon.gen_cleanup  s%    .DI>>>>>rl   Nr  r3  r   )
r  r  r  r  rr  r  ru   r\   r_   r  r   rl   rj   rt  rt  g  s           10000000   
9 9 9 9[ [ [ ["
 
 
 
? ? ? ? ? ?rl   rt  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.rV   rW   c                   | j         }| j        }|                    t          | j        d|                    }|                    |                    | j                  |                    || j	        |          |           d S Nr   
rK   rT   r   r   r  r+  r,  rL   r*  rx   )r	  rK   rT   keys       rj   r^   zForDictionaryKeys.begin_body  s}    ,y kk(4?At<<==))$*55NN3 0$77	
 	
 	
 	
 	
rl   Nr   )	r  r  r  r  r:   ru  r8   rv  r^   r   rl   rj   r   r     s8        DD#L#L

 

 

 

 

 

rl   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.rV   rW   c                   | j         }| j        }|                    t          | j        d|                    }|                    |                    | j                  |                    || j	        |          |           d S r  r  )r	  rK   rT   values       rj   r^   zForDictionaryValues.begin_body  s}    ,y HT_a>>??))$*55NN5$"2D99	
 	
 	
 	
 	
rl   Nr   )	r  r  r  r  r;   ru  r<   rv  r^   r   rl   rj   r   r     s8        FF%L%L

 

 

 

 

 

rl   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.rV   rW   c                r   | j         }| j        }|                    t          | j        d|                    }|                    t          | j        d|                    }t          | j        t                    sJ | 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   r   z'Expected a pair for dict item iteration)rK   rT   r   r   r  r   rx   r!   r*  typesr,  rL   r5   r   r   r   r+  r   )r	  rK   rT   r  r  r[  rvalues          rj   r^   zForDictionaryItems.begin_body  s   ,ykk(4?At<<==HT_a>>?? $*F33EET5EEEE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0rl   Nr   )	r  r  r  r  r9   ru  r7   rv  r^   r   rl   rj   r   r     s8        EE$L$L1 1 1 1 1 1rl   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   rU   rV   rW   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 rP  )rK   r   r   r   r   
end_targetr+   rr   r1   r'   r%   r   r+  re  rf  r,  rL   rw   rv   rT   )r	  r   r   r   rK   
index_typerf  s          rj   ru   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\]]]]]rl   c                   | j         }| j        }| j        dk    rdnd}|                    |                    | j        |          |                    | j        |          ||          }|                    || j        | j	                   d S )Nr   ri  >)
rK   rT   r   rj  rv   rf  r  r   rc   r   )r	  rK   rT   cmprk  s        rj   r\   zForRange.gen_condition  s    ,yY]]cc&&LL..T_d0S0SUXZ^
 

 	
DOT^LLLLLrl   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+)rK   rT   r+   r   rr   r   int_opr1   rv   rf  r   r   r   ADDrj  r+  rw   r	  rK   rT   new_vals       rj   r_   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8rl   N)r   r   r   r   r   rU   rV   rW   r   )r  r  r  r  ru   r\   r_   r   rl   rj   r   r     s^        EE^ ^ ^ ^&M M M M9 9 9 9 9 9rl   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.rV   rW   c                    | j         }t          d          }|                    |          | _        |                    | j                  | _        d S rz  )rK   r   re  rf  r,  rL   rw   )r	  rK   zeros      rj   ru   zForInfiniteCounter.init1  sK    , qzz 77==9@9V9VW[Wa9b9brl   c                    | j         }| j        }|                    t          |                    | j        |          t          d          t          j        |          }|	                    | j        ||           d S r   )
rK   rT   r  r1   rv   rf  r   r   r  r+  r  s       rj   r_   zForInfiniteCounter.gen_step9  sk    ,y .. ',,t~t"D"DgajjRWR[]a
 
 	t~w55555rl   c                    | j                             | j        | j                             | j                  | j                   d S r   )rK   r+  rw   rv   rf  rT   r  s    rj   r^   zForInfiniteCounter.begin_bodyD  s;    D-t|/@/@/P/PRVR[\\\\\rl   Nr   )r  r  r  r  ru   r_   r^   r   rl   rj   r  r  .  s`        KKc c c c	6 	6 	6 	6] ] ] ] ] ]rl   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 optimized IR for a for loop of form "for i, x in enumerate(it)".rV   rS   c                    dS r  r   r  s    rj   r  zForEnumerate.need_cleanupK  rx  rl   index1r   index2rM   r	   rW   c           	         t          | j        || j        | j        | j        d          | _        | j                                         t          | j        ||| j        | j        | j        d          | _        d S )NTr   )	r  rK   rc   r   rT   	index_genru   rY   main_gen)r	  r  r  rM   s       rj   ru   zForEnumerate.initP  sv    +L&$/4>49UY
 
 
 	/L&$[_
 
 
rl   c                8    | j                                          d S r   )r  r\   r  s    rj   r\   zForEnumerate.gen_condition[  s    ##%%%%%rl   c                j    | j                                          | j                                         d S r   )r  r^   r  r  s    rj   r^   zForEnumerate.begin_body_  s0    !!###  """""rl   c                j    | j                                          | j                                         d S r   )r  r_   r  r  s    rj   r_   zForEnumerate.gen_stepc  s0    !!!     rl   c                j    | j                                          | j                                         d S r   )r  r  r  r  s    rj   r  zForEnumerate.gen_cleanupg  s0    ""$$$!!#####rl   Nr  )r  r   r  r   rM   r	   rV   rW   r   r4  r   rl   rj   r   r   H  s        SS   
	
 	
 	
 	
& & & &# # # #! ! ! !$ $ $ $ $ $rl   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, ...)`.rV   rS   c                    dS r  r   r  s    rj   r  zForZip.need_cleanupo  rx  rl   indexeslist[Lvalue]exprsr   rW   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     rj   
<listcomp>zForZip.init.<locals>.<listcomp>x  s    JJJQJLLJJJrl   r   Tr  )r   rangerc   cond_blocksgensr   rY   rK   r   rT   append)r	  r  r  rL   rM   
next_blockrz   s          rj   ru   zForZip.initt  s    7||s5zz)))) KJ%Gq8H2I2IJJJdoM^^(*	'*7E4;K'L'L 	" 	"#E4)eT:t~tyY]  C IS!!!!		" 	"rl   c                    t          | j                  D ]Y\  }}|                                 |t          | j                  dz
  k     r%| j                            | j        |                    Zd S r   )	enumerater  r\   r   rK   r]   r  )r	  irz   s      rj   r\   zForZip.gen_condition  sv    	** 	A 	AFAs3ty>>A%%%++D,<Q,?@@@	A 	Arl   c                B    | j         D ]}|                                 d S r   )r  r^   r	  rz   s     rj   r^   zForZip.begin_body  s0    9 	 	CNN	 	rl   c                B    | j         D ]}|                                 d S r   )r  r_   r  s     rj   r_   zForZip.gen_step  s,    9 	 	CLLNNNN	 	rl   c                B    | j         D ]}|                                 d S r   )r  r  r  s     rj   r  zForZip.gen_cleanup  s0    9 	 	COO	 	rl   Nr  )r  r  r  r   rV   rW   r   r4  r   rl   rj   r   r   l  s        II   

" 
" 
" 
"A A A A           rl   r   )rK   r2   rL   r   rM   r	   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   )rK   r2   rL   r   rM   r	   rm   r   rN   rn   rT   rU   rV   rW   )
rK   r2   rz   r
   r{   r|   r}   rG   rV   r~   )rK   r2   rz   r
   rV   r   )rK   r2   rz   r   rV   rS   )
rK   r2   r   r   r   r   rT   rU   rV   rW   )rM   r   rV   rS   )FF)rK   r2   rL   r   rM   r	   rc   r   r   r   rT   rU   rR   rS   r   rS   rV   r   )
rK   r2   r[  r   rL   r   rT   rU   rV   r   )rr  
__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   r   r   r   mypyc.ir.rtypesr    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   mypyc.irbuild.builderr2   mypyc.irbuild.preparer3   mypyc.irbuild.targetsr4   r5   mypyc.primitives.dict_opsr6   r7   r8   r9   r:   r;   r<   mypyc.primitives.exc_opsr=   r>   mypyc.primitives.generic_opsr?   r@   rA   rB   mypyc.primitives.list_opsrC   rD   rE   mypyc.primitives.misc_opsrF   mypyc.primitives.registryrG   mypyc.primitives.set_opsrH   mypyc.primitives.str_opsrI   mypyc.primitives.tuple_opsrJ   rO   rk   ry   r   r   r   r   r   r   rY   r   r   r   r   r_  rt   rt  r   r   r   r   r  r   r   r   rl   rj   <module>r     s    # " " " " " % % % % % % % %                                                                                                  ( , + + + + + 7 7 7 7 7 7 I I I I I I I I                  O N N N N N N N 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    \ \ \ \ \~7
 7
 7
 7
 7
 7
 7
 7
t.? .? .? .? .?, .? .? .?b6Y 6Y 6Y 6Y 6Y 6Y 6Y 6Yr= = = = =| = = =@S S S SU5 U5 U5 U5 U5, U5 U5 U5p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$ $ $ $ $\ $ $ $ $ $rl   