
    mi                    *   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
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 dd	lmZm Z  dd
l!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-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< ddl=m>Z>m?Z?m@Z@ ddlAmBZBmCZCmDZDmEZE ddlFmGZGmHZH ddlImJZJmKZKmLZLmMZM ddlNmOZOmPZP ddlQmRZR ddlSmTZTmUZUmVZV ddlWmXZXmYZYmZZZ ddl[m\Z\ ddl]m^Z^ ddl_m`Z`maZa ddZbdd!Zcdd$Zddd(Ze	 	 ddd6Zfdd8Zgdd9Zh	 dddAZiddDZjddFZkddGZlddJZmddKZnddMZoddNZpd*dOddWZq G dX dYe          Zrdd^Zsdd`ZtddbZudddZvddfZwddkZxddoZyddqZzddtZ{ddxZ|dd{Z}dd|Z~dd}ZddZddZddZddZddZddZd)S )a  Transform mypy AST functions to IR (and related things).

Normal functions are translated into a list of basic blocks
containing various IR ops (defined in mypyc.ir.ops).

This also deals with generators, async functions and nested
functions. All of these are transformed into callable classes. These
have a custom __call__ method that implements the call, and state, such
as an environment containing non-local variables, is stored in the
instance of the callable class.
    )annotations)defaultdict)Sequence)
NamedTuple)
ArgKindClassDef	DecoratorFuncBaseFuncDefFuncItem
LambdaExprOverloadedFuncDefTypeInfoVar)CallableTypeTypeUnboundTypeget_proper_type)FAST_PREFIXLAMBDA_NAMEPROPSET_PREFIX	SELF_NAME)ClassIRNonExtClassInfo)FUNC_CLASSMETHODFUNC_NORMALFUNC_STATICMETHODFuncDeclFuncIRFuncSignature
RuntimeArg)
BasicBlockComparisonOpGetAttrIntegerLoadAddressLoadLiteralRegisterReturnSetAttrUnboxUnreachableValue)	RInstancebool_rprimitivec_int_rprimitivedict_rprimitiveint_rprimitiveobject_rprimitive)	IRBuildercalculate_arg_defaultsgen_arg_defaults)add_call_to_callable_classadd_get_to_callable_classinstantiate_callable_classsetup_callable_class)FuncInfoGeneratorClass)add_vars_to_envfinalize_env_classload_env_registerssetup_env_class)gen_generator_funcgen_generator_func_body)AssignmentTarget)dict_get_method_with_nonedict_new_opexact_dict_set_item_op)generic_getattrgeneric_setattrpy_setattr_op)register_function)builtin_names)is_same_method_signatureis_same_typebuilderr4   fdefr   returnNonec                2   | j                             || j        j                  }t	          | ||j        |          \  }}|r*|                     t          | |          ||j                   t          | |           | 
                    ||j                   d S N)mapperfdef_to_sigoptionsstrict_dunders_typinggen_func_itemnameassignget_func_targetlinemaybe_insert_into_registry_dictadd_function)rN   rO   sigfunc_irfunc_regs        n/var/www/html/bestrading.cuttalo.com/scripts/kaggle-env/lib/python3.11/site-packages/mypyc/irbuild/function.pytransform_func_defrc   ^   s    
.
$
$T7?+P
Q
QC%gtTYDDGX  Lw55xKKK#GT222$),,,,,    or   c                L    |j         sJ |                     |j                    d S rS   )implaccept)rN   re   s     rb   transform_overloaded_func_defri   j   s'    6MMMNN16rd   decr	   c                   | j                             |j        | j        j                  }t          | |j        |j        j        |          \  }}d }|rKt          | |j        |          }|                     t          | |j                  ||j        j
                   nd|j        | j        v rV|j        j                            d          d         }|                     ||j
                  }t          | |j        |          }|S|                     t           |                                 |                     |j        j                  |g|j
                   t'          | |j                   | j                            |           d S )N.)rT   rU   funcrV   rW   rX   rY   load_decorated_funcrZ   r[   r\   fdefs_to_decoratorsfullnamesplitload_global_strcall_crF   load_globals_dictload_strr]   	functionsappend)rN   rj   r_   r`   ra   decorated_funcrY   	orig_funcs           rb   transform_decoratorr{   p   sa   
.
$
$SXw/T
U
UC%gsxLLGX#'N K,WchIIw99>38=YYYY 
W0	0	0x &&s++B/ ++D#(;;	,Wch	JJ!"&&(('*:*:38=*I*I>Z	
 	
 	
 $GSX666W%%%%%rd   exprr   r-   c           	     Z   t          | j        |                   }t          |t                    s
J |            g }t	          |j        |j                  D ]W\  }}||j        _        |	                    t          |j        j        |                     |          |j                             X|                     |j                  }t          ||          }t            | j         }| xj        dz  c_        t%          | |||          \  }	}
|
J | j        	                    |	           |
S )N   )r   types
isinstancer   zip	arguments	arg_typesvariabletyperx   r!   rY   type_to_rtypekindret_typer    r   lambda_counterrX   rw   )rN   r|   typruntime_argsargarg_typer   fsigfnamer`   ra   s              rb   transform_lambda_exprr      s2   
'--
.
.Cc<((--#---LT^S];; 
 
X$s|('*?*?*I*I38TT	
 	
 	
 	
 $$S\22Hx00D4G244Ea%gtUDAAGXW%%%Ord   NFfitemr   rY   strr_   r    cdefClassDef | Nonemake_ext_methodbooltuple[FuncIR, Value | None]c                :    d}| j         v pt          |t                    }| j                                        v }| j        v }	| j        v d}
t           |          }d}r) j        j	        j
                 }|j         o| }
j        }rt          |          }n|}t          |||t                     |||	|
|	  	        }|j        }                     |j                   |rV j        j        }t          |t*                    s
J |             j        j        |         }t/          |           j        _        |s|r( j                                        st5                      |s|
rt7                      |r+t9            fd          \  }}t;           ||           nt=                     \  }}rQ j                             |           t          |t*                    s
J |            tC           ||j        |          S ||fS )a  Generate and return the FuncIR for a given FuncDef.

    If the given FuncItem is a nested function, then we generate a
    callable class representing the function and use that instead of
    the actual function. if the given FuncItem contains a nested
    function, then we generate an environment class so that inner
    nested functions can access the environment of the given FuncDef.

    Consider the following nested function:

        def a() -> None:
            def b() -> None:
                def c() -> None:
                    return None
                return None
            return None

    The classes generated would look something like the following.

                has pointer to        +-------+
        +-------------------------->  | a_env |
        |                             +-------+
        |                                 ^
        |                                 | has pointer to
    +-------+     associated with     +-------+
    | b_obj |   ------------------->  | b_env |
    +-------+                         +-------+
                                          ^
                                          |
    +-------+         has pointer to      |
    | c_obj |   --------------------------+
    +-------+
    NF)	r   rY   
class_name	namespace	is_nestedcontains_nestedis_decorated
in_non_extadd_nested_funcs_to_env)r   c           	     .    t          | ||          S rS   )gen_func_ir)argsblocksfn_inforN   r   is_singledispatchr_   s      rb   <lambda>zgen_func_item.<locals>.<lambda>  s!    +vsGT;L+ + rd   )"nested_fitemsr   r   encapsulating_funcskeysrp   singledispatch_implshas_nested_func_self_referencerT   
type_to_irinfois_ext_classrY   singledispatch_main_func_namer;   gen_func_nsis_generatorenterr   r   r   r   fdef_to_generatorr<   generator_class#can_merge_generator_and_env_classesr@   r:   rA   rB   gen_func_bodyrw   rx   gen_dispatch_func_ir)rN   r   rY   r_   r   r   ra   r   r   r   r   r   r   ir	func_namer   r   generator_class_irr`   r   s   `  ``              @rb   rX   rX      s   X "H
 ..O*UJ2O2OIw:??AAAOG77L!==J<WeLLJ ^&ty1(@-@
Y
 1$77			g&&'! 7
 
 
G 'LMM'CLM111 M%%))005000$^=eD*89K*L*L'  !!$_PPRR! 	    &J &W%%% Q.      
 
 	 (;;;;)'3>OPP M  )))%))005000#GUGL$LLLHrd   r   c           	        t          |            t          |            | j        j        rt	          |            t          |            |                     | j        j        j                   | 	                                 | j
        d         }|                                 \  }}}}}	t          | ||||	||          \  }
}t          | |	||           |
|fS )Nrm   )r?   r6   r   r   r>   r=   rh   r   bodymaybe_add_implicit_return	symtablesleaver   r5   )rN   r_   r   r   symtabler   _r   r   r   r`   ra   s               rb   r   r   #  s     wW& $7###GNN7?(-...%%'''  $H)0&D!VXw#GT63O`aaGX 7GXx@@@Hrd   c                     t          d  j                            |t                                D                       rdS t           fd j                            |g           D                       S )zDoes a nested function contain a self-reference in its body?

    If a nested function only has references in the surrounding function,
    we don't need to add it to the environment.
    c              3  @   K   | ]}t          |t                    V  d S rS   )r   r
   ).0syms     rb   	<genexpr>z1has_nested_func_self_reference.<locals>.<genexpr>B  s,      
