NL i-TdZddlmZddlmZmZmZmZddlm Z m Z m Z m Z m Z ddlmZddlmZmZmZmZddlmZmZmZddlmZmZdd lmZmZmZdd l m!Z!dd Z"ddd Z#dd Z$dddZ% ddZ&ddZ'ddZ( d ddZ)dddZ* d d dZ+d!dZ,y)"aGenerate 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)ArgumentFuncDef SymbolNodeVar) BITMAP_BITS ENV_ATTR_NAMEGENERATOR_ATTRIBUTE_PREFIX SELF_NAME bitmap_name)ClassIR)CallGetAttrSetAttrValue) RInstancebitmap_rprimitiveobject_rprimitive) IRBuilder SymbolTarget)FuncInfoGeneratorClass ImplicitClass)AssignmentTargetAttrct|jjd|jdd}d|_t ||j t<|jjr3t |jdj|j t<|g|_ ||j_ |jj||S)aGenerate 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_generatedis_final_class)r fn_infonamespaced_name module_namereuse_freed_instancer attributesr is_nestedfn_infos env_classr mroclassesappend)builderr's ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/mypyc/irbuild/env_class.pysetup_env_classr-$s ?? * * , -T2 I &*I"&/ &:I#  /88H8H8L8V8V.W ]+KIM )GOO OO9% c|jjs t||jjr$t |d|jj |yt |d|j|y)zJGenerate, instantiate, and set up the environment of an environment class.F)localbaseprefixN)r #can_merge_generator_and_env_classesinstantiate_env_classr%add_args_to_envcallable_class)r+r2s r,finalize_env_classr7EsT ?? > > @g&   u7??3Q3QZ`au7??6Rr.c|jt|jjjg|jj j }|jjrz||jj_ |jt|t|jjj|jj j |S||j_ |S)zTAssign an environment class to a register named after the given function definition.) addrr r'ctorfitemliner%r6 _curr_env_regrr prev_env_reg)r+ curr_env_regs r,r4r4Ss;; W__ & & + +R1F1F1K1KLL  7C&&4 ..;;%%**    )5% r.ct|d||j}|j}|jrNt ||j t |tr'|jrt|||j |yyyy)aGLoad 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)r0r2)r2N) r5r r;r%load_outer_envsr6 isinstanceradd_nested_funcs_to_envsetup_func_for_recursive_call)r+r2r r;s r,load_env_registersrEisrG47ooG MME!7!78 eW %'*I*I )'5':P:PY_ `+J % r.c|jt|t|jjj }t |jts J|d|jD]x\}}|j}t |tr |j}|j|jjj|<t||}|j||z|S)aLoad 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)r9rr r r;r<rBtyperitemsnamerattrclass_irr$ add_target)r+r1 outer_envenvsymboltarget attr_name symbol_targets r,load_outer_envrS}s ++gdM7??3H3H3M3MN OC chh *Nse3M,NN *#//+2KK f2 3 I28++$$Y/,S)< 6=1 2 Jr.cjt|jdz }|dkDri|j|}t|trt ||j ||_nt ||j||_|j}|dz}|dkDr(|j|}t ||}|dz}|dkDr'yy)N) lenbuilders symtablesrBrrSr?r>self_reg)r+r1indexrMenv_regs r,rArAs   !A %E qy%%e, dN + .w8I8I9 UD  .w y QD ##   !)%%e,  '9=   !)r.cd}|D]T}|j|jj}|js5|jj sP|dz }V|t dz zt zS)NrrV) type_to_rtypevariablerG error_overlapkind is_optionalr)r+argsnargts r,num_bitmap_argsrgse A  ! !#,,"3"3 4 ??sxx335 FA q !k 11r.Ncp|j}|jj}t||}|r|D]E}|j |j j } |j|j | dGtt|D],} |jtt| td.y|D]s}t||j s |js&|j |j j } |Jd|j|j | |||uy)NT)is_argz,base cannot be None for adding nonlocal argsreassignr2)r r; argumentsrgr^r_rG add_local_regreversedrangerr ris_free_variable is_generatoradd_var_to_env_class) r+r0r1rkr2r rcnbrertypeis r,r5r5s ooG == " "D $ 'B  DC))#,,*;*;z!add_vars_to_env..s WXW]W]r.)keyFrjN)r rqgenerator_classr% in_non_extr6r;free_variablessortedrBrr^rGrrencapsulating_funcsrr r)r+r2 env_for_funcvarrt nested_fns r,add_vars_to_envrsN.5__L##66  " "goo&@&@55  6 66'001F1FGM]^ C#s#--chh7,, uV-  ; ;; 44W__5J5JK I)W- ))7F,,0,W]- rmrassign) r+fdefr1r2prev_envrQr>valrPs r,rDrDs#--H"I%,%:%:499%EH "$'{{74+<+rs "#99&66KK9II6B S,a( #0N 8.2,0    *     4$PKM$ $%$-:$DG$ $<Wr.