
    Ae                         d dl mZ d dlZddlmZmZmZmZmZm	Z	 ddlm
Z
mZmZ ddlmZmZmZmZ ddlmZ  G d	 d
e      Zy)    )absolute_importN   )	ExprNodes
PyrexTypes
MemoryViewParseTreeTransformsStringEncodingErrors)	CloneNode	ProxyNode	TupleNode)FuncDefNodeCFuncDefNodeStatListNodeDefNode   )
OrderedSetc                        e Zd ZdZdZdZdZdZdZe	j                  g dz   Z
 fdZd Zd Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd ZdZdZd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z& xZ'S ) FusedCFuncDefNodea  
    This node replaces a function with fused arguments. It deep-copies the
    function for every permutation of fused types, and allocates a new local
    scope for it. It keeps track of the original function in self.node, and
    the entry of the original function in the symbol table is given the
    'fused_cfunction' attribute which points back to us.
    Then when a function lookup occurs (to e.g. call it), the call can be
    dispatched to the right function.

    node    FuncDefNode    the original function
    nodes   [FuncDefNode]  list of copies of node with different specific types
    py_func DefNode        the fused python function subscriptable from
                           Python space
    __signatures__         A DictNode mapping signature specialization strings
                           to PyCFunction nodes
    resulting_fused_function  PyCFunction for the fused DefNode that delegates
                              to specializations
    fused_func_assignment   Assignment of the fused function to the function name
    defaults_tuple          TupleNode of defaults (letting PyCFunctionNode build
                            defaults would result in many different tuples)
    specialized_pycfuncs    List of synthesized pycfunction nodes for the
                            specializations
    code_object             CodeObjectNode shared by all specializations and the
                            fused function

    fused_compound_types    All fused (compound) types (e.g. floating[:])
    N)__signatures__resulting_fused_functionfused_func_assignmentc                    t         t        |   |j                         g | _        || _        t        | j
                  t              }|r| j                  |       n| j                  |       | j                  D ]  }|j                  j                  j                  rJ |j                  j                  j                  rJ |j                  j                  r|j                  j                  rJ |rx|j                  j                   s|j                  j"                  rJ  | |j                  _        | j                  d d  | _        y N)superr   __init__posnodesnode
isinstancer   copy_def	copy_cdefentrytypeis_fusedlocal_scopereturn_typecfunc_declaratoroptional_arg_countop_arg_structfused_cfunctionstats)selfr   envis_defn	__class__s        9lib/python3.12/site-packages/Cython/Compiler/FusedNode.pyr   zFusedCFuncDefNode.__init__2   s    /9
	DIIw/MM#NN3  	,Aww||,,,}}00999((==111a00CCvv+++	, &*

" ZZ]
    c                    t        j                  | j                  j                  D cg c]%  }|j                  j
                  s|j                  ' c}      }| j                  |      }t        j                  |      }|| _        | j                  j                  |j                  v r/|j                  j                  | j                  j                         |D ]X  \  }}t        j                  | j                        }| j                  j                  j                  |j                  _        | j                  |j                  |       | j                  j                   j#                  |      |_        |j%                  |       | j'                  |||       | j)                  ||| j                  j                  ||       t        j*                  |j                  |       d|j                  _        |j                  |j.                  |j                  j0                  <   | j3                  |      rY n | j                  | _        | j7                  | j                  |d      | _        yc c}w )zh
        Create a copy of the original def or lambda function for specialized
        versions.
        Tr/   N)r   uniquer   argsr$   r%   _get_fused_base_types get_all_specialized_permutationsfused_compound_typesr#   pyfunc_entriesremovecopydeepcopy	signature_specialize_function_argsr'   
specializeanalyse_declarationscreate_new_local_scopespecialize_copied_defspecialize_entryusedentriesnamereplace_fused_typechecksorig_py_funcmake_fused_cpdefpy_func)	r-   r.   argr:   fused_typespermutationscnamefused_to_specificcopied_nodes	            r2   r!   zFusedCFuncDefNode.copy_defO   s   
  *00!%E#3883D3DSXXE G001EF!BB;O$8!99??c000%%diioo6(4 	$E$--		2K*.))//*C*CK'**;+;+;=NO&*ii&;&;&F&F4E'GK# ,,S1 ''S:KL&&{E499??'8:NP ''(9(95A%)K"2=2C2CCKK))../00=+	. !II,,TYYD,IC Fs   I*I*c                    | j                   j                  j                         }| j                   j                  x| _        }d| j                   _        |r%|j
                  j                  |j                         | j                   j                  j                         }|| _	        g }|D ]  \  }}t        j                  | j                         }|j                  j                  |      }	|j                  }