Y
Y:c8$$
Y
Y
Y
Y
Y
Yrd   Tc              3  8   K   | ]}t          |          V  d S rS   )r   )r   nestedrN   s     rb   r   z1has_nested_func_self_reference.<locals>.<genexpr>D  sA         	'w77     rd   )anyfree_variablesgetsetr   )rN   r   s   ` rb   r   r   <  s     
Y
Y0F0J0J5RURWRW0X0X
Y
Y
YYY t    155eR@@     rd   r   list[Register]r   list[BasicBlock]r   r;   is_singledispatch_main_funcc           	        d}|j         s|j        r4t          | ||||          }t          | |           t	          | |          }n|j        }	t          |	t                    s
J |	            | j        j	        |	         }
|r?|j
        t          |
j
        z   k    r'| j        j        |j                 j        |j
                 }
|j        s|rY|dn|j
        }t!          |j
        || j        ||
j        |
j        |
j                  }
t+          |
|||	j        |	j
                  }nt+          |
|||	j        |	j
                  }||fS )a  Generate the FuncIR for a function.

    This takes the basic blocks and function info of a particular
    function and returns the IR. If the function is nested,
    also returns the register containing the instance of the
    corresponding callable class.
    N)is_prop_getteris_prop_setter)traceback_name)r   r   r7   r8   r9   r   r   r   rT   func_to_declrY   r   r   r   method_declsr   r   module_namer   r   r   r   r\   )rN   r   r   r_   r   r   r   ra   r`   r   	func_declr   s               rb   r   r   J  sf     "H ]G. ],WdFCQQ!'7333-gw??%))005000N/6	 	XGLK).$@@@1$)<I',WI 	]#> 	]!%49J #(7(7  I YfejQVQ[\\\GGYfejQVQ[\\\GXrd   r   getattrc           	     ,   |j         dz   }| j        j        |j                 }|j        }d|j          d}|j        r|                     |dz   |           |j        r|                     |dz   |           |                     ||t          d          5  | 
                    dt                    }|                     t          |                                 |g|          }t                      t                      }
}	t          d	t          |          }|                     t#          ||t"          j        |                    }|                     ||	|
           |                     |	           |                     t+          ||                     |                     |
           |                     |                                 |j         |gt          |          }|                     t+          ||                     d
d
d
           d
S # 1 swxY w Y   d
S )ag  
    Generate a wrapper function for __getattr__ that can be put into the tp_getattro slot.
    The wrapper takes one argument besides self which is the attribute name.
    It first checks if the name matches any of the attributes of this class.
    If it does, it returns that attribute. If none match, it calls __getattr__.

    __getattr__ is not supported in classes that allow interpreted subclasses because the
    tp_getattro slot is inherited by subclasses and if the subclass overrides __getattr__,
    the override would be ignored in our wrapper. TODO: To support this, the wrapper would
    have to check type of self and if it's not the compiled class, resolve "__getattr__" against
    the type at runtime and call the returned method, like _Py_slot_tp_getattr_hook in cpython.

    __getattr__ is not supported in classes which inherit from non-native classes because those
    have __dict__ which currently has some strange interactions when class attributes and
    variables are assigned through __dict__ vs. through regular attribute access. Allowing
    __getattr__ on top of that could be problematic.
    	__wrapperz&"__getattr__" not supported in class "
" because  it allows interpreted subclasses#it inherits from a non-native classTinternalattrr   N)rY   rT   r   r   r\   allow_interpreted_subclasseserrorinherits_pythonenter_methodr3   add_argumentrt   rG   selfr"   r%   addr#   NEQadd_bool_branchactivate_blockr)   gen_method_call)rN   r   r   rY   r   r\   
error_baseattr_arggeneric_getattr_resultreturn_genericcall_getattrnullgot_genericgetattr_results                 rb   generate_getattr_wrapperr   w  s8   $ <+%D		"49	-B<DO$)OOOJ	& Mj#EEtLLL	 Pj#HH$OOO			b$(9D		I	I 2 2''0ABB!(',,..RZA[]a!b!b'1||Z\\q+T22kk/|7GNN
 
 	^\JJJ~...F1488999|,,, 00LLNNGL8*6G
 
 	F>400111'2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2s   E*H		HHsetattrc           	        |j         dz   }| j        j        |j                 }|j        }d|j          d}|j        r|                     |dz   |           |j        r|                     |dz   |           |                     ||t          d          5  | 
                    dt                    }| 
                    d	t                    }t                      t                      }
}	t          d
t          |          }|                     t          ||t          j        |                    }|                     ||	|
           |                     |	           |j                            d          }|r|j        nd}|duo|j                            d           }|r-|                     |                                 d|gd|           n1|                     t4          |                                 ||g|           |                     t7          t          d
t                    |                     |                     |
           |                     |                                 |j         ||gd|           |                     t7          t          d
t                    |                     ddd           dS # 1 swxY w Y   dS )a  
    Generate a wrapper function for __setattr__ that can be put into the tp_setattro slot.
    The wrapper takes two arguments besides self - attribute name and the new value.
    Returns 0 on success and -1 on failure. Restrictions are similar to the __getattr__
    wrapper above.

    The wrapper calls the user-defined __setattr__ when the value to set is not NULL.
    When it's NULL, this means that the call to tp_setattro comes from a del statement,
    so it calls __delattr__ instead. If __delattr__ is not overridden in the native class,
    this will call the base implementation in object which doesn't work without __dict__.
    r   z&"__setattr__" not supported in class "r   r   r   Tr   r   valuer   __delattr__N	builtins.)rY   rT   r   r   r\   r   r   r   r   r0   r   r3   r"   r%   r   r#   EQr   r   r   noderq   
startswithr   r   rt   rH   r)   )rN   r   r   rY   r   r\   r   r   	value_argcall_delattrcall_setattrr   
is_delattrdelattr_symboldelattrdelattr_overrides                   rb   generate_setattr_wrapperr    s    <+%D		"49	-B<DO$)OOOJ	& Mj#EEtLLL	 Pj#HH$OOO			b$(84		H	H @ @''0ABB((2CDD	%/\\:<<lq+T22[[i|PT!U!UVV

L,GGG|,,,}55)7A.%%T"$._w7G7R7RS^7_7_3_ 	T##GLLNNMH:tUYZZZZ NN?W\\^^Xt,LdSSSF71&677>>???|,,,x>SUY[_```F71&677>>???5@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @s   HJ::J>J>c           	         |j         }| j        j        |j                 }| j                            || j        j                  }t          | ||||          \  }}| j        	                    |           t          | |          r|j                            d          \  }}}|                     |j                  }	|                     |	||j                  }
t!          | ||
          }|                     t$          |	|                     |          |g|j                   |j        r||j        vsJ |d f|j        |<   n0|| j        v r'||j        v sJ |j        |         \  }}||f|j        |<   ||j        |j        j         <   |j        dd          D ]}||j        v r|dk    rzt7          |j        |         j        |j        |         j                  sJt;          | |j        |         j        ||||          }||j        ||f<   | j        	                    |           |j        rAt;          | |j        ||||d          }||j        ||f<   | j        	                    |           |j         dk    rtA          | ||           d S |j         dk    rtC          | ||           d S |j         dk    r_|j        "                    d          }|r&|j#        r|j#        j        $                    d	          r| %                    d
