Ë
    pPêjÓ=  ã                  ót  — d Z ddl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 ddlmZ ddlmZmZmZmZ ddlmZmZmZ ddlmZmZ dd	lmZmZmZ dd
l m!Z! ddl"m#Z# dd„Z$dddœdd„Z%d d„Z&dd!d„Z'ddœ	 	 	 	 	 	 	 	 	 d"d„Z(d#d„Z)d$d„Z*	 	 	 	 d%	 	 	 	 	 	 	 	 	 	 	 d&d„Z+	 	 	 	 	 	 	 	 	 	 d'd„Z,dd!d„Z-d(d„Z.d)d„Z/y)*a  Generate classes representing function environments (+ related operations).

If we have a nested function that has non-local (free) variables, access to the
non-locals is via an instance of an environment class. Example:

    def f() -> int:
        x = 0  # Make 'x' an attribute of an environment class instance

        def g() -> int:
            # We have access to the environment class instance to
            # allow accessing 'x'
            return x + 2

        x = x + 1  # Modify the attribute
        return g()
é    )Úannotations)ÚArgumentÚFuncDefÚ
SymbolNodeÚVar)ÚBITMAP_BITSÚENV_ATTR_NAMEÚGENERATOR_ATTRIBUTE_PREFIXÚ	SELF_NAMEÚbitmap_name)ÚClassIR)ÚCallÚGetAttrÚSetAttrÚValue)Ú	RInstanceÚbitmap_rprimitiveÚobject_rprimitive)Ú	IRBuilderÚSymbolTarget)ÚFuncInfoÚGeneratorClassÚImplicitClass)ÚAssignmentTargetAttr)Úget_func_target_symbolc                óJ  — t        | j                  j                  «       › d�| j                  dd¬«      }d|_        t        |«      |j                  t        <   | j                  j                  r�| j                  d   j                  �ht        | j                  d   j                  «      |j                  t        <   | j                  j                  r|j                  j                  t        «       |g|_        || j                  _        | j"                  j%                  |«       |S )a‘  Generate a class representing a function environment.

    Note that the variables in the function environment are not
    actually populated here. This is because when the environment
    class is generated, the function environment has not yet been
    visited. This behavior is allowed so that when the compiler visits
    nested functions, it can use the returned ClassIR instance to
    figure out free variables it needs to access.  The remaining
    attributes of the environment class are populated when the
    environment registers are loaded.

    Return a ClassIR representing an environment for a function
    containing a nested function.
    Ú_envT)Úis_generatedÚis_final_classéþÿÿÿ)r   Úfn_infoÚnamespaced_nameÚmodule_nameÚreuse_freed_instancer   Ú
attributesr   Ú	is_nestedÚfn_infosÚ
_env_classÚ	env_classr	   Úcontains_nestedÚ!attrs_to_keep_alive_on_completionÚaddÚmroÚclassesÚappend)Úbuilderr)   s     ú\/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/mypyc/irbuild/env_class.pyÚsetup_env_classr2   %   sí   € ô Ø�?‰?×*Ñ*Ó,Ð
-¨TÐ2Ø×ÑØØô	€Ið &*€IÔ"Ü&/°	Ó&:€I×ÑœÑ#Ø‡�× Ò  W×%5Ñ%5°bÑ%9×%DÑ%DÐ%Pô /8¸×8HÑ8HÈÑ8L×8VÑ8VÓ.Wˆ	×Ñœ]Ñ+Ø�?‰?×*Ò*Ø×7Ñ7×;Ñ;¼MÔJØ�K€I„MØ )€G‡O�OÔØ‡O�O×Ñ˜9Ô%ØÐó    T)Úadd_argsc               óV  — | j                   j                  «       st        | «      }n| j                   j                  }|rj| j                   j                  sT| j                   j
                  r%t        | d| j                   j                  |¬«       |S t        | d| j                   |¬«       |S )zJGenerate, instantiate, and set up the environment of an environment class.F)ÚlocalÚbaseÚprefix)r!   Ú#can_merge_generator_and_env_classesÚinstantiate_env_classÚcurr_env_regÚis_comprehension_scoper&   Úadd_args_to_envÚcallable_class)r0   r8   r4   Úenv_regs       r1   Úfinalize_env_classr@   H   s…   € à�?‰?×>Ñ>Ô@Ü'¨Ó0‰à—/‘/×.Ñ.ˆñ
 ˜Ÿ™×>Ò>Ø�?‰?×$Ò$ÜØ˜u¨7¯?©?×+IÑ+IÐRXõð
 €Nô ˜G¨5°w·±ÈvÕVØ€Nr3   c                ót  — | j                  t        | j                  j                  j                  g | j                  j
                  j                  «      «      }| j                  j                  r�| j                  j                  sz|| j                  j                  _
        | j                  t        |t        | j                  j                  j                  | j                  j
                  j                  «      «       |S || j                  _
        | j                  j                  r| j                  d   nd}|�<|j                  �0|j                   r|j"                  j$                  }n|j                  }nd}|�?| j                  t        |t        || j                  j
                  j                  «      «       |S )zTAssign an environment class to a register named after the given function definition.r    N)r,   r   r!   r)   ÚctorÚfitemÚliner&   r<   r>   Ú_curr_env_regr   r	   Úprev_env_regr'   r(   Úis_generatorÚgenerator_classr;   )r0   r;   ÚparentÚparent_env_regs       r1   r:   r:   \   sP  € à—;‘;ÜˆW�_‰_×&Ñ&×+Ñ+¨R°·±×1FÑ1F×1KÑ1KÓLó€Lð ‡�× Ò ¨¯©×)OÒ)OØ7Cˆ�‰×&Ñ&Ô4Ø�‰ÜØÜØ—‘×.Ñ.×;Ñ;Ø—‘×%Ñ%×*Ñ*ó	ô	
