
    X-Phd                    r   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	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 dd	lmZ dd
lm Z m!Z!m"Z" ddl#m$Z$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z< ddl=m>Z>m?Z?m@Z@mAZAmBZB ddlCmDZD ddlEmFZF dbd$ZGdcd*ZHddd-ZIded/ZJdfd7ZKdgd=ZLdhd@ZMdidBZNdjdDZOdkdEZPdldHZQdmdLZRdndMZSdndNZTdodOZUdjdPZVeWeef         ZX G dQ dRe          ZYdpdSZZ G dT dUe          Z[ G dV dWe          Z\dqd\Z]drd`Z^daS )sa  Prepare for IR transform.

This needs to run after type checking and before generating IR.

For example, construct partially initialized FuncIR and ClassIR
objects for all functions and classes. This allows us to bind
references to functions and classes before we've generated full IR for
functions or classes.  The actual IR transform will then populate all
the missing bits, such as function bodies (basic blocks).

Also build a mapping from mypy TypeInfos to ClassIR objects.
    )annotations)defaultdict)Iterable)
NamedTuple)Graph)ARG_STAR	ARG_STAR2CallExprClassDef	Decorator
ExpressionFuncDef
MemberExprMypyFileNameExprOverloadedFuncDefRefExpr
SymbolNodeTypeInfoVar)refers_to_fullname)TraverserVisitor)InstanceTypeget_proper_type)PROPSET_PREFIXget_id_from_name)catch_errors)Errors)ClassIR)FUNC_CLASSMETHODFUNC_NORMALFUNC_STATICMETHODFuncDeclFuncSignature
RuntimeArg)	DeserMaps)	RInstanceRTypedict_rprimitivenone_rprimitivetuple_rprimitive)Mapper)get_func_defget_mypyc_attrsis_dataclassis_extension_classis_trait)CompilerOptions)is_same_typemapperr-   moduleslist[MypyFile]graphr   typesdict[Expression, Type]optionsr3   errorsr   returnNonec                t   g }|D ]4d j         D             }|                    fd|D                        5|D ]\  }t          |j        j        t          |          |j        j        |j        j                  }	t          j
        ||          |	_        |	j        r#|j        j                                        |	_        |j        sd |	_        |	| j        |j        <   | j                            |	j                   |D ]\  }t)          j
        |j                  5  | j        |j                 j        rt-          j
        j        ||| |           nt/          j
        j        ||| |           d d d            n# 1 swxY w Y   |D ]:\  }| j        |j                 }	|	j        rt1          |j        |	j        |            ;|D ],t3                    D ]}
t5          j        d |
| |           -|D ]\  }| j        |j                 }	|	j        D ]}|	j        dd          D ]}||j        v r|t;          |	j        |         |j        |                   sV|j        j        |         j        }|J |j        rdnd}|                     d| d| d	|j         d
j
        |j                   d S )Nc                <    g | ]}t          |t                    |S  )
