
    miÿ                       d Z ddlmZ ddl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mZmZmZ ddlmZmZmZmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ ddlAmBZB dd	lCmDZD dd
lEmFZFmGZG ddlHmIZImJZJmKZKmLZLmMZMmNZNmOZO ddlPmQZQmRZR ddlSmTZTmUZUmVZVmWZW ddlXmYZYmZZZm[Z[ ddl\m]Z] ddl^m_Z_ ddl`maZa ddlbmcZc ddldmeZe eg df         Zfddd%Zgded*Zhdfd2Zidgd3Zjdhd5Zkdgd6Zldid;Zmdjd=Zn	 	 dkdldDZo G dE dC          Zp G dF dGep          Zq G dH dIep          Zr G dJ dKep          ZsdmdMZt G dN dOep          Zu G dP dQep          Zv G dR dSev          Zw G dT dUev          Zx G dV dWev          Zy G dX dYep          Zz G dZ d[ep          Z{ G d\ d]ep          Z| G d^ d_ep          Z}dndaZ~dodcZdS )pzHelpers 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cast)ARG_POS	BytesExprCallExprDictionaryComprehension
ExpressionGeneratorExprListExprLvalue
MemberExprNameExprRefExprSetExprStarExprStrExpr	TupleExpr	TypeAliasVar)LiteralType	TupleTypeget_proper_typeget_proper_types)	ERR_NEVER
BasicBlockBranchIntegerIntOpLoadAddressLoadErrorValueLoadLiteral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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)r]   )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)rV   rW   rX   rY   r[   r]   r_   
body_block
step_block
else_block
exit_blocknormal_loop_exitfor_gencondition_blocks                 q/var/www/html/bestrading.cuttalo.com/scripts/kaggle-env/lib/python3.11/site-packages/mypyc/irbuild/for_helpers.pyfor_loop_helperrv   ]   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]lengthc                   t          |j                  sJ ||j        f            |                     |          }t                      }t                      }	t                      }
t                      }t	          | |||
|d          }|                    ||d|           |                     |	|
           t          |t                    r6|j	        dk    r+| 
                    |           |                     |           n|                     |           |                                 |                     |           |                                  ||                     |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)reverserz   r   N)r5   typeget_sequence_typer   ForSequenceinitre   
isinstancer   valuerk   rh   rf   rg   ri   readindex_targetrj   rl   rm   )rV   rW   rX   rx   rY   r_   rz   target_typern   ro   rq   rt   rs   s                ru   for_loop_helper_with_indexr      s   ( "(-00KK8X]2KKKK++D11KJJJ llO'5*j$NNGLL;fLEEEJ
333&'"" 3v|a'7'7Z   ////!!/222:&&&Jw||G011222j)))LL!!!
###:&&&&&rw   genr   empty_op_llbuilderCallable[[Value, int], Value]set_item_oprR   Value | Nonec           	     
    t          j                  dk    rt          j                  dk    rt          j        d                   dk    rj        j        d         }                     |          }t          |          st          |t                    sdS t          |t                    rt           j
        |                   }t          |t                    s
J |            |j        sdS t          |j                  }t          d |D                       rd |D             }n1                     |          fdt!          |          D             }t#          t%           j        |                    }	                     |	          n                     |          t+           |d          }
 ||
          d fd}t-           j        d         |||
           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/an_rtuple)
          (2) list(f(x) for x in a_list/a_tuple/a_str/a_bytes/an_rtuple)
          (3) [f(x) for x in a_list/a_tuple/a_str/a_bytes/an_rtuple]

    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   Nc              3  @   K   | ]}t          |t                    V  d S N)r   r   .0typs     ru   	<genexpr>z=sequence_from_generator_preallocate_helper.<locals>.<genexpr>  s,      HHC:c;//HHHHHHrw   c                h    g | ]/}t          t          t          |          j        t                    0S  )r#   r   r   r   r:   r   s     ru   
<listcomp>z>sequence_from_generator_preallocate_helper.<locals>.<listcomp>  s@            [# 6 6 <>OPP     rw   c                    g | ]E\  }}t          |t                    rt          |j        t                    nt          |          FS r   )r   r   r#   r   r:   r(   )r   ir   r_   sequences      ru   r   z>sequence_from_generator_preallocate_helper.<locals>.<listcomp>
  s_           3 &c;779CI/@AAA%h488	     rw   Tuse_pyssize_t
item_indexr*   ra   rb   c                p                         j                  }                    | |g           d S r   )accept	left_exprcall_c)r   erV   r   r_   r   	target_ops     ru   set_itemz<sequence_from_generator_preallocate_helper.<locals>.set_item  s9    s}--ANN;J(BDIIIIIrw   )r   r*   ra   rb   )len	sequencesindices	condlistsr_   	node_typer5   r   r,   r   typesr   itemsr   allr   	enumeratelistmapadd	new_tupleget_expr_length_valuer   )rV   r   r   r   sequence_exprrtypeproper_typeproper_typesget_item_opsr   rz   r   r_   r   r   s   `` `        @@@ru   *sequence_from_generator_preallocate_helperr      sn   8 3=Q3s{#3#3q#8#8SqAQ=R=RVW=W=Wxa(!!-00&u-- 	E61J1J 	4eV$$ "	5 *'-*FGGKk955BB{BBB $ t+K,=>>L HH<HHHHH    +      #>>-88          #,L"9"9      W[,7788E((55HH ~~m44H&wx]abbb&&vt44		J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	#S[^]Hhf	
 	
 	
 4rw   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   ra   rb   c                                          j                  }                     t                                        | gj                   d S r   )r   r   primitive_oprN   r   r_   )r   rV   r   list_opss    ru   gen_inner_stmtsz5translate_list_comprehension.<locals>.gen_inner_stmts;  sG    NN3=))^gll8.D.Da-H#(SSSSSrw   ra   rb   ))raise_error_if_contains_unreachable_namesnoner   rV   new_list_op_with_lengthrP   maybe_spillnew_list_opr_   r   zipr   r   r   r]   comprehension_helperr   )rV   r   valloop_paramsr   r   s   ``   @ru   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<<!!!rw   '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   )r   r   node)r   ss     ru   r   z<raise_error_if_contains_unreachable_names.<locals>.<genexpr>K  s7      