ð2 Ðð )5ˆ�‰Ô%à)0¯©×)BÒ)B�×!Ñ! "Ò%ÈˆØÐ &×"3Ñ"3Ð"?Ø×"Ò"Ø!'×!7Ñ!7×!DÑ!D‘à!'×!5Ñ!5‘à!ˆNØÐ%Ø�K‰KÜ˜¤m°^ÀWÇ_Á_×EZÑEZ×E_ÑE_Ó`ôð Ðr3   c                ó4  — t        | d|¬«       | j                  }|j                  }|j                  rf| j                  d   j
                  �Lt        | |j                  «       t        |t        «      r%|j                  rt        | ||j                  «       yyyyy)aG  Load the registers for the current FuncItem being visited.

    Adds the arguments of the FuncItem to the environment. If the
    FuncItem is nested inside of another function, then this also
    loads all of the outer environments of the FuncItem into registers
    so that they can be used when accessing free variables.
    T)r6   r8   r    N)r=   r!   rC   r&   r'   r(   Úload_outer_envsr>   Ú
isinstancer   Úadd_nested_funcs_to_envÚsetup_func_for_recursive_call)r0   r8   r!   rC   s       r1   Úload_env_registersrP   €   s‰   € ô �G 4°Õ7à�o‰o€GØ�M‰M€EØ×Ò˜W×-Ñ-¨bÑ1×<Ñ<ÐHÜ˜ ×!7Ñ!7Ô8ô �eœWÔ%¨'×*IÒ*IÜ)¨'°5¸'×:PÑ:PÕQð +JÐ%ð	 IÐr3   F©Úborrowc               ó  — |rGt        |j                  t        «      sJ ‚|j                  j                  j	                  t
        «      sJ ‚| j                  t        |t
        | j                  j                  j                  |¬«      «      }t        |j                  t        «      s
J |› d�«       ‚|j                  «       D ]Ç  \  }}t        |t        «      rZt        |j                  j                  t        «      r6|j                  j                  j                  |j                  j                  urŒq|j                  }|j                  |j                  j                  j                  |<   t        ||«      }| j!                  ||«       ŒÉ |S )aµ  Load the environment referenced by "base" and bind its symbols.

    "outer_env" may contain registers and attributes from multiple environment levels.
    Bind only attributes stored directly on the loaded environment, since the other targets
    cannot be accessed through it.

    If "borrow" is true, return a borrowed reference. In that case the environment link on
    "base" must be final so that the reference remains valid.
    rQ   z must be of type RInstance)rM   Útyper   Úclass_irÚis_final_attrr	   r,   r   r!   rC   rD   Úitemsr   ÚobjÚattrr%   Ú
add_target)	r0   r7   Ú	outer_envrR   ÚenvÚsymbolÚtargetÚ	attr_nameÚsymbol_targets	            r1   Úload_outer_envra   ”   s*  € ñ  Ü˜$Ÿ)™)¤YÔ/Ð/Ð/Ø�y‰y×!Ñ!×/Ñ/´Ô>Ð>Ð>Ø
�+‰+”g˜d¤M°7·?±?×3HÑ3H×3MÑ3MÐV\Ô]Ó
^€CÜ�c—h‘h¤	Ô*ÐN¨s¨eÐ3MÐ,NÓNÐ*à#Ÿ/™/Ó+ò 2‰ˆ�ô ˜6Ô#7Ô8Ü˜fŸj™jŸo™o¬yÔ9Ø�z‰z�‰×'Ñ'¨s¯x©x×/@Ñ/@Ñ@àØ—K‘Kˆ	Ø28·+±+ˆ�‰×Ñ×$Ñ$ YÑ/Ü,¨S°)Ó<ˆØ×Ñ˜6 =Õ1ð2ð €Jr3   c                ó  — t        | j                  «      dz
  }|dkD  xs  |dk(  xr | j                  d   j                  }|r„| j                  |   j                  �k| j
                  |   }t        |t        «      rt        | |j                  |«      |_
        nt        | |j                  |d¬«      |_
        |j                  }|dz  }|dkD  rB| j                  |   j                  €y | j
                  |   }t        | |«      }|dz  }|dkD  rŒAy y )Né   é   TrQ   )ÚlenÚbuildersr'   r*   r(   Ú	symtablesrM   r   ra   r;   rF   Úself_reg)r0   r7   ÚindexÚ	has_outerr[   r?   s         r1   rL   rL   º   s  € Ü�× Ñ Ó! AÑ%€Eð ˜‘	ÒQ˜e q™jÒP¨W×-=Ñ-=¸aÑ-@×-PÑ-P€IÙ�W×%Ñ% eÑ,×7Ñ7ÐCà×%Ñ% eÑ,ˆ	Ü�dœNÔ+Ü .¨w¸×8IÑ8IÈ9Ó UˆDÕô !/¨w¸¿¹ÀyÐY]Ô ^ˆDÔØ×#Ñ#ˆØ�‰
ˆð �!Š)Ø×Ñ˜EÑ"×-Ñ-Ð5Øà×%Ñ% eÑ,ˆ	Ü  ¨'°9Ó=ˆØ�‰
ˆð �!�)r3   c                óÞ   — d}|D ]T  }| j                  |j                  j                  «      }|j                  sŒ5|j                  j                  «       sŒP|dz  }ŒV |t        dz
  z   t        z  S )Nr   rd   )Útype_to_rtypeÚvariablerT   Úerror_overlapÚkindÚis_optionalr   )r0   ÚargsÚnÚargÚts        r1   Únum_bitmap_argsru   Ö   se   € Ø	€AØò ˆØ×!Ñ! #§,¡,×"3Ñ"3Ó4ˆØ�?‹?˜sŸx™x×3Ñ3Õ5Ø�‰F‰Aðð ”˜q‘Ñ!¤kÑ1Ð1r3   Nc           
     óŒ  — | j                   }|j                  j                  }|ršt        | |«      }|D ]E  }| j	                  |j
                  j                  «      }	| j                  |j
                  |	d¬«       ŒG t        t        |«      «      D ],  }
| j                  t        t        |
«      «      t        d¬«       Œ. y |D ]�  }t        | |j
                  «      sŒ| j	                  |j
                  j                  «      }	|€J d«       ‚| j                  |j
                  |	||| j                  |j
                  «      |¬«       Œƒ y )NT)Úis_argz,base cannot be None for adding nonlocal args©ÚreassignÚkeep_alive_on_completionr8   )r!   rC   Ú	argumentsru   rl   rm   rT   Úadd_local_regÚreversedÚranger   r   r   Úis_free_variableÚadd_var_to_env_classÚis_captured_by_nested_func)r0   r6   r7   ry   r8   r!   rq   Únbrs   ÚrtypeÚis              r1   r=   r=   ß   s1  € ð �o‰o€GØ�=‰=×"Ñ"€DÙÜ˜W dÓ+ˆØò 	DˆCØ×)Ñ)¨#¯,©,×*;Ñ*;Ó<ˆEØ×!Ñ! #§,¡,°¸dÐ!ÕCð	Dô œ% ›)Ó$ò 	WˆAØ×!Ñ!¤#¤k°!£nÓ"5Ô7HÐQUÐ!ÕVñ	Wð ò 	ˆCÜ ¨¯©Õ6Ø×-Ñ-¨c¯l©l×.?Ñ.?Ó@�ØÐ'ÐWÐ)WÓWÐ'Ø×,Ñ,Ø—L‘LØØØ%Ø-4×-OÑ-OÐPS×P\ÑP\Ó-]Ø!ð -õ ñ		r3   c           
     ó‚  — | j                   j                  j                  D ]œ  }| j                  |j                  j
                  «      }| j                  |j                  «      r;| j                  |j                  || j                   j                  ||dt        ¬«       Œ~| j                  |j                  |||¬«       Œž y)zKPut generator arguments on either the closure environment or private frame.Trx   )ry   N)r!   rC   r{   rl   rm   rT   r�   Úadd_var_to_classr)   r
   Úadd_var_to_generator_frame)r0   Ú	frame_regr?   ry   rs   rƒ   s         r1   Úadd_generator_argsr‰   þ   s¥   € ð �‰×$Ñ$×.Ñ.ò bˆØ×%Ñ% c§l¡l×&7Ñ&7Ó8ˆØ×-Ñ-¨c¯l©lÔ;Ø×$Ñ$Ø—‘ØØ—‘×)Ñ)ØØ!Ø)-Ü1ð %õ ð ×.Ñ.¨s¯|©|¸UÀIÐX`Ð.Õañbr3   c           	     óÄ  — | j                   }|}|j                  r|j                  }n0|j                  s|j                  r|j
                  s|j                  }|j                  | j                  v rkt        | j                  |j                     d„ ¬«      D ]C  }t        |t        «      sŒ| j                  || j                  |j                  «      |dd|¬«       ŒE |j                  | j                  v rît!        «       }| j                  |j                     D ]Ç  }t        |t"        «      sŒt%        |«      }||v rŒ$|j'                  |«       |}|j                  s|j(                  rt*        }t-        | |«      }	|j                  r@| j/                  |«      s/| j1                  |t2        |j                  j4                  d|	¬«       Œ­| j                  |t2        |d|	|¬«       ŒÉ yy)aV  Add relevant local variables and nested functions to persistent storage.

    Captured bindings go in the environment class. Generator-owned bindings that aren't
    captured go on the private generator frame. This is done before visiting the function body
    so nested functions can reference declarations that occur later in the body.
    c                ó   — | j                   S ©N)Úname)Úxs    r1   ú<lambda>z!add_vars_to_env.<locals>.<lambda>"  s
   € ÈqÏvÉv€ r3   )ÚkeyFTrx   )ry   rz   N)r!   rG   rH   r&   Ú