|j                   d S d S d S )Nrl   r~   __init__T	do_py_ops__getattr____setattr__r   r   z{"__delattr__" supported only in classes that also override "__setattr__", or inherit from a native class that overrides it.)&rY   rT   r   r   rU   rV   rW   rX   rw   rx   r   rq   
rpartitionload_native_type_objectpy_get_attrr\   ro   primitive_oprI   rv   is_property
propertiesprop_settersmethodsdeclmror   rL   r_   gen_glueglue_methodsr   r   r  r   r   r   r   )rN   r   rO   rY   class_irr_   r`   ra   r   r   rz   ry   	getter_irbasefr   s                   rb   handle_ext_methodr    su   9D~(3H
.
$
$T7?+P
Q
QC%gtT3EEGXW%%%GT"" 
]--c22
1d --dm<<''T49==	 -WdIFF 	C!1!1$!7!7H$)	
 	
 	
  
9 8.....%,dOD!!	%	%	%x******40	1%.$8D!*1HW\&' QRR  ( (D%%%
"",%d+/1B41H1L  # $"3D"9"=wRVX\]]A23H!4,/$$Q''' , $Wgk7HhX\]]]23x./  ###yM!! $55555	m	#	# $55555	m	#	#)--.. 	gl 	gl.C.N.N{.[.[ 	MMF	     
$	#	 	rd   non_extr   c                   |j         }| j                            || j        j                  }t          | ||||          \  }}|J | j                            |           t          | |          r|}t          | ||          }|j
        r9|                     d|j                  }	|                     |	|g|j                  }n| j        j        |         j        t           k    r9|                     d|j                  }
|                     |
|g|j                  }nX| j        j        |         j        t"          k    r8|                     d|j                  }|                     ||g|j                  }|                     ||||j                   | j        j        |j                 }t*          |j         z   }||j        v r>t          | ||||d          \  }}||j        |<   | j                            |           d S d S )Nzbuiltins.propertyzbuiltins.classmethodzbuiltins.staticmethodT)r   )rY   rT   rU   rV   rW   rX   rw   rx   r   ro   r  load_module_attr_by_fullnamer\   py_callr   r   r   r   add_to_non_ext_dictr   r   r   r   r  )rN   r  r   rO   rY   r_   r`   ra   rz   propcls_meth	stat_methr  s                rb   handle_non_ext_methodr$  ,  s    9D
.
$
$T7?+P
Q
QC%gtT3EEGXW%%%GT"" A	&wi@@  
E334GSS??4(TY??		$T	*	/3C	C	C778NPTPYZZ??8hZCC		$T	*	/3D	D	D889PRVR[\\	??9xj$)DDxCCC ~(3H"Dx$$$)'4sDZ^___!(  ))))) %$rd   c                J    d                     d | j        D                       S )zJGenerate a namespace for a nested function using its outer function names.r   c              3  p   K   | ]1}|j         r(|j         d k    |j         |j        sdn	d|j        z   z   V  2dS )z<module> r   N)rY   r   )r   r   s     rb   r   zgen_func_ns.<locals>.<genexpr>V  s`        9 j00 		tIRRC$/4IJ0000 rd   )joinfn_infos)rN   s    rb   r   r   T  s7    88  $     rd   orig_func_regc                   t          | |          s|S | j        |         }|}t          |          D ]X}|                    | j                  }t          |t                    s
J |            |                     ||g|j                  }Y|S )zApply decorators to a function.

    Given a decorated FuncDef and an instance of the callable class
    representing that FuncDef, apply the corresponding decorator
    functions on that decorated FuncDef and return the decorated
    function.
    )	r   rp   reversedrh   visitorr   r-   r  r\   )rN   rO   r*  