K
K!:a""5qv~
K
K
K
K
K
Krw   z+mypyc internal error: should be unreachableTF)anyr   r&   RUNTIME_ERRORr_   r   )rV   r   errors      ru   r   r   C  s`     
K
Ks{
K
K
KKK ",9H
 

 	Et5rw   c                z    t                     r                                 S                                           g j                            t          t          j        j        j	        j
                            }d fd}t           ||j                                                  S )Nra   rb   c                                          j                  }                     t                                        | gj                   d S r   )r   r   r   rS   r   r_   )r   rV   r   set_opss    ru   r   z4translate_set_comprehension.<locals>.gen_inner_stmts^  sG    NN3=))Z',,w*?*?)CSXNNNNNrw   r   )r   r   r   
new_set_opr_   r   r   r   r   r   r]   r   r   )rV   r   r   r   r   s   ``  @ru   translate_set_comprehensionr   W  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<<   rw   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   ra   rb   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   r   )condsloop_contentsr   s   ru   <lambda>z;comprehension_helper.<locals>.handle_loop.<locals>.<lambda>  s    MM%QRR99 rw   N)r]   r_   )rv   )r   rW   rX   r]   r   rV   r_   r   s   `   @ru   handle_loopz)comprehension_helper.<locals>.handle_loopw  s^     (31~$tUH999999	
 	
 	
 	
 	
 	
rw   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_branchrh   nonlocal_controlgen_continuer_   rf   )	r   r   condcond_val
cont_block
rest_blockrV   r   r   s	         ru   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rw   N)r   r   ra   rb   )r   r   r   r   ra   rb   r   )rV   r   r   r_   r   r   s   ` ``@@ru   r   r   f  so    "
 
 
 
 
 
 
 
"       < Krw   r   c                b    | j         dk    p$t          | j        t                    o
| j         dk    S )Nzbuiltins.rangezsix.moves.xrange)fullnamer   r   r   )rX   s    ru   is_range_refr     s6    )) 	0di++ 0M//rw   Frn   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)".
    F)r|         Nr   r   zrange() step can't be zerozbuiltins.enumeratezbuiltins.zipzbuiltins.reversedT)keysvaluesr   r   r   )precompute_set_literal)1r   ForAsyncIterable_analyze_iterable_item_typetype_to_rtyper   r   r5   r~   r   r1   get_dict_key_typeForDictionaryKeysr   r	   calleer   r   r   argsextract_intset	arg_kindsr   r   r   r_   ForRanger   r   r   ForEnumerateForZipr   rX   nameget_dict_value_typeForDictionaryValuesget_dict_item_typeForDictionaryItemsr   mypyc.irbuild.expressionr   r}   r+   class_ir
has_methodr>   ForNativeGeneratorForIterable) rV   rW   rX   rn   r   r_   r]   r   rx   	async_obj	item_type
item_rtypertypr   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                                    ru   rd   rd     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///Nrw   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.rV   r=   rW   r   rn   r   r   r_   r`   r   r^   ra   rb   c                    || _         || _        || _        || _        |                                 r|st                      | _        d S || _        d S r   )rV   rW   rn   r_   need_cleanupr   r   )selfrV   rW   rn   r   r_   r   s          ru   __init__zForGenerator.__init__S  sY     
$	  	'v 	''\\DNNN 'DNNNrw   c                    dS )z0If this returns true, we need post-loop cleanup.Fr   r$  s    ru   r#  zForGenerator.need_cleanupl  s    urw   rq   c                    |                                  rO| j                            | j                   |                                  | j                            |           dS dS )z!Add post-loop cleanup, if needed.N)r#  rV   rh   r   gen_cleanuprk   )r$  rq   s     ru   rl   zForGenerator.add_cleanupp  sf     	*L''777Lj)))))	* 	*rw   c                    dS )z<Generate check for loop exit (e.g. exhaustion of iteration).Nr   r'  s    ru   rg   zForGenerator.gen_conditionw        rw   c                    dS )z6Generate ops at the beginning of the body (if needed).Nr   r'  s    ru   ri   zForGenerator.begin_bodyz  r+  rw   c                    dS )z/Generate stepping to the next item (if needed).Nr   r'  s    ru   rj   zForGenerator.gen_step}  r+  rw   c                    dS )z'Generate post-loop cleanup (if needed).Nr   r'  s    ru   r)  zForGenerator.gen_cleanup  r+  rw   rX   Value | AssignmentTargetr*   c                    | j         j                             | j                             || j                  | j        d          S )z>A helper to get collection length, used by several subclasses.Tr   )rV   builtin_lenr   r_   )r$  rX   s     ru   load_lenzForGenerator.load_len  sA    |#//LdDI..	 0 
 
 	
rw   N)rV   r=   rW   r   rn   r   r   r   r_   r`   r   r^   ra   rb   ra   r^   )rq   r   ra   rb   r   )rX   r/  ra   r*   )__name__
__module____qualname____doc__r%  r#  rl   rg   ri   rj   r)  r2  r   rw   ru   r   r   P  s        77' ' ' '2   * * * *K K K KE E E E> > > >6 6 6 6
 
 
 
 
 
rw   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).ra   r^   c                    dS NTr   r'  s    ru   r#  zForIterable.need_cleanup      trw   rx   r*   r   r-   rb   c                    | j         }|                    t          |g| j                  }|                    |           |                    |          | _        || _        d S r   )rV   r   rL   r_   r   iter_targetr   r$  rx   r   rV   iter_regs        ru   r   zForIterable.init  s^     ,''(TYGGH%%%"..x88&rw   c                   | j         }| j        }|                    t          |                    | j        |          g|          | _        |                    t          | j        | j	        | j
        t          j                             d S r   )rV   r_   r   rM   r   r=  next_regr   r   r   rn   IS_ERRORr$  rV   r_   s      ru   rg   zForIterable.gen_condition  sm    
 ,ywd>NPT1U1U0VX\]]F4=$.$/6?[[\\\\\rw   c                    | j         }| j        }|                    | j        | j        |          }|                    |                    | j                  ||           d S r   rV   r_   coercerA  r   assignget_assignment_targetrW   r$  rV   r_   rA  s       ru   ri   zForIterable.begin_body  Z     ,y >>$-1A4HHw44TZ@@(DQQQQQrw   c                    d S r   r   r'  s    ru   rj   zForIterable.gen_step      rw   c                R    | j                             t          g | j                   d S r   rV   r   rH   r_   r'  s    ru   r)  zForIterable.gen_cleanup  s'    
 	.DI>>>>>rw   Nr3  rx   r*   r   r-   ra   rb   r   
r4  r5  r6  r7  r#  r   rg   ri   rj   r)  r   rw   ru   r  r    s        SS   ' ' ' '] ] ] ]R R R R   ? ? ? ? ? ?rw   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.ra   r^   c                    dS r:  r   r'  s    ru   r#  zForNativeGenerator.need_cleanup  r;  rw   rx   r*   r   r-   rb   c                V    | j         }|                    |          | _        || _        d S r   )rV   r   r=  r   )r$  rx   r   rV   s       ru   r   zForNativeGenerator.init  s/     ,"..x88&rw   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)rV   r_   r'   r:   return_valuer   r"   rG  r!   r9   none_objectr%   r   r=  r>   r   
error_kindrA  r   r   rn   rB  )r$  rV   r_   errptrnnhelper_calls          ru   rg   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?[[\\\\\rw   c                    | j         }| j        }|                    | j        | j        |          }|                    |                    | j                  ||           d S r   rE  rI  s       ru   ri   zForNativeGenerator.begin_body  rJ  rw   c                    d S r   r   r'  s    ru   rj   zForNativeGenerator.gen_step  rL  rw   c                ^    | j                             t          | j        g| j                   d S r   )rV   r   rI   rV  r_   r'  s    ru   r)  zForNativeGenerator.gen_cleanup  s-     	!!"7$:K9LdiXXXXXrw   Nr3  rO  r   rP  r   rw   ru   r  r    s        ==   ' ' ' '] ] ] ].R R R R   Y Y Y Y Y Yrw   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.rx   r*   r   r-   ra   rb   c                    | j         }|                    t          |g| j                  }|                    |           |                    |          | _        || _        t          t                    | _	        d S r   )
rV   r   rJ   r_   r   r=  r   r'   r.   stop_regr>  s        ru   r   zForAsyncIterable.init  si    
 ,>>(XJ	BBH%%%"..x88& 11rw   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_exceptra   r*   c                                          t          t          t          j                            }                      t          t          j        | d                    S )NT)borrow)r   r!   r;   rQ   srcr$   r}   )addrrV   r_   s    ru   except_matchz4ForAsyncIterable.gen_condition.<locals>.except_match  sL    ;;{+=?V?Z\`aabbD;;w'>'CTRVWWWXXXrw   rb   c                                          t                              j                  g          }  |           _                            j                                        d           d S Nr   )r   rK   r   r=  rA  rG  rb  false)	awaitablerV   rd  r_   r$  s    ru   try_bodyz0ForAsyncIterable.gen_condition.<locals>.try_body  sf    x',,t?O2P2P1QSWXXI&Jw	4@@DMNN4='--//2>>>>>rw   c                 d                          j                                                    d S r   )rG  rb  true)rV   r_   r$  s   ru   except_bodyz3ForAsyncIterable.gen_condition.<locals>.except_body  s)    NN4=',,..$?????rw   )ra   r*   r   )mypyc.irbuild.statementrd  re  rV   r_   r   r   rb  r   rn   BOOL)r$  re  rj  ro  rr  rV   rd  r_   s   `    @@@ru   rg   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Xrw   c                    | j         }| j        }|                    | j        | j        |          }|                    |                    | j                  ||           d S r   rE  rI  s       ru   ri   zForAsyncIterable.begin_body%  rJ  rw   c                    d S r   r   r'  s    ru   rj   zForAsyncIterable.gen_step/  rL  rw   NrO  r   )r4  r5  r6  r7  r   rg   ri   rj   r   rw   ru   r   r     ss        ,,