in_non_extr<   r>   rC   Úfree_variablesÚsortedrM   r   r€   rl   rT   Úencapsulating_funcsÚsetr   r   r,   Úis_coroutiner
   r   r�   r‡   r   rh   )
r0   r8   r!   Úenv_for_funcÚvarÚaddedÚ	nested_fnÚtarget_symbolÚnested_prefixÚ
keep_alives
             r1   Úadd_vars_to_envrž     s¾  € ð �o‰o€GØ-4€LØ×ÒØ×.Ñ.‰Ø
×
Ò
˜w×1Ò1¸7×;YÒ;YØ×-Ñ-ˆà‡}�}˜×.Ñ.Ñ.ä˜'×0Ñ0°·±Ñ?ÑEUÔVò 		ˆCÜ˜#œsÕ#Ø×,Ñ,ØØ×)Ñ)¨#¯(©(Ó3Ø Ø"Ø-1Ø!ð -õ ð		ð ‡}�}˜×3Ñ3Ñ3Ü!$£ˆØ ×4Ñ4°W·]±]ÑCò 	ˆIÜ˜)¤WÕ-Ü 6°yÓ A�Ø  EÑ)ØØ—	‘	˜-Ô(ð !'�Ø×)Ò)¨Y×-CÒ-CÜ$>�MÜ-¨g°}ÓE�
Ø×'Ò'°×0RÑ0RÐS`Ô0aØ×6Ñ6Ø%Ü)Ø×/Ñ/×8Ñ8Ø!&Ø1;ð 7õ ð ×0Ñ0Ø%Ü)Ø$Ø!&Ø1;Ø,ð 1õ ñ1	ð 4r3   c                óž  — | j                   d   j                  }t        |«      }| j                  d   j	                  |«      }t        |t        «      rVt        |t        «      rFt        |j                  j                  t        «      r"|j                  j                  j                  |ury|j                  }|j                  }t        |t        «      r+| j                  t!        |j"                  t$        |«      «      }n|j&                  }| j                  t!        |||«      «      }	| j)                  |t*        «      }| j-                  ||	|«       y)a.  Enable calling a nested function (with a callable class) recursively.

    Adds the instance of the callable class representing the given
    FuncDef to a register in the environment so that the function can
    be called recursively. Note that this needs to be done only for
    nested functions.
    r    N)r'   r)   r   rg   ÚgetrM   r   r   rX   rT   r   rU   rY   rD   r   r,   r   r;   r	   rF   r|   r   Úassign)
r0   Úfdefr7   Úprev_envr›   r^   r_   rD   rF   Úvals
             r1   rO   rO   Q  s  € ð ×Ñ Ñ#×-Ñ-€HÜ*¨4Ó0€MØ×Ñ˜rÑ"×&Ñ& }Ó5€Fä�}¤gÔ.Ü˜&Ô"6Ô7Ü˜&Ÿ*™*Ÿ/™/¬9Ô5Ø�:‰:�?‰?×#Ñ#¨8Ñ3àà—‘€IØ�9‰9€Dä�$œÔ'ð —{‘{¤7¨4×+<Ñ+<¼mÈTÓ#RÓS‰à×(Ñ(ˆð �+‰+”g˜l¨I°tÓ<Ó
=€CØ×"Ñ" =Ô2CÓD€FØ‡N�N�6˜3 Õ%r3   c                óp   — | j                   j                  }|| j                  v xr || j                  |   v S rŒ   )r!   rC   r’   )r0   r]   rC   s      r1   r   r   v  s8   € Ø�O‰O×!Ñ!€EØ�G×*Ñ*Ð*ÒV¨v¸×9OÑ9OÐPUÑ9VÐ/VÐVr3   )r0   r   Úreturnr   )Ú )r0   r   r8   Ústrr4   Úboolr¦   r   )r0   r   r¦   r   )r0   r   r8   r¨   r¦   ÚNone)
r0   r   r7   r   r[   zdict[SymbolNode, SymbolTarget]rR   r©   r¦   r   )r0   r   r7   r   r¦   rª   )r0   r   rq   zlist[Argument]r¦   Úint)TNTr§   )r0   r   r6   r©   r7   zFuncInfo | ImplicitClass | Nonery   r©   r8   r¨   r¦   rª   )
r0   r   rˆ   r   r?   r   ry   r©   r¦   rª   )r0   r   r¢   r   r7   r   r¦   rª   )r0   r   r]   r   r¦   r©   )0Ú__doc__Ú
__future__r   Ú
mypy.nodesr   r   r   r   Úmypyc.commonr   r	   r
   r   r   Úmypyc.ir.class_irr   Úmypyc.ir.opsr   r   r   r   Úmypyc.ir.rtypesr   r   r   Úmypyc.irbuild.builderr   r   Úmypyc.irbuild.contextr   r   r   Úmypyc.irbuild.targetsr   Úmypyc.irbuild.utilr   r2   r@   r:   rP   ra   rL   ru   r=   r‰   rž   rO   r   © r3   r1   ú<module>r¸      s1  ðñõ" #ç 9Ó 9÷õ õ &ß 6Ó 6ß KÑ Kß 9ß IÑ IÝ 6Ý 5ó ðFÐQUõ ó(!ôHRð2 ñ#Øð#à
ð#ð .ð#ð
 ð#ð ó#óLó82ð Ø,0ØØðØðàðð *ðð ð	ð
 ðð 
óð>bØðbØ#(ðbØ38ðbØDHðbà	óbô(<ó~"&ôJWr3   