|	j                  |
|       t        |j                        D ]  \  }}|
j                   |j                   k(  s |	j#                  |j                        s<|j                  |   |_        |j                  j$                  s|
j$                  |j                  _        |j                  }
|
j                  }	 n |j'                  |
       |	|_        |	|
c|
_        |	_        |
j(                  xs< | j                   j                  j*                  xs |j,                  xs |
j.                  |
_        | j                   j0                  j2                  r(| j                   j0                  j5                  |	||       |	j6                  |_        | j9                  |||       | j;                  |j0                  j<                  |       |j?                  |       |j                  rl|j
                  j                  |j                  j                         | jA                  |j                  || j                   j                  jB                  ||       | jE                  |      r n 	 |j                  jG                  | j                   j                        }||j                  ||dz    |r| jM                  ||d      | _        y|| _        y# tH        $ r |j                  jK                  |       Y Jw xY w)zf
        Create a copy of the original c(p)def function for all specialized
        versions.
        N)fused_cnamer   Fr5   )'r   r$   r9   rL   rJ   r;   r<   r#   get_fused_typesr:   r=   r>   rA   rE   	enumeratecfunc_entriesrP   same_as_resolved_type
func_cnameappendrF   defined_in_pxdis_c_class_scope
is_cmethodr(   r)   declare_optional_arg_structr'   rC   r@   r7   declare_cpdef_wrapperrD   as_variablerI   index
ValueErrorextendrK   )r-   r.   rO   rJ   rN   new_cfunc_entriesrP   rQ   rR   r$   r#   i
orig_entrycindexs                 r2   r"   zFusedCFuncDefNode.copy_cdefx   s`   
 yy~~FFH ,099+<+<<L 		%%l&8&89iinn446$/!(4 2	$E$--		2K ##../@AD%%E!!%/ "+3+<+<!= 	0:;;*"2"22t7Q7QR\RaRa7b(+(9(9!(<K%&,,777<7G7G))4'--E ::D	0 "((/#K%)5"EJ
** +))//88+..+  ** J
 yy))<<		**FF+/% G I '+&6&6K#''S:KL **;+G+G+L+L+<>
 --c2""""))+*=*=*C*CD**#++UDIIOO4O4O);8 00=e2	j	C&&,,TYY__=F 2CCfVAX.00s8= 1 ?DL (DL  	8$$%67	8s   !/O $O-,O-c                 R    g }t               }|D ]  }|j                  ||        |S )zq
        Get a list of unique basic fused types, from a list of
        (possibly) compound fused types.
        )resultseen)setrU   )r-   r:   
base_typesrj   
fused_types        r2   r8   z'FusedCFuncDefNode._get_fused_base_types   s:    
 
u. 	EJ&&jt&D	Er3   c                     |D ]u  }|j                   j                  s|j                   j                  |      |_         |j                   j                  sQ|j                   j	                  |j
                         w y r   )r$   r%   rA   is_memoryviewslicevalidate_memslice_dtyper   )r-   r7   rQ   rM   s       r2   r@   z+FusedCFuncDefNode._specialize_function_args   sX     	>Cxx  88../@A88..HH44SWW=		>r3   c                     |j                  |       ||j                  _        d|_        | j                  j                  |       y)a  
        Create a new local scope for the copied node and append it to
        self.nodes. A new local scope is needed because the arguments with the
        fused types are already in the local scope, and we need the specialized
        entries created after analyse_declarations on each specialized version
        of the (CFunc)DefNode.
        f2s is a dict mapping each fused type to its specialized version
        FN)create_local_scoper&   rQ   has_fused_argumentsr   rZ   )r-   r   r.   f2ss       r2   rC   z(FusedCFuncDefNode.create_new_local_scope   s<     	$-0* $) 

$r3   c                    | j                  |      }|D cg c]  }t        j                  ||       }}dj                  |      |_        t        j
                  ||j                  j                        |j                  _        |j                  |j                  _        |j                  |j                  _	        yc c}w )zwSpecialize the copy of a DefNode given the copied node,
        the specialization cname and the original DefNode entry|N)
r8   r   specialization_signature_stringjoinspecialized_signature_stringget_fused_cnamer#   pymethdef_cnamedoc	doc_cname)	r-   r   rP   py_entryrt   r:   rN   rm   type_stringss	            r2   rD   z'FusedCFuncDefNode.specialize_copied_def   s     001EF #.
 66z3G
 

 -0HH\,B)%/%?%?(-tzz/I/I&K

"!

'11


s   B;c                     t         j                  }t        j                  |j                        } ||       t         j                  |kD  ryy)z
        Branch-prune fused type checks like

            if fused_t is int:
                ...

        Returns whether an error was issued and whether we should stop in
        in order to prevent a flood of errors.
        FT)r
   
num_errorsr   ReplaceFusedTypeChecksr&   )r-   rR   r   	transforms       r2   rI   z*FusedCFuncDefNode.replace_fused_typechecks  sE     &&
'>>'2'>'>@	+z)r3   c                     |D ]H  }|j                   j                  |j                         |j                         |j	                  d       J y)zl
        Generate Cython code for instance checks, matching an object to
        specialized types.
        )py_type_namespecialized_type_namez
                    if isinstance(arg, {{py_type_name}}):
                        dest_sig[{{dest_sig_idx}}] = '{{specialized_type_name}}'; break
                N)contextupdater   specialization_string	put_chunk)r-   normal_typespyx_coder.   specialized_types        r2   _fused_instance_checksz(FusedCFuncDefNode._fused_instance_checks  sW    
 !- 
	##-::<&6&L&L $  
	r3   c                 Z    |j                   rd|z  S t        |      j                  dd      S )Nz	___pyx_%s _)
is_typedefstrreplacer-   dtypes     r2   _dtype_namezFusedCFuncDefNode._dtype_name&  s-    &&5z!!#s++r3   c                 R    |j                   r| j                  |      S t        |      S r   )r   r   r   r   s     r2   _dtype_typezFusedCFuncDefNode._dtype_type+  s&    ##E**5zr3   c                 D    |j                   ryd| j                  |      z  S )Nzsizeof(void *)z
sizeof(%s))is_pyobjectr   r   s     r2   _sizeof_dtypezFusedCFuncDefNode._sizeof_dtype0  s$    #$"2"25"999r3   c                    |j                  d      r2|j                  d       |j                  d       |j                          |j                  d      r2|j                  d       |j                  d       |j                          |j                  d      r2|j                  d       |j                  d       |j                          |j                  d      r3|j                  d       |j                  d	       |j                          y
y
)z?Setup some common cases to match dtypes against specializationszif kind in b'iu':pass	dtype_intzelif kind == b'f':dtype_floatzelif kind == b'c':dtype_complexzelif kind == b'O':dtype_objectN)indenterputlnnamed_insertion_pointdedentr-   r   s     r2   %_buffer_check_numpy_dtype_setup_casesz7FusedCFuncDefNode._buffer_check_numpy_dtype_setup_cases6  s    01NN6"**;7OO12NN6"**=9OO12NN6"**?;OO12NN6"**>:OO 3r3   z8dest_sig[{{dest_sig_idx}}] = '{{specialized_type_name}}'z!dest_sig[{{dest_sig_idx}}] = Nonec                    | j                  |       ||z   D ]M  }|}|j                  r|j                  }|j                  }|j                  j                  | j                  |      dz   d| j                  |      z  ||j                         |j                  |j                  f|j                  |j                  f|j                  |j                  fg}|D ]  \  }}	|s	d|j                  fz  }
|j                  r|
dz  }
|j                  r|
dz  }
|	j!                  d|
z        sP|	j#                  | j$                         |	j#                  d       |	j'                           P y	)
zO
        Match a numpy dtype object to the individual specializations.
        z == itemsizez!not (%s_is_signed ^ dtype_signed))itemsize_matchsigned_matchr   r   z3{{itemsize_match}} and (<Py_ssize_t>arg.ndim) == %dz and {{signed_match}}z and arg_is_pythran_compatiblezif %s:breakN)r   is_pythran_expr
org_bufferr   r   r   r   r   r   is_intr   is_floatr   
is_complexr   ndimr   r   matchr   )r-   r   specialized_buffer_typespythran_typesr   
final_typer   dtypesdtype_category
codewriterconds              r2   _buffer_check_numpy_dtypez+FusedCFuncDefNode._buffer_check_numpy_dtypeN  ss    	228< -.F F 	,)J//#3#>#> $**E###11%8>I@4CSCSTYCZZ&0&F&F	 $ H x112!5!56!!8#9#9:F /5 ,*
!P4D4I4ITL LD|| 77!11 @@!**8d?;"((4"((1"))+,#	,r3   c                    |j                   }|j                  rdg|j                  z  }n|j                  }t	        j
                  ||      }|j                  |       |j                  j                  |j                  |       |j                  d       |j                  j                  |j                  | j                  |             |j                  d| j                  z         y)z
        For each specialized type, try to coerce the object to a memoryview
        slice of that type. This means obtaining a buffer and parsing the
        format string.
        TODO: separate buffer acquisition from format parsing
        )directstrided)coerce_from_py_funcr   z;{{memviewslice_cname}} {{coerce_from_py_func}}(object, int))r   sizeof_dtypea  
                # try {{dtype}}
                if itemsize == -1 or itemsize == {{sizeof_dtype}}:
                    memslice = {{coerce_from_py_func}}(arg, 0)
                    if memslice.memview:
                        __PYX_XDEC_MEMVIEW(&memslice, 1)
                        # print 'found a match for the buffer through format parsing'
                        %s
                        break
                    else:
                        __pyx_PyErr_Clear()
            N)r   	is_bufferr   axesr   MemoryViewSliceTypecreate_from_py_utility_coder   r   from_py_functionr   r   r   r   r   )r-   r   	decl_coder   r.   r   r   memslice_types           r2   !_buffer_parse_format_string_checkz3FusedCFuncDefNode._buffer_parse_format_string_checku  s     !&&%%)*-=-B-BBD#((D"66udC11#6 - > > 	  	 	I	K 	"2"H"H++E2 	  	4 	 **	r3   c                 6   |j                  d|rdndz   dz   |rdndz   dz          |j                  d       |r|j                  d       |j                  d	       | j                  |||       |j	                  d       |D ]  }| j                  ||||        y
)a^  
        Generate Cython code to match objects to buffer specializations.
        First try to get a numpy dtype object and match it against the individual
        specializations. If that fails, try naively to coerce the object
        to each specialization, which obtains the buffer each time and tries
        to match the format string.
        z
                z!arg_is_pythran_compatible = False z
                if ndarray is not None:
                    if isinstance(arg, ndarray):
                        dtype = arg.dtype
                        z arg_is_pythran_compatible = TrueaC  
                    elif __pyx_memoryview_check(arg):
                        arg_base = arg.base
                        if isinstance(arg_base, ndarray):
                            dtype = arg_base.dtype
                        else:
                            dtype = None
                    else:
                        dtype = None

                    itemsize = -1
                    if dtype is not None:
                        itemsize = dtype.itemsize
                        kind = ord(dtype.kind)
                        dtype_signed = kind == 'i'
            r   aS  
                        # Pythran only supports the endianness of the current compiler
                        byteorder = dtype.byteorder
                        if byteorder == "<" and not __Pyx_Is_Little_Endian():
                            arg_is_pythran_compatible = False
                        elif byteorder == ">" and __Pyx_Is_Little_Endian():
                            arg_is_pythran_compatible = False
                        if arg_is_pythran_compatible:
                            cur_stride = itemsize
                            shape = arg.shape
                            strides = arg.strides
                            for i in range(arg.ndim-1, -1, -1):
                                if (<Py_ssize_t>strides[i]) != cur_stride:
                                    arg_is_pythran_compatible = False
                                    break
                                cur_stride *= <Py_ssize_t> shape[i]
                            else:
                                arg_is_pythran_compatible = not (arg.flags.f_contiguous and (<Py_ssize_t>arg.ndim) > 1)
                numpy_dtype_checksN)r   indentr   r   r   r   )r-   buffer_typesr   r   r   r.   r   s          r2   _buffer_checksz FusedCFuncDefNode._buffer_checks  s     	?L;RUWZ
 GTBY\^
a	, 	( 	&&';<&&x}M , 	@22i)93@	@r3   c           
         |j                  d       |j                  j                  d       |r|j                  j                  d       |j                  j                  d       t               }t               }|D ]  }|j                  }| j                  |      }	|j                  rL|	|vrH|j                  |	       |j                  d|j                         d|	d|j                         d       |j                  j                  st        |      |vs|j                  t        |             |j                  j                  |	| j                  |      	       |j                  j                  d
        y)zr
        If we have any buffer specializations, write out some variable
        declarations and imports.
        z
                ctypedef struct {{memviewslice_cname}}:
                    void *memview

                void __PYX_XDEC_MEMVIEW({{memviewslice_cname}} *, int have_gil)
                bint __pyx_memoryview_check(object)
            z
                cdef {{memviewslice_cname}} memslice
                cdef Py_ssize_t itemsize
                cdef bint dtype_signed
                cdef char kind

                itemsize = -1
            zl
                cdef bint arg_is_pythran_compatible
                cdef Py_ssize_t cur_stride
            zq
                cdef type ndarray
                ndarray = __Pyx_ImportNumPyArrayTypeIfAvailable()
            z	ctypedef r   z "")
dtype_name
dtype_typez
                            cdef bint {{dtype_name}}_is_signed
                            {{dtype_name}}_is_signed = not (<{{dtype_type}}> -1 > 0)
                        N)r   local_variable_declarationsimportsrk   r   r   r   addr   resolveempty_declaration_coder   r   r   r   r   )
r-   r   r   all_buffer_typesr   seen_typedefsseen_int_dtypesbuffer_typer   r   s
             r2   _buffer_declarationsz&FusedCFuncDefNode._buffer_declarations  s`   
 		 	,,66	 00:: < 
 	""	 %+ 	K%%E))%0J]2!%%j1OO16*161M1M1OQR   ''u:_4#''E
3$$++z7;7G7G7N , P88BB	r3   c                    t        j                  |j                        }|j                          t	               }g g g }}}d}|D ]  }|j                         }	|	r0|	|v r|j                  |	       |	dk(  rd}3|j                  |       E|j                  r|j                  |       c|j                  s|j                  s||j                  |        ||||fS )zV
        Specialize fused types and split into normal types and buffer types.
        FobjectT)r   get_specialized_typesr$   sortrk   r   r   rZ   r   r   ro   )
r-   rM   specialized_typesseen_py_type_namesr   r   r   has_object_fallbackr   r   s
             r2   _split_fused_typesz$FusedCFuncDefNode._split_fused_types  s     '<<SXXF 	  U46BMl# 1 	6+88:L#55"&&|48+*.' ''(89!11$$%56!++/?/R/R##$45	6 \=:MMMr3   c                 &    |j                  d       y )Na  
                # PROCESSING ARGUMENT {{arg_tuple_idx}}
                if {{arg_tuple_idx}} < len(<tuple>args):
                    arg = (<tuple>args)[{{arg_tuple_idx}}]
                elif kwargs is not None and '{{arg.name}}' in <dict>kwargs:
                    arg = (<dict>kwargs)['{{arg.name}}']
                else:
                {{if arg.default}}
                    arg = (<tuple>defaults)[{{default_idx}}]
                {{else}}
                    {{if arg_tuple_idx < min_positional_args}}
                        raise TypeError("Expected at least %d argument%s, got %d" % (
                            {{min_positional_args}}, {{'"s"' if min_positional_args != 1 else '""'}}, len(<tuple>args)))
                    {{else}}
                        raise TypeError("Missing keyword-only argument: '%s'" % "{{arg.default}}")
                    {{endif}}
                {{endif}}
            )r   r   s     r2   _unpack_argumentz"FusedCFuncDefNode._unpack_argument2  s    	r3   c                 
   ddl m}m}m} | j	                  | j
                  j                  D cg c]%  }|j                  j                  s|j                  ' c}      }t        j                  | j
                  j                  t        |      |r-| j
                  j                  | j
                  j                  z
  n%t        d | j
                  j                  D              |j                  j                   d}	|j#                  |	      }
|j#                  |	      }|j%                  d       |j'                          |
j%                  d       |
j'                          |
j)                  d       |
j)                  d	       |
j)                  d
       d}d}t+               }t-               }t/        | j
                  j                        D ]  \  }}|j                  j                  r8|j                  j1                         }t        |      dkD  rt3        d      |d   }|j                  j                  r`|vr[|j5                  |       |	j7                  ||||       | j9                  |      \  }}}}| j;                  |
       |
j=                  d      r|r| j?                  ||
|       |s|r@|jA                  |j                  jC                  dd             | jE                  |||
||       |r8|
jF                  j7                  d       |
jI                  | jJ                         n|
jI                  | jL                         |
jI                  d       |
jO                          |dz  }|j7                  |       |j7                  d |D               |jP                  s|dz  } |rj| jS                  |
||       |jA                  |j                  jC                  dd             |jA                  |j                  jC                  dd             |
j%                  d       |
jU                         }ddl+m,} |j                  |d |       g      } |j[                  | j
                  j\                        |j^                        }|ja                  |jU                         |jc                                ||_2        |jg                  |       |jh                  d   }|jd                  | _5        tm        | j
                  tn              r| jp                  dd |_9        |S | jp                  D cg c]  }|jt                   c}|_9        |S c c}w c c}w )a!  
        This creates the function that is indexable from Python and does
        runtime dispatch based on the argument types. The function gets the
        arg tuple and kwargs dict (or None) and the defaults tuple
        as arguments from the Binding Fused Function's tp_call.
        r   )TreeFragmentCodeUtilityCodec              3   :   K   | ]  }|j                   d  y w)Nr   )default).0rM   s     r2   	<genexpr>z5FusedCFuncDefNode.make_fused_cpdef.<locals>.<genexpr>Z  s     G#3;;3FAGs   )memviewslice_cname	func_argsn_fusedmin_positional_argsrH   )r   z
                cdef extern from *:
                    void __pyx_PyErr_Clear "PyErr_Clear" ()
                    type __Pyx_ImportNumPyArrayTypeIfAvailable()
                    int __Pyx_Is_Little_Endian()
            a  
                def __pyx_fused_cpdef(signatures, args, kwargs, defaults):
                    # FIXME: use a typed signature - currently fails badly because
                    #        default arguments inherit the types we specify here!

                    dest_sig = [None] * {{n_fused}}

                    if kwargs is not None and not kwargs:
                        kwargs = None

                    cdef Py_ssize_t i

                    # instance check body
            r   	func_defsr   r   zODetermination of more than one fused base type per argument is not implemented.)arg_tuple_idxrM   dest_sig_idxdefault_idxzwhile 1:IsLittleEndianzModuleSetupCode.cr   )r   r   c              3   4   K   | ]  }|j                     y wr   )r   )r   tys     r2   r   z5FusedCFuncDefNode.make_fused_cpdef.<locals>.<genexpr>  s     'N"'Ns   ImportzImportExport.cImportNumPyArraya  
                candidates = []
                for sig in <dict>signatures:
                    match_found = False
                    src_sig = sig.strip('()').split('|')
                    for i in range(len(dest_sig)):
                        dst_type = dest_sig[i]
                        if dst_type is not None:
                            if src_sig[i] == dst_type:
                                match_found = True
                            else:
                                match_found = False
                                break

                    if match_found:
                        candidates.append(sig)

                if not candidates:
                    raise TypeError("No matching signature found")
                elif len(candidates) > 1:
                    raise TypeError("Function call with ambiguous argument types")
                else:
                    return (<dict>signatures)[candidates[0]]
            )ConstantFoldingmodule)levelpipelineN);r   r   r   r   r8   r   r7   r$   r%   r   r   lennum_required_argsnum_required_kw_argssumr#   rH   PyxCodeWriterr   r   r   r   rk   rV   rU   NotImplementedErrorr   r   r   r   r   r   use_utility_codeload_cachedr   r   r   r   no_matchr   r   r   getvalueOptimizer   SetPosTransformr   rootdeclare_declarations_in_scopeglobal_scopescoperB   r,   fragment_scoper    r   r   specialized_cpdefsrL   )r-   rJ   r.   r/   r   r   r   rM   rN   r   r   r   fused_indexr   r   seen_fused_typesre   arg_fused_typesrm   r   r   r   r   fragment_coder   fragmentastrL   r0   s                                r2   rK   z"FusedCFuncDefNode.make_fused_cpdefG  s    	6500 $		2E#((2C2CCHH2E F #-"?"?;'  		++dii.L.LLGGG &&++	
 %%g%6&&w&7		 		  	&&y1&&{3&&'DE%<5		/ )	!FAsxx  "%((":":"<'!+- /V W W,Q/
xx  Z7G%G $$Z0"#!, +	   RVQhQhilQmNlM;N%%h/ $$Z0#33L(CP#},,T-=-=-I-IJZ\o-pq++L-S\^ab* ((//h/O tzz2 t}}5NN7+OO%q  ''5 '''N'NN{{q S)	!V %%h	;K][  !1!1!=!=hHX!YZ  !1!1!=!=>PRb!cd	4 !))+ 	.,,_5F4G - I9l**499==9(--H11 #"2"2"4	6	  %))B-!iidii))-AG&  >BZZ)H!)))HG&a2E\ *Is   U
U1Uc           	         d}| j                   j                  }|D ]-  }t        ||t        | j                  j                  |             / | j                  j
                  | j                   _        | j                  j                  | j                   _        |j                  j                  dd        t        | j                  t              r||j                  |j
                  <   n||j                  |j
                     _        |j                  j                  |       | | j                   j                  _        | j                   D ]Z  }t        | j                  t              r| j                   |_        /| j                   |j                   _        ||j                  _        \ | j%                          | j&                  j                  | j(                         y )N)
rH   r   rP   rY   pyfunc_cnamer{   r|   r}   	is_memberr  __pyx_fused_cpdef)rL   r#   setattrgetattrrJ   rH   r|   rG   popr    r   r   r`   r;   rZ   r+   r   fused_py_funcsynthesize_defnodesr,   r   )r-   r.   copy_attributesr#   attrr   s         r2   update_fused_defnode_entryz,FusedCFuncDefNode.update_fused_defnode_entry  sZ   
 ""# 	<DE4D--33T:<	< !--22,,00+T2dii)&+CKK

#27CKK

#/!!%(-1*JJ 	/D$))W-%)\\"-1\\*).

&	/ 	  "

$--.r3   c                    | j                   D ]F  }|j                         D ]1  }|j                  D ]   }|j                  s|j	                  |       " 3 H | j
                  r| j                  j                  |      | _        | j
                  j                  |      | _        | j                  j                  |      | _        | j                  j                  |      | _	        g x| _
        }| j                  j                  D ]d  }|j                  rE|j                  j                  |      |_        |j                  t        |j                               T|j                  d       f t!        | j"                        D ]  \  }}|j                  |      x}| j"                  |<   t%        |t&              s7t)        |j                  |      D ]2  \  }}	|		t+        |	      j-                  |j.                  |      |_        4  | j
                  rd|D 	cg c]  }	|	st+        |	       }
}	t1        | j2                  |
      | _        | j4                  j7                  |d      j9                  |      | _        t        | j4                        | _        t        | j:                  d   j<                        | _        | j                  j>                  }t+        | j4                        |_        t+        | j<                        |_        t!        | j:                        D ]Y  \  }}t+        | j<                        |_        |j7                  |      x}| j:                  |<   t+        | j4                        |_        [ | S c c}	w )z
        Analyse the expressions. Take care to only evaluate default arguments
        once and clone the result for all specializations
        N)r7   T)skip_childrenr   ) r:   rU   typesr   create_declaration_utility_coderL   r   analyse_expressionsr   r   defaultsr   r7   r   rZ   r   rV   r,   r    r   zipr   	coerce_tor$   r   r   defaults_tupleanalyse_typescoerce_to_pyobjectspecialized_pycfuncscode_objectrM   )r-   r.   fused_compound_typerm   specialization_typer*  rM   re   statr   r7   
fused_funcpycfuncs                r2   r)  z%FusedCFuncDefNode.analyse_expressions  s   
 $(#<#< 	Q1AAC Q
+5+;+; Q'*55+KKCPQQ	Q <<"&"5"5"I"I#"ND<<;;C@DL,0,I,I,],]^a,bD))-)C)C)W)WX[)\D&#%%99>> 	&C{{!kk==cB	#++ 67%	& !, 	RGAt#'#;#;C#@@D4::a=$,$'		8$< RLC*&/&8&B&B388S&QR	R <<6>J7'Ig&JDJ"+DHH4"@D"&"5"5"C"CCW["C"\"o"ops"tD"+D,?,?"@D()B)B1)E)Q)QRD66::J(1$2E2E(FJ%%.t/?/?%@J"'(A(AB H
7&/0@0@&A#9@9N9Ns9SS$33A6)243F3F)G&H  Ks   M4M4c                    t        | j                  d   t              r$| j                  D cg c]  }|j                   }}n| j                  }|D cg c]!  }t	        j
                  |j                        # }}t        ||      D cg c]&  \  }}t        j                  |j                  |      ( }}}|D cg c]#  }t        j                  j                  |d      % }}t        j                  j                  | j                  t        ||            | _        || _        |D ]	  }d|_         yc c}w c c}w c c}}w c c}w )zT
        Create the __signatures__ dict of PyCFunctionNode specializations.
        r   )valueT)bindingN)r    r   r   rL   r	   EncodedStringry   r+  r   
StringNoder   PyCFunctionNodefrom_defnodeDictNode
from_pairsr   r0  is_specialization)r-   r   r   
signaturessigkeysvaluespycfuncnodes           r2   r!  z%FusedCFuncDefNode.synthesize_defnodes7  s6    djjm\2.2jj9dT\\9E9JJE #() %2243T3TU )
 ) "%UJ!79D# $$TXXS9 9 9 $% ++88t8L % % (00;;DHHc$PVFWX$*!! 	1K,0K)	1 :)9%s   D4&D9	+D>;(Ec                 B   | j                   r-d| j                   _        | j                  j                  ||       | j                  D ]W  }t        |t              s|j                  j                  s+|j                  |j                         |j                  ||       Y y )NT)rL   pymethdef_requiredr   generate_function_definitionsr,   r    r   r#   rF   mark_posr   )r-   r.   coder4  s       r2   rH  z/FusedCFuncDefNode.generate_function_definitionsM  sr    <<.2DLL+&&DDS$OJJ 	>D$,dhh'223=	>r3   c                 (   | j                   D ]  }||j                  |        | j                  r6| j                  j                  |       | j                  j                  |       | j
                  D ]Z  }|j                  |j                         t        |t        j                        r|j                  |       J|j                  |       \ | j                  r| j                  j                  |       |j                  d| j                  j                         d| j                  j                         d       |j!                  | j                  j                                | j                  j#                  |       | j                  j%                  |       | j&                  j                  |       | j                  j)                  |       | j                  j%                  |       | j                  j)                  |       | j                  j%                  |       | j                  j)                  |       | j                  j%                  |       | j                   D ]'  }||j)                  |       |j%                  |       ) y )Nz((__pyx_FusedFunctionObject *) z)->__signatures__ = ;)r*  generate_evaluation_coderL   r-  r1  r,   rI  r   r    r   ExprNodegenerate_execution_coder   r   r   ri   put_giverefgenerate_post_assignment_code
free_tempsr   generate_disposal_code)r-   rJ  r   r4  s       r2   rO  z)FusedCFuncDefNode.generate_execution_codeW  s    }} 	7G"006	7 <<88>55d;JJ 	3DMM$((#$	 2 23--d3,,T2	3 ))BB4HJJ%)%B%B%I%I%K%)%8%8%?%?%ACD T00779:==dC**40&&>>tD ))@@F))44T:66t<**4033D9''-}} 	)G"..t4""4(	)r3   c                 H    | j                   D ]  }|j                  |        y r   )r,   annotate)r-   rJ  r4  s      r2   rU  zFusedCFuncDefNode.annotate  s!    JJ 	 DMM$	 r3   )(__name__
__module____qualname____doc__r   r   r   r-  
decoratorsr   child_attrsr   r!   r"   r8   r@   rC   rD   rI   r   r   r   r   r   r   r	  r   r   r   r   r   r   rK   r$  r)  r!  rH  rO  rU  __classcell__)r1   s   @r2   r   r      s    8 N# NJ** .O OK#:'JRT(l	> "2 (",

:, GE2H%,N&P;@z9vN:*Yv!/F0d1,>*)X r3   r   )
__future__r   r=   r   r   r   r   r   r	   r
   r   r   r   Nodesr   r   r   r   Utilsr   r    r3   r2   <module>ra     s2    & < < 6 6 C C y  y r3   