2 
2 
2 
2 Y  Y  Y  YDR R R R     rw   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)
r4   r}   r   rO   r8   r   rU   r7   rT   gen_method_call)rV   rw  rW   r_   s       ru   unsafe_indexr{  4  s    
 &+&& S##$;fe_dSSS	V[	)	) S~~6NNN	6;	'	' S~~4vuotLLL&&v}ugtTRRRrw   c                  B    e Zd ZU dZded<   	 dddZddZddZddZdS )r   zoGenerate optimized IR for a for loop over a sequence.

    Supports iterating in both forward and reverse.
    zValue | AssignmentTarget | None
length_regNrx   r*   r   r-   r|   r^   rz   r   ra   rb   c                x   t          |j                  sJ ||j        f            | j        }|| _        || _        |                    |          | _        t          |j                  r:|                    | j        p|                     | j                            | _	        nd | _	        |st          dt                    }nW| j	        |                    | j	                  }n|                     | j                  }|j                            |d          }|                    |          | _        || _        d S )Nr   r   )r5   r}   rV   rz   r|   r   expr_targetr3   r2  r}  r   r/   r   int_submaybe_spill_assignabler   r   )r$  rx   r   r|   rz   rV   	index_reglen_vals           ru   r   zForSequence.initK  s$    &hm44OOx6OOOO, #..x88"8=11 	#%11$+2`tO_A`A`aaDOO #DO 	<&q*@AAII*!,,t77--(899//;;I#::9EE&rw   c                H   | j         }| j        }| j        r}|                    |                    | j        |          t          d          d|          }t                      }|                    ||| j	                   |
                    |           | j        |                     | j                  }n|                    | j        |          }|                    |                    | j        |          |d|          }|                    || j        | j	                   d S )Nr   z>=<)rV   r_   r|   	binary_opr   r   r   r   r   r   rh   r}  r2  r  rn   )r$  rV   r_   