decoratorsra   d	decorators          rb   ro   ro   ]  s     &&  ,T2JHj!! I IHHW_--	)U++66Y666??9xj(-HHOrd   c                    || j         v S rS   )rp   rN   rO   s     rb   r   r   s  s    7...rd   r  base_sigtargetr   clsr   r  r  c          	     x    |j         rt          | |||||j        |          S t          | |||||j        |          S )ay  Generate glue methods that mediate between different method types in subclasses.

    Works on both properties and methods. See gen_glue_methods below
    for more details.

    If do_py_ops is True, then the glue methods should use generic
    C API operations instead of direct calls, to enable generating
    "shadow" glue methods that work with interpreted subclasses.
    )r  gen_glue_propertyr\   gen_glue_method)rN   r3  r4  r5  r  rO   r  s          rb   r  r  w  sJ    &  [ (FCtyR[\\\w&#tTYPYZZZrd   c                  .    e Zd ZU ded<   ded<   ded<   dS )ArgInfozlist[Value]r   zlist[str | None]	arg_nameszlist[ArgKind]	arg_kindsN)__name__
__module____qualname____annotations__ rd   rb   r:  r:    s6         rd   r:  rt_argsSequence[RuntimeArg]r\   intc                     d |D             } fd|D             }d |D             }d |D             }t          |||          S )Nc                D    g | ]}t          |j                  |j        fS rA  )r   rY   r   r   r   s     rb   
<listcomp>zget_args.<locals>.<listcomp>  s'    >>>s#ch--*>>>rd   c           	     n    g | ]1\  }}                                         ||d                     2S )T)is_arg)readadd_local_reg)r   varr   rN   r\   s      rb   rH  zget_args.<locals>.<listcomp>  sP       C 	W**3T*BBDII  rd   c                    g | ]D}|j                                         s |j                                         r|j        s|j        nd ES rS   )r   is_namedis_optionalpos_onlyrY   rG  s     rb   rH  zget_args.<locals>.<listcomp>  s\        H%%''bCH,@,@,B,Bb3<b^b  rd   c                    g | ]	}|j         
S rA  )r   rG  s     rb   rH  zget_args.<locals>.<listcomp>  s    ---c---rd   )r:  )rN   rB  r\   	fake_varsr   r;  r<  s   ` `    rb   get_argsrT    s    >>g>>>I    "  D   I .-W---I4I...rd   	do_pycallc                   t          | ||j        j        |           |                                  |j        | j        d<   t          |j                  }|j        j        t          k    r0t          |j        d         j        t          |                    |d<   t          | ||          }|j        |j        |j        }}
}	d}|j        rW|	d|j                  }	|
d|j                  }
|d|j                  }t          | j        j        |j         d                   }t%          d |
D                       r*t%          d |j        j        j        D                       rd}|ry|j        j        t&          k    r| j                            |          }d}n
|	d         }d}| j                            ||j        |	|d         ||
|d         ||d                   }n%| j                            |j        |	|
|||          }|                     ||j        |          }|                     t3          |                     |                                 \  }}}}}|j        r|d|j                  }t7          t9          |j        d	z   |j        z   d
z   |j        | j        t=          ||          |j        j                  ||          S )aU  Generate glue methods that mediate between different method types in subclasses.

    For example, if we have:

    class A:
        def f(builder: IRBuilder, x: int) -> object: ...

    then it is totally permissible to have a subclass

    class B(A):
        def f(builder: IRBuilder, x: object) -> int: ...

    since '(object) -> int' is a subtype of '(int) -> object' by the usual
    contra/co-variant function subtyping rules.

    The trickiness here is that int and object have different
    runtime representations in mypyc, so A.f and B.f have
    different signatures at the native C level. To deal with this,
    we need to generate glue methods that mediate between the
    different versions by coercing the arguments and return
    values.

    If do_pycall is True, then make the call using the C API
    instead of a native call.
    rm   r   Nc              3  >   K   | ]}|                                 V  d S rS   )is_star)r   r   s     rb   r   z"gen_glue_method.<locals>.<genexpr>  s*      