isinstancer   ).0nodes     U/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypyc/irbuild/prepare.py
<listcomp>z"build_type_map.<locals>.<listcomp>N   s'    UUU4*T8:T:TU$UUU    c                    g | ]}|fS rA   rA   )rC   cdefmodules     rE   rF   z"build_type_map.<locals>.<listcomp>O   s    BBB4BBBrG   )is_abstractis_final_class   traitclassz	Type of "z%" is incompatible with definition in z "")!defsextendr    namefullnamer2   inforK   is_finalr1   pathis_ext_classdeletable_attributescopy	deletableglobal_optschildren
type_to_irsymbol_fullnamesaddr   lineprepare_class_defprepare_non_ext_class_def#prepare_implicit_property_accessorsget_module_func_defsprepare_func_def
attributesmror4   namesrD   error)r5   r6   r8   r9   r;   r<   classesmodule_classesrI   class_irfuncattrbase_irrD   kindrJ   s                  @rE   build_type_maprr   C   s    G D DUU6;UUUBBBB>BBBCCCC   7 7IOTNN	-9-
 
 
 !36;f M M  	G!%!?!D!D!F!FH" 	% $H'/$)$##H$56666    &+ty11 	 	 +8 !&+vffV]^^^^)K$  		 	 	 	 	 	 	 	 	 	 	 	 	 	 	   ^ ^$TY/  	^/	8V_V\]]]
  K K(00 	K 	KDV_dD&'JJJJ	K    $TY/' 	 	D#<+  7---'(;D(A7CUVZC[\\ 	#yt49#///*1*:GwwE E E-1E E5<\E E E"K I	  	 s   $AFF		F		rD   r   rJ   r   boolc                8    | j         |j         dz   | j        z   k    S )N.)rT   rS   )rD   rJ   s     rE   is_from_modulerv      s    =FOc1DI===rG   	deser_ctxr'   c                   |D ]}|j                                         D ]}t          |j        t                    rst          |j        |          r^|j        |j        j                 }|| j        |j        <   | j	        
                    |j        j                   |j        | j        |j        <   |D ]N}t          |          D ]<}t          |j        |j        |j                  }|j        |         j        | j        |<   =OdS )z>Populate a Mapper with deserialized IR from a list of modules.N)ri   valuesrB   rD   r   rv   rk   rT   r^   r_   r`   ctorfunc_to_declre   r   rS   ra   	functionsdecl)r5   r6   rw   rJ   rD   irrn   func_ids           rE   load_type_mapr      s    9 9L'')) 	9 	9D$)X.. 9>$)V3T3T 9&ty'9:/1!$),'++DI,>???13#DI.	9  J J(00 	J 	JD&ty$-KKG(1(;G(D(IF%%	JJ JrG   Iterable[FuncDef]c              #     K   | j                                         D ]T}t          |j        t          t
          t          f          r+t          |j        |           rt          |j                  V  UdS )z?Collect all of the (non-method) functions declared in a module.N)	ri   ry   rB   rD   r   r   r   rv   r.   )rJ   rD   s     rE   re   re      s~      ##%% * * di'96G!HII 	*nIvO
 O
 	* ty)))))* *rG   module_namestr
class_name
str | Nonefdefr   r$   c                    |j         rt          n|j        rt          nt          }|                    ||j                  }t          |j        || ||          }||j	        |<   |S N)
	is_staticr#   is_classr!   r"   fdef_to_sigstrict_dunders_typingr$   rS   r{   )r   r   r   r5   r;   rq   sigr}   s           rE   rf   rf      sm     >	B"&-@[ 	
 

T7#@
A
ACDIz;TBBD $FKrG   r~   r    rI   r   FuncDef | Decoratorc                   t          |t                    r't          ||j        |||          | j        |j        <   d S t          |t
                    r t          ||j        |j        ||          }|j        s|| j        |j        <   nt          |j        d         t                    r_|j        d         j        dk    rIt          |j        z   |_        d|_
        d|j        j        d         _        || j        t          |j        z   <   |j        j        rC|j        j        sJ d|j         d            d|_        |j        j        | j        |j        <   d S d S d S )Nr   setterTrM   z.Expected return type annotation for property '')rB   r   rf   rS   method_declsr   rn   
decoratorsr   r   is_prop_setterr   argspos_onlyis_propertytypeis_prop_getterret_typeproperty_types)r~   r   rI   r5   rD   r;   r}   s          rE   prepare_method_defr      s`    $   =%5D&'&
 &
	""" 
D)	$	$ =  TY	67SS 
	?)-BODI&&*J77 	?DOA<N<SW_<_<_ '2DI"&D(,DHM!%:>BONTY679  	=9>``#`TXT]#`#`#````"&D+/8+<Bdi((('= = 	= 	=rG   propr   c                r   t          | j                  dk    rdS | j        d         }| j        d         }t          |t                    ont          |t                    oY|j        j        oMt          |j                  dk    o5t          |j        d         t                    o|j        d         j        dk    S )N   Fr   rM   r   )	lenitemsrB   r   rn   r   r   r   rS   )r   getterr   s      rE   is_valid_multipart_property_defr      s    
4:!uZ]FZ]F 	69%% 	2vy))	2K#	2 !""a'	2 v(+Z88		2
 a %1rG   builtin_basec                
    | dv S )N)zbuiltins.Exceptionzbuiltins.LookupErrorzbuiltins.IndexErrorzbuiltins.Warningzbuiltins.UserWarningzbuiltins.ValueErrorbuiltins.objectrA   )r   s    rE   can_subclass_builtinr      s    	

 
rG   rW   c           	        j         |j                 }|j        }t          |          }|                    d          du rd|_        |                    d          du rd|_        |j        D ]r}	|	j        dk    rd|_        |	j        dk    rd|_        (|	j        	                    d          r0t          |	j                  s|                    d	| |j                   sfd
|j        D             }
t          |
          dk    rBt          d |
D                       r)|
d         j        r|                    d| |j                   d |
D             |_        g }g }|j        D ]v}	|	j         vr|	j        dk    rd|_        j         |	         }|j        s|                    |           |                    |           |	j        j        s|j        sd|_        w|j        sdnd}t          |          |k    r||         |_        ||_        ||_        t3          ||| |||           t5          |||           |
D ]#}|j        |j                            |           $t9          |          r	d|_        dS dS )zPopulate the interface-level information in a class IR.

    This includes attribute and method declarations, and the MRO, among other things, but
    method bodies are generated in a later pass.
    allow_interpreted_subclassesTserializablezbuiltins.BaseExceptionPyBaseExceptionObjectzbuiltins.dictPyDictObjectz	builtins.z3Inheriting from most builtin types is unimplementedc                P    g | ]"}|j         j        v j        |j                  #S rA   )r   r^   )rC   baser5   s     rE   rF   z%prepare_class_def.<locals>.<listcomp>/  s3    cccdDIQWQbDbDbVty)DbDbDbrG   rM   c              3  &   K   | ]}|j          V  d S r   r2   rC   cs     rE   	<genexpr>z$prepare_class_def.<locals>.<genexpr>0  s&      <<!*n<<<<<<rG   r   z/Non-trait base must appear first in parent listc                     g | ]}|j         	|S rA   r   r   s     rE   rF   z%prepare_class_def.<locals>.<listcomp>6  s    000qQZ0000rG   r   N)r^   rU   r/   getr   _serializablerh   rT   r   
startswithr   rj   ra   basesr   anyr2   traitsinherits_pythonappenddefnremoved_base_type_exprsrX   r   base_mroprepare_methods_and_attributesprepare_init_methodr]   r0   is_augmented)rW   r   rI   r<   r5   r;   r~   rU   attrsclsr   rh   r   rp   base_idxr   s       `           rE   rb   rb     s    
	49	%B9DD!!Eyy/00D88*.'yy  D(( x   <3335BOO\_,,,BOO\$$[11 	'55 
 I4QUQZ  
 dcccdjcccE
5zzA~~#<<e<<<<<~qAR~
 	FdiXXX00E000BI
CHx & &f'''|000%)"#C( 	%OOG$$$

78+ 	&73G 	&!%B*qqH
8}}x8$BFBK"4T;PWXXXb+v666 % %=$M  $$$D  rG   c           
        | j         }|j                                        D ]\  }}	|	j        rt	          |	j        t                    r|	j        j        sJ d|z              |	j        j        s_|dvr[|	                    |	j        j                  }
|j
        r+|
j        r$t          |||
|           t          |||
|           |
|j        |<   t	          |	j        t          t           f          rt#          ||| ||	j        |           t	          |	j        t$                    r|	j        j        rWt)          |	j                  r%|	j        j        D ]}t#          ||| |||           G|                    d|| j                   e|	j        j        s|                    d|| j                   t#          ||| ||	j        j        |           |j        r|j                                         dS dS )z+Populate attribute and method declarations.zClass member %s missing type)	__slots____deletable__(Unsupported property decorator semanticsz2Overloads without implementation are not supportedN)rU   ri   r   plugin_generatedrB   rD   r   r   is_classvartype_to_rtyper2   error_overlapadd_getter_declarationadd_setter_declarationrg   r   r   r   r   r   r   rj   ra   implr   clear)rI   r~   rW   r   r<   r5   r;   rU   rS   rD   
attr_rtypeitems               rE   r   r   X  s3    9Dj&&(( #_ #_
d  	di%% 	_9>HH#AD#HHHH9( 	1T9W-W-W#11$).AA
; N:#; N
 +2tZMMM*2tZMMM&0d#	GY#788 	_r;fdiQQQQ	#455 	_y$ _249== ^ $	 Y Y*2{D&$PWXXXXY LL!KTSWS\]]]] y~ _LLLdTXT]    'r;fdinV]^^^	 
 rG   rU   r   c           
     f   t                      }|j        D ]J}|j                                        D ].\  }}|                    |           t          | |||||           /K|j        dd          D ]@}|j        r7|j                                        D ]\  }}||vrt          | |||||           Ad S )NrM   )setr   rg   r   r`   ,add_property_methods_for_attribute_if_neededrh   r2   )rU   r~   r   r5   concrete_attributesr   rS   r   s           rE   rd   rd     s     %%   $ 5 5 7 7 	 	D*##D)))8b$
K   	
 qrr
  = 	$(O$9$9$;$;   j222@b$
K  	 rG   	attr_namer   r)   c                    | j         dd         D ]}||j        v r|j        |         }|j                            |          }|5|j        }	t          |	t                    r!|	j        |j        vrt          ||||           rt          |	t                    r4t          |	          r%t          ||||           t          ||||           |j        r+|j        r$t          ||||           t          ||||           dS )zAdd getter and/or setter for attribute if defined as property in a base class.

    Only add declarations. The body IR will be synthesized later during irbuild.
    rM   N)rh   r^   ri   r   rD   rB   r   rS   r   r   r   r   r   r2   r   )
rU   r~   r   r   r   r5   r   rp   nrD   s
             rE   r   r     s1     O O6$$$'-G
y))Ay6D$	** 	Oty/O/O&r9j+NNNND"344 O9XY]9^9^ O&r9j+NNN&r9j+NNNN! Oj&> O&r9j+NNN&r9j+NNN!O OrG   c                    t          dt          |           d          }t          |g|          }t          || j        ||t
                    }d|_        d|_        || j        |<   || j	        |<   d S )NselfTr   )
r&   r(   r%   r$   rS   r"   r   implicitr   r   )r~   r   r   r   self_argr   r}   s          rE   r   r     st     &)B--$???H

J
/
/CIrwS+FFDDDM!%BOI#-Bi   rG   c                   t          dt          |           d          }t          d|d          }t          ||gt                    }t          |z   }t          || j        ||t                    }d|_        d|_	        || j
        |<   d S )Nr   Tr   value)r&   r(   r%   r+   r   r$   rS   r"   r   r   r   )	r~   r   r   r   r   	value_argr   setter_namer}   s	            rE   r   r     s     &)B--$???H7J>>>I
9-
?
?C 9,KK+sKHHDDDM#'BOK   rG   c           	        | j         d         j        }|j        sK|j        sEt	          |t
                    r1|                    |d          }|j                            |j                   }||j	        r| j         d         j
        re|j         j        dk    rUt          |j        d         t          dt          t                     t          dt"          t$                    g|j                  }t)          |j                  |j        z
  }t          |j        d|         t-          |                    }t/          | j        d ||          |_        |j        |j        | j         <   d S d S d S d S )N__init__Tr   r   r   kwargsrM   )rU   rD   r2   r   rB   r   r   r^   r   rX   r   rT   r%   r   r&   r,   r   r*   r	   r   r   num_bitmap_argsr(   r$   rS   rz   r{   )	rI   r~   r   r5   	init_nodeinit_sigdefining_irlast_argctor_sigs	            rE   r   r     sd   	*%*I; 1r 1:i3Q3Q 1%%i66'++IN;;
 +  y$5   n%):::$M!$v'7BBx)DD
 ! H x}%%(@@ qz!:IbMMJJ49dKBB)+DI&&&11 1 1 1 1 1rG   c           
        j         |j                 }|j        }|j                                        D ]}t	          |j        t          t          f          rt          ||||j        |           =t	          |j        t                    r|j        j
        rTt          |j                  s|                    d| |j                   |j        j        D ]}	t          ||||	|           t          |||t          |j                  |           t!          fd|j        D                       r|                    d| |j                   d S d S )Nr   c              3  N   K   | ]}|j         v oj         |         j        V   d S r   )r^   rX   )rC   r   r5   s     rE   r   z,prepare_non_ext_class_def.<locals>.<genexpr>  s<      
`
`PS3&##K(9#(>(K
`
`
`
`
`
`rG   z<Non-extension classes may not inherit from extension classes)r^   rU   ri   ry   rB   rD   r   r   r   r   r   r   rj   ra   r   r.   r   rh   )
rW   r   rI   r<   r5   r;   r~   rU   rD   r   s
       `     rE   rc   rc     s}    
	49	%B9D
!!## d ddi'9!566 	dr;fdiQQQQ	#455 		dy$ d6tyAA ^LL!KTSWS\]]] IO U UD&r;fdGTTTTU #2{D&,tyBYBY[bccc

`
`
`
`W[W_
`
`
``` 
JDRVR[	
 	
 	
 	
 	

 
rG   c                  $    e Zd ZU ded<   ded<   dS )SingledispatchInfoz%dict[FuncDef, list[RegisterImplInfo]]singledispatch_implszdict[FuncDef, list[int]]decorators_to_removeN__name__
__module____qualname____annotations__rA   rG   rE   r   r     s*         ????222222rG   r   c                    t          |          }| D ]#}|j        |_        |                    |           $t	          |j        |j                  S r   )SingledispatchVisitorrW   current_pathacceptr   r   r   )r6   r<   visitorrJ   s       rE   "find_singledispatch_register_implsr     sU     $F++G  %{gg:G<XYYYrG   c                  8     e Zd ZU ded<   d fdZd fd
Z xZS )r   r   r   r<   r   r=   r>   c                    t                                                       t          t                    | _        i | _        || _        d S r   )superr   r   listr   r   r<   )r   r<   	__class__s     rE   r   zSingledispatchVisitor.__init__)  sD     S^^bRcRc! ?A!$rG   decr   c                   |j         r|j                                         }g }d }t          |          D ]\  }}t          ||j                  }|u| j        |j                                     |j        |j        f           |                    |           |,| j	        
                    d| j        ||         j                   t          |d          r5|                    |           | j                            |j        g            |}|r|| j        |j        <   t!                                          |           d S )Nz:Calling decorator after registering function not supportedzfunctools.singledispatch)r   rZ   	enumerate%get_singledispatch_register_call_inforn   r   singledispatch_funcr   dispatch_typer<   rj   r   ra   r   
setdefaultr   r  visit_decorator)	r   r  decorators_to_storer   last_non_registeridr   r  s	           rE   r  z%SingledispatchVisitor.visit_decorator4  sj   > $	K"%."5"5"7"7.0  -1!"566 * *1<QII#-d.FGNN+SX6   )//222(4
 ))X -/0ABG   *!-GHH K,33A666 1<<SXrJJJ()%%# K 7K)#(3$$$$$rG   )r<   r   r=   r>   )r  r   r=   r>   )r   r   r   r   r   r  __classcell__)r  s   @rE   r   r   &  si         	% 	% 	% 	% 	% 	%'% '% '% '% '% '% '% '% '% '%rG   r   c                  $    e Zd ZU ded<   ded<   dS )RegisteredImplr   r  r   r	  Nr   rA   rG   rE   r  r  ^  s*             rG   r  	decoratorr   rn   RegisteredImpl | Nonec                ,   t          | t                    rt          | j                  dk    rvt          | j        d         t                    rV| j        }| j        d         j        }t          |t                    sd S t          |t                    rt          ||          S npt          | t                    r[|j
        sd S t          |j
        d         j        j                  }t          |t                    sd S |j        }t          | |          S d S )NrM   r   )rB   r
   r   r   r   calleerD   r   r   +registered_impl_from_possible_register_call	argumentsr   variabler   r   )r  rn   r  r	  arg_typerU   s         rE   r  r  c  s    	9h''L	1$$y~a('22 % !!q).-22 	4fj)) 	V>v}UUU	V 
Iz	*	* 	L ~ 	4"4>!#4#=#BCC(H-- 	4}:9dKKK4rG   exprr   r	  c                    | j         dk    rPt          | j        t                    r6| j        j        }t          |t
                    rt          |j        |          S d S )Nregister)rS   rB   r  r   rD   r   r  rn   )r  r	  rD   s      rE   r  r    sT     yJ:di#B#By~dI&& 	<!$)];;;4rG   N)r5   r-   r6   r7   r8   r   r9   r:   r;   r3   r<   r   r=   r>   )rD   r   rJ   r   r=   rs   )r5   r-   r6   r7   rw   r'   r=   r>   )rJ   r   r=   r   )r   r   r   r   r   r   r5   r-   r;   r3   r=   r$   )r~   r    r   r   rI   r   r5   r-   rD   r   r;   r3   r=   r>   )r   r   r=   rs   )r   r   r=   rs   )rW   r   r   r   rI   r   r<   r   r5   r-   r;   r3   r=   r>   )rI   r   r~   r    rW   r   r   r   r<   r   r5   r-   r;   r3   r=   r>   )
rU   r   r~   r    r   r   r5   r-   r=   r>   )rU   r   r~   r    r   r   r   r)   r   r   r5   r-   r=   r>   )
r~   r    r   r   r   r)   r   r   r=   r>   )
rI   r   r~   r    r   r   r5   r-   r=   r>   )r6   r7   r<   r   r=   r   )r  r   rn   r   r=   r  )r  r   r	  r   r=   r  )___doc__
__future__r   collectionsr   collections.abcr   typingr   
mypy.buildr   
mypy.nodesr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   mypy.semanalr   mypy.traverserr   
mypy.typesr   r   r   mypyc.commonr   r   mypyc.crashr   mypyc.errorsr   mypyc.ir.class_irr    mypyc.ir.func_irr!   r"   r#   r$   r%   r&   mypyc.ir.opsr'   mypyc.ir.rtypesr(   r)   r*   r+   r,   mypyc.irbuild.mapperr-   mypyc.irbuild.utilr.   r/   r0   r1   r2   mypyc.optionsr3   mypyc.sametyper4   rr   rv   r   re   rf   r   r   r   rb   r   rd   r   r   r   r   rc   tupleRegisterImplInfor   r   r   r  r  r  rA   rG   rE   <module>r5     s    # " " " " " # # # # # # $ $ $ $ $ $                                             " , + + + + + + + + + + + 6 6 6 6 6 6 6 6 6 6 9 9 9 9 9 9 9 9 $ $ $ $ $ $       % % % % % %                # " " " " " ` ` ` ` ` ` ` ` ` ` ` ` ` ` ' ' ' ' ' '              * ) ) ) ) ) ' ' ' ' ' 'I I I IX> > > >J J J J 	* 	* 	* 	*   $= = = =D   $   Q Q Q Qh1 1 1 1h   &O O O O>	. 	. 	. 	.
( 
( 
( 
(1 1 1 1<
 
 
 
> 7*+ 3 3 3 3 3 3 3 3
Z Z Z Z5% 5% 5% 5% 5%, 5% 5% 5%p    Z   
   @     rG   