comparisonsecond_checklen_regs         ru   rg   zForSequence.gen_conditioni  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Lrw   c                D   | j         }| j        }t          ||                    | j        |          |                    | j        |          |          }|sJ |                    |                    | j                  |	                    || j
        |          |           d S r   )rV   r_   r{  r   r  r   rG  rH  rW   rF  r   )r$  rV   r_   	value_boxs       ru   ri   zForSequence.begin_body  s    ,y LL)400LL*D11	
 
	  	))$*55NN9d&6==	
 	
 	
 	
 	
rw   c                    | j         }| j        }| j        sdnd}|j                             |                    | j        |          |          }|                    | j        ||           d S )Nr   r   )rV   r_   r|   int_addr   r   rG  )r$  rV   r_   r  r   s        ru   rj   zForSequence.gen_step  sg    ,y,qq"o%%gll43Dd&K&KTRRt(#t44444rw   r   )
rx   r*   r   r-   r|   r^   rz   r   ra   rb   r   )	r4  r5  r6  r7  __annotations__r   rg   ri   rj   r   rw   ru   r   r   C  s          
 0/// Z^' ' ' ' '<M M M M0
 
 
 
(5 5 5 5 5 5rw   r   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_opra   r^   c                    dS r:  r   r'  s    ru   r#  z ForDictionaryCommon.need_cleanup  	     trw   rx   r*   r   r-   rb   c                   | j         }|| _        |                    |          | _        t	          d          }|                    |          | _        |                    |                     | j                            | _        |	                    | j
        |g| j                  }|                    |          | _        d S Nr   )rV   r   r   r  r   r  offset_targetr2  sizer   r  r_   r=  )r$  rx   r   rV   offsetr?  s         ru   r   zForDictionaryCommon.init  s    ,& #..x88$;;FCC''d6F(G(GHH	 >>$"3hZKK"..x88rw   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)rV   r_   r   r  r   r=  r  
