
    X-Ph+                       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 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Z!d.dZ"d/dZ#d.dZ$d0dZ%d1dZ&d2dZ'	 	 	 d3d4d%Z(d.d&Z)d5d)Z*d6d,Z+d S )7a  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	SELF_NAMEbitmap_name)ClassIR)CallGetAttrSetAttrValue)	RInstancebitmap_rprimitiveobject_rprimitive)	IRBuilderSymbolTarget)FuncInfoGeneratorClassImplicitClass)AssignmentTargetAttrbuilderr   returnr   c                l   t          | j                                         d| j        d          }t	          |          |j        t          <   | j        j        r,t	          | j        d         j	                  |j        t          <   |g|_        || 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)r   fn_infonamespaced_namemodule_namer   
attributesr
   	is_nestedfn_infos	env_classr	   mroclassesappend)r   r&   s     W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypyc/irbuild/env_class.pysetup_env_classr+      s     ?**,,222G4GVZ  I '0	&:&:I#  X /88H8L8V.W.W	]+KIM )GOO9%%%    Nonec                    t          |            | j        j        rt          | d| j        j                   dS t          | d| j                   dS )zJGenerate, instantiate, and set up the environment of an environment class.F)localbaseN)instantiate_env_classr    r$   add_args_to_envcallable_class)r   s    r*   finalize_env_classr4   ;   s]    '"""
   Du7?3QRRRRRRu7?CCCCCCr,   r   c                n   |                      t          | j        j        j        g | j        j        j                            }| j        j        rZ|| j        j        _	        |                      t          |t          | j        j        j        | j        j        j                             n|| j        _	        |S )zTAssign an environment class to a register named after the given function definition.)addr   r    r&   ctorfitemliner$   r3   _curr_env_regr   r	   prev_env_reg)r   curr_env_regs     r*   r1   r1   H   s    ;;W_&+R1F1KLL L   57C&4.;%*	 	
 	
 	
 	
 )5%r,   c                    t          | d           | j        }|j        }|j        rIt	          | |j                   t          |t                    r!|j        rt          | ||j                   dS dS dS dS )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)r/   N)
r2   r    r8   r$   load_outer_envsr3   
isinstancer   add_nested_funcs_to_envsetup_func_for_recursive_call)r   r    r8   s      r*   load_env_registersrB   ^   s     G4((((oGME R!7888 eW%% 	R'*I 	R)'5':PQQQQQR R	R 	R 	R 	Rr,   r0   	outer_envdict[SymbolNode, SymbolTarget]c                   |                      t          |t          | j        j        j                            }t          |j        t                    sJ | d            |	                                D ]N\  }}|j        |j        j
        j        |j        <   t          ||j                  }|                     ||           O|S )a  Load the environment class for a given base into a register.

    Additionally, iterates through all of the SymbolNode and
    AssignmentTarget instances of the environment at the given index's
    symtable, and adds those instances to the environment of the
    current environment. This is done so that the current environment
    can access outer environment variables without having to reload
    all of the environment registers.

    Returns the register where the environment class was loaded.
    z must be of type RInstance)r6   r   r	   r    r8   r9   r?   typer   itemsclass_irr#   namer   
add_target)r   r0   rC   envsymboltargetsymbol_targets          r*   load_outer_envrO   r   s     ++gdM7?3H3MNN
O
OCch	**NNs,N,N,NNNN#//++ 2 24:K$V[1,S&+>>6=1111Jr,   r   c                l   t          | j                  dz
  }|dk    re| j        |         }t          |t                    rt          | |j        |          |_        nt          | |j        |          |_        |j        }|dz  }|dk    r+| j        |         }t          | ||          }|dz  }|dk    )d S d S )N      )	lenbuilders	symtablesr?   r   rO   r<   r;   self_reg)r   r0   indexrC   env_regs        r*   r>   r>      s     !!A%E qyy%e,	dN++ 	R .w8I9 U UD .wy Q QD#
 !))%e,	 '9==
	 !))))))r,   argslist[Argument]intc                    d}|D ]F}|                      |j        j                  }|j        r|j                                        r|dz  }G|t          dz
  z   t          z  S )Nr   rR   )type_to_rtypevariablerF   error_overlapkindis_optionalr   )r   rY   nargts        r*   num_bitmap_argsre      sk    	A  !!#,"344? 	sx3355 	FAq!k11r,   TNr/   boolFuncInfo | ImplicitClass | Nonereassignc                V   | j         }|j        j        }t          | |          }|r|D ]>}|                     |j        j                  }|                     |j        |d           ?t          t          |                    D ]9}	|                     t          t          |	                    t          d           :d S |D ]g}t          | |j                  s|j        rI|                     |j        j                  }|
J d            |                     |j        |||           hd S )NT)is_argz,base cannot be None for adding nonlocal argsrh   )r    r8   	argumentsre   r]   r^   rF   add_local_regreversedranger   r   r   is_free_variableis_generatoradd_var_to_env_class)
r   r/   r0   rh   r    rY   nbrc   rtypeis
             r*   r2   r2      s\    oG="D	$	'	'B [ 	D 	DC))#,*;<<E!!#,d!CCCC%))$$ 	W 	WA!!#k!nn"5"57HQU!VVVV	W 	W  	[ 	[C66 [':N [--cl.?@@'')W''',,S\5$QY,ZZZ		[ 	[r,   c                \   | j         }| j         j        r| j         j        }n$| j         j        s| j         j        r| j         j        }| j         j        | j        v rrt          | j        | j         j                 d           D ]J}t          |t                    r3|                     |j                  }|                     |||d           K| j         j        | j        v rM| j        | j         j                 D ]7}t          |t                    r|                     |t           |d           6dS dS )a  Add relevant local variables and nested functions to the environment class.

    Add all variables and functions that are declared/defined within current
    function and are referenced in functions nested within this one to this
    function's environment class so the nested functions can reference
    them even if they are declared after the nested function's definition.
    Note that this is done before visiting the body of the function.
    c                    | j         S N)rI   )xs    r*   <lambda>z!add_vars_to_env.<locals>.<lambda>   s
    WXW] r,   )keyFrk   N)r    rq   generator_classr$   
in_non_extr3   r8   free_variablessortedr?   r   r]   rF   rr   encapsulating_funcsr   r   )r   env_for_funcvarrt   	nested_fns        r*   add_vars_to_envr      sM    .5_L# 66		" 6go&@ 65 666'01FGM]M]^^^ 	W 	WC#s## W--ch77,,S%PU,VVV ;;; 4W_5JK 		 		I)W--  ,,0, -    <;		 		r,   fdefr   c                   | j         d         j        }|                     |j                  |j        |j        <   t          |t                    r/|                     t          |j
        t          d                    }n|j        }|                     t          ||j        d                    }|                     |t                    }|                     ||d           dS )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]   rF   r#   rI   r?   r   r6   r   r<   r	   r;   rm   r   assign)r   r   r0   prev_envr;   valrM   s          r*   rA   rA      s     #-H%,%:%:49%E%EH	"$'' ) {{74+<mR#P#PQQ( ++glDIr::
;
;C""4):;;FNN63#####r,   rL   r   c                J    | j         j        }|| j        v o|| j        |         v S rx   )r    r8   r~   )r   rL   r8   s      r*   rp   rp      s-    O!EG**Vv9OPU9V/VVr,   )r   r   r   r   )r   r   r   r-   )r   r   r   r   )r   r   r0   r   rC   rD   r   r   )r   r   r0   r   r   r-   )r   r   rY   rZ   r   r[   )TNT)
r   r   r/   rf   r0   rg   rh   rf   r   r-   )r   r   r   r   r0   r   r   r-   )r   r   rL   r   r   rf   ),__doc__
__future__r   
mypy.nodesr   r   r   r   mypyc.commonr   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   r+   r4   r1   rB   rO   r>   re   r2   r   rA   rp    r,   r*   <module>r      s   " # " " " " " 9 9 9 9 9 9 9 9 9 9 9 9 K K K K K K K K K K K K % % % % % % 6 6 6 6 6 6 6 6 6 6 6 6 K K K K K K K K K K 9 9 9 9 9 9 9 9 I I I I I I I I I I 6 6 6 6 6 6   :
D 
D 
D 
D   ,R R R R(   2   .2 2 2 2 ,0	[ [ [ [ [.       F$ $ $ $6W W W W W Wr,   