B
    \W             0   @   s  d dl mZ d dlZejeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeejd" d dlZd dlZd dlZd dl	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 ddlmZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlmZmZm Z m!Z!m"Z" ddlm#Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ ddlm$Z$ ddlm0Z0 ddlm1Z1 ddl2m3Z3 ddlm4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z: ddl;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJ ddlmKZK yd dlLmMZM W n" eNk
r:   eOZMePeOfZQY n
X ePeRfZQejSd  dkr^dZTeUZVndZTeUeWfZVG dd deZXeX ZYe ZZe*e+fde*e,fd e*ej[fd!e*ej\fd!e*ej]fd!e*ej^fd!e,e*fd"e,e+fd#e,e.fd$e,ej_fd%e,ej`fd%e.e,fd&e+e*fd'e+e,fd(e+ej[fd)e+ej\fd)e+ej]fd)e+ej^fd)e+ej_fd*e+ej`fd*ej[e*fd+ej\e*fd+ej]e*fd+ej^e*fd+iZad,d- Zbd.d/ Zcd0d1 Zddd2d3Zed4d5 Zfd6d7 Zgd8d9 Zhd:d; Zid<d= ZjG d>d? d?eZkG d@dA dAekZlG dBdC dCelZmG dDdE dEemZnG dFdG dGemZoG dHdI dIelZpG dJdK dKepZqG dLdM dMepZrG dNdO dOepZsG dPdQ dQepZtG dRdS dSepZudTdU ZvG dVdW dWepZwG dXdY dYepZxG dZd[ d[emZyG d\d] d]eyZzG d^d_ d_elZ{G d`da daelZ|G dbdc dcelZ}G ddde deekZ~G dfdg dgekZG dhdi diekZG djdk dkelZG dldm dmekZG dndo doelZG dpdq dqekZG drds dselZG dtdu duekZG dvdw dweZG dxdy dyekZG dzd{ d{ekZG d|d} d}ekZG d~d delZG dd delZG dd dekZG dd deZG dd deZG dd deZG dd deZG dd dekZG dd deZG dd deZG dd dekZG dd dekZG dd deZG dd dekZG dd deZG dd deZG dd deZG dd deZG dd dekZG dd deZG dd deZG dd deZG dd dekZG dd dekZG dd dekZG dd dekZG dd dekZG dd deZG dd deZG dd dekZG dd deZG dd deZG dd deZG dd dekZG ddÄ dekZG ddń dekZG ddǄ dekZG ddɄ dekZG dd˄ dekZG dd̈́ deZG ddτ dekeZG ddф dekZG ddӄ dekZG ddՄ dekeZG ddׄ dekZG ddل dekZG ddۄ dekeZG dd݄ deZG dd߄ dekZG dd dekZG dd dekZG dd deZG dd deZG dd deZG dd deZG dd dekZG dd deZG dd deÃZG dd deÃZG dd deŃZG dd delZG dd deZG dd deZG dd delZdd Zejejejejϐd ZG dd dekZG dd deуZG dd deуZG dd deуZG d	d
 d
eуZG dd deуZG dd deփZG dd deփZؐdd ZG dd deփZeeeՐdZېdd ZG dd dekZݐdZސdZߐdZdZdZG dd  d ekZG d!d" d"ekZG d#d$ d$eZG d%d& d&eZG d'd( d(ekZG d)d* d*ekZy
ejZW n" ek
r   d+d, ZY nX ejejejejejejejejejejejejejejejejejejejejed-d. d/d. d0Z d1d2 ZG d3d4 d4ekZG d5d6 d6eZd7d8 ZG d9d: d:eZG d;d< d<eZG d=d> d>eZG d?d@ d@eZG dAdB dBeZ	G dCdD dDeZ
G dEdF dFeZedGjZedHdIdJdKdLdMdNdOdPdQdRdSdTdUdVgZG dWdX dXeZG dYdZ dZeZG d[d\ d\ekZG d]d^ d^ekZG d_d` d`ekZdadbdcdddddedfdcdddg	ZG dhdi dieZG djdk dkekeZG dldm dmeeZeeeeeeeeee	e
eeeednZddodpZG dqdr drekZG dsdt dteZG dudv dveZG dwdx dxeZG dydz dzeZ G d{d| d|eZ!G d}d~ d~e!Z"G dd deZ#G dd deZ$G dd deZ%G dd deZ&G dd deZ'G dd deZ(G dd de(Z)G dd dekZ*G dd dekZ+edddZ,edddZ-edddZ.edde-gdZ/e0ddZ1e0ddZ2e0ddZ3dS (      )absolute_importN)"errorwarningZ	warn_onceInternalErrorCompileErrorUtilityCodeTempitaUtilityCodeStringEncodingoperatorlocal_errorsreport_errorNamingNodes
PyrexTypespy_object_type	list_type
tuple_typeset_type	dict_typeunicode_typestr_type
bytes_type	type_typeBuiltinSymtabUtilsfind_coercion_errordebug_disposal_codedebug_temp_allocdebug_coercionbytearray_type
slice_type_py_int_types
IS_PYTHON3   )r   r   r   r   r   r   )r   r   )r	   )r   )r   )Nodeutility_code_for_importsanalyse_type_annotation)r   )r   c_long_typetypecast
error_typeunspecified_type)	TypeSlots)r   r   r   r   r   r   r   r   r    basestring_typer!   )r   )r      )r   )AnnotationItem)Future)print_call_chain)r   r   r   )
to_pythranis_pythran_supported_type#is_pythran_supported_operation_typeis_pythran_exprpythran_func_typepythran_binop_typepythran_unaryop_typehas_np_pythranpythran_indexing_codepythran_indexing_type!is_pythran_supported_node_or_nonepythran_typepythran_is_numpy_func_supportedpythran_get_func_include_filepythran_functor)PythranExpr)
basestring   TFc                   s(   e Zd ZdZ fddZdd Z  ZS )NotConstantNc                s"   t jd krtt | | t _t jS )N)rD   _objsuper__new__)cls)	__class__ 8lib/python3.7/site-packages/Cython/Compiler/ExprNodes.pyrG   J   s    
zNotConstant.__new__c             C   s   dS )Nz<NOT CONSTANT>rJ   )selfrJ   rJ   rK   __repr__P   s    zNotConstant.__repr__)__name__
__module____qualname__rE   rG   rM   __classcell__rJ   rJ   )rI   rK   rD   G   s   rD   zgCannot convert Unicode string to 'str' implicitly. This is not portable and requires explicit encoding.zGCannot convert Unicode string to 'bytes' implicitly, encoding required.z5Unicode objects only support coercion to Py_UNICODE*.zFCannot convert 'bytes' object to unicode implicitly, decoding requiredzMCannot convert 'bytes' object to str implicitly. This is not portable to Py3.zTCannot convert 'bytes' object to basestring implicitly. This is not portable to Py3.z<Cannot convert 'bytes' object to Py_UNICODE*, use 'unicode'.zMCannot convert 'basestring' object to bytes implicitly. This is not portable.zZstr objects do not support coercion to unicode, use a unicode string literal instead (u'')zACannot convert 'str' to 'bytes' implicitly. This is not portable.z@'str' objects do not support coercion to C types (use 'bytes'?).zB'str' objects do not support coercion to C types (use 'unicode'?).z?Cannot convert 'char*' to unicode implicitly, decoding requiredc                s~   t  }|d kr|S |jd rvt fddtjtjtjtjfD rv d j	rT|S |jd dkrf|S d d j
 S n|S d S )Nc_string_encodingc             3   s   | ]}| kV  qd S )NrJ   ).0t)
type_tuplerJ   rK   	<genexpr>   s    z&find_coercion_error.<locals>.<genexpr>r$   )asciidefaultz_'%s' objects do not support coercion to C types with non-ascii or non-default c_string_encodingr   )coercion_error_dictget
directivesanyr   c_char_ptr_typec_uchar_ptr_typec_const_char_ptr_typec_const_uchar_ptr_typeis_pyobjectname)rU   rX   enverrrJ   )rU   rK   r   z   s    


r   c             C   s   t tttd| jd S )N)bytes	bytearraystrunicodeZc_string_type)r   r    r   r   rZ   r[   )rc   rJ   rJ   rK   default_str_type   s
    ri   c              G   sN   xH| D ]@}|dkst |jts,t |jts,q|jdk rt|jddd qW dS )z
    Raise a warning on nodes that are known to have negative numeric values.
    Used to find (potential) bugs inside of "wraparound=False" sections.
    Nr   zfthe result of using negative indices inside of code sections marked as 'wraparound=False' is undefinedr$   )level)
isinstanceconstant_resultr"   floatr   pos)nodesnoderJ   rJ   rK   check_negative_indices   s    

rq   c          
      s   |j sZ|d kr| }|tkrZ|jrZt|jdkrZy|jd j}W n tk
rX   Y nX |d k	r|j r|d k	r| ry|j|j	 }W n t
ttfk
r   Y nX | S t fdd|jD }t|dkr| S d S )Nr$   r   c                s   g | ]}|  qS rJ   )
infer_type)rS   item)rc   rJ   rK   
<listcomp>   s    z,infer_sequence_item_type.<locals>.<listcomp>)is_sequence_constructorrr   r   cf_statelenrhsAttributeErrorhas_constant_resultargsrl   
ValueError	TypeError
IndexErrorsetpop)rc   Zseq_node
index_nodeseq_typers   
item_typesrJ   )rc   rK   infer_sequence_item_type   s(    

r   c             C   s&   dd |D }d|krdS | t |fS )a  
    Recursively generate a deduplication key from a sequence of values.
    Includes Cython node types to work around the fact that (1, 2.0) == (1.0, 2), for example.

    @param outer_type: The type of the outer container.
    @param item_nodes: A sequence of constant nodes that will be traversed recursively.
    @return: A tuple that can be used as a dict key for deduplication.
    c             S   sx   g | ]p}|d krt d fnZ|jr>t|j|jr0|jnd g|j n4|jr\t|j|j|j	|j
fn| rp|j|jfnd qS )N)r   ru   make_dedup_keytype
is_literalmult_factorr{   is_slicestartstopsteprz   rl   )rS   rp   rJ   rJ   rK   rt      s   z"make_dedup_key.<locals>.<listcomp>N)tuple)Z
outer_typeZ
item_nodesZ	item_keysrJ   rJ   rK   r      s
    
r   c             C   sZ   | d krdS | j tjkr&| jdkr&dS | j jrFd| jj| jjf dfS d| jj dfS d S )N)z__Pyx_CppExn2PyErr();F*)z__Pyx_CppExn2PyErr();Tzttry { throw; } catch(const std::exception& exn) {PyErr_SetString(%s, exn.what());} catch(...) { PyErr_SetNone(%s); }Fzc%s(); if (!PyErr_Occurred())PyErr_SetString(PyExc_RuntimeError, "Error converting c++ exception.");)r   r   c_char_typevaluera   entrycname)exception_valuerJ   rJ   rK   get_exception_handler   s    

r   c             C   s2   |r.|r|  | d| n|  | d| d S )Nz__Pyx_ErrOccurredWithGIL()zPyErr_Occurred())putlnerror_goto_if)codecheck_py_exceptionrn   nogilrJ   rJ   rK   maybe_check_py_error   s    r   c             C   s   t |\}}| d | d|  |r:| | || t| ||| | d |rb| jdd | | |rx|   | | | | d d S )Nztry {z%sz} catch(...) {T)declare_gilstate})r   r   error_goto_if_nullr   put_ensure_gilput_release_ensured_gil
error_goto)r   rn   Zinside	py_resultr   r   Zraise_py_exceptionr   rJ   rJ   rK   translate_cpp_exception   s    


r   c             C   s  t |\}}	t |\}
}| d | |d|  t| |	|| | d | d|  t| ||| | d |r| jdd | |
 |r|   | | | | d | d |r| jdd | | |r|   | | | | d d S )Nztry {z__pyx_local_lvalue = %s;z} catch(...) {T)r   r   )r   r   declaration_coder   r   r   r   )r   rn   lhs_typeZlhs_coderhs_codeZlhs_exc_valZassign_exc_valr   Zhandle_lhs_excZlhc_check_py_excZhandle_assignment_excZassignment_check_py_excrJ   rJ   rK   translate_double_cpp_exception  s0    






r   c               @   s  e 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eZeeddZ dd Z!dd	 Z"d
d Z#dd Z$dd Z%dd Z&dddZ'dd Z(dddZ)dd Z*dd Z+dd Z,dd Z-d d! Z.d"d# Z/d$d% Z0d&d' Z1d(d) Z2d*d+ Z3d,d- Z4d.d/ Z5d0d1 Z6d2d3 Z7d4d5 Z8d6d7 Z9dd9d:Z:d;d< Z;d=d> Z<d?d@ Z=dAdB Z>dCdD Z?dEdF Z@dGdH ZAdIdJ ZBdKdL ZCdMdN ZDdOdP ZEdQdR ZFdSdT ZGdUdV ZHdWdX ZIdYdZ ZJd[d\ ZKd]d^ ZLd_d` ZMdadb ZNdcdd ZOdedf ZPdgdh ZQdidj ZRddkdlZSddmdnZTdodp ZUdqdr ZVdsdt ZWdudv ZXdwdx ZYdydz ZZdd{d|Z[d}d~ Z\dd Z]dd Z^dd Z_dd Z`dd Zadd Zbdd ZcdddZdeedd ZfdS )ExprNodeNTFsubexprs)fgetc             C   s"   t |d td| jj|f d S )Nznot implementedz%s.%s not implemented)r1   r   rI   rN   )rL   method_namerJ   rJ   rK   not_implemented  s    
zExprNode.not_implementedc             C   s   dS )Nr   rJ   )rL   rJ   rJ   rK   	is_lvalue  s    zExprNode.is_lvaluec             C   s   |   o| jj S )N)r   r   is_memoryviewslice)rL   rJ   rJ   rK   is_addressable  s    zExprNode.is_addressablec             C   s   | j jo| jS )N)r   ra   is_temp)rL   rJ   rJ   rK   is_ephemeral  s    zExprNode.is_ephemeralc             C   sL   g }xB| j D ]8}t| |}|d k	rt|tkr:|| q|| qW |S )N)r   getattrr   listextendappend)rL   ro   rb   rs   rJ   rJ   rK   subexpr_nodes  s    
zExprNode.subexpr_nodesc             C   s   | j r| jS |  S d S )N)r   	temp_codecalculate_result_code)rL   rJ   rJ   rK   result  s    zExprNode.resultc             C   s&   t | rt| S |d k	stt| |S )N)r<   r2   AssertionError)rL   Ztype_rJ   rJ   rK   pythran_result  s    zExprNode.pythran_resultc             C   s   dS )zZ
        Subtypes may return False here if result temp allocation can be skipped.
        TrJ   )rL   rJ   rJ   rK   is_c_result_required  s    zExprNode.is_c_result_requiredc             C   s:   | j r&| jjr&|tkr&t|t|  S t||  |  S )N)r   r   ra   r   r)   r   ctype)rL   r   rJ   rJ   rK   	result_as   s    zExprNode.result_asc             C   s
   |  tS )N)r   r   )rL   rJ   rJ   rK   r   	  s    zExprNode.py_resultc             C   s   | j p
| jS )N)result_ctyper   )rL   rJ   rJ   rK   r     s    zExprNode.ctypec             C   s   d S )NrJ   )rL   rJ   rJ   rK   get_constant_c_result_code  s    	z#ExprNode.get_constant_c_result_codec             C   s   d S )NrJ   )rL   rJ   rJ   rK   calculate_constant_result  s    
z"ExprNode.calculate_constant_resultc             C   s   | j tk	o| j tk	S )N)rl   constant_value_not_setnot_a_constant)rL   rJ   rJ   rK   rz   )  s    
zExprNode.has_constant_resultc             C   s   t | jd d S )NzInvalid compile-time expression)r   rn   )rL   denvrJ   rJ   rK   compile_time_value-  s    zExprNode.compile_time_valuec             C   s   t | jd|jj|f  d S )Nz(Error in compile-time expression: %s: %s)r   rn   rI   rN   )rL   erJ   rJ   rK   compile_time_value_error1  s    z!ExprNode.compile_time_value_errorc             C   s   t | jd d S )NzCannot assign to or delete this)r   rn   )rL   rc   rJ   rJ   rK   analyse_target_declaration7  s    z#ExprNode.analyse_target_declarationc             C   s   |  |}|  |S )N)analyse_typescheck_const)rL   rc   rp   rJ   rJ   rK   analyse_const_expression<  s    
z!ExprNode.analyse_const_expressionc             C   s
   |  |S )N)r   )rL   rc   rJ   rJ   rK   analyse_expressionsE  s    zExprNode.analyse_expressionsc             C   s
   |  |S )N)analyse_target_types)rL   rc   rx   rJ   rJ   rK   analyse_target_expressionK  s    z"ExprNode.analyse_target_expressionc             C   s   |  |}||}|S )N)r   coerce_to_boolean)rL   rc   rp   boolrJ   rJ   rK   analyse_boolean_expressionQ  s    

z#ExprNode.analyse_boolean_expressionc             C   s   |  |}|||S )N)r   r   coerce_to_simple)rL   rc   rp   rJ   rJ   rK   analyse_temp_boolean_expressionW  s    
z(ExprNode.analyse_temp_boolean_expressionc                s4   t | dr| jd k	rdS t fdd|  D dS )Nr   rJ   c                s   g | ]}|  qS rJ   )type_dependencies)rS   rp   )rc   rJ   rK   rt   i  s    z.ExprNode.type_dependencies.<locals>.<listcomp>)hasattrr   sumr   )rL   rc   rJ   )rc   rK   r   d  s    zExprNode.type_dependenciesc             C   sD   t | dr| jd k	r| jS t | dr6| jd k	r6| jjS | d d S )Nr   r   rr   )r   r   r   r   )rL   rc   rJ   rJ   rK   rr   k  s
    zExprNode.infer_typec             C   s   | j p| jp| jjp| jjS )N)r   r   r   is_arrayis_cfunction)rL   rJ   rJ   rK   nonlocally_immutablew  s    zExprNode.nonlocally_immutabler   c          	   C   s$   t | j| t| jt||tjddS )z
        Return a node that represents the (type) result of an indexing operation,
        e.g. for tuple unpacking or iteration.
        )r   rl   r   )baseindex)	IndexNodern   IntNoderg   r   c_py_ssize_t_type)rL   r   rJ   rJ   rK   inferable_item_node|  s    
zExprNode.inferable_item_nodec             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   analyse_as_module  s    zExprNode.analyse_as_modulec             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   analyse_as_type  s    zExprNode.analyse_as_typec             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   analyse_as_extension_type  s    z"ExprNode.analyse_as_extension_typec             C   s   |  d d S )Nr   )r   )rL   rc   rJ   rJ   rK   r     s    zExprNode.analyse_typesc             C   s
   |  |S )N)r   )rL   rc   rJ   rJ   rK   r     s    zExprNode.analyse_target_typesc             C   s   | j r| j jr|   d S )N)r   ra   	gil_error)rL   rc   rJ   rJ   rK   nogil_check  s    zExprNode.nogil_checkc             C   s   |j r| jjrt| jd d S )Nz3Assignment of Python object not allowed without gil)r   r   ra   r   rn   )rL   rc   rJ   rJ   rK   gil_assignment_check  s    zExprNode.gil_assignment_checkc             C   s   |    dS )NF)	not_const)rL   rJ   rJ   rK   r     s    zExprNode.check_constc             C   s   t | jd d S )Nz$Not allowed in a constant expression)r   rn   )rL   rJ   rJ   rK   r     s    zExprNode.not_constc             C   s   |    dS )NF)addr_not_const)rL   rJ   rJ   rK   check_const_addr  s    zExprNode.check_const_addrc             C   s   t | jd d S )NzAddress is not constant)r   rn   )rL   rJ   rJ   rK   r     s    zExprNode.addr_not_constc             C   s   | j S )N)r   )rL   rJ   rJ   rK   result_in_temp  s    zExprNode.result_in_tempc             C   s   |   S )N)r   )rL   rJ   rJ   rK   target_code  s    zExprNode.target_codec             C   s   |  d d S )Nr   )r   )rL   rJ   rJ   rK   r     s    zExprNode.calculate_result_codec             C   st   | j rtd| jj| jf | j}|jsj|jr6tj	}n| j
sT|jsT|  sTd | _ d S |jj|| jd| _ nd | _ d S )Nz'Temp allocated multiple times in %r: %r)
manage_ref)r   RuntimeErrorrI   rN   rn   r   is_voidra   r   r   result_is_usedr   r   	funcstateallocate_tempuse_managed_ref)rL   r   r   rJ   rJ   rK   allocate_temp_result  s    zExprNode.allocate_temp_resultc             C   s   | j st| jsd S | jr<tj| jd  f| jdd   nd}| jr`td| j| j	j
|f ntd| j	j
|f |j| j  | j | _d | _ d S )Nr   r$   z(?)z+temp %s released multiple times in %s at %rz*no temp, but release requested in %s at %r)r   r   rn   ospathbasenameZget_descriptionold_tempr   rI   rN   r   release_temp)rL   r   rn   rJ   rJ   rK   release_temp_result  s    0zExprNode.release_temp_resultc             C   s(   | j jr$|  s$||  |   dS )z
        If result is a pyobject, make sure we own a reference to it.
        If the result is in a temp, it is already a new reference.
        N)r   ra   r   
put_increfr   r   )rL   r   rJ   rJ   rK   make_owned_reference  s    zExprNode.make_owned_referencec             C   s    |   s|j|  | jd dS )zJ
        Make sure we own the reference to this memoryview slice.
        )have_gilN)r   put_incref_memoryviewslicer   in_nogil_context)rL   r   rJ   rJ   rK   make_owned_memoryviewslice  s    
z#ExprNode.make_owned_memoryviewslicec             C   s^   |  | || j | jr&| | | | | jrZ| jjsZ| jjsZ| 	| | 
| d S )N) generate_subexpr_evaluation_codemark_posrn   r   r   generate_result_coder   	is_stringis_pyunicode_ptrgenerate_subexpr_disposal_codefree_subexpr_temps)rL   r   rJ   rJ   rK   generate_evaluation_code  s    



z!ExprNode.generate_evaluation_codec             C   s    x|   D ]}|| q
W d S )N)r   r  )rL   r   rp   rJ   rJ   rK   r   
  s    z)ExprNode.generate_subexpr_evaluation_codec             C   s   |  d d S )Nr   )r   )rL   r   rJ   rJ   rK   r     s    zExprNode.generate_result_codec             C   s   | j r| jjs| jjr*| | | | |  r| jjrP||  | 	  q| jj
r|j|  | j d |d|    |d|    n
| | d S )N)r   z%s.memview = NULL;z%s.data = NULL;)r   r   r  r  r  r  r   ra   put_decref_clearr   r   put_xdecref_memoryviewslicer   r   )rL   r   rJ   rJ   rK   generate_disposal_code  s    

zExprNode.generate_disposal_codec             C   s    x|   D ]}|| q
W d S )N)r   r  )rL   r   rp   rJ   rJ   rK   r  #  s    z'ExprNode.generate_subexpr_disposal_codec             C   s   | j rv| jjs| jjr,| | | | q| jjrH|d|    q| jj	r|d|    |d|    n
| | d S )Nz%s = 0;z%s.memview = NULL;z%s.data = NULL;)
r   r   r  r  r  r  ra   r   r   r   )rL   r   rJ   rJ   rK   generate_post_assignment_code)  s    
z&ExprNode.generate_post_assignment_codec             C   s   d S )NrJ   )rL   rx   r   overloaded_assignmentexception_checkr   rJ   rJ   rK   generate_assignment_code7  s    z!ExprNode.generate_assignment_codec             C   s   d S )NrJ   )rL   r   ignore_nonexistingrJ   rJ   rK   generate_deletion_code>  s    zExprNode.generate_deletion_codec             C   s(   | j r| jjs$| | n
| | d S )N)r   r   r   r   r  )rL   r   rJ   rJ   rK   
free_tempsD  s    zExprNode.free_tempsc             C   s    x|   D ]}|| q
W d S )N)r   r  )rL   r   subrJ   rJ   rK   r  K  s    zExprNode.free_subexpr_tempsc             C   s   d S )NrJ   )rL   rc   r   rJ   rJ   rK   generate_function_definitionsO  s    z&ExprNode.generate_function_definitionsc             C   s    x|   D ]}|| q
W d S )N)r   annotate)rL   r   rp   rJ   rJ   rK   r  T  s    zExprNode.annotatec             C   s.  | }| j }| ||r| S |j}|r0|js0|j}|jr<|j}|jsH|jr|jr|js|jr|j	jr|j	}x6|
 D ]*}||rr||_ |j j|_d|j_| S qrW |jrt| jd n|jr|jr| S t| jd t| _ | S | jd k	rt| jd| jd}||| |jrddlm} |j jsv|j jr:t|||}n:|j jrZt||||}n|jst| jd|f  nl|j jrd|_|j j|| j | j!d	s*|j j"|j"rd
}	|j |f}
nd}	|j j"|j"f}
t| j|	|
  nD|jrN|j js&|t#kr|j j$rt%||}nt&|||d}|j '|s*|j(d k	r*t)|||}nt*|rht+|j rh|S t*|j rt+|r|S t&|||d}n|j jr|r|j,rt-| jd|  t.|||}nb|j/r||kr|0|rt1|||}n6|j |ks*t2|j t2|ks*|0|s*| 3| |S )NTzType is not specializedz/Cannot coerce to a type that is not specialized )rb   r   r$   )
MemoryViewz&Cannot convert '%s' to memoryviewslice)Z	broadcastZcopyingz3Memoryview '%s' not conformable to memoryview '%s'.z-Different base types for memoryviews (%s, %s))r   zTCannot pass Python object as C++ data structure reference (%s &), will pass by copy.)4r   check_for_coercion_erroris_referenceref_base_typeis_constconst_base_typeis_fusedr   is_ptr	base_type"get_all_specialized_function_typessame_asr   usedr   rn   Zis_null_ptrr*   coercion_typeNameNode	coerce_tor   r  r  ra   CoerceToMemViewSliceNoder   CythonArrayNodefrom_carrayis_errorwritable_neededZconforms_tois_memview_broadcastis_memview_copy_assignmentdtyper   is_intCoerceIntToBytesNodeCoerceToPyTypeNode
subtype_ofrl   PyTypeTestNoder5   r3   is_cpp_classr   CoerceFromPyTypeNode
is_complexassignable_fromCoerceToComplexNoderg   fail_assignment)rL   dst_typerc   srcZsrc_typeZused_as_reference	signaturerp   r  msgtuprJ   rJ   rK   r"  Z  s    











,
zExprNode.coerce_toc             C   s   t | jd| j|f  d S )NzCannot assign type '%s' to '%s')r   rn   r   )rL   r6  rJ   rJ   rK   r5    s    zExprNode.fail_assignmentc             C   sX   |r|sd}t | j|f||}|d k	rBt| j|| j|d  dS |rT| | dS dS )Nz)Cannot assign type '%(FROM)s' to '%(TO)s')ZFROMZTOTF)r   r   r   rn   r5  )rL   r6  rc   failrX   messagerJ   rJ   rK   r    s    
z!ExprNode.check_for_coercion_errorc             C   s   |  tj|S )N)r"  r   r   )rL   rc   rJ   rJ   rK   coerce_to_pyobject  s    zExprNode.coerce_to_pyobjectc             C   s   |   r"t| j}t| j||dS | j}|js4|jr8| S |jsP|j	sP|j
sP|jrZt| |S |jrt| jt| j| ddg d|S |jrt|jdk}t| j||dS t| jd|  | S d S )N)r   rl   zoperator bool)obj	attribute)functionr{   r   z%Type '%s' not acceptable as a boolean)rz   r   rl   BoolNodern   r   is_enumr&  ra   r+  r  is_floatCoerceToBooleanNoder0  SimpleCallNodeAttributeNoder   	is_ctuplerw   
componentsr   )rL   rc   
bool_valuer   rJ   rJ   rK   r     s*    

zExprNode.coerce_to_booleanc             C   s   | j jr| S | tj|S d S )N)r   r+  r"  r   r(   )rL   rc   rJ   rJ   rK   coerce_to_integer  s    zExprNode.coerce_to_integerc             C   s   |   r| S t| |S d S )N)r   CoerceToTempNode)rL   rc   rJ   rJ   rK   coerce_to_temp
  s    zExprNode.coerce_to_tempc             C   s   |   r| S | |S d S )N)	is_simplerL  )rL   rc   rJ   rJ   rK   r     s    zExprNode.coerce_to_simplec             C   s   |   S )N)r   )rL   rJ   rJ   rK   rM    s    zExprNode.is_simplec             C   s0   | j r| j js| j jsdS |  r,| jd k	S dS )NFT)r   ra   r   rz   rl   )rL   rJ   rJ   rK   may_be_none  s    
zExprNode.may_be_nonec             C   s   d S )NrJ   )rL   rJ   rJ   rK   as_cython_attribute'  s    zExprNode.as_cython_attributePyExc_TypeErrorrJ   c             C   s   |   rt| |||S | S d S )N)rN  NoneCheckNode)rL   r<  r   format_argsrJ   rJ   rK   as_none_safe_node*  s    zExprNode.as_none_safe_nodec          	   K   sZ   d  }xB|D ]:}||krqyt||}W n tk
r>   Y qX |||< qW | |jf|S )zInstantiate this node class from another node, properly
        copying over all attributes that one would forget otherwise.
        z1cf_state cf_maybe_null cf_is_null constant_result)splitr   ry   rn   )rH   rp   kwargsZ
attributesZ	attr_namer   rJ   rJ   rK   	from_node2  s    
zExprNode.from_node)N)N)r   )FNN)F)FN)rP  rJ   )grN   rO   rP   r   r   
annotationr   r   r   r   is_numpy_attributeru   is_dict_literalis_set_literalis_string_literalis_attributeis_subscriptr   is_buffer_accessis_memview_indexis_memview_slicer(  r)  Zsaved_subexpr_nodesr   	is_target
is_starredr   rl   propertyr
   
attrgetterchild_attrsr   r   r   r   r   r   r   r   r   r   r   r   r   rz   r   r   r   r   r   r   r   r   r   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r  r  r	  r  r  r  r  r  r  r"  r5  r  r=  r   rJ  rL  r   rM  rN  rO  rS  classmethodrV  rJ   rJ   rJ   rK   r   %  s   s


		

 

z

r   c               @   s    e Zd Zg Zdd Zdd ZdS )AtomicExprNodec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   K  s    z/AtomicExprNode.generate_subexpr_evaluation_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  M  s    z-AtomicExprNode.generate_subexpr_disposal_codeN)rN   rO   rP   r   r   r  rJ   rJ   rJ   rK   rg  D  s   rg  c               @   s<   e Zd ZdZeZdd Zdd Zdd Zdd	 Z	d
d Z
dS )PyConstNoder$   c             C   s   dS )Nr$   rJ   )rL   rJ   rJ   rK   rM  V  s    zPyConstNode.is_simplec             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  Y  s    zPyConstNode.may_be_nonec             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   \  s    zPyConstNode.analyse_typesc             C   s   | j S )N)r   )rL   rJ   rJ   rK   r   _  s    z!PyConstNode.calculate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   b  s    z PyConstNode.generate_result_codeN)rN   rO   rP   r   r   r   rM  rN  r   r   r   rJ   rJ   rJ   rK   rh  P  s   rh  c                   s<   e Zd ZdZdZdZdZdd Zdd Z fdd	Z	  Z
S )
NoneNoder$   Py_NoneNc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   p  s    zNoneNode.compile_time_valuec             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rN  s  s    zNoneNode.may_be_nonec                s4   |j s"|js"|js"t| jd|  tt| ||S )NzCannot assign None to %s)ra   r   r&  r   rn   rF   ri  r"  )rL   r6  rc   )rI   rJ   rK   r"  v  s    zNoneNode.coerce_to)rN   rO   rP   is_noner   rl   r   r   rN  r"  rQ   rJ   rJ   )rI   rK   ri  f  s   ri  c               @   s   e Zd ZdZeZdd ZdS )EllipsisNodeZPy_Ellipsisc             C   s   t S )N)Ellipsis)rL   r   rJ   rJ   rK   r     s    zEllipsisNode.compile_time_valueN)rN   rO   rP   r   rm  rl   r   rJ   rJ   rJ   rK   rl  }  s   rl  c               @   sT   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd ZdS )	ConstNoder$   Nc             C   s   dS )Nr$   rJ   )rL   rJ   rJ   rK   rM    s    zConstNode.is_simplec             C   s   dS )Nr$   rJ   )rL   rJ   rJ   rK   r     s    zConstNode.nonlocally_immutablec             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN    s    zConstNode.may_be_nonec             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r     s    zConstNode.analyse_typesc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   r     s    zConstNode.check_constc             C   s   |   S )N)r   )rL   rJ   rJ   rK   r     s    z$ConstNode.get_constant_c_result_codec             C   s
   t | jS )N)rg   r   )rL   rJ   rJ   rK   r     s    zConstNode.calculate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r     s    zConstNode.generate_result_code)rN   rO   rP   r   r   rM  r   rN  r   r   r   r   r   rJ   rJ   rJ   rK   rn    s   rn  c               @   s2   e Zd ZejZdd Zdd Zdd Zdd Z	d	S )
rA  c             C   s   | j | _d S )N)r   rl   )rL   rJ   rJ   rK   r     s    z"BoolNode.calculate_constant_resultc             C   s   | j S )N)r   )rL   r   rJ   rJ   rK   r     s    zBoolNode.compile_time_valuec             C   s(   | j jr| jrdpdS tt| jS d S )NZPy_TrueZPy_False)r   ra   r   rg   int)rL   rJ   rJ   rK   r     s    zBoolNode.calculate_result_codec             C   sZ   |j r&| jjr&t| j| j| jtjdS |jrL| jj rLt| j| j| jt	j
dS t| ||S )N)r   rl   r   )ra   r   r+  rA  rn   r   rl   r   Z	bool_typer   c_bint_typern  r"  )rL   r6  rc   rJ   rJ   rK   r"    s    

zBoolNode.coerce_toN)
rN   rO   rP   r   rp  r   r   r   r   r"  rJ   rJ   rJ   rK   rA    s
   rA  c               @   s"   e Zd ZejZdZdZdd ZdS )NullNodeNULLr   c             C   s   | j S )N)r   )rL   rJ   rJ   rK   r     s    z#NullNode.get_constant_c_result_codeN)	rN   rO   rP   r   Zc_null_ptr_typer   r   rl   r   rJ   rJ   rJ   rK   rq    s   rq  c               @   s*   e Zd ZejZdd Zdd Zdd ZdS )CharNodec             C   s   t | j| _d S )N)ordr   rl   )rL   rJ   rJ   rK   r     s    z"CharNode.calculate_constant_resultc             C   s
   t | jS )N)rt  r   )rL   r   rJ   rJ   rK   r     s    zCharNode.compile_time_valuec             C   s   dt | j S )Nz'%s')r	   Zescape_charr   )rL   rJ   rJ   rK   r     s    zCharNode.calculate_result_codeN)	rN   rO   rP   r   r   r   r   r   r   rJ   rJ   rJ   rK   rs    s   rs  c               @   sh   e Zd ZdZdZdZdd Zdd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )r   r  Nc             K   s&   t j| |f| d|kr"|  | _d S )Nr   )r   __init__find_suitable_type_for_valuer   )rL   rn   kwdsrJ   rJ   rK   ru    s    zIntNode.__init__c             C   s   | j tkr,y|   W n tk
r*   Y nX | jsJ|  rJ| jsJ| jdkr| jdkrXdpZd}tj	| j |df }| j
rt|| j
}n>d| j   krdk rn n| j
r| j
jr| j
}qtj}ntj}|S )NLLr.   r$   ro  i   l        )rl   r   r   r|   is_c_literalrz   unsignedlongnessr   Zmodifiers_and_name_to_typer   widest_numeric_typer+  r(   r   )rL   rankZsuitable_typerJ   rJ   rK   rv    s$    
z$IntNode.find_suitable_type_for_valuec          	   C   s   | j |kr| S |jrR|  r>t| jdt| j |t| jdS t| j| j|t	dS |j
r|jst| j| j| j|d| j| jd}|S |jrt| j| j| jtjd| j| jd}nt| j| j| j| j| jd}t|||S )Nz%d.0)r   r   rl   T)r   rl   r   ry  rz  r{  F)r   rl   rz  r{  )r   rC  rz   	FloatNodern   ro  rl   rm   r   r   
is_numericr2  r   rz  r{  ra   r   r   rn  r"  )rL   r6  rc   rp   rJ   rJ   rK   r"    s(    
zIntNode.coerce_toc             C   s    t | j| j| jtj| j| jdS )N)r   rl   r   rz  r{  )r   rn   r   rl   r   rp  rz  r{  )rL   rc   rJ   rJ   rK   r   $  s
    zIntNode.coerce_to_booleanc             C   s8   | j jr*tt| j}||| j| _n
| 	 | _d S )N)
r   ra   rg   r   str_to_numberr   Z
get_py_intr{  result_coder   )rL   r   Zplain_integer_stringrJ   rJ   rK   r  +  s    z IntNode.generate_evaluation_codec             C   st   | j | j }}|  }|sh|sh| jjrh|d dkrh|d dkrh| jjtjjkrTd}n| jjtjjkrhd}|| | S )Nr   -r$   0rx  L)	rz  r{  value_as_c_integer_stringr   r+  r}  r   Zc_longlong_typer(   )rL   rz  r{  literalrJ   rJ   rK   r   3  s    (z"IntNode.get_constant_c_result_codec             C   s   | j }t|dkr|S d}|d dkr6d}|dd  }|d dkr|d }|rv|dkrv|dd   rvtt|}q|dkrd|dd   }q|d	krtt|dd  d}n$| r| js| js|sd
t| }|| S )Nr.   r  r   r  r$   r  ZoOxX0123456789ZoOZbBz0x%X)	r   rw   isdigitrg   r   r  ro  rz  r{  )rL   r   Zneg_signZliteral_typerJ   rJ   rK   r  >  s&    z!IntNode.value_as_c_integer_stringc             C   s   | j S )N)r  )rL   rJ   rJ   rK   r   Z  s    zIntNode.calculate_result_codec             C   s   t | j| _d S )N)r   r  r   rl   )rL   rJ   rJ   rK   r   ]  s    z!IntNode.calculate_constant_resultc             C   s   t | jS )N)r   r  r   )rL   r   rJ   rJ   rK   r   `  s    zIntNode.compile_time_value)rN   rO   rP   rz  r{  ry  ru  rv  r"  r   r  r   r  r   r   r   rJ   rJ   rJ   rK   r     s   r   c               @   sB   e Zd ZejZdd Zdd Zdd Zdd Z	d	d
 Z
dd ZdS )r~  c             C   s   t | j| _d S )N)rm   r   rl   )rL   rJ   rJ   rK   r   f  s    z#FloatNode.calculate_constant_resultc             C   s
   t | jS )N)rm   r   )rL   r   rJ   rJ   rK   r   i  s    zFloatNode.compile_time_valuec             C   sX   |j r&| jjr&t| j| j| jtjdS |jrJ| jj rJt| j| j| j|dS t	
| ||S )N)r   rl   r   )ra   r   rC  r~  rn   r   rl   r   Z
float_typern  r"  )rL   r6  rc   rJ   rJ   rK   r"  l  s    
zFloatNode.coerce_toc             C   s   | j S )N)r  )rL   rJ   rJ   rK   r   y  s    zFloatNode.calculate_result_codec             C   sL   | j }t|tsttt|}|dkr,dS |dkr8dS |dkrDdS |S d S )Nnanz(Py_HUGE_VAL * 0)infZPy_HUGE_VALz-infz(-Py_HUGE_VAL))r   rk   rB   r   reprrm   )rL   ZstrvalZcmpvalrJ   rJ   rK   r   |  s    z$FloatNode.get_constant_c_result_codec             C   s,   |   }| jjr"|| j|| _n|| _d S )N)r   r   ra   Zget_py_floatr   r  )rL   r   Zc_valuerJ   rJ   rK   r    s    z"FloatNode.generate_evaluation_codeN)rN   rO   rP   r   c_double_typer   r   r   r"  r   r   r  rJ   rJ   rJ   rK   r~  c  s   r~  c          	   C   s   t | }|d k	r|S | | }|rT|jrT|jjsN|jjsN|jjsN|jjrT|jS ddl	m	} t
dd |d |d |d d f}y|d|  |d j|d	}W n tk
r   Y n4X |jjd j}t|tr||}t|tr|jS W d Q R X d S )
Nr$   )TreeFragmentT)ignorer   r.      z
sizeof(%s))rb   Zinitial_pos)r   parse_basic_typeglobal_scopelookupr   is_extension_typeis_struct_or_unionis_builtin_typer0  r  r   filenamer   rootZstatsexprrk   SizeofTypeNoder   arg_type)rb   rn   rc   r   Zglobal_entryr  ZdeclarationZsizeof_noderJ   rJ   rK   _analyse_name_as_type  s.    




r  c               @   sf   e Zd ZdZeZdd ZdddZdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )	BytesNodeTc             C   s   | j | _d S )N)r   rl   )rL   rJ   rJ   rK   r     s    z#BytesNode.calculate_constant_resultNc             C   s,   t | j||| | jj}t| j||dS )N)r   rl   )r	   bytes_literalr   encodingr  rn   )rL   r   r   r   r   rJ   rJ   rK   as_sliced_node  s    zBytesNode.as_sliced_nodec             C   s
   | j  S )N)r   
byteencode)rL   r   rJ   rJ   rK   r     s    zBytesNode.compile_time_valuec             C   s   t | jd| j|S )Nz	ISO8859-1)r  r   decodern   )rL   rc   rJ   rJ   rK   r     s    zBytesNode.analyse_as_typec             C   s   t | jdkS )Nr$   )rw   r   )rL   rJ   rJ   rK   can_coerce_to_char_literal  s    z$BytesNode.can_coerce_to_char_literalc             C   s   t | j}t| j||dS )N)r   rl   )r   r   rA  rn   )rL   rc   rI  rJ   rJ   rK   r     s    
zBytesNode.coerce_to_booleanc             C   s&  | j |kr| S |jrZ|  s,t| jd | S |jrBt| jd | S t| j| jt| jdS t	| j| j| j
d}|jr|ttjfkrtj|_ n| j||dd |S |tjtjfkr||_ |S |tjtjtjfkr|tjkrtjntj|_ t||S |tjr|jr|jr||_ |S t|||S )Nz?Only single-character string literals can be coerced into ints.zRBytes literals cannot coerce to Py_UNICODE/Py_UCS4, use a unicode literal instead.)r   rl   T)r;  )r   r+  r  r   rn   is_unicode_charrs  r   rt  r  rl   ra   r   r   r   r  r   r]   r_   r^   r`   c_void_ptr_typeCastNoder3  r0  r  rn  r"  )rL   r6  rc   rp   rJ   rJ   rK   r"    s:    



zBytesNode.coerce_toc             C   sP   | j jr|| j}n0| j jr,|| j}n| j }t| j tj	|}|| _
d S )N)r   ra   get_py_string_constr   r  Zget_string_constZas_c_string_literalr)   r   r  r  )rL   r   r   r  rJ   rJ   rK   r    s    
z"BytesNode.generate_evaluation_codec             C   s   d S )NrJ   )rL   rJ   rJ   rK   r     s    z$BytesNode.get_constant_c_result_codec             C   s   | j S )N)r  )rL   rJ   rJ   rK   r     s    zBytesNode.calculate_result_code)N)rN   rO   rP   r[  r   r   r   r  r   r   r  r   r"  r  r   r   rJ   rJ   rJ   rK   r    s   
&r  c               @   sj   e Zd ZdZdZeZdd Zdd ZdddZ	d	d
 Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )UnicodeNodeTNc             C   s   | j | _d S )N)r   rl   )rL   rJ   rJ   rK   r     s    z%UnicodeNode.calculate_constant_resultc             C   s   t | j| j|S )N)r  r   rn   )rL   rc   rJ   rJ   rK   r     s    zUnicodeNode.analyse_as_typec             C   sv   t | jd | rd S t | j||| }| jj|_| jd k	r`t | j||| | jj}nd }t| j|||dS )N)r   bytes_valuerl   )	r	   string_contains_surrogatesr   EncodedStringr  r  r  r  rn   )rL   r   r   r   r   r  rJ   rJ   rK   r    s    

zUnicodeNode.as_sliced_nodec             C   s   || j krn|jrJ|  s*t| jd | S t| j}t| j|t||dS |j	s|j
rx| jd k	rxt| j| jd||S |jrt| j| jd}||_ |S t| jd n|ttjfkr| j||dd | S )NzhOnly single-character Unicode string literals or surrogate pairs can be coerced into Py_UCS4/Py_UNICODE.)r   r   rl   )r   zUnicode literals do not support coercion to C types other than Py_UNICODE/Py_UCS4 (for characters) or Py_UNICODE* (for strings).T)r;  )r   r  r  r   rn   rt  r   r   rg   ra   r  r  r  r"  r  r  r   r   r-   r  )rL   r6  rc   Z	int_valuerp   rJ   rJ   rK   r"  '  s.    

zUnicodeNode.coerce_toc             C   s   t | jdkS )Nr$   )rw   r   )rL   rJ   rJ   rK   r  E  s    z&UnicodeNode.can_coerce_to_char_literalc             C   s   t | j}t| j||dS )N)r   rl   )r   r   rA  rn   )rL   rc   rI  rJ   rJ   rK   r   K  s    
zUnicodeNode.coerce_to_booleanc             C   s   t | jS )N)r	   r  r   )rL   rJ   rJ   rK   contains_surrogatesO  s    zUnicodeNode.contains_surrogatesc          
   C   s   | j jr|  r|td| _|| j}|| j}|d krBd S |	| j
 |d| j|||| j| j
f  || j
d| j  q|| j| _n|| j| _d S )NZustringzG%s = PyUnicode_FromUnicode(%s, (sizeof(%s) / sizeof(Py_UNICODE))-1); %sz__Pyx_PyUnicode_READY(%s))r   ra   r  get_py_constr   r  Zget_pyunicode_ptr_constr   get_cached_constants_writerr   rn   r   r   put_error_if_negr  )rL   r   Z
data_cname
const_coderJ   rJ   rK   r  R  s$    z$UnicodeNode.generate_evaluation_codec             C   s   | j S )N)r  )rL   rJ   rJ   rK   r   j  s    z!UnicodeNode.calculate_result_codec             C   s   | j S )N)r   )rL   rc   rJ   rJ   rK   r   m  s    zUnicodeNode.compile_time_value)N)rN   rO   rP   r[  r  r   r   r   r   r  r"  r  r   r  r  r   r   rJ   rJ   rJ   rK   r    s   
r  c               @   sf   e Zd ZeZdZdZdZdd Zdd Z	dddZ
d	d
 Zdd Zdd Zdd Zdd Zdd ZdS )
StringNodeTNc             C   s   | j d k	r| j | _d S )N)unicode_valuerl   )rL   rJ   rJ   rK   r   ~  s    
z$StringNode.calculate_constant_resultc             C   s   t | jp| jd| j|S )Nz	ISO8859-1)r  r  r   r  rn   )rL   rc   rJ   rJ   rK   r     s    zStringNode.analyse_as_typec             C   sx   t | j| j||| }| jj|_| jd k	r^t| jd | rFd S t| j||| }nd }t| j|||| j	dS )N)r   r  rl   is_identifier)
r   r   r  r  r	   r  r  r  rn   r  )rL   r   r   r   r   r  rJ   rJ   rK   r    s    

zStringNode.as_sliced_nodec             C   sN   |t k	rJt|sJ|js0t| j| jd||S |tj	k	rJ| j
||dd | S )N)r   T)r;  )r   r   r.  ra   r  rn   r   r"  r   r-   r  )rL   r6  rc   rJ   rJ   rK   r"    s    
zStringNode.coerce_toc             C   s   | j  ot| jdkS )Nr$   )r  rw   r   )rL   rJ   rJ   rK   r    s    z%StringNode.can_coerce_to_char_literalc             C   s   |j | j| jd| jd| _d S )NT)
identifieris_strr  )r  r   r  r  r  )rL   r   rJ   rJ   rK   r    s    
z#StringNode.generate_evaluation_codec             C   s   d S )NrJ   )rL   rJ   rJ   rK   r     s    z%StringNode.get_constant_c_result_codec             C   s   | j S )N)r  )rL   rJ   rJ   rK   r     s    z StringNode.calculate_result_codec             C   s8   | j jr| j S ts| j  S | jd k	r,| jS | j dS )Nz	iso8859-1)r   Z
is_unicoder#   r  r  r  )rL   rc   rJ   rJ   rK   r     s    

zStringNode.compile_time_value)N)rN   rO   rP   r   r   r[  r  r  r   r   r  r"  r  r  r   r   r   rJ   rJ   rJ   rK   r  q  s   
r  c               @   s   e Zd ZdZdS )IdentifierStringNodeTN)rN   rO   rP   r  rJ   rJ   rJ   rK   r    s   r  c               @   sN   e Zd ZejZdd Zdd Zdd Zdd Z	d	d
 Z
dZdd Zdd ZdS )ImagNodec             C   s   t dt| j| _d S )Ng        )complexrm   r   rl   )rL   rJ   rJ   rK   r     s    z"ImagNode.calculate_constant_resultc             C   s   t dt| jS )Ng        )r  rm   r   )rL   r   rJ   rJ   rK   r     s    zImagNode.compile_time_valuec             C   s   | j | | S )N)r   create_declaration_utility_code)rL   rc   rJ   rJ   rK   r     s    zImagNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN    s    zImagNode.may_be_nonec             C   s@   | j |kr| S t| j| jd}|jr2d|_tj|_ t	|||S )N)r   r$   )
r   r  rn   r   ra   r   r   complex_typerg  r"  )rL   r6  rc   rp   rJ   rJ   rK   r"    s    
zImagNode.coerce_tozConstructing complex numberc             C   s*   | j jr|  S d| j jt| jf S d S )Nz	%s(0, %r))r   ra   r   
from_partsrm   r   )rL   rJ   rJ   rK   r     s    zImagNode.calculate_result_codec          	   C   sF   | j jrB|d|  t| j||  | jf  || 	  d S )Nz'%s = PyComplex_FromDoubles(0.0, %r); %s)
r   ra   r   r   rm   r   r   rn   
put_gotrefr   )rL   r   rJ   rJ   rK   r     s    zImagNode.generate_result_codeN)rN   rO   rP   r   Zc_double_complex_typer   r   r   r   rN  r"  gil_messager   r   rJ   rJ   rJ   rK   r    s   r  c               @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )NewExprNodeNc             C   s`   | j |}|d ks|js0t| jd t| _d S | | || j}|| _	|| _
|j| _| jS )Nz/new operator can only be applied to a C++ class)Zcppclassr   r0  r   rn   r*   r   Z	cpp_checkZget_constructor
class_typer   )rL   rc   r   constructorrJ   rJ   rK   rr     s    
zNewExprNode.infer_typec             C   s   | j d kr| | | S )N)r   rr   )rL   rc   rJ   rJ   rK   r     s    

zNewExprNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN    s    zNewExprNode.may_be_nonec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r     s    z NewExprNode.generate_result_codec             C   s   d| j   S )Nznew )r  empty_declaration_code)rL   rJ   rJ   rK   r     s    z!NewExprNode.calculate_result_code)	rN   rO   rP   r   rr   r   rN  r   r   rJ   rJ   rJ   rK   r    s   r  c                   sd  e 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d Zdd Zd	d
 Zdd Zdd Z fddZdJddZdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#ZdKd$d%Zd&d' Z d(d) Z!d*d+ Z" fd,d-Z#d.d/ Z$d0d1 Z%d2d3 Z&d4d5 Z'd6d7 Z(d8d9 Z)d:d; Z*d<d= Z+d>d? Z,dLd@dAZ-dBdC Z.dDdE Z/dMdFdGZ0dHdI Z1  Z2S )Nr!  TFNr   c             C   s   | j S )N)cython_attribute)rL   rJ   rJ   rK   rO  .  s    zNameNode.as_cython_attributec             C   s:   | j d kr|| j| _ | j d k	r2| j jjr2| fS dS d S )NrJ   )r   r  rb   r   is_unspecified)rL   rc   rJ   rJ   rK   r   1  s
    
zNameNode.type_dependenciesc             C   s   | j d kr|| j| _ | j d ks.| j jtkrB| jd k	r>| jS tS | j jjsV| j jjrj| j| j jjkrjt	S | j jj
r| j jjrtS t| j jS n.| j jjr| jr| jjr| j js| jS | j jS d S )N)r   r  rb   r   r+   inferred_typer   r  r  r   r   scopeZis_builtin_scoper   CPtrTypera   r+  Zmight_overflow)rL   rc   rJ   rJ   rK   rr   9  s"    



zNameNode.infer_typec             C   s8   y| | jS  tk
r2   t| jd| j  Y nX d S )Nz"Compile-time name '%s' not defined)r  rb   KeyErrorr   rn   )rL   r   rJ   rJ   rK   r   U  s    zNameNode.compile_time_valuec             C   s   | j r| j jjrd S | j jS )N)r   r   ra   r   )rL   rJ   rJ   rK   r   [  s    z#NameNode.get_constant_c_result_codec                st   |t krb| j}|rb|jrb|j}|rb|jr>|jr>||j| j}t	| j| jd}||_|
| |S tt	| ||S )N)rb   )r   r   r   as_variable
is_builtinr  declare_builtinrb   rn   r!  analyse_rvalue_entryrF   r"  )rL   r6  rc   r   Z	var_entryrp   )rI   rJ   rK   r"  `  s    

zNameNode.coerce_toc             C   s   |j d sdS |js|jrdS | j}| js8||dk	r<dS | j}|jrNd}nt||\}}|dkr~|rz|j d dkrzt	nt
}|j||| j| d| _|| j_dS )aV  Implements PEP 526 annotation typing in a fairly relaxed way.

        Annotations are ignored for global variables, Python class attributes and already declared variables.
        String literals are allowed and ignored.
        The ambiguous Python types 'int' and 'long' are ignored and the 'cython.int' form must be used instead.
        Zannotation_typingNinfer_typesF)Zis_cdef)r[   is_module_scopeis_py_class_scoperb   r   lookup_hererW  r[  r'   r+   r   declare_varrn   )rL   rc   	as_targetrb   rW  atype_rJ   rJ   rK   declare_from_annotations  s    
z NameNode.declare_from_annotationc             C   s*   | j }|s|| j}|r&|jr&|jS d S )N)r   r  rb   	as_module)rL   rc   r   rJ   rJ   rK   r     s    
zNameNode.analyse_as_modulec             C   sV   | j rt| j }nt| j}|r(|S | j}|s>|| j}|rN|jrN|jS d S d S )N)r  r   r  rb   r   r  is_typer   )rL   rc   r   r   rJ   rJ   rK   r     s    
zNameNode.analyse_as_typec             C   s:   | j }|s|| j}|r6|jr6|jjs0|jjr6|jS d S )N)r   r  rb   r  r   r  r  )rL   rc   r   rJ   rJ   rK   r     s    
z"NameNode.analyse_as_extension_typec             C   s   | j s|| j| _ | j s2| jd k	r2| j|dd | j s|jd rVt| jd| j d |jd dkrjt}nt	}|
| j|| j| _ | j jrt	| _| j jrd| j _d S )NT)r  zwarn.undeclaredzimplicit declaration of '%s'r$   r  F)r   r  rb   rW  r  r[   r   rn   r+   r   r  is_declared_genericr   r  is_variable)rL   rc   r   rJ   rJ   rK   r     s    
z#NameNode.analyse_target_declarationc             C   s   |j d | _| j}|d krf|| j}|sP|| j| j}|rP|jrP|jrPd| _	|s`t
j| _| S || _d|_|jjrddlm} || | | | S )NinitializedcheckTr$   )Buffer)r[   initialized_checkr   r  rb   r  rn   r  r  r   r   r*   r   r  	is_bufferr  r  used_buffer_aux_varsr  )rL   rc   r   r  rJ   rJ   rK   r     s$    

zNameNode.analyse_typesc             C   s   | j |dd | j}|jrP|jrP|js<|jjs<|  sP|jrP|j }| _|j| _| jjrjt	| j
d| j  | jjrt	| j
d| j  |  st	| j
d| j  tj| _d|_|jjrddlm} || | S )NT)ra  zAssignment to const '%s'zAssignment to reference '%s'zAssignment to non-lvalue '%s'r$   )r  )analyse_entryr   r   r  is_overridabler   r   fused_cfunctionr  r   rn   rb   r  r   r*   r  r  r  r  r  )rL   rc   r   r  rJ   rJ   rK   r     s$    
zNameNode.analyse_target_typesc             C   sh   |  | | j}|jrt| _|js(|jrJ|jr<|jr<d| _nd| _d| _	n|j
jrdd| _d| _	d| _| S )Nr   r$   FT)r  r   r  r   r   is_pyglobalr  r  r   is_used_as_rvaluer   r   r   )rL   rc   r   rJ   rJ   rK   r    s    
zNameNode.analyse_rvalue_entryc             C   s:   d| _ | jr6| j}|jr(|js6|   n|jr6|   d S )NT)r   r  r   r  r  r   r  )rL   rc   r   rJ   rJ   rK   r   	  s    
zNameNode.nogil_checkz"Accessing Python global or builtinc             C   s<   |    | j}|j}|s2|jr2| jr2| jjr2| j}|| _d S )N)check_identifier_kindr   r   ra   r  r  )rL   rc   ra  r   r   rJ   rJ   rK   r    s    zNameNode.analyse_entryc             C   s   | j }|jr|jjr|| _|jrP|jjrPt| jd t	}d|_
| j j|_|| _ nJ|js|js|js|js|js| j jr| j j| _ n| jst| jd| j  d S )NTz7'%s' is not a constant, variable or function identifier)r   r  r   r  
type_entryrB  r   Entryrb   r   r  r  r  r  r  r   r0  r  is_cython_moduler   rn   )rL   r   Zpy_entryrJ   rJ   rK   r     s    
zNameNode.check_identifier_kindc             C   s0   | j s| jrdS | jp || j}|jo.|j S )NF)r  r  r   r  rb   r  r  )rL   rc   r   rJ   rJ   rK   "is_cimported_module_without_shadow4  s    z+NameNode.is_cimported_module_without_shadowc             C   s   dS )Nr$   rJ   )rL   rJ   rJ   rK   rM  :  s    zNameNode.is_simplec                sl   | j r^| jr^| jjs| jjr^t| ddr,dS d| _d}x| j D ]}|j r>d}P q>W | `|S tt	|  S )N_none_checkingFT)
rv   r   ra   r   r   r  rx   rN  rF   r!  )rL   rN  
assignment)rI   rJ   rK   rN  >  s    
zNameNode.may_be_nonec             C   s:   t | rdS | j}|r|jr"dS |jp8|jp8|jp8|jS )NTF)r   r   r   
in_closureis_localis_argr  is_readonly)rL   r   rJ   rJ   rK   r   S  s    

zNameNode.nonlocally_immutablec             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   calculate_target_results[  s    z!NameNode.calculate_target_resultsc             C   s8   | j }|d k	r4|js4|js4|js4|jjs4|   dS dS )NFT)r   r  r   r  r   r   )rL   r   rJ   rJ   rK   r   ^  s    zNameNode.check_constc             C   s(   | j }|js$|js$|js$|   dS dS )NFT)r   
is_cglobalr   r  r   )rL   r   rJ   rJ   rK   r   i  s
    zNameNode.check_const_addrc             C   s"   | j jr| j j p | j jo | j jS )N)r   r  r  r   r  )rL   rJ   rJ   rK   r   p  s    
zNameNode.is_lvaluec             C   s   | j jo| jj S )N)r   r  r   r   )rL   rJ   rJ   rK   r   y  s    zNameNode.is_addressablec             C   s   dS )Nr   rJ   )rL   rJ   rJ   rK   r   |  s    zNameNode.is_ephemeralc             C   s   | j }|sdS |jS )Nz<error>)r   r   )rL   r   rJ   rJ   rK   r     s    zNameNode.calculate_result_codec          
   C   s  t | dst| j}|d kr d S |jr4|j|j |jrD|jrDd S |jr|j	j
s\td|| jj}|jrxtj}n|jj}| js|d|  ||f  |d|    |d |jtdd |d|  |f  | js|d	 |||  | j ||   n|jr|jjs|j	j
sBtd|| jj}|jtd
d |d|  |||  | jf  ||   nV|js|jrl|jjrl|j	j
std|| jj}|jjr|jtdd |d|  |||  | jf  n@|jtdd |d|  |jj|||  | jf  ||   n|js|js|js|j	jr| j s| jo| j! }|j	"|j#}|j	jo| j$}|r|r|j	j
s|r|%| j|| j& d S )Nr   z,Python global or builtin not a Python objectz%s = PyObject_GetItem(%s, %s);zif (unlikely(!%s)) {zPyErr_Clear();ZGetModuleGlobalNamezObjectHandling.cz"__Pyx_GetModuleGlobalName(%s, %s);r   ZGetBuiltinNamez!%s = __Pyx_GetBuiltinName(%s); %sz%__Pyx_GetModuleGlobalName(%s, %s); %sZGetNameInClassz$__Pyx_GetNameInClass(%s, %s, %s); %s)'r   r   r   utility_codeglobalstateuse_utility_coder  r  is_pyclass_attrr   ra   intern_identifierrb   r   Zbuiltins_cnamer  namespace_cname
cf_is_nullr   r   r   load_cachedr   rn   r  r   r  r  r  r  from_closurer   cf_maybe_null
allow_nullZcheck_for_null_coder   r  put_error_if_unboundr   )rL   r   r   interned_cname	namespaceZraise_unboundZ	null_codeZmemslice_checkrJ   rJ   rK   r     s    




"zNameNode.generate_result_codec          	   C   sn  | j }|d krd S | j jjr>t|tr>| js>|js>t| jd |j	r0|jj
sVtd|| j j}| j jj}|jrd}	d| }nD|jjrd}	tj}n0|jr|jtdd d}	ndstt||| jd	|	||| f  trtd
 td|  || || |jrj| d|jj!j"  n:| jj#rH| $|| n| jj%r^| &|| d}
| jj
rT| j'rT|(| |j)p| j j*p| j j+}|r| j,s| j-r|.|   n|/|   d}
|j)r|0| 1 |2| 3  nL| j,s<| j-r |4| 1 |2| 3  n|0| 1 |2| 3  nd}
|rT|5|  | jj#sL|
s&|r|1 }|dkrt6|| jd| 1 |f | jj
r| 1 nd || j7 n| d| 1 |f  n\|2| 3 }t8| jr| d| 1 | 1 |f  n$|| 1 kr&| d| 1 |f  tr@td
 td|  |9| n|: r`|9| || d S )Nz?Literal list must be assigned to pointer at time of declarationz,Python global or builtin not a Python objectPyDict_SetItemz%s->tp_dictZSetNameInClasszObjectHandling.cZ__Pyx_SetNameInClassFz%s(%s, %s, %s)z"NameNode.generate_assignment_code:z"...generating disposal code for %szPyType_Modified(%s);T+z%s = %s;znew (&%s) decltype(%s){%s};z)...generating post-assignment code for %s);r   r   r  rk   ListNodelhs_of_first_assignmentin_module_scoper   rn   r  ra   r   r  rb   r  r  	is_memberr  r   moddict_cnamer  r  r  r   r  r  r  r   r   printr  r  r   Zparent_typeZtypeptr_cnamer    generate_acquire_memoryviewslicer  generate_acquire_bufferr   r   r  r  r  r  r  put_xgotrefr  put_decref_setr   r   r   Zput_xdecref_setput_giverefr   r   r5   r	  r   )rL   rx   r   r
  r  r   r   r  r  setterassignedZis_external_refr   rJ   rJ   rK   r    s    











z!NameNode.generate_assignment_codec          	   C   s6   ddl m} |j|  | j| j||| j | jd dS )z
        Slices, coercions from objects, return values etc are new references.
        We have a borrowed reference in case of dst = src
        r$   )r  )Z	lhs_cnamer   Zlhs_posrx   r   r   Zfirst_assignmentN)r  r  Zput_acquire_memoryviewslicer   r   rn   r   r  )rL   rx   r   r  rJ   rJ   rK   r  P	  s    z)NameNode.generate_acquire_memoryviewslicec             C   s   t |tp|j}|r$||  }n0|jj| jjdd}|	d|||  f  ddl
m} |j|  || j| j | j|d |s|	d|  |j| d S )NF)r   z%s = %s;r$   )r  )Zis_initializedrn   r   z%s = 0;)rk   r!  r   r   r   r   r   r   r   r   r  r  Zput_assign_to_bufferr   r  rn   r   )rL   rx   r   Z
pretty_rhsZrhstmpr  rJ   rJ   rK   r  `	  s    z NameNode.generate_acquire_bufferc          	   C   s  | j d krd S | j jrf| j jj}|| j j}|r8d}nd| j j }|d||||| jf  nV| j j	r|j
tdd || j j}dtj|f }|r|d||| jf  n|| j| n| j jjs| j jjr| js| jr
|s
|| j| j  | j jjr| j jrL|r>| jr>||   n||   |rp| jrp||  |   n||  |   |d|    n|j| j j| j  d	 nt!| jd
 d S )NzPyErr_Clear(); elsezT{ PyErr_Clear(); PyErr_Format(PyExc_NameError, "name '%%s' is not defined", "%s"); }ziif (unlikely(PyObject_DelItem(%s, %s) < 0)) { if (likely(PyErr_ExceptionMatches(PyExc_KeyError))) %s %s }PyObjectSetAttrStrzObjectHandling.cz!__Pyx_PyObject_DelAttrStr(%s, %s)zjif (unlikely(%s < 0)) { if (likely(PyErr_ExceptionMatches(PyExc_AttributeError))) PyErr_Clear(); else %s }z
%s = NULL;)r   z!Deletion of C names not supported)"r   r  r  r  r  rb   r   r   rn   r  r  r  r   r  r   module_cnamer  r   ra   r   r  r  r  r  r  r   r  Zput_xdecrefr   
put_decrefr  r   r   r   )rL   r   r  r  r  Zkey_error_codeZdel_coderJ   rJ   rK   r  t	  sR    




zNameNode.generate_deletion_codec             C   sz   t | drv| jrv| jd | jd | jd t| j d f}| jjrLd\}}nd\}}||t||| j t| jd d S )N	is_calledr   r$   r.   )Zpy_callzpython function (%s))Zc_callzc function (%s))size)	r   r  rn   rw   rb   r   ra   r  r/   )rL   r   rn   styletextrJ   rJ   rK   r  	  s    *
zNameNode.annotate)F)F)FNN)F)3rN   rO   rP   is_namer  r  r  r  r   r  r  r  r  r   r  rO  r   rr   r   r   r"  r  r   r   r   r   r   r   r  r   r  r  r  r  rM  rN  r   r  r   r   r   r   r   r   r   r  r  r  r  r  rQ   rJ   rJ   )rI   rK   r!    s`   



	S 
u
8r!  c               @   s2   e Zd ZeZdgZdd ZdZdd Zdd Z	d	S )
BackquoteNodeargc             C   s&   | j || _ | j || _ d| _| S )Nr$   )r  r   r=  r   )rL   rc   rJ   rJ   rK   r   	  s    zBackquoteNode.analyse_typeszBackquote expressionc             C   s   t | jj| _d S )N)r  r  rl   )rL   rJ   rJ   rK   r   	  s    z'BackquoteNode.calculate_constant_resultc          	   C   s>   | d|  | j ||  | jf  ||   d S )Nz%s = PyObject_Repr(%s); %s)r   r   r  r   r   rn   r  )rL   r   rJ   rJ   rK   r   	  s    z"BackquoteNode.generate_result_codeN)
rN   rO   rP   r   r   r   r   r  r   r   rJ   rJ   rJ   rK   r  	  s   r  c               @   s,   e Zd ZeZddgZdd ZdZdd ZdS )	
ImportNodemodule_name	name_listc             C   st   | j d kr4|jd s&tj| jjkr.d| _ nd| _ | j|}|	|| _| j
rj| j
|}|	|| _
d| _| S )NZ
py2_importr   r$   )rj   r[   r0   r   r  contextZfuture_directivesr  r   r=  r  r   )rL   rc   r  r  rJ   rJ   rK   r   	  s    

zImportNode.analyse_typeszPython importc          	   C   s   | j r| j  }nd}|jtdd d| j || jf }| jdkr| jjr| jj	t
krt
| jj	 \}}}|jt|| d||f }|d|  |||  | jf  ||   d S )Nr  ZImportzImportExport.cz__Pyx_Import(%s, %s, %d)r   z%s(%s)z%s = %s; %s)r  r   r  r  r   r  r  rj   r[  r   r&   r   r   r   rn   r  )rL   r   Zname_list_codeZimport_codeZhelper_funcZ	code_nameZ	code_filerJ   rJ   rK   r   	  s&    

zImportNode.generate_result_codeN)	rN   rO   rP   r   r   r   r   r  r   rJ   rJ   rJ   rK   r  	  s
   r  c            
   @   s   e Zd ZeZdZdZdZdZdZ	dgZ
dd ZdZeeejedejdgZdd	 Zd
d Zdd Zdd Zdd Zdd Zdd ZdS )IteratorNodeNFsequencec             C   s   | j || _ | j jjs"| j jjr8| j jjs8| j j| _nB| j jjrN| | n,| j || _ | j jt	t
fkrz| j d| _ d| _| S )Nz!'NoneType' object is not iterabler$   )r  r   r   r   r  r  r0  analyse_cpp_typesr=  r   r   rS  r   )rL   rc   rJ   rJ   rK   r   "
  s    

zIteratorNode.analyse_typeszIterating over Python objectitc             C   s   | j |S )N)r  r   )rL   rc   rJ   rJ   rK   r   8
  s    zIteratorNode.type_dependenciesc             C   sN   | j |}|js|jr|S |jr@|jd}|d k	rJ|jjS n
|j	rJ|S t
S )Nbegin)r  rr   r   r  r0  r  r  r   return_typera   r   )rL   rc   sequence_typer  rJ   rJ   rK   rr   ;
  s    
zIteratorNode.infer_typec             C   s  | j j}|jr|j}|jd}|jd}|d ksD|jjrD|jjrbt| j	d| j j  t
| _d S |d ksz|jjrz|jjrt| j	d| j j  t
| _d S |jj}|jrV|| j	d||jjgd krt| j	d| j j  t
| _d S || j	d|gd krt| j	d| j j  t
| _d S || j	d	|gd krNt| j	d
| j j  t
| _d S || _nH|jr||jjksxt| j	d || _nt| j	d| j j  t
| _d S d S )Nr  endzmissing begin() on %szmissing end() on %sz!=z-missing operator!= on result of begin() on %sz++z-missing operator++ on result of begin() on %sr   z,missing operator* on result of begin() on %sz(incompatible types for begin() and end()z;result type of begin() on %s must be a C++ class or pointer)r  r   r  r  r  r  r   r{   r   rn   r*   r  r0  lookup_operator_for_types)rL   rc   r   r  r!  Z	iter_typerJ   rJ   rK   r  G
  sT    zIteratorNode.analyse_cpp_typesc          	   C   s  | j j}|jr| j jr&d| j   }n@t|}|jj|dd| _	|
d| j	| j  f  d| j	 }|
d|  |f  d S |js|jrtd|ttfk}|s| jrtd|j | _| jr|
d	| j  | j  f  |s| jrZ|jjtjdd| _| jr.|tkr d
|   }nd|   }nd}|
d|  | j  |  | j|f  |s|jj| jdd| _| jr|
d| j  |
d |d| j  |
d|  | j  ||  | jf  ||   |
d| j|  || j| jf  | jr|
d d S )Nz%s.beginF)r   z	%s = &%s;z	%s->beginz
%s = %s();z#for in carray slice not transformedzBinternal error: reversed() only implemented for list/tuple objectsz>if (likely(PyList_CheckExact(%s)) || PyTuple_CheckExact(%s)) {zPyList_GET_SIZE(%s) - 1zPyTuple_GET_SIZE(%s) - 1r  z#%s = %s; __Pyx_INCREF(%s); %s = %s;z
%s = NULL;z} else {z	%s = -1; z%s = PyObject_GetIter(%s); %sz!%s = Py_TYPE(%s)->tp_iternext; %sr   )r  r   r0  r  r   r   
c_ptr_typer   r   cpp_iterator_cnamer   r   r  r   r   r   reversedr   r  may_be_a_sequencer   r   counter_cname_func_iternext_typeiter_func_ptrputr   rn   r  )rL   r   r   Z
begin_funcZis_builtin_sequenceZ
init_valuerJ   rJ   rK   r   t
  sf    






z!IteratorNode.generate_result_codec             C   s  | j stdd||  f }| jjrbt| jj}| jjd krD|}nt| jjj	t
rb|| jjj	 }|d| j |f  | jrd}nd}|d |d|||  | j || j ||d| jf  |d	 |d
||  | j | j |||| jf  || |d d S )Nz/internal error: counter_cname temp not preparedzPy%s_GET_SIZE(%s)zif (%s >= %s) break;z--z++z<#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFSz6%s = Py%s_GET_ITEM(%s, %s); __Pyx_INCREF(%s); %s%s; %sr  z#elsez&%s = PySequence_ITEM(%s, %s); %s%s; %sz#endif)r'  r   r   r  ru   rw   r{   r   rk   rl   r"   r   r%  error_goto_if_negrn   r   r  )rL   Z	test_nameresult_namer   Z
final_sizeZ
item_countZinc_decrJ   rJ   rK   generate_next_sequence_item
  sD    


z(IteratorNode.generate_next_sequence_itemc             C   s  | j j}| jr|d| j  |jr| jr6d| j }nd| j   }|d|  |f  |d||  f  |d|    d S |tkr| 	d|| d S |t
kr| 	d|| d S | jr|d	| j  |d
|    | 	d|| |d | 	d|| |d |d |d |d|| j|  f  |d|  |d |d |d |d|| j  |d |d |d || |d d S )Nzif (%s < 0) break;z%s->endz%s.endzif (!(%s != %s())) break;z	%s = *%s;z++%s;ListTuplezif (likely(!%s)) {z$if (likely(PyList_CheckExact(%s))) {z} else {r   z} else {z%s = %s(%s);zif (unlikely(!%s)) {z&PyObject* exc_type = PyErr_Occurred();zif (exc_type) {z\if (likely(__Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();zelse %szbreak;)r  r   r%  r   r'  r0  r$  r   r   r-  r   r&  r)  r   r*  r   rn   r  )rL   r,  r   r   Zend_funcrJ   rJ   rK   generate_iter_next_result_code
  sZ    











z+IteratorNode.generate_iter_next_result_codec             C   sR   | j r|j| j  | jr.|j| j d | _| jrB|j| j t| | d S )N)r'  r   r   r)  r$  r   r  )rL   r   rJ   rJ   rK   r    s    zIteratorNode.free_temps)rN   rO   rP   r   r   r)  r'  r$  r%  is_asyncr   r   r  r   r  	CFuncTypeCFuncTypeArgr(  r   rr   r  r   r-  r1  r  rJ   rJ   rJ   rK   r  
  s&   -B'3r  c               @   s>   e Zd Zdd Zdd Zdd Zddd	Zd
d Zdd ZdS )NextNodec             C   s   t | |j || _d S )N)rg  ru  rn   iterator)rL   r6  rJ   rJ   rK   ru  #  s    zNextNode.__init__c             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   r   '  s    zNextNode.nogil_checkc             C   s   | j |S )N)r6  r   )rL   rc   rJ   rJ   rK   r   +  s    zNextNode.type_dependenciesNc             C   s   |d kr| j |}|js |jr&|jS |jr^|| jd|gjj	}|j
rN|j}|jrZ|j}|S t| j| j jt| jdtjdd}||S d S )Nr   PY_SSIZE_T_MAX)r   r   )r   r   )r6  rr   r  r   r  r0  r"  rn   r   r  r  r  r  r  r   r  r   r   r   )rL   rc   Ziterator_type	item_typeZfake_index_noderJ   rJ   rK   rr   .  s"    zNextNode.infer_typec             C   s   |  || jj| _d| _| S )Nr$   )rr   r6  r   r   )rL   rc   rJ   rJ   rK   r   C  s    zNextNode.analyse_typesc             C   s   | j |  | d S )N)r6  r1  r   )rL   r   rJ   rJ   rK   r   H  s    zNextNode.generate_result_code)N)	rN   rO   rP   ru  r   r   rr   r   r   rJ   rJ   rJ   rK   r5    s   
r5  c               @   s6   e Zd ZdgZdZeZdZdd Zdd Z	dd	 Z
d
S )AsyncIteratorNoder  Tr$   c             C   s   t S )N)r   )rL   rc   rJ   rJ   rK   rr   Y  s    zAsyncIteratorNode.infer_typec             C   s6   | j || _ | j jjs2t| jd | j || _ | S )Nz*async for loops not allowed on C/C++ types)r  r   r   ra   r   rn   r=  )rL   rc   rJ   rJ   rK   r   \  s
    
zAsyncIteratorNode.analyse_typesc          	   C   sR   |j tdd |d|  | j ||  | j	f  |
|   d S )N	AsyncIterzCoroutine.cz)%s = __Pyx_Coroutine_GetAsyncIter(%s); %s)r  r  r   r  r   r   r  r   r   rn   r  )rL   r   rJ   rJ   rK   r   c  s    z&AsyncIteratorNode.generate_result_codeN)rN   rO   rP   r   r2  r   r   r   rr   r   r   rJ   rJ   rJ   rK   r9  L  s   r9  c               @   s4   e Zd ZeZdZdd Zdd Zdd Zdd	 Z	d
S )AsyncNextNoder$   c             C   s   t | |j || _d S )N)rg  ru  rn   r6  )rL   r6  rJ   rJ   rK   ru  w  s    zAsyncNextNode.__init__c             C   s   t S )N)r   )rL   rc   rJ   rJ   rK   rr   {  s    zAsyncNextNode.infer_typec             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   ~  s    zAsyncNextNode.analyse_typesc          	   C   sR   |j tdd |d|  | j ||  | j	f  |
|   d S )Nr:  zCoroutine.cz*%s = __Pyx_Coroutine_AsyncIterNext(%s); %s)r  r  r   r  r   r   r6  r   r   rn   r  )rL   r   rJ   rJ   rK   r     s    z"AsyncNextNode.generate_result_codeN)
rN   rO   rP   r   r   r   ru  rr   r   r   rJ   rJ   rJ   rK   r;  l  s   r;  c               @   s,   e Zd ZddgZdZdZdd Zdd ZdS )	WithExitCallNoder{   
await_exprTNc             C   s4   | j || _ | jr"| j|| _tj| _d| _| S )NT)r{   r   r=  r   rp  r   r   )rL   rc   rJ   rJ   rK   r     s    zWithExitCallNode.analyse_typesc             C   sv  | j r|d| jj  | j| |jjtdd}|	| j
 |jtdd |d|| jj| j f  |j| jjtd | j| | j| |||| j
 || | jr| jj||dd	 |d
|| j f  | j| | j| | jr,| | |d|  |f  |j|td | jrT|| j
|   |j| | j rr|d d S )Nz	if (%s) {F)r   PyObjectCallzObjectHandling.cz'%s = __Pyx_PyObject_Call(%s, %s, NULL);)r   T)source_cnamedecref_sourcez%s = %s;z%s = __Pyx_PyObject_IsTrue(%s);r   )test_if_runr   Z	with_statZexit_varr{   r  r   r   r   r   rn   r  r  r   r  r   r  r  r  r   r  r=  r   r	  r   r   r  r   )rL   r   Z
result_varrJ   rJ   rK   r    s<    



z)WithExitCallNode.generate_evaluation_code)rN   rO   rP   r   rA  r=  r   r  rJ   rJ   rJ   rK   r<    s
   r<  c               @   s8   e Zd ZeZdd Zdd Zdd Zdd Zd	d
 Z	dS )ExcValueNodec             C   s   t | | d S )N)r   ru  )rL   rn   rJ   rJ   rK   ru    s    zExcValueNode.__init__c             C   s
   || _ d S )N)var)rL   rC  rJ   rJ   rK   set_var  s    zExcValueNode.set_varc             C   s   | j S )N)rC  )rL   rJ   rJ   rK   r     s    z"ExcValueNode.calculate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r     s    z!ExcValueNode.generate_result_codec             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r     s    zExcValueNode.analyse_typesN)
rN   rO   rP   r   r   ru  rD  r   r   r   rJ   rJ   rJ   rK   rB    s   rB  c               @   sZ   e Zd Zg ZdddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdS )TempNodeNc             C   s(   t | | || _|jrt| _d| _d S )Nr$   )r   ru  r   ra   r   r   r   )rL   rn   r   rc   rJ   rJ   rK   ru    s
    zTempNode.__init__c             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r     s    zTempNode.analyse_typesc             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   r     s    z#TempNode.analyse_target_declarationc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r     s    zTempNode.generate_result_codec             C   s   |j j| jdd| _d S )NT)r   )r   r   r   
temp_cname)rL   r   rJ   rJ   rK   allocate  s    zTempNode.allocatec             C   s   |j | j d | _d S )N)r   r   rF  )rL   r   rJ   rJ   rK   release  s    zTempNode.releasec             C   s&   y| j S    dstd Y nX d S )NFz-Remember to call allocate/release on TempNode)rF  r   )rL   rJ   rJ   rK   r     s
    zTempNode.resultc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r     s    zTempNode.allocate_temp_resultc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   
  s    zTempNode.release_temp_result)N)rN   rO   rP   r   ru  r   r   r   rG  rH  r   r   r   rJ   rJ   rJ   rK   rE    s   	
rE  c               @   s   e Zd Zdd ZdS )
PyTempNodec             C   s   t | |tj| d S )N)rE  ru  r   r   )rL   rn   rc   rJ   rJ   rK   ru    s    zPyTempNode.__init__N)rN   rO   rP   ru  rJ   rJ   rJ   rK   rI    s   rI  c               @   s:   e Zd Zg ZdddZdd Zdd Zdd	 Zd
d ZdS )RawCNameExprNodeNc             C   s"   t j| ||d |d k	r|| _d S )N)r   )r   ru  r   )rL   rn   r   r   rJ   rJ   rK   ru    s    zRawCNameExprNode.__init__c             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r     s    zRawCNameExprNode.analyse_typesc             C   s
   || _ d S )N)r   )rL   r   rJ   rJ   rK   	set_cname  s    zRawCNameExprNode.set_cnamec             C   s   | j S )N)r   )rL   rJ   rJ   rK   r   !  s    zRawCNameExprNode.resultc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   $  s    z%RawCNameExprNode.generate_result_code)NN)	rN   rO   rP   r   ru  r   rK  r   r   rJ   rJ   rJ   rK   rJ    s   
rJ  c               @   s2   e Zd ZeZdZdgZdd Zdd Zdd Z	d	S )
JoinedStrNodeTvaluesc                s    fdd| j D | _ | S )Nc                s   g | ]}|   qS rJ   )r   r=  )rS   v)rc   rJ   rK   rt   :  s    z/JoinedStrNode.analyse_types.<locals>.<listcomp>)rM  )rL   rc   rJ   )rc   rK   r   9  s    zJoinedStrNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  =  s    zJoinedStrNode.may_be_nonec             C   s  | | j t| j}|jjtdd}|jjtjdd}|jjtj	dd}|
d||||| jf  || |
d|  |
d|  x|t| jD ]l\}}|| || d|  }d|  }	d}
t|try$|jd	 d
}	|jd d}
W nl tk
rr   |	d
krnttt|j}|dk rPd}	tt|j}n|dkrnd}	tt|j}n Y nX tt|j}nt|tr|jjjrd}
|
s|
d||	||	|f  |
d||f  ||  |
d||| f  || || qW | | j |  | |j!"t#$dd |
d| % ||||||  | jf  ||   |&|t |j'| |j'| |j'| d S )NT)r   Fz%s = PyTuple_New(%s); %sz%s = 0;z	%s = 127;z__Pyx_PyUnicode_GET_LENGTH(%s)z"__Pyx_PyUnicode_MAX_CHAR_VALUE(%s)z	iso8859-1Z255zus-asciii   Z65535i   Z1114111z%s = (%s > %s) ? %s : %s;z	%s += %s;zPyTuple_SET_ITEM(%s, %s, %s);ZJoinPyUnicodezStringTools.cz-%s = __Pyx_PyUnicode_Join(%s, %d, %s, %s); %s)(r   rn   rw   rM  r   r   r   r   r   c_py_ucs4_typer   r   r  	enumerater  r   r   rk   r  r   encodeUnicodeEncodeErrormaxmaprt  rg   FormattedValueNoder   r  r  r	  r  r   r  r  r   r  r   r  r   )rL   r   Z	num_itemsZlist_varZulength_varZmax_char_varirp   ZulengthZmax_char_valueZis_asciiZmax_charrJ   rJ   rK   r  A  sx    








z&JoinedStrNode.generate_evaluation_codeN)
rN   rO   rP   r   r   r   r   r   rN  r  rJ   rJ   rJ   rK   rL  /  s   rL  c               @   sF   e Zd ZddgZeZdZdZddddjZ	d	d
 Z
dd Zdd ZdS )rU  r   format_specTNZPyObject_UnicodeZPyObject_ReprZPyObject_ASCII)srac             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN    s    zFormattedValueNode.may_be_nonec             C   s   | j || _ | jr| jjrL| jr*| jj n| j jj}| j jj||drL|| _| jrf| j||| _| jd kr| j || _ | js| j	r| j	dkr| j jt
kr| j  s| j S | S )N)rW  rX  )r   r   rW  r[  r   Zdefault_format_specZcan_coerce_to_pystringc_format_specr=  conversion_charr   rN  )rL   rc   r[  rJ   rJ   rK   r     s    
z FormattedValueNode.analyse_typesc       	      C   sz  | j d k	rf| jjjsf| jj| j || j }|d|  |||  | jf  |	| 
  d S | j
 }| jjtko| j  }| jrd}| j
 }n
d}tj}| j}|dkr|rd }|r| |}|d k	std| d||f }|jtdd |d	7 }n2| jr*|jtd
d n|jtdd |d|  |||||  | jf  |	| 
  d S )Nz%s = %s; %sZ__Pyx_PyObject_FormatZ__Pyx_PyObject_FormatSimplerX  z(invalid conversion character found: '%s'z%s(%s)ZPyObjectFormatAndDecrefzStringTools.cZ	AndDecrefZPyObjectFormatZPyObjectFormatSimplez%s = %s(%s, %s); %s)r[  r   r   ra   Zconvert_to_pystringr   r   r   rn   r  r   r   rN  rW  r   Zempty_unicoder\  find_conversion_funcr   r  r  r   r  )	rL   r   Zconvert_func_callZvalue_resultZvalue_is_unicodeZformat_funcrW  r\  fnrJ   rJ   rK   r     sL    


z'FormattedValueNode.generate_result_code)rN   rO   rP   r   r   r   r   r[  rZ   r]  rN  r   r   rJ   rJ   rJ   rK   rU    s   
rU  c               @   s.   e Zd ZdZejZdd Zdd Zdd Z	dS )	ParallelThreadsAvailableNodea*  
    Note: this is disabled and not a valid directive at this moment

    Implements cython.parallel.threadsavailable(). If we are called from the
    sequential part of the application, we need to call omp_get_max_threads(),
    and in the parallel part we can just call omp_get_num_threads()
    c             C   s
   d| _ | S )NT)r   )rL   rc   rJ   rJ   rK   r     s    z*ParallelThreadsAvailableNode.analyse_typesc             C   sR   | d | d| j  | d| j  | d | d| j  | d d S )Nz#ifdef _OPENMPz2if (omp_in_parallel()) %s = omp_get_max_threads();z else %s = omp_get_num_threads();z#elsez%s = 1;z#endif)r   r   )rL   r   rJ   rJ   rK   r      s    


z1ParallelThreadsAvailableNode.generate_result_codec             C   s   | j S )N)r   )rL   rJ   rJ   rK   r   	  s    z#ParallelThreadsAvailableNode.resultN)
rN   rO   rP   __doc__r   
c_int_typer   r   r   r   rJ   rJ   rJ   rK   r_    s
   	r_  c               @   s.   e Zd ZdZejZdd Zdd Zdd Z	dS )	ParallelThreadIdNodez/
    Implements cython.parallel.threadid()
    c             C   s
   d| _ | S )NT)r   )rL   rc   rJ   rJ   rK   r     s    z"ParallelThreadIdNode.analyse_typesc             C   sB   | d | d| j  | d | d| j  | d d S )Nz#ifdef _OPENMPz%s = omp_get_thread_num();z#elsez%s = 0;z#endif)r   r   )rL   r   rJ   rJ   rK   r     s
    

z)ParallelThreadIdNode.generate_result_codec             C   s   | j S )N)r   )rL   rJ   rJ   rK   r      s    zParallelThreadIdNode.resultN)
rN   rO   rP   r`  r   ra  r   r   r   r   rJ   rJ   rJ   rK   rb    s
   rb  c               @   s$   e Zd Zdd Zdd Zdd ZdS )_IndexingBaseNodec             C   s    | j  p| j jtttttfkS )N)r   r   r   r-   r   r   r    r   )rL   rJ   rJ   rK   r   0  s    z_IndexingBaseNode.is_ephemeralc             C   s   | j  o| j S )N)r   r   r   r   )rL   rJ   rJ   rK   r   6  s    z"_IndexingBaseNode.check_const_addrc             C   s(   | j jr| j jjr$dS n| j jr$dS dS )NFT)r   r  r  r   r  )rL   rJ   rJ   rK   r   9  s    
z_IndexingBaseNode.is_lvalueN)rN   rO   rP   r   r   r   rJ   rJ   rJ   rK   rc  +  s   rc  c               @   s  e Zd ZddgZdZdZdZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zd=ddZdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd>d*d+Zd,d- Zd.Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd?d7d8Z d9d: Z!d@d;d<Z"dS )Ar   r   r   NTFc             C   s   | j j| jj | _d S )N)r   rl   r   )rL   rJ   rJ   rK   r   Y  s    z#IndexNode.calculate_constant_resultc          
   C   sR   | j |}| j|}y|| S  tk
rL } z| | W d d }~X Y nX d S )N)r   r   r   	Exceptionr   )rL   r   r   r   r   rJ   rJ   rK   r   \  s    zIndexNode.compile_time_valuec             C   s.   | j }| o,| j o,|jo,|jjp,|jjS )N)r   rM  r   r   r  r   )rL   r   rJ   rJ   rK   rM  d  s    zIndexNode.is_simplec             C   sF   | j j}|r<|jrdS t| jtr<|tttt	t
ttfkr<dS t| S )NF)r   r   r  rk   r   	SliceNoder   r    r   r   r-   r   r   r   rN  )rL   r  rJ   rJ   rK   rN  i  s    
zIndexNode.may_be_nonec             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   r   u  s    z$IndexNode.analyse_target_declarationc          	   C   s  | j |}|r
|js
|jr^t| jtr6| jj}n| jg}tj	| j
|d d}|j||dS | jjsn| jjrddlm} ||j | jjr| jgn
t| jj}t||||S | j|}|d k	 ryt|}W n ttfk
r   Y nX t||S t| j
d d S )N)rn   positional_argskeyword_args)r  r$   )r  z*Array size must be a compile time constant)r   r   ra   r0  rk   r   	TupleNoder{   r   ZTemplatedTypeNodern   analyser   ru   r  r  r  view_utility_coder   r   MemoryViewSliceTypeget_axes_specsr   ro  r|   r}   
CArrayTyper   )rL   rc   r  Ztemplate_valuesZ	type_noder  axesr   rJ   rJ   rK   r   x  s2    

zIndexNode.analyse_as_typec             C   s   | j || j| S )N)r   r   r   )rL   rc   rJ   rJ   rK   r     s    zIndexNode.type_dependenciesc       
      C   s  | j |}| jjrD|jrtS |jr(tS |tttt	t
tfkr@|S tS | j|}|rZ|jsht| jtrF|tkrvtjS |tkr|S |t	krtjS t| j trtS |tt
fkrt|| j | j|d}|d k	r|S nx|js|jr|jS |jrFt| jtrF| j rF| jj}|dk r||j7 }d|  kr8|jk rFn n
|j| S |jrG dd d}|| j|d|| j|dg}|d|}|d k	r|j j!S t"|rt"|r| j|f}	t#t$||	gS |ttfkr|S tS d S )N)r   r   c               @   s   e Zd Zdd ZdS )z)IndexNode.infer_type.<locals>.FakeOperandc             [   s   | j | d S )N)__dict__update)rL   rw  rJ   rJ   rK   ru    s    z2IndexNode.infer_type.<locals>.FakeOperand.__init__N)rN   rO   rP   ru  rJ   rJ   rJ   rK   FakeOperand  s   rq  )rn   r   z[])%r   rr   r   r   r  r   r  r   r   r    r   r   r   r+  rk   r   r   rO  c_uchar_typer  r   r  r   r  rG  rz   rl   r  rH  r0  rn   lookup_operatorr   r  r5   rA   r;   )
rL   rc   r  
index_typer8  r   rq  operandsZ
index_funcZindex_with_typerJ   rJ   rK   rr     s^    




zIndexNode.infer_typec             C   s   | j |ddS )NT)getting)analyse_base_and_index_types)rL   rc   rJ   rJ   rK   r     s    zIndexNode.analyse_typesc             C   sH   | j |dd}|jjr"t| jd || krD| sDt| jd|j  |S )NT)settingzAssignment to const dereferencez%Assignment to non-lvalue of type '%s')rw  r   r  r   rn   r   )rL   rc   rp   rJ   rJ   rK   r     s    zIndexNode.analyse_target_typesc       	      C   s  |r| j || _ | j jjr(tj| _| S | jj}|jd s\|rRt	| jj
| jj n
t	| j |st| jtrt| jjr| j|| _| j jj}|st| j ts|r| j jjs| j jjs| j jjs| j || _ | ||}|d k	r|S |j| _| j j}|jsd| j|| _| jj| _|jrd|r:t| jddd n| jjdkrN| j S | j || _ | j j}|jr||  ||||S |js|jr| !||S |j"r| #||S |jr| $|S |j%r| &|||S t'| jd|  tj| _| S d S )N
wraparoundz%cannot assign to Unicode string indexr$   )rj   )r   r  z'Attempting to index non-array type '%s')(r   r   r   r&  r   r*   r   r   r[   rq   r   r   rk   r   r   Zlong_literalr   r=  r   r  r  r  r   analyse_as_buffer_operationr   r   original_index_typer  r   rn   rl   ra   analyse_as_pyobjectanalyse_as_c_arrayr0  analyse_as_cppanalyse_as_c_functionrG  analyse_as_c_tupler   )	rL   rc   rv  rx  analyse_baser   Zis_memslicereplacement_noder  rJ   rJ   rK   rw    s\    





z&IndexNode.analyse_base_and_index_typesc             C   s  | j j}| jjjr@|tk	r@t| jddd | j|| _d| _n| jjj	r|tk	r|r|t
ttfkr| jjjr|jd rt| jtr| j r| jjdkr|jd sd| _nd| _| jtj||| _| j| n| j|| _d| _| jjj	r|tkrtj| _n| jjj	r<|tkr<|r2tj| _ntj| _n|r`|ttttt
tfkr`|| _ndd }|t
tfkr| jjj	rt|| j | j|d}|d krt }|| _|t
ttfkr| j !d| _ | "|| | S )	NzItem lookup of unicode character codes now always converts to a Unicode string. Use an explicit C integer cast to get back the previous integer lookup behaviour.r$   )rj   ry  r   boundscheck)r   z&'NoneType' object is not subscriptable)#r   r   r   r  r   r   rn   r=  r   r+  r   r   r    signedr[   rk   r   rz   rl   r"  r   r   r   r{  create_to_py_utility_coder   rO  rr  ra  r   r   r   r   rS  wrap_in_nonecheck_node)rL   rc   r   rv  rx  r  r8  rJ   rJ   rK   r|  =  sP    






zIndexNode.analyse_as_pyobjectc             C   s\   | j j}|j| _|r|| _n<| jjjr:| jtj|| _n| jjjsXt	| j
d| jj  | S )NzInvalid index type '%s')r   r   r  r   ra   r"  r   r   r+  r   rn   )rL   rc   r   r  rJ   rJ   rK   r}  r  s    

zIndexNode.analyse_as_c_arrayc             C   s   | j j}|d| j | jg}|d krNt| jd|| jjf  tj| _d| _| S |j}|j	r`|j
}|j| _|j| _| jr|sd| _| jd kr|tdd | j|jd j|| _|j| _|r|jjst| jd| j  | S )	Nz[]z/Indexing '%s' not supported for index type '%s'z<error>TCppExceptionConversionzCppSupport.cppr   z#Can't set non-reference result '%s')r   r   rs  r   r   rn   r   r*   r  r  r  r  r   r   r  r   r  r"  r{   r  r  )rL   rc   rx  r  r@  	func_typerJ   rJ   rK   r~  }  s,    
zIndexNode.analyse_as_cppc             C   s   | j j}|jr| | n| || _d | _|jd krJt| j	d t
| _nh| jd kr\t
| _nVt|jt| jkrt| j	dt|jt| jf  t
| _n|tt|j| j| _| S )Nz)Can only parameterize template functions.z7Wrong number of template arguments: expected %s, got %s)r   r   r  parse_indexed_fused_cdefparse_index_as_typestype_indicesr   Z	templatesr   rn   r*   rw   
specializedictzip)rL   rc   r  rJ   rJ   rK   r    s     

zIndexNode.analyse_as_c_functionc             C   s   | j j}t| jtr| j r| jj}|j |  kr@|jk rdn n |dk rV||j7 }|j| | _nt	| j
d||f  tj| _| S | j || _ | j|||ddS d S )Nr   zIndex %s out of bounds for '%s'F)rv  rx  r  )r   r   rk   r   r   rz   rl   r  rH  r   rn   r   r*   r=  rw  )rL   rc   rv  rx  r  r   rJ   rJ   rK   r    s    
zIndexNode.analyse_as_c_tuplec                s  t | jtr| jj}n| jg}| j}|j}d}|jrddlm} |j	rh|
|}|dk	rh|j}|j}|}|||j\}	}}
|	rt| j||d}nt| j||d}n|js|jr|jst||jkrd} fdd|D }|jrJtdd	 |D }|r\x\t|D ]<\}}|jrt|j|j|j|jd
}| }|||< qW ntdd	 |D }|rt| j||d}t | jtrt|dk	r| |}|S )zI
        Analyse buffer indexing and memoryview indexing/slicing
        Nr$   )r  )indicesr   Tc                s   g | ]}|  qS rJ   )r   )rS   r   )rc   rJ   rK   rt     s    z9IndexNode.analyse_as_buffer_operation.<locals>.<listcomp>c             s   s$   | ]}|j jp|jp|j jV  qd S )N)r   r+  r   r5   )rS   r   rJ   rJ   rK   rV     s   z8IndexNode.analyse_as_buffer_operation.<locals>.<genexpr>)r   r   r   c             s   s   | ]}|j jV  qd S )N)r   r+  )rS   r   rJ   rJ   rK   rV     s    )rk   r   rh  r{   r   r   r   r  r  r`  merged_indicesunellipsifyndimMemoryViewSliceNodern   MemoryViewIndexNoder  r5   rw   allrP  r   SliceIntNoder   r   r   r   BufferIndexNode	CloneNoder   )rL   rc   rv  r  r   r  r  r  r  have_slicesnewaxesr^  Zdo_replacementrV  r   rJ   )rc   rK   rz    sN    




z%IndexNode.analyse_as_buffer_operationc             C   s*   |j d r| j sd S | jd| _d S )N	nonecheckz&'NoneType' object is not subscriptable)r[   r   rN  rS  )rL   rc   rv  rJ   rJ   rK   r    s    z IndexNode.wrap_in_nonecheck_nodec             C   sd   t | jtr| jj}n| jg}g }x<|D ]4}||| |d d kr(|rXt|jd d S q(W |S )Nr  znot parsable as a type)rk   r   rh  r{   r   r   r   rn   )rL   rc   requiredr  r  r   rJ   rJ   rK   r    s    

zIndexNode.parse_index_as_typesc                sZ  t j| _d| _| jj}g }| jjs*| jjr:|| jj	 n*t
| jtrdx| jjD ]}||j	 qPW | j|dd}|dkr| j|| _| jjjst| j	d n<| jjj | j_| _| jj | j_| _d| j_d| _d| j_d| _dS x"t|D ]\}}||||< qW | }t|t|kr0t| j	dS t|t|k r^|t| }	t| j	d|	 S xVt|||D ]F\}
 }t fdd	|jD st|
d
S  dks jrldS qlW tt||}||}|jrt| j	d nrxp| jj D ]X}| |r|| _| jjr*|j| _d| _| jj!| _!d| jj_|| j_|j| j_P qW t"ddS )aK  
        Interpret fused_cdef_func[specific_type1, ...]

        Note that if this method is called, we are an indexed cdef function
        with fused argument types, and this IndexNode will be replaced by the
        NameNode with specific entry just after analysis of expressions by
        AnalyseExpressionsTransform.
        TF)r  Nz)Can only index fused functions with typeszToo many types specifiedzHNot enough types specified to specialize the function, %s is still fusedc                s   g | ]}  |qS rJ   )r  )rS   rT   )specific_typerJ   rK   rt   =  s    z6IndexNode.parse_indexed_fused_cdef.<locals>.<listcomp>zType not in fused typez6Index operation makes function only partially specificz!Couldn't find the right signature)#r   r*   r   is_fused_indexr   r   r  r\  r   rn   rk   rh  r{   r  r   r   r  r   r   r  rP  Zspecialize_fusedZget_fused_typesrw   r  r\   typesr&  r  r  r  r  r  r>  r   )rL   rc   r  Z	positionsr  Zspecific_typesrV  r   Zfused_typesrT   rn   Z
fused_typefused_to_specificr8  rJ   )r  rK   r    sl    	







z"IndexNode.parse_indexed_fused_cdefzIndexing Python objectc             C   s  | j jtttfkr^| j jtkr$d}q| j jtkr6d}q| j jtkrHd}qdstd| j j n| j jjrd| j  ddd	 | j	D f S | j jj
r| jj}|d
k r|| j jj7 }d| j  |f S | jjs| jjr| j| j jkrt| jd d S d}|| j  | j f S )NzPyList_GET_ITEM(%s, %s)zPyTuple_GET_ITEM(%s, %s)z0((unsigned char)(PyByteArray_AS_STRING(%s)[%s]))Fz$unexpected base type in indexing: %sz%s<%s>,c             S   s   g | ]}|  qS rJ   )r  )rS   ZparamrJ   rJ   rK   rt   o  s    z3IndexNode.calculate_result_code.<locals>.<listcomp>r   z%s.f%szInvalid use of pointer slicez(%s[%s]))r   r   r   r   r    r   r   r   joinr  rG  r   rl   r  r  r   r   rn   )rL   
index_coder   rJ   rJ   rK   r   b  s,    

zIndexNode.calculate_result_codec             C   s   | j jjr| jjtk}t|jjd oH| jj	oHt
| j jtoF| j jdk }t|jjd }d| j | jj	rpdprd| jj|||f S dS d S )Nry  r   r  z, %s, %d, %s, %d, %d, %dr$   r  )r   r   r+  r   r   r   r  r[   r{  r  rk   rl   r"   r  Zto_py_function)rL   r   Zis_listry  r  rJ   rJ   rK   extra_index_params|  s    
zIndexNode.extra_index_paramsc             C   sz  | j s
d S d }| jjrd}| jjjrZ| jjtkr6d}n| jjtkrHd}nd}t	dd}np| jjt
krxd}t	dd}nR| jjtkr| jjttfkrd	}t	dd}n$d
}|jt	dd t	dd}n| jjr| jjtkr| jjjstd}d}t	dd}nr| jjtkrJ| jjjs&t| jjs4td}d}t	dd}n0| jjjr^| jszdsztd| j| jjf |d k	r|j| | jjjr| j }n
| j }| jjjr| jrt|| jd|  | j | j f | jjr|  nd | j| j nR|dkrdnd| }|d|  || j || ||||   | jf  | jjrv||   d S )Nrr  Z__Pyx_GetItemInt_ListZ__Pyx_GetItemInt_TupleZ__Pyx_GetItemIntZ
GetItemIntzObjectHandling.cZ__Pyx_PyDict_GetItemZDictGetItemZ__Pyx_PyObject_Dict_GetItemZ__Pyx_PyObject_GetItemZObjectGetItemZ__Pyx_GetItemInt_Unicodez(Py_UCS4)-1ZGetItemIntUnicodezStringTools.cZ__Pyx_GetItemInt_ByteArrayz-1ZGetItemIntByteArrayFz0unexpected type %s and base type %s for indexingz%s = %s[%s];z!%sz	%%s == %sz%s = %s(%s, %s%s); %s) r   r   ra   r   r+  r   r   r   r   r  r   r   r   r   r   r  r  r  r   r    r0  r  r   r   r   rn   r   r   r   r  r   r  )rL   r   r  Zerror_valuer@  r  Zerror_checkrJ   rJ   rK   r     st    




zIndexNode.generate_result_codec             C   s   | j jjrT| jjtkr0|jtdd d}n|jtdd d}| j 	 }n | j 
 }| jjtkrpd}nd}||d	|| j
 ||| |f | j d S )
NZSetItemIntByteArrayzStringTools.cZ__Pyx_SetItemInt_ByteArrayZ
SetItemIntzObjectHandling.cZ__Pyx_SetItemIntr  ZPyObject_SetItemz%s(%s, %s, %s%s))r   r   r+  r   r    r  r  r   r  r   r   r   r   r+  r  rn   )rL   
value_coder   r@  r  rJ   rJ   rK   generate_setitem_code  s*    

	zIndexNode.generate_setitem_codec          	   C   s  |  | | jjr$| | | n| jjtkrJ| ||}| || n| jjjr| j	r| j	dkr|r|r| j
|krt|| j| j|  | | j
|| j qt|| jd|  | f | jjr|  nd | j
| j n|d|  | f  | | | | || || d S )Nr  z%s = %s;)r   r   ra   r  r   r   r    _check_byte_valuer0  r  r   r   rn   r   r   r   r   r  r  r  r  )rL   rx   r   r
  r  r   r  rJ   rJ   rK   r    s,    




z"IndexNode.generate_assignment_codec             C   s  |j jstt|j | }| r`d|j  kr<dk rDn n|S d}t|jd|j dd n|j t	j
k}| jsg }|js|j jr|d|  |js|jr|j t	j
t	jt	jfks|d|  |r|d	d
|  |d|| j  |d |rd| }|S )Nr      Tz9value outside of range(0, 256) when assigning to byte: %sr$   )rj   z%s < 0z%s > 255zif (unlikely(%s)) {z || zFPyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); %sr   z((unsigned char)%s))r   r+  r   r  r   rz   rl   r   rn   r   rr  r   r   r  r   r   r   c_schar_typer   r  r   )rL   r   rx   r  Z
needs_castZ
conditionsrJ   rJ   rK   r    s:    



zIndexNode._check_byte_valuec             C   s   |  | | jjjr8d}| j }|jtdd n | j	 }| j
jtkrTd}nd}||d|| j
	 || |f | j | | | | d S )NZ__Pyx_DelItemIntZ
DelItemIntzObjectHandling.cZPyDict_DelItemZPyObject_DelItemz%s(%s, %s%s))r   r   r   r+  r   r  r  r   r  r   r   r   r   r+  r  rn   r  r  )rL   r   r  r@  r  rJ   rJ   rK   r  ;  s&    





z IndexNode.generate_deletion_code)FFT)T)FNN)F)#rN   rO   rP   r   r  r]  r  r   r   rM  rN  r   r   r   rr   r   r   rw  r|  r}  r~  r  r  rz  r  r  r  r  r   r  r   r  r  r  r  rJ   rJ   rJ   rK   r   I  sD   
 Q 
E55
[G! 
!$r   c               @   s   e Zd ZdZddgZdZdZdd Zd"dd	Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zd#ddZd$ddZdd  Zd!S )%r  z
    Indexing of buffers and memoryviews. This node is created during type
    analysis from IndexNode and replaces it.

    Attributes:
        base - base node being indexed
        indices - list of indexing expressions
    r   r  TFc             C   s   | j |dd d S )NF)rv  )r   )rL   rc   rJ   rJ   rK   r   e  s    z$BufferIndexNode.analyse_target_typesc             C   s   | j js*t| j js*t| jd t| _| S |sd| j jjjsHt| jd nd| _	| j jj
rdd| j jj_	d| _| || | | | S )zz
        Analyse types for buffer indexing only. Overridden by memoryview
        indexing and slicing subclasses
        zCan only index buffer variableszWriting to readonly bufferTz&'NoneType' object is not subscriptable)r   r  r5   r   r   rn   r*   r   writabler'  r  Z
buffer_auxnone_error_messageanalyse_buffer_indexr  )rL   rc   rv  rJ   rJ   rK   r   h  s    

zBufferIndexNode.analyse_typesc             C   st   t | jjr2dd | jD }tt| jj|| _n| j|| _| jjj| _| jj| _|rp| jj	sj| jj rpd| _
d S )Nc             S   s   g | ]}||j fqS rJ   )r   )rS   idxrJ   rJ   rK   rt     s    z8BufferIndexNode.analyse_buffer_index.<locals>.<listcomp>T)r5   r   r   r  rA   r;   r   r*  buffer_typera   r   )rL   rc   rv  Zindex_with_type_listrJ   rJ   rK   r    s    
z$BufferIndexNode.analyse_buffer_indexc             C   s   dS )zk
        Called by IndexNode when this node is assigned to,
        with the rhs of the assignment
        NrJ   )rL   rx   rJ   rJ   rK   analyse_assignment  s    z"BufferIndexNode.analyse_assignmentc             C   s,   |j d r| j sd S | j| j| _d S )Nr  )r[   r   rN  rS  r  )rL   rc   rJ   rJ   rK   r    s    z&BufferIndexNode.wrap_in_nonecheck_nodec             C   s*   | j s| jr&| jjr&t| jd t| _d S )Nz2Cannot access buffer with object dtype without gil)r^  r_  r   ra   r   rn   r*   )rL   rc   rJ   rJ   rK   r     s    zBufferIndexNode.nogil_checkc             C   s
   d| j  S )Nz(*%s))buffer_ptr_code)rL   rJ   rJ   rK   r     s    z%BufferIndexNode.calculate_result_codec             C   s    | j }| j jr|j}|j|S )N)r   is_nonecheckr  r   Z	get_entry)rL   r   rJ   rJ   rK   buffer_entry  s    zBufferIndexNode.buffer_entryc             C   sD   |j jt|j|jjrtjntjdd}|d||	 f  |S )NF)r   z%s = %s;)
r   r   r   r|  r   r  Zc_ssize_t_typec_size_t_typer   r   )rL   r   ivarZretrJ   rJ   rK   get_index_in_temp  s    z!BufferIndexNode.get_index_in_tempc                s   j r.jsjr. jjd r.tjddd  fddjD }ddlm	} 
 }|jjrj|jj}n
|jd }||j|d	d jD | jjj |j d
fS )z8
        ndarray[1, 2, 3] and memslice[1, 2, 3]
        r  z(Use boundscheck(False) for faster accessr$   )rj   c                s   g | ]}  |qS rJ   )r  )rS   r  )r   rL   rJ   rK   rt     s    z6BufferIndexNode.buffer_lookup_code.<locals>.<listcomp>)r  negative_indicesc             S   s   g | ]}|j jqS rJ   )r   r  )rS   r  rJ   rJ   rK   rt     s    )r   Zindex_signedsZindex_cnamesr[   rn   r   r  r   )r   r^  r_  r  r[   r   rn   r  r  r  r  r   r  r  Zbuffer_defaultsZput_buffer_lookup_code)rL   r   Zindex_tempsr  r  r  rJ   )r   rL   rK   buffer_lookup_code  s$    

z"BufferIndexNode.buffer_lookup_codec             C   sB   |  | | || | | | | || || d S )N)r   generate_buffer_setitem_coder  r  r  r  )rL   rx   r   r
  rJ   rJ   rK   r    s    



z(BufferIndexNode.generate_assignment_coder  c       
      C   s4  | j j}t|rt|jr|jjtt| j jdd}|d|  |d||| j 	 f  |d|t
| j||	 f  d S | |\}}| jjjr|jj|jdd}| }	|d||f  |d|  |d|	|f  |d	|||	f  |d|  |j| n|d	||| f  d S )
NF)r   z__Pyx_call_destructor(%s);znew (&%s) decltype(%s){%s};z%s%s %s= %s;z%s = %s;z*%sz$__Pyx_INCREF(%s); __Pyx_DECREF(*%s);z*%s %s= %s;)r   r   r5   r3   r   r   rA   r=   r   r   r:   r  r  r  r*  ra   Zbuf_ptr_typer   r  r  r   )
rL   rx   r   opr  r>  r  Zptrexprptrr   rJ   rJ   rK   r    s0    	
z,BufferIndexNode.generate_buffer_setitem_codec             C   s   t | jjrH|  }|d|  |d||| j t| jf  d S | |\}| _	| jj
r|d|  | j	f  |d|    d S )Nz__Pyx_call_destructor(%s);znew (&%s) decltype(%s){%s%s};z%s = (PyObject *) *%s;z__Pyx_INCREF((PyObject*)%s);)r5   r   r   r   r   r   r:   r  r  r  ra   )rL   r   Zresr  rJ   rJ   rK   r     s    z$BufferIndexNode.generate_result_codeN)T)F)r  )rN   rO   rP   r`  r   r^  r'  r   r   r  r  r  r   r   r  r  r  r  r  r   rJ   rJ   rJ   rK   r  T  s"   
	

)r  c               @   s:   e Zd ZdZdZdZdddZdd Zdd Zd	d
 Z	dS )r  TFc             C   sn  ddl m} t|| _| j}||| jjj\}}}|sZd| _	| jj
sN| jjrZd| jjj_	| opt|| jjjk| _g }tj}g }	t|t| | jjjkrt| _t|| jjj jd| jj  | S d}
x`t|d d  D ]J\}}||}|jrd| _|	| |d q| jjj|
 \}}|
d7 }
|jrd| _|jjrV|||f n||df xdD ]:}t||}|jsj|||}t||| |	| qjW q|jjs|jj r|jj r| j!st"|jd	d
d dt#_!d| _$|||}|||< |	| qt| _t|jd|j  | S qW | j$o:| j | _$|	| _|| _%|j&| _&| '||| | (| | S )Nr$   )r  Tz&Too many indices specified for type %sr   )directstridedr  )r   r   r   z/Index should be typed for more efficient accessr.   )rj   z/Invalid index for memoryview specified, type %s))r  r  r9   Zis_pythran_moder  r  r   r   r  r'  r  r\  r   rw   Zmemslice_indexr   r   r*   r   rn   rP  r   rk  r`  r   rn  r   r   r   r"  setattrr+  ra   warned_untyped_idxr   r  r_  original_indicesr   analyse_operationr  )rL   rc   rv  r  r  r  r  rn  rt  new_indicesZaxis_idxrV  r   accesspackingattrr   rJ   rJ   rK   r     sp    







z!MemoryViewIndexNode.analyse_typesc             C   s   d| _ | || d S )Nz"Cannot index None memoryview slice)r  r  )rL   rc   rv  rn  rJ   rJ   rK   r  f  s    z%MemoryViewIndexNode.analyse_operationc             C   s(   | j jr$| }|js|jr$d|_d|_dS )z
        Support broadcasting for slice assignment.
        E.g.
            m_2d[...] = m_1d  # or,
            m_1d[...] = m_2d  # if the leading dimension has extent 1
        TN)r   r   r(  )rL   rx   lhsrJ   rJ   rK   analyse_broadcast_operationj  s
    z/MemoryViewIndexNode.analyse_broadcast_operationc             C   s   |  |}|r|j|_|S | S )N)r  r)  )rL   rx   r  rJ   rJ   rK   $analyse_as_memview_scalar_assignmentw  s
    
z8MemoryViewIndexNode.analyse_as_memview_scalar_assignmentN)T)
rN   rO   rP   r_  r^  r  r   r  r  r  rJ   rJ   rJ   rK   r    s   
Pr  c               @   sb   e Zd ZdZdZdZdZdZdd Zdd Z	dd Z
d	d
 Zdd Zdd Zdd ZdddZdS )r  TFc             C   s*   t dd | jD | _| jr&| jj| _dS )z@Slicing operations needing no evaluation, i.e. m[...] or m[:, :]c             s   s,   | ]$}|j o"|jjo"|jjo"|jjV  qd S )N)r   r   rk  r   r   )rS   r   rJ   rJ   rK   rV     s   z<MemoryViewSliceNode.analyse_ellipsis_noop.<locals>.<genexpr>N)r  r  is_ellipsis_noopr   r   )rL   rc   rv  rJ   rJ   rK   analyse_ellipsis_noop  s
    z)MemoryViewSliceNode.analyse_ellipsis_noopc             C   s   ddl m} |sd| _d| _nd| _| || | jr:d S d | _d| _d| _|	| j
|sdt| _d S t| jjj|| _| j s| j s| j|| _d S )Nr$   )r  Tz&Cannot assign to None memoryview slicez"Cannot slice None memoryview slice)r  r  r(  r  r  r  r   r   r   Zvalidate_axesrn   r*   r   r   rk  r   r*  rM  r   rL  )rL   rc   rv  rn  r  rJ   rJ   rK   r    s"    z%MemoryViewSliceNode.analyse_operationc             C   s<   |j js,| j j|j s |j jr,t| j| S t| j| S d S )N)r   r   r*  r3  ra   MemoryCopyScalarrn   MemoryCopySlice)rL   rx   rJ   rJ   rK   r    s
    z&MemoryViewSliceNode.analyse_assignmentc             C   s   |sdS | j dd }|dd }xft| j D ]X\}}|jrz|jjrt|jjrt|jjrt|d ||< |d |sx|S qdS q.|jj	s.dS q.W |rt
|t
| | jjjkrdS ||7 }|S )a  Return a new list of indices/slices with 'indices' merged into the current ones
        according to slicing rules.
        Is used to implement "view[i][j]" => "view[i, j]".
        Return None if the indices cannot (easily) be merged at compile time.
        Nr   )r  rP  r   r   rk  r   r   r   r   r+  rw   r   r  )rL   r  r  rV  rX  rJ   rJ   rK   r    s&    
z"MemoryViewSliceNode.merged_indicesc             C   s"   | j r| j p| j S |  S )N)r  r   rM  r   )rL   rJ   rJ   rK   rM    s    zMemoryViewSliceNode.is_simplec             C   s
   | j  S )z3This is called in case this is a no-op slicing node)r   r   )rL   rJ   rJ   rK   r     s    z)MemoryViewSliceNode.calculate_result_codec             C   s   | j r
d S |  }| j }d}t| j}xX| jD ]N}|jrvd}|jjsPt	||_|j
jsbt	||_
|jjs~t	||_q0t	| q0W t|rt|j|| j|  |||jjd d S )NFT)r   r  r[   )r  r  r   iterr  r  r   r   rk  nextr   r   r   r   Zgenerate_buffer_slice_coder   r  r[   )rL   r   r  r   r  r  r   rJ   rJ   rK   r     s*    


z(MemoryViewSliceNode.generate_result_codec             C   sp   | j r| | n
| | | jr0| || n| || | j rN| | n
| | || || d S )N)	r  r   r  is_memview_scalar_assignmentZ+generate_memoryviewslice_assign_scalar_codeZ&generate_memoryviewslice_setslice_coder  r  r  )rL   rx   r   r
  rJ   rJ   rK   r    s    


z,MemoryViewSliceNode.generate_assignment_codeN)F)rN   rO   rP   r`  r  r  r_  r(  r  r  r  r  rM  r   r   r  rJ   rJ   rJ   rK   r    s   
	r  c                   s0   e Zd ZdZdgZ fddZdddZ  ZS )	MemoryCopyNodeze
    Wraps a memoryview slice for slice assignment.

        dst: destination mememoryview slice
    dstc                s"   t t| | || _|j| _d S )N)rF   r  ru  r  r   )rL   rn   r  )rI   rJ   rK   ru    s    zMemoryCopyNode.__init__Fc             C   s<   | j | | || | j | || || d S )N)r  r  _generate_assignment_coder  r  )rL   rx   r   r
  rJ   rJ   rK   r    s
    
z'MemoryCopyNode.generate_assignment_code)F)rN   rO   rP   r`  r   ru  r  rQ   rJ   rJ   )rI   rK   r    s   r  c               @   s    e Zd ZdZdZdZdd ZdS )r  z
    Copy the contents of slice src to slice dst. Does not support indirect
    slices.

        memslice1[...] = memslice2
        memslice1[:] = memslice2
    TZ__pyx_memoryview_copy_contentsc             C   sb   | j }|j|j |j|j ||d| j| | |jj|jj|jj	j
f |j d S )Nz%s(%s, %s, %d, %d, %d))r  r   assert_direct_dimsrn   r   r+  copy_slice_cnamer   r  r*  ra   )rL   r7  r   r  rJ   rJ   rK   r  2  s    z)MemoryCopySlice._generate_assignment_codeN)rN   rO   rP   r`  r)  r  r  rJ   rJ   rJ   rK   r  &  s   r  c                   s(   e Zd ZdZ fddZdd Z  ZS )r  z
    Assign a scalar to a slice. dst must be simple, scalar will be assigned
    to a correct type and not just something assignable.

        memslice1[...] = 0.0
        memslice1[:] = 0.0
    c                s    t t| || |jj| _d S )N)rF   r  ru  r   r*  )rL   rn   r  )rI   rJ   rK   ru  I  s    zMemoryCopyScalar.__init__c       
      C   s  ddl m} | jj| jj | jjj}|d}| jjd}|  |	d||
 f  | j sr| j r~| j
 }n|	d|| j
 f  d}|| jj|| jjj|}| }	|jr|	d|	  |	d||	f  |jr|	d	 |  |  d S )
Nr$   )r  r  z%s __pyx_temp_scalar = %s;z%s __pyx_temp_slice = %s;Z__pyx_temp_slicezPy_DECREF(*(PyObject **) %s);z!*((%s *) %s) = __pyx_temp_scalar;zPy_INCREF(__pyx_temp_scalar);)r  r  r  r   r  rn   r*  r   Zbegin_blockr   r   r   rM  Z
slice_iterr  Zstart_loopsra   Z	end_loopsZ	end_block)
rL   Zscalarr   r  r*  Z	type_declZ
slice_declZdst_tempZslice_iter_objprJ   rJ   rK   r  M  s*    


z*MemoryCopyScalar._generate_assignment_code)rN   rO   rP   r`  ru  r  rQ   rJ   rJ   )rI   rK   r  @  s   r  c                   s   e Zd ZddddgZdZdd Zd4d	d
Zdd Zdd Zdd Z	dd Z
dd Zd5ddZdd ZejZdZejddddidZejdddd idZ fd!d"Zd#d$ Zd6d&d'Zd7d(d)Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z  ZS )8SliceIndexNoder   r   r   sliceNc             C   s^   | j |}|js|jrtS |jr&tS |ttttt	t
tfkr@|S |jsL|jrZt|jd S tS )N)r   rr   r  r0  r   r  r   r    r   r-   r   r   r  r   r   Zc_array_typer  r   )rL   rc   r  rJ   rJ   rK   rr   z  s    
zSliceIndexNode.infer_typer   c             C   s6   |t k	r*| jr*| j r&|| jj7 }nt }| j|S )N)r   r   rz   rl   r   r   )rL   r   rJ   rJ   rK   r     s
    
z"SliceIndexNode.inferable_item_nodec             C   s8   | j j}|r.|jrdS |ttttttfkr.dS t	
| S )NF)r   r   r  r   r   r   r-   r   r   r   rN  )rL   r  rJ   rJ   rK   rN    s    zSliceIndexNode.may_be_nonec             C   sF   | j d krd }n| j j}| jd kr(d }n| jj}| jj|| | _d S )N)r   rl   r   r   )rL   r   r   rJ   rJ   rK   r     s    

z(SliceIndexNode.calculate_constant_resultc          
   C   s   | j |}| jd krd}n| j|}| jd kr8d }n| j|}y||| S  tk
r| } z| | W d d }~X Y nX d S )Nr   )r   r   r   r   rd  r   )rL   r   r   r   r   r   rJ   rJ   rK   r     s    

z!SliceIndexNode.compile_time_valuec             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   r     s    z)SliceIndexNode.analyse_target_declarationc             C   s    | j |dd}|jjrt|_|S )NF)rv  )r   r   ra   r   )rL   rc   rp   rJ   rJ   rK   r     s    z#SliceIndexNode.analyse_target_typesTc                sh  | j || _ | j jjs,| j jjs,| j jjrzt| j}t| j| j	pD|| j
pL||d}t| j|| j d}|j||| ddS | j	r| j	|| _	| j
r| j
|| _
|jd st| j	| j
 | j j}|jr|s| j	s| j
s|| _nt|j| _nf|js|jr
t|| _nL|jrt| _n<|jr*|| _n,|jrBt|j| _n| j || _ t| _|jrr|| _| j d| _ | jtkr| j	r| j	jr^| j
r| j
jr^t| j}t| jt| j	p|t| j
p||d|| _ nztj!  fdd}| j	r*| j	jj"r|| j	d	|| _	| j	# || _	| j
r^| j
jj"rN|| j
d
|| _
| j
# || _
d| _$| S )N)r   r   r   )r   r   F)rv  rx  r  ry  z&'NoneType' object is not subscriptablec          
      sz   ddl m}m} || }t| jt| j || r8t|ntd|	 |t
| j|dt| jd|d|}|||S )Nr$   )EvalWithTempExprNodeResultRefNode)r   r   rl   is)operand1r
   operand2)true_val	false_valtest)	UtilNodesr  r  CondExprNodern   r   r  ro  r   r"  PrimaryCmpNoderi  r   analyse_result_type)rp   default_valuerc   r  r  Znode_refZnew_expr)c_intrJ   rK   
allow_none  s$    

z0SliceIndexNode.analyse_types.<locals>.allow_noner  r7  r$   )%r   r   r   r  r5   r   ri  rn   re  r   r   r   rw  r[   rq   r   r   r  r  r  is_cpp_stringri   r  r   r  r=  r   r  rS  r   copydeepcopyr  r   ra   r"  r   )rL   rc   rv  	none_noder   r   r  r  rJ   )r  rK   r     sr    




zSliceIndexNode.analyse_typesc             C   sp   | j |}|rl|jsl| jsl| jslddlm} ||j t	| j
}t| j
|||d}t||||gS d S )Nr$   )r  )r   r   r   )r   r   ra   r   r   r  r  r  rj  ri  rn   re  r   rk  rl  )rL   rc   r  r  r  Z
slice_noderJ   rJ   rK   r     s    

zSliceIndexNode.analyse_as_typezSlicing Python objectZSliceObjectzObjectHandling.cr  ZGet)r  Setc                s   | j jjs| j jjrX|ttttfkrX|ttfkrR|jd sRt	| j
d| j j|f  || _|jr| j jjr| js| js| j ||S tt| ||S )NrR   z:default encoding required for conversion from '%s' to '%s')r   r   r  r  r   r    r   r   r[   r   rn   r   r   r   r"  rF   r  )rL   r6  rc   )rI   rJ   rK   r"  9  s    
zSliceIndexNode.coerce_toc             C   s  | j jst| jd| j   d S | j }|  }|  }|  }| jj jr| j }| jj t	j
t	jfkrpd| }| j tkrd}n| j j }| jd kr|d||||||| jf  n&|d||||||||| jf  n| jj jrh| j }| jj t	jkrd| }| jd kr@|d|||||| jf  n$|d|||||||| jf  n6| jj tkr|jtd	d
 |d||||||| jf  n| j tkr |j| j |  \}}}	}
}}}|d|| j |	|
|||||t|jjd ||| jf  n~| jj tkrH|jt dd d}n,| jj t!krp|jt dd d}nd}|d||| j ||||| jf  |"|   d S )Nz,Slicing is not currently supported for '%s'.z((const char*)%s)Z	ByteArrayz'%s = __Pyx_Py%s_FromString(%s + %s); %sz7%s = __Pyx_Py%s_FromStringAndSize(%s + %s, %s - %s); %sz((const Py_UNICODE*)%s)z-%s = __Pyx_PyUnicode_FromUnicode(%s + %s); %sz?%s = __Pyx_PyUnicode_FromUnicodeAndLength(%s + %s, %s - %s); %sZPyUnicode_SubstringzStringTools.cz.%s = __Pyx_PyUnicode_Substring(%s, %s, %s); %szD%s = __Pyx_PyObject_GetSlice(%s, %s, %s, %s, %s, %s, %d, %d, %d); %sry  ZSliceTupleAndListzObjectHandling.cZ__Pyx_PyList_GetSliceZ__Pyx_PyTuple_GetSliceZPySequence_GetSlicez%s = %s(%s, %s, %s); %s)#r   ra   r   rn   r   r   
start_code	stop_coder  r   r]   r_   r    rb   titler   r   r   r  c_py_unicode_ptr_typer   r  r  r   r  r   get_slice_utility_codeget_slice_configr   r   r[   r   r   r   r  )rL   r   Zbase_resultr   r  r  Z	type_namehas_c_start
has_c_stopc_startc_stoppy_startpy_stoppy_slicecfuncrJ   rJ   rK   r   I  s    





z#SliceIndexNode.generate_result_codeFc             C   s  |  | | jjrt|j| j |  \}}}}	}
}}|| jd| j	
 |
 ||	|
||||t|jjd f
  n|| jr|  nd}|jjr|jj}| || nd|  |f }|jtdd |d| j	 || | j	 |f  | | | | || || d S )Nz?__Pyx_PyObject_SetSlice(%s, %s, %s, %s, %s, %s, %s, %d, %d, %d)ry  r  z%s - %sIncludeStringHzStringTools.cz,memcpy(&(%s[%s]), %s, sizeof(%s[0]) * (%s));)r   r   ra   r  r  set_slice_utility_coder  r  rn   r   r   r   r[   r   r  r   r  generate_slice_guard_coder  r   r  r   r   r  r  r  r  )rL   rx   r   r
  r  r   r  r  r  r  r  r  r  Zstart_offsetarray_lengthrJ   rJ   rK   r    s4    




z'SliceIndexNode.generate_assignment_codec       
      C   s   | j jjs t| jd| j  d S | | |j| j | 	 \}}}}}}}	|
| jd| j  |||||	||t|jjd f	  | | | | d S )Nz=Deleting slices is only supported for Python types, not '%s'.z;__Pyx_PyObject_DelSlice(%s, %s, %s, %s, %s, %s, %d, %d, %d)ry  )r   r   ra   r   rn   r   r  r  r  r  r  r   r   r[   r  r  )
rL   r   r  r  r  r  r  r  r  r  rJ   rJ   rK   r    s     


z%SliceIndexNode.generate_deletion_codec             C   s   d\}}}| j r:| j jj }|r,| j  }nd| j   }d\}}}| jrt| jjj }|rf| j }nd| j  }| jrd| j  pd}|||||||fS )N)Fr  rr  z&%srr  )r   r   ra   r   r   r   r  )rL   r  r  r  r  r  r  r  rJ   rJ   rK   r    s    

zSliceIndexNode.get_slice_configc       
      C   sT  | j jjsd S | j jj}yt| }}W n tk
r@   d }Y nX d  }}| jr| j }y<t|}|dk r|d krd||f }q||7 }n|}d }W n tk
r   Y nX | jr4| j }y^t|}|dk r|d krd| j jj|f }n||7 }t	|t
r
||8 }nd||f }d }W n tk
r2   Y nX d }d}yt|}	W n tk
rb   d }	Y nX t	|t
}|r|dk r|	dkr
t| jd nt|r|d kr|d kr|	|kr
t| jd||f  n:|d k	r|d kr|}d||f }n|d k	r|}n|}|rP|d||f  |d	||f  ||| j |d
 d S )Nr   z%s + %dz	%s - (%s)FzAssignment to empty slice.z8Assignment to slice of wrong length, expected %s, got %sz	(%s)-(%s)zif (unlikely((%s) != (%s))) {zPyErr_Format(PyExc_ValueError, "Assignment to slice of wrong length, expected %%" CYTHON_FORMAT_SSIZE_T "d, got %%" CYTHON_FORMAT_SSIZE_T "d", (Py_ssize_t)(%s), (Py_ssize_t)(%s));r   )r   r   r   r  ro  r|   r   r   r   rk   r"   r   rn   r   r   )
rL   r   Ztarget_sizeZ
slice_sizeZtotal_lengthr   r   Zruntime_checkZcompile_time_checkZint_target_sizerJ   rJ   rK   r    s|    













z(SliceIndexNode.generate_slice_guard_codec             C   s   | j r| j  S dS d S )Nr  )r   r   )rL   rJ   rJ   rK   r  E  s    
zSliceIndexNode.start_codec             C   s,   | j r| j  S | jjjr$| jjjS dS d S )Nr7  )r   r   r   r   r   r  )rL   rJ   rJ   rK   r  K  s
    


zSliceIndexNode.stop_codec             C   s   dS )Nz<unused>rJ   )rL   rJ   rJ   rK   r   S  s    z$SliceIndexNode.calculate_result_code)r   )T)FNN)F) rN   rO   rP   r   r  rr   r   rN  r   r   r   r   r   r   r%   r   r   r  r   loadr  r  r"  r   r  r  r  r  r  r  r   rQ   rJ   rJ   )rI   rK   r  n  s6   
	

`e 
"
Nr  c               @   sV   e Zd ZdddgZdZeZdZdd Zdd	 Z	d
d Z
dd ZdZdd Zdd ZdS )re  r   r   r   Tr$   c             C   s   t | jj| jj| jj| _d S )N)r  r   rl   r   r   )rL   rJ   rJ   rK   r   d  s    z#SliceNode.calculate_constant_resultc          
   C   sb   | j |}| j|}| j|}yt|||S  tk
r\ } z| | W d d }~X Y nX d S )N)r   r   r   r   r  rd  r   )rL   r   r   r   r   r   rJ   rJ   rK   r   j  s    zSliceNode.compile_time_valuec             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  s  s    zSliceNode.may_be_nonec             C   sp   | j |}| j|}| j|}||| _ ||| _||| _| j jrl| jjrl| jjrld| _d| _| S )NTF)r   r   r   r   r=  r   r   )rL   rc   r   r   r   rJ   rJ   rK   r   v  s    zSliceNode.analyse_typesz Constructing Python slice objectc             C   s   | j S )N)r  )rL   rJ   rJ   rK   r     s    zSliceNode.calculate_result_codec             C   s   | j rLt| j| f}|jtdd|d| _|| j}|d kr@d S || j |	d| 
 | j | j | j || 
 | jf  ||   | j r||   d S )Nr  r.   )cleanup_level	dedup_keyz %s = PySlice_New(%s, %s, %s); %s)r   r   r   r  r   r  r  r   rn   r   r   r   r   r   r   r   r  r  )rL   r   r  rJ   rJ   rK   r     s"    zSliceNode.generate_result_codeN)rN   rO   rP   r   r   r!   r   r   r   r   rN  r   r  r   r   rJ   rJ   rJ   rK   re  X  s   
	re  c               @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )r  r   c             C   s   t | jj| jj| jj| _d S )N)r  r   rl   r   r   )rL   rJ   rJ   rK   r     s    z&SliceIntNode.calculate_constant_resultc          
   C   sb   | j |}| j|}| j|}yt|||S  tk
r\ } z| | W d d }~X Y nX d S )N)r   r   r   r   r  rd  r   )rL   r   r   r   r   r   rJ   rJ   rK   r     s    zSliceIntNode.compile_time_valuec             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN    s    zSliceIntNode.may_be_nonec             C   s   | j || _ | j|| _| j|| _| j js@| j || _ | jjsV| j|| _| jjsl| j|| _| j jr| jjr| jjrd| _d| _| S )NTF)r   r   r   r   rk  rJ  r   r   )rL   rc   rJ   rJ   rK   r     s    zSliceIntNode.analyse_typesc             C   s   d S )NrJ   )rL   rJ   rJ   rK   r     s    z"SliceIntNode.calculate_result_codec             C   s2   x,| j | j| jfD ]}t|tr|j  qW d S )N)r   r   r   rk   r  r  r   )rL   r   rZ  rJ   rJ   rK   r     s    
z!SliceIntNode.generate_result_codeN)
rN   rO   rP   r   r   r   rN  r   r   r   rJ   rJ   rJ   rK   r    s   		r  c               @   sV   e Zd ZdZdd Zdd Zdd Zdd	 Zdd
dZdd Z	dd Z
dd ZdZdS )CallNodeNc       	         s.  | j }| }t|tr&t|jS |tkrLt|dd }|d k	rL|j	pJ|}|j
rX|j}|jrt| j dd rt| dr| j j } fdd| jD }t||}|r|j	}|j
r|j}|jS |jS |tkr*|jr*|jr*|jj	r*|jj	}|jr|S |jr*|jjdkrtjS |jjtjkr*|S tS )Nr   r{   c                s   g | ]}|  qS rJ   )rr   )rS   r  )rc   rJ   rK   rt     s    z'CallNode.infer_type.<locals>.<listcomp>rm   )r@  rr   rk   r  r   r  r  r   r   r   r  r  r   r   r   all_alternativesr{   
best_matchr  r   r  r  r  rb   r  r   #types_that_construct_their_instance)	rL   rc   r@  r  r   alternatives	arg_types
func_entryresult_typerJ   )rc   rK   rr     s@    



zCallNode.infer_typec             C   s   | j |S )N)r@  r   )rL   rc   rJ   rJ   rK   r     s    zCallNode.type_dependenciesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rM    s    zCallNode.is_simplec             C   s^   | j d k	r| j S | jj}|tkrT| jjrT| jj}|jjr<dS |jjrT|jt	j
krTdS t| S )NF)may_return_noner@  r   r   r  r   r  r  rb   r   r  r   rN  )rL   r  r   rJ   rJ   rK   rN    s    
zCallNode.may_be_nonec             C   s   |d kr|j }|tjkrv|jrv|jrv|jjrv|jjtjkrv|jjdkrXtj	| _ tj	| _
ntj|jj | _ t| _
d| _n*|jr|jr|jj | _ t| _
d| _nt| _ d S )Nrm   F)r   r   r   r  r   r  rb   r  r   r  r   Zbuiltin_typesr   r  r  )rL   r@  r  rJ   rJ   rK   set_py_result_type  s$    


zCallNode.set_py_result_typec       	   	      s.  | j  }|r|jr|  \}}g }x<t||jjD ]*\}}|t|j	t
|j	|jd|d q6W |rr||j7 }|| _t| _|   | |  dS |r*|jr* fdd| jD | _|jd}|st| j j	d| j j  t| _| S t| j j	|j| _ || j _| j |  |   || _dS d S )N)rn   r   )rn   keyr   Tc                s   g | ]}|  qS rJ   )r   )rS   r  )rc   rJ   rK   rt   >  s    z8CallNode.analyse_as_type_constructor.<locals>.<listcomp>z<init>z'no constructor found for C++  type '%s')r@  r   r  explicit_args_kwdsr  r  var_entriesr   DictItemNodern   r  rb   key_value_pairsDictNoderI   r   r"  r0  r{   r  r   r*   r   rJ  r   rK  r  analyse_c_function_call)	rL   rc   r   r{   rw  itemsr  memberr  rJ   )rc   rK   analyse_as_type_constructor/  s4    
&


z$CallNode.analyse_as_type_constructorc             C   s   | j jS )N)r   r  )rL   rJ   rJ   rK   r   K  s    zCallNode.is_lvaluec             C   s0   |   }|jr|   nt|dds,|   d S )Nr   F)function_typera   r   r   )rL   rc   r  rJ   rJ   rK   r   N  s
    
zCallNode.nogil_checkzCalling gil-requiring function)N)rN   rO   rP   r  rr   r   rM  rN  r	  r  r   r   r  rJ   rJ   rJ   rK   r     s   %
r   c                   s   e Zd ZdddddgZdZdZdZdZdZdZ	dZ
dZdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z fddZdd Z  ZS )rE  rL   coerced_selfr@  r{   	arg_tupleNFc          
      sZ   | j  } fdd| jD }y|| S  tk
rT } z| | W d d }~X Y nX d S )Nc                s   g | ]}|  qS rJ   )r   )rS   r  )r   rJ   rK   rt   q  s    z5SimpleCallNode.compile_time_value.<locals>.<listcomp>)r@  r   r{   rd  r   )rL   r   r@  r{   r   rJ   )r   rK   r   o  s    z!SimpleCallNode.compile_time_valuec             C   s   | j  }|dkrdt| jdkr0t| jjd q| jd |}|sXt| jd jd qt|S n:|dkrt| jdkrt| jjd | jd 	|}|j
S d S )NZpointerr$   zonly one type allowed.r   zUnknown typeZtypeof)r@  rO  rw   r{   r   rn   r   r   r  r   r   )rL   rc   r  r   operandrJ   rJ   rK   r   w  s    
zSimpleCallNode.analyse_as_typec             C   s
   | j d fS )N)r{   )rL   rJ   rJ   rK   r    s    z!SimpleCallNode.explicit_args_kwdsc                s  |   r| S | jr| S d| _d| j_| j | _| j}|jrb|jrb|jjrb|j| _	t
| j	|_|  }d| _t r|jrt|rd}t| j| jd| _| j | _x| jjD ]}|t|M }qW t|| _| jr
 t| tj| || jtt|| jjdS |jrRt| j| jd| _| j  | _d | _| || d| _n2 fdd| jD | _|    |j!dkrd| _| S )	NTr$   F)r{   )r@  r  r   c                s   g | ]}|  qS rJ   )r   )rS   r  )rc   rJ   rK   rt     s    z0SimpleCallNode.analyse_types.<locals>.<listcomp>r  )"r  analysedr@  r  r   r\  r   
is_cmethodr>  rL   r  r  Zis_numpy_call_with_exprsr9   rX  r>   rh  rn   r{   r  r<   r   Zadd_include_filer?   NumPyMethodCallNoderV  rA   r6   ra   r=  r	  r   r  r  )rL   rc   r@  r  Zhas_pythran_argsr  rJ   )rc   rK   r     sN    


zSimpleCallNode.analyse_typesc             C   s   | j j}|jr|j}|S )N)r@  r   r  r  )rL   r  rJ   rJ   rK   r    s    zSimpleCallNode.function_typec             C   s  | j j}|tkrt| _d S |jrJ|jrJ| jrB| jjjrBt| jd | j	}n| jr`| jg| j	 }n| j	}|j
r| j jjd}|d krtj| _d| _d S n6t| j dr| j j}n | j jr| j jr| j jj}nd }|rb| j jjr| j j }dd |D }n| }tdd |D || j||}|s8tj| _d| _d S d|_|j
sN|| j _|j| j _|  }n6d }|  }|jst| jd	|  tj| _d| _d S t|j	}||j }	t|}
|jr|	|
krd
| _d
| _|r|jr|j	r|js|j	d }|d }|jr^| jrF| jjd t|j!dkr0dndd|j!gd| _n|jd|j!|jj!gd}| jr|j"rzt#| j}n
t$| j}|%|j| }| _&n4|jj'r|%|j|}|jj'rt(|t)rd|_*||d< d}xt+t,||
D ]}|j	| }|j}|| %||}|jr |d}|jr8|dkrd}nN|jj-r|j.s|dkrb| jd k	rbn$|/ rnn|dkr|d}|0|}|||< qW xt+||
D ]~}|| }|jj-r|jt1krtj2}n
|j3 }|d krt| j	| jd n|%|| ||< }|jr|dkrd}qW |rxt+|
d
 D ]}|dkrT| jd k	rTq6|| }|/ rhnP|jj
rtnD|j.r|jj-rn0|dks|d
kr6| jd k	r6t4|jdd P q6W || j	d d < t(| j t5rt6| j j7| _n|j8| _| j j9s| j j:r,| j j}|r,|j;s&|j<r,d
| _| jj-rDt=| _>d
| _n,|j?d k	sX|j@r`d
| _n| jjArpd
| _| jr| jjBrtC| jjD| _|j.| _.| j.r|j@r|j@dkr|EtF |j@dkr|j?d kr|EtGHdd |jId | _Jd S )Nz4Cannot call a static method on an instance variable.z
operator()z<error>r   c             S   s   g | ]
}|j qS rJ   )r   )rS   frJ   rJ   rK   rt     s    z:SimpleCallNode.analyse_c_function_call.<locals>.<listcomp>c             S   s   g | ]
}|j qS rJ   )r   )rS   r  rJ   rJ   rK   rt     s    TzCalling non-function type '%s'r$   r   z*'NoneType' object has no attribute '%{0}s'   z.30r  PyExc_AttributeError)r   rR  z@descriptor '%s' requires a '%s' object but received a 'NoneType')rR  FzGcannot pass None into a C function argument that is declared 'not None'z5Python object cannot be passed as a varargs parameterzTArgument evaluation order in C function call is undefined and may not be as expectedr  r  zCppSupport.cppoverflowcheck)Kr@  r   r*   r   is_static_methodrL   r  r   rn   r{   r0  r  r  r   r  r   r   r]  r  r  r  r  r  r  r  rw   optional_arg_counthas_optional_argsr   r  Znot_nonerS  formatrb   Zaccept_builtin_subtypesCMethodSelfCloneNoder  r"  r  r  rk   r/  exact_builtin_typerangeminra   r   r   rL  r   r]   Zdefault_coerced_ctyper   r  r  r  r  r  r\  r  Zutility_code_definitionr   r   r   r  r   r  CFakeReferenceTyper  r  #pyerr_occurred_withgil_utility_coder   r  r[   r  )rL   rc   r  r{   Zoverloaded_entryZ	functypesr  r   	max_nargsexpected_nargsactual_nargs
formal_argr  some_args_in_tempsrV  formal_typeZ	arg_ctyper  rJ   rJ   rK   r    s"   



















 


z&SimpleCallNode.analyse_c_function_callc             C   s   |   S )N)c_call_code)rL   rJ   rJ   rK   r     s    z$SimpleCallNode.calculate_result_codec             C   s  |   }| jtjks|jsdS |j}g }tt|| j}t|j}||j	 }t| j}x.|d | D ]\}}	|	
|j}
||
 qdW |jr|tt| jp| jjj |j	r||krd}n
d| j }|| x(| jt|d  D ]}	||	  qW d| j d|f }|S )Nz<error>rr  z&%sz%s(%s)z, )r  r   r   r*   r   r{   r   r  rw   r   r   r   r  rg   ro  wrapper_callr@  r   is_unbound_cmethodopt_arg_structr   r  )rL   r  Zformal_argsZarg_list_coder{   r)  r*  r+  r,  
actual_argarg_codeZoptional_argsr   rJ   rJ   rK   r/    s.    




zSimpleCallNode.c_call_codec             C   s    |   }|jr|jdkrdS dS )Nr  FT)r  r   r  )rL   r  rJ   rJ   rK   r     s    z#SimpleCallNode.is_c_result_requiredc          
      s  | j }|js|jr |j|j |jjrHt| j	j
dksH| j	j
r\| j	jr\tt| | d S | j	j
rp| j	j
d nd }| j| j||f}x|D ]}|d k	r|| qW || j | jst| | |d kr|jtdd |d|  | ||  | jf  nD|jtdd |d|  | | ||  | jf  ||   x,|D ]$}|d k	rf|| || qfW d S )Nr$   r   PyObjectCallNoArgzObjectHandling.cz%%s = __Pyx_PyObject_CallNoArg(%s); %sPyObjectCallOneArgz*%s = __Pyx_PyObject_CallOneArg(%s, %s); %s) r@  r  r\  r  use_entry_utility_coder   r   ra   rw   r  r{   r   rF   rE  r  rL   r  r   rn   r   r   r   r  r   r  r   r   r   r   r  r  r  )rL   r   r@  r  r   Zsubexpr)rI   rJ   rK   r    sH    










z'SimpleCallNode.generate_evaluation_codec          
   C   s(  |   }|jrl| j }|jtdd |d| 	 | j
 ||| 	 | jf  ||   n|jr$| jr$t| j}t|j|j }|jj|jjdd| _|d| jtjd t| j| f  tt|j| j}x>||| D ].\}}|d| j||j||jf  qW g }	| jjrN| j rN|	!d| 	   n| jj"r|| j sdt#|	!| j$| 	  nd|j%d	kr|j&}
|j%}|
d k	r|	!d
| 	 |j'(|
f  |r| j)r|	!d n
|	!d | j s|	r| * }| 	 r.d| 	  }| j r2| jjr2t+t,| j|}nd}|j%d	krrt-|| jd||f | jjrb| 	 nd |j&| j) nz| j.r| jj/r| jj0r| j
	 dkrd| 	 |1| jf }n |	r|2d3|	| j}nd}|d|||f  | jjr| 	 r||   | jr$|j4| j d S )Nr>  zObjectHandling.cz*%s = __Pyx_PyObject_Call(%s, %s, NULL); %sT)r   z%s.%s = %s;nz!%sr  z%s == %sz__Pyx_ErrOccurredWithGIL()zPyErr_Occurred()z%s = r  z%s%s;)absZlabsZ__Pyx_abs_longlongzVif (unlikely(%s < 0)) { PyErr_SetString(PyExc_OverflowError, "value too large"); %s; }z && z%s%s; %s)5r  ra   r  r   r  r  r   r  r   r   r@  r   rn   r  r   r!  rw   r{   r   r   r   op_arg_structr  r2  r   Zpyrex_prefixr   r  Zopt_arg_cnamerb   r   r   r   r   r   r   Zerror_conditionr  r   r  	cast_coder   r/  r)   r   r   r  r+  r  r   r   r  r   )rL   r   r  r4  r+  r*  r{   r,  r3  Z
exc_checksZexc_valZ	exc_checkrx   r  Z
goto_errorrJ   rJ   rK   r     s    










z#SimpleCallNode.generate_result_code)rN   rO   rP   r   rL   r  r  r0  r!  r   r  r  r   r   r  r   r  r  r   r/  r   r  r   rQ   rJ   rJ   )rI   rK   rE  X  s*   / R.rE  c               @   s$   e Zd ZddgZdZdZdd ZdS )r  r@  r  Tc          
   C   s   | | j | | | j| | jjd ks2t| jj}x|D ]}|| q@W |	d |	d| 
   |	d| 
 | 
 t| jddd |D f  d S )Nz.// function evaluation code for numpy functionz__Pyx_call_destructor(%s);z!new (&%s) decltype(%s){%s{}(%s)};z, c             s   s   | ]}|  V  qd S )N)r   )rS   rZ  rJ   rJ   rK   rV   S  s    z?NumPyMethodCallNode.generate_evaluation_code.<locals>.<genexpr>)r   rn   r   r@  r  r  r   r   r{   r   r   r@   r  )rL   r   r{   r  rJ   rJ   rK   r  C  s    


z,NumPyMethodCallNode.generate_evaluation_codeN)rN   rO   rP   r   r   r  r  rJ   rJ   rJ   rK   r  9  s   r  c               @   s    e Zd ZddgZdZdd ZdS )PyMethodCallNoder@  r  Tc             C   s`  | | j | | | j| | jjd ks2t| jj}x|D ]}|| q@W | jj	}|rj| j
 }nL|jjtdd}| j| |d|| j f  | j| | j| |jjtdd}|d|  d }t|dkr|jjtjdd}|d|  dd	 }| jjr.|| jr(d
nd}	nb| jjr| jjrxL| jjD ]6}
|
j}|rL|jrL|jjjrL||rLd
}	P qLW d}	nd}	|d|	|f  |d||f  |d|  |d|  ||t |dt ||d t|dkr|d|  |d |d |s|j !t"#dd |j !t"#dd |d| 
 ||||f  |$|t |j%| ||&| 
 | j |'|   ntt|dkrl|j !t"#dd |j !t"#dd |d }|d| 
 |||| || f  |$|t |j%| || || ||&| 
 | j |'|   n|j !t"#dd |j !t"#dd xdD ]\}}|d|(   |d||f  |dt)j*t|d |d +d!d" |D f  |d#| 
 ||t)j*|t|||&| 
 | jf  |$|t |'|   x|D ]}|| qLW |d$ |d% qW |d& |jjtdd}|d'|t|||&|| jf  |'| t|dkr|d(|  |d)||||f  |j%| t|dkr|d xHt,|D ]<\}}|| |-|  |d*|||| f  q"W t|dkr||j%| x"|D ]}|.| || qW |j !t"#d+d |d,| 
 |||&| 
 | jf  |'|   |/|t |j%| t|dkr|d |d |rD| j| | j| n|/|t |j%| d S )-NT)r   z	%s = %s; z
%s = NULL;r$   Fz%s = 0;c             S   s   | j }|jr|jjrdS dS )NFT)r>  r  r   r  )r  r>  rJ   rJ   rK   attribute_is_likely_method|  s    zMPyMethodCallNode.generate_evaluation_code.<locals>.attribute_is_likely_methodZlikelyZunlikelyz6if (CYTHON_UNPACK_METHODS && %s(PyMethod_Check(%s))) {z%s = PyMethod_GET_SELF(%s);zif (likely(%s)) {z/PyObject* function = PyMethod_GET_FUNCTION(%s);r@  z%s = 1;r   r5  zObjectHandling.cr6  zM%s = (%s) ? __Pyx_PyObject_CallOneArg(%s, %s) : __Pyx_PyObject_CallNoArg(%s);ZPyObjectCall2Argsr   zU%s = (%s) ? __Pyx_PyObject_Call2Args(%s, %s, %s) : __Pyx_PyObject_CallOneArg(%s, %s);ZPyFunctionFastCallZPyCFunctionFastCall))ZPyFunction_CheckZPy)Z__Pyx_PyFastCFunction_CheckZPyCz#if CYTHON_FAST_%sCALLzif (%s(%s)) {zPyObject *%s[%d] = {%s, %s};z, c             s   s   | ]}|  V  qd S )N)r   )rS   r  rJ   rJ   rK   rV     s    z<PyMethodCallNode.generate_evaluation_code.<locals>.<genexpr>z6%s = __Pyx_%sFunction_FastCall(%s, %s+1-%s, %d+%s); %sz} elsez#endifr0  z%s = PyTuple_New(%d+%s); %sz	if (%s) {z:__Pyx_GIVEREF(%s); PyTuple_SET_ITEM(%s, 0, %s); %s = NULL;z PyTuple_SET_ITEM(%s, %d+%s, %s);r>  z*%s = __Pyx_PyObject_Call(%s, %s, NULL); %s)0r   rn   r   r@  r  r  r   r   r{   r   r   r   r   r   r   r*  r   r  r  r   rw   r   ra  r\  r  rv   rx   r>  r   ra   r   r  r  r  r   r  Zput_xdecref_clearr   r   r  upperr   quick_temp_cnamer  rP  r  r	  r  )rL   r   r{   r  Zreuse_function_tempr@  Zself_argZarg_offset_cnamer=  Zlikely_methodr  r   	test_funcZcall_prefixZ
args_tuplerV  rJ   rJ   rK   r  `  s   





















z)PyMethodCallNode.generate_evaluation_codeN)rN   rO   rP   r   r   r  rJ   rJ   rJ   rK   r<  V  s   r<  c               @   s<   e Zd ZddgZdZeZdZdZdd Z	dd Z
d	d
 ZdS )InlinedDefNodeCallNoder{   function_namer$   Nc             C   s>   | j j}|js|jrdS t|jt| jkr0dS |jr:dS dS )NFT)r@  def_nodestar_argstarstar_argrw   r{   num_kwonly_args)rL   r  rJ   rJ   rK   can_be_inlined  s    z%InlinedDefNodeCallNode.can_be_inlinedc                s  | j  | _  fdd| jD | _| jj}t| j}d}xzt|D ]n}|j| j}| j| | }|j	rz|dkrd}n.|jj
r js| rn|dkrd}| }|| j|< qDW |rxZt|d D ]J}| j| }| rq|jjrqʈ jr|jj
rq|dkrt|jdd P qW | S )Nc                s   g | ]}|  qS rJ   )r   )rS   r  )rc   rJ   rK   rt   +  s    z8InlinedDefNodeCallNode.analyse_types.<locals>.<listcomp>Fr   Tr$   zTArgument evaluation order in C function call is undefined and may not be as expected)rB  r   r{   r@  rC  rw   r%  r   r"  r   ra   r   r   rL  r0  r   rn   )rL   rc   r  r+  r-  rV  r.  r  rJ   )rc   rK   r   (  s>    


z$InlinedDefNodeCallNode.analyse_typesc          
   C   s   | j  g}| jj}xDt| j|jD ]2\}}|jjrH||	|j q$||
  q$W d|}|d| 
 | jjjj||| 
 | jf  ||   d S )Nz, z%s = %s(%s); %s)rB  r   r@  rC  r  r{   r   ra   r   r   r   r  r   r   pyfunc_cnamer   rn   r  )rL   r   r4  r  r  Z	proto_argrJ   rJ   rK   r   a  s    

z+InlinedDefNodeCallNode.generate_result_code)rN   rO   rP   r   r   r   r   r@  rB  rG  r   r   rJ   rJ   rJ   rK   rA    s   
9rA  c               @   s2   e Zd Zg Zd
ddZdd Zdd Zdd	 ZdS )PythonCapiFunctionNodeNc             C   s   t j| |||||d d S )N)rb   r   r   r  )r   ru  )rL   rn   py_namer   r  r  rJ   rJ   rK   ru  v  s    zPythonCapiFunctionNode.__init__c             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   z  s    z$PythonCapiFunctionNode.analyse_typesc             C   s   | j r|j| j  d S )N)r  r  r  )rL   r   rJ   rJ   rK   r   }  s    z+PythonCapiFunctionNode.generate_result_codec             C   s   | j S )N)r   )rL   rJ   rJ   rK   r     s    z,PythonCapiFunctionNode.calculate_result_code)N)rN   rO   rP   r   ru  r   r   r   rJ   rJ   rJ   rK   rI  s  s
   
rI  c               @   s   e Zd ZdZdddZdS )PythonCapiCallNodeFNc             K   s8   |j | _| j| _t|||||d| _tj| |f| d S )N)r  )r  r   r   rI  r@  rE  ru  )rL   rn   rB  r  r  rJ  rU  rJ   rJ   rK   ru    s    
zPythonCapiCallNode.__init__)NN)rN   rO   rP   r  ru  rJ   rJ   rJ   rK   rK    s   rK  c                   s8   e Zd ZddgZdZ fddZdd Zdd	 Z  ZS )
CachedBuiltinMethodCallNoder>  r{   Tc                s&   t t| j|j||||j|jd d S )N)r>  r   r{   r  r   )rF   rL  ru  rn   r  r   )rL   Z	call_noder>  r   r{   )rI   rJ   rK   ru    s
    
z$CachedBuiltinMethodCallNode.__init__c             C   s   | j d k	r| j S t| S )N)r  r   rN  )rL   rJ   rJ   rK   rN    s    
z'CachedBuiltinMethodCallNode.may_be_nonec          	   C   sp   | j jj}| j  }dd | jD }|j||| j|}|d| 	 ||
| 	 | jf  || 	  d S )Nc             S   s   g | ]}|  qS rJ   )r   )rS   r  rJ   rJ   rK   rt     s    zDCachedBuiltinMethodCallNode.generate_result_code.<locals>.<listcomp>z%s = %s; %s)r>  r   r   r   r{   r  Zcached_unbound_method_call_coder   r   r   r   rn   r  )rL   r   Z
type_cnameZ	obj_cnamer{   Z	call_coderJ   rJ   rK   r     s    

z0CachedBuiltinMethodCallNode.generate_result_code)	rN   rO   rP   r   r   ru  rN  r   rQ   rJ   rJ   )rI   rK   rL    s
   rL  c               @   sH   e Zd ZeZdddgZejZdd Z	dd Z
dd	 Zd
d Zdd ZdS )GeneralCallNoder@  rf  rg  c          
   C   s`   | j |}| j|}| j|}y
|||S  tk
rZ } z| | W d d }~X Y nX d S )N)r@  r   rf  rg  rd  r   )rL   r   r@  rf  rg  r   rJ   rJ   rK   r     s    
z"GeneralCallNode.compile_time_valuec             C   s0   | j r| j jr| jjs"t| jd| jj| j fS )Nz0Compile-time keyword arguments must be explicit.)rg  rY  rf  ru   r   rn   r{   )rL   rJ   rJ   rK   r    s
    z"GeneralCallNode.explicit_args_kwdsc             C   s   |  |r| S | j|| _| jjjs| jjjr:t| _| S t| jdr|  }|d k	rh|| k	rh||S | jj	j
r| j|| _q|| krt| jd qn| j|| _| jr| j|| _| j|| _| j|| _| | j d| _| S )Nr   zRNon-trivial keyword arguments and starred arguments not allowed in cdef functions.r$   )r  r@  r   r   ra   r&  r*   r   map_to_simple_call_noder   r  r=  r   rn   rg  rf  r	  r   )rL   rc   rp   rJ   rJ   rK   r     s2    




zGeneralCallNode.analyse_typesc                s  t | jts| S | jjs| S | j}t|dd}|s6| S |j}|jrH|j	}|j
sR| S | jj | j}|j}|jrx|dd }t t|krt| jdt|t f  dS tdd |dt  D }|t d }d}t }	t|}
d}x>|jD ]4}|jj}||
kr t|jd	|  d
}|
| qW xPt||jD ]@\}}|jj}|j|krx|| |d7 }|	|j nP q<W ddlm}m} g }t|j|kr|t|	d }t fddt|jD }d}x|D ]}|j}||kr|s|}qn&|r$|jr| S t| jd|  dS || \}}|| |d7 }|j r\|	|j n0||j}| stt |	| |||f qW |rg }g }|d d }xL|	D ]D}||krP | r|| n||}|| || qW |r|}	|dd t!|D  }x6|jD ],}|jj}||kr"d
}t|jd|  q"W |r\dS t"| j||	d}x"|ddd D ]}|||}q|W |S )z
        Tries to map keyword arguments to declared positional arguments.
        Returns self to try a Python call, None to report an error
        or a SimpleCallNode if the mapping succeeds.
        r   Nr$   zDfunction call got too many positional arguments, expected %d, got %sc             S   s   g | ]}|j r|j qS rJ   )rb   )rS   r  rJ   rJ   rK   rt     s    z;GeneralCallNode.map_to_simple_call_node.<locals>.<listcomp>r   Fzargument '%s' passed twiceT)r  
LetRefNodec                s(   g | ] \}}|j j|t  |ffqS rJ   )r
  r   rw   )rS   rV  r  )pos_argsrJ   rK   rt   ;  s   z(C function call is missing argument '%s'r  c             S   s   g | ]\}}|qS rJ   rJ   )rS   rV  r  rJ   rJ   rK   rt   k  s    z/C function got unexpected keyword argument '%s')r@  r{   )#rk   rf  rh  rg  rY  r@  r   r   r  r  r   r{   r  rw   r   rn   r   r   r  r
  r   addr  rb   r   r  r  rO  r  rP  r  rM  r   sortedrE  )rL   r@  r   r  rU  Zdeclared_argsZmatched_argsZunmatched_argsZmatched_kwargs_countr{   seenZ
has_errorsr  rb   Zdecl_argr  rO  ZtempskeywordsZfirst_missing_keywordrn   tempZ
final_argsZ	new_tempsZfirst_temp_argZ	arg_valuerp   rJ   )rP  rK   rN    s    











z'GeneralCallNode.map_to_simple_call_nodec             C   s~   | j jrd S | jr| j }nd}|jtdd |d| 	 | j
 | j ||| 	 | jf  ||   d S )Nrr  r>  zObjectHandling.cz(%s = __Pyx_PyObject_Call(%s, %s, %s); %s)r   r&  rg  r   r  r  r   r  r   r   r@  rf  r   rn   r  )rL   r   rU  rJ   rJ   rK   r     s     

z$GeneralCallNode.generate_result_codeN)rN   rO   rP   r   r   r   r%   r   r   r   r  r   rN  r   rJ   rJ   rJ   rK   rM    s   
	  rM  c               @   sH   e Zd ZdgZdZdd Zdd Zdd Zd	d
 Ze	j
ZdZdd ZdS )AsTupleNoder  r$   c             C   s   t | jj| _d S )N)r   r  rl   )rL   rJ   rJ   rK   r     s    z%AsTupleNode.calculate_constant_resultc          
   C   sF   | j |}yt|S  tk
r@ } z| | W d d }~X Y nX d S )N)r  r   r   rd  r   )rL   r   r  r   rJ   rJ   rK   r     s
    zAsTupleNode.compile_time_valuec             C   s6   | j ||| _ | j jtkr,| j dS t| _| S )Nz!'NoneType' object is not iterable)r  r   r=  r   r   rS  )rL   rc   rJ   rJ   rK   r     s
    zAsTupleNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN    s    zAsTupleNode.may_be_nonezConstructing Python tuplec          
   C   sX   | j jttfkrdnd}|d|  || j  ||  | jf  |	|   d S )NZ__Pyx_PySequence_TupleZPySequence_Tuplez%s = %s(%s); %s)
r  r   r   r   r   r   r   r   rn   r  )rL   r   r  rJ   rJ   rK   r     s    
z AsTupleNode.generate_result_codeN)rN   rO   rP   r   r   r   r   r   rN  r%   r   r   r  r   rJ   rJ   rJ   rK   rV    s   rV  c               @   sb   e Zd ZdgZdZeZdZdd Zdd Z	dd	 Z
d
d Zdd Zdd ZdZdd Zdd ZdS )MergedDictNoderg  r$   Tc             C   sx   i }| j }xb| jD ]X}|jr.dd |jD }n
|j }x0|D ](\}}|r^||kr^td| |||< q>W qW || _d S )Nc             s   s   | ]\}}|j |j fV  qd S )N)rl   )rS   r
  r   rJ   rJ   rK   rV     s   z;MergedDictNode.calculate_constant_result.<locals>.<genexpr>z$duplicate keyword argument found: %s)reject_duplicatesrg  rY  r  rl   	iteritemsr|   )rL   r   rX  rs   r  r
  r   rJ   rJ   rK   r     s    
z(MergedDictNode.calculate_constant_resultc       	         s   i }| j }x| jD ]}|jr2 fdd|jD }n|  }y6x0|D ](\}}|rh||krhtd| |||< qHW W q tk
r } z| | W d d }~X Y qX qW |S )Nc                s$   g | ]\}}|  |  fqS rJ   )r   )rS   r
  r   )r   rJ   rK   rt     s   z5MergedDictNode.compile_time_value.<locals>.<listcomp>z$duplicate keyword argument found: %s)	rX  rg  rY  r  r   rY  r|   rd  r   )	rL   r   r   rX  rs   r  r
  r   r   rJ   )r   rK   r     s    
 z!MergedDictNode.compile_time_valuec             C   s   dS )NrJ   rJ   )rL   rc   rJ   rJ   rK   r     s    z MergedDictNode.type_dependenciesc             C   s   t S )N)r   )rL   rc   rJ   rJ   rK   rr     s    zMergedDictNode.infer_typec                sh    fdd| j D }t|dkr^|d jtkr^|d }|jrZ|jjrZt|jjdkrZd|_|S || _ | S )Nc                s"   g | ]}|   d qS )z1argument after ** must be a mapping, not NoneType)r   r=  rS  )rS   r  )rc   rJ   rK   rt     s   z0MergedDictNode.analyse_types.<locals>.<listcomp>r$   r   T)	rg  rw   r   r   r  r   r  Zcf_assignmentsr  )rL   rc   r{   r  rJ   )rc   rK   r     s    

zMergedDictNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN    s    zMergedDictNode.may_be_nonezConstructing Python dictc          
   C   s  | | j | | t| j}t|}|| |jtk	rN|	d|
   |jr|| |	d|  |
 f  || nB|	d|  |
 ||  |jf  ||   || |jtk	r(|	d |	d|  |
 ||  | jf  || 
  || |	d || t }xn|D ]d}|jrx|jD ]}|| | jr|	d|  |j
 f  |d |	d	|j
 || jf  |	d ||jjd
|  |j
 |j
 f  || || qVW n|| | jr>|d ||jd|  |
 f  nR|d |	d|  |
 f  |	d|
   |	||j |	d || || q@W x&t|D ]}|jt|d qW d S )Nz$if (likely(PyDict_CheckExact(%s))) {z%s = %s;z%s = PyDict_Copy(%s); %sz} else {zH%s = PyObject_CallFunctionObjArgs((PyObject*)&PyDict_Type, %s, NULL); %sr   z(if (unlikely(PyDict_Contains(%s, %s))) {RaiseDoubleKeywordsz2__Pyx_RaiseDoubleKeywordsError("function", %s); %szPyDict_SetItem(%s, %s, %s)ZMergeKeywordsz__Pyx_MergeKeywords(%s, %s)ZRaiseMappingExpectedz*if (unlikely(PyDict_Update(%s, %s) < 0)) {zVif (PyErr_ExceptionMatches(PyExc_AttributeError)) __Pyx_RaiseMappingExpectedError(%s);zFunctionArguments.c) r   rn   r   r  rg  r  r  r   r   r   r   rY  r   r   r	  r   r  r  r  r   r  rX  r
  rQ  r   r  r   rR  r  r  r   r  )rL   r   r{   rs   helpersr  helperrJ   rJ   rK   r    s    



















z'MergedDictNode.generate_evaluation_codec             C   s   x| j D ]}|| qW d S )N)rg  r  )rL   r   rs   rJ   rJ   rK   r  Z  s    zMergedDictNode.annotateN)rN   rO   rP   r   r   r   r   rX  r   r   r   rr   r   rN  r  r  r  rJ   rJ   rJ   rK   rW    s   OrW  c               @   s2  e Zd ZdZdgZejZdZdZ	dZ
dZdZdZdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd ZdDddZdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& ZdEd'd(ZdFd)d*Zd+d, Z d-d. Z!d/Z"d0d1 Z#d2d3 Z$d4d5 Z%d6d7 Z&d8d9 Z'd:d; Z(d<d= Z)dGd>d?Z*dHd@dAZ+dBdC Z,dS )IrF  r$   r>  Nr   TFc             C   sD   t | jtr$| jjr$| jdks$| jS | j }|r@d|| jf S d S )NZparallelz%s.%s)rk   r>  r!  r  r?  rO  )rL   ZcyrJ   rJ   rK   rO  y  s    

z!AttributeNode.as_cython_attributec             C   sH   |t kr:| j}|r:|jr:|jr:d| _|j| _| | | S t| ||S )Nr$   )r   r   r   r  r   analyse_as_python_attributer   r"  )rL   r6  rc   r   rJ   rJ   rK   r"    s    
zAttributeNode.coerce_toc             C   s2   | j }|dr|drd S t| jj|| _d S )N__)r?  
startswithendswithr   r>  rl   )rL   r  rJ   rJ   rK   r     s    z'AttributeNode.calculate_constant_resultc          
   C   sv   | j }|dr.|dr.t| jd|  d S | j|}y
t||S  tk
rp } z| 	| W d d }~X Y nX d S )Nr^  z6Invalid attribute name '%s' in compile-time expression)
r?  r_  r`  r   rn   r>  r   r   rd  r   )rL   r   r  r>  r   rJ   rJ   rK   r     s    

z AttributeNode.compile_time_valuec             C   s   | j |S )N)r>  r   )rL   rc   rJ   rJ   rK   r     s    zAttributeNode.type_dependenciesc             C   s|   | j |dd}|d k	r|jjS | |}|d k	r8|jjS | j|}| j||d |jrd| jjrdt	S | jrv| jj
rvt	S | jS )NF)target)obj_type)#analyse_as_cimported_attribute_noder   r   analyse_as_type_attributer>  rr   analyse_attributer  r   r   r  )rL   rc   rp   rb  rJ   rJ   rK   rr     s    
zAttributeNode.infer_typec             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   r     s    z(AttributeNode.analyse_target_declarationc             C   sF   | j |dd}|jjr(t| jd| j  | sBt| jd| j  |S )Nr$   )ra  z"Assignment to const attribute '%s'z%Assignment to non-lvalue of type '%s')r   r   r  r   rn   r?  r   )rL   rc   rp   rJ   rJ   rK   r     s    z"AttributeNode.analyse_target_typesc             C   sp   |j d | _| ||}|d kr.|s.| |}|d krN| ||}|d k	sNt|jr\d|j_|jrl|	| |S )Nr  T)
r[   r  rc  rd  "analyse_as_ordinary_attribute_noder   r   r  r\  wrap_obj_in_nonecheck)rL   rc   ra  rp   rJ   rJ   rK   r     s    

zAttributeNode.analyse_typesc             C   sj   | j |}|rf|| j}|rF|js8|js8|js8|jrF| |||S | 	|rft
| jd| j  | S d S )Nz&cimported module has no attribute '%s')r>  r   r  r?  r  r   r  r  as_name_noder  r   rn   )rL   rc   ra  module_scoper   rJ   rJ   rK   rc    s    
z1AttributeNode.analyse_as_cimported_attribute_nodec             C   s  | j jrd S | j |}|r|js2|js2|jrZ|j| j}|r|j	s^|jr|j
jr|jrt| jsnd S |}n|jrt|j
ds|j}|j
js|jr|jjr|j
}nV|jrt| jd|j|f  tj}n2t|j
}|jd d  |_td|dd |jd< nd|j|jf }|j
}t|j||}d|_|j|_d|_|j|_| j||ddS nv|j r| j|j!krx^|j"j#D ]$}|j| jkrz| j||ddS qzW t| jd	| j|f  nt| jd	| j|f  d S )
Nr:  z%s not a static member of %srL   r   z%s->%sr$   F)ra  z%s not a known value of %s)$r>  r[  r   r  r  r0  r  r  r?  r  r   r   r  Z
func_cnamer   r  parent_scopeZis_cpp_class_scoper   rn   rb   r   r*   r  r{   r4  Zvtabptr_cnamer   r   r  r1  rh  rB  rM  r   Zenum_values)rL   rc   r   r   Z
ubcm_entryr   r   rJ   rJ   rK   rd    sL    z'AttributeNode.analyse_as_type_attributec             C   sZ   | j |}|r|| jS | j jsV| j |}|rVt|drV|jd k	rV|j| jS d S )Nr  )r>  r   Zlookup_typer?  r[  r   r   r  )rL   rc   ri  r  rJ   rJ   rK   r     s    zAttributeNode.analyse_as_typec             C   s@   | j |}|r<|| j}|r<|jr<|jjs6|jjr<|jS d S )N)r>  r   r  r?  r  r   r  r  )rL   rc   ri  r   rJ   rJ   rK   r   $  s    
z'AttributeNode.analyse_as_extension_typec             C   s0   | j |}|r,|| j}|r,|jr,|jS d S )N)r>  r   r  r?  r  )rL   rc   ri  r   rJ   rJ   rK   r   /  s    
zAttributeNode.analyse_as_modulec             C   s8   t j| | j|d}|r"||}n
||}d|j_|S )N)rb   r   r$   )r!  rV  r?  r   r  r   r  )rL   rc   r   ra  rp   rJ   rJ   rK   rh  9  s    
zAttributeNode.as_name_nodec             C   sb   | j || _ | | | jr,| jjr,| js,| jrD|s^d| _t| _	n|r^| j j
jr^t| jd | S )Nr$   z'Assignment to an immutable object field)r>  r   re  r   r  r  
is_py_attrr   r   r   r   r  r   rn   )rL   rc   ra  rJ   rJ   rK   rf  D  s    
z0AttributeNode.analyse_as_ordinary_attribute_nodec             C   s  |d k	}d| _ | j| _|d krJ| jjjs2| jjjr@| j|| _| jj}n|jsV|jrZt}|j	sf|j
rt|j}d| _n.|js|jrd| _n|jr|jrd| _nd| _|jr| r8|j| j}|jr$|s$| jdkrd| _d| _d| _| jj| j| _d S || j|| j |j| j}|rT|jrTd }nt| jd|  tj| _d S || _ |r|jr|j!dkrt| jd |j"r|j#r|j$r|j| _|j%| _d S | &||| d S )	Nr   z->.TTz/Cannot select attribute of incomplete type '%s'__weakref__z,Illegal use of special attribute __weakref__)'rk  r?  r  r>  r   r  r  r=  r   r  r   r  r  r  r  r  Zis_fake_referenceZhas_attributesZattributes_knownr  r  r   is_memslice_transposer   r   Z	transposern   Zdeclare_attributer  r   r   r*   r   rb   r  r  r  r   r]  )rL   rc   rb  immutable_objr   rJ   rJ   rK   re  W  sZ    


zAttributeNode.analyse_attributec             C   s   |d kr| j j}|| j| _| j| _t| _d| _|js|js|j	s||j
s||js||js||js||jrl||s||jr||r|s| j || _ nX|jr| j js| j jr| j jjr| j jjjjr|s| j || _ nt| jd|| jf  d S )Nr$   z)Object of type '%s' has no attribute '%s')r>  r   Zmangle_class_private_namer?  r  r   rk  ra   r&  r  r  r  r   r  rG  can_coerce_to_pyobject	is_structr=  r   r  r\  r   r  r   rn   )rL   rc   rb  rp  rJ   rJ   rK   r]    s,    
z)AttributeNode.analyse_as_python_attributec             C   s   |j d sd S d }d}| jjjrR| jrR| jsRdt| jdkrBdnd}| jf}n8| jjj	r| j
rhd}n"| jjj| j}|rd}|jf}|r| jj|d	|d
| _d S )Nr  rJ   z*'NoneType' object has no attribute '%{0}s'r  z.30r  z&Cannot transpose None memoryview slicez5Cannot access '%s' attribute of None memoryview slicer  )rR  )r[   r>  r   r  needs_none_checkrk  r"  rw   r?  r   ro  r  r  rb   rS  )rL   rc   r9  rR  r   rJ   rJ   rK   rg    s$    



z#AttributeNode.wrap_obj_in_nonecheckc             C   s   | j r|   d S )N)rk  r   )rL   rc   rJ   rJ   rK   r     s    zAttributeNode.nogil_checkzAccessing Python attributec             C   s   | j |S )N)r>  r  )rL   rc   rJ   rJ   rK   r    s    z0AttributeNode.is_cimported_module_without_shadowc             C   s&   | j r|  p| j  S t| S d S )N)r>  r   rM  r!  )rL   rJ   rJ   rK   rM    s    zAttributeNode.is_simplec             C   s   | j r
dS t| S d S )NT)r>  r!  r   )rL   rJ   rJ   rK   r     s    zAttributeNode.is_lvaluec             C   s   | j r| j  S t| S d S )N)r>  r   r!  )rL   rJ   rJ   rK   r     s    
zAttributeNode.is_ephemeralc             C   s   | j }||j}| jr| jjr|jjrp| jjsp| jjr@| jjS | jjrR| jj	| _
d|jj|| j|jj| j
f S | jr|| j
S d S |jjrd| j
 |f S |jjr| jr| jjr|jj| dd}d|| j| j
f S d S )Nz((struct %s *)%s%s%s)->%sz__Pyx_C%s(%s)T)Zto_object_structz%s%s%s)r>  r   r   r   r  r  Zis_builtin_cmethodZfinal_func_cnameZ
from_fusedr   r  Zvtabstruct_cnamer  Zvtabslot_cnamer   r2  r>  r  r  r;  r   )rL   r>  Zobj_coderJ   rJ   rK   r     s&    
z#AttributeNode.calculate_result_codec             C   sz  | j r| jr&|jtdd d}n|jtdd d}|d|  || j	 |
| j||  | jf  || 	  n| jjr>| jrx,| jjD ] \}}|dkrt| jd d S qW |d	|  | j f  |j|  d
d d}||||   | j n&| jrv|d|  || jf  n8| jjrV| jjjrVn | jrv| jjrv|j| j d S )NZPyObjectLookupSpecialzObjectHandling.cZ__Pyx_PyObject_LookupSpecialZPyObjectGetAttrStrZ__Pyx_PyObject_GetAttrStrz%s = %s(%s, %s); %sr  z=Transposing not supported for slices with indirect dimensionsz%s = %s;T)r   z"__pyx_memslice_transpose(&%s) == 0zeif (unlikely(!%s.memview)) {PyErr_SetString(PyExc_AttributeError,"Memoryview is not initialized");%s})rk  is_special_lookupr  r  r   r  r   r   r>  r   r  r?  r   rn   r  r   r   ro  rn  r   r   r   r  r   r  r   r  r7  )rL   r   Zlookup_func_namer  r  rm  rJ   rJ   rK   r     sD    

z"AttributeNode.generate_result_codec             C   s\   | j rL| jjrL| jrL|j|  dd |d|    |d|    nt| | d S )NT)r   z%s.memview = NULL;z%s.data = NULL;)	r   r   r   ro  r  r   r   r   r  )rL   r   rJ   rJ   rK   r  ?  s    z$AttributeNode.generate_disposal_codec          	   C   sp  | j | | jrf|jtdd || jd| j 	 |
| j|	 f  || || n| j jjr|d| j | j | j j||  f  n|  }| jjr| jr|| ||	  || |||   n.| jjrddlm} |||| | j| | jjs@|d|||  f  | | || | j | | j | d S )Nr  zObjectHandling.cz%__Pyx_PyObject_SetAttrStr(%s, %s, %s)z__Pyx_SET_C%s(%s, %s);r$   )r  z%s = %s;)!r>  r  rk  r  r  r   r  r  rn   r   r  r?  r  r  r   r2  r   r  r>  r   r   r   ra   r   r   r  r  r  r   r  r  Zput_assign_to_memviewslicer	  )rL   rx   r   r
  r  r   Zselect_coder  rJ   rJ   rK   r  I  sF    








z&AttributeNode.generate_assignment_codec             C   s   | j | | js*| jjjrdd| jjjkrd|jt	
dd || jd| j  || jf  nt| jd | j | | j | d S )N__del__r  zObjectHandling.cz!__Pyx_PyObject_DelAttrStr(%s, %s)z+Cannot delete C attribute of extension type)r>  r  rk  r   r  Zis_property_scopeentriesr  r  r   r  r  rn   r   r  r?  r   r  r  )rL   r   r  rJ   rJ   rK   r  r  s    z$AttributeNode.generate_deletion_codec             C   s@   | j rd\}}nd\}}|| jt||| j t| jd d S )N)Zpy_attrzpython attribute (%s))Zc_attrzc attribute (%s))r  )rk  r  rn   r/   r   rw   r?  )rL   r   r  r  rJ   rJ   rK   r    s    
zAttributeNode.annotate)r   )N)NF)FNN)F)-rN   rO   rP   r\  r   r   r*   r   r   r  rs  ro  rt  rk  rO  r"  r   r   r   rr   r   r   r   rc  rd  r   r   r   rh  rf  re  r]  rg  r   r  r  rM  r   r   r   r   r  r  r  r  rJ   rJ   rJ   rK   rF  _  sR   
5


C
%0
 
(
rF  c               @   sb   e Zd ZdgZdZeZdZdZdd Z	dd Z
dd	 Zd
d Zdd Zdd Zdd Zdd ZdS )StarredUnpackingNodera  r$   Fc             C   s   t j| ||d d S )N)ra  )r   ru  )rL   rn   ra  rJ   rJ   rK   ru    s    zStarredUnpackingNode.__init__c             C   s"   | j st| jd | j| d S )Nz&starred expression is not allowed here)starred_expr_allowed_herer   rn   ra  analyse_declarations)rL   rc   rJ   rJ   rK   ry    s    z)StarredUnpackingNode.analyse_declarationsc             C   s   | j |S )N)ra  rr   )rL   rc   rJ   rJ   rK   rr     s    zStarredUnpackingNode.infer_typec             C   s.   | j st| jd | j|| _| jj| _| S )Nz&starred expression is not allowed here)rx  r   rn   ra  r   r   )rL   rc   rJ   rJ   rK   r     s
    
z"StarredUnpackingNode.analyse_typesc             C   s   | j | d S )N)ra  r   )rL   rc   rJ   rJ   rK   r     s    z/StarredUnpackingNode.analyse_target_declarationc             C   s   | j || _ | j j| _| S )N)ra  r   r   )rL   rc   rJ   rJ   rK   r     s    
z)StarredUnpackingNode.analyse_target_typesc             C   s   dS )Nr  rJ   )rL   rJ   rJ   rK   r     s    z*StarredUnpackingNode.calculate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r     s    z)StarredUnpackingNode.generate_result_codeN)rN   rO   rP   r   rb  r   r   r   rx  ru  ry  rr   r   r   r   r   r   rJ   rJ   rJ   rK   rw    s   rw  c            
       s   e Zd ZddgZdZdZdZdZdd Zdd	 Z	d
d Z
d,ddZdd Zdd Zdd Zdd Zdd Zdd Zd-ddZ fddZd.ddZeeejed ejdgZd!d" Zd#d$ Zd/d&d'Zd(d) Zd*d+ Z  Z S )0SequenceNoder{   r   r$   NFc                s    fdd| j D S )Nc                s   g | ]}|  qS rJ   )r   )rS   r  )r   rJ   rK   rt     s    z8SequenceNode.compile_time_value_list.<locals>.<listcomp>)r{   )rL   r   rJ   )r   rK   compile_time_value_list  s    z$SequenceNode.compile_time_value_listc             C   sX   d| _ g }xB| jD ]8}|jr@| j r.t|jd d| _ |j}d|_|| qW || _d S )NFz,more than 1 starred expression in assignmentT)starred_assignmentr{   rb  r   rn   ra  r   )rL   r{   r  rJ   rJ   rK   replace_starred_target_node  s    z(SequenceNode.replace_starred_target_nodec             C   s&   |    x| jD ]}|| qW d S )N)r}  r{   r   )rL   rc   r  rJ   rJ   rK   r     s    z'SequenceNode.analyse_target_declarationc             C   sl   x4t | jD ]&\}}|s"||}||| j|< qW | jrb| j|| _| jjjsb| j|| _d| _| S )Nr$   )rP  r{   r   r=  r   r   r+  r   )rL   rc   skip_childrenrV  r  rJ   rJ   rK   r     s    

zSequenceNode.analyse_typesc                sr   | j |kr| S | jrtt| j|jkrDt| jd|jt| jf   fddt| j|j	D }t
| j||ddS )NzHtrying to coerce sequence to ctuple of wrong length, expected %d, got %dc                s   g | ]\}}| | qS rJ   )r"  )rS   r  r   )rc   rJ   rK   rt     s    z1SequenceNode.coerce_to_ctuple.<locals>.<listcomp>T)r{   r   r   )r   r   r   rw   r{   r  r   rn   r  rH  rh  )rL   r6  rc   coerced_argsrJ   )rc   rK   coerce_to_ctuple  s    

zSequenceNode.coerce_to_ctuplec          	   C   s   |    tdd | jD s | S g }g }xV| jD ]L}|jrr|rd|t|d j|dj|dd g }||j q0|| q0W |r|t|d j|dj|dd t	| j|| j
}| jrt| jd|| j|d| j
dd}|S )	Nc             s   s   | ]}|j V  qd S )N)rb  )rS   r  rJ   rJ   rK   rV     s    z?SequenceNode._create_merge_node_if_necessary.<locals>.<genexpr>r   )r{   T)r~  r   )inplacer   r   )_flatten_starred_argsr\   r{   rb  r   rh  rn   r   ra  MergedSequenceNoder   r   
binop_noder=  )rL   rc   r{   rM  r  rp   rJ   rJ   rK   _create_merge_node_if_necessary  s(    ""z,SequenceNode._create_merge_node_if_necessaryc             C   sV   g }x>| j D ]4}|jr6|jjr6|jjs6||jj  q|| qW || j d d < d S )N)r{   rb  ra  ru   r   r   r   )rL   r{   r  rJ   rJ   rK   r    s    z"SequenceNode._flatten_starred_argsc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN     s    zSequenceNode.may_be_nonec             C   s   | j rt| jd g | _g | _d| _xt| jD ]\}}|| }| j|< |j	rz|j
tsjt|jd |j
tkrzt|_
t| j|}||j
|}||k	rd| _| j| | j| q0W t| _
| S )Nz#can't assign to multiplied sequenceFz2starred target must have Python object (list) typeT)r   r   rn   unpacked_itemscoerced_unpacked_itemsZany_coerced_itemsrP  r{   r   rb  r   r3  r   r   rI  r"  r   )rL   rc   rV  r  Zunpacked_itemZcoerced_unpacked_itemrJ   rJ   rK   r   #  s*    
z!SequenceNode.analyse_target_typesc             C   s   |  | d S )N)generate_operation_code)rL   r   rJ   rJ   rK   r   :  s    z!SequenceNode.generate_result_codec             C   s  |d kr|   }d }}d }| jr~|s~| j}|jjr~|  }t|jtr^|jdkr^d|j }n |jjrtd||f }n
d|f }| jtkr| j	s| j
r|s|d|t| jddd	 | jD ||| jf  || n| jjrx.t| jD ] \}}|d
|||  f  qW nf| jtkr2d\}	}
n$| jtkrHd\}	}
ntd| j t| j}|d||	||||| jf  || |rtj}|d|  |dkr|}nd||f }|d||||f  nd}xt|D ]|}| j| }|s| s||  |  ||  |d|
||rL|rLd||f pT|pT|| f  qW |r|d |d |d k	r|jjr|dtj|| |tj| jf  |tj ||t  |d|tjf  |d d S )Nr  r   z * %sz * ((%s<0) ? 0:%s)z * (%s)z%s = PyTuple_Pack(%d, %s); %sz, c             s   s   | ]}|  V  qd S )N)r   )rS   r  rJ   rJ   rK   rV   T  s    z>SequenceNode.generate_sequence_packing_code.<locals>.<genexpr>z%s.f%s = %s;)Z
PyList_NewZPyList_SET_ITEM)ZPyTuple_NewZPyTuple_SET_ITEMz'sequence packing for unexpected type %sz%s = %s(%s%s); %sz{ Py_ssize_t %s;r$   z%s * %szfor (%s=0; %s < %s; %s++) {z%s(%s, %s, %s);z%s + %sr   z5{ PyObject* %s = PyNumber_InPlaceMultiply(%s, %s); %sz%s = %s;)!r   r   r   r+  rk   rl   r"   r  r   r   slowr   rw   r{   r  r   rn   r  rG  rP  r   r   r   r?  r%  r   r   r   r  r   ra   r  r   )rL   r   ra  plainZsize_factorZc_multr   rV  r  Zcreate_funcZset_item_funcZ	arg_countZcounteroffsetrJ   rJ   rK   generate_sequence_packing_code=  s    









"

z+SequenceNode.generate_sequence_packing_codec                sz   | j r"| j jjr"tt| | nT| jtkrJ| js8| jrJtt| | n,x| j	D ]}|
| qRW | j rv| j | d S )N)r   r   r+  rF   rz  r  r   r   r  r{   r	  r  )rL   r   r  )rI   rJ   rK   r    s    z+SequenceNode.generate_subexpr_disposal_codec             C   sH   | j r| || n| || x| jD ]}|| q(W || d S )N)r|   generate_starred_assignment_code!generate_parallel_assignment_coder  rH  r  )rL   rx   r   r
  r  r   rs   rJ   rJ   rK   r    s    z%SequenceNode.generate_assignment_coder  c             C   s   x| j D ]}|| qW |jtkp:|jttfkp:|jj }t| j dk}|r`| j|||d n(|	d | j
||| j |d |	d x| jD ]}|| qW x.tt| jD ]}| j| | j| | qW d S )NrC   )use_loopr0  r   )r  rG  r   r   r   r   r  rw   (generate_special_parallel_unpacking_coder   (generate_generic_parallel_unpacking_coder  r  r%  r{   r  )rL   rx   r   rs   Zspecial_unpackZlong_enough_for_a_loop
value_noderV  rJ   rJ   rK   r    s$    




z.SequenceNode.generate_parallel_assignment_codec          
   C   s  d}d|   }|jtkr.dg}| rx|}nJ|jtkrLdg}| rx|}n,ddg}d|   }d|   }d||f }|d|  |d	|    |d
 |dt| j  |j	t
 |dt| jt| jf  |j	t |d ||| j |d |d t|dkr<|d|d   x4t| jD ]&\}	}
|d|
 |d |	f  qHW t|dkr|d x4t| jD ]&\}	}
|d|
 |d |	f  qW |d x$| jD ]}
||
 |
  qW |d |sPxt| jD ]>\}	}
|d|
 |	||
 | jf  ||
  qW n|d |d |dt| jddd | jD f  |dt| j  |d|d | j  |d  |d! |d |d |d" || |dkr|d nr||krV|d |j	td#d$ |d%|| j  |d n(|d | j||| j|d& |d d S )'N1zlikely(%s != Py_None)r.  r/  zlikely(PyTuple_CheckExact(%s))zPyList_CheckExact(%s)z(%s) || (%s)z	if (%s) {zPyObject* sequence = %s;z2Py_ssize_t size = __Pyx_PySequence_SIZE(sequence);zif (unlikely(size != %d)) {z1if (size > %d) __Pyx_RaiseTooManyValuesError(%d);z9else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);r   z<#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFSr.   z(if (likely(Py%s_CheckExact(sequence))) {r   z"%s = Py%s_GET_ITEM(sequence, %d); z} else {r$   z#elsez&%s = PySequence_ITEM(sequence, %d); %sr0  zPy_ssize_t i;zPyObject** temps[%s] = {%s};r  c             S   s   g | ]}d |   qS )z&%s)r   )rS   rs   rJ   rJ   rK   rt     s    zISequenceNode.generate_special_parallel_unpacking_code.<locals>.<listcomp>zfor (i=0; i < %s; i++) {z1PyObject* item = PySequence_ITEM(sequence, i); %srs   z*(temps[i]) = item;z#endifZRaiseNoneIterErrorzObjectHandling.cz%__Pyx_RaiseNoneNotIterableError(); %s)r  )r   r   r   rN  r   r   rw   r{   r  r  raise_too_many_values_to_unpack raise_need_more_values_to_unpackr   rn   rP  r  r   r   r   r   r  r  r  r   r  r  )rL   r   rx   r  Zsequence_type_testZ
none_checkZsequence_typesZtuple_checkZ
list_checkrV  rs   rJ   rJ   rK   r    s    




















z5SequenceNode.generate_special_parallel_unpacking_codeTc          	   C   sF  |j t |j tdd |d |rV|dt| jddd |D f  |j	j
tdd	}|d
|| ||| jf  || || |j	j
| jdd	}|d||f  |d}d||f }	|r&|dt|  |d|	  || |d |d |d nNxLt|D ]@\}
}|d|
| |	| f  || ||  q0W |r|j tdd || jd|	t|f  |d|  ||t |d}|| || ||t |d|  |d ||| j || |j	| |rB|j	| d }|S )NZ
IterFinishzObjectHandling.czPy_ssize_t index = -1;zPyObject** temps[%s] = {%s};r  c             S   s   g | ]}d |   qS )z&%s)r   )rS   rs   rJ   rJ   rK   rt   /  s    zISequenceNode.generate_generic_parallel_unpacking_code.<locals>.<listcomp>T)r   z%s = PyObject_GetIter(%s); %sFz%s = Py_TYPE(%s)->tp_iternext;Zunpacking_failedz%s(%s)z$for (index=0; index < %s; index++) {z*PyObject* item = %s; if (unlikely(!item)) rs   z*(temps[index]) = item;r   z(index = %d; %s = %s; if (unlikely(!%s)) ZUnpackItemEndCheckz$__Pyx_IternextUnpackEndCheck(%s, %d)z
%s = NULL;Zunpacking_donezCif (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);)r  r  r  r   r  r   rw   r  r  r   r   r   r   r   rn   r  r  r(  	new_labelr*  put_gotorP  r   r  r  	put_labelr   r   )rL   r   rx   r  r  	terminateiterator_tempZiternext_funcZunpacking_error_labelZunpack_coderV  rs   Zunpacking_done_labelrJ   rJ   rK   r  '  sr    













z5SequenceNode.generate_generic_parallel_unpacking_codec             C   sV  xPt | jD ]:\}}|jr| j| }| jd | }| j|d d  }P qW dsRtd }|rx|D ]}	|	| q`W |d | j|||ddd}x(t |D ]\}}	| j| }
|
	| qW |d || |
 }|d||p| ||| jf  || |r&||t |j| n
|| |r&|jt |jjtjdd}|d	||f  |d
|t|f  |dt|||| jf  |d x"|d d d D ]}	|	| qW xt t|d d d | jd d d D ]\}\}	}|d |d|	 |||d f  |d|  |d |d|	 |||d f  |d ||	  |	| qW |d |jjtdd}|d|||t|||| jf  || |j| ||t |d|||f  |d |d|  |j| |d x*t | jD ]\}}|| j| | q2W d S )Nr$   Fr0  T)r  r  r   z%s = PySequence_List(%s); %s)r   z%s = PyList_GET_SIZE(%s);zif (unlikely(%s < %d)) {z)__Pyx_RaiseNeedMoreValuesError(%d+%s); %sr  z#if CYTHON_COMPILING_IN_CPYTHONz!%s = PyList_GET_ITEM(%s, %s-%d); z((PyVarObject*)%s)->ob_size--;z#elsez!%s = PySequence_ITEM(%s, %s-%d); z#endifz #if !CYTHON_COMPILING_IN_CPYTHONz*%s = PySequence_GetSlice(%s, 0, %s-%d); %sz%s = %s; %s = NULL;z	(void)%s;)rP  r{   rb  r  r   rG  r   r  r  r  r   r   r   rn   r  r  r   r   r   r  r  r  r  r   r   r   rw   r   r  r  r  )rL   rx   r   rV  r  Zstarred_targetZunpacked_fixed_items_leftZunpacked_fixed_items_rightr  rs   r  Ztarget_listZlength_tempZcoerced_argZsublist_temprJ   rJ   rK   r  l  s    










 






z-SequenceNode.generate_starred_assignment_codec             C   sX   x| j D ]}|| qW | jrTx| jD ]}|| q(W x| jD ]}|| qBW d S )N)r{   r  r  r  )rL   r   r  rJ   rJ   rK   r    s    zSequenceNode.annotate)F)NF)FNN)T)!rN   rO   rP   r   ru   r  r   r  r{  r}  r   r   r  r  r  rN  r   r   r  r  r  r   r  r3  r   r4  r(  r  r  r  r  r  rQ   rJ   rJ   )rI   rK   rz    s6   	

	
X 

X
EPrz  c               @   sr   e Zd ZeZdZdZdd ZdddZdd Z	d	d
 Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )rh  FzConstructing Python tuplec                sJ   | j s| jstS  fdd| jD }tdd |D r:tS  | j|jS )Nc                s   g | ]}|  qS rJ   )rr   )rS   r  )rc   rJ   rK   rt     s    z(TupleNode.infer_type.<locals>.<listcomp>c             s   s&   | ]}|j p|jp|jp|jV  qd S )N)ra   r   r  r  )rS   r   rJ   rJ   rK   rV     s   z'TupleNode.infer_type.<locals>.<genexpr>)r   r{   r   r\   declare_tuple_typern   r   )rL   rc   r  rJ   )rc   rK   rr     s    
zTupleNode.infer_typec             C   s(  t | jdkrd| _d| _| S |sVx2t| jD ]$\}}|jrBd|_||| j|< q.W | jst	dd | jD s|
| jdd | jD j| _d| _| S tj| |dd}||}|js|S td	d |jD s|S |jr|jjrt|jjtrd|_d|_n&|jjjs|j||_d|_d|_|S )
Nr   FTc             s   s,   | ]$}|j p"|jjp"|jjp"|jjV  qd S )N)rb  r   ra   r   r  )rS   r  rJ   rJ   rK   rV     s   z*TupleNode.analyse_types.<locals>.<genexpr>c             s   s   | ]}|j V  qd S )N)r   )rS   r  rJ   rJ   rK   rV     s    r$   )r~  c             s   s   | ]}|j V  qd S )N)r   )rS   ZchildrJ   rJ   rK   rV     s    )rw   r{   r   r   rP  rb  rx  r   r   r\   r  rn   r   rz  r  ru   r  rk   rl   r"   ra   r=  is_partly_literal)rL   rc   r~  rV  r  rp   rJ   rJ   rK   r     s>    
zTupleNode.analyse_typesc                sH   | j s
d S  fdd| j D }tdd |D r4d S  | j|}|jS )Nc                s   g | ]}|  qS rJ   )r   )rS   r  )rc   rJ   rK   rt      s    z-TupleNode.analyse_as_type.<locals>.<listcomp>c             s   s   | ]}|d kV  qd S )NrJ   )rS   rT   rJ   rJ   rK   rV     s    z,TupleNode.analyse_as_type.<locals>.<genexpr>)r{   r\   r  rn   r   )rL   rc   r   r   rJ   )rc   rK   r     s    zTupleNode.analyse_as_typec                s   | j jr||jr(| j j|jkr(| | S |tks8|tkrh fdd| jD }t| j|tddj	 ddS | 
 | S n&|jr| js| | S t| | S d S )Nc                s   g | ]}|  qS rJ   )r=  )rS   r  )rc   rJ   rK   rt     s    z'TupleNode.coerce_to.<locals>.<listcomp>r$   )r{   r   r   T)r~  )r   rG  r  r  r   r   r{   rh  rn   r   r=  r"  r   rz  )rL   r6  rc   r  rJ   )rc   rK   r"    s    zTupleNode.coerce_toc             C   s0   t | j| j| jd}t| jtr,t| j|_|S )N)r{   r   )r  rn   r{   r   rk   rl   r   r   )rL   rT   rJ   rJ   rK   as_list  s    zTupleNode.as_listc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rM    s    zTupleNode.is_simplec             C   s   dS )NTrJ   )rL   rJ   rJ   rK   r     s    zTupleNode.nonlocally_immutablec             C   s   t | jdkr| jS tjS d S )Nr   )rw   r{   r  r   empty_tuple)rL   rJ   rJ   rK   r   "  s    zTupleNode.calculate_result_codec             C   s   t dd | jD | _d S )Nc             S   s   g | ]
}|j qS rJ   )rl   )rS   r  rJ   rJ   rK   rt   *  s    z7TupleNode.calculate_constant_result.<locals>.<listcomp>)r   r{   rl   )rL   rJ   rJ   rK   r   (  s    z#TupleNode.calculate_constant_resultc          
   C   sD   |  |}yt|S  tk
r> } z| | W d d }~X Y nX d S )N)r{  r   rd  r   )rL   r   rM  r   rJ   rJ   rK   r   ,  s
    
zTupleNode.compile_time_valuec          
   C   s   t | jdkrd S | js| jrt| j| jr0| jnd g| j }|jtdd|d}|	|}|d k	r|
| j | j||| j d || | jr|| _q|d|  || j ||  | jf  ||   nd| jj_| | d S )Nr   r   r.   )r  r  )r  z"%s = PyNumber_Multiply(%s, %s); %sT)rw   r{   r   r  r   r   r   r  r   r  r   rn   r  r  r  r   r   r   r   r  r   r  )rL   r   r  Ztuple_targetr  rJ   rJ   rK   r  3  s$     


z!TupleNode.generate_operation_codeN)F)rN   rO   rP   r   r   r  r  rr   r   r   r"  r  rM  r   r   r   r   r  rJ   rJ   rJ   rK   rh    s   	
%
rh  c               @   s|   e Zd Zg ZeZdZdZdd Zdd Z	dd Z
d	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )r  FzConstructing Python listc             C   s   dS )NrJ   rJ   )rL   rc   rJ   rJ   rK   r   \  s    zListNode.type_dependenciesc             C   s   t S )N)r   )rL   rc   rJ   rJ   rK   rr   _  s    zListNode.infer_typec             C   s2   x| j D ]}|jrd|_qW t| |}||S )NT)r{   rb  rx  rz  r   r=  )rL   rc   r  rp   rJ   rJ   rK   r   c  s
    
zListNode.analyse_expressionsc          	   C   sN   t dd}t| j| _t| |}W d Q R X ||_|jr@d| _|	|}|S )NT)r  )
r   r   r{   original_argsrz  r   obj_conversion_errorsr  r   r  )rL   rc   errorsrp   rJ   rJ   rK   r   j  s    
zListNode.analyse_typesc       	      C   s2  |j rDx| jD ]}t| qW g | _| j|s@t| jd|  n|jsR|jr|j	t
jk	rt| j}| jrt| jjtr| jjdkrt| jd|  q|| jj9 }nt| jd|  |j	}t
||| _xBtt| jD ]0}| j| }t|tr|j}|||| j|< qW n|jr8t| j| t
jd||S | jrRt| jd|  n|jrt| jt|jjkrt| jd|  nxt| jt|jjk rt| jd| d	 xLtt | j|jjD ]4\}\}}t|tr|j}||j|| j|< qW || _n*|j!r| "||S t#| _t| jd|  | S )
NzCannot coerce list to type '%s'r   z4Cannot coerce non-positively multiplied list to '%s'z1Cannot coerce dynamically multiplied list to '%s')r  r   z%Cannot coerce multiplied list to '%s'zToo many members for '%s'zToo few members for '%s'r$   )$ra   r  r   r   r.  r   rn   r   r  r  r   Zc_void_typerw   r{   r   rk   rl   r"   rm  r%  r  r-  r  r"  r0  TypecastNoder   rr  r  r  r   rP  r  rG  r  r*   )	rL   r6  rc   rd   r  r  rV  r  r  rJ   rJ   rK   r"  t  sP    


"zListNode.coerce_toc             C   s   | S )NrJ   )rL   rJ   rJ   rK   r    s    zListNode.as_listc             C   s0   t | j| j| jd}t| jtr,t| j|_|S )N)r{   r   )rh  rn   r{   r   rk   rl   r   r   )rL   rT   rJ   rJ   rK   as_tuple  s    zListNode.as_tuplec             C   s6   | j jr&| jr&|jj| j ddd| _nt| | d S )NFT)r   Zstatic)r   r   r   r   r   r   rz  r   )rL   r   rJ   rJ   rK   r     s    zListNode.allocate_temp_resultc             C   s   | j jr
nt| | d S )N)r   r   rz  r   )rL   rc   rJ   rJ   rK   r     s    zListNode.release_temp_resultc             C   s"   | j rt dd | jD | _d S )Nc             S   s   g | ]
}|j qS rJ   )rl   )rS   r  rJ   rJ   rK   rt     s    z6ListNode.calculate_constant_result.<locals>.<listcomp>)r   r|   r{   rl   )rL   rJ   rJ   rK   r     s    z"ListNode.calculate_constant_resultc             C   s$   |  |}| jr || j|9 }|S )N)r{  r   r   )rL   r   lrJ   rJ   rK   r     s    
zListNode.compile_time_valuec          
   C   s|  | j jr.x| jD ]}t| qW | | nJ| j jr$| jr|d |dtj	  |dj
tj	| j d dt| jtj	f }nd}xvt| jD ]h\}}|j jr|jtdd |d	|  ||| |  f  q|d
|  ||| f  qW | jrx|d |d nT| j jrpxHt| j| j jjD ](\}}|d|  |j| f  qBW ntdd S )Nr0  zPy_ssize_t %s;z&for ({i} = 0; {i} < {count}; {i}++) {{)rV  countz+ (%d * %s)r  r  zStringTools.cz'memcpy(&(%s[%s%s]), %s, sizeof(%s[0]));z%s[%s%s] = %s;r   z%s.%s = %s;zList type never specified)r   ra   r  r   r  r   r   r   r   r?  r"  r   rw   r{   rP  r  r  r   r  rr  r  r  r  r   r   )rL   r   rd   r  rV  r  r  rJ   rJ   rK   r    sB    




z ListNode.generate_operation_codeN)rN   rO   rP   r  r   r   r   r  r   rr   r   r   r"  r  r  r   r   r   r   r  rJ   rJ   rJ   rK   r  P  s    
/	r  c                   sZ   e Zd Zg ZdZdZdddZdd Zdd Zd	d
 Z	dd Z
 fddZdd Z  ZS )ScopedExprNodeNTc             C   s.   |d k	r|| _ n| jr$t|| _ nd | _ d S )N)
expr_scopehas_local_scoper   ZGeneratorExpressionScope)rL   outer_scoper  rJ   rJ   rK   
init_scope  s
    zScopedExprNode.init_scopec             C   s   |  | d S )N)r  )rL   rc   rJ   rJ   rK   ry     s    z#ScopedExprNode.analyse_declarationsc             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   analyse_scoped_declarations
   s    z*ScopedExprNode.analyse_scoped_declarationsc             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r      s    zScopedExprNode.analyse_typesc             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   analyse_scoped_expressions   s    z)ScopedExprNode.analyse_scoped_expressionsc                sJ  t t| j}| jr| jjs&|| d S |d g }xDtdd | jj	 D D ]&\}}|j
sP|jjrP|jrP|| qPW |s|| |d d S | }| }|| | || |d}|| xT|j|fgtt| | D ]2\}	}
||	r||	 | || ||
 qW || |d || ||_d S )Nz{ /* enter inner scope */c             s   s   | ]}|d  r|V  qdS )r   NrJ   )rS   rs   rJ   rJ   rK   rV       s    z:ScopedExprNode.generate_evaluation_code.<locals>.<genexpr>z} /* exit inner scope */
exit_scope)rF   r  r  r  r  r  r   rR  rv  r  r  r   ra   r  r   Znew_loop_labelsZnew_error_label_generate_vars_cleanupr  r  error_labelr   r  Zget_loop_labelsZ
label_usedr  Zset_loop_labels)rL   r   Zgenerate_inner_evaluation_code
py_entriesr  r   Zold_loop_labelsZold_error_labelr  ZlabelZ	old_label)rI   rJ   rK   r     s<    
$







z'ScopedExprNode.generate_evaluation_codec             C   s<   x6|D ].}|j r*|| ||jd q|| qW d S )Nrj  )r  Zput_var_gotrefr  r   Zput_var_xdecref_clear)rL   r   r  r   rJ   rJ   rK   r  B   s
    

z%ScopedExprNode._generate_vars_cleanup)N)rN   rO   rP   r   r  r  r  ry  r  r   r  r  r  rQ   rJ   rJ   )rI   rK   r    s   
,r  c               @   sb   e Zd ZdgZdZeZdd Zdd Zdd Z	d	d
 Z
dd Zdd Zdd Zdd Zdd ZdS )ComprehensionNodeloopTc             C   s   | j S )N)r   )rL   rc   rJ   rJ   rK   rr   S   s    zComprehensionNode.infer_typec             C   s   | | j _| | d S )N)r   ra  r  )rL   rc   rJ   rJ   rK   ry  V   s    z&ComprehensionNode.analyse_declarationsc             C   s   | j | d S )N)r  ry  )rL   rc   rJ   rJ   rK   r  Z   s    z-ComprehensionNode.analyse_scoped_declarationsc             C   s   | j s| j|| _| S )N)r  r  r   )rL   rc   rJ   rJ   rK   r   ]   s    zComprehensionNode.analyse_typesc             C   s   | j r| j|| _| S )N)r  r  r   )rL   rc   rJ   rJ   rK   r  b   s    z,ComprehensionNode.analyse_scoped_expressionsc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  g   s    zComprehensionNode.may_be_nonec             C   s   |  | d S )N)r  )rL   r   rJ   rJ   rK   r   j   s    z&ComprehensionNode.generate_result_codec          	   C   s   | j tjkrd}n2| j tjkr$d}n | j tjkr6d}ntd| j  |d|  |||  | j	f  |
|   | j| d S )NzPyList_New(0)zPySet_New(NULL)zPyDict_New()z"illegal type for comprehension: %sz%s = %s; %s)r   r   r   r   r   r   r   r   r   rn   r  r  generate_execution_code)rL   r   Zcreate_coderJ   rJ   rK   r  m   s    z)ComprehensionNode.generate_operation_codec             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  }   s    zComprehensionNode.annotateN)rN   rO   rP   re  r   r   rl   rr   ry  r  r   r  rN  r   r  r  rJ   rJ   rJ   rK   r  K   s   r  c               @   s<   e Zd ZdgZdZejZdd Zdd Z	dd Z
d	d
 ZdS )ComprehensionAppendNoder  Nc             C   s*   | j || _ | j jjs&| j || _ | S )N)r  r   r   ra   r=  )rL   rc   rJ   rJ   rK   r      s    
z+ComprehensionAppendNode.analyse_expressionsc          	   C   s   | j jtkr&|jtdd d}n"| j jtkr8d}ntd| j j | j	
| ||d|| j  | j	 f | j | j	| | j	| d S )NListCompAppendz
Optimize.c__Pyx_ListComp_Append	PySet_Addz'Invalid type for comprehension node: %sz%s(%s, (PyObject*)%s))ra  r   r   r  r  r   r  r   r   r  r  r   r   r   rn   r  r  )rL   r   r@  rJ   rJ   rK   r     s     

z/ComprehensionAppendNode.generate_execution_codec             C   s   | j || d S )N)r  r  )rL   rc   r   rJ   rJ   rK   r     s    z5ComprehensionAppendNode.generate_function_definitionsc             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r     s    z ComprehensionAppendNode.annotate)rN   rO   rP   re  ra  r   ra  r   r   r  r  r  rJ   rJ   rJ   rK   r     s   r  c               @   s4   e Zd ZddgZdd Zdd Zdd Zd	d
 ZdS )DictComprehensionAppendNodekey_expr
value_exprc             C   sP   | j || _ | j jjs&| j || _ | j|| _| jjjsL| j|| _| S )N)r  r   r   ra   r=  r  )rL   rc   rJ   rJ   rK   r      s    

z/DictComprehensionAppendNode.analyse_expressionsc          	   C   s|   | j | | j| ||d| j | j  | j f | j | j | | j 	| | j| | j	| d S )Nz0PyDict_SetItem(%s, (PyObject*)%s, (PyObject*)%s))
r  r  r  r   r   ra  r   rn   r  r  )rL   r   rJ   rJ   rK   r     s    

z3DictComprehensionAppendNode.generate_execution_codec             C   s    | j || | j|| d S )N)r  r  r  )rL   rc   r   rJ   rJ   rK   r     s    z9DictComprehensionAppendNode.generate_function_definitionsc             C   s   | j | | j| d S )N)r  r  r  )rL   r   rJ   rJ   rK   r     s    z$DictComprehensionAppendNode.annotateN)rN   rO   rP   re  r   r  r  r  rJ   rJ   rJ   rK   r     s
   	r  c                   sT   e Zd ZdgZdZdZdZeZd fdd	Z	dd Z
dd	 Zd
d Zdd Z  ZS )InlinedGeneratorExpressionNodegenNTc                sj   |j j}d|_|d k	rJ|tttfks,t|||_|jt	||t
j|d tt| j|fd|i| d S )NT)ra  r   r  )rC  gbodyZ
is_inlinedr   r   r   r   Zinlined_comprehension_typerp  rJ  r   retval_cnamerF   r  ru  )rL   rn   r  Zcomprehension_typerU  r  )rI   rJ   rK   ru     s    z'InlinedGeneratorExpressionNode.__init__c             C   s
   | j dkS )N)r\   r  rR  )	orig_func)rL   rJ   rJ   rK   rN     s    z*InlinedGeneratorExpressionNode.may_be_nonec             C   s   | j S )N)r   )rL   rc   rJ   rJ   rK   rr      s    z)InlinedGeneratorExpressionNode.infer_typec             C   s   | j || _ | S )N)r  r   )rL   rc   rJ   rJ   rK   r      s    z,InlinedGeneratorExpressionNode.analyse_typesc          	   C   s>   | d|  | j ||  | jf  ||   d S )Nz!%s = __Pyx_Generator_Next(%s); %s)r   r   r  r   rn   r  )rL   r   rJ   rJ   rK   r      s    z3InlinedGeneratorExpressionNode.generate_result_code)N)rN   rO   rP   r   r  ra  r   r   r   ru  rN  rr   r   r   rQ   rJ   rJ   )rI   rK   r     s   r  c               @   sf   e Zd ZdZdgZdZdZdd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )r  z
    Merge a sequence of iterables into a set/list/tuple.

    The target collection is determined by self.type, which must be set externally.

    args    [ExprNode]
    r{   TzConstructing Python collectionc             C   s\   |t tfkrF|rF|d jrF|d jt k	rFt|d j|d jdd|d< tj| |||d d S )Nr   T)r{   r   )r{   r   )	r   r   ru   r   r  rn   r{   r   ru  )rL   rn   r{   r   rJ   rJ   rK   ru  !  s    zMergedSequenceNode.__init__c             C   s   g }xV| j D ]L}|jr*|jr*|jjdkr*q|js6|jrHdd |j D }n|j}|| qW | jtkrpt|}n"| jt	krt
|}n| jtkst|| _d S )Nr   c             s   s   | ]}|j V  qd S )N)rl   )rS   r  rJ   rJ   rK   rV   !  s    z?MergedSequenceNode.calculate_constant_result.<locals>.<genexpr>)r{   ru   r   rl   rZ  r   r   r   r   r   r   r   r   )rL   r   rs   r  rJ   rJ   rK   r   !  s    



z,MergedSequenceNode.calculate_constant_resultc          
      s   g }xb| j D ]X}|jr.|jr.|j dkr.q|js:|jrP fdd|j D }n
| }|| qW | jtkryt|}W q t	k
r } z| 
| W d d }~X Y qX n"| jtkrt|}n| jtkst|S )Nr   c             3   s   | ]}|  V  qd S )N)r   )rS   r  )r   rJ   rK   rV   *!  s    z8MergedSequenceNode.compile_time_value.<locals>.<genexpr>)r{   ru   r   r   rZ  r   r   r   r   rd  r   r   r   r   r   )rL   r   r   rs   r  r   rJ   )r   rK   r   "!  s$    



z%MergedSequenceNode.compile_time_valuec             C   s   dS )NrJ   rJ   )rL   rc   rJ   rJ   rK   r   9!  s    z$MergedSequenceNode.type_dependenciesc             C   s   | j S )N)r   )rL   rc   rJ   rJ   rK   rr   <!  s    zMergedSequenceNode.infer_typec                sV    fdd| j D }t|dkr8|d j| jkr8|d S | jtttfksLt|| _ | S )Nc                s"   g | ]}|   d qS )z2argument after * must be an iterable, not NoneType)r   r=  rS  )rS   r  )rc   rJ   rK   rt   A!  s   z4MergedSequenceNode.analyse_types.<locals>.<listcomp>r$   r   )r{   rw   r   r   r   r   r   )rL   rc   r{   rJ   )rc   rK   r   ?!  s    

z MergedSequenceNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  P!  s    zMergedSequenceNode.may_be_nonec       
   
   C   s  | | j | | | jtk}t| j}t|}|| |rF|j	sZ|s|j
r|jtkr|d|  | f  || nL|d|  |rdnd| ||  | jf  ||   || || t }|rd}d}nd}d}x|D ]}|r|j	s"|j
s"|j
r|js|s:|jr:|d	 xN|jD ]D}|| ||jd
||  | f  || || qBW q|r|d n
|d || ||jd
||  | f  || || qW | jtkrh|d |dtj|  f  ||  t |d|  tj||  | jf  ||   |d x$t|D ]}	|jt j!|	  qrW d S )Nz%s = %s;z%s = %s(%s); %sZ	PySet_NewZPySequence_Listr  Z__Pyx_PySet_Updater  Z__Pyx_PyList_Extend)r  z
Optimize.cz
%s(%s, %s))ZPySet_Updatez
Builtins.c)Z
ListExtendz
Optimize.cr0  z"PyObject *%s = PyList_AsTuple(%s);z%s = %s; %sr   )"r   rn   r   r   r   r  r{   r  r  rZ  ru   r   r   r   r   r	  r   r  r  r  r   r   rQ  r  r   r   r?  r  r   rR  r  r  r   r  )
rL   r   Zis_setr{   rs   r[  Zadd_funcZextend_funcr  r\  rJ   rJ   rK   r  S!  s|    


















z+MergedSequenceNode.generate_evaluation_codec             C   s   x| j D ]}|| qW d S )N)r{   r  )rL   r   rs   rJ   rJ   rK   r  !  s    zMergedSequenceNode.annotateN)rN   rO   rP   r`  r   r   r  ru  r   r   r   rr   r   rN  r  r  rJ   rJ   rJ   rK   r     s   Kr  c               @   sJ   e Zd ZdZdgZeZdZdZdd Z	dd Z
d	d
 Zdd Zdd ZdS )SetNodez
    Set constructor.
    r{   TzConstructing Python setc             C   sL   x:t t| jD ](}| j| }||}||| j|< qW t| _d| _| S )Nr$   )r%  rw   r{   r   r=  r   r   r   )rL   rc   rV  r  rJ   rJ   rK   r   !  s    

zSetNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  !  s    zSetNode.may_be_nonec             C   s   t dd | jD | _d S )Nc             S   s   g | ]
}|j qS rJ   )rl   )rS   r  rJ   rJ   rK   rt   !  s    z5SetNode.calculate_constant_result.<locals>.<listcomp>)r   r{   rl   )rL   rJ   rJ   rK   r   !  s    z!SetNode.calculate_constant_resultc          
      sN    fdd| j D }yt|S  tk
rH } z| | W d d }~X Y nX d S )Nc                s   g | ]}|  qS rJ   )r   )rS   r  )r   rJ   rK   rt   !  s    z.SetNode.compile_time_value.<locals>.<listcomp>)r{   r   rd  r   )rL   r   rM  r   rJ   )r   rK   r   !  s
    zSetNode.compile_time_valuec             C   s   x| j D ]}|| qW | | |d|  ||  | jf  ||   x@| j D ]6}|	| jd|  | f  |
| || q^W d S )Nz%s = PySet_New(0); %szPySet_Add(%s, %s))r{   r  r   r   r   r   rn   r  r   r  r  r  )rL   r   r  rJ   rJ   rK   r  !  s    

z SetNode.generate_evaluation_codeN)rN   rO   rP   r`  r   r   r   rZ  r  r   rN  r   r   r  rJ   rJ   rJ   rK   r  !  s   	r  c               @   s   e Zd ZdgZdZdZeZdZdZ	g Z
edd Zdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd ZdZdd Zdd ZdS )r  r  r$   FTc                s   |   fdd|D dS )Nc                s   g | ]\}}t  ||d qS ))r
  r   )r  )rS   krN  )rn   rJ   rK   rt   !  s    z'DictNode.from_pairs.<locals>.<listcomp>)r  rJ   )rH   rn   pairsrJ   )rn   rK   
from_pairs!  s    zDictNode.from_pairsc             C   s   t dd | jD | _d S )Nc             S   s   g | ]
}|j qS rJ   )rl   )rS   rs   rJ   rJ   rK   rt   !  s    z6DictNode.calculate_constant_result.<locals>.<listcomp>)r  r  rl   )rL   rJ   rJ   rK   r   !  s    z"DictNode.calculate_constant_resultc          
      sN    fdd| j D }yt|S  tk
rH } z| | W d d }~X Y nX d S )Nc                s$   g | ]}|j  |j fqS rJ   )r
  r   r   )rS   rs   )r   rJ   rK   rt   !  s   z/DictNode.compile_time_value.<locals>.<listcomp>)r  r  rd  r   )rL   r   r  r   rJ   )r   rK   r   !  s    

zDictNode.compile_time_valuec             C   s   dS )NrJ   rJ   )rL   rc   rJ   rJ   rK   r   !  s    zDictNode.type_dependenciesc             C   s   t S )N)r   )rL   rc   rJ   rJ   rK   rr   !  s    zDictNode.infer_typec          	      s6   t dd} fdd| jD | _W d Q R X || _| S )NT)r  c                s   g | ]}|  qS rJ   )r   )rS   rs   )rc   rJ   rK   rt   !  s   z*DictNode.analyse_types.<locals>.<listcomp>)r   r  r  )rL   rc   r  rJ   )rc   rK   r   !  s
    
zDictNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  "  s    zDictNode.may_be_nonec                s  |j rn|   | jjrNt|s0t| jd|  t| j fdd| j	D dS | j|sjt| jd|  n*|jr|| _|j
st| j	dkrt| jd|  n.|j
rt| j	t|jjk rt| jd| d x| j	D ]}t|jtr|jj|_|jjst|jjd	 t|jjd
d|_qt|jj}|j|}|sTt|jjd||f  q|j}t|trl|j}||j |_qW nt| _t| jd|  | S )Nz-Cannot interpret struct as non-dict type '%s'c                s,   g | ]$}t |j|j |j d qS ))r
  r   )r  rn   r
  r=  r   )rS   rs   )rc   rJ   rK   rt   "  s   z&DictNode.coerce_to.<locals>.<listcomp>)r  z"Cannot interpret dict as type '%s'r$   z<Exactly one field must be specified to convert to union '%s'z%Not all members given for struct '%s'zInvalid struct field identifierz<error>)r   zstruct '%s' has no field '%s')ra   release_errorsr   r  r   r.  r   rn   r  r  rr  rw   r  r  r   rk   r
  r-  r  r[  r  rg   r   r  r"  r*   )rL   r6  rc   rs   r
  r  r   rJ   )rc   rK   r"  "  sB    



zDictNode.coerce_toc             C   s"   x| j D ]}t| qW g | _ d S )N)r  r   )rL   rd   rJ   rJ   rK   r  /"  s    zDictNode.release_errorszConstructing Python dictc          	   C   s$  | | j | | | jj}|rd|   |d|  t| j	|
|  | jf  ||   t }d }d}x| j	D ]}|| |r| jr|d|j   |j}| jrt|d k	r$|jsd }nT|j|krd }nD|t|jk	r|d krt|j}||j nd }n||j |d krt|d|  | f  d}|d| ||jf  |d || jd|  |j |j f  | jr|d kr|d	 | jr|d	 n"|d
|  |jj|j f  || || q|W |r |jtdd d S )Nz%%s = __Pyx_PyDict_NewPresized(%d); %sFz	if (%s) {z(if (unlikely(PyDict_Contains(%s, %s))) {Tz2__Pyx_RaiseDoubleKeywordsError("function", %s); %sz} else {zPyDict_SetItem(%s, %s, %s)r   z%s.%s = %s;rZ  zFunctionArguments.c)r   rn   r   r   ra   r  r   r   rw   r  r   r  r   r   r  exclude_null_valuesr   r
  rX  r[  rQ  r   r  r  r  r  r  r   r  )rL   r   Zis_dictZ	keys_seenZkey_typeZneeds_error_helperrs   r
  rJ   rJ   rK   r  6"  sr    










z!DictNode.generate_evaluation_codec             C   s   x| j D ]}|| qW d S )N)r  r  )rL   r   rs   rJ   rJ   rK   r  "  s    zDictNode.annotateN)rN   rO   rP   r   r   r  r   r   rY  rX  r  rf  r  r   r   r   rr   r   rN  r"  r  r  r  r  rJ   rJ   rJ   rK   r  !  s&   	'Jr  c               @   sH   e Zd ZddgZdZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )r  r
  r   Nc             C   s   | j j| jjf| _d S )N)r
  rl   r   )rL   rJ   rJ   rK   r   "  s    z&DictItemNode.calculate_constant_resultc             C   s<   | j || _ | j|| _| j || _ | j|| _| S )N)r
  r   r   r=  )rL   rc   rJ   rJ   rK   r   "  s
    zDictItemNode.analyse_typesc             C   s   | j | | j| d S )N)r
  r  r   )rL   r   rJ   rJ   rK   r  "  s    z%DictItemNode.generate_evaluation_codec             C   s   | j | | j| d S )N)r
  r  r   )rL   r   rJ   rJ   rK   r  "  s    z#DictItemNode.generate_disposal_codec             C   s   | j | | j| d S )N)r
  r  r   )rL   r   rJ   rJ   rK   r  "  s    zDictItemNode.free_tempsc             C   s   t | j| jgS )N)r  r
  r   )rL   rJ   rJ   rK   __iter__"  s    zDictItemNode.__iter__)rN   rO   rP   r   r   r   r   r  r  r  r  rJ   rJ   rJ   rK   r  "  s   r  c               @   s6   e Zd ZdgZdZdd Zdd Zdd Zd	d
 ZdS )SortedDictKeysNoder  Tc             C   s   t j| |j|d tj| _d S )N)r  )r   ru  rn   r   r   r   )rL   r  rJ   rJ   rK   ru  "  s    zSortedDictKeysNode.__init__c             C   s,   | j |}|jtjkr"|d}|| _ | S )Nz!'NoneType' object is not iterable)r  r   r   r   r   rS  )rL   rc   r  rJ   rJ   rK   r   "  s    z SortedDictKeysNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  "  s    zSortedDictKeysNode.may_be_nonec          
   C   s  | j  }| j jtjkrN|d|  |||  | jf  |	|   n|j
tdd |td}|d|  ||||  | jf  |	|   |d|    ||  d|    |||  | j |	|   |d || jd	|    d S )
Nz%s = PyDict_Keys(%s); %sZPyObjectCallMethod0zObjectHandling.ckeysz+%s = __Pyx_PyObject_CallMethod0(%s, %s); %sz"if (unlikely(!PyList_Check(%s))) {zPySequence_List(%s)r   zPyList_Sort(%s))r  r   r   r   r   r   r   r   rn   r  r  r  r   r  r  r	   r  r  r  )rL   r   Zdict_resultZ
keys_cnamerJ   rJ   rK   r   "  s(    




z'SortedDictKeysNode.generate_result_codeN)	rN   rO   rP   r   r   ru  r   rN  r   rJ   rJ   rJ   rK   r  "  s   r  c               @   s   e Zd Zdd Zdd ZdS )ModuleNameMixinc             C   s   |j | jddS )NT)r  )r  r  )rL   r   rJ   rJ   rK   get_py_mod_name"  s    zModuleNameMixin.get_py_mod_namec             C   s   |j | jddS )NT)r  )r  qualname)rL   r   rJ   rJ   rK   get_py_qualified_name"  s    z%ModuleNameMixin.get_py_qualified_nameN)rN   rO   rP   r  r  rJ   rJ   rJ   rK   r  "  s   r  c               @   s@   e Zd ZddgZeZdZdd Zdd Zdd	 Z	d
Z
dd ZdS )	ClassNodebasesdocTc             C   s   t S )N)r   )rL   rc   rJ   rJ   rK   rr   "  s    zClassNode.infer_typec             C   sF   | j || _ | jr0| j|| _| j|| _|tdd | S )NZCreateClasszObjectHandling.c)r  r   r  r=  r  r   r  )rL   rc   rJ   rJ   rK   r   "  s    zClassNode.analyse_typesc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rN  "  s    zClassNode.may_be_nonezConstructing Python classc             C   s   | | j}| jrB|| jd| j | td| j f  | 	|}| 
|}|d|  | j | j |||||  | jf  ||   d S )NzPyDict_SetItem(%s, %s, %s)r`  z.%s = __Pyx_CreateClass(%s, %s, %s, %s, %s); %s)r  rb   r  r  rn   r  r   r	   r  r  r  r   r   r  r   r  )rL   r   r   py_mod_namer  rJ   rJ   rK   r   #  s(    


zClassNode.generate_result_codeN)rN   rO   rP   r   r   r   r   rr   r   rN  r  r   rJ   rJ   rJ   rK   r  "  s   r  c               @   s<   e Zd Zg ZeZdZdd Zdd Zdd Z	dZ
d	d
 ZdS )Py3ClassNodeTc             C   s   t S )N)r   )rL   rc   rJ   rJ   rK   rr   (#  s    zPy3ClassNode.infer_typec             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   ,#  s    zPy3ClassNode.analyse_typesc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rN  /#  s    zPy3ClassNode.may_be_nonezConstructing Python classc             C   s   |j tdd || j}| jr2| j }nd}| jrH| j }nd}|	d| 
 ||| j | j || j| j|| 
 | jf	  ||   d S )NZPy3ClassCreatezObjectHandling.crr  z$((PyObject*)&__Pyx_DefaultClassType)z9%s = __Pyx_Py3ClassCreate(%s, %s, %s, %s, %s, %d, %d); %s)r  r  r   r  r  rb   mkwr   	metaclassr   r   r  r  Zcalculate_metaclassZallow_py2_metaclassr   rn   r  )rL   r   r   r  r  rJ   rJ   rK   r   4#  s(    z!Py3ClassNode.generate_result_codeN)rN   rO   rP   r   r   r   r   rr   r   rN  r  r   rJ   rJ   rJ   rK   r  #  s   r  c               @   s(   e Zd Zg Zdd Zdd Zdd ZdS )PyClassMetaclassNodec             C   s   t | _d| _| S )NT)r   r   r   )rL   rc   rJ   rJ   rK   r   U#  s    z"PyClassMetaclassNode.analyse_typesc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rN  Z#  s    z PyClassMetaclassNode.may_be_nonec          	   C   s   | j r4|jtdd d| j | j  f }n"|jtdd d| j  }|d|  |||  | j	f  |
|   d S )NZPy3MetaclassGetzObjectHandling.cz__Pyx_Py3MetaclassGet(%s, %s)ZCalculateMetaclassz"__Pyx_CalculateMetaclass(NULL, %s)z%s = %s; %s)r  r  r  r   r  r  r   r   r   rn   r  r   )rL   r   ZcallrJ   rJ   rK   r   ]#  s    z)PyClassMetaclassNode.generate_result_codeN)rN   rO   rP   r   r   rN  r   rJ   rJ   rJ   rK   r  M#  s   r  c               @   s*   e Zd ZdgZdd Zdd Zdd ZdS )	PyClassNamespaceNoder  c             C   s2   | j r"| j || _ | j || _ t| _d| _| S )Nr$   )r  r   r=  r   r   r   )rL   rc   rJ   rJ   rK   r   z#  s    z"PyClassNamespaceNode.analyse_typesc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rN  #  s    z PyClassNamespaceNode.may_be_nonec             C   s   | | j}| |}| |}| jr2| j }nd}| jrH| j }nd}| jr^| j }nd}|	d|  || j
 |||||||  | jf	  ||   d S )Nz(PyObject *) NULLz>%s = __Pyx_Py3MetaclassPrepare(%s, %s, %s, %s, %s, %s, %s); %s)r  rb   r  r  r  r   r  r   r  r   r  r   rn   r  )rL   r   r   r  r  Zdoc_coder  r  rJ   rJ   rK   r   #  s0    

z)PyClassNamespaceNode.generate_result_codeN)rN   rO   rP   r   r   rN  r   rJ   rJ   rJ   rK   r  o#  s   	r  c               @   s4   e Zd ZdZeZg ZdZdd Zdd Z	dd Z
d	S )
ClassCellInjectorNodeTFc             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   #  s    z)ClassCellInjectorNode.analyse_expressionsc             C   sF   | j rB| | |d|  ||  | jf  ||   d S )Nz%s = PyList_New(0); %s)	is_activer   r   r   r   rn   r  )rL   r   rJ   rJ   rK   r  #  s    
z.ClassCellInjectorNode.generate_evaluation_codec             C   s8   | j r4|jtdd || jd|  |f  d S )NZCyFunctionClassCellzCythonFunction.cz&__Pyx_CyFunction_InitClassCell(%s, %s))r  r  r  r   r  r  rn   r   )rL   r   Zclassobj_cnamerJ   rJ   rK   generate_injection_code#  s
    
z-ClassCellInjectorNode.generate_injection_codeN)rN   rO   rP   r   r   r   r   r  r   r  r  rJ   rJ   rJ   rK   r  #  s   	r  c               @   s,   e Zd Zg ZdZdZeZdd Zdd Z	dS )ClassCellNodeTFc             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   #  s    zClassCellNode.analyse_typesc             C   sj   | j s |d|  tjf  n|d|  tjf  |d|  || jf  ||  t	 d S )Nz&%s = __Pyx_CyFunction_GetClassObj(%s);z%s =  %s->classobj;zTif (!%s) { PyErr_SetString(PyExc_SystemError, "super(): empty __class__ cell"); %s })
is_generatorr   r   r   
self_cnamegenerator_cnamer   rn   r   r   )rL   r   rJ   rJ   rK   r   #  s    z"ClassCellNode.generate_result_codeN)
rN   rO   rP   r   r   r  r   r   r   r   rJ   rJ   rJ   rK   r  #  s   r  c               @   s   e Zd ZddddgZdZdZdZdZdZdZ	dZ
dZdZdZeZdZdZdZed	d
 Zdd Zdd Zdd Zdd ZdZdd Zdd Zdd Zdd ZdS )PyCFunctionNodecode_objectdefaults_tupledefaults_kwdictannotations_dictNFr   r$   c             C   s&   | |j ||jj|p|j|jt|dS )N)rC  pymethdef_cnamebindingspecialized_cpdefsr  )rn   r   r  r  CodeObjectNode)rH   rp   r  rJ   rJ   rK   from_defnode#  s    zPyCFunctionNode.from_defnodec             C   s   | j r| | | S )N)r  analyse_default_args)rL   rc   rJ   rJ   rK   r   $  s    
zPyCFunctionNode.analyse_typesc             C   s  g }g }g }g }g }|j p&| jjo&|j}x| jjD ]}|jr|s|jjsnd|_|jj	rb|
| q~|
| nt|j|j|_|jr|
| n
|
| |jr2| ||j|_|
|j|j|jf q2W xH| jj| jjfD ]4}|r|jr| ||j|_|
|j|j|jf qW | jj}	|	rN| ||	}	|	| j_|
|	jtd|	f |sZ|rZ| }
|
tj}t|}g | _x>|D ]6}|j|j|jdtj|j dd}| j
||f qW x@|D ]8}|j|j|jdtj|j ddd}| j
||f qW |
jdd|dd|d	}|| _ t!|| _"x&| jD ]\}}d
tj#|j$f |_%q.W | j j| j_ |sf|r| j dkr|rt&| jdd |D d}|'|(|| _)|rt*| jdd |D d}|'|| _+n|rt,| j|| j }n
t-| j}|rt.| j|| j }n
t-| j}t/j0| jg ddt/j1| jt2t&| j||gdddtdd}| }
|3|
 |4|
}|j54|j6|_5d|_7d|_8|| j_9|rt*| jdd |D d}|'|| _:dS )zB
        Handle non-literal function's default arguments.
        TreturnN)allow_pyobjectF)r  Zallow_memoryviewZstructr$   )r   z%s->%sc             S   s   g | ]
}|j qS rJ   )rX   )rS   r  rJ   rJ   rK   rt   P$  s    z8PyCFunctionNode.analyse_default_args.<locals>.<listcomp>)r{   c             S   s*   g | ]"}t |jt|j|jd |jdqS ))r   )r
  r   )r  rn   r  rb   rX   )rS   r  rJ   rJ   rK   rt   T$  s   )r  )r  r   __defaults__)r{   rD  rE  bodyZ
decoratorsrb   c             S   s(   g | ] \}}}t |t||d |dqS ))r   )r
  r   )r  r  )rS   rn   rb   r   rJ   rJ   rK   rt   y$  s   );is_c_class_scoperC  Z
is_wrapperr  r{   rX   r   Z
is_dynamicr   ra   r   DefaultLiteralArgNodern   Zkw_onlyrW  analyse_annotationrb   rD  rE  Zreturn_type_annotationr	   r  r  next_idr   Zdefaults_struct_prefixr   ZStructOrUnionScopedefaultsr  Z
arg_prefixZdeclare_struct_or_uniondefaults_structrw   defaults_pyobjectsZdynamic_args_cnamer   r  rh  r   r=  r  r  r  DefaultsTupleNoderi  DefaultsKwDictNoder   ZDefNodeZReturnStatNoder   ry  r   r  local_scopepy_wrapper_requiredpymethdef_requireddefaults_getterr  )rL   rc   Znonliteral_objectsZnonliteral_otherZdefault_argsZdefault_kwargsZannotationsZmust_use_constantsr  rW  ri  r   r  r   r  r  r  r  rJ   rJ   rK   r  $  s    















z$PyCFunctionNode.analyse_default_argsc             C   s\   |d krd S | |}|d k	r<t|jt|jdddd}||}|jjsX|	|}|S )Nr  T)Zfor_display)r   )
r   r  rn   r	   r  r   r   r   ra   r=  )rL   rc   rW  r  rJ   rJ   rK   r  $  s    


z"PyCFunctionNode.analyse_annotationc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  $  s    zPyCFunctionNode.may_be_nonezConstructing Python functionc             C   s   | j d krd}n
| j  }|S )Nrr  )self_objectr   )rL   Zself_resultrJ   rJ   rK   self_result_code$  s    

z PyCFunctionNode.self_result_codec             C   s    | j r| | n
| | d S )N)r  generate_cyfunction_codegenerate_pycfunction_code)rL   r   rJ   rJ   rK   r   $  s    z$PyCFunctionNode.generate_result_codec             C   sL   |  |}|d|  | j|  |||  | jf  ||   d S )Nz'%s = PyCFunction_NewEx(&%s, %s, %s); %s)	r  r   r   r  r  r   rn   r  r   )rL   r   r  rJ   rJ   rK   r  $  s    
z)PyCFunctionNode.generate_pycfunction_codec       
      C   s  | j r| j d }n| j}| j s$| jr>|jtdd d}n|jtdd d}| jrh| j }nd}g }|j	r|
d n|jr|
d	 |jjjr|jjs|
d
 |rd|}nd}|d|  || j|| ||  | |tj|||  | jf
  ||   |jrt|js0td|jd }| |  t! |d|j" |  f  |#|   | j$r|d|  | j%j&| j'|(| jf  d| j%j&|  f }x,| j$D ]"\}}	|j)|d||	j*f d qW | j+r
|d|  | j+ f  | j,r.|d|  | j, f  |j-rZ| j sZ|d|  |j-jj.f  | j/r~|d|  | j/ f  d S )Nr   ZFusedFunctionzCythonFunction.cZ__pyx_FusedFunction_NewExZCythonFunctionZ__Pyx_CyFunction_NewExrr  Z__Pyx_CYFUNCTION_STATICMETHODZ__Pyx_CYFUNCTION_CLASSMETHODZ__Pyx_CYFUNCTION_CCLASSz | r  z(%s = %s(&%s, %s, %s, %s, %s, %s, %s); %szpyclass_stack is emptyr  zPyList_Append(%s, %s);z:if (!__Pyx_CyFunction_InitDefaults(%s, sizeof(%s), %d)) %sz!__Pyx_CyFunction_Defaults(%s, %s)z%s->%s)ra  z*__Pyx_CyFunction_SetDefaultsTuple(%s, %s);z+__Pyx_CyFunction_SetDefaultsKwDict(%s, %s);z+__Pyx_CyFunction_SetDefaultsGetter(%s, %s);z,__Pyx_CyFunction_SetAnnotationsDict(%s, %s);)0r  rC  is_specializationr  r  r   r  r  r   Zis_staticmethodr   Zis_classmethodr  rj  r  r   Zis_anonymousr  r   r   r  r  r  r  r   r  r   rn   r  Zrequires_classobjZpyclass_stackr   r   r   Z
class_cellr  r  r  rb   r  r   r  r   r  r  r  rH  r  )
rL   r   rC  r  Zcode_object_resultflagsZ
class_noder  r  r   rJ   rJ   rK   r  $  s    


z(PyCFunctionNode.generate_cyfunction_code)rN   rO   rP   r   r  r  r  rC  r  r  r  r  r  r  r   r   r   r  r  rf  r  r   r  r  rN  r  r  r   r  r  rJ   rJ   rJ   rK   r  #  s4   	yr  c               @   s   e Zd ZdZdZdd ZdS )InnerFunctionNodeTc             C   s   | j rdtj S dS )Nz((PyObject*)%s)rr  )needs_self_coder   cur_scope_cname)rL   rJ   rJ   rK   r  %  s    
z"InnerFunctionNode.self_result_codeN)rN   rO   rP   r  r   r  rJ   rJ   rJ   rK   r   %  s   r  c               @   s<   e Zd ZdgZdZdZdd Zdd Zddd	Zd
d Z	dS )r  varnamesFNc             C   sT   t j| |j|d t|j}dd |jjD }t|jdd || D ddd| _d S )N)rC  c             S   s   g | ]}|j r|qS rJ   )rb   )rS   r  rJ   rJ   rK   rt   %  s    z+CodeObjectNode.__init__.<locals>.<listcomp>c             S   s   g | ]}t |j|jd qS ))r   )r  rn   rb   )rS   r  rJ   rJ   rK   rt   %  s   r   r$   )r{   r   r   )	r   ru  rn   r   r{   r  r  rh  r  )rL   rC  r{   
local_varsrJ   rJ   rK   ru  %  s    

zCodeObjectNode.__init__c             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  #%  s    zCodeObjectNode.may_be_nonec             C   s"   | j d kr|jtddd| _ | j S )Ncodeobjr.   )r  )r  r  r   )rL   r   rJ   rJ   rK   r   &%  s    
z$CodeObjectNode.calculate_result_codec             C   s,  | j d kr|jtddd| _ || j }|d kr4d S || j | j}|j|jdd|jd}t	
|jd  dd}|j|ddd	}d
dg}| jjr|d | jjr|d |d| j t|j|j |jt| jjd|pdtjtjtj| j tjtj||| jd tj|| j | jf  d S )Nr  r.   )r  TF)r  r  r  r   utf8)r  r  ZCO_OPTIMIZEDZCO_NEWLOCALSZ
CO_VARARGSZCO_VARKEYWORDSz_%s = (PyObject*)__Pyx_PyCode_New(%d, %d, %d, 0, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %s); %s|r  r$   )r  r  r   r  r   rn   rC  r  rb   r	   r  Zget_filenametable_entryrQ  rD  r   rE  r   rw   r{   rF  r  r  r   Zempty_bytesr  r   r   )rL   r   func	func_nameZ	file_pathZfile_path_constr  rJ   rJ   rK   r   +%  sB    



z#CodeObjectNode.generate_result_code)N)
rN   rO   rP   r   r   r  ru  rN  r   r   rJ   rJ   rJ   rK   r  %  s   
r  c                   sH   e Zd Zg ZdZdZ fddZdd Zdd Zd	d
 Z	dd Z
  ZS )r  TFc                s*   t t| | || _| jj| _d| _d S )NF)rF   r  ru  r  r   	evaluated)rL   rn   r  )rI   rJ   rK   ru  _%  s    
zDefaultLiteralArgNode.__init__c             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   e%  s    z#DefaultLiteralArgNode.analyse_typesc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   h%  s    z*DefaultLiteralArgNode.generate_result_codec             C   s   | j s| j| d| _ d S )NT)r	  r  r  )rL   r   rJ   rJ   rK   r  k%  s    z.DefaultLiteralArgNode.generate_evaluation_codec             C   s   | j | j S )N)r   r;  r  r   )rL   rJ   rJ   rK   r   p%  s    zDefaultLiteralArgNode.result)rN   rO   rP   r   r   r   ru  r   r   r  r   rQ   rJ   rJ   )rI   rK   r  V%  s   r  c                   s8   e Zd Zg Z fddZdd Zdd Zdd Z  ZS )	DefaultNonLiteralArgNodec                s    t t| | || _|| _d S )N)rF   r
  ru  r  r  )rL   rn   r  r  )rI   rJ   rK   ru  y%  s    z!DefaultNonLiteralArgNode.__init__c             C   s   | j j| _d| _| S )NF)r  r   r   )rL   rc   rJ   rJ   rK   r   ~%  s    
z&DefaultNonLiteralArgNode.analyse_typesc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   %  s    z-DefaultNonLiteralArgNode.generate_result_codec             C   s"   d| j jtj| j | jjjf S )Nz%__Pyx_CyFunction_Defaults(%s, %s)->%s)r  rb   r   r  r  r  r   )rL   rJ   rJ   rK   r   %  s    
zDefaultNonLiteralArgNode.result)	rN   rO   rP   r   ru  r   r   r   rQ   rJ   rJ   )rI   rK   r
  t%  s
   r
  c                   s*   e Zd Z fddZd fdd	Z  ZS )r  c                sP   g }x2|D ]*}|j js$t|||}n|j }|| q
W tt| j||d d S )N)r{   )rX   r   r
  r   rF   r  ru  )rL   rn   r  r  r{   r  )rI   rJ   rK   ru  %  s    
zDefaultsTupleNode.__init__Fc                s   t t| |||S )N)rF   r  r   r=  )rL   rc   r~  )rI   rJ   rK   r   %  s    zDefaultsTupleNode.analyse_types)F)rN   rO   rP   ru  r   rQ   rJ   rJ   )rI   rK   r  %  s   
r  c                   s   e Zd Z fddZ  ZS )r  c                sl   g }xN|D ]F}t |j|jd}|jjs4t|||}n|j}|t|j||d q
W tt	| j
||d d S )N)r   )r
  r   )r  )r  rn   rb   rX   r   r
  r   r  rF   r  ru  )rL   rn   r  r  r  r  rb   )rI   rJ   rK   ru  %  s    
zDefaultsKwDictNode.__init__)rN   rO   rP   ru  rQ   rJ   rJ   )rI   rK   r  %  s   r  c                   s@   e Zd ZdgZedZdd Z fddZ fddZ	  Z
S )	
LambdaNoderC  z<lambda>c             C   sT   | d | _| j_d| j_d| j_| j| d| j_| jjj| _|	| j d S )NlambdaT)
r  Zlambda_namerC  Zno_assignment_synthesisr  ry  is_cyfunctionr   r  Zadd_lambda_def)rL   rc   rJ   rJ   rK   ry  %  s    zLambdaNode.analyse_declarationsc                s   | j || _ tt| |S )N)rC  r   rF   r  r   )rL   rc   )rI   rJ   rK   r   %  s    zLambdaNode.analyse_typesc                s    | j | tt| | d S )N)rC  r  rF   r  r   )rL   r   )rI   rJ   rK   r   %  s    zLambdaNode.generate_result_code)rN   rO   rP   re  r	   r  rb   ry  r   r   rQ   rJ   rJ   )rI   rK   r  %  s
   

	r  c                   s2   e Zd ZedZdZ fddZdd Z  Z	S )GeneratorExpressionNodegenexprFc                sD   | d| _tt| | d| j_d| j_d| j_t	j
| jj_d S )Nr  F)r  Zgenexpr_namerF   r  ry  rC  r  r  r  r,   Zpyfunction_noargsr   r8  )rL   rc   )rI   rJ   rK   ry  %  s    z,GeneratorExpressionNode.analyse_declarationsc          
   C   sD   | d|  | jjj|  ||  | jf  || 	  d S )Nz%s = %s(%s); %s)
r   r   rC  r   rH  r  r   rn   r  r   )rL   r   rJ   rJ   rK   r   %  s    z,GeneratorExpressionNode.generate_result_code)
rN   rO   rP   r	   r  rb   r  ry  r   rQ   rJ   rJ   )rI   rK   r  %  s   

r  c               @   sR   e Zd ZdgZeZdZdZdZdZ	dZ
dd Zdd Zd	d
 Zdd Zdd ZdS )YieldExprNoder  r   Fyieldc             C   sZ   | j r| jr$| jr$t| jd| j  d| _| jd k	rV| j|| _| jj	j
sV| | | S )Nz'%s' not supported herer$   )	label_numis_yield_fromin_async_genr   rn   expr_keywordr   r  r   r   ra   coerce_yield_argument)rL   rc   rJ   rJ   rK   r   %  s    


zYieldExprNode.analyse_typesc             C   s   | j || _ d S )N)r  r=  )rL   rc   rJ   rJ   rK   r  &  s    z#YieldExprNode.coerce_yield_argumentc             C   sp   | j rT| j | | j | |dtj| j tf  | j | | j 	| n|
tjt | | d S )Nz%s = %s;)r  r  r   r   r   r  r   r   r	  r  Zput_init_to_py_nonegenerate_yield_code)rL   r   rJ   rJ   rK   r  &  s    z&YieldExprNode.generate_evaluation_codec             C   s   | | jdd\}}|| g }|jj  x\|j D ]N\}}}|jj|}|	|||f |j
rv|| |dtj||f  q>W |tj |jjd }	|jjd }
|	s|
r|jtj|jj d |  |jjdk	r|dtj  n|d	tj  |d
| jrdnd| jr(dndf  |dtj|f  | jrh| jsh|dtj  n|dtj  || xP|D ]H\}}}|d|tj|f  |j
r|dtj|f  || qW | |tj | jr| | |d|   tjf  |!|   t" dS )z
        Generate the code to return the argument in 'Naming.retval_cname'
        and to continue at the yield label.
         r  z%s->%s = %s;profile	linetrace)r   Nz"__Pyx_Coroutine_SwapException(%s);z+__Pyx_Coroutine_ResetAndClearException(%s);z*/* return from %sgenerator, %sing value */zasync r  awaitr  z%s->resume_label = %d;z,return __Pyx__PyAsyncGenValueWrapperNew(%s);z
return %s;z%s = %s->%s;z%s->%s = 0;z	%s = %s; )#Znew_yield_labelr  replaceZ	use_labelr   Zclosure_tempsresetZtemps_in_user   r   ra   Zput_xgiverefr   r   r  r  r  r[   Zput_trace_returnZ	gil_ownedZput_finish_refcount_contextZcurrent_exceptr  r  is_awaitr  r  !generate_sent_value_handling_codeZsent_value_cnamer   r   r*  r   r   r   )rL   r   r  Z
label_nameZsavedr   r   r   Z
save_cnamer  r  rJ   rJ   rK   r  &  sP    



z!YieldExprNode.generate_yield_codec             C   s   | ||| j d S )N)r   r   rn   )rL   r   value_cnamerJ   rJ   rK   r  M&  s    z/YieldExprNode.generate_sent_value_handling_codeN)rN   rO   rP   r   r   r   r  r  r  r  r  r   r  r  r  r  rJ   rJ   rJ   rK   r  %  s   
8r  c               @   s.   e Zd Zdd ZdddZdd Zd	d
 ZdS )_YieldDelegationExprNodec             C   s
   t  d S )N)NotImplementedError)rL   r   rJ   rJ   rK   yield_from_funcR&  s    z(_YieldDelegationExprNode.yield_from_funcNFc             C   s   |d kr| j | |dtj| |tj|d kr<| j  n|f  |d krh| j | | j 	| n|rx|
|t |tj |dtj  | | |d | jr| | n
| | |d d S )Nz%s = %s(%s, %s);zif (likely(%s)) {z} else {r   )r  r  r   r   r  r#  r  r   r  r  r  r   r  r  r   fetch_iteration_resulthandle_iteration_exception)rL   r   r?  r@  rJ   rJ   rK   r  U&  s(    


z1_YieldDelegationExprNode.generate_evaluation_codec             C   s:   | d|    || jd|    ||   d S )Nz
%s = NULL;z(__Pyx_PyGen_FetchStopIterationValue(&%s))r   r   r  rn   r  )rL   r   rJ   rJ   rK   r$  n&  s    z/_YieldDelegationExprNode.fetch_iteration_resultc             C   sB   | d | d | d | d|| j  | d d S )Nz,PyObject* exc_type = __Pyx_PyErr_Occurred();zif (exc_type) {zif (likely(exc_type == PyExc_StopIteration || (exc_type != PyExc_GeneratorExit && __Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration)))) PyErr_Clear();zelse %sr   )r   r   rn   )rL   r   rJ   rJ   rK   r%  t&  s
    


z3_YieldDelegationExprNode.handle_iteration_exception)NF)rN   rO   rP   r#  r  r$  r%  rJ   rJ   rJ   rK   r!  Q&  s   
r!  c               @   s$   e Zd ZdZdZdd Zdd ZdS )YieldFromExprNodeTz
yield fromc             C   s(   | j jjst| jd | j || _ d S )Nz-yielding from non-Python object not supported)r  r   r  r   rn   r=  )rL   rc   rJ   rJ   rK   r  &  s    
z'YieldFromExprNode.coerce_yield_argumentc             C   s   |j tdd dS )NZGeneratorYieldFromzCoroutine.cZ__Pyx_Generator_Yield_From)r  r  r   r  )rL   r   rJ   rJ   rK   r#  &  s    z!YieldFromExprNode.yield_from_funcN)rN   rO   rP   r  r  r  r#  rJ   rJ   rJ   rK   r&  }&  s   r&  c               @   s$   e Zd ZdZdZdd Zdd ZdS )AwaitExprNodeTr  c             C   s   | j d k	r| j || _ d S )N)r  r=  )rL   rc   rJ   rJ   rK   r  &  s    
z#AwaitExprNode.coerce_yield_argumentc             C   s   |j tdd dS )NZCoroutineYieldFromzCoroutine.cZ__Pyx_Coroutine_Yield_From)r  r  r   r  )rL   r   rJ   rJ   rK   r#  &  s    zAwaitExprNode.yield_from_funcN)rN   rO   rP   r  r  r  r#  rJ   rJ   rJ   rK   r'  &  s   r'  c                   s,   e Zd Zdd Z fddZdd Z  ZS )AwaitIterNextExprNodec             C   sJ   |j tdd |d |d |d |d |d d S )NStopAsyncIterationzCoroutine.cz,PyObject* exc_type = __Pyx_PyErr_Occurred();zif (unlikely(exc_type && (exc_type == __Pyx_PyExc_StopAsyncIteration || ( exc_type != PyExc_StopIteration && exc_type != PyExc_GeneratorExit && __Pyx_PyErr_GivenExceptionMatches(exc_type, __Pyx_PyExc_StopAsyncIteration))))) {zPyErr_Clear();zbreak;r   )r  r  r   r  r   )rL   r   rJ   rJ   rK   _generate_break&  s    



z%AwaitIterNextExprNode._generate_breakc                s,   |j std| | tt| | d S )Nz1AwaitIterNextExprNode outside of 'async for' loop)break_labelr   r*  rF   r(  r$  )rL   r   )rI   rJ   rK   r$  &  s    
z,AwaitIterNextExprNode.fetch_iteration_resultc             C   sF   |j std|d|  | | ||| j |d d S )Nz1AwaitIterNextExprNode outside of 'async for' loopzif (unlikely(!%s)) {r   )r+  r   r   r*  r   rn   )rL   r   r   rJ   rJ   rK   r  &  s
    
z7AwaitIterNextExprNode.generate_sent_value_handling_code)rN   rO   rP   r*  r$  r  rQ   rJ   rJ   )rI   rK   r(  &  s   
r(  c               @   s0   e Zd ZeZdZdd ZdZdd Zdd Z	d	S )
GlobalsExprNoder$   c             C   s   | tj | S )N)r  r   Zglobals_utility_code)rL   rc   rJ   rJ   rK   r   &  s    zGlobalsExprNode.analyse_typeszConstructing globals dictc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  &  s    zGlobalsExprNode.may_be_nonec             C   s6   | d|  ||  | jf  ||   d S )Nz%s = __Pyx_Globals(); %s)r   r   r   rn   r  )rL   r   rJ   rJ   rK   r   &  s    z$GlobalsExprNode.generate_result_codeN)
rN   rO   rP   r   r   r   r   r  rN  r   rJ   rJ   rJ   rK   r,  &  s   r,  c               @   s   e Zd Zdd ZdS )LocalsDictItemNodec             C   sR   | j || _ | j|| _| j || _ | jj|rH| j|| _nd | _| S )N)r
  r   r   r=  r   rq  )rL   rc   rJ   rJ   rK   r   &  s    z LocalsDictItemNode.analyse_typesN)rN   rO   rP   r   rJ   rJ   rJ   rK   r-  &  s   r-  c                   s$   e Zd Zdd Z fddZ  ZS )FuncLocalsExprNodec                s@   t dd |j D } fdd|D }tj|  |dd d S )Nc             S   s   g | ]}|j r|j qS rJ   )rb   )rS   r   rJ   rJ   rK   rt   &  s    z/FuncLocalsExprNode.__init__.<locals>.<listcomp>c          
      s,   g | ]$}t  t |d t |dddqS ))r   T)rb   r  )r
  r   )r-  r  r!  )rS   rC  )rn   rJ   rK   rt   &  s   T)r  r  )rR  rv  rM  r  ru  )rL   rn   rc   r  r  rJ   )rn   rK   ru  &  s    

zFuncLocalsExprNode.__init__c                s&   t t| |}dd |jD |_|S )Nc             S   s   g | ]}|j d k	r|qS )N)r   )rS   rV  rJ   rJ   rK   rt   &  s    z4FuncLocalsExprNode.analyse_types.<locals>.<listcomp>)rF   r.  r   r  )rL   rc   rp   )rI   rJ   rK   r   &  s    z FuncLocalsExprNode.analyse_types)rN   rO   rP   ru  r   rQ   rJ   rJ   )rI   rK   r.  &  s   
r.  c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )PyClassLocalsExprNodec             C   s   t | | || _d S )N)rg  ru  pyclass_dict)rL   rn   r0  rJ   rJ   rK   ru  &  s    zPyClassLocalsExprNode.__init__c             C   s   | j j| _d| _| S )NF)r0  r   r   )rL   rc   rJ   rJ   rK   r   &  s    
z#PyClassLocalsExprNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  &  s    z!PyClassLocalsExprNode.may_be_nonec             C   s
   | j  S )N)r0  r   )rL   rJ   rJ   rK   r   &  s    zPyClassLocalsExprNode.resultc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   &  s    z*PyClassLocalsExprNode.generate_result_codeN)rN   rO   rP   ru  r   rN  r   r   rJ   rJ   rJ   rK   r/  &  s
   r/  c             C   s*   |j rt| S |jr t| |jS t| |S )N)r  r,  r  r/  r  r.  )rn   Z
scope_noderc   rJ   rJ   rK   LocalsExprNode'  s
    r1  )not~r  r  c               @   s   e Zd ZdgZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd$d!d"Zd#S )%UnopNoder  Tc             C   s   t | j }|| jj| _d S )N)compile_time_unary_operatorsr
   r  rl   )rL   r  rJ   rJ   rK   r   ''  s    
z"UnopNode.calculate_constant_resultc          
   C   sh   t | j}|s"t| jd| j  | j|}y||S  tk
rb } z| | W d d }~X Y nX d S )Nz3Unary '%s' not supported in compile-time expression)	r5  rZ   r
   r   rn   r  r   rd  r   )rL   r   r  r  r   rJ   rJ   rK   r   +'  s    
zUnopNode.compile_time_valuec             C   s<   | j |}|js|jr0|| j}|d k	r0|S | ||S )N)r  rr   r0  r  find_cpp_operation_typer
   infer_unop_type)rL   rc   operand_typecpp_typerJ   rJ   rK   rr   7'  s    zUnopNode.infer_typec             C   s   |j r
tS |S d S )N)ra   r   )rL   rc   r8  rJ   rJ   rK   r7  ?'  s    zUnopNode.infer_unop_typec             C   s,   | j jr"| j jjr"| j jtk	r"dS t| S )NF)r  r   r  r   r   rN  )rL   rJ   rJ   rK   rN  E'  s    zUnopNode.may_be_nonec             C   sx   | j || _ | |r6tt| j| j j| _d| _n>|  rV| 	| t
| _d| _n|  rj| | n
| | | S )Nr$   )r  r   is_pythran_operationrA   r8   r
   r   r   is_py_operationcoerce_operand_to_pyobjectr   is_cpp_operationanalyse_cpp_operationanalyse_c_operation)rL   rc   rJ   rJ   rK   r   K'  s    


zUnopNode.analyse_typesc             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   Z'  s    zUnopNode.check_constc             C   s   | j jjp| j jjS )N)r  r   ra   rG  )rL   rJ   rJ   rK   r;  ]'  s    zUnopNode.is_py_operationc             C   s    t |}| jj}|o|jp|jS )N)r9   r  r   r  r5   )rL   rc   Z
np_pythranZop_typerJ   rJ   rK   r:  `'  s    zUnopNode.is_pythran_operationc             C   s   |   r|   d S )N)r;  r   )rL   rc   rJ   rJ   rK   r   e'  s    zUnopNode.nogil_checkc             C   s   | j j}|jS )N)r  r   r0  )rL   r   rJ   rJ   rK   r=  i'  s    zUnopNode.is_cpp_operationc             C   s   | j || _ d S )N)r  r=  )rL   rc   rJ   rJ   rK   r<  m'  s    z#UnopNode.coerce_operand_to_pyobjectc             C   s   | j jrL|d |d|    |d|  |  | j| j f  n| jj jrb| | nx| j	r| 
 r| jdkrt|| jd|  | j| j f | j jr|  nd | j| j n |d|  | j| j f  d S )Nz// Pythran unaryopz__Pyx_call_destructor(%s);znew (&%s) decltype(%s){%s%s};r  z%s = %s %s;)r   r5   r   r   r
   r  r   ra   generate_py_operation_coder   r=  r  r   rn   r   r   )rL   r   rJ   rJ   rK   r   p'  s"    

zUnopNode.generate_result_codec          
   C   sJ   |  |}|d|  || j ||  | jf  ||   d S )Nz%s = %s(%s); %s)py_operation_functionr   r   r  r   r   rn   r  )rL   r   r@  rJ   rJ   rK   r@  '  s    
z#UnopNode.generate_py_operation_codec             C   s0   | j jjs$t| jd| j| j jf  tj| _d S )Nz"Invalid operand type for '%s' (%s))r  r   r&  r   rn   r
   r   r*   )rL   rJ   rJ   rK   
type_error'  s    
zUnopNode.type_errorc             C   s   | | j| jg}|r&|s&|   d S |rl|jj| _|jj| _| jdkrxd| _| jd krx|t	
dd nd| _d| _| jj| j}|r|d krt| jd| jtf  |   d S || _d S )Nr  Tr  zCppSupport.cppr  z '%s' operator not defined for %s)rs  r
   r  rB  r   r  r   r   r  r   r  r6  r   rn   )rL   rc   overload_checkr   r9  rJ   rJ   rK   r>  '  s(    



zUnopNode.analyse_cpp_operationN)T)rN   rO   rP   r   infixr   r   rr   r7  rN  r   r   r;  r:  r   r=  r<  r   r@  rB  r>  rJ   rJ   rJ   rK   r4  '  s$   
r4  c               @   s>   e Zd ZdZejZdd Zdd Zdd Z	dd	 Z
d
d ZdS )NotNode!c             C   s   | j j | _d S )N)r  rl   )rL   rJ   rJ   rK   r   '  s    z!NotNode.calculate_constant_resultc          
   C   sD   | j |}y| S  tk
r> } z| | W d d }~X Y nX d S )N)r  r   rd  r   )rL   r   r  r   rJ   rJ   rK   r   '  s
    zNotNode.compile_time_valuec             C   s   t jS )N)r   rp  )rL   rc   r8  rJ   rJ   rK   r7  '  s    zNotNode.infer_unop_typec             C   s:   | j || _ | j j}|jr(| | n| j || _ | S )N)r  r   r   r0  r>  r   )rL   rc   r8  rJ   rJ   rK   r   '  s    zNotNode.analyse_typesc             C   s   d| j   S )Nz(!%s))r  r   )rL   rJ   rJ   rK   r   '  s    zNotNode.calculate_result_codeN)rN   rO   rP   r
   r   rp  r   r   r   r7  r   r   rJ   rJ   rJ   rK   rE  '  s   	rE  c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	UnaryPlusNoder  c             C   s   t | jjt j| _d S )N)r   r|  r  r   ra  )rL   rc   rJ   rJ   rK   r?  '  s    z!UnaryPlusNode.analyse_c_operationc             C   s   dS )NZPyNumber_PositiverJ   )rL   r   rJ   rJ   rK   rA  '  s    z#UnaryPlusNode.py_operation_functionc             C   s$   |   rd| j  S | j S d S )Nz(+%s))r=  r  r   )rL   rJ   rJ   rK   r   '  s    z#UnaryPlusNode.calculate_result_codeN)rN   rO   rP   r
   r?  rA  r   rJ   rJ   rJ   rK   rG  '  s   rG  c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )UnaryMinusNoder  c             C   sN   | j jjr t| j jtj| _n| j jjr4tj| _n|   | jjrJd| _	d S )NF)
r  r   r  r   r|  ra  rB  rB  r2  rD  )rL   rc   rJ   rJ   rK   r?  '  s    


z"UnaryMinusNode.analyse_c_operationc             C   s   dS )NZPyNumber_NegativerJ   )rL   r   rJ   rJ   rK   rA  '  s    z$UnaryMinusNode.py_operation_functionc             C   s4   | j rd| j  S d| jjd| j f S d S )Nz(-%s)z%s(%s)r  )rD  r  r   r   unary_op)rL   rJ   rJ   rK   r   '  s    z$UnaryMinusNode.calculate_result_codec             C   s   | j  }|rd| S d S )Nz(-%s))r  r   )rL   r   rJ   rJ   rK   r   '  s    
z)UnaryMinusNode.get_constant_c_result_codeN)rN   rO   rP   r
   r?  rA  r   r   rJ   rJ   rJ   rK   rH  '  s
   rH  c               @   s$   e Zd Zdd Zdd Zdd ZdS )	TildeNodec             C   s@   | j jjr t| j jtj| _n| j jjr4tj| _n|   d S )N)r  r   r+  r   r|  ra  rB  rB  )rL   rc   rJ   rJ   rK   r?  (  s    


zTildeNode.analyse_c_operationc             C   s   dS )NZPyNumber_InvertrJ   )rL   r   rJ   rJ   rK   rA  (  s    zTildeNode.py_operation_functionc             C   s   d| j   S )Nz(~%s))r  r   )rL   rJ   rJ   rK   r   (  s    zTildeNode.calculate_result_codeN)rN   rO   rP   r?  rA  r   rJ   rJ   rJ   rK   rJ  '  s   	rJ  c               @   s   e Zd Zdd ZdS )	CUnopNodec             C   s   dS )NFrJ   )rL   rJ   rJ   rK   r;  (  s    zCUnopNode.is_py_operationN)rN   rO   rP   r;  rJ   rJ   rJ   rK   rK  (  s   rK  c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	DereferenceNoder   c             C   s   |j r|jS tjS d S )N)r  r  r   r*   )rL   rc   r8  rJ   rJ   rK   r7  (  s    zDereferenceNode.infer_unop_typec             C   s$   | j jjr| j jj| _n|   d S )N)r  r   r  r  rB  )rL   rc   rJ   rJ   rK   r?  "(  s    
z#DereferenceNode.analyse_c_operationc             C   s   d| j   S )Nz(*%s))r  r   )rL   rJ   rJ   rK   r   ((  s    z%DereferenceNode.calculate_result_codeN)rN   rO   rP   r
   r7  r?  r   rJ   rJ   rJ   rK   rL  (  s   rL  c               @   s   e Zd Zdd Zdd ZdS )DecrementIncrementNodec             C   sB   | j jjr t| j jtj| _n| j jjr6| j j| _n|   d S )N)r  r   r  r   r|  ra  r  rB  )rL   rc   rJ   rJ   rK   r?  /(  s    

z*DecrementIncrementNode.analyse_c_operationc             C   s2   | j rd| j| j f S d| j | jf S d S )Nz(%s%s))	is_prefixr
   r  r   )rL   rJ   rJ   rK   r   8(  s    z,DecrementIncrementNode.calculate_result_codeN)rN   rO   rP   r?  r   rJ   rJ   rJ   rK   rM  ,(  s   	rM  c                s    fddS )Nc                s   t | f d|S )N)rN  r
   )rM  )rn   rw  )rN  r
   rJ   rK   <lambda>?(  s    z%inc_dec_constructor.<locals>.<lambda>rJ   )rN  r
   rJ   )rN  r
   rK   inc_dec_constructor>(  s    rP  c               @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )AmpersandNode&c             C   s
   t |S )N)r   r#  )rL   rc   r8  rJ   rJ   rK   r7  H(  s    zAmpersandNode.infer_unop_typec             C   s   | j || _ | j j}|jr*| j|dd |jsd|jsd| j  sd|jrR| 	d n| 	d|  | S |j
r| 	d| j jrd| j j n| j jrd| j j nd  | S |jr| jst|| _| S )	NF)rC  z'Cannot take address of memoryview slicez&Taking address of non-lvalue (type %s)z Cannot take address of Python %szvariable '%s'zobject attribute '%s'object)r  r   r   r0  r>  r   r  r   r   r   ra   r  rb   r\  r?  r   r#  )rL   rc   ZargtyperJ   rJ   rK   r   K(  s$    zAmpersandNode.analyse_typesc             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   `(  s    zAmpersandNode.check_constc             C   s   t | j| tj| _d| _d S )Nz<error>)r   rn   r   r*   r   r  )rL   messrJ   rJ   rK   r   c(  s    zAmpersandNode.errorc             C   s   d| j   S )Nz(&%s))r  r   )rL   rJ   rJ   rK   r   h(  s    z#AmpersandNode.calculate_result_codec             C   sV   | j jjrR| jdkrRt|| jd|  | j| j  f | jjrD|  nd | j	| j
 d S )Nr  z%s = %s %s;)r  r   r0  r  r   rn   r   r
   ra   r   r   )rL   r   rJ   rJ   rK   r   k(  s
    z"AmpersandNode.generate_result_codeN)
rN   rO   rP   r
   r7  r   r   r   r   r   rJ   rJ   rJ   rK   rQ  B(  s   rQ  )r  r  r3  c             C   s   t |tr6|dkr6t|jtt|j |j|jdS t |t	rp|j
|  krVdkrpn nt| d|fd  d t| | ||dS )Nr  )rn   r   r{  rz  z+-zAPython has no increment/decrement operator: %s%sx == %s(%sx) == x      )r
   r  )rk   r   rn   rg   r   r  r   r{  rz  r4  r
   r   unop_node_classes)rn   r
   r  rJ   rJ   rK   	unop_nodey(  s    $rX  c               @   s   e Zd ZdgZd Z ZZdd Zdd Zdd Z	d	d
 Z
dd Zdd Zdd Zdd Zdd ZdddZdd Zdd Zdd ZdS )r  r  Nc             C   s   dS )NrJ   rJ   )rL   rc   rJ   rJ   rK   r   (  s    zTypecastNode.type_dependenciesc             C   s0   | j d kr*| j|}| j||\}| _ | j S )N)r   r  ri  
declarator)rL   rc   r  r  rJ   rJ   rK   rr   (  s    
zTypecastNode.infer_typec             C   s  | j d kr*| j|}| j||\}| _ | j r<|   | j jrXt| j	d t
j| _ | j|| _| j t
jkr~| j|S | j j}| jj j}|r|s| j r| j js| j jst| j	d |rv|sv| j tkr| jj jrt| j|S | jj |rt| _| j| j || _n^| jj jrJ| jj jjsd| jj jjsdt| j	d nt| j	d| jj | j f  | j|| _n|r|s| j |r| j| j || _nJ| j jr| j jjs| j jjst| j	d nt| j	d| j | jj f  n|r>|r>| j rt!| j| j |dd| _n t"| jt#r| j| j || _nD| j j$rd| jj j$rd| j|| _n| jj j%r| j| j || _| S )NzCannot cast to a function typez>Casting temporary Python object to non-numeric non-Python typez>Python objects cannot be cast from pointers of primitive typesz8No conversion from %s to %s, python object pointer used.z<Python objects cannot be cast to pointers of primitive typesT)notnone)&r   r  ri  rY  r  rz   r   r   r   rn   r   r*   r   rp  r   ra   r   r  r0  r   r+  r,  rq  r   r   r"  r  r   rr  r   r   create_from_py_utility_codeZ	typecheckr/  rk   r  r2  r  )rL   rc   r  r  Zto_pyZfrom_pyrJ   rJ   rK   r   (  sZ    



zTypecastNode.analyse_typesc             C   s
   | j  S )N)r  rM  )rL   rJ   rJ   rK   rM  (  s    zTypecastNode.is_simplec             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   (  s    zTypecastNode.is_ephemeralc             C   s   | j p| j S )N)r   r  r   )rL   rJ   rJ   rK   r   (  s    z!TypecastNode.nonlocally_immutablec             C   s    | j r| j jr| jr|   d S )N)r   ra   r   r   )rL   rc   rJ   rJ   rK   r   (  s    zTypecastNode.nogil_checkc             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   (  s    zTypecastNode.check_constc             C   s   |  | jj| _d S )N)r   r  rl   )rL   rJ   rJ   rK   r   (  s    z&TypecastNode.calculate_constant_resultc             C   s   |d kr| j  }| jjrx| j  }| j jjrT| jjd| }| jjd| }n| jj|}d}d| jj||f S | j|S d S )Nz__Pyx_CREAL(%s)z__Pyx_CIMAG(%s)r  z
%s(%s, %s))r  r   r   r2  	real_typer;  r  )rL   operand_result	real_part	imag_partrJ   rJ   rK   r   (  s    


z"TypecastNode.calculate_result_codec             C   s   | j  }|r| j|S d S )N)r  r   r   r;  )rL   r]  rJ   rJ   rK   r   (  s    
z'TypecastNode.get_constant_c_result_codec             C   s*   | j jr| js| j|S t| |S d S )N)r   ra   r   r  r   r   )rL   r   rJ   rJ   rK   r    )  s    zTypecastNode.result_asc             C   s:   | j r6|d|  | j f  ||  |   d S )Nz%s = (PyObject *)%s;)r   r   r   r  r   r   )rL   r   rJ   rJ   rK   r   )  s    z!TypecastNode.generate_result_code)N)rN   rO   rP   r   r  rY  r   r   rr   r   rM  r   r   r   r   r   r   r   r   r   rJ   rJ   rJ   rK   r  (  s   7
r  zStart may not be givenz'Stop must be provided to indicate shapezVStrides may only be given to indicate contiguity. Consider slicing it after conversionz2Can only create cython.array from pointer or arrayz7Pointer base type does not match cython.array base typec               @   sb   e Zd ZdZddgZdZdZdZdZe	j
Zdd Zd	d
 Zdd Zdd Zdd Zedd ZdS )r$  an  
    Used when a pointer of base_type is cast to a memoryviewslice with that
    base type. i.e.

        <int[:M:1, :N]> p

    creates a fortran-contiguous cython.array.

    We leave the type set to object so coercions to object are more efficient
    and less work. Acquiring a memoryviewslice from this will be just as
    efficient. ExprNode.coerce_to() will do the additional typecheck on
    self.compile_time_type

    This also handles <int[:, :]> my_c_array


    operand             ExprNode                 the thing we're casting
    base_type_node      MemoryViewSliceTypeNode  the cast expression node
    r  shapesNTcc             C   s  ddl m} | j|| _| jr(| j}n| jj|}| jj}t| _	g | _
t|}| jj	}| jj	js| jj	jst| jjt | S g }|jrx@|jr||j |j}qW n"|jr|j}nt| jd|  | S ||s|jst| jjt | S | jj	jr&t||kr&t| jjd|t|f  | S xnt|D ]`\}}	|	jjsXt|	jjt | S |	jjr|r|| }
t| jt|
|
tjd|	_nt|	jt  | S |	j||	_|	j!| j"|}|j#s|$| | j
| |d|d fk}|	j%jsp|rp|	j%||	_%|	j%j	j&s:|	j%j#r:|	j%j	j's:t|	j%jd | S |	j%(|dkr^t|	j%jt) | S |dkrd| _*n"|	j%js2|s2t|	j%jt) | S q2W | jj+s| j$|| _d	gt| }| j*dkrd
|d< nd
|d< t,||| _-| j-.| j | /|| _	|0| |1|j2 | S )Nr$   )r  zunexpected base type %s foundz/Expected %d dimensions, array has %d dimensions)r   rl   r   r   zExpected an integer literalZfortran)r  Zfollow)r  Zcontigr  )3r  r  r  r   array_dtypebase_type_noderi  rn  r*   r   r`  rw   r  r   r   rn   ERR_NOT_POINTERr   r  r  r  r   ERR_BASE_TYPErP  r   rk  	ERR_STARTr   r   rg   r   ra  ERR_NOT_STOPr"  
shape_typer   rL  r   r+  r&  r   	ERR_STEPSmoder  rk  r   Zvalidate_memslice_dtypeget_cython_array_typeZuse_cython_array_utility_coder  Ztypeinfo_to_format_code)rL   rc   r  rb  rn  r  r  Zarray_dimension_sizesZaxis_noZaxisZdimsizeshapeZfirst_or_lastrJ   rJ   rK   r   6)  s    







zCythonArrayNode.analyse_typesc             C   s$   | j rtd|j| jd| _ d S )Nztemp allocated multiple timesT)r   r   r   r   r   )rL   r   rJ   rJ   rK   r   )  s    z$CythonArrayNode.allocate_temp_resultc             C   s
   |  |S )N)rk  )rL   rc   rJ   rJ   rK   rr   )  s    zCythonArrayNode.infer_typec             C   s"   |  jj}|  |jdjS )NZarray)r  r  cython_scopeZload_cythonscopeZ	viewscoper  r   )rL   rc   rm  rJ   rJ   rK   rk  )  s    z%CythonArrayNode.get_cython_array_typec                s  ddl m} fddjD }jj} jtd} jtd}d|  }|	 |}j
jjr dj
    d   j
j  d	  d
||f  dt| }	 d||	d|f  d|||f }
  |
j  |  |  |||jj
 f} d|     j     fdd}|| || d S )Nr$   )r  c                s   g | ]} j | qS rJ   )rh  r;  r   )rS   rl  )rL   rJ   rK   rt   )  s   z8CythonArrayNode.generate_result_code.<locals>.<listcomp>Tz
sizeof(%s)z
if (!%s) {zQPyErr_SetString(PyExc_ValueError,"Cannot create cython.array from NULL pointer");r   z%%s = __pyx_format_from_typeinfo(&%s);z __PYX_BUILD_PY_SSIZE_T z+%s = Py_BuildValue((char*) "(" %s ")", %s);z, z#!%s || !%s || !PyBytes_AsString(%s)zP%s = __pyx_array_new(%s, %s, PyBytes_AS_STRING(%s), (char *) "%s", (char *) %s);c                s     | t  j|  d S )N)r  r   r   r   )rU  )r   rJ   rK   dispose)  s    z5CythonArrayNode.generate_result_code.<locals>.dispose)r  r  r`  r   r*  r   r   r   r  Zget_type_information_cnamer  r   r  r   r   r   rn   rw   r  r   r  rj  r   )rL   r   r  r`  r*  Zshapes_tempZformat_tempitemsize	type_infoZbuildvalue_fmtrd   r:  rn  rJ   )r   rL   rK   r   )  sB    






z$CythonArrayNode.generate_result_codec       	      C   s   |j }|j}t|}g }x&|jr>|t||||d |j}qW t|ddd|d _t	j
|||d}t||||d}||}|S )z@
        Given a C array type, return a CythonArrayNode
        )r   r   r   r  T)r   ry  r  )rn  rc  )rc  r  rb  )rn   r   ri  r   r   re  r  r   r   r   ZMemoryViewSliceTypeNoder$  r   )	rH   Zsrc_noderc   rn   r  r  rn  Zmemslicenoder   rJ   rJ   rK   r%  )  s    



zCythonArrayNode.from_carray)rN   rO   rP   r`  r   r`  r   rj  rb  r   r   rh  r   r   rr   rk  r   rf  r%  rJ   rJ   rJ   rK   r$  )  s   h0r$  c               @   s"   e Zd ZejZdd Zdd ZdS )
SizeofNodec             C   s   dS )NTrJ   )rL   rJ   rJ   rK   r   )  s    zSizeofNode.check_constc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   )  s    zSizeofNode.generate_result_codeN)rN   rO   rP   r   r  r   r   r   rJ   rJ   rJ   rK   rq  )  s   rq  c               @   s,   e Zd Zg ZdZdd Zdd Zdd ZdS )r  Nc       
      C   s   dr| j jr| j j}||d }|jd krt| j|d d}x$|dd  D ]}t| j||d}qLW t| j|| j jd}t| j|d	|}|S | j
d kr| j |}| j||\}}	|	| _
|   | S )Nr   )rn   rb   r$   )rn   r>  r?  )r  )r  Zmodule_pathr  r  r!  rn   rF  rb   SizeofVarNoder   r  ri  rY  
check_type)
rL   rc   r   r>  r  r  rp   r  r  r  rJ   rJ   rK   r   	*  s     

zSizeofTypeNode.analyse_typesc             C   sX   | j }|sd S |jr(|js(t| jd n,|jr<t| jd n| sTt| jd|  d S )Nz Cannot take sizeof Python objectzCannot take sizeof voidz'Cannot take sizeof incomplete type '%s')r  ra   r  r   rn   r   is_complete)rL   r  rJ   rJ   rK   rs  *  s    zSizeofTypeNode.check_typec             C   s,   | j jr| j jddd}n
| j  }d| S )Nr  r$   )Zderefz(sizeof(%s)))r  r  r   r  )rL   r4  rJ   rJ   rK   r   (*  s    
z$SizeofTypeNode.calculate_result_code)rN   rO   rP   r   r  r   rs  r   rJ   rJ   rJ   rK   r   *  s
   r  c               @   s*   e Zd ZdgZdd Zdd Zdd ZdS )	rr  r  c             C   sP   | j |}|r>|| _| jjr.| j|j| _t| _|   n| j 	|| _ | S )N)
r  r   r  r  r  r  r  rI   rs  r   )rL   rc   Zoperand_as_typerJ   rJ   rK   r   9*  s    
zSizeofVarNode.analyse_typesc             C   s   d| j   S )Nz(sizeof(%s)))r  r   )rL   rJ   rJ   rK   r   G*  s    z#SizeofVarNode.calculate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   J*  s    z"SizeofVarNode.generate_result_codeN)rN   rO   rP   r   r   r   r   rJ   rJ   rJ   rK   rr  2*  s   rr  c               @   sT   e Zd ZejZdgZdZdZdZ	dd Z
dd Zdd	 Zd
d Zdd Zdd ZdS )
TypeidNoder  Nr$   c             C   s@   |}x|j s|j}qW |d| j}|d}tt|jS )Nzlibcpp.typeinforp  )	r  r  find_modulern   r  r   r'  Zc_const_typer   )rL   rc   Z
env_moduleZtypeinfo_moduleZtypeinfo_entryrJ   rJ   rK   get_type_info_type]*  s    

zTypeidNode.get_type_info_typec             C   s   |  |}|s| d | S || _| j|}|r@|| _d| _nf| j|| _d| _| jjjrl| d | S | jjj	r| d | S | jj
 s| d| jj  | S |tdd | S )	NzKThe 'libcpp.typeinfo' module must be cimported to use the typeid() operatorTFz$Cannot use typeid on a Python objectzCannot use typeid on voidz)Cannot use typeid on incomplete type '%s'r  zCppSupport.cpp)rw  r   r   r  r   r  r  r   ra   r   rt  r  r   r  )rL   rc   rp  Zas_typerJ   rJ   rK   r   e*  s,    





zTypeidNode.analyse_typesc             C   s   t | j| tj| _d| _d S )Nz<error>)r   rn   r   r*   r   r  )rL   rT  rJ   rJ   rK   r   ~*  s    zTypeidNode.errorc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   r   *  s    zTypeidNode.check_constc             C   s   | j S )N)r   )rL   rJ   rJ   rK   r   *  s    z TypeidNode.calculate_result_codec             C   s@   | j r| j }n
| j }t|| jd| j|f d d | j d S )Nz%s = typeid(%s);)r  r  r  r   r   rn   r   r   )rL   r   r4  rJ   rJ   rK   r   *  s    
zTypeidNode.generate_result_code)rN   rO   rP   r   r*   r   r   r  r  r   rw  r   r   r   r   r   rJ   rJ   rJ   rK   ru  N*  s   ru  c               @   sB   e Zd ZdZeZdgZdd Zdd Zdd Z	d	d
 Z
dd ZdS )
TypeofNodeNr  c             C   sH   | j || _ tt| j j}t| j|d}||}||| _	| S )N)r   )
r  r   r	   r  rg   r   r  rn   r=  r  )rL   rc   r   r  rJ   rJ   rK   r   *  s    
zTypeofNode.analyse_typesc             C   s   | j || _ | j jS )N)r  r   r   )rL   rc   rJ   rJ   rK   r   *  s    zTypeofNode.analyse_as_typec             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  *  s    zTypeofNode.may_be_nonec             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  *  s    z#TypeofNode.generate_evaluation_codec             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   *  s    z TypeofNode.calculate_result_code)rN   rO   rP   r  r   r   r   r   r   rN  r  r   rJ   rJ   rJ   rK   rx  *  s   rx  c             C   s0   y
| j }W n tk
r$   |j}Y nX || |S )N)
__matmul__ry   __rmatmul__)rZ  br  rJ   rJ   rK   matmul_operator*  s
    
r|  c             C   s   | |kS )NrJ   )xseqrJ   rJ   rK   rO  *  s    rO  c             C   s   | |kS )NrJ   )r}  r~  rJ   rJ   rK   rO  *  s    )<z<=z==z!=z>=>r  is_notr  rR  /z//z<<%r   r  z**z>>r  ^@innot_inc             C   s&   t | j}|s"t| jd| j  |S )Nz4Binary '%s' not supported in compile-time expression)compile_time_binary_operatorsrZ   r
   r   rn   )rp   r  rJ   rJ   rK   get_compile_time_binop*  s    
r  c                   s   e Zd ZddgZdZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Z fd$d%Zd&d' Zd(d) Z  ZS )*	BinopNoder  r  Fc             C   s"   t | j }|| jj| jj| _d S )N)r  r
   r  rl   r  )rL   r  rJ   rJ   rK   r   *  s    
z#BinopNode.calculate_constant_resultc          
   C   s\   t | }| j|}| j|}y
|||S  tk
rV } z| | W d d }~X Y nX d S )N)r  r  r   r  rd  r   )rL   r   r  r  r  r   rJ   rJ   rK   r   *  s    
zBinopNode.compile_time_valuec             C   s   |  | j|| j||S )N)r  r  rr   r  )rL   rc   rJ   rJ   rK   rr   +  s    zBinopNode.infer_typec             C   s*   | j || _ | j|| _| | | S )N)r  r   r  r  )rL   rc   rJ   rJ   rK   r   +  s    
zBinopNode.analyse_typesc             C   s   |  |r6| | jj| jj|| _| jjs.td| _n\|  rt| 	| | | jj| jj|| _| jj
sltd| _n|  r| | n
| | d S )Nr$   )r:  r  r  r   r  r5   r   r   r;  coerce_operands_to_pyobjectsra   r=  r>  r?  )rL   rc   rJ   rJ   rK   r  +  s    



zBinopNode.analyse_operationc             C   s   |  | jj| jjS )N)is_py_operation_typesr  r   r  )rL   rJ   rJ   rK   r;  +  s    zBinopNode.is_py_operationc             C   s   |j p|j p|jp|jS )N)ra   rG  )rL   type1type2rJ   rJ   rK   r  "+  s    zBinopNode.is_py_operation_typesc             C   s   |  | jj| jj|S )N)is_pythran_operation_typesr  r   r  )rL   rc   rJ   rJ   rK   r:  %+  s    zBinopNode.is_pythran_operationc             C   s(   t |o&t|o&t|o&t|p&t|S )N)r9   r4   r5   )rL   r  r  rc   rJ   rJ   rK   r  (+  s    z$BinopNode.is_pythran_operation_typesc             C   s   | j jjp| jjjS )N)r  r   r0  r  )rL   rJ   rJ   rK   r=  .+  s    
zBinopNode.is_cpp_operationc             C   s   | | j| j| jg}|s&|   d S |j}|j| _|j| _| jdkrhd| _| jd krh|	t
dd |jrt|j}t|jdkr| j|jd j|| _n0| j|jd j|| _| j|jd j|| _|j| _d S )Nr  r$   r  zCppSupport.cppr   )rs  r
   r  r  rB  r   r  r   r   r  r   r  r  r  rw   r{   r"  r  )rL   rc   r   r  rJ   rJ   rK   r>  2+  s$    

zBinopNode.analyse_cpp_operationc             C   s   |  |||r tt| j||S | ||r|jr:tj}n|jrFtj	}|jrTtj}n|jr`tj	}|j
sl|j
r||kr| jdkr|S | ||}|d k	r|S tS |js|jrtjS | ||S d S )Nz**%+|&^)r  rA   r7   r
   r  r  r   r   r  r   r  infer_builtin_types_operationr   r&  r   r*   compute_c_result_type)rL   r  r  rc   r  rJ   rJ   rK   r  I+  s*    zBinopNode.result_typec             C   s   d S )NrJ   )rL   r  r  rJ   rJ   rK   r  b+  s    z'BinopNode.infer_builtin_types_operationc             C   s   |   r|   d S )N)r;  r   )rL   rc   rJ   rJ   rK   r   e+  s    zBinopNode.nogil_checkc             C   s    | j || _ | j|| _d S )N)r  r=  r  )rL   rc   rJ   rJ   rK   r  i+  s    z&BinopNode.coerce_operands_to_pyobjectsc             C   s   | j  o| j S )N)r  r   r  )rL   rJ   rJ   rK   r   m+  s    zBinopNode.check_constc                s"   t t|  p | j p | j S )N)rF   r  r   r  r  )rL   )rI   rJ   rK   r   p+  s    zBinopNode.is_ephemeralc             C   sr  | j jr|d |d|    | jdkrZ|d|  |  | j | j f  n.|d|  |  | j | j| j f  n| jj jr| 	|}| jdkrd}nd}|d|  || j
 | j
 |||  | jf  || 
  nt| jrn|  rT| jd	krTt|| jd
|  |  f | j jrD|  nd | j| j n|d
|  |  f  d S )Nz// Pythran binopz__Pyx_call_destructor(%s);z**zBnew (&%s) decltype(%s){pythonic::numpy::functor::power{}(%s, %s)};z!new (&%s) decltype(%s){%s %s %s};z	, Py_Noner  z%s = %s(%s, %s%s); %sr  z%s = %s;)r   r5   r   r   r
   r  r   r  ra   rA  r   r   rn   r  r   r=  r  r   r   r   r   )rL   r   r@  Z
extra_argsrJ   rJ   rK   r   t+  sH    




zBinopNode.generate_result_codec             C   s@   | j jjs4| jjjs4t| jd| j| j j| jjf  tj| _d S )Nz'Invalid operand types for '%s' (%s; %s))	r  r   r&  r  r   rn   r
   r   r*   )rL   rJ   rJ   rK   rB  +  s    


zBinopNode.type_error)rN   rO   rP   r   r  r   r   rr   r   r  r;  r  r:  r  r=  r>  r  r  r   r  r   r   r   rB  rQ   rJ   rJ   )rI   rK   r  *  s*   	+r  c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )

CBinopNodec             C   s    t | |}| rtj|_|S )N)r  r   r;  r   r*   r   )rL   rc   rp   rJ   rJ   rK   r   +  s    zCBinopNode.analyse_typesc             C   s   dS )Nr  rJ   )rL   r   rJ   rJ   rK   rA  +  s    z CBinopNode.py_operation_functionc             C   s   d| j  | j| j f S )Nz
(%s %s %s))r  r   r
   r  )rL   rJ   rJ   rK   r   +  s    z CBinopNode.calculate_result_codec             C   sD   d }|j s|jr|| j|}|d kr@|j s2|jr@|| j|}|S )N)r0  r  r6  r
   )rL   r  r  r9  rJ   rJ   rK   r  +  s    z CBinopNode.compute_c_result_typeN)rN   rO   rP   r   rA  r   r  rJ   rJ   rJ   rK   r  +  s   r  c                s    fdd}|S )Nc                s   t | fd i|S )Nr
   )r  )rn   ru  )r
   rJ   rK   make_binop_node+  s    z,c_binop_constructor.<locals>.make_binop_noderJ   )r
   r  rJ   )r
   rK   c_binop_constructor+  s    r  c                   s   e Zd ZdZdZdZdd Zdd Z fdd	Zd
d Z	dd Z
 fddZdd Zdd Zdd Zddddddddddd d!d"d#Zd$d%d&d'd(Z  ZS ))NumBinopNodeTFNc             C   s   | j j}| jj}| ||| _| js0|   d S | jjr>d| _| jjr|jd r| j	| j
kr| j	dkr| j  r| j s| j| j  | _ | _d| _|jd | _| jj| j
| j	 || j d| _d| _| jr|jr|jr| j | j|| _ | j| j|| _d S )NFr  )r  r   Tzoverflowcheck.fold)Z	const_rhs)r  r   r  r  rB  r2  rD  r+  r[   r
   overflow_op_namesrz   overflow_checkZoverflow_foldZoverflow_check_binopr  r   r  r"  )rL   rc   r  r  rJ   rJ   rK   r?  +  s2    




z NumBinopNode.analyse_c_operationc             C   sN   |  ||rFt||}|tjkr4| jdkrBtj}nt|tj}|S d S d S )Nz|^&)c_types_okayr   r|  rp  r
   ra  )rL   r  r  widest_typerJ   rJ   rK   r  +  s    


z"NumBinopNode.compute_c_result_typec                sH   | j r| j jrdS | jj }| jj }|r:|jr:|r:|jr:dS tt|  S )NF)r   r  r  r  rF   r  rN  )rL   r  r  )rI   rJ   rK   rN  +  s    zNumBinopNode.may_be_nonec             C   s4   | j  }| j }|r,|r,d|| j|f S d S d S )Nz
(%s %s %s))r  r   r  r
   )rL   Zvalue1Zvalue2rJ   rJ   rK   r   ,  s
    

z'NumBinopNode.get_constant_c_result_codec             C   s   |j s|jo|j p|jS )N)r  rB  )rL   r  r  rJ   rJ   rK   r  ,  s    zNumBinopNode.c_types_okayc                s   | j r0| | _|jjtjdd| _|d| j  tt	| 
| | j r|d| j  |d ||| j |d |j| j d S )NF)r   z%s = 0;zif (unlikely(%s)) {z8PyErr_SetString(PyExc_OverflowError, "value too large");r   )r  overflow_bit_noder   r   r   ra  overflow_bitr   rF   r  r  r   rn   r   )rL   r   )rI   rJ   rK   r  ,  s    

z%NumBinopNode.generate_evaluation_codec             C   s   | j d k	r,d| j| j | j | j jf S | jjs:| jrt	| jr\| j
 | j
  }}n| j | j  }}d|| j|f S | j| j}|d krt| jd| j| jf  d|| j | j f S d S )Nz%s(%s, %s, &%s)z
(%s %s %s)z'binary operator %s not supported for %sz
%s(%s, %s))r  r  r  r   r  r  r   r0  rD  r5   r   r
   	binary_opr   rn   )rL   result1result2r  rJ   rJ   rK   r   ,  s$    

z"NumBinopNode.calculate_result_codec             C   s   |j p|j pt| ||S )N)r  r  r  )rL   r  r  rJ   rJ   rK   r  5,  s    z"NumBinopNode.is_py_operation_typesc             C   s"   | j | j }| jr|dd}|S )NZ	PyNumber_ZPyNumber_InPlace)py_functionsr
   r  r  )rL   r   rB  rJ   rJ   rK   rA  :,  s    z"NumBinopNode.py_operation_functionZPyNumber_OrZPyNumber_XorZPyNumber_AndZPyNumber_LshiftZPyNumber_RshiftZPyNumber_AddZPyNumber_SubtractZPyNumber_MultiplyZ__Pyx_PyNumber_MatrixMultiplyZ__Pyx_PyNumber_DivideZPyNumber_FloorDivideZPyNumber_RemainderZPyNumber_Power)r  r  rR  z<<z>>r  r  r   r  r  z//r  z**rQ  r  mullshift)r  r  r   z<<)rN   rO   rP   rD  r  r  r?  r  rN  r   r  r  r   r  rA  r  r  rQ   rJ   rJ   )rI   rK   r  +  s:   r  c               @   s   e Zd Zdd ZdS )IntBinopNodec             C   s   |j s|jo|j p|jS )N)r+  rB  )rL   r  r  rJ   rJ   rK   r  [,  s    zIntBinopNode.c_types_okayN)rN   rO   rP   r  rJ   rJ   rJ   rK   r  X,  s   r  c                   s4   e Zd Zdd Zdd Zdd Z fddZ  ZS )	AddNodec             C   s.   |j r|j s|jr|jrdS t| ||S d S )Nr$   )r  r  r  r  )rL   r  r  rJ   rJ   rK   r  d,  s    zAddNode.is_py_operation_typesc             C   s<   t ttttf}||kr8||kr8|t|||| S d S )N)r   r    r   r-   r   rS  r   )rL   r  r  string_typesrJ   rJ   rK   r  j,  s
    z%AddNode.infer_builtin_types_operationc             C   sJ   |j s|jr|js|jr|S |j s(|jr8|js4|jr8|S t| ||S d S )N)r  r   r+  rB  r  r  )rL   r  r  rJ   rJ   rK   r  s,  s    zAddNode.compute_c_result_typec                s   d}t | jtst | jtr"d}n.| jj| jj }}|tksD|tkrP|joN|j}|rp| j sh| j rldS dS tt	| 
|S )NFTZ__Pyx_PyUnicode_ConcatSafeZ__Pyx_PyUnicode_Concat)rk   r  rU  r  r   r   r  rN  rF   r  rA  )rL   r   Zis_unicode_concatr  r  )rI   rJ   rK   rA  },  s    zAddNode.py_operation_function)rN   rO   rP   r  r  r  rA  rQ   rJ   rJ   )rI   rK   r  a,  s   	
r  c               @   s   e Zd Zdd ZdS )SubNodec             C   sL   |j s|jr|js|jr|S |j s(|jr:|j s4|jr:tjS t| ||S d S )N)r  r   r+  rB  r   Zc_ptrdiff_t_typer  r  )rL   r  r  rJ   rJ   rK   r  ,  s    zSubNode.compute_c_result_typeN)rN   rO   rP   r  rJ   rJ   rJ   rK   r  ,  s   r  c               @   s   e Zd Zdd Zdd ZdS )MulNodec             C   s.   |j r|js|j r|jrdS t| ||S d S )Nr$   )r  r+  r  r  )rL   r  r  rJ   rJ   rK   r  ,  s    zMulNode.is_py_operation_typesc             C   sJ   t ttttf}||kr |jr |S ||kr2|jr2|S |jr<|S |jrF|S d S )N)r   r    r   r-   r   r  r+  )rL   r  r  r  rJ   rJ   rK   r  ,  s    z%MulNode.infer_builtin_types_operationN)rN   rO   rP   r  r  rJ   rJ   rJ   rK   r  ,  s   r  c                   s$   e Zd Zdd Z fddZ  ZS )MatMultNodec             C   s   dS )NTrJ   )rL   r  r  rJ   rJ   rK   r  ,  s    z!MatMultNode.is_py_operation_typesc                s(   |j tdd tt| | d S )NZMatrixMultiplyzObjectHandling.c)r  r  r   r  rF   r  r  )rL   r   )rI   rJ   rK   r  ,  s    z$MatMultNode.generate_evaluation_code)rN   rO   rP   r  r  rQ   rJ   rJ   )rI   rK   r  ,  s   r  c               @   sx   e Zd ZdZdZdZdZdZdd Zdd Z	dd Z
d	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )DivNodeNFc             C   s>   t | j }| jdkr:| jd kr:t|tr:t|tr:t d }|S )Nr  z//)r  r
   truedivisionrk   r"   )rL   op1op2r  rJ   rJ   rK   !find_compile_time_binary_operator,  s
    
z)DivNode.find_compile_time_binary_operatorc             C   s4   | j j}| jj}| ||}|| j j| jj| _d S )N)r  rl   r  r  )rL   r  r  r  rJ   rJ   rK   r   ,  s    z!DivNode.calculate_constant_resultc          
   C   s`   | j |}| j|}y| ||}|||S  tk
rZ } z| | W d d }~X Y nX d S )N)r  r   r  r  rd  r   )rL   r   r  r  r  r   rJ   rJ   rK   r   ,  s    
zDivNode.compile_time_valuec             C   s$   | j s|jd rd| _n| j| _d S )N	cdivisionF)r  r[   ctruedivisionr  )rL   rc   rJ   rJ   rK   _check_truedivision,  s    zDivNode._check_truedivisionc             C   s(   |  | | | j|| j||S )N)r  r  r  rr   r  )rL   rc   rJ   rJ   rK   rr   ,  s    

zDivNode.infer_typec             C   s   |  | t| | |  r$d| _| jjs| jd koX|jd  oX| j	  pX| jj
dk| _| jsl|jd r| j|| _| j|| _d S )NTr  r   cdivision_warnings)r  r  r  r=  r  r   ra   r[   r  rz   rl   zerodivision_checkr  r   )rL   rc   rJ   rJ   rK   r  ,  s    
zDivNode.analyse_operationc             C   sH   | j dkr:| jr:|js:|js:t|tj}t||}|S t| ||S )Nr  )r
   r  rC  r   r|  r  r  r  )rL   r  r  r  rJ   rJ   rK   r  ,  s    zDivNode.compute_c_result_typec             C   s   | j jrdS dS d S )Nz"integer division or modulo by zerozfloat division)r   r+  )rL   rJ   rJ   rK   zero_division_message-  s    zDivNode.zero_division_messagec             C   s   | j jsl| j jsl| jd krJ|jjd pF| j jpF| j js>| j joF| j j	 | _| jsl|j
tdd| j  t| | | | d S )Nr  ZDivIntzCMath.c)r   ra   r2  r  r  r[   rC  r  rB  r  r  r   r  r  r  r  generate_div_warning_code)rL   r   rJ   rJ   rK   r  -  s    
z DivNode.generate_evaluation_codec             C   sb  | j }| jjs^| jrp| js:d| jd| j f }nd| j  }|d|  |rb|	  |d| 
   |r|  ||| j |d | jjrp| jjrp| jdkrp|jtdd	 | jjjd
krd| j  }n | jj }d|| j |f }|d| j || j f  |r<|	  |d |rT|  ||| j |d |jjd r^| jdkr^|jtdd |d| j | j f  dtjtjd }|r d}|d|  |	  ||j| jdd |d||f  |  n|}||j| jdd |d|  ||j |d d S )Nz%s(%s)Zzeroz%s == 0zif (unlikely(%s)) {z/PyErr_SetString(PyExc_ZeroDivisionError, "%s");r   r  ZUnaryNegOverflowsz
Overflow.cr.   zunlikely(%s == -1)z+(!(((%s)-1) > 0)) && unlikely(%s == (%s)-1)zWelse if (sizeof(%s) == sizeof(long) && %s  && unlikely(UNARY_NEG_WOULD_OVERFLOW(%s))) {zLPyErr_SetString(PyExc_OverflowError, "value too large to perform division");r  r  ZCDivisionWarningzCMath.cz$if (unlikely((%s < 0) ^ (%s < 0))) {z1__Pyx_cdivision_warning(%(FILENAME)s, %(LINENO)s))ZFILENAMEZLINENOr   zint %s;T)r  z%s = %s;zif (unlikely(%s)) )r   r   ra   r  rD  rI  r  r   r   r   r  r   r   rn   r+  r  r
   r  r  r   r  r  r  r[   r   Zfilename_cnameZlineno_cnameZset_error_infor*  r  r  )rL   r   Zin_nogilZ	zero_testZminus1_checkZtype_of_op2Zwarning_coder  rJ   rJ   rK   r  -  sj    

 


z!DivNode.generate_div_warning_codec             C   s   | j jrt| S | j jr<| jdkr<d| j | j f S | j	sH| j
r| j }| j }| j	r| j | jj kr|| j |}| j | jj kr| j |}d||f S d| j  | j | j f S d S )Nz//zfloor(%s / %s)z	(%s / %s)z__Pyx_div_%s(%s, %s))r   r2  r  r   rC  r
   r  r   r  r  r  r;  specialization_name)rL   r  r  rJ   rJ   rK   r   Z-  s&    


zDivNode.calculate_result_code)rN   rO   rP   r  r  r  r  r  r  r   r   r  rr   r  r  r  r  r  r   rJ   rJ   rJ   rK   r  ,  s    
?r  s'   %(?:%|(?:\([^)]+\))?[-+#,0-9 ]*([a-z]))   d   i   o   u   x   X   e   E   f   F   g   G   c   b   ac                   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Z fddZ	  Z
S )ModNodec             C   s   |j p|j pt| ||S )N)r  r  r  )rL   r  r  rJ   rJ   rK   r  -  s    zModNode.is_py_operation_typesc             C   s   |t kr|js| j s|S nn|tttfkr|t kr8|S |jrB|S | jjrv|tksZ|tkrrt	t
| jjtkrr|S tS |tkr|jsd S tS d S )N)r   r  r  rN  r   r   r-   r  r[  r   _find_formatting_typesr   _safe_bytes_formats)rL   r  r  rJ   rJ   rK   r  -  s"    z%ModNode.infer_builtin_types_operationc             C   s   | j jrdS dS d S )Nz"integer division or modulo by zerozfloat divmod())r   r+  )rL   rJ   rJ   rK   r  -  s    zModNode.zero_division_messagec             C   s`   t | | | jjs\| jd kr4|jd p0| jj | _| js\| jjs\| jjs\t	| j
d| j  d S )Nr  z(mod operator not supported for type '%s')r  r  r   ra   r  r[   r  r+  rC  r   rn   )rL   rc   rJ   rJ   rK   r  -  s    
zModNode.analyse_operationc             C   sr   | j jsX| jsX| j jr4|jtdd| j  n$|jtddj| j | j j	d t
| | | | d S )NZModIntzCMath.cZModFloat)math_h_modifier)r   ra   r  r+  r  r  r   r  r  r  r  r  r  )rL   r   rJ   rJ   rK   r  -  s    z ModNode.generate_evaluation_codec             C   sj   | j rF| jjr,d| jj| j | j f S d| j | j f S n d| j | j | j f S d S )Nzfmod%s(%s, %s)z
(%s %% %s)z__Pyx_mod_%s(%s, %s))r  r   rC  r  r  r   r  r  )rL   rJ   rJ   rK   r   -  s    zModNode.calculate_result_codec                s   | j j| jj }}|tkrR| j  sH|jr4||sH|tkrLt| jt	sLdS dS n>|t
kr| j  s|jrt||s|tkrt| jt	sdS dS tt| |S )NZ__Pyx_PyUnicode_FormatSafeZPyUnicode_FormatZ__Pyx_PyString_FormatSafeZ__Pyx_PyString_Format)r  r   r  r   rN  r  r.  r   rk   r-  r   rF   r  rA  )rL   r   r  r  )rI   rJ   rK   rA  -  s    

zModNode.py_operation_function)rN   rO   rP   r  r  r  r  r  r   rA  rQ   rJ   rJ   )rI   rK   r  -  s   r  c                   s,   e Zd Zdd Zdd Z fddZ  ZS )PowNodec             C   s   t | | | jjrf| jjjrR| j| j|| _| j| j|| _| j	d| _
qt| jd d| _
n| jjr~d| jj | _
nx| jjrd| j dd | _
|tdd	j| j
| j | jjrd
pdd n$| jjst| jd| jj| jjf  d S )Nz**z complex int powers not supportedz<error>powz__Pyx_pow_%sr  r  ZIntPowzCMath.cr$   r   )r  r   r  z1got unexpected types for C power operator: %s, %s)r  r?  r   r2  r\  rC  r  r"  r  r  pow_funcr   rn   r  r+  r  r  r  r   r  r  r  r&  )rL   rc   rJ   rJ   rK   r?  -  s(    
zPowNode.analyse_c_operationc                s(    fdd}d j | j| jf S )Nc                s(    j | j kr|  S  j |  S d S )N)r   r   r;  )r  )rL   rJ   rK   r)   -  s    z/PowNode.calculate_result_code.<locals>.typecastz
%s(%s, %s))r  r  r  )rL   r)   rJ   )rL   rK   r   -  s
    zPowNode.calculate_result_codec                s`   | j jrP| jjdkrPt| jjtrP| jj tkrP|j	t
dd | jrLdS dS tt| |S )Nr.   ZPyNumberPow2z
Optimize.cZ__Pyx_PyNumber_InPlacePowerOf2Z__Pyx_PyNumber_PowerOf2)r   ra   r  rl   rk   r"   r  r   r  r  r   r  r  rF   r  rA  )rL   r   )rI   rJ   rK   rA  .  s    zPowNode.py_operation_function)rN   rO   rP   r?  r   rA  rQ   rJ   rJ   )rI   rK   r  -  s   r  c               @   s   e Zd ZdZddgZdZdZdZdZdd Z	dd	 Z
d
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdZdd  Zd!d" Zd#d$ Zd%d& ZdS )'BoolBinopNodeag  
    Short-circuiting boolean operation.

    Note that this node provides the same code generation method as
    BoolBinopResultNode to simplify expression nesting.

    operator  string                              "and"/"or"
    operand1  BoolBinopNode/BoolBinopResultNode   left operand
    operand2  BoolBinopNode/BoolBinopResultNode   right operand
    r  r  TNc             C   s$   | j |}| j|}t||S )N)r  rr   r  r   independent_spanning_type)rL   rc   r  r  rJ   rJ   rK   rr   !.  s    zBoolBinopNode.infer_typec             C   s,   | j dkr| j S | j p&| j S d S )Nor)r
   r  rN  r  )rL   rJ   rJ   rK   rN  &.  s    

zBoolBinopNode.may_be_nonec             C   s4   | j j}| jj}| jdkr&|o || _n
|p,|| _d S )Nand)r  rl   r  r
   )rL   r  r  rJ   rJ   rK   r   ,.  s
    
z'BoolBinopNode.calculate_constant_resultc             C   s6   | j |}| j|}| jdkr*|o(|S |p0|S d S )Nr  )r  r   r  r
   )rL   r   r  r  rJ   rJ   rK   r   4.  s
    
z BoolBinopNode.compile_time_valuec             C   s   | j  p| j S )N)r  r   r  )rL   rJ   rJ   rK   r   <.  s    zBoolBinopNode.is_ephemeralc             C   sJ   | j |}| j|}t|j|j| _| ||| _ | ||| _| S )N)r  r   r  r   r  r   _wrap_operand)rL   rc   r  r  rJ   rJ   rK   r   ?.  s    zBoolBinopNode.analyse_typesc             C   s    t |ttfst|| j|}|S )N)rk   r  BoolBinopResultNoder   )rL   r  rc   rJ   rJ   rK   r  K.  s    zBoolBinopNode._wrap_operandc             C   s$   |  | j|| _|  | j|| _dS )z
        Must get called by transforms that want to create a correct BoolBinopNode
        after the type analysis phase.
        N)r  r  r  )rL   rc   rJ   rJ   rK   wrap_operandsP.  s    zBoolBinopNode.wrap_operandsc             C   s   |  tj|S )N)r"  r   rp  )rL   rc   rJ   rJ   rK   r   X.  s    zBoolBinopNode.coerce_to_booleanc             C   s2   | j ||}| j||}tj| || j||dS )N)r   r
   r  r  )r  r"  r  r  rV  r
   )rL   r6  rc   r  r  rJ   rJ   rK   r"  [.  s    zBoolBinopNode.coerce_toc       
   	   C   s   | | j ||f}| jdkr.|d }	}n|d }	}| j|||||||	 |\}}||	 | j||||||| d S )Nr  Znext_andZnext_or)r   rn   r
   r  r  generate_bool_evaluation_coder  r  )
rL   r   final_result_tempfinal_result_type	and_labelor_label	end_labelfall_throughZouter_labelsZmy_labelrJ   rJ   rK   r  c.  s    

z+BoolBinopNode.generate_bool_evaluation_codec          	   C   sX   |  | | jjrtjn| j}d  }}|d}| ||  ||||| || d S )NZbool_binop_done)	r   r   ra   r   r   r  r  r   r  )rL   r   r  r  r  r  rJ   rJ   rK   r  t.  s    

z&BoolBinopNode.generate_evaluation_codezTruth-testing Python objectc             C   s   | j  o| j S )N)r  r   r  )rL   rJ   rJ   rK   r   ~.  s    zBoolBinopNode.check_constc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  .  s    z,BoolBinopNode.generate_subexpr_disposal_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  .  s    z BoolBinopNode.free_subexpr_tempsc          	   C   sV   | j jr@|jjtjdd}|d|| j |	|| j
f  n
| j }|| j jfS )NF)r   z"%s = __Pyx_PyObject_IsTrue(%s); %s)r   ra   r   r   r   rp  r   r  r   r+  rn   r   )rL   r   test_resultrJ   rJ   rK   generate_operand1_test.  s    
z$BoolBinopNode.generate_operand1_test)rN   rO   rP   r`  r   r   r
   r  r  rr   rN  r   r   r   r   r  r  r   r"  r  r  r  r   r  r  r  rJ   rJ   rJ   rK   r  .  s.   
r  c                   s\   e Zd ZdZddgZdZdZdZ fddZdd	 Z	d
d Z
dd Zdd Zdd Z  ZS )r  a  
    Intermediate result of a short-circuiting and/or expression.
    Tests the result for 'truthiness' and takes care of coercing the final result
    of the overall expression to the target type.

    Note that this node provides the same code generation method as
    BoolBinopNode to simplify expression nesting.

    arg     ExprNode    the argument to test
    value   ExprNode    the coerced result value node
    r  r   TNc                s<   | |}t|}tt| j|j||t|||d d S )N)r  r   r   )r   	ProxyNoderF   r  ru  rn   r  r"  )rL   r  r  rc   )rI   rJ   rK   ru  .  s
    

zBoolBinopResultNode.__init__c             C   s   |  tj|S )N)r"  r   rp  )rL   rc   rJ   rJ   rK   r   .  s    z%BoolBinopResultNode.coerce_to_booleanc             C   s(   | j j }|tjkr||}t|||S )N)r  r   rp  r   r  )rL   r6  rc   r  rJ   rJ   rK   r"  .  s    

zBoolBinopResultNode.coerce_toc             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   r   .  s    zBoolBinopResultNode.nogil_checkc          	   C   sZ   | j jjrB|jjtjdd}|d|| j  |	|| j
f  n
| j  }|| j jjfS )NF)r   z"%s = __Pyx_PyObject_IsTrue(%s); %s)r  r   ra   r   r   r   rp  r   r   r+  rn   r   )rL   r   r  rJ   rJ   rK   generate_operand_test.  s    

z)BoolBinopResultNode.generate_operand_testc             C   s  | | j | j| |s |r| |\}}	|	rF|rF|rF| j| |rNdnd}
|d|
|f  |	rt|j| |	r|r|s| j| |r||kr|	| |r|r|d |	s| j| ||kr|	| |r|s\|s|r|d | j
| | j
| |d|| j
|f  | j
| | j| | j
| ||kr\|	| |sh|rr|d | j| d S )NrF  r  zif (%s%s) {z} else {z%s = %s;r   )r   rn   r  r  r  r  r   r   r   r  r   r   r   r	  r  )rL   r   r  r  r  r  r  r  r  Z	uses_tempZsenserJ   rJ   rK   r  .  sF    







z1BoolBinopResultNode.generate_bool_evaluation_code)rN   rO   rP   r`  r   r   r  r   ru  r   r"  r   r  r  rQ   rJ   rJ   )rI   rK   r  .  s   	r  c               @   s   e Zd ZdZdZdZdddgZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! ZdS )"r  NTr  r  r  c             C   s   | j || j| S )N)r  r   r  )rL   rc   rJ   rJ   rK   r   /  s    zCondExprNode.type_dependenciesc             C   s   t | j|| j|S )N)r   r  r  rr   r  )rL   rc   rJ   rJ   rK   rr   /  s    
zCondExprNode.infer_typec             C   s"   | j jr| jj| _n
| jj| _d S )N)r  rl   r  r  )rL   rJ   rJ   rK   r   /  s    z&CondExprNode.calculate_constant_resultc             C   s   | j  p| j S )N)r  r   r  )rL   rJ   rJ   rK   r   !/  s    zCondExprNode.is_ephemeralc             C   s:   | j ||| _ | j|| _| j|| _| |S )N)r  r   r   r  r  r  )rL   rc   rJ   rJ   rK   r   $/  s    zCondExprNode.analyse_typesc             C   s   t | jj| jj| _| jjr.t | jj| _| jjr>t	| _
n | j sR| j r^t| jd | jjjsr| jjjr| j| j|| _| j| j|| _| jjr|   | S )NzPUnsafe C derivative of temporary Python reference used in conditional expression)r   r  r  r   r  r  r'  r  ra   r   r   r   r   rn   r"  r&  rB  )rL   rc   rJ   rJ   rK   r  */  s    z CondExprNode.analyse_result_typec             C   s,   | j || _ | j|| _d | _| |S )N)r  rJ  r  r   r  )rL   rc   rJ   rJ   rK   rJ  :/  s    zCondExprNode.coerce_to_integerc             C   s0   | j ||| _ | j||| _d | _| |S )N)r  r"  r  r   r  )rL   r6  rc   rJ   rJ   rK   r"  @/  s    zCondExprNode.coerce_toc             C   s<   | j jjs0| jjjs0t| jd| j j| jjf  tj| _d S )Nz5Incompatible types in conditional expression (%s; %s))r  r   r&  r  r   rn   r   r*   )rL   rJ   rJ   rK   rB  F/  s    zCondExprNode.type_errorc             C   s   | j  o| j o| j S )N)r  r   r  r  )rL   rJ   rJ   rK   r   L/  s    

zCondExprNode.check_constc             C   s   | | j | | | j| |d| j   | || j |d | || j	 |d | j
| | j| d S )Nz	if (%s) {z} else {r   )r   rn   r   r  r  r   r   eval_and_getr  r  r  r  )rL   r   rJ   rJ   rK   r  Q/  s    


z%CondExprNode.generate_evaluation_codec             C   s`   | | | jjr|| n
|| |d|  ||  f  |	| |
| d S )Nz%s = %s;)r  r   r   r   r   r   r   r   r   r	  r  )rL   r   r  rJ   rJ   rK   r  `/  s    

 
zCondExprNode.eval_and_getc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  j/  s    z+CondExprNode.generate_subexpr_disposal_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  m/  s    zCondExprNode.free_subexpr_temps)rN   rO   rP   r  r  r   r   r   rr   r   r   r   r  rJ  r"  rB  r   r  r  r  r  rJ   rJ   rJ   rK   r  /  s$   

r  ZPy_LTZPy_LEZPy_EQZPy_NEZPy_GTZPy_GE)	r  z<=z==z!=z<>r  z>=r  r  c               @   s   e Zd ZdZdZdd Zdd Zdd Zdd	 Zd
d Z	d!ddZ
dd Zdd Zdd Zdd Zdd Zdd Zd"ddZdd Zdd  ZdS )#CmpNodeNc             C   s   t S )N)r   )rL   rc   rJ   rJ   rK   rr   /  s    zCmpNode.infer_typec             C   s   t | j }| jj}t|tr:t|tr:t|t|kr:d S | jdkrt| jttt	fr| jj
sn| jdk| _d S t| jtr| js| j | _n$t| jtr| jjs| jdk| _d S |||| _d S )N)r  r  r  )r  r
   r  rl   rk   any_string_typer   r  rh  r  r{   cascader  r  r  )rL   Zoperand1_resultr  Zoperand2_resultrJ   rJ   rK   "calculate_cascaded_constant_result/  s$    



z*CmpNode.calculate_cascaded_constant_resultc          
   C   sv   t | }| j|}y|||}W n0 tk
rR } z| | d }W d d }~X Y nX |rr| j}|rr|op|||}|S )N)r  r  r   rd  r   r  cascaded_compile_time_value)rL   r  r   r  r  r   r   r  rJ   rJ   rK   r  /  s    
z#CmpNode.cascaded_compile_time_valuec             C   s   | j jjp| jjjS )N)r  r   r0  r  )rL   rJ   rJ   rK   is_cpp_comparison/  s    zCmpNode.is_cpp_comparisonc       	      C   s~   |j }|j }d}d}|jr&| r&d}|jr8| r8d}|jrH|rz|S n2|jrX|rz|S n"|rz|rztj||fkrttjS tjS d S )NFT)	r   r[  r  r+  r   r   r   rO  rr  )	rL   rc   r  r  r  r  r  Ztype1_can_be_intZtype2_can_be_intrJ   rJ   rK   find_common_int_type/  s(    zCmpNode.find_common_int_typec       	      C   s  | j }|j}|j}d }|tkr0|jsJ|ttfksJ|tkr\|jsJ|ttfkr\t| jd t}n|j	sh|j	r|dkr|j	s||j
r|j	s|j
rt| jd t}nH|jr|tjrtjnt}n*|jr|tjrtjnt}nt||}n<|j
r|j
rt||}n"|d ks|js | ||||}|d kr|js:|jr@t}n||krP|}n|js`|jr|j
sp|jr|||rt}nt}nZ|j
s|jr|||rt}nt}n0t|rt|rt}n| ||| t}n6||r|}n$||r
|}n| ||| t}|jr@t|ts<t|tr@t}|d ksR|jrX|}nt||}| jr| j|| j||}|S )NzFComparisons between bytes/unicode and str are not portable to Python 3)z==z!=zcomplex types are unordered)r  r   r   r  r   r   r   rn   r*   r2  r  ra   r.  r   r  r   r   r|  r  rG  r  r3  invalid_types_errorrk   r  r&  Zspanning_typer  find_common_typer
   )	rL   rc   r  r  common_typer  r  r  Znew_common_typerJ   rJ   rK   r  /  sp    

zCmpNode.find_common_typec             C   s   t | jd||j|jf  d S )NzInvalid types for '%s' (%s, %s))r   rn   r   )rL   r  r  r  rJ   rJ   rK   r   0  s    zCmpNode.invalid_types_errorc             C   s6   |    o4|   o4|  p4| jr,| j p4| jdkS )N)r  r  )is_ptr_containsis_c_string_containshas_python_operandsr  is_python_comparisonr
   )rL   rJ   rJ   rK   r  $0  s
    

zCmpNode.is_python_comparisonc             C   s6   | j }|j|kr|||| _ | jr2| j|| d S )N)r  r   r"  r  coerce_operands_to)rL   r6  rc   r  rJ   rJ   rK   r  +0  s
    
zCmpNode.coerce_operands_toc             C   s@   |   r0| jd kr0| jdkr0|   r0|   p>| jo>| j S )N)r  r  r  r  )r  special_bool_cmp_functionr
   r  r  r  is_python_result)rL   rJ   rJ   rK   r  20  s    



zCmpNode.is_python_resultc             C   s@   | j dko>| jjjr*| jjjp>| jjtkp>| jjjo>| jjtkS )N)r  r  )	r
   r  r   r+  r  r  r   r  r   )rL   rJ   rJ   rK   r  :0  s
    


zCmpNode.is_c_string_containsc             C   s*   | j dkr&| jj}|js|jo$|j S d S )N)r  r  )r
   r  r   r  r   r  )rL   Zcontainer_typerJ   rJ   rK   r  A0  s    
zCmpNode.is_ptr_containsFc             C   s  | j dkr|j| jj }}|s*|jr|jr|tjks>|tjkrVtdd| _d| _	dS |tj
ksj|tj
krtdd| _d| _	dS |tjks|tjkrtdd| _d| _	dS |tjks|tjkrtdd| _d	| _	dS n| j d
kr| jjtjkr| jd| _tdd| _d| _	dS | jjtjkrT| jd| _tdd| _d| _	dS | jjtjkr| jd| _tdd| _d| _	dS | jjjs| j|| _tdd| _d| _	dS dS )N)z==z!=ZUnicodeEqualszStringTools.cZ__Pyx_PyUnicode_EqualsTZBytesEqualsZ__Pyx_PyBytes_EqualsZ	StrEqualsZ__Pyx_PyString_Equals)r  r  z!'NoneType' object is not iterableZPyDictContainszObjectHandling.cZ__Pyx_PyDict_ContainsTFZPySetContainsZ__Pyx_PySet_ContainsTFZPyUnicodeContainsZ__Pyx_PyUnicode_ContainsTFZPySequenceContainsZ__Pyx_PySequence_ContainsTFF)r
   r   r  r  r   r   r   r  special_bool_cmp_utility_coder  r   r-   r   r   rS  r   ra   r=  )rL   rc   r  result_is_boolr  r  rJ   rJ   rK   "find_special_bool_compare_functionG0  sR    
z*CmpNode.find_special_bool_compare_functionc             C   s"  | j jr<|j}d| }| jr6|jtdd d}qJd}n|j}d}d}| jr|j jrb|	 }	n|
 }	|j jr||	 }
n|
 }
| jr|j| j |d||| j|	|
t| |||| jf  nV|j jr|dkr|d	kst||d
||	 |	 t| |||| jf  n |j jrd|d|||dkrBdpDd|j d|
 |
 f  n|j }|j }|js|jr||st}n|jrt||}n|}||}||}d|||| ||f }|  r| jdkrt|| j|| j jr|nd | j| j || d S )Nz__Pyx_XGOTREF(%s); ZPyBoolOrNullFromLongzObjectHandling.cZ__Pyx_PyBoolOrNull_FromLongZ__Pyx_PyBool_FromLongr  z%s = %s(%s(%s, %s, %s)); %s%s)r  r  )r  r  z+%s = PyObject_RichCompare(%s, %s, %s); %s%sz%s = %s(%s%s(%s, %s));z!=rF  eqz%s = %s(%s %s %s);r  )r   ra   r   r  r  r  r   r  r+  r   r   r  r   richcmp_constantsrn   r   r2  rI  r  r  r   r  r   r|  r   
c_operatorr  r  r   r   r   )rL   r   r  r  r  r  Zerror_clauseZgot_refZcoerce_resultr  r  r  r  r  code1code2Z	statementrJ   rJ   rK   r  t0  s    






zCmpNode.generate_operation_codec             C   s    |dkrdS |dkrdS |S d S )Nr  z==r  z!=rJ   )rL   r  rJ   rJ   rK   r  0  s
    zCmpNode.c_operator)N)F)rN   rO   rP   r  r  rr   r  r  r  r  r  r  r  r  r  r  r  r  r  r  rJ   rJ   rJ   rK   r  ~/  s"   
S
-Sr  c               @   s   e Zd ZddddgZdZdZdZdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ ZdS )%r  r  r  coerced_operand2r  NFc             C   sN   | j |}| j|}t|s(t|rJt|rJt|rJtt| j||S tS )N)	r  rr   r  r5   r3   rA   r7   r
   r   )rL   rc   r  r  rJ   rJ   rK   rr   0  s    zPrimaryCmpNode.infer_typec             C   s   dS )NrJ   rJ   )rL   rc   rJ   rJ   rK   r   0  s    z PrimaryCmpNode.type_dependenciesc             C   s   | j r
t| | jj d S )N)r  r   r  r  rl   )rL   rJ   rJ   rK   r   0  s    
z(PrimaryCmpNode.calculate_constant_resultc             C   s   | j |}| ||S )N)r  r   r  )rL   r   r  rJ   rJ   rK   r   0  s    z!PrimaryCmpNode.compile_time_valuec             C   s
  | j || _ | j|| _|  rD| | | jr@t| jd | S | j j}| jj}t	|sdt	|rt
|rt
|rtt| j||| _d| _| S | |r| S | jr| j|| _| jdkr|  rnd| _d }| jrt| jd | S | jjtkr|tdd nR| j jtjkr.| j tj|| _ | jjtk	rL| jt|| _|tdd | jd| _nj|  r| jrt| jd	 tj| _| S | || j r| j jjs| j || _ d }d| _n
t }d
| _nP| || j r| j jjs| j || _ d }d| _n| !|| j| j }|j| _|d k	rf|j"sf| j j|krZ| j ||| _ | #|| | jr| j$|| _| j%| | j&| j|}|| jk	r|| _'| ( rtj | _ntj| _| j}x|r| j|_|j}qW | js | js | j)rd| _*| S )Nz5Cascading comparison not yet supported for cpp types.F)r  r  z?Cascading comparison not yet supported for 'int_val in string'.ZPyUCS4InUnicodezStringTools.cZBytesContainsz+argument of type 'NoneType' is not iterablez?Cascading comparison not supported for 'val in sliced pointer'.Tr$   )+r  r   r  r  analyse_cpp_comparisonr  r   rn   r   r5   r3   rA   r7   r
   is_pycmp"analyse_memoryviewslice_comparisonr  r   r  r   r  r   rr  r"  r   r   rS  r  rp  r  ra   r=  r   r  r&  r  r    coerce_cascaded_operands_to_tempoptimise_comparisonr  r  r  r   )rL   rc   r  r  r  r  ZcdrrJ   rJ   rK   r   0  s    






zPrimaryCmpNode.analyse_typesc             C   s  | j j}| jj}d| _|| j| j | jg}|d kr^t| jd| j||f  tj	| _d| _
d S |j}|jrp|j}|j| _|j| _| jdkrd| _| jd kr|tdd t|jdkr| j|jd	 j|| _n0| j |jd	 j|| _ | j|jd j|| _|j| _d S )
NFzInvalid types for '%s' (%s, %s)z<error>r  Tr  zCppSupport.cppr$   r   )r  r   r  r  rs  r
   r   rn   r   r*   r  r  r  r  r   r   r  r   r  rw   r{   r"  r  )rL   rc   r  r  r   r  rJ   rJ   rK   r  P1  s0    

z%PrimaryCmpNode.analyse_cpp_comparisonc             C   sV   | j jp| jj}| j jjp"| jjj}d}|rR|rR| j|krRd| _tj| _d| _	dS dS )N)z==z!=r  r  FT)
r  rk  r  r   r   r
   r  r   rp  is_memslice_nonecheck)rL   rc   Z	have_noneZ
have_sliceZopsrJ   rJ   rK   r  k1  s    

z1PrimaryCmpNode.analyse_memoryviewslice_comparisonc             C   sf   | j rZ| j|| jddrZd| _ tj| _d| _| jrV| jj| j	|dd}|| j	k	rV|| _
| S t| |S )NT)r  Fr$   )r  r  r  r   rp  r   r   r  r  r  r  r   r   )rL   rc   r  rJ   rJ   rK   r   x1  s    
z PrimaryCmpNode.coerce_to_booleanc             C   s   | j jjp| jjjS )N)r  r   ra   r  )rL   rJ   rJ   rK   r  1  s    
z"PrimaryCmpNode.has_python_operandsc             C   s*   | j r|   dS | j o$| j S d S )NF)r  r   r  r   r  )rL   rJ   rJ   rK   r   1  s    zPrimaryCmpNode.check_constc             C   s   | j | j }}|jjrJ| jdkr&d}nd}d||jd| | f S |  r|jtkrbd}nd}| jdkrvd}nd}d||| | f S t	| jr|
 |
  }}n2| |  }}| jr|jjrd	| }nd	| }d
|| | j|f S d S )Nz!=rF  r  z(%s%s(%s, %s))z==Z__Pyx_UnicodeContainsUCS4Z__Pyx_BytesContainsr  z((PyObject *) %s.memview)z
(%s %s %s))r  r  r   r2  r
   r  r   r  r   r5   r   r  r   r  )rL   r  r  Znegationmethodr  r  rJ   rJ   rK   r   1  sB    






z$PrimaryCmpNode.calculate_result_codec             C   s   | j | | j| | jr| | | ||  | j | j| j | jrp| jj||  | j	pb| j| j	d k	d | j 
| | j | | j
| | j| d S )N)needs_evaluation)r  r  r  r   r   r  r   r
   r  r  r  r  )rL   r   rJ   rJ   rK   r  1  s    
z'PrimaryCmpNode.generate_evaluation_codec             C   s   | j | | j| d S )N)r  r  r  )rL   r   rJ   rJ   rK   r  1  s    z-PrimaryCmpNode.generate_subexpr_disposal_codec             C   s   | j | | j| d S )N)r  r  r  )rL   r   rJ   rJ   rK   r  1  s    z!PrimaryCmpNode.free_subexpr_tempsc             C   s.   | j | | j| | jr*| j| d S )N)r  r  r  r  )rL   r   rJ   rJ   rK   r  1  s    zPrimaryCmpNode.annotate)rN   rO   rP   re  r  r  r  rr   r   r   r   r   r  r  r   r  r   r   r  r  r  r  rJ   rJ   rJ   rK   r  0  s&   W*r  c               @   s~   e Zd ZdddgZdZdZeZdd Zdd Z	d	d
 Z
dd Zdd Zdd ZdddZdd Zdd ZdddZdd ZdS )CascadedCmpNoder  r  r  Nc             C   s   t S )N)r   )rL   rc   rJ   rJ   rK   rr   1  s    zCascadedCmpNode.infer_typec             C   s   dS )NrJ   rJ   )rL   rc   rJ   rJ   rK   r   1  s    z!CascadedCmpNode.type_dependenciesc             C   s   | j tk	o| j tk	S )N)rl   r   r   )rL   rJ   rJ   rK   rz   1  s    
z#CascadedCmpNode.has_constant_resultc             C   s&   | j || _ | jr"| j|| _| S )N)r  r   r  )rL   rc   rJ   rJ   rK   r   1  s    zCascadedCmpNode.analyse_typesc             C   s
   | j jjS )N)r  r   ra   )rL   rJ   rJ   rK   r  2  s    z#CascadedCmpNode.has_python_operandsc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   r  2  s    z!CascadedCmpNode.is_cpp_comparisonFc             C   sZ   |  |||r.d| _tj| _|jjs.||}| jrV| j| j	||}|| j	k	rV|| _
|S )NF)r  r  r   rp  r   ra   r=  r  r  r  r  )rL   r  rc   r  r  rJ   rJ   rK   r  	2  s    

z#CascadedCmpNode.optimise_comparisonc             C   sH   | j || _ | j jtkr2| jdkr2| j d| _ | jrD| j| d S )N)r  r  z!'NoneType' object is not iterable)r  r=  r   r   r
   rS  r  r  )rL   rc   rJ   rJ   rK   r  2  s
    z,CascadedCmpNode.coerce_operands_to_pyobjectsc             C   s$   | j r | j|| _| j | d S )N)r  r  r   r  )rL   rc   rJ   rJ   rK   r  2  s    z0CascadedCmpNode.coerce_cascaded_operands_to_tempc             C   s   | j jr&|d|  ||| j  n|d|  |rB|| | j| | |||| j| j | jr| jj||| j	p~| j| j	d k	d |r|
| || | j
| | j| |d d S )Nz if (__Pyx_PyObject_IsTrue(%s)) {z	if (%s) {)r  r   )r   ra   r   r  r  r  r  r
   r  r  r  r  )rL   r   r   r  r  rJ   rJ   rK   r  "2  s&    


z(CascadedCmpNode.generate_evaluation_codec             C   s"   | j | | jr| j| d S )N)r  r  r  )rL   r   rJ   rJ   rK   r  92  s    zCascadedCmpNode.annotate)F)F)rN   rO   rP   re  r  r  r   rl   rr   r   rz   r   r  r  r  r  r  r  r  rJ   rJ   rJ   rK   r   1  s   



r   )r  r  r  r  rR  z<<z>>r  r  r   r  r  z//r  z**c             K   s   t | | f||||d|S )N)r
   r  r  r  )binop_node_classes)rn   r
   r  r  r  rU  rJ   rJ   rK   r  R2  s    r  c                   s6   e Zd ZdgZeZ fddZdd Zdd Z  Z	S )CoercionNoder  c                s2   t t| |j || _tr.td| | jf  d S )Nz%s Coercing %s)rF   r  ru  rn   r  r   r  )rL   r  )rI   rJ   rK   ru  q2  s    zCoercionNode.__init__c             C   s   d S )NrJ   )rL   rJ   rJ   rK   r   w2  s    z&CoercionNode.calculate_constant_resultc          	   C   sX   | j | | j j| jkrT| j\}}}||||d ftddd| j j| jf d d S )Nr$   Zcoercez[%s] to [%s])r  tagr  )r  r  r   rn   r/   )rL   r   filelinecolrJ   rJ   rK   r  {2  s
    zCoercionNode.annotate)
rN   rO   rP   r   r   rl   ru  r   r  rQ   rJ   rJ   )rI   rK   r  i2  s
   r  c               @   s    e Zd ZdZdd Zdd ZdS )r#  zc
    Coerce an object to a memoryview slice. This holds a new reference in
    a managed temp.
    c             C   sD   |j s
t|jj rtt| | || _d| _|| _d| _|| _d S )Nr$   T)	r   r   r   r  ru  r   rc   r   r  )rL   r  r6  rc   rJ   rJ   rK   ru  2  s    
z!CoerceToMemViewSliceNode.__init__c             C   s6   | j | j || j | j |  | j| d S )N)	r   r[  rc   r   from_py_call_coder  r   r   rn   )rL   r   rJ   rJ   rK   r   2  s    
z-CoerceToMemViewSliceNode.generate_result_codeN)rN   rO   rP   r`  ru  r   rJ   rJ   rJ   rK   r#  2  s   
r#  c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r  c             C   s   t | | || _d S )N)r  ru  r   )rL   r  Znew_typerJ   rJ   rK   ru  2  s    zCastNode.__init__c             C   s
   | j  S )N)r  rN  )rL   rJ   rJ   rK   rN  2  s    zCastNode.may_be_nonec             C   s   | j | jS )N)r  r   r   )rL   rJ   rJ   rK   r   2  s    zCastNode.calculate_result_codec             C   s   | j | d S )N)r  r   )rL   r   rJ   rJ   rK   r   2  s    zCastNode.generate_result_codeN)rN   rO   rP   ru  rN  r   r   rJ   rJ   rJ   rK   r  2  s   r  c               @   s   e Zd ZdZdddZejZdZdd Z	dd	 Z
d
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS ) r/  TFc             C   s:   |j s|jstdt| | || _| | _|| _d S )Nz PyTypeTest on non extension type)	r  r  r   r  ru  r   r   r   rZ  )rL   r  r6  rc   rZ  rJ   rJ   rK   ru  2  s
    
zPyTypeTestNode.__init__zPython type testc             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   2  s    zPyTypeTestNode.analyse_typesc             C   s   | j r
dS | j S )NF)rZ  r  rN  )rL   rJ   rJ   rK   rN  2  s    zPyTypeTestNode.may_be_nonec             C   s
   | j  S )N)r  rM  )rL   rJ   rJ   rK   rM  2  s    zPyTypeTestNode.is_simplec             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   2  s    zPyTypeTestNode.result_in_tempc             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   2  s    zPyTypeTestNode.is_ephemeralc             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   2  s    z#PyTypeTestNode.nonlocally_immutablec             C   sR   | j | jj ks| jjs| S | j  s(| S | j rL| jrL| jd| j j S | jS )Nz!Cannot convert NoneType to %.200s)r   r  r   typeobj_is_availablerN  rZ  rS  rb   )rL   rJ   rJ   rK   	reanalyse2  s    
zPyTypeTestNode.reanalysec             C   s   d S )NrJ   )rL   rJ   rJ   rK   r   2  s    z(PyTypeTestNode.calculate_constant_resultc             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   2  s    z$PyTypeTestNode.calculate_result_codec             C   s   | j  rv| j jr0| j j| j | j| jd}n*| j | j | j}|j	t
dd |d||| jf  nt| jd d S )N)exactZExtTypeTestzObjectHandling.czif (!(%s)) %szICannot test type of extern C class without type object name specification)r   r  r  Ztype_test_coder  r   rZ  r$  r  r  r   r  r   r   rn   r   )rL   r   Z	type_testrJ   rJ   rK   r   2  s    
z#PyTypeTestNode.generate_result_codec             C   s   | j | d S )N)r  r	  )rL   r   rJ   rJ   rK   r	  2  s    z,PyTypeTestNode.generate_post_assignment_codec             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  2  s    zPyTypeTestNode.free_tempsN)F)rN   rO   rP   r$  ru  r%   r   r   r  r   rN  rM  r   r   r   r	  r   r   r   r	  r  rJ   rJ   rJ   rK   r/  2  s    
		r/  c               @   s   e Zd ZdZd"ddZdZdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zed#ddZed$ddZdd Zdd Zdd Zd d! ZdS )%rQ  TrJ   c             C   s<   t | | |j| _| | _|| _|| _t|p2d| _d S )NrJ   )	r  ru  r   r   r   exception_type_cnameexception_messager   exception_format_args)rL   r  r  r  r  rJ   rJ   rK   ru  3  s    
zNoneCheckNode.__init__Nc             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   3  s    zNoneCheckNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  3  s    zNoneCheckNode.may_be_nonec             C   s
   | j  S )N)r  rM  )rL   rJ   rJ   rK   rM  3  s    zNoneCheckNode.is_simplec             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   3  s    zNoneCheckNode.result_in_tempc             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   3  s    z"NoneCheckNode.nonlocally_immutablec             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   3  s    z#NoneCheckNode.calculate_result_codec             C   s4   | j jr| j S | j jr(d| j  S tdd S )Nz((PyObject *) %s.memview)zunsupported type)r   ra   r  r   r   r   rd  )rL   rJ   rJ   rK   	condition!3  s
    
zNoneCheckNode.conditionrP  Fc             C   s"   | ||||}||_ || d S )N)r   put_nonecheck)rH   r  r   r  r  r  r   rp   rJ   rJ   rK   generate)3  s    zNoneCheckNode.generatec             C   s    |  r| |||||| d S )N)rN  r  )rH   r  r   r  r  r  r   rJ   rJ   rK   generate_if_needed03  s    z NoneCheckNode.generate_if_neededc          
      s   | d|    | jr |  tj | jrh| d| jt| j	dd
 fdd| jD f  n | d| j | j	df  | jr|  | || j | d d S )	Nzif (unlikely(%s == Py_None)) {zPyErr_Format(%s, "%s", %s);zUTF-8z, c                s"   g | ]}d  t |d qS )z"%s"zUTF-8)rg   rQ  )rS   r  )escaperJ   rK   rt   C3  s   z/NoneCheckNode.put_nonecheck.<locals>.<listcomp>zPyErr_SetString(%s, "%s");r   )r   r  r   r   r	   Zescape_byte_stringr  r  r  rQ  r  r   r   rn   )rL   r   rJ   )r  rK   r  63  s&    zNoneCheckNode.put_nonecheckc             C   s   |  | d S )N)r  )rL   r   rJ   rJ   rK   r   P3  s    z"NoneCheckNode.generate_result_codec             C   s   | j | d S )N)r  r	  )rL   r   rJ   rJ   rK   r	  S3  s    z+NoneCheckNode.generate_post_assignment_codec             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  V3  s    zNoneCheckNode.free_temps)rJ   )rP  rJ   F)rP  rJ   F)rN   rO   rP   r  ru  r   r   rN  rM  r   r   r   r  rf  r  r  r  r   r	  r  rJ   rJ   rJ   rK   rQ  2  s$   
rQ  c               @   sP   e Zd ZeZeZdZefddZdZdd Z	dd Z
d	d
 Zdd Zdd ZdS )r-  r$   c             C   s   |j |s t|jd|j   n|j jr2||}t| | |tkr|j j	sV|j j
rbt|| _ n(|j jsr|j jrzt| _ n|j jrtj| _ | j | _nP|j j	s|j j
r|ttfkr|jd st|jd|j |f  | | _ | _n|| _d S )Nz$Cannot convert '%s' to Python objectrR   z:default encoding required for conversion from '%s' to '%s')r   r  r   rn   r2  r   r  ru  r   r  r  ri   r  r  r   r   r  target_typer   r    r[   )rL   r  rc   r   rJ   rJ   rK   ru  b3  s*    


zCoerceToPyTypeNode.__init__zConverting to Python objectc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  3  s    zCoerceToPyTypeNode.may_be_nonec             C   s<   | j j}|tjks"|jr.|jdkr.| j |S t| |S d S )Nr   )r  r   r   rp  ra   rb   rL  rD  )rL   rc   r  rJ   rJ   rK   r   3  s
    
z$CoerceToPyTypeNode.coerce_to_booleanc             C   s$   | j jjr| j S | j tj|S d S )N)r  r   r+  r"  r   r(   )rL   rc   rJ   rJ   rK   rJ  3  s    
z$CoerceToPyTypeNode.coerce_to_integerc             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   3  s    z CoerceToPyTypeNode.analyse_typesc             C   sL   | d| jj| j |  | j||  | jf  || 	  d S )Nz%s; %s)
r   r  r   Zto_py_call_coder   r  r   rn   r  r   )rL   r   rJ   rJ   rK   r   3  s    z'CoerceToPyTypeNode.generate_result_codeN)rN   rO   rP   r   r   r  r   ru  r  rN  r   rJ  r   r   rJ   rJ   rJ   rK   r-  Z3  s   r-  c               @   s    e Zd ZdZdd Zdd ZdS )r,  r$   c             C   s"   | |}t| | tj| _d S )N)r   r  ru  r   r   r   )rL   r  rc   rJ   rJ   rK   ru  3  s    
zCoerceIntToBytesNode.__init__c          	   C   s   | j }| }|jtjtjtjfkrn|jjr@|d||f  n|d|  |d|	| j
  |d d }|jtjk	r|jjtjdd}|d||f  |}|d|  |||  | j
f  |d k	r|j| ||   d S )	Nzif ((%s < 0) || (%s > 255)) {zif (%s > 255) {zOPyErr_SetString(PyExc_OverflowError, "value too large to pack into a byte"); %sr   F)r   z%s = (char)%s;z*%s = PyBytes_FromStringAndSize(&%s, 1); %s)r  r   r   r   r   rr  r  r  r   r   rn   r   r   r   r   r  r   )rL   r   r  Z
arg_resultrU  rJ   rJ   rK   r   3  s0    

z)CoerceIntToBytesNode.generate_result_codeN)rN   rO   rP   r   ru  r   rJ   rJ   rJ   rK   r,  3  s   r,  c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )r1  c             C   st   t | | || _d| _||s2t|jd|  | jjsB| jjrp| j	j
rp| j	jrp| j	jjrpt|jd| dd d S )Nr$   z$Cannot convert Python object to '%s'z=Obtaining '%s' from externally modifiable global Python value)rj   )r  ru  r   r   r[  r   rn   r  r  r  r  r   r  r   )rL   r  r  rc   rJ   rJ   rK   ru  3  s    

zCoerceFromPyTypeNode.__init__c             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   3  s    z"CoerceFromPyTypeNode.analyse_typesc             C   s   | j jo| j j o| j S )N)r   r  r   r  r   )rL   rJ   rJ   rK   r   3  s    z!CoerceFromPyTypeNode.is_ephemeralc          	   C   s   d }| j jrN| jj tkrN| j jdrNd| j jtdd   }t| j|d |	| j j
| j |  | j||d | j jr||   d S )NZ__Pyx_PyObject_AsZ__Pyx_PyBytesZ__Pyx_PyObjectzexpected bytes, NoneType found)from_py_function)r   r  r  r   r  r_  rw   rQ  r  r   r  r   r   rn   ra   r  )rL   r   r  rJ   rJ   rK   r   3  s    
z)CoerceFromPyTypeNode.generate_result_codec             C   s   t | jd d S )Nz0Coercion from Python not allowed without the GIL)r   rn   )rL   rc   rJ   rJ   rK   r   3  s    z CoerceFromPyTypeNode.nogil_checkN)rN   rO   rP   ru  r   r   r   r   rJ   rJ   rJ   rK   r1  3  s
   r1  c               @   sl   e Zd ZejZejdejdej	dej
dejdejdejdiZdd Zd	d
 ZdZdd Zdd Zdd ZdS )rD  ZPyList_GET_SIZEZPyTuple_GET_SIZEZPySet_GET_SIZEZPyBytes_GET_SIZEZPyByteArray_GET_SIZEZ__Pyx_PyUnicode_IS_TRUEc             C   s   t | | |jjrd| _d S )Nr$   )r  ru  r   ra   r   )rL   r  rc   rJ   rJ   rK   ru  4  s    zCoerceToBooleanNode.__init__c             C   s*   | j jjr&| j| j jd kr&|   d S )N)r  r   ra   _special_builtinsrZ   r   )rL   rc   rJ   rJ   rK   r   	4  s    zCoerceToBooleanNode.nogil_checkzTruth-testing Python objectc             C   s   | j r|   dS | j S )NF)r   r   r  r   )rL   rJ   rJ   rK   r   4  s    zCoerceToBooleanNode.check_constc             C   s   d| j   S )Nz	(%s != 0))r  r   )rL   rJ   rJ   rK   r   4  s    z)CoerceToBooleanNode.calculate_result_codec          	   C   s   | j s
d S | j| jj}|d k	rv| j r<d| j  gng }|d|| j f  |d| 	 d
|f  n,|d| 	 | j || 	 | jf  d S )Nz(%s != Py_None)z(%s(%s) != 0)z%s = %s;z&&z"%s = __Pyx_PyObject_IsTrue(%s); %s)r   r  rZ   r  r   rN  r   r   r   r   r  r+  rn   )rL   r   r@  ZchecksrJ   rJ   rK   r   4  s    z(CoerceToBooleanNode.generate_result_codeN)rN   rO   rP   r   rp  r   r   r   r   r   Zfrozenset_typer   r    r   r  ru  r   r  r   r   r   rJ   rJ   rJ   rK   rD  3  s   
rD  c               @   s$   e Zd Zdd Zdd Zdd ZdS )r4  c             C   s2   |j jr||}|| _ t| | || d S )N)r   r2  r   r  ru  r  )rL   r  r6  rc   rJ   rJ   rK   ru  *4  s
    
zCoerceToComplexNode.__init__c             C   sH   | j jjr(d| j   }d| j   }n| j  }d}d| jj||f S )Nz__Pyx_CREAL(%s)z__Pyx_CIMAG(%s)r  z
%s(%s, %s))r  r   r2  r   r  )rL   r^  r_  rJ   rJ   rK   r   14  s    

z)CoerceToComplexNode.calculate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   =4  s    z(CoerceToComplexNode.generate_result_codeN)rN   rO   rP   ru  r   r   rJ   rJ   rJ   rK   r4  (4  s   r4  c               @   s0   e Zd Zdd ZdZdd Zdd Zdd	 Zd
S )rK  c             C   s<   t | | | jj | _| jj| _d| _| jjr8t| _	d S )Nr$   )
r  ru  r  r   Zas_argument_typerl   r   ra   r   r   )rL   r  rc   rJ   rJ   rK   ru  E4  s    
zCoerceToTempNode.__init__z#Creating temporary Python referencec             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   O4  s    zCoerceToTempNode.analyse_typesc             C   s4   | j || _ | j  r| j S | j j| _| j| _| S )N)r  r   rM  r   r   )rL   rc   rJ   rJ   rK   r   S4  s    

z"CoerceToTempNode.coerce_to_booleanc             C   sf   | d|  | j|  f  | jrb| jjrF||  |   n| jj	rb|
|  | j  d S )Nz%s = %s;)r   r   r  r   r   r   r   ra   r   r   r   r   )rL   r   rJ   rJ   rK   r   [4  s    
z%CoerceToTempNode.generate_result_codeN)rN   rO   rP   ru  r  r   r   r   rJ   rJ   rJ   rK   rK  @4  s
   rK  c                   st   e Zd ZdZdZ fddZdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Z  ZS )r  z
    A node that should not be replaced by transforms or other means,
    and hence can be useful to wrap the argument to a clone node

    MyNode    -> ProxyNode -> ArgNode
    CloneNode -^
    Nc                s$   t t| | |j| _|   d S )N)rF   r  ru  rl   _proxy_type)rL   r  )rI   rJ   rK   ru  r4  s    zProxyNode.__init__c             C   s   | j || _ |   | S )N)r  r   r  )rL   rc   rJ   rJ   rK   r   w4  s    zProxyNode.analyse_typesc             C   s   | j |S )N)r  rr   )rL   rc   rJ   rJ   rK   rr   |4  s    zProxyNode.infer_typec             C   s:   t | jdr | jj| _| jj| _t | jdr6| jj| _d S )Nr   r   )r   r  r   r   r   )rL   rJ   rJ   rK   r  4  s
    

zProxyNode._proxy_typec             C   s   | j | d S )N)r  r   )rL   r   rJ   rJ   rK   r   4  s    zProxyNode.generate_result_codec             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   4  s    zProxyNode.resultc             C   s
   | j  S )N)r  rM  )rL   rJ   rJ   rK   rM  4  s    zProxyNode.is_simplec             C   s
   | j  S )N)r  rN  )rL   rJ   rJ   rK   rN  4  s    zProxyNode.may_be_nonec             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  4  s    z"ProxyNode.generate_evaluation_codec             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  4  s    z ProxyNode.generate_disposal_codec             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  4  s    zProxyNode.free_temps)rN   rO   rP   r`  r   ru  r   rr   r  r   r   rM  rN  r  r  r  rQ   rJ   rJ   )rI   rK   r  g4  s   r  c                   s|   e Zd Zg ZdZdd Zdd Zdd Zdd	 Zd
d Z	dd Z
 fddZdd Zdd Zdd Zdd Zdd Z  ZS )r  Nc             C   sD   t | | |j| _t|dr.|j| _|j| _t|dr@|j| _d S )Nr   r   )r  ru  rl   r   r   r   r   )rL   r  rJ   rJ   rK   ru  4  s    

zCloneNode.__init__c             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   4  s    zCloneNode.resultc             C   s
   | j  S )N)r  rN  )rL   rJ   rJ   rK   rN  4  s    zCloneNode.may_be_nonec             C   s   | j |S )N)r  r   )rL   rc   rJ   rJ   rK   r   4  s    zCloneNode.type_dependenciesc             C   s   | j |S )N)r  rr   )rL   rc   rJ   rJ   rK   rr   4  s    zCloneNode.infer_typec             C   s4   | j j| _| j j| _d| _t| j dr0| j j| _| S )Nr$   r   )r  r   r   r   r   r   )rL   rc   rJ   rJ   rK   r   4  s    


zCloneNode.analyse_typesc                s(   | j jr| j ||S tt| ||S )N)r  r   r"  rF   r  )rL   Z	dest_typerc   )rI   rJ   rK   r"  4  s    zCloneNode.coerce_toc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rM  4  s    zCloneNode.is_simplec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  4  s    z"CloneNode.generate_evaluation_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   4  s    zCloneNode.generate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  4  s    z CloneNode.generate_disposal_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  4  s    zCloneNode.free_temps)rN   rO   rP   r   r   ru  r   rN  r   rr   r   r"  rM  r  r   r  r  rQ   rJ   rJ   )rI   rK   r  4  s   	r  c               @   s   e Zd Zdd ZdS )r#  c             C   s$   |j r| j|r| S t| ||S )N)r  r   r.  r  r"  )rL   r6  rc   rJ   rJ   rK   r"  4  s    zCMethodSelfCloneNode.coerce_toN)rN   rO   rP   r"  rJ   rJ   rJ   rK   r#  4  s   r#  c               @   s8   e Zd ZeZdZg Zdd Zdd Zdd Z	dd	 Z
d
S )ModuleRefNodeFc             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   4  s    zModuleRefNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rN  4  s    zModuleRefNode.may_be_nonec             C   s   t jS )N)r   r  )rL   rJ   rJ   rK   r   4  s    z#ModuleRefNode.calculate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   4  s    z"ModuleRefNode.generate_result_codeN)rN   rO   rP   r   r   r   r   r   rN  r   r   rJ   rJ   rJ   rK   r  4  s   r  c               @   s2   e Zd ZdgZeZdZdd Zdd Zdd Z	d	S )
DocstringRefNoder  Tc             C   s"   t | | |jjst|| _d S )N)r   ru  r   ra   r   r  )rL   rn   r  rJ   rJ   rK   ru  4  s    zDocstringRefNode.__init__c             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   5  s    zDocstringRefNode.analyse_typesc          
   C   sL   | d|  | j |td||  | jf  ||   d S )Nz%s = __Pyx_GetAttr(%s, %s); %sr`  )	r   r   r  r  r	   r  r   rn   r  )rL   r   rJ   rJ   rK   r   5  s
    z%DocstringRefNode.generate_result_codeN)
rN   rO   rP   r   r   r   r   ru  r   r   rJ   rJ   rJ   rK   r  4  s   r  zF
static CYTHON_INLINE int __Pyx_ErrOccurredWithGIL(void); /* proto */
z
static CYTHON_INLINE int __Pyx_ErrOccurredWithGIL(void) {
  int err;
  #ifdef WITH_THREAD
  PyGILState_STATE _save = PyGILState_Ensure();
  #endif
  err = !!PyErr_Occurred();
  #ifdef WITH_THREAD
  PyGILState_Release(_save);
  #endif
  return err;
}
)protoimplzN
static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname);
z
static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname) {
    PyErr_Format(PyExc_UnboundLocalError, "local variable '%s' referenced before assignment", varname);
}
zM
static CYTHON_INLINE void __Pyx_RaiseClosureNameError(const char *varname);
z
static CYTHON_INLINE void __Pyx_RaiseClosureNameError(const char *varname) {
    PyErr_Format(PyExc_NameError, "free variable '%s' referenced before assignment in enclosing scope", varname);
}
zJ
static void __Pyx_RaiseUnboundMemoryviewSliceNogil(const char *varname);
a  
static void __Pyx_RaiseUnboundMemoryviewSliceNogil(const char *varname) {
    #ifdef WITH_THREAD
    PyGILState_STATE gilstate = PyGILState_Ensure();
    #endif
    __Pyx_RaiseUnboundLocalError(varname);
    #ifdef WITH_THREAD
    PyGILState_Release(gilstate);
    #endif
}
)r  r  ZrequiresZRaiseTooManyValuesToUnpackzObjectHandling.cZRaiseNeedMoreValuesToUnpackZUnpackTupleError)NN)F(4  Z
__future__r   ZcythonZdeclarerS  Zbintresysr  os.pathr   r
   ZErrorsr   r   r   r   r   r   ZCoder   r   r  r	   r   r   r%   r&   r'   r   r   r(   r)   r*   r+   r,   r   r   r   r   r   r   r   r   r   r    r-   r!   r   r   ZAnnotater/   r0   Z	Debuggingr1   Z
DebugFlagsr   r   r   ZPythranr2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   Z__builtin__rB   ImportErrorrg   re   r  rh   version_infor#   ro  r"   ZlongrD   r   r   r]   r_   r^   r`   r  Zc_const_py_unicode_ptr_typerY   r   ri   rq   r   r   r   r   r   r   r   rg  rh  ri  rl  rn  rA  rq  rs  r   r~  r  r  r  r  r  r  r  r!  r  r  r  r5  r9  r;  r<  rB  rE  rI  rJ  rL  rU  r_  rb  rc  r   r  r  r  r  r  r  r  re  r  r   rE  r  r<  rA  rI  rK  rL  rM  rV  rW  rF  rw  rz  rh  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r  r  r  r  r!  r&  r'  r(  r,  r-  r.  r/  r1  not_invnegrn   r5  r4  rE  rG  rH  rJ  rK  rL  rM  rP  rQ  rW  rX  r  rf  rg  ri  rd  re  r$  rq  r  rr  ru  rx  matmulr|  ry   ltler  negegtis_r  rQ  and_truedivfloordivr  modr  or_r  rshiftr  xorr  r  r  r  r  r  r  r  r  r  r  r  compilefindallr  r   r  r  r  r  r  r  r  r  r  r   r  r  r  r#  r  r/  rQ  r-  r,  r1  rD  r4  rK  r  r  r#  r  r  r(  Z&raise_unbound_local_error_utility_codeZ%raise_closure_name_error_utility_codeZ+raise_unbound_memoryview_utility_code_nogilr  r  r  Ztuple_unpacking_error_coderJ   rJ   rJ   rK   <module>   s  
 4D











	      %!	 .XkI2$     $@  1 <0`a       =o .   mC7    d <b [* #    43      ![6)". *0 3$.
84"4  *I f, "2
  ]2D&

	 D 	- 1D^3 pl  S  _O]I()4'4=