next_tupler   r(   rG  r   rn   r   rt  )r$  rV   r_   
new_offsetshould_continues        ru   rg   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Zrw   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)rV   r_   r   rA   r   r  r  rC  s      ru   rj   zForDictionaryCommon.gen_step  sa    
 ,y\\$*D117<<	43P3PQ	
 	
 	
 	
 	
rw   c                R    | j                             t          g | j                   d S r   rN  r'  s    ru   r)  zForDictionaryCommon.gen_cleanup  s%    .DI>>>>>rw   Nr3  rO  r   )
r4  r5  r6  r7  r  r#  r   rg   rj   r)  r   rw   ru   r  r    s           10000000   
9 9 9 9[ [ [ ["
 
 
 
? ? ? ? ? ?rw   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 keys.ra   rb   c                   | j         }| j        }|                    t          | j        d|                    }|                    |                    | j                  |                    || j	        |          |           d S Nr   
rV   r_   r   r(   r  rG  rH  rW   rF  r   )r$  rV   r_   keys       ru   ri   zForDictionaryKeys.begin_body  s}    ,y kk(4?At<<==))$*55NN3 0$77	
 	
 	
 	
 	
rw   Nr   )	r4  r5  r6  r7  rE   r  rC   r  ri   r   rw   ru   r   r     s8        DD#L#L

 

 

 

 

 