0
0d4<<>>
0
0
0
0
0
0rd   c              3  J   K   | ]}|j                                          V  d S rS   )r   rX  rG  s     rb   r   z"gen_glue_method.<locals>.<genexpr>  sD       9 9#&CH9 9 9 9 9 9rd   Tr~   )bitmap_args___glue)check_native_overrider  r_   r   r   	ret_typeslistr   r   r   r!   rY   r.   rT  r<  r;  num_bitmap_argsrN   r   r   get_native_typepy_method_callcallcoercer   r)   r   r   r   r   r    )rN   r3  r4  r5  r  r\   rU  rB  arg_infor   r<  r;  rZ  firststretvalarg_regsr   r   r   s                       rb   r8  r8    s   D '8V[_dCCCMMOOO$-Gb8=!!G{;&&a 0 5y~~FF
$//H!)0BHDVY)DK N/x///09!9 99:	9!9 99:	7?/1I0I0K0KLMM 
0
0i
0
0
000 S 9 9*0+/*>9 9 9 6 6  	 
;000O33C88EBBGEB//6;RSS	4233233
 
 %%Ky)T{ & 
 
 ^^FH$5t<<FKKv'.}}$Ha1 75X5556K$*W4H'8,,K	
 	
 	
 
 
rd   sub_sigc           	        t          |                                |                                          D ]\  }}|j        j        r7|j        s0|j        r)|j        r"|                     dd|j         dz   |           |j        j        s|j        j        rDt          |j        |j                  s*|                     dd|j         d|j         dz   |           dS )zReport an error if an override changes signature in unsupported ways.

    Glue methods can work around many signature changes but not all of them.
    zAn argument with type "z6" cannot be given a default value in a method overridezIncompatible argument type z" (base class has type "z")N)r   	real_argsr   error_overlapoptionalr`  r   rM   )rN   r3  rj  r\   base_argsub_args         rb   r]  r]    s    !!3!3!5!5w7H7H7J7JKK  '=& 		$ )9 h>V  ,_(-___`  
 =& 	',*D 	w|<<  1Q',QQQQQR   rd   do_pygetattrc                   |                                   t          t          t          |                    }|                     |          }|                     ||          }	|j        | j        d<   |r|                     |	|j	        |          }
n)| 
                    t          |	|j	        |                    }
|                     |
|j        |          }| 
                    t          |                     |                                 \  }}}}}t          t!          |j	        dz   |j	        z   dz   |j	        | j        t%          |g|                    ||          S )a  Generate glue methods for properties that mediate between different subclass types.

    Similarly to methods, properties of derived types can be covariantly subtyped. Thus,
    properties also require glue. However, this only requires the return type to change.
    Further, instead of a method call, an attribute get is performed.

    If do_pygetattr is True, then get the attribute using the Python C
    API instead of a native call.
    rm   r[  r\  )r   r!   r   r.   add_self_to_envrK  r   r^  r  rY   r   r$   rd  r)   r   r   r   r   r    )rN   r_   r4  r5  r  r\   rr  rt_argself_targetr   rh  retboxr   r   r   return_types                   rb   r7  r7     s;   $ MMOOO	9S>>22F))#..K
,,{D
)
)CLGb >$$S&+t<<WS&+t<<==^^FCL$77FKKv&-mmoo#D!V[!K$*W4H6(K00		
 	
 		 	 	rd   rC   c                    |j         r|                     |j                   S | j        j        s| j        j        r|                     |          S |                     |t                    S )zGiven a FuncDef, return the target for the instance of its callable class.

    If the function was not already defined somewhere, then define it
    and add it to the current environment.
    )original_deflookupr   r   r   rL  r3   r2  s     rb   r[   r[   L  sf      1~~d/000# $w'N $~~d###  '8999rd   r   r   unbounded_typeType | Nonec                .   t          |t                    r|j        n|j                            d|j                   || j        j        v r.| j        j        |         }| j                            |          }n|j        t          v r:t          |j                 \  }}| 
                    t          |||                    }nt          fd| j        D             dt                    x}r|                     |          }	|k    r+                    | d                              d          ng D ]}
|                     |	|
|          }	| j                            |	|j        t(          |d          }n|                     |j        |          }|S )Nrl   c              3  X   K   | ]$}|k    s                     | d            |V  %dS )rl   N)r   )r   iload_attr_paths     rb   r   zload_type.<locals>.<genexpr>y  sJ      aaq~':':n>W>W[\X_X_X_>`>`':':':':':aard   r'  )defaultkeyF)borrow)r   r   rY   rq   removesuffixrT   r   rN   ra  rK   r   r&   maximportslenload_moduleremoveprefixrr   r  get_attrr3   rs   )rN   r   r|  r\   r  	class_objbuiltin_addr_typesrcr   loaded_moduler   r  s              @rb   	load_typer  a  s     *.+FFXCLl>sx>>""  gn'''>,S1O33H==				&	&!.s|!<3KK,=sD I IJJ		aaaaGOaaa   
 <  ++K88
 ,, '';(9(9(9::@@EEE	K 	KD
 $//tTJJMMO,,38%6U - 
 
		 ++CHd;;	rd   r   rq   
str | Nonec                    |rb|                     | j                  sH|                    d          d         }|                     |          }|                     |||          }n|                     ||          }|S )Nrl   r   )r   current_modulersplitr  r  rs   )rN   r   rq   r\   moduler  rn   s          rb   	load_funcr    s     8++G,BCC 8 %%a(++F33""=)TBB&&y$77Krd   !main_singledispatch_function_namec                r   |j         }| j        j        |         }t          | |j        j        |          }|                                 }| j                            |j        d         |          }| j        	                    |dt          |          }t                      t                      t                      }}
}	|                     t          ||g|          }|                     ||                                 d|          }t!          t"                    }|                     ||
|	           |                     |
           |                     |||           |                     |           |                     |	           |                     d|          }t/          | ||          }|                     |||g|          }|                     t4          |||g|           |                     |||           |                     |           |                     |           t7          | ||||           d S )Nr   dispatch_cachezis notzfunctools._find_impl)r\   rT   r   rT  r_   r   r   rN   get_type_of_objr  r1   r"   r  rD   translate_is_opnone_objectr(   r3   r   r   rZ   gotor  load_singledispatch_registryr  rt   rF   gen_calls_to_correct_impl)rN   r  r   r\   current_func_declre  dispatch_func_objr   r  call_find_impl	use_cache	call_func
get_resultis_not_noneimpl_to_use	find_implregistryuncached_impls                     rb   )generate_singledispatch_dispatch_functionr    s    :D3E:!2!6!;TBBH..x}Q/?FFH_--+_d N ,6<<z||yIN%%&?.RZA[]abbJ))*g6I6I6K6KXW[\\K,--KKNCCC9%%%NN;
D111LL>***445KTRRI+G5FMMHOOI(/CTJJMNN)NHm+TVZ[[[NN;t444LL9%%%g{HeTJJJJJrd   r  re  c                     j         j        |         d fd}t          d         \  }}                     t	          ||                    } j                            ||          }	t                      t                      }}
                     |	|
|            	                    |
                                t          |t                              }t           |          }|                                D ]\  }}t                      t                      }}                     |          }                     ||d          }                     |||            	                    |            ||            	                    |                                t!                                  	                    |                                |j        j        j                  }                     |j        j                  }                     t1          |                     d S )	NrO   r   rP   rQ   c                   j         j        |          }j                            |j        j        j                  }                    |j        j	                  }
                    t          |                     d S rS   )rT   r   rN   rc  r   r<  r;  rd  r_   r   r   r)   )rO   r   ret_valcoercedre  rN   r  r\   s       rb   gen_native_func_call_and_returnzBgen_calls_to_correct_impl.<locals>.gen_native_func_call_and_return  sx    N/5	/&&x}h&8(:Ld
 
 ..*;*?*H$OOF7OO$$$$$rd   zbuiltins.intz==)rO   r   rP   rQ   )rT   r   rK   r   r&   rN   
type_is_opr"   r   r   r+   r2   get_native_impl_idsitemsload_int	binary_opr,   r  r   r<  r;  rd  r_   r   r)   )rN   r  re  r   r\   r  r   r  int_type_objis_intnative_callnon_native_call	passed_id
native_idsrg   r  	call_impl	next_impl
current_idcondr  r  r  s   ` ` `                 @rb   r  r    s4     3E:% % % % % % % % % ^,HC;;{3T::;;L_''\4HHF#-<<KFKAAA;'''E+~tDDEEI$We44J##%% * *a)||Z\\9	%%a((
  JdCCi;;; 	y)))''---y)))) KK?+++ooX]D(*<h>P G nnW&7&;&DdKKGKKw     rd   main_func_namedispatch_nametuple[FuncIR, Value]c                   |                      t          ||                     t          |            t          | j        j        j        j        d<   t          | j        j        j        j        d<   d| j        j        j        _        d| j        j        j        _	        t          |            t          | ||           |                                 \  }}}}}t          | ||||          }	| j                            |	           t!          | |           t#          | |           t%          | |          }
t'          | ||	j        |          }||
fS )zCreate a dispatch function (a function that checks the first argument type and dispatches
    to the correct implementation)
    r  r  T)r   r;   r:   r1   r   callable_classr   
attributeshas_dictneeds_getseters+generate_singledispatch_callable_class_ctorr  r   r7   rw   rx   r8   %add_register_method_to_callable_classr9   &generate_dispatch_glue_native_functionr  )rN   r   r  r  r_   r   r   r   r   dispatch_callable_classra   dispatch_func_irs               rb   r   r     s:    MM(5-00111!!!?NGO"%0<ETGO"%01AB15GO"%.8<GO"%5/888-g~uMMM")--//D!VQ8$PSU\]]4555gw///)'7;;;)'7;;H=/4m  X%%rd   callable_class_declr   c                   |j         }|                                  |                     ||          }| j        j        |         }t          | |j        j        |          }|g|j        z   }t          j	        g|j
        z   }	|j        }
|
                    dd           | j                            |||	|
|          }|                     t!          |                     |                                 \  }}}}}t%          |||          S )Nr   r   )r\   r   rs   rT   r   rT  r_   r   r   ARG_POSr<  r;  insertrN   rc  r   r)   r   r   )rN   r   r  r  r\   r  r  re  r   r<  r;  r  ri  r   r   r   s                   rb   r  r    s     :DMMOOO,,]DAAN>&u-D55Hhm+D!H$66I"IQo""#6iTXYYGKKw   &-mmoo#HaG$&)))rd   c           
     n   d}| j         j        j        }|                     |dt                    5  |                     t          g |          }|                     t          | 	                                d||                     |                     t          g |          }| 
                    d          }|                     t          | 	                                ||g|           |                     t          t          dt          |          |                     ddd           dS # 1 swxY w Y   dS )zGCreate an __init__ that sets registry and dispatch_cache to empty dictsrm   r  r  r  r~   N)r   r  r   r   r/   rt   rE   r   r*   r   rv   r  rI   r)   r%   )rN   r\   r  
empty_dict
cache_dictdispatch_cache_strs         rb   r  r  "  s]   D-0H			h
O	D	D E E^^KT::
GGLLNNJ
DIIJJJ^^KT::
$--.>??]W\\^^=OQ[,\^bcccF71ot<<dCCDDDE E E E E E E E E E E E E E E E E Es   C-D**D.1D.c                   d}|                      |j        j        dt                    5  |                     dt                    }|                     dt          t
          j                  }|                     t          | 	                                ||g|          }| 
                    t          ||                     d d d            d S # 1 swxY w Y   d S )Nrm   registerr5  rn   )r   r  r   r3   r   r   ARG_OPTrt   rJ   r   r   r)   )rN   r   r\   cls_argfunc_argr  s         rb   r  r  1  s    D			g47EV	W	W + +&&u.?@@''0A7?SS..!2W\\^^Wh4WY]^^F7D))***	+ + + + + + + + + + + + + + + + + +s   BCCCr  c                F    | j                             |dt          |          S )Nr  )rN   r  r1   )rN   r  r\   s      rb   r  r  :  s     ?##$5z?TXYYYrd   	orig_namec                    d|  dS )N%__mypyc_singledispatch_main_function_r[  rA  )r  s    rb   r   r   >  s    @9@@@@rd   c                   |j         }|| j        v }t          t                    }| j                                        D ].\  }}|D ]&\  }}||k    r||                             |           '/|s|sd S |rt          |j                  }	t          | |	|j	        |          }
| 
                    d|          }| j                            ||
fg|          }|                     |j        |          }|                     t          ||                     d          |g|           |                                D ]\  }}t#          | |          }||vrt          | |j        |j	        |          }n2||         }t%          |t&                    }|                     |          }t          | |j        |j	        |          }t+          | ||          }|D ]3}t-          | |d |          }|                     t0          |||g|           4| j                            |dt4          |          }|                     |dg d |           	d S )Nzbuiltins.objectr  r  clear)r\   r   r   r_  r  rx   r   rY   r  rq   r  rN   	make_dictrs   r  rI   rv   r  r'   r3   r   r  r  rt   rF   r  r1   r   )rN   r   r\   r   to_register	main_funcimplsdispatch_typerg   r  main_func_objloaded_object_typeregistry_dictr  singledispatch_funcr   r  	to_insertr  load_literalr  r   loaded_typer  s                           rb   r]   r]   B  s   :D"'7+G"G8CD8I8IK#8>>@@ = =	5#( 	= 	=M4}}I&--m<<<	=  : " 

6uzBB!'>5>4PP$AABSUYZZ114F3V2WY]^^#33EJEE-w/?/?
/K/K][]a	
 	
 	
 '2&7&7&9&9 I I"U )2EFF

""!'5:u~tLLII#E*J&z3DEELL11I%(-/B/KT
 
 09JDQQ 	] 	]C#GS$==KNN1Hk93UW[\\\\ 11/$
 
 	T4HHHH+I Ird   r  dict[FuncDef, int]c                T      j         |         } fdt          |          D             S )zgReturn a dict of registered implementation to native implementation ID for all
    implementations
    c                B    i | ]\  }\  }}t          |          ||S rA  )r   )r   r  r   rg   rN   s       rb   
<dictcomp>z'get_native_impl_ids.<locals>.<dictcomp>x  s3    ^^^;C,wX\B]B]^D!^^^rd   )r   	enumerate)rN   r  r  s   `  rb   r  r  s  s5     ()<=E^^^^)E*:*:^^^^rd   r   is_traitc                   |j         }|                     |           |                     d|j        j        d         j                  }|sJ| j                            |||j        j        d          }| 	                    t          |                     n!| 	                    t                                 |                                 \  }}}	}
}t          |||	          S )zGenerate an implicit trivial property getter for an attribute.

    These are used if an attribute can also be accessed as a property.
    r   r   rm   )rY   r   r   r_   r   r   rN   r  r   r   r)   r,   r   r   )rN   r   r   r  rY   self_regr   r   r   r   r   r   s               rb   gen_property_getter_irr  {  s     >DMM$##FIM,>q,A,FGGH #((49OQSTTF5MM""""KMM""")0&D!VXw)T6***rd   c                b   |j         }|                     |           |                     d|j        j        d         j                  }|                     d|j        j        d         j                  }|                    t                    sJ |t          t                    d         }|s%| 	                    t          |||d                     | 	                    t          |                                                      |                                 \  }}	}
}}t          |||
          S )zGenerate an implicit trivial property setter for an attribute.

    These are used if an attribute can also be accessed as a property.
    r   r   r   r~   Nrm   )rY   r   r   r_   r   r   r   r   r  r   r*   r)   noner   r   )rN   r   r   r  rY   r  	value_reg	attr_namer   r   r   r   r   s                rb   gen_property_setter_irr    s    >DMM$##FIM,>q,A,FGGH$$Wim.@.C.HIII??>*****S((**+I AGHiB??@@@KKw||~~&&''')0&D!VXw)T6***rd   )rN   r4   rO   r   rP   rQ   )rN   r4   re   r   rP   rQ   )rN   r4   rj   r	   rP   rQ   )rN   r4   r|   r   rP   r-   )NF)rN   r4   r   r   rY   r   r_   r    r   r   r   r   rP   r   )
rN   r4   r_   r    r   r   r   r   rP   r   )rN   r4   r   r   rP   r   )F)rN   r4   r   r   r   r   r_   r    r   r;   r   r   r   r   rP   r   )rN   r4   r   r   r   r   rP   rQ   )rN   r4   r   r   r   r   rP   rQ   )rN   r4   r   r   rO   r   rP   rQ   )
rN   r4   r  r   r   r   rO   r   rP   rQ   )rN   r4   rP   r   )rN   r4   rO   r   r*  r-   rP   r-   )rN   r4   rO   r   rP   r   )rN   r4   r3  r    r4  r   r5  r   r  r   rO   r   r  r   rP   r   )rN   r4   rB  rC  r\   rD  rP   r:  )rN   r4   r3  r    r4  r   r5  r   r  r   r\   rD  rU  r   rP   r   )
rN   r4   r3  r    rj  r    r\   rD  rP   rQ   )rN   r4   r_   r    r4  r   r5  r   r  r   r\   rD  rr  r   rP   r   )rN   r4   rO   r   rP   rC   )
rN   r4   r   r   r|  r}  r\   rD  rP   r-   )
rN   r4   r   r   rq   r  r\   rD  rP   r-   )rN   r4   r  r   r   r   rP   rQ   )rN   r4   r  r-   re  r:  r   r   r\   rD  rP   rQ   )rN   r4   r   r   r  r   r  r   r_   r    rP   r  )
rN   r4   r   r   r  r   r  r   rP   r   )rN   r4   rP   rQ   )rN   r4   r   r;   rP   rQ   )rN   r4   r  r-   r\   rD  rP   r-   )r  r   rP   r   )rN   r4   r   r   rP   rQ   )rN   r4   r  r   rP   r  )
rN   r4   r   r   r   r   r  r   rP   r   )__doc__
__future__r   collectionsr   collections.abcr   typingr   
mypy.nodesr   r   r	   r
   r   r   r   r   r   r   
mypy.typesr   r   r   r   mypyc.commonr   r   r   r   mypyc.ir.class_irr   r   mypyc.ir.func_irr   r   r   r   r   r    r!   mypyc.ir.opsr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   mypyc.ir.rtypesr.   r/   r0   r1   r2   r3   mypyc.irbuild.builderr4   r5   r6   mypyc.irbuild.callable_classr7   r8   r9   r:   mypyc.irbuild.contextr;   r<   mypyc.irbuild.env_classr=   r>   r?   r@   mypyc.irbuild.generatorrA   rB   mypyc.irbuild.targetsrC   mypyc.primitives.dict_opsrD   rE   rF   mypyc.primitives.generic_opsrG   rH   rI   mypyc.primitives.misc_opsrJ   mypyc.primitives.registryrK   mypyc.sametyperL   rM   rc   ri   r{   r   rX   r   r   r   r   r  r  r$  r   ro   r   r  r:  rT  r8  r]  r7  r[   r  r  r  r  r   r  r  r  r  r   r]   r  r  r  rA  rd   rb   <module>r     s*  
 
 # " " " " " # # # # # # $ $ $ $ $ $                              H G G G G G G G G G G G L L L L L L L L L L L L 6 6 6 6 6 6 6 6                                                            V U U U U U U U U U            ; : : : : : : :            P O O O O O O O 2 2 2 2 2 2         
 Y X X X X X X X X X 7 7 7 7 7 7 3 3 3 3 3 3 A A A A A A A A
	- 	- 	- 	-   & & & &>   > !!w w w w wt   2   * ).* * * * *Z/2 /2 /2 /2d0@ 0@ 0@ 0@fM M M M`%* %* %* %*P      ,/ / / / [ [ [ [ [ [2    j   / / / /[ [ [ [|   :) ) ) )X: : : :*, , , ,^     K  K  K  KF-! -! -! -!`& & & &8* * * *&E E E E+ + + +Z Z Z ZA A A A.I .I .I .Ib_ _ _ _+ + + +&+ + + + + +rd   