rw   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.ra   rb   c                   | j         }| j        }|                    t          | j        d|                    }|                    |                    | j                  |                    || j	        |          |           d S r  r  )r$  rV   r_   r   s       ru   ri   zForDictionaryValues.begin_body  s}    ,y HT_a>>??))$*55NN5$"2D99	
 	
 	
 	
 	
rw   Nr   )	r4  r5  r6  r7  rF   r  rG   r  ri   r   rw   ru   r  r    s8        FF%L%L

 

 

 

 

 

rw   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.ra   rb   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)rV   r_   r   r(   r  r   r   r,   rF  r   rH  rW   r@   r   r   r   rG  r)   )r$  rV   r_   r  r   rw  rvalues          ru   ri   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0rw   Nr   )	r4  r5  r6  r7  rD   r  rB   r  ri   r   rw   ru   r  r    s8        EE$L$L1 1 1 1 1 1rw   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  r`   ra   rb   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 rl  )rV   r  r  r  r   
end_targetr6   r}   r<   r2   r0   r'   rG  r  r  rH  rW   r   r   r_   )r$  r  r  r  rV   
index_typer  s          ru   r   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\]]]]]rw   c                   | j         }| j        }| j        dk    rdnd}|                    |                    | j        |          |                    | j        |          ||          }|                    || j        | j	                   d S )Nr   r  >)
rV   r_   r  r  r   r  r  r   rn   r   )r$  rV   r_   cmpr  s        ru   rg   zForRange.gen_conditionB  s    ,yY]]cc&&LL..T_d0S0SUXZ^
 

 	
DOT^LLLLLrw   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+)rV   r_   r6   r  r}   r  int_opr<   r   r  r   r  r    ADDr  rG  r   r$  rV   r_   new_vals       ru   rj   zForRange.gen_stepL  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8rw   N)r  r*   r  r*   r  r`   ra   rb   r   )r4  r5  r6  r7  r   rg   rj   r   rw   ru   r   r   ,  s^        EE^ ^ ^ ^&M M M M9 9 9 9 9 9rw   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.ra   rb   c                    | j         }t          d          }|                    |          | _        |                    | j                  | _        d S r  )rV   r   r  r  rH  rW   r   )r$  rV   zeros      ru   r   zForInfiniteCounter.inith  sK    , qzz 77==9@9V9VW[Wa9b9brw   c                    | j         }| j        }|                    t          |                    | j        |          t          d          t          j        |          }|	                    | j        ||           d S r   )
rV   r_   r  r<   r   r  r   r    r  rG  r  s       ru   rj   zForInfiniteCounter.gen_stepp  sk    ,y .. ',,t~t"D"DgajjRWR[]a
 
 	t~w55555rw   c                    | j                             | j        | j                             | j                  | j                   d S r   )rV   rG  r   r   r  r_   r'  s    ru   ri   zForInfiniteCounter.begin_body{  s;    D-t|/@/@/P/PRVR[\\\\\rw   Nr   )r4  r5  r6  r7  r   rj   ri   r   rw   ru   r  r  e  s`        KKc c c c	6 	6 	6 	6] ] ] ] ] ]rw   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)".ra   r^   c                    dS r:  r   r'  s    ru   r#  zForEnumerate.need_cleanup  r  rw   index1r   index2rX   r   rb   c           	         t          | j        || j        | j        | j        d          | _        | j                                         t          | j        ||| j        | j        | j        d          | _        d S )NTr   )	r  rV   rn   r   r_   	index_genr   rd   main_gen)r$  r  r  rX   s       ru   r   zForEnumerate.init  sv    +L&$/4>49UY
 
 
 	/L&$[_
 
 
rw   c                8    | j                                          d S r   )r  rg   r'  s    ru   rg   zForEnumerate.gen_condition  s    ##%%%%%rw   c                j    | j                                          | j                                         d S r   )r  ri   r  r'  s    ru   ri   zForEnumerate.begin_body  s0    !!###  """""rw   c                j    | j                                          | j                                         d S r   )r  rj   r  r'  s    ru   rj   zForEnumerate.gen_step  s0    !!!     rw   c                j    | j                                          | j                                         d S r   )r  r)  r  r'  s    ru   r)  zForEnumerate.gen_cleanup  s0    ""$$$!!#####rw   Nr3  )r  r   r  r   rX   r   ra   rb   r   rP  r   rw   ru   r  r    s        SS   
	
 	
 	
 	
& & & &# # # #! ! ! !$ $ $ $ $ $rw   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, ...)`.ra   r^   c                    dS r:  r   r'  s    ru   r#  zForZip.need_cleanup  r  rw   indexeslist[Lvalue]exprsr   rb   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     ru   r   zForZip.init.<locals>.<listcomp>  s    JJJQJLLJJJrw   r   Tr  )r   rangern   cond_blocksgensr   rd   rV   r   r_   append)r$  r  r  rW   rX   
next_blockr   s          ru   r   zForZip.init  s    7||s5zz)))) KJ%Gq8H2I2IJJJdoM^^(*	'*7E4;K'L'L 	" 	"#E4)eT:t~tyY]  C IS!!!!		" 	"rw   c                    t          | j                  D ]Y\  }}|                                 |t          | j                  dz
  k     r%| j                            | j        |                    Zd S r   )r   r  rg   r   rV   rh   r  )r$  r   r   s      ru   rg   zForZip.gen_condition  sv    	** 	A 	AFAs3ty>>A%%%++D,<Q,?@@@	A 	Arw   c                B    | j         D ]}|                                 d S r   )r  ri   r$  r   s     ru   ri   zForZip.begin_body  s0    9 	 	CNN	 	rw   c                B    | j         D ]}|                                 d S r   )r  rj   r  s     ru   rj   zForZip.gen_step  s,    9 	 	CLLNNNN	 	rw   c                B    | j         D ]}|                                 d S r   )r  r)  r  s     ru   r)  zForZip.gen_cleanup  s0    9 	 	COO	 	rw   Nr3  )r  r  r  r   ra   rb   r   rP  r   rw   ru   r  r    s        II   

" 
" 
" 
"A A A A           rw   r  
int | Nonec                    t          |t          t          f          rt          |j                  S t          |t
          t          f          rH fd|j        D             }d |vr0t          d |j        D                       }|t          |          z   S nt          |t                    rt           |j                  S t          |t                    rjt          |j        t                    rP|j        j        rDt          |j        j        t"                    r%|j        j        rt          |j        j                  S                      |          }t          |t(                    rt          |j                  S d S )Nc                Z    g | ]'}t          |t                    t          |          (S r   )r   r   get_expr_length)r   r   rV   s     ru   r   z#get_expr_length.<locals>.<listcomp>  s3    \\\JqRZD[D[\!,,\\\rw   c              3  B   K   | ]}t          |t                     V  d S r   )r   r   )r   r   s     ru   r   z"get_expr_length.<locals>.<genexpr>  s/      HHJq(333HHHHHHrw   )r   r   r   r   r   r   r   r   sumr   r  rX   r   r   r   is_finalfinal_valuestrhas_explicit_valuer   r,   r   )rV   rX   starsotherr   s   `    ru   r  r    sp   $),-- *4:	D8Y/	0	0 * ]\\\dj\\\uHHTZHHHHHE3u::%%  
D(	#	# 	*w	2224!!*ty#&&* I* ty,c22	*
 I(* 49())) d##E%    5;4rw   r   c                "   |                      |          }t          |          st          |t                    s
J |            t	          | |          }|| j                            |||          S t          ||rt          nt                    S )Nr   )
r   r5   r   r,   r  rV   r1  r   r/   r<   )rV   rX   rx   r_   r   r   rz   s          ru   r   r     s     d##E!%((LJuf,E,ELLuLLLWd++F~**8T*WWW6]\11H\]]]rw   )rV   r=   rW   r   rX   r   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   )rV   r=   rW   r   rX   r   rx   r*   rY   ry   r_   r`   rz   r*   ra   rb   )
rV   r=   r   r   r   r   r   rR   ra   r   )rV   r=   r   r   ra   r*   )rV   r=   r   r   ra   r^   )
rV   r=   r   r   r   r   r_   r`   ra   rb   )rX   r   ra   r^   )FF)rV   r=   rW   r   rX   r   rn   r   r   r   r_   r`   r]   r^   r   r^   ra   r   )
rV   r=   rw  r*   rW   r*   r_   r`   ra   r*   )rV   r=   rX   r   ra   r  )rV   r=   rX   r   rx   r*   r_   r`   r   r^   ra   r*   )r7  
__future__r   typingr   r   r   
mypy.nodesr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   
mypy.typesr   r   r   r   mypyc.ir.opsr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   mypyc.ir.rtypesr+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   mypyc.irbuild.builderr=   mypyc.irbuild.preparer>   mypyc.irbuild.targetsr?   r@   mypyc.primitives.dict_opsrA   rB   rC   rD   rE   rF   rG   mypyc.primitives.exc_opsrH   rI   mypyc.primitives.generic_opsrJ   rK   rL   rM   mypyc.primitives.list_opsrN   rO   rP   mypyc.primitives.misc_opsrQ   mypyc.primitives.registryrR   mypyc.primitives.set_opsrS   mypyc.primitives.str_opsrT   mypyc.primitives.tuple_opsrU   rZ   rv   r   r   r   r   r   r   r   rd   r   r  r  r   r{  r   r  r   r  r  r   r  r  r  r  r   r   rw   ru   <module>r     se    # " " " " " + + + + + + + + + +                                     & Q P P P P P P P P P P P                                 "                                       ( , + + + + + 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
;' ;' ;' ;'|4' 4' 4' 4'nT T T Tn" " " "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X5 X5 X5 X5 X5, X5 X5 X5v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$ $ $ $ $\ $ $ $N   @
^ 
^ 
^ 
^ 
^ 
^rw   