B
    de             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 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 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__ f/work/yifan.wang/ringdown/master-ringdown-env/lib/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   s   g | ]}|d krt d td fnp|jrDt|j|jr6|jnd g|j nJ|jrbt|j|j|j	|j
fn,| r|j|j|jt krt|jnd fnd qS )N)r   typeru   make_dedup_key
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   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_error3  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_declaration9  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_expressionsG  s    zExprNode.analyse_expressionsc             C   s
   |  |S )N)analyse_target_types)rL   rc   rx   rJ   rJ   rK   analyse_target_expressionM  s    z"ExprNode.analyse_target_expressionc             C   s   |  |}||}|S )N)r   coerce_to_boolean)rL   rc   rp   boolrJ   rJ   rK   analyse_boolean_expressionS  s    

z#ExprNode.analyse_boolean_expressionc             C   s   |  |}|||S )N)r   r   coerce_to_simple)rL   rc   rp   rJ   rJ   rK   analyse_temp_boolean_expressionY  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   k  s    z.ExprNode.type_dependencies.<locals>.<listcomp>)hasattrr   sumr   )rL   rc   rJ   )rc   rK   r   f  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   m  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_immutabley  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basename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_code9  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_tempsF  s    zExprNode.free_tempsc             C   s    x|   D ]}|| q
W d S )N)r   r  )rL   r   subrJ   rJ   rK   r  M  s    zExprNode.free_subexpr_tempsc             C   s   d S )NrJ   )rL   rc   r   rJ   rJ   rK   generate_function_definitionsQ  s    z&ExprNode.generate_function_definitionsc             C   s    x|   D ]}|| q
W d S )N)r   annotate)rL   r   rp   rJ   rJ   rK   r  V  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)	broadcast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&  \  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   r:  rJ   rJ   rK   r9    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   r9  )rL   r:  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r|jr|jdrt| jt| j| t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CoerceToBooleanNoder4  scopelookupSimpleCallNodeAttributeNoder	   EncodedStringr   	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_simplerS  )rL   rc   rJ   rJ   rK   r     s    zExprNode.coerce_to_simplec             C   s   |   S )N)r   )rL   rJ   rJ   rK   rT    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)rU  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   kwargs
attributes	attr_namer   rJ   rJ   rK   	from_node4  s    
zExprNode.from_node)N)N)r   )FNN)F)FN)rW  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&  r9  r  rA  r   rQ  rS  r   rT  rU  rV  rZ  classmethodr_  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   M  s    z/AtomicExprNode.generate_subexpr_evaluation_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  O  s    z-AtomicExprNode.generate_subexpr_disposal_codeN)rN   rO   rP   r   r   r  rJ   rJ   rJ   rK   rp  F  s   rp  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   rT  X  s    zPyConstNode.is_simplec             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU  [  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   a  s    z!PyConstNode.calculate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  d  s    z PyConstNode.generate_result_codeN)rN   rO   rP   r   r   r   rT  rU  r   r   r  rJ   rJ   rJ   rK   rq  R  s   rq  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   r  s    zNoneNode.compile_time_valuec             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rU  u  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   rr  r&  )rL   r:  rc   )rI   rJ   rK   r&  x  s    zNoneNode.coerce_to)rN   rO   rP   is_noner   rl   r   r   rU  r&  rQ   rJ   rJ   )rI   rK   rr  h  s   rr  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   rv  rl   r   rJ   rJ   rJ   rK   ru    s   ru  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   rT    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   rU    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   rT  r   rU  r   r   r   r   r  rJ   rJ   rJ   rK   rw    s   rw  c               @   s2   e Zd ZejZdd Zdd Zdd Zdd Z	d	S )
rE  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   s   || j kr| S |tkr&| j tjkr&| S |jrL| j jrLt| j| j| j	tjdS |jrr| j jrrt| j| j| j	t
jdS t| ||S )N)r   rl   r   )r   r   r   Z	bool_typera   r/  rE  rn   r   rl   r   c_bint_typerw  r&  )rL   r:  rc   rJ   rJ   rK   r&    s    


zBoolNode.coerce_toN)
rN   rO   rP   r   ry  r   r   r   r   r&  rJ   rJ   rJ   rK   rE    s
   rE  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   rz    s   rz  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)r}  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   r|    s   r|  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   r~    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$   rx  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   r    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   r  r  r  F)r   rl   r  r  )r   rG  rz   	FloatNodern   rx  rl   rm   r   r   
is_numericr6  r   r  r  ra   r   r   rw  r&  )rL   r:  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   r  r  )r   rn   r   rl   r   ry  r  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  1  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$   0r  L)	r  r  value_as_c_integer_stringr   r/  r  r   Zc_longlong_typer(   )rL   r  r  literalrJ   rJ   rK   r   9  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  rx  r  r  )rL   r   Zneg_signZliteral_typerJ   rJ   rK   r  D  s&    z!IntNode.value_as_c_integer_stringc             C   s   | j S )N)r  )rL   rJ   rJ   rK   r   `  s    zIntNode.calculate_result_codec             C   s   t | j| _d S )N)r   r  r   rl   )rL   rJ   rJ   rK   r   c  s    z!IntNode.calculate_constant_resultc             C   s   t | jS )N)r   r  r   )rL   r   rJ   rJ   rK   r   f  s    zIntNode.compile_time_value)rN   rO   rP   r  r  r  r~  r  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   l  s    z#FloatNode.calculate_constant_resultc             C   s
   t | jS )N)rm   r   )rL   r   rJ   rJ   rK   r   o  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   rG  r  rn   r   rl   r   Z
float_typerw  r&  )rL   r:  rc   rJ   rJ   rK   r&  r  s    
zFloatNode.coerce_toc             C   s   | j S )N)r  )rL   rJ   rJ   rK   r     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   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  i  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_scoperJ  r   is_extension_typeis_struct_or_unionis_builtin_typer4  r  r   filenamer   root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   rE  rn   )rL   rc   rP  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_charr|  r   r}  r  rl   ra   r   r   r   r  r   r]   r_   r^   r`   c_void_ptr_typeCastNoder7  r4  r  rw  r&  )rL   r:  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  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   rd  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   rM  r  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   r}  r   r   rg   ra   r  r  r  r&  r  r  r   r   r-   r  )rL   r:  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  K  s    z&UnicodeNode.can_coerce_to_char_literalc             C   s   t | j}t| j||dS )N)r   rl   )r   r   rE  rn   )rL   rc   rP  rJ   rJ   rK   r   Q  s    
zUnicodeNode.coerce_to_booleanc             C   s   t | jS )N)r	   r  r   )rL   rJ   rJ   rK   contains_surrogatesU  s    zUnicodeNode.contains_surrogatesc          
   C   s   | j jrt| jr|td| _|t	| j
d}|| j}|d krRd S || j |d| j|||| j| jf  || jd| j  q|| j| _n|| j| _d S )NZustringZunicode_escapez@%s = PyUnicode_DecodeUnicodeEscape(%s, sizeof(%s) - 1, NULL); %sz__Pyx_PyUnicode_READY(%s))r   ra   r	   Zstring_contains_lone_surrogatesr   get_py_constr   r  r  ZBytesLiteralencodeget_cached_constants_writerr  rn   r   r   put_error_if_negr  Zget_pyunicode_ptr_const)rL   r   Z
data_cname
const_coderJ   rJ   rK   r  X  s&    z$UnicodeNode.generate_evaluation_codec             C   s   | j S )N)r  )rL   rJ   rJ   rK   r   v  s    z!UnicodeNode.calculate_result_codec             C   s   | j S )N)r   )rL   rc   rJ   rJ   rK   r   y  s    zUnicodeNode.compile_time_value)N)rN   rO   rP   rd  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  rM  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   r2  ra   r  rn   r   r&  r   r-   r  )rL   r:  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   rd  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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   rU    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_typerp  r&  )rL   r:  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   rU  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   r4  r   rn   r*   r   	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   rU    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   rU  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   rV  :  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   rJ  rb   r   is_unspecified)rL   rc   rJ   rJ   rK   r   =  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   rJ  rb   r   r+   inferred_typer   r  r  r   r   rI  Zis_builtin_scoper   CPtrTypera   r/  Zmight_overflow)rL   rc   rJ   rJ   rK   rr   E  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)rJ  rb   KeyErrorr   rn   )rL   r   rJ   rJ   rK   r   a  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   g  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   r:  rc   r   Z	var_entryrp   )rI   rJ   rK   r&  l  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_herer`  rd  r'   r+   r   declare_varrn   )rL   rc   	as_targetrb   r`  atype_rJ   rJ   rK   declare_from_annotation  s    
z NameNode.declare_from_annotationc             C   s*   | j }|s|| j}|r&|jr&|jS d S )N)r   rJ  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   rJ  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   rJ  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   r`  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   rJ  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)rj  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   rj  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_entryrF  r   Entryrb   r   r  rI  r  r  r  r   r4  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   rJ  rb   r  r  )rL   rc   r   rJ   rJ   rK   "is_cimported_module_without_shadow@  s    z+NameNode.is_cimported_module_without_shadowc             C   s   dS )Nr$   rJ   )rL   rJ   rJ   rK   rT  F  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   rU  rF   r%  )rL   rU  
assignment)rI   rJ   rK   rU  J  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_localZis_argr  is_readonly)rL   r   rJ   rJ   rK   r   _  s    

zNameNode.nonlocally_immutablec             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   calculate_target_resultsg  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   j  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   u  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   |  s    
zNameNode.is_lvaluec             C   s   | j jo| jj S )N)r   r  r   r   )rL   rJ   rJ   rK   r     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_cnamerI  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   rI  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  \	  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  l	  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  rI  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  	  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  rV  r   rr   r   r   r&  r  r   r   r   r   r   r   r  r   r  r  r  r  rT  rU  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   rA  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   rA  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  ImportzImportExport.cz__Pyx_Import(%s, %s, %d)r   z%s(%s)z%s = %s; %s)r!  r   r  r  r   r  r   rj   rd  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  r4  analyse_cpp_typesrA  r   r   rZ  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   D
  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  r4  rI  rJ  r   return_typera   r   )rL   rc   sequence_typer)  rJ   rJ   rK   rr   G
  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   rI  rJ  r   r{   r   rn   r*   r*  r4  lookup_operator_for_types)rL   rc   r+  r)  r,  Z	iter_typerJ   rJ   rK   r'  S
  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   r4  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  
  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)r2  r   r   r&  ru   rw   r{   r   rk   rl   r"   r   r0  error_goto_if_negrn   r   r  )rL   Z	test_nameresult_namer   Z
final_size
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   r0  r   r2  r4  r/  r   r   r9  r   r1  r4  r   r5  r   rn   r  )rL   r7  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)r2  r   r   r4  r/  r   r  )rL   r   rJ   rJ   rK   r    s    zIteratorNode.free_temps)rN   rO   rP   r   r   r4  r2  r/  r0  is_asyncr   r   r  r   r  	CFuncTypeCFuncTypeArgr3  r   rr   r'  r  r9  r=  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)rp  r~  rn   iterator)rL   rB  rJ   rJ   rK   r~  /  s    zNextNode.__init__c             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   r   3  s    zNextNode.nogil_checkc             C   s   | j |S )N)rB  r   )rL   rc   rJ   rJ   rK   r   7  s    zNextNode.type_dependenciesNc             C   s   |d kr| j |}|js |jr&|jS | j jjtkr8tS |j	rp|
| jd|gjj}|jr`|j}|jrl|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   )rB  rr   r  r   r   r&  r   r    r   r4  r-  rn   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   rB  r   r   )rL   rc   rJ   rJ   rK   r   S  s    zNextNode.analyse_typesc             C   s   | j |  | d S )N)rB  r=  r   )rL   r   rJ   rJ   rK   r  X  s    zNextNode.generate_result_code)N)	rN   rO   rP   r~  r   r   rr   r   r  rJ   rJ   rJ   rK   rA  '  s   
rA  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   i  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   rA  )rL   rc   rJ   rJ   rK   r   l  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  s  s    z&AsyncIteratorNode.generate_result_codeN)rN   rO   rP   r   r>  r   r   r   rr   r   r  rJ   rJ   rJ   rK   rE  \  s   rE  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)rp  r~  rn   rB  )rL   rB  rJ   rJ   rK   r~    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 )NrF  zCoroutine.cz*%s = __Pyx_Coroutine_AsyncIterNext(%s); %s)r  r  r   r  r   r   rB  r   r   rn   r  )rL   r   rJ   rJ   rK   r    s    z"AsyncNextNode.generate_result_codeN)
rN   rO   rP   r   r   r   r~  rr   r   r  rJ   rJ   rJ   rK   rG  |  s   rG  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   rI  r   ry  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  rI  r   r  r   r   r  r   )rL   r   Z
result_varrJ   rJ   rK   r    s<    



z)WithExitCallNode.generate_evaluation_code)rN   rO   rP   r   rM  rI  r   r  rJ   rJ   rJ   rK   rH    s
   rH  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   r~  )rL   rn   rJ   rJ   rK   r~    s    zExcValueNode.__init__c             C   s
   || _ d S )N)var)rL   rO  rJ   rJ   rK   set_var  s    zExcValueNode.set_varc             C   s   | j S )N)rO  )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   r~  rP  r   r  r   rJ   rJ   rJ   rK   rN    s   rN  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   r~  r   ra   r   r   r   )rL   rn   r   rc   rJ   rJ   rK   r~    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   rR  )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)rR  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   r~  r   r   r  rS  rT  r   r   r   rJ   rJ   rJ   rK   rQ    s   	
rQ  c               @   s   e Zd Zdd ZdS )
PyTempNodec             C   s   t | |tj| d S )N)rQ  r~  r   r   )rL   rn   rc   rJ   rJ   rK   r~     s    zPyTempNode.__init__N)rN   rO   rP   r~  rJ   rJ   rJ   rK   rU    s   rU  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   r~  r   )rL   rn   r   r   rJ   rJ   rK   r~  &  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   1  s    zRawCNameExprNode.resultc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  4  s    z%RawCNameExprNode.generate_result_code)NN)	rN   rO   rP   r   r~  r   rW  r   r  rJ   rJ   rJ   rK   rV  #  s   
rV  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   rA  )rS   v)rc   rJ   rK   rt   J  s    z/JoinedStrNode.analyse_types.<locals>.<listcomp>)rY  )rL   rc   rJ   )rc   rK   r   I  s    zJoinedStrNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU  M  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   rY  r   r   r   r   r   c_py_ucs4_typer   r   r  	enumerater  r   r   rk   r  r   r  UnicodeEncodeErrormaxmapr}  rg   FormattedValueNoder   r  r  r  r  r   r  r  r   r  r   r  r   )rL   r   	num_itemsZlist_varZulength_varZmax_char_varirp   ZulengthZmax_char_valueZis_asciiZmax_charrJ   rJ   rK   r  Q  sx    








z&JoinedStrNode.generate_evaluation_codeN)
rN   rO   rP   r   r   r   r   r   rU  r  rJ   rJ   rJ   rK   rX  ?  s   rX  c               @   sH   e Zd ZddgZeZdZdZddddd	jZ	d
d Z
dd Zdd ZdS )r`  r   format_specTNZPyObject_UnicodeZPyObject_ReprZPyObject_ASCIIZ__Pyx_PyNumber_IntOrLong)sradc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU    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)rc  rd  )r   r   rc  rd  r   Zdefault_format_specZcan_coerce_to_pystringc_format_specrA  conversion_charr   rU  )rL   rc   rh  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_FormatSimplerd  z(invalid conversion character found: '%s'z%s(%s)ZPyObjectFormatAndDecrefzStringTools.cZ	AndDecrefZPyObjectFormatZPyObjectFormatSimplez%s = %s(%s, %s); %s)rh  r   r   ra   Zconvert_to_pystringr   r   r   rn   r  r   r   rU  rc  r   Zempty_unicoderi  find_conversion_funcr   r  r  r   r  )	rL   r   Zconvert_func_callZvalue_resultZvalue_is_unicodeZformat_funcrc  ri  fnrJ   rJ   rK   r    sL    


z'FormattedValueNode.generate_result_code)rN   rO   rP   r   r   r   r   rh  rZ   rj  rU  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 )	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   rl    s
   	rl  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   1  s    zParallelThreadIdNode.resultN)
rN   rO   rP   rm  r   rn  r   r   r  r   rJ   rJ   rJ   rK   ro    s
   ro  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   A  s    z_IndexingBaseNode.is_ephemeralc             C   s   | j  o| j S )N)r   r   r   r   )rL   rJ   rJ   rK   r   G  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   J  s    
z_IndexingBaseNode.is_lvalueN)rN   rO   rP   r   r   r   rJ   rJ   rJ   rK   rp  <  s   rp  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   j  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   m  s    zIndexNode.compile_time_valuec             C   s.   | j }| o,| j o,|jo,|jjp,|jjS )N)r   rT  r   r   r  r   )rL   r   rJ   rJ   rK   rT  u  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   rU  )rL   r   rJ   rJ   rK   rU  z  s    
zIndexNode.may_be_nonec             C   s   d S )NrJ   )rL   rc   rJ   rJ   rK   r     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   r4  rk   r   	TupleNoder{   r   ZTemplatedTypeNodern   analyser   ru   r  r  r  view_utility_coder   r   MemoryViewSliceTypeget_axes_specsr   rx  r|   r}   
CArrayTyper   )rL   rc   r   Ztemplate_valuesZ	type_noder  axesr   rJ   rJ   rK   r     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   r  rJ   rJ   rK   r~    s    z2IndexNode.infer_type.<locals>.FakeOperand.__init__N)rN   rO   rP   r~  rJ   rJ   rJ   rK   FakeOperand  s   r~  )rn   r   z[])%r   rr   r   r   r  r   r  r   r   r    r   r   r   r/  rk   r   r   r[  c_uchar_typer  r   r  r   r   rN  rz   rl   r  rO  r4  rn   lookup_operatorr   r*  r5   rA   r;   )
rL   rc   r   
index_typerD  r   r~  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')r  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   rA  r   r  r  r  r   analyse_as_buffer_operationr   r   original_index_typer  r   rn   rl   ra   analyse_as_pyobjectanalyse_as_c_arrayr4  analyse_as_cppanalyse_as_c_functionrN  analyse_as_c_tupler   )	rL   rc   r  r  analyse_baser   Zis_memslicereplacement_noder   rJ   rJ   rK   r    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   r  r   boundscheck)r   z&'NoneType' object is not subscriptable)#r   r   r   r  r   r   rn   rA  r   r/  r   r   r    signedr[   rk   r   rz   rl   r&  r   r   r   r  create_to_py_utility_coder   r[  r  rn  r   r   r   r   rZ  wrap_in_nonecheck_node)rL   rc   r   r  r  r   rD  rJ   rJ   rK   r  N  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    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   r  r   r   rn   r   r*   r  r  r   r  r   r   r  r   r  r&  r{   r*  r  )rL   rc   r  r   rD  	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)r  r  r  )r   r   rk   r   r   rz   rl   r  rO  r   rn   r   r*   rA  r  )rL   rc   r  r  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   ru  r{   r   r   r   r  r  ri  merged_indicesunellipsifyndimMemoryViewSliceNodern   MemoryViewIndexNoder  r5   rw   allr\  r   SliceIntNoder   r   r   r   BufferIndexNode	CloneNoder   )rL   rc   r  r  r   r   r  r  r  have_slicesnewaxesrg  Zdo_replacementrb  r   rJ   )rc   rK   r    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   rU  rZ  )rL   rc   r  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   ru  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   N  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  re  r   rn   rk   ru  r{   r  r   r   r  r   r   r#  r\  Zspecialize_fusedZget_fused_typesrw   r  r\   typesr*  r  r  r  r!  r"  rB  r   )rL   rc   r   Z	positionsr  Zspecific_typesrb  r   Zfused_typesrT   rn   Z
fused_typefused_to_specificr<  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   paramrJ   rJ   rK   rt     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  rN  r   rl   r  r  r   r   rn   )rL   
index_coder   rJ   rJ   rK   r   s  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 )Nr  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_listr  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 )Nr{  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    r4  r  r   r   r   rn   r   r   r   r  r   r  )rL   r   r  Zerror_valuerD  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   r6  r  rn   )rL   
value_coder   rD  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_valuer4  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   r  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   r6  r  rn   r  r  )rL   r   r  rD  r  rJ   rJ   rK   r  L  s&    





z IndexNode.generate_deletion_code)FFT)T)FNN)F)#rN   rO   rP   r   r  rf  r  r   r   rT  rU  r   r   r   rr   r   r   r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  rJ   rJ   rJ   rK   r   Z  sD   
 Q 
E55
[G! 
!$r   c                   s   e Zd ZdZddgZd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 fd"d#Z  Z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  TFrJ   c             C   s   | j |dd d S )NF)r  )r   )rL   rc   rJ   rJ   rK   r   y  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   r  rJ   rJ   rK   r   |  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   r  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   rU  rZ  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)rg  rh  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   ivar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rp|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   rg  rh  r  r[   r   rn   r  index_tempsr  r  r  r   r  r  Zbuffer_defaultsZput_buffer_lookup_code)rL   r   r  r  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   s@  | j j}t|rt|jr|jjtt| j jdd}|d|  |d||| j 	 f  |d|t
| j||	 f  |j| 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_XDECREF(*%s);z*%s %s= %s;)r   r   r5   r3   r   r   rA   r=   r   r   r:   r  r   r  r  r.  ra   Zbuf_ptr_typer   r  put_xgiveref)
rL   rx   r   opr   rB  r  Zptrexprptrr   rJ   rJ   rK   r    s2    	
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||f  |d|  d S )Nz__Pyx_call_destructor(%s);znew (&%s) decltype(%s){%s%s};z%s = (PyObject *) *%s;z'if (unlikely(%s == NULL)) %s = Py_None;z__Pyx_INCREF((PyObject*)%s);)r5   r   r   r   r   r   r:   r  r  r  ra   )rL   r   resr  rJ   rJ   rK   r    s    z$BufferIndexNode.generate_result_codec                s6   x| j D ]}|j| qW d| _ tt| | d S )NrJ   )r  r   r   rF   r  r  )rL   r   temp)rI   rJ   rK   r  '  s    z"BufferIndexNode.free_subexpr_temps)T)F)r  )rN   rO   rP   rm  r   rg  r+  r  r   r   r  r  r  r   r   r  r  r  r  r  r  r  rQ   rJ   rJ   )rI   rK   r  e  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  re  r   rw   Zmemslice_indexr   r   r*   r   rn   r\  r   rt  ri  r   r{  r   r   r   r&  setattrr/  ra   warned_untyped_idxr   r  rh  original_indicesr   analyse_operationr  )rL   rc   r  r  r  r  r  r{  r  new_indicesZaxis_idxrb  r   accesspackingattrr   rJ   rJ   rK   r   4  sp    







z!MemoryViewIndexNode.analyse_typesc             C   s   d| _ | || d S )Nz"Cannot index None memoryview slice)r  r  )rL   rc   r  r{  rJ   rJ   rK   r    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_operation  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_assignment  s
    
z8MemoryViewIndexNode.analyse_as_memview_scalar_assignmentN)T)
rN   rO   rP   rh  rg  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   rt  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   r  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   rx  r   r.  rT  r   rS  )rL   rc   r  r{  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.  r7  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  r\  r   r   rt  r   r   r   r   r/  rw   r   r  )rL   r  r  rb  rd  rJ   rJ   rK   r    s&    
z"MemoryViewSliceNode.merged_indicesc             C   s"   | j r| j p| j S |  S )N)r  r   rT  r   )rL   rJ   rJ   rK   rT    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   rt  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   ri  r  r  rh  r,  r  r  r  r  rT  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  r~  r  r   )rL   rn   r  )rI   rJ   rK   r~  7  s    zMemoryCopyNode.__init__Fc             C   sH   | j | | || | 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   rm  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 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   r6  copy_slice_cnamer   r  r.  ra   )rL   r;  r   r  rJ   rJ   rK   r  Q  s    z)MemoryCopySlice._generate_assignment_codeN)rN   rO   rP   rm  r-  r  r  rJ   rJ   rJ   rK   r  E  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  r~  r   r.  )rL   rn   r  )rI   rJ   rK   r~  h  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   rT  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  l  s*    


z*MemoryCopyScalar._generate_assignment_code)rN   rO   rP   rm  r~  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  r4  r   r  r   r    r   r-   r   r   r  r   r   Zc_array_typer   r   )rL   rc   r   rJ   rJ   rK   rr     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   rU  )rL   r   rJ   rJ   rK   rU    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   rq  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)r  )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)r  r  r  r  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  rx  r   r&  PrimaryCmpNoderr  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  rC  r$   )%r   r   r   r  r5   r   rr  rn   rr  r   r   r   r  r[   rq   r   r   r  r   r  is_cpp_stringri   r  r   r  rA  r   r  rZ  r   copydeepcopyr  r   ra   r&  r   )rL   rc   r  	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  rw  rr  rn   rr  r   rx  ry  )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   r:  rc   )rI   rJ   rK   r&  X  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); %sr  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  	type_namehas_c_start
has_c_stopc_startc_stoppy_startpy_stoppy_slicecfuncrJ   rJ   rK   r  h  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)r  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)r  )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  r{  z&%sr{  )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  rx  r|   r   r   r   rk   r"   r   rn   r   r   )
rL   r   Ztarget_sizeZ
slice_size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  d  s    
zSliceIndexNode.start_codec             C   s,   | j r| j  S | jjjr$| jjjS dS d S )NrC  )r   r   r   r   r   r  )rL   rJ   rJ   rK   r  j  s
    


zSliceIndexNode.stop_codec             C   s   dS )Nz<unused>rJ   )rL   rJ   rJ   rK   r   r  s    z$SliceIndexNode.calculate_result_code)r   )T)FNN)F) rN   rO   rP   r   r  rr   r   rU  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    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 )rr  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     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  rq  r   )rL   r   r   r   r   r   rJ   rJ   rK   r     s    zSliceNode.compile_time_valuec             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU    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   rA  r   r   )rL   rc   r   r   r   rJ   rJ   rK   r     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   rU  r   r  r   r  rJ   rJ   rJ   rK   rr  w  s   
	rr  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  rq  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   rU    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   rt  rQ  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   rf  rJ   rJ   rK   r    s    
z!SliceIntNode.generate_result_codeN)
rN   rO   rP   r   r   r   rU  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   )rD  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   rD  r  r   alternatives	arg_types
func_entryresult_typerJ   )rc   rK   rr     s@    



zCallNode.infer_typec             C   s   | j |S )N)rD  r   )rL   rc   rJ   rJ   rK   r     s    zCallNode.type_dependenciesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rT     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_nonerD  r   r   r  r   r  r  rb   r   r  r   rU  )rL   r  r   rJ   rJ   rK   rU  '  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   rD  r  rJ   rJ   rK   set_py_result_type4  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')rD  r   r  explicit_args_kwdsr  rI  var_entriesr   DictItemNodern   r  rb   key_value_pairsDictNoderI   r   r&  r4  r{   rJ  r   r*   r   rV  r   rW  r  analyse_c_function_call)	rL   rc   r   r{   r  itemsr  memberr  rJ   )rc   rK   analyse_as_type_constructorN  s4    
&


z$CallNode.analyse_as_type_constructorc             C   s   | j jS )N)r   r  )rL   rJ   rJ   rK   r   j  s    zCallNode.is_lvaluec             C   s6   |   }|jr|   n|js2t|dds2|   d S )Nr   F)function_typera   r   r*  r   )rL   rc   r  rJ   rJ   rK   r   m  s
    
zCallNode.nogil_checkzCalling gil-requiring function)N)rN   rO   rP   r  rr   r   rT  rU  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 )rK  rL   coerced_selfrD  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     s    z5SimpleCallNode.compile_time_value.<locals>.<listcomp>)rD  r   r{   rq  r   )rL   r   rD  r{   r   rJ   )r   rK   r     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)rD  rV  rw   r{   r   rn   r   r   r  r   r   )rL   rc   r  r   operandrJ   rJ   rK   r     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| t|| jtt|| jjdS |jrVt| j| jd| _| j  | _d | _| || d| _ n2 fdd| jD | _| !  |j"dkrd| _ | S )	NTr$   F)r{   )function_cnamer+  r   c                s   g | ]}|  qS rJ   )r   )rS   r  )rc   rJ   rK   rt     s    z0SimpleCallNode.analyse_types.<locals>.<listcomp>r  )#r(  analysedrD  r  r   re  r   
is_cmethodrB  rL   r  r)  Zis_numpy_call_with_exprsr9   ra  r>   ru  rn   r{   r+  r<   r   Zadd_include_filer?   NumPyMethodCallNoder_  r@   rA   r6   ra   rA  r  r   r%  r  )rL   rc   rD  r  Zhas_pythran_argsr  rJ   )rc   rK   r     sN    


zSimpleCallNode.analyse_typesc             C   s   | j j}|jr|j}|S )N)rD  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   rY  z@descriptor '%s' requires a '%s' object but received a 'NoneType')rY  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)KrD  r   r*   r   is_static_methodrL   r  r   rn   r{   r4  rI  rJ  r   r  r   r   rf  r  r  r!  r  r  r#  r)  rw   optional_arg_counthas_optional_argsr   r/  Znot_nonerZ  formatrb   Zaccept_builtin_subtypesCMethodSelfCloneNoder  r&  r*  r  rk   r3  exact_builtin_typerangeminra   r   r   rS  r   r]   Zdefault_coerced_ctyper   r  r  r  r*  r  re  r  Zutility_code_definitionr   r   r   r  r   r  CFakeReferenceTyper  r  #pyerr_occurred_withgil_utility_coder   r  r[   r4  )rL   rc   r  r{   Zoverloaded_entryZ	functypesr  r   	max_nargsexpected_nargsactual_nargs
formal_argr  some_args_in_tempsrb  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>r{  z&%sz%s(%s)z, )r)  r   r   r*   r   r{   r   r  rw   r6  r   r   r  rg   rx  wrapper_callrD  r   is_unbound_cmethodopt_arg_structr   r  )rL   r  Zformal_argsZarg_list_coder{   r?  r@  rA  rB  
actual_argarg_codeZoptional_argsr   rJ   rJ   rK   rE    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 d}| jjo2| jj}| j	r|r|
 |kr|jtdd |d| jd 
 | jd j || jf  |jjrt| jjdks| jjr| jjrtt| | d S | jjr| jjd nd }| j| j||f}x|D ]}|d k	r|| qW || j | js,t| | |d kr|jtdd |d	| 
 |  |!| 
 | jf  nD|jtd
d |d| 
 |  |  |!| 
 | jf  |"|    x,|D ]$}|d k	r|#| |$| qW d S )N)absZlabsZ__Pyx_abs_longlongZCommonz
Overflow.czif (unlikely(%s == __PYX_MIN(%s))) {                PyErr_SetString(PyExc_OverflowError,                                "Trying to take the absolute value of the most negative integer is not defined."); %s; }r   r$   PyObjectCallNoArgzObjectHandling.cz%%s = __Pyx_PyObject_CallNoArg(%s); %sPyObjectCallOneArgz*%s = __Pyx_PyObject_CallOneArg(%s, %s); %s)%rD  r  re  r  use_entry_utility_coder   r   r/  r  r4  r   r  r   r  r   r{   r  r   rn   ra   rw   r+  r   rF   rK  r  rL   r*  r  r   r   r   r   r   r  r
  r  )rL   r   rD  Zabs_function_cnamesZis_signed_intr  r   Zsubexpr)rI   rJ   rK   r    sZ    









z'SimpleCallNode.generate_evaluation_codec          
   C   s  |   }|jrl| j }|jtdd |d| 	 | j
 ||| 	 | jf  ||   nr|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) n4|	r|.d/|	| j}nd}|d|||f  | jjr| 	 r||   | jr|j0| j d S )NrJ  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;z && z%s%s; %s)1r)  ra   r+  r   r  r  r   r  r   r   rD  r   rn   r  r   r7  rw   r{   r6  r   r   op_arg_structr   rH  r   Zpyrex_prefixr   r  Zopt_arg_cnamerb   r   r   r   r   r   r   Zerror_conditionr  r   r*  	cast_coder   rE  r)   r   r   r   r  r   )rL   r   r  rJ  rA  r@  r{   rB  rI  Z
exc_checksexc_valZ	exc_checkrx   r  Z
goto_errorrJ   rJ   rK   r    s|    








z#SimpleCallNode.generate_result_code)rN   rO   rP   r   rL   r*  r+  rF  r7  r   r.  r4  r   r   r   r   r)  r%  r   rE  r   r  r  rQ   rJ   rJ   )rI   rK   rK  w  s*   / R9rK  c               @   s"   e Zd ZdgZdZdZdd ZdS )r0  r+  Tc          
   C   s   | | j | | | jjd ks&t| jj}x|D ]}|| q4W |d |d| 	   |d| 	 | 	 | 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   rf  rJ   rJ   rK   rV   v  s    z?NumPyMethodCallNode.generate_evaluation_code.<locals>.<genexpr>)r  rn   r   r+  r   r   r{   r  r   r   r-  r  )rL   r   r{   r  rJ   rJ   rK   r  g  s    


z,NumPyMethodCallNode.generate_evaluation_codeN)rN   rO   rP   r   r   r  r  rJ   rJ   rJ   rK   r0  ]  s   r0  c               @   s    e Zd ZddgZdZdd ZdS )PyMethodCallNoderD  r+  Tc             C   sj  | | 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}	nl| jjr| jjrxV| jjD ]@}
|
j}|rL|jrL|j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rv|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 ]}|| qVW |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 |rN| 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)rB  r  r   r  )r  rB  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);rD  z%s = 1;r   rL  zObjectHandling.crM  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#endifr<  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);rJ  z*%s = __Pyx_PyObject_Call(%s, %s, NULL); %s)0r  rn   r   rD  r  r+  r   r   r{   r   r   r   r   r   r   r5  r   r
  r  r   rw   r   rn  re  r  rv   rx   rB  r   ra   r   r  r  r  r   r  Zput_xdecref_clearr   r   r  upperr   quick_temp_cnamer  r\  r  r  r  )rL   r   r{   r  Zreuse_function_temprD  Zself_argZarg_offset_cnamerT  Zlikely_methodr  r   	test_funcZcall_prefixZ
args_tuplerb  rJ   rJ   rK   r    s   


$


















z)PyMethodCallNode.generate_evaluation_codeN)rN   rO   rP   r   r   r  rJ   rJ   rJ   rK   rS  y  s   rS  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)rD  def_nodestar_argstarstar_argrw   r{   num_kwonly_args)rL   r  rJ   rJ   rK   can_be_inlinedA  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   N  s    z8InlinedDefNodeCallNode.analyse_types.<locals>.<listcomp>Fr   Tr$   zTArgument evaluation order in C function call is undefined and may not be as expected)rY  r   r{   rD  rZ  rw   r;  r   r&  r   ra   r   r   rS  r4  r   rn   )rL   rc   r  rA  rC  rb  rD  r  rJ   )rc   rK   r   K  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)rY  r   rD  rZ  r  r{   r   ra   r   r   r   r  r   r   pyfunc_cnamer   rn   r  )rL   r   rJ  r  r  Z	proto_argrJ   rJ   rK   r    s    

z+InlinedDefNodeCallNode.generate_result_code)rN   rO   rP   r   r   r   r   rD  rY  r^  r   r  rJ   rJ   rJ   rK   rX  4  s   
9rX  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   r~  )rL   rn   py_namer   r  r  rJ   rJ   rK   r~    s    zPythonCapiFunctionNode.__init__c             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r     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   r~  r   r  r   rJ   rJ   rJ   rK   r`    s
   
r`  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   r`  rD  rK  r~  )rL   rn   rY  r  r  ra  r\  rJ   rJ   rK   r~    s    
zPythonCapiCallNode.__init__)NN)rN   rO   rP   r  r~  rJ   rJ   rJ   rK   rb    s   rb  c                   s8   e Zd ZddgZdZ fddZdd Zdd	 Z  ZS )
CachedBuiltinMethodCallNoderB  r{   Tc                s&   t t| j|j||||j|jd d S )N)rB  r   r{   r  r   )rF   rc  r~  rn   r  r   )rL   Z	call_noderB  r   r{   )rI   rJ   rK   r~    s
    
z$CachedBuiltinMethodCallNode.__init__c             C   s   | j d k	r| j S t| S )N)r  r   rU  )rL   rJ   rJ   rK   rU    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)rB  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   r~  rU  r  rQ   rJ   rJ   )rI   rK   rc    s
   rc  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 )GeneralCallNoderD  rs  rt  c          
   C   s`   | j |}| j|}| j|}y
|||S  tk
rZ } z| | W d d }~X Y nX d S )N)rD  r   rs  rt  rq  r   )rL   r   rD  rs  rt  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.)rt  rb  rs  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(  rD  r   r   ra   r*  r*   r   map_to_simple_call_noder   r  rA  r   rn   rt  rs  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   rb  r  )pos_argsrJ   rK   rt   ^  s   z(C function call is missing argument '%s'r"  c             S   s   g | ]\}}|qS rJ   rJ   )rS   rb  r  rJ   rJ   rK   rt     s    z/C function got unexpected keyword argument '%s')rD  r{   )#rk   rs  ru  rt  rb  rD  r   r   r  r   r   r{   r/  rw   r   rn   r   r   r#  r  r   addr  rb   r   r  r  rf  r  r\  r  rT  r   sortedrK  )rL   rD  r   r)  r\  Zdeclared_argsZmatched_argsZunmatched_argsZmatched_kwargs_countr{   seenZ
has_errorsr  rb   Zdecl_argr  rf  ZtempskeywordsZfirst_missing_keywordrn   r  Z
final_argsZ	new_tempsZfirst_temp_arg	arg_valuerp   rJ   )rg  rK   re    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 )Nr{  rJ  zObjectHandling.cz(%s = __Pyx_PyObject_Call(%s, %s, %s); %s)r   r*  rt  r   r  r  r   r  r   r   rD  rs  r   rn   r  )rL   r   r\  rJ   rJ   rK   r    s     

z$GeneralCallNode.generate_result_codeN)rN   rO   rP   r   r   r   r%   r   r   r   r   r   re  r  rJ   rJ   rJ   rK   rd    s   
	  rd  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   rq  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   rA  r   r   rZ  )rL   rc   rJ   rJ   rK   r     s
    zAsTupleNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU    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   rU  r%   r   r   r  r  rJ   rJ   rJ   rK   rm    s   rm  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 )MergedDictNodert  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_duplicatesrt  rb  r#  rl   	iteritemsr|   )rL   r   ro  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)	ro  rt  rb  r#  r   rp  r|   rq  r   )	rL   r   r   ro  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                s    fdd| j D | _ | S )Nc                s"   g | ]}|   d qS )z1argument after ** must be a mapping, not NoneType)r   rA  rZ  )rS   r  )rc   rJ   rK   rt     s   z0MergedDictNode.analyse_types.<locals>.<listcomp>)rt  )rL   rc   rJ   )rc   rK   r     s    
zMergedDictNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU     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  rt  r  r  r   r   r   r   rb  r   r   r  r   r  r
  r  r   r#  ro  r  rh  r   r  r   ri  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)rt  r  )rL   r   rs   rJ   rJ   rK   r  t  s    zMergedDictNode.annotateN)rN   rO   rP   r   r   r   r   ro  r   r   r   rr   r   rU  r  r  r  rJ   rJ   rJ   rK   rn    s   
Orn  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 )IrL  r$   rB  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 )Nparallelz%s.%s)rk   rB  r%  r  rC  rV  )rL   cyrJ   rJ   rK   rV    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   r:  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__)rC  
startswithendswithr   rB  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 )Nrw  z6Invalid attribute name '%s' in compile-time expression)
rC  rx  ry  r   rn   rB  r   r   rq  r   )rL   r   r  rB  r   rJ   rJ   rK   r     s    

z AttributeNode.compile_time_valuec             C   s   | j |S )N)rB  r   )rL   rc   rJ   rJ   rK   r     s    zAttributeNode.type_dependenciesc             C   s   | j |dd}|d k	r>|jjr6|jjjr6t|jjS |jjS | |}|d k	rX|jjS | j|}| j	||d |j
r| jjrtS | jr| jjrtS | jS )NF)target)obj_type)#analyse_as_cimported_attribute_noder   r   r   r   r  analyse_as_type_attributerB  rr   analyse_attributer  r   r/  )rL   rc   rp   r{  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$   )rz  z"Assignment to const attribute '%s'z%Assignment to non-lvalue of type '%s')r   r   r  r   rn   rC  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  r|  r}  "analyse_as_ordinary_attribute_noder   r   r#  re  wrap_obj_in_nonecheck)rL   rc   rz  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')rB  r   r  rC  r  r   r  r  as_name_noder  r   rn   )rL   rc   rz  module_scoper   rJ   rJ   rK   r|    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 )
NrP  z%s not a static member of %srL   r   z%s->%sr$   F)rz  z%s not a known value of %s)$rB  rd  r   r  r  r4  rI  r  rC  r/  r   r   r  Z
func_cnamer   r5  parent_scopeZis_cpp_class_scoper   rn   rb   r   r*   r  r{   r@  Zvtabptr_cnamer   r   r  rG  r  rF  rY  r   Zenum_values)rL   rc   r   r   Z
ubcm_entryr   r   rJ   rJ   rK   r}    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 )NrI  )rB  r   Zlookup_typerC  rd  r   r   rI  )rL   rc   r  r   rJ   rJ   rK   r   8  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)rB  r   r  rC  r  r   r  r  )rL   rc   r  r   rJ   rJ   rK   r   B  s    
z'AttributeNode.analyse_as_extension_typec             C   s0   | j |}|r,|| j}|r,|jr,|jS d S )N)rB  r   r  rC  r  )rL   rc   r  r   rJ   rJ   rK   r   M  s    
zAttributeNode.analyse_as_modulec             C   s8   t j| | j|d}|r"||}n
||}d|j_|S )N)rb   r   r$   )r%  r_  rC  r   r  r   r#  )rL   rc   r   rz  rp   rJ   rJ   rK   r  W  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)rB  r   r~  r   r/  r  
is_py_attrr   r   r   r   r  r   rn   )rL   rc   rz  rJ   rJ   rK   r  b  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__)'r  rC  r'  rB  r   r  r  rA  r   r  r   r   r  r  r  r  Zis_fake_referenceZhas_attributesZattributes_knownrI  r  r   is_memslice_transposer   r   Z	transposern   Zdeclare_attributer  r   r   r*   r   rb   r  r  r/  r   rv  )rL   rc   r{  immutable_objr   rJ   rJ   rK   r~  u  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')rB  r   Zmangle_class_private_namerC  r'  r   r  ra   r*  r  r  r  r   r  rN  can_coerce_to_pyobject	is_structrA  r   r  re  r   r  r   rn   )rL   rc   r{  r  rJ   rJ   rK   rv    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'r2  z.30r  z&Cannot transpose None memoryview slicez5Cannot access '%s' attribute of None memoryview slicer3  )rY  )r[   rB  r   r  needs_none_checkr  r8  rw   rC  r   r  rI  r  rb   rZ  )rL   rc   r=  rY  r   rJ   rJ   rK   r    s$    



z#AttributeNode.wrap_obj_in_nonecheckc             C   s   | j r|   d S )N)r  r   )rL   rc   rJ   rJ   rK   r     s    zAttributeNode.nogil_checkzAccessing Python attributec             C   s   | j |S )N)rB  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)rB  r   rT  r%  )rL   rJ   rJ   rK   rT    s    zAttributeNode.is_simplec             C   s   | j r
dS t| S d S )NT)rB  r%  r   )rL   rJ   rJ   rK   r     s    zAttributeNode.is_lvaluec             C   s   | j r| j  S t| S d S )N)rB  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)rB  r   r   r   r/  r  Zis_builtin_cmethodZfinal_func_cnameZ
from_fusedr   r'  Zvtabstruct_cnamer  Zvtabslot_cnamer   r6  rU  r  r  rQ  r   )rL   rB  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})r  is_special_lookupr  r  r   r  r   r   rB  r   r  rC  r   rn   r  r   r   r  r{  r   r   r   r  r   r  r   r/  rN  )rL   r   Zlookup_func_namer  r  r  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   r  r	  r   r   r   r
  )rL   r   rJ   rJ   rK   r
  ]  s    z$AttributeNode.generate_disposal_codec          	   C   s  | j | | jrh|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r4ddlm} |||| | j| | jjsZ|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;)!rB  r  r  r  r  r   r  r  rn   r   r  rC  r
  r  r   r6  r   r'  rU  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  g  sJ    









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)rB  r  r  r   rI  Zis_property_scopeentriesr  r  r   r  r  rn   r   r  rC  r   r
  r  )rL   r   r  rJ   rJ   rK   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  )r  r  rn   r/   r   rw   rC  )rL   r   r  r  rJ   rJ   rK   r    s    
zAttributeNode.annotate)r   )N)NF)FNN)F)-rN   rO   rP   re  r   r   r*   r   r   r  r  r  r  r  rV  r&  r   r   r   rr   r   r   r   r|  r}  r   r   r   r  r  r~  rv  r  r   r  r  rT  r   r   r   r  r
  r  r  r  rJ   rJ   rJ   rK   rL  y  sR   
5


C
%0
 
*
rL  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 )StarredUnpackingNoderz  r$   Fc             C   s   t j| ||d d S )N)rz  )r   r~  )rL   rn   rz  rJ   rJ   rK   r~    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   rz  analyse_declarations)rL   rc   rJ   rJ   rK   r    s    z)StarredUnpackingNode.analyse_declarationsc             C   s   | j |S )N)rz  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)r  r   rn   rz  r   r   )rL   rc   rJ   rJ   rK   r     s
    
z"StarredUnpackingNode.analyse_typesc             C   s   | j | d S )N)rz  r   )rL   rc   rJ   rJ   rK   r     s    z/StarredUnpackingNode.analyse_target_declarationc             C   s   | j || _ | j j| _| S )N)rz  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   rk  r   r   r   r  r~  r  rr   r   r   r   r   r  rJ   rJ   rJ   rK   r    s   r  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{   rk  r   rn   rz  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$   )r\  r{   r   rA  r   r   r/  r   )rL   rc   skip_childrenrb  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  rO  ru  )rL   r:  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)rk  )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{   rk  r   ru  rn   r   rz  MergedSequenceNoder   r   
binop_noderA  )rL   rc   r{   rY  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{   rk  rz  ru   r   r   r   )rL   r{   r  rJ   rJ   rK   r  7  s    z"SequenceNode._flatten_starred_argsc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU  @  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_itemsr\  r{   r   rk  r   r7  r   r   rU  r&  r   )rL   rc   rb  r  Zunpacked_itemZcoerced_unpacked_itemrJ   rJ   rK   r   C  s*    
z!SequenceNode.analyse_target_typesc             C   s   |  | d S )N)generate_operation_code)rL   r   rJ   rJ   rK   r  Z  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  rN  r\  r   r   r   rV  r;  r   r   r   r  r   ra   r  r   )rL   r   rz  plainZsize_factorZc_multr   rb  r  Zcreate_funcZset_item_func	arg_count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   r  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  rT  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_loopr<  r   )r  rS  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_noderb  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); %sr<  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   rU  r   r   rw   r{   r  r  raise_too_many_values_to_unpack raise_need_more_values_to_unpackr   rn   r\  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_checkrb  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   O  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
  r3  	new_labelr5  put_gotor\  r   r  r  	put_labelr   r   )rL   r   rx   r  r  	terminateiterator_tempZiternext_funcZunpacking_error_labelZunpack_coderb  rs   Zunpacking_done_labelrJ   rJ   rK   r  G  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$   Fr<  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;)r\  r{   rk  r  r   rS  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   rb  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    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  rU  r   r  r  r  r  r   r  r?  r   r@  r3  r  r  r  r  r  rQ   rJ   rJ   )rI   rK   r    s6   	

	
X 

X
EPr  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 )ru  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)rk  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   childrJ   rJ   rK   rV     s    )rw   r{   r   r   r\  rk  r  r   r   r\   r  rn   r   r  r  ru   r  rk   rl   r"   ra   rA  is_partly_literal)rL   rc   r  rb  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   )rA  )rS   r  )rc   rJ   rK   rt   +  s    z'TupleNode.coerce_to.<locals>.<listcomp>r$   )r{   r   r   T)r  )r   rN  r  r  r   r   r{   ru  rn   r   rA  r&  r   r  )rL   r:  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_list4  s    zTupleNode.as_listc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rT  :  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   B  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   J  s    z7TupleNode.calculate_constant_result.<locals>.<listcomp>)r   r{   rl   )rL   rJ   rJ   rK   r   H  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   rq  r   )rL   r   rY  r   rJ   rJ   rK   r   L  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  S  s$     


z!TupleNode.generate_operation_codeN)F)rN   rO   rP   r   r   r  r  rr   r   r   r&  r  rT  r   r   r   r   r  rJ   rJ   rJ   rK   ru    s   	
%
ru  c               @   st   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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{   rk  r  r  r   rA  )rL   rc   r  rp   rJ   rJ   rK   r     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_argsr  r   obj_conversion_errorsr  r
  r  )rL   rc   errorsrp   rJ   rJ   rK   r     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   r2  r   rn   r   r  r   r   Zc_void_typerw   r{   r   rk   rl   r"   rz  r;  r  r1  r  r&  r4  TypecastNoder   r  rI  r!  r   r\  r  rN  r  r*   )	rL   r:  rc   rd   r  r   rb  r  r'  rJ   rJ   rK   r&    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   )ru  rn   r{   r   rk   rl   r   r   )rL   rT   rJ   rJ   rK   as_tuple  s    zListNode.as_tuplec             C   sP   | j jr@| jr(|jj| j dddd| _qL|jj| j ddd| _nt| | d S )NFT)r   Zstaticreusable)r   r  )r   r   r
  r   r   r   r  r   )rL   r   rJ   rJ   rK   r     s    zListNode.allocate_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 )Nr<  zPy_ssize_t %s;z&for ({i} = 0; {i} < {count}; {i}++) {{)rb  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   rV  r8  r   rw   r{   r\  r  r  r   r  r  r  rI  r!  r   r   )rL   r   rd   r  rb  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  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   r  %   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_expressions0   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   ri  r  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  labelZ	old_label)rI   rJ   rK   r  4   s<    
$







z'ScopedExprNode.generate_evaluation_codec             C   s<   x6|D ].}|j r*|| ||jd q|| qW d S )Nrs  )r  Zput_var_gotrefr  r   Zput_var_xdecref_clear)rL   r   r  r   rJ   rJ   rK   r  `   s
    

z%ScopedExprNode._generate_vars_cleanup)N)rN   rO   rP   r   r  r  r  r  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   q   s    zComprehensionNode.infer_typec             C   s   | | j _| | d S )N)r   rz  r  )rL   rc   rJ   rJ   rK   r  t   s    z&ComprehensionNode.analyse_declarationsc             C   s   | j | d S )N)r  r  )rL   rc   rJ   rJ   rK   r  x   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     s    z,ComprehensionNode.analyse_scoped_expressionsc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU     s    zComprehensionNode.may_be_nonec             C   s   |  | d S )N)r  )rL   r   rJ   rJ   rK   r     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     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   rn  r   r   rl   rr   r  r  r   r  rU  r  r  r  rJ   rJ   rJ   rK   r  i   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   rA  )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))rz  r   r   r  r  r   r  r   r   r  r  r   r   r   rn   r
  r  )rL   r   rD  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   rn  rz  r   rn  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   rA  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   rz  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   rn  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)rz  r   r  )rZ  gbodyZ
is_inlinedr   r   r   r   Zinlined_comprehension_typer}  rV  r   retval_cnamerF   r  r~  )rL   rn   r  Zcomprehension_typer\  r  )rI   rJ   rK   r~     s    z'InlinedGeneratorExpressionNode.__init__c             C   s
   | j dkS )N)r\   r  ri  )	orig_func)rL   rJ   rJ   rK   rU  !  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  rz  r   r   r   r~  rU  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   sd   |t tfkrN|rN|d jrN|d jt k	rNt|d j|d jd|d jd|d< tj	| |||d d S )Nr   T)r{   r   r   )r{   r   )
r   r   ru   r   r  rn   r{   r   r   r~  )rL   rn   r{   r   rJ   rJ   rK   r~  $!  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   4!  s    z?MergedSequenceNode.calculate_constant_result.<locals>.<genexpr>)r{   ru   r   rl   rc  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   H!  s    z8MergedSequenceNode.compile_time_value.<locals>.<genexpr>)r{   ru   r   r   rc  r   r   r   r   rq  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   W!  s    z$MergedSequenceNode.type_dependenciesc             C   s   | j S )N)r   )rL   rc   rJ   rJ   rK   rr   Z!  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   rA  rZ  )rS   r  )rc   rJ   rK   rt   _!  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   rU  n!  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.cr<  z"PyObject *%s = PyList_AsTuple(%s);z%s = %s; %sr   )"r  rn   r   r   r   r  r{   r  r  rc  ru   r   r   r   r   r  r   r  r
  r  r   r   rh  r  r   r   rV  r  r   ri  r  r  r   r  )
rL   r   is_setr{   rs   rr  Zadd_funcZextend_funcr  rs  rJ   rJ   rK   r  q!  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   rm  r   r   r  r~  r   r   r   rr   r   rU  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   rA  r   r   r   )rL   rc   rb  r  rJ   rJ   rK   r   !  s    

zSetNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU  !  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   rq  r   )rL   r   rY  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   rm  r   r   r   rc  r  r   rU  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   krZ  )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  rq  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   rU  #"  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  rA  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   r2  r   rn   r$  r#  r  rw   rI  r!  r   rk   r  r1  r  rd  r  rg   r   r  r&  r*   )rL   r:  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  M"  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;rq  zFunctionArguments.c)r  rn   r   r   ra   r  r   r   rw   r#  r   r  r   r   r  exclude_null_valuesr   r  ro  rd  rh  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  T"  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   rb  ro  r  ro  r  r   r   r   rr   r   rU  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   rA  )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   r~  rn   r   r   r   )rL   r  rJ   rJ   rK   r~  "  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   rZ  )rL   rc   r  rJ   rJ   rK   r   "  s    z SortedDictKeysNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU  "  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	   rM  r  r  )rL   r   Zdict_resultZ
keys_cnamerJ   rJ   rK   r  "  s(    




z'SortedDictKeysNode.generate_result_codeN)	rN   rO   rP   r   r   r~  r   rU  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gZeZdZdd Zdd Zdd Z	d	Z
d
d ZdS )	ClassNodedocTc             C   s   t S )N)r   )rL   rc   rJ   rJ   rK   rr   #  s    zClassNode.infer_typec             C   s8   | j r"| j || _ | j || _ |tdd | S )NZCreateClasszObjectHandling.c)r  r   rA  r  r   r  )rL   rc   rJ   rJ   rK   r   #  s
    zClassNode.analyse_typesc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rU  #  s    zClassNode.may_be_nonezConstructing Python classc             C   s   | j }|| j}| jrH|| jd|j |t	d| j f  | 
|}| |}|d|  |j |j |||||  | jf  ||   d S )NzPyDict_SetItem(%s, %s, %s)rm  z.%s = __Pyx_CreateClass(%s, %s, %s, %s, %s); %s)class_def_noder  rb   r  r  rn   r  r   r	   rM  r  r  r   r   basesr   r  )rL   r   r  r   py_mod_namer  rJ   rJ   rK   r  #  s*    


zClassNode.generate_result_codeN)rN   rO   rP   r   r   r   r   rr   r   rU  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   E#  s    zPy3ClassNode.infer_typec             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   I#  s    zPy3ClassNode.analyse_typesc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rU  L#  s    zPy3ClassNode.may_be_nonezConstructing Python classc             C   s   |j tdd || j}| j}|jr6|j nd}|j	rL|j	 }nd}|
d|  |||j |j || j| j||  | jf	  ||   d S )NZPy3ClassCreatezObjectHandling.cr{  z$((PyObject*)&__Pyx_DefaultClassType)z9%s = __Pyx_Py3ClassCreate(%s, %s, %s, %s, %s, %d, %d); %s)r  r  r   r  r  rb   r  mkwr   	metaclassr   r   r  r  Zcalculate_metaclassZallow_py2_metaclassr   rn   r  )rL   r   r   r  r  r  rJ   rJ   rK   r  Q#  s&    z!Py3ClassNode.generate_result_codeN)rN   rO   rP   r   r   r   r   rr   r   rU  r  r  rJ   rJ   rJ   rK   r  6#  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   o#  s    z"PyClassMetaclassNode.analyse_typesc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rU  t#  s    z PyClassMetaclassNode.may_be_nonec          	   C   s   | j j}| j j}|r>|jtdd d| | f }n |jtdd d|  }|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   r   rn   r  r   )rL   r   r  r  callrJ   rJ   rK   r  w#  s"    
z)PyClassMetaclassNode.generate_result_codeN)rN   rO   rP   r   r   rU  r  rJ   rJ   rJ   rK   r  h#  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   s*   | j r| j ||| _ t| _d| _| S )Nr$   )r  r   rA  r   r   r   )rL   rc   rJ   rJ   rK   r   #  s
    z"PyClassNamespaceNode.analyse_typesc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rU  #  s    z PyClassNamespaceNode.may_be_nonec       
      C   s   | | j}| |}| |}| j}d}| jr:| j n|}|jrN|j n|}|j	rb|j	 n|}	|
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  r   rn   r  )
rL   r   r   r  r  r  nullZdoc_coder  r  rJ   rJ   rK   r  #  s(    

z)PyClassNamespaceNode.generate_result_codeN)rN   rO   rP   r   r   rU  r  rJ   rJ   rJ   rK   r  #  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   s@   | j s
t|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_result_codec             C   s<   | j s
t|jtdd || jd|  |f  d S )NZCyFunctionClassCellzCythonFunction.cz&__Pyx_CyFunction_InitClassCell(%s, %s))	r  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)rZ  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_memoryviewstructr$   )r   z%s->%sc             S   s   g | ]
}|j qS rJ   )rX   )rS   r  rJ   rJ   rK   rt   b$  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   f$  s   )r#  )r*  r   __defaults__)r{   r[  r\  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   $  s   );is_c_class_scoperZ  Z
is_wrapperr  r{   rX   r   Z
is_dynamicr   ra   r   DefaultLiteralArgNodern   kw_onlyr`  analyse_annotationrb   r[  r\  Zreturn_type_annotationr	   rM  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  ru  r   rA  r  r$  r  DefaultsTupleNoderr  DefaultsKwDictNoder   ZDefNodeZReturnStatNoder   r  r   r  local_scopepy_wrapper_requiredpymethdef_requireddefaults_getterr  )rL   rc   Znonliteral_objectsZnonliteral_otherZdefault_argsZdefault_kwargsannotationsZmust_use_constantsr  r`  r  r   rI  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	   rM  r   r   r   ra   rA  )rL   rc   r`  r  rJ   rJ   rK   r  $  s    


z"PyCFunctionNode.analyse_annotationc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU  $  s    zPyCFunctionNode.may_be_nonezConstructing Python functionc             C   s   | j d krd}n
| j  }|S )Nr{  )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_NewZCythonFunctionZ__Pyx_CyFunction_Newr{  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)rz  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  rZ  is_specializationr  r  r   r  r   r   Zis_staticmethodr   Zis_classmethodr  r  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  r_  r  )
rL   r   rZ  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  rZ  r  r  r  r  r  r  r   r   r   r  r"  ro  r  r   r	  r  rU  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)r{  )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)rZ  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   0%  s   r   r$   )r{   r   r   )	r   r~  rn   r   r{   r  r!  ru  r'  )rL   rZ  r{   
local_varsrJ   rJ   rK   r~  )%  s    

zCodeObjectNode.__init__c             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU  5%  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   8%  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_NEWLOCALS
CO_VARARGS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   rZ  r  rb   r	   r  Zget_filenametable_entryr  r[  r   r\  r   rw   r{   r]  r'  r  r   Zempty_bytesr  r   r   )rL   r   func	func_name	file_pathZfile_path_constr#  rJ   rJ   rK   r  =%  sB    



z#CodeObjectNode.generate_result_code)N)
rN   rO   rP   r   r   r  r~  rU  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  r~  r  r   	evaluated)rL   rn   r  )rI   rJ   rK   r~  q%  s    
zDefaultLiteralArgNode.__init__c             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   w%  s    z#DefaultLiteralArgNode.analyse_typesc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  z%  s    z*DefaultLiteralArgNode.generate_result_codec             C   s   | j s| j| d| _ d S )NT)r1  r  r  )rL   r   rJ   rJ   rK   r  }%  s    z.DefaultLiteralArgNode.generate_evaluation_codec             C   s   | j | j S )N)r   rQ  r  r   )rL   rJ   rJ   rK   r   %  s    zDefaultLiteralArgNode.result)rN   rO   rP   r   r   r   r~  r   r  r  r   rQ   rJ   rJ   )rI   rK   r  h%  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   r2  r~  r  r  )rL   rn   r  r  )rI   rJ   rK   r~  %  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  rJ  r  r   )rL   rJ   rJ   rK   r   %  s    
zDefaultNonLiteralArgNode.result)	rN   rO   rP   r   r~  r   r  r   rQ   rJ   rJ   )rI   rK   r2  %  s
   r2  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   r2  r   rF   r  r~  )rL   rn   r  r  r{   r  )rI   rJ   rK   r~  %  s    
zDefaultsTupleNode.__init__Fc                s   t t| |||S )N)rF   r  r   rA  )rL   rc   r  )rI   rJ   rK   r   %  s    zDefaultsTupleNode.analyse_types)F)rN   rO   rP   r~  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   r2  r   r"  rF   r  r~  )rL   rn   r  r  r&  r  rb   )rI   rJ   rK   r~  %  s    
zDefaultsKwDictNode.__init__)rN   rO   rP   r~  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 )	
LambdaNoderZ  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_namerZ  Zno_assignment_synthesisr  r  is_cyfunctionr   r  Zadd_lambda_def)rL   rc   rJ   rJ   rK   r  %  s    zLambdaNode.analyse_declarationsc                s   | j || _ tt| |S )N)rZ  r   rF   r3  r   )rL   rc   )rI   rJ   rK   r   %  s    zLambdaNode.analyse_typesc                s    | j | tt| | d S )N)rZ  r  rF   r3  r  )rL   r   )rI   rJ   rK   r  %  s    zLambdaNode.generate_result_code)rN   rO   rP   rn  r	   rM  rb   r  r   r  rQ   rJ   rJ   )rI   rK   r3  %  s
   

	r3  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 )Nr7  F)r  Zgenexpr_namerF   r6  r  rZ  r  r  r5  r,   Zpyfunction_noargsr   r<  )rL   rc   )rI   rJ   rK   r  %  s    z,GeneratorExpressionNode.analyse_declarationsc          
   C   sD   | d|  | jjj|  ||  | jf  || 	  d S )Nz%s = %s(%s); %s)
r   r   rZ  r   r_  r  r   rn   r  r   )rL   r   rJ   rJ   rK   r  %  s    z,GeneratorExpressionNode.generate_result_code)
rN   rO   rP   r	   rM  rb   r  r  r  rQ   rJ   rJ   )rI   rK   r6  %  s   

r6  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  rA  )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  awaitr9  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   r  r   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   r5  r   r   r   )rL   r   r:  Z
label_nameZsavedr   r   r   Z
save_cnamerA  rB  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   rG  _&  s    z/YieldExprNode.generate_sent_value_handling_codeN)rN   rO   rP   r   r   r   r:  r;  rF  r<  r=  r   r>  r  r?  rG  rJ   rJ   rJ   rK   r8  %  s   
8r8  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_funcd&  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  rK  r  r   r
  r  r  r   r  r?  r   fetch_iteration_resulthandle_iteration_exception)rL   r   rK  rL  rJ   rJ   rK   r  g&  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   rL  &  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   rM  &  s
    


z3_YieldDelegationExprNode.handle_iteration_exception)NF)rN   rO   rP   rK  r  rL  rM  rJ   rJ   rJ   rK   rI  c&  s   
rI  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   rA  )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   rK  &  s    z!YieldFromExprNode.yield_from_funcN)rN   rO   rP   r;  r=  r>  rK  rJ   rJ   rJ   rK   rN  &  s   rN  c               @   s$   e Zd ZdZdZdd Zdd ZdS )AwaitExprNodeTrC  c             C   s   | j d k	r| j || _ d S )N)r  rA  )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   rK  &  s    zAwaitExprNode.yield_from_funcN)rN   rO   rP   rF  r=  r>  rK  rJ   rJ   rJ   rK   rO  &  s   rO  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   rR  rF   rP  rL  )rL   r   )rI   rJ   rK   rL  &  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   )rS  r   r   rR  r   rn   )rL   r   rH  rJ   rJ   rK   rG  &  s
    
z7AwaitIterNextExprNode.generate_sent_value_handling_code)rN   rO   rP   rR  rL  rG  rQ   rJ   rJ   )rI   rK   rP  &  s   
rP  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   rU  &  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  rU  r  rJ   rJ   rJ   rK   rT  &  s   rT  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   rA  r   r  )rL   rc   rJ   rJ   rK   r   &  s    z LocalsDictItemNode.analyse_typesN)rN   rO   rP   r   rJ   rJ   rJ   rK   rU  &  s   rU  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   )rU  r  r%  )rS   rO  )rn   rJ   rK   rt   &  s   T)r#  r  )ri  r  rY  r$  r~  )rL   rn   rc   r(  r&  rJ   )rn   rK   r~  &  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   rb  rJ   rJ   rK   rt   &  s    z4FuncLocalsExprNode.analyse_types.<locals>.<listcomp>)rF   rV  r   r#  )rL   rc   rp   )rI   rJ   rK   r   &  s    z FuncLocalsExprNode.analyse_types)rN   rO   rP   r~  r   rQ   rJ   rJ   )rI   rK   rV  &  s   
rV  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)rp  r~  pyclass_dict)rL   rn   rX  rJ   rJ   rK   r~  '  s    zPyClassLocalsExprNode.__init__c             C   s   | j j| _d| _| S )NF)rX  r   r   )rL   rc   rJ   rJ   rK   r   '  s    
z#PyClassLocalsExprNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU  '  s    z!PyClassLocalsExprNode.may_be_nonec             C   s
   | j  S )N)rX  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   r~  r   rU  r   r  rJ   rJ   rJ   rK   rW  '  s
   rW  c             C   s*   |j rt| S |jr t| |jS t| |S )N)r  rT  r  rW  r  rV  )rn   Z
scope_noderc   rJ   rJ   rK   LocalsExprNode'  s
    rY  )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   9'  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)	r]  rZ   r
   r   rn   r,  r   rq  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   r4  r  find_cpp_operation_typer
   infer_unop_type)rL   rc   operand_typecpp_typerJ   rJ   rK   rr   I'  s    zUnopNode.infer_typec             C   s   |j r
tS |S d S )N)ra   r   )rL   rc   r`  rJ   rJ   rK   r_  Q'  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   rU  )rL   rJ   rJ   rK   rU  W'  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   ]'  s    


zUnopNode.analyse_typesc             C   s
   | j  S )N)r,  r   )rL   rJ   rJ   rK   r   l'  s    zUnopNode.check_constc             C   s   | j jjp| j jjS )N)r,  r   ra   rN  )rL   rJ   rJ   rK   rc  o'  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_pythranop_typerJ   rJ   rK   rb  r'  s    zUnopNode.is_pythran_operationc             C   s   |   r|   d S )N)rc  r   )rL   rc   rJ   rJ   rK   r   w'  s    zUnopNode.nogil_checkc             C   s   | j j}|jS )N)r,  r   r4  )rL   r   rJ   rJ   rK   re  {'  s    zUnopNode.is_cpp_operationc             C   s   | j || _ d S )N)r,  rA  )rL   rc   rJ   rJ   rK   rd  '  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   re  r  r   rn   r   r   )rL   r   rJ   rJ   rK   r  '  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   rD  rJ   rJ   rK   ri  '  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)r  r
   r,  rk  r   r  r   r   r  r   r  r^  r   rn   )rL   rc   overload_checkr   ra  rJ   rJ   rK   rf  '  s(    



zUnopNode.analyse_cpp_operationN)T)rN   rO   rP   r   infixr   r   rr   r_  rU  r   r   rc  rb  r   re  rd  r  ri  rk  rf  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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   rq  r   )rL   r   r,  r   rJ   rJ   rK   r   '  s
    zNotNode.compile_time_valuec             C   s   t jS )N)r   ry  )rL   rc   r`  rJ   rJ   rK   r_  '  s    zNotNode.infer_unop_typec             C   s:   | j || _ | j j}|jr(| | n| j || _ | S )N)r,  r   r   r4  rf  r   )rL   rc   r`  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   ry  r   r   r   r_  r   r   rJ   rJ   rJ   rK   rn  '  s   	rn  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   rn  )rL   rc   rJ   rJ   rK   rg  '  s    z!UnaryPlusNode.analyse_c_operationc             C   s   dS )NZPyNumber_PositiverJ   )rL   r   rJ   rJ   rK   rj  '  s    z#UnaryPlusNode.py_operation_functionc             C   s$   |   rd| j  S | j S d S )Nz(+%s))re  r,  r   )rL   rJ   rJ   rK   r   '  s    z#UnaryPlusNode.calculate_result_codeN)rN   rO   rP   r
   rg  rj  r   rJ   rJ   rJ   rK   rp  '  s   rp  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  rn  rF  rk  r6  rm  )rL   rc   rJ   rJ   rK   rg  '  s    


z"UnaryMinusNode.analyse_c_operationc             C   s   dS )NZPyNumber_NegativerJ   )rL   r   rJ   rJ   rK   rj  (  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  )rm  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
   rg  rj  r   r   rJ   rJ   rJ   rK   rq  '  s
   rq  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  rn  rF  rk  )rL   rc   rJ   rJ   rK   rg  (  s    


zTildeNode.analyse_c_operationc             C   s   dS )NZPyNumber_InvertrJ   )rL   r   rJ   rJ   rK   rj  (  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   rg  rj  r   rJ   rJ   rJ   rK   rs  (  s   	rs  c               @   s   e Zd Zdd ZdS )	CUnopNodec             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rc  &(  s    zCUnopNode.is_py_operationN)rN   rO   rP   rc  rJ   rJ   rJ   rK   rt  $(  s   rt  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   r`  rJ   rJ   rK   r_  .(  s    zDereferenceNode.infer_unop_typec             C   s$   | j jjr| j jj| _n|   d S )N)r,  r   r  r   rk  )rL   rc   rJ   rJ   rK   rg  4(  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
   r_  rg  r   rJ   rJ   rJ   rK   ru  )(  s   ru  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  rn  r  rk  )rL   rc   rJ   rJ   rK   rg  A(  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   J(  s    z,DecrementIncrementNode.calculate_result_codeN)rN   rO   rP   rg  r   rJ   rJ   rJ   rK   rv  >(  s   	rv  c                s    fddS )Nc                s   t | f d|S )N)rw  r
   )rv  )rn   r  )rw  r
   rJ   rK   <lambda>Q(      z%inc_dec_constructor.<locals>.<lambda>rJ   )rw  r
   rJ   )rw  r
   rK   inc_dec_constructorP(  s    rz  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   r`  rJ   rJ   rK   r_  Z(  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)rl  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   r4  rf  r   r  r   r   r   ra   r  rb   re  rC  r   r.  )rL   rc   ZargtyperJ   rJ   rK   r   ](  s$    zAmpersandNode.analyse_typesc             C   s
   | j  S )N)r,  r   )rL   rJ   rJ   rK   r   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   u(  s    zAmpersandNode.errorc             C   s   d| j   S )Nz(&%s))r,  r   )rL   rJ   rJ   rK   r   z(  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   r4  r  r   rn   r   r
   ra   r   r   )rL   r   rJ   rJ   rK   r  }(  s
    z"AmpersandNode.generate_result_codeN)
rN   rO   rP   r
   r_  r   r   r   r   r  rJ   rJ   rJ   rK   r{  T(  s   r{  )r  r  r[  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  r  z+-zAPython has no increment/decrement operator: %s%sx == %s(%sx) == x      )r
   r,  )rk   r   rn   rg   r   r  r   r  r  r\  r
   r   unop_node_classes)rn   r
   r,  rJ   rJ   rK   	unop_node(  s    $r  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   rv  
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 || _| j jr| j jjr| j jj&r| jj }|jr|j}|jr|j&st| j	dd	 | 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)notnonezQCasting a GIL-requiring function into a nogil function circumvents GIL validationr$   )'r   r   rv  r  r,  rz   r   r   r   rn   r   r*   r   ry  r   ra   r   r  r4  r   r/  r0  r  r   r   r&  r  r   r  r   r   create_from_py_utility_codeZ	typecheckr3  rk   r  r6  r  r   )rL   rc   r   r  Zto_pyZfrom_pyrh  rJ   rJ   rK   r   (  sl    



"zTypecastNode.analyse_typesc             C   s
   | j  S )N)r,  rT  )rL   rJ   rJ   rK   rT  (  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   r6  	real_typerQ  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   rQ  )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   r  r   r   rr   r   rT  r   r   r   r   r   r   r   r   r  rJ   rJ   rJ   rK   r  (  s   @
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 literalfortran)r  follow)r  Zcontigr"  )3r  r  r,  r   array_dtypebase_type_noderv  r{  r*   r   r  rw   r  r   r   rn   ERR_NOT_POINTERr   r  r   r"  r   ERR_BASE_TYPEr\  r   rt  	ERR_STARTr   r   rg   r   rn  ERR_NOT_STOPr&  
shape_typer   rS  r   r/  r*  r   	ERR_STEPSmoder  rx  r$  Zvalidate_memslice_dtypeget_cython_array_typeZuse_cython_array_utility_coder  Ztypeinfo_to_format_code)rL   rc   r  r  r{  r  r   Zarray_dimension_sizesZaxis_noZaxisZdimsizeshapeZfirst_or_lastrJ   rJ   rK   r   Q)  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)r  )rL   rc   rJ   rJ   rK   rr   )  s    zCythonArrayNode.infer_typec             C   s"   |  jj}|  |jdjS )Narray)r  r#  cython_scopeZload_cythonscopeZ	viewscoperJ  r   )rL   rc   r  rJ   rJ   rK   r  )  s    z%CythonArrayNode.get_cython_array_typec          
      sv  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
|| |jf   | dt| }	 d||	d| |jf   |  |||jj
 f}
 d|
     j     fdd}|| || d S )Nr$   )r  c                s   g | ]} j | qS rJ   )r  rQ  r   )rS   r  )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); %sz __PYX_BUILD_PY_SSIZE_T z.%s = Py_BuildValue((char*) "(" %s ")", %s); %sz, 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   )r  )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   r   r  rw   r  r  )rL   r   r  r  r.  Zshapes_tempZformat_tempitemsize	type_infoZbuildvalue_fmtr>  r  rJ   )r   rL   rK   r  )  sD    






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   r  r"  )r{  r  )r  r,  r  )rn   r   rr  r   r   rr  r   r   r   r   ZMemoryViewSliceTypeNoder(  r   )	rH   Zsrc_noderc   rn   r   r  r{  Zmemslicenoder   rJ   rJ   rK   r)  )  s    



zCythonArrayNode.from_carray)rN   rO   rP   rm  r   r  r   r  r  r   r   r  r   r   rr   r  r  ro  r)  rJ   rJ   rJ   rK   r(  3)  s   h3r(  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   r  *  s   r  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   rB  rC  )r,  )r   module_pathrJ  r  r%  rn   rL  rb   SizeofVarNoder   r  rv  r  
check_type)
rL   rc   r   rB  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   r  ;*  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   rJ  rJ   rJ   rK   r   F*  s    
z$SizeofTypeNode.calculate_result_code)rN   rO   rP   r   r  r   r  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 )	r  r,  c             C   sP   | j |}|r>|| _| jjr.| j|j| _t| _|   n| j 	|| _ | S )N)
r,  r   r  r  r  r  r  rI   r  r   )rL   rc   Zoperand_as_typerJ   rJ   rK   r   W*  s    
zSizeofVarNode.analyse_typesc             C   s   d| j   S )Nz(sizeof(%s)))r,  r   )rL   rJ   rJ   rK   r   e*  s    z#SizeofVarNode.calculate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  h*  s    z"SizeofVarNode.generate_result_codeN)rN   rO   rP   r   r   r   r  rJ   rJ   rJ   rK   r  P*  s   r  c               @   sX   e Zd ZejZdgZdZdZdZ	dd Z
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.typeinfor  )	r  r  find_modulern   rJ  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_typeztypeid operatorc             C   s   |  | | |}|s&| d | S || _| j|}|rJ|| _d| _nf| j|| _d| _| jjj	rv| 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)r  r  r   r   r,  r   r  r  r   ra   r   r  r  r   r  )rL   rc   r  Zas_typerJ   rJ   rK   r   *  s.    






zTypeidNode.analyse_typesc             C   s   t | j| tj| _d| _d S )Nz<error>)r   rn   r   r*   r   r  )rL   r~  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   rJ  rJ   rJ   rK   r  *  s    
zTypeidNode.generate_result_code)rN   rO   rP   r   r*   r   r   r  r  r   r  Zcpp_messager   r   r   r   r  rJ   rJ   rJ   rK   r  l*  s   r  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	   rM  rg   r   r  rn   rA  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   rU  *  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   rU  r  r   rJ   rJ   rJ   rK   r  *  s   r  c             C   s0   y
| j }W n tk
r$   |j}Y nX || |S )N)
__matmul__ry   __rmatmul__)rf  br.  rJ   rJ   rK   matmul_operator*  s
    
r  c             C   s   | |kS )NrJ   )xseqrJ   rJ   rK   rx  *  ry  rx  c             C   s   | |kS )NrJ   )r  r  rJ   rJ   rK   rx  *  ry  )<z<=z==z!=z>=>r  is_notr  r|  /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  rq  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$   )rb  r  r  r   r  r5   r   r   rc  coerce_operands_to_pyobjectsra   re  rf  rg  )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   rc  @+  s    zBinopNode.is_py_operationc             C   s   |j p|j p|jp|jS )N)ra   rN  )rL   type1type2rJ   rJ   rK   r  C+  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   rb  F+  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  I+  s    z$BinopNode.is_pythran_operation_typesc             C   s   | j jjp| jjjS )N)r  r   r4  r  )rL   rJ   rJ   rK   re  O+  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   )r  r
   r  r  rk  r   r  r   r   r  r   r  r  r   rw   r{   r&  r*  )rL   rc   r   r  rJ   rJ   rK   rf  S+  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  j+  s*    zBinopNode.result_typec             C   s   d S )NrJ   )rL   r  r  rJ   rJ   rK   r  +  s    z'BinopNode.infer_builtin_types_operationc             C   s   |   r|   d S )N)rc  r   )rL   rc   rJ   rJ   rK   r   +  s    zBinopNode.nogil_checkc             C   s    | j || _ | j|| _d S )N)r  rA  r  )rL   rc   rJ   rJ   rK   r  +  s    z&BinopNode.coerce_operands_to_pyobjectsc             C   s   | j  o| j S )N)r  r   r  )rL   rJ   rJ   rK   r   +  s    zBinopNode.check_constc                s"   t t|  p | j p | j S )N)rF   r  r   r  r  )rL   )rI   rJ   rK   r   +  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   rj  r   r   rn   r  r   re  r  r   r   r   r   )rL   r   rD  
extra_argsrJ   rJ   rK   r  +  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   rk  +  s    


zBinopNode.type_error)rN   rO   rP   r   r  r   r   rr   r   r  rc  r  rb  r  re  rf  r  r  r   r  r   r   r  rk  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   rc  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   rj  +  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)r4  r  r^  r
   )rL   r  r  ra  rJ   rJ   rK   r  +  s    z CBinopNode.compute_c_result_typeN)rN   rO   rP   r   rj  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   r  )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 )NFr4  )r  r   Tzoverflowcheck.fold)Z	const_rhs)r  r   r  r  rk  r6  rm  r/  r[   r
   overflow_op_namesrz   overflow_checkZoverflow_foldZoverflow_check_binopr.  r   r  r&  )rL   rc   r  r  rJ   rJ   rK   rg  +  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  ry  r
   rn  )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  rU  )rL   r  r  )rI   rJ   rK   rU  ,  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  rF  )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   rn  overflow_bitr   rF   r  r  r   rn   r   )rL   r   )rI   rJ   rK   r  3,  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   r4  rm  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  V,  s    z"NumBinopNode.is_py_operation_typesc             C   s"   | j | j }| jr|dd}|S )NZ	PyNumber_ZPyNumber_InPlace)py_functionsr
   r  rD  )rL   r   rY  rJ   rJ   rK   rj  [,  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  r|  z<<z>>r  r  r   r  r  z//r  z**rh  r  mullshift)r  r  r   z<<)rN   rO   rP   rm  r  r  rg  r  rU  r   r  r  r   r  rj  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/  rF  )rL   r  r  rJ   rJ   rK   r  |,  s    zIntBinopNode.c_types_okayN)rN   rO   rP   r  rJ   rJ   rJ   rK   r  y,  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  ,  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   r^  r   )rL   r  r  string_typesrJ   rJ   rK   r  ,  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/  rF  r  r  )rL   r  r  rJ   rJ   rK   r  ,  s    zAddNode.compute_c_result_typec                s   | j j| jj }}|tks"|tkr|ttfkr@|ttfkr@d}n"t| j tsXt| jtr^d}nd}|r| j  sz| j r~dS dS tt	| 
|S )NTFZ__Pyx_PyUnicode_ConcatSafeZ__Pyx_PyUnicode_Concat)r  r   r  r   r   rk   r`  rU  rF   r  rj  )rL   r   r  r  Zis_unicode_concat)rI   rJ   rK   rj  ,  s    zAddNode.py_operation_function)rN   rO   rP   r  r  r  rj  rQ   rJ   rJ   )rI   rK   r  ,  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/  rF  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  rq  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  re  r  r   ra   r[   r  rz   rl   zerodivision_checkr  r   )rL   rc   rJ   rJ   rK   r  -  s    
zDivNode.analyse_operationc             C   sT   | j dkrF| jrF|jsF|jsF|jsF|jsFt|tj}t||}|S t| ||S )Nr  )	r
   r  r4  rG  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   r6  r  r  r[   rG  r  rF  r  r  r   r  r  r  r  generate_div_warning_code)rL   r   rJ   rJ   rK   r  3-  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)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  rm  rr  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_infor5  r  r  )rL   r   Zin_nogilZ	zero_testZminus1_checkZtype_of_op2Zwarning_coder  rJ   rJ   rK   r  A-  sj    

 


z!DivNode.generate_div_warning_codec             C   s   | j js|  rt| S | j jrD| jdkrDd| j | j	 f S | j
sP| 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   r6  re  r  r   rG  r
   r  r   r  r  r  rQ  specialization_name)rL   r  r  rJ   rJ   rK   r   -  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  rU  r   r   r-   r  rd  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/  rG  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   rG  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   rU  r  r2  r   rk   r1  r   rF   r  rj  )rL   r   r  r  )rI   rJ   rK   rj  -  s    

zModNode.py_operation_function)rN   rO   rP   r  r  r  r  r  r   rj  rQ   rJ   rJ   )rI   rK   r  -  s   r  c                   s8   e Zd Z fddZdd Zdd Z fddZ  ZS )	PowNodec                s&   |j d st| jd tt| |S )NZcpowzKThe 'cpow' directive is provided for forward compatibility and must be True)r[   r   rn   rF   r  r   )rL   rc   )rI   rJ   rK   r   .  s    
zPowNode.analyse_typesc             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  rg  r   r6  r  rG  r  r&  r  r  pow_funcr   rn   r  r/  r  rD  r  r   r  r  r  r*  )rL   rc   rJ   rJ   rK   rg  .  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   rQ  )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  rj  )rL   r   )rI   rJ   rK   rj  1.  s    zPowNode.py_operation_function)rN   rO   rP   r   rg  r   rj  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   O.  s    zBoolBinopNode.infer_typec             C   s,   | j dkr| j S | j p&| j S d S )Nor)r
   r  rU  r  )rL   rJ   rJ   rK   rU  T.  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   Z.  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   b.  s
    
z BoolBinopNode.compile_time_valuec             C   s   | j  p| j S )N)r  r   r  )rL   rJ   rJ   rK   r   j.  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   m.  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  y.  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_operands~.  s    zBoolBinopNode.wrap_operandsc             C   s   |  tj|S )N)r&  r   ry  )rL   rc   rJ   rJ   rK   r   .  s    zBoolBinopNode.coerce_to_booleanc             C   s2   | j ||}| j||}tj| || j||dS )N)r   r
   r  r  )r  r&  r  r  r_  r
   )rL   r:  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  .  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  .  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   ry  r   r  r   r6  rn   r   )rL   r   test_resultrJ   rJ   rK   generate_operand1_test.  s    
z$BoolBinopNode.generate_operand1_test)rN   rO   rP   rm  r   r   r
   r  r  rr   rU  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   r~  rn   r  r&  )rL   r  r  rc   )rI   rJ   rK   r~  .  s
    

zBoolBinopResultNode.__init__c             C   s   |  tj|S )N)r&  r   ry  )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   ry  r   r   )rL   r:  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   ry  r   r   r6  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 )Nro  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   rm  r   r   r  r   r~  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   A/  s    zCondExprNode.type_dependenciesc             C   s   t | j|| j|S )N)r   r  r  rr   r  )rL   rc   rJ   rJ   rK   rr   D/  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   I/  s    z&CondExprNode.calculate_constant_resultc             C   s   | j  p| j S )N)r  r   r  )rL   rJ   rJ   rK   r   O/  s    zCondExprNode.is_ephemeralc             C   s:   | j ||| _ | j|| _| j|| _| |S )N)r  r   r   r  r  r  )rL   rc   rJ   rJ   rK   r   R/  s    zCondExprNode.analyse_typesc             C   s   | j j}| jj}t||| _| jjr6t| jj| _| jjrFt	| _
n | j  sZ| j rft| jd |jsr|jr|| jkr| j | j|| _ || jkr| 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*  rk  )rL   rc   Ztrue_val_typeZfalse_val_typerJ   rJ   rK   r  X/  s"    

z CondExprNode.analyse_result_typec             C   s@   | j jjs| j || _ | jjjs0| j|| _d | _| |S )N)r  r   r/  rQ  r  r   r  )rL   rc   rJ   rJ   rK   rQ  n/  s    

zCondExprNode.coerce_to_integerc             C   sH   | j j|kr| j ||| _ | jj|kr8| j||| _d | _| |S )N)r  r   r&  r  r   r  )rL   r:  rc   rJ   rJ   rK   r&  v/  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   rk  ~/  s    zCondExprNode.type_errorc             C   s   | j  o| j o| j S )N)r  r   r  r  )rL   rJ   rJ   rK   r   /  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  /  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  /  s    z+CondExprNode.generate_subexpr_disposal_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  /  s    zCondExprNode.free_subexpr_temps)rN   rO   rP   r  r  r   r   r   rr   r   r   r   r  rQ  r&  rk  r   r  r  r  r  rJ   rJ   rJ   rK   r  4/  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  ru  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   rq  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   r4  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   rd  r  r/  r   r   r   r[  r  )	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*   r6  r  ra   r2  r   r  r   r   r  r  rN  r  r7  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  0  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  X0  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   r:  rc   r  rJ   rJ   rK   r  c0  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  j0  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  r0  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   container_typerJ   rJ   rK   r  y0  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   rZ  r   ra   rA  )rL   rc   r  result_is_boolr  r  rJ   rJ   rK   "find_special_bool_compare_function0  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  nX|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 n
|| 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!=ro  eqz%s = %s(%s %s %s);r  )r   ra   r   r  r  r  r   r  r6  r   r   r   r   richcmp_constantsrn   r   r6  rr  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  0  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%   1  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
-Tr  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   1  s    zPrimaryCmpNode.infer_typec             C   s   dS )NrJ   rJ   )rL   rc   rJ   rJ   rK   r   '1  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   *1  s    
z(PrimaryCmpNode.calculate_constant_resultc             C   s   | j |}| ||S )N)r  r   r  )rL   r   r  rJ   rJ   rK   r   .1  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   r  r&  r   r   rZ  r  ry  r"  ra   rA  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   21  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*  r  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)  1  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  rt  r  r   r   r
   r*  r   ry  is_memslice_nonecheck)rL   rc   Z	have_noneZ
have_sliceZopsrJ   rJ   rK   r+  1  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   ry  r   r   r  r-  r  r(  r   r   )rL   rc   r  rJ   rJ   rK   r   1  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!=ro  r  z(%s%s(%s, %s))z==Z__Pyx_UnicodeContainsUCS4Z__Pyx_BytesContainsr  z((PyObject *) %s.memview)z
(%s %s %s))r  r  r   r6  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  2  s    z-PrimaryCmpNode.generate_subexpr_disposal_codec             C   s   | j | | j| d S )N)r  r  r  )rL   r   rJ   rJ   rK   r  2  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  2  s    zPrimaryCmpNode.annotate)rN   rO   rP   rn  r  r(  r.  rr   r   r   r   r   r)  r+  r   r  r   r   r  r  r  r  rJ   rJ   rJ   rK   r  1  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   *2  s    zCascadedCmpNode.infer_typec             C   s   dS )NrJ   rJ   )rL   rc   rJ   rJ   rK   r   .2  s    z!CascadedCmpNode.type_dependenciesc             C   s   | j tk	o| j tk	S )N)rl   r   r   )rL   rJ   rJ   rK   rz   12  s    
z#CascadedCmpNode.has_constant_resultc             C   s&   | j || _ | jr"| j|| _| S )N)r  r   r  )rL   rc   rJ   rJ   rK   r   52  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   ry  r   ra   rA  r  r-  r  r(  )rL   r  rc   r!  r  rJ   rJ   rK   r-  B2  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  rA  r   r   r
   rZ  r  r  )rL   rc   rJ   rJ   rK   r  N2  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,  U2  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) {)r0  r   )r   ra   r   r  r  r  r  r
   r  r(  r
  r  )rL   r   r   r  r0  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  r2  s    zCascadedCmpNode.annotate)F)F)rN   rO   rP   rn  r  r(  r   rl   rr   r   rz   r   r  r  r-  r  r,  r  r  rJ   rJ   rJ   rK   r1  2  s   



r1  )r  r  r-  r  r|  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  r\  rJ   rJ   rK   r  2  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   r3  r~  rn   r  r   r  )rL   r  )rI   rJ   rK   r~  2  s    zCoercionNode.__init__c             C   s   d S )NrJ   )rL   rJ   rJ   rK   r   2  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   r~  r   r  rQ   rJ   rJ   )rI   rK   r3  2  s
   r3  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   sJ   |j s
t|jj rtt| | || _d| _d| _|| _| j| d S )Nr$   T)	r   r   r   r3  r~  r   r   r  r  )rL   r  r:  rc   rJ   rJ   rK   r~  2  s    
z!CoerceToMemViewSliceNode.__init__c             C   s(   | | j| j |  | j| d S )N)r   r   from_py_call_coder  r   r   rn   )rL   r   rJ   rJ   rK   r  2  s
    
z-CoerceToMemViewSliceNode.generate_result_codeN)rN   rO   rP   rm  r~  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)r3  r~  r   )rL   r  new_typerJ   rJ   rK   r~  2  s    zCastNode.__init__c             C   s
   | j  S )N)r  rU  )rL   rJ   rJ   rK   rU  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   r~  rU  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d Zd d! Zd"d# Zd$S )&r3  TFc             C   s:   |j s|jstdt| | || _| | _|| _d S )Nz PyTypeTest on non extension type)	r  r  r   r3  r~  r   r   r   r  )rL   r  r:  rc   r  rJ   rJ   rK   r~  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)r  r  rU  )rL   rJ   rJ   rK   rU  2  s    zPyTypeTestNode.may_be_nonec             C   s
   | j  S )N)r  rT  )rL   rJ   rJ   rK   rT  3  s    zPyTypeTestNode.is_simplec             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   3  s    zPyTypeTestNode.result_in_tempc             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   3  s    zPyTypeTestNode.is_ephemeralc             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   
3  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_availablerU  r  rZ  rb   )rL   rJ   rJ   rK   	reanalyse3  s    
zPyTypeTestNode.reanalysec             C   s   d S )NrJ   )rL   rJ   rJ   rK   r   3  s    z(PyTypeTestNode.calculate_constant_resultc             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   3  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   r  r:  r  r  r   r  r   r   rn   r   )rL   r   Z	type_testrJ   rJ   rK   r  3  s    
z#PyTypeTestNode.generate_result_codec             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  .3  s    z,PyTypeTestNode.generate_post_assignment_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   13  s    z#PyTypeTestNode.allocate_temp_resultc             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r   43  s    z"PyTypeTestNode.release_temp_resultc             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  73  s    zPyTypeTestNode.free_tempsc             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  :3  s    z!PyTypeTestNode.free_subexpr_tempsN)F)rN   rO   rP   r:  r~  r%   r   r   r  r   rU  rT  r   r   r   r;  r   r   r  r  r   r   r  r  rJ   rJ   rJ   rK   r3  2  s&   
		r3  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 )%rX  TrJ   c             C   s<   t | | |j| _| | _|| _|| _t|p2d| _d S )NrJ   )	r3  r~  r   r   r   exception_type_cnameexception_messager   exception_format_args)rL   r  r=  r>  r?  rJ   rJ   rK   r~  E3  s    
zNoneCheckNode.__init__Nc             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   P3  s    zNoneCheckNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU  S3  s    zNoneCheckNode.may_be_nonec             C   s
   | j  S )N)r  rT  )rL   rJ   rJ   rK   rT  V3  s    zNoneCheckNode.is_simplec             C   s
   | j  S )N)r  r   )rL   rJ   rJ   rK   r   Y3  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   rq  )rL   rJ   rJ   rK   	conditionb3  s
    
zNoneCheckNode.conditionrW  Fc             C   s"   | ||||}||_ || d S )N)r   put_nonecheck)rH   r  r   r>  r=  r?  r   rp   rJ   rJ   rK   generatej3  s    zNoneCheckNode.generatec             C   s    |  r| |||||| d S )N)rU  rB  )rH   r  r   r>  r=  r?  r   rJ   rJ   rK   generate_if_neededq3  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   r  )rS   r  )escaperJ   rK   rt   3  s   z/NoneCheckNode.put_nonecheck.<locals>.<listcomp>zPyErr_SetString(%s, "%s");r   )r   r@  r   r   r	   Zescape_byte_stringr?  r=  r>  r  r  r   r   rn   )rL   r   rJ   )rD  rK   rA  w3  s&    zNoneCheckNode.put_nonecheckc             C   s   |  | d S )N)rA  )rL   r   rJ   rJ   rK   r  3  s    z"NoneCheckNode.generate_result_codec             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  3  s    z+NoneCheckNode.generate_post_assignment_codec             C   s   | j | d S )N)r  r  )rL   r   rJ   rJ   rK   r  3  s    zNoneCheckNode.free_temps)rJ   )rW  rJ   F)rW  rJ   F)rN   rO   rP   r  r~  r   r   rU  rT  r   r   r   r@  ro  rB  rC  rA  r  r  r  rJ   rJ   rJ   rK   rX  >3  s$   
rX  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 )r1  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   r6  r   r3  r~  r   r  r  ri   r  r  r   r   r  target_typer   r    r[   )rL   r  rc   r   rJ   rJ   rK   r~  3  s*    


zCoerceToPyTypeNode.__init__zConverting to Python objectc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU  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   ry  ra   rb   rS  rH  )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   rQ  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   rE  r   rn   r  r   )rL   r   rJ   rJ   rK   r  3  s    z'CoerceToPyTypeNode.generate_result_codeN)rN   rO   rP   r   r   rE  r   r~  r  rU  r   rQ  r   r  rJ   rJ   rJ   rK   r1  3  s   r1  c               @   s    e Zd ZdZdd Zdd ZdS )r0  r$   c             C   s"   | |}t| | tj| _d S )N)r   r3  r~  r   r   r   )rL   r  rc   rJ   rJ   rK   r~  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   r  r  r  r   r   rn   r   r   r   r   r  r   )rL   r   r  Z
arg_resultr  rJ   rJ   rK   r  3  s0    

z)CoerceIntToBytesNode.generate_result_codeN)rN   rO   rP   r   r~  r  rJ   rJ   rJ   rK   r0  3  s   r0  c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )r5  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   )r3  r~  r   r   r  r   rn   r  r  r  r  r   r  r   )rL   r  r  rc   rJ   rJ   rK   r~  4  s    

zCoerceFromPyTypeNode.__init__c             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   4  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   !4  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   rF  rx  rw   rX  rC  r   r8  r   r   rn   ra   r  )rL   r   rF  rJ   rJ   rK   r  $4  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   14  s    z CoerceFromPyTypeNode.nogil_checkN)rN   rO   rP   r~  r   r   r  r   rJ   rJ   rJ   rK   r5  4  s
   r5  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 )rH  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$   )r3  r~  r   ra   r   )rL   r  rc   rJ   rJ   rK   r~  E4  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   J4  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   P4  s    zCoerceToBooleanNode.check_constc             C   s   d| j   S )Nz	(%s != 0))r  r   )rL   rJ   rJ   rK   r   V4  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   rG  rZ   r  r   rU  r   r   r   r   r  r6  rn   )rL   r   rW  ZchecksrJ   rJ   rK   r  Y4  s    z(CoerceToBooleanNode.generate_result_codeN)rN   rO   rP   r   ry  r   r   r   r   r   Zfrozenset_typer   r    r   rG  r~  r   r  r   r   r  rJ   rJ   rJ   rK   rH  54  s   
rH  c               @   s$   e Zd Zdd Zdd Zdd ZdS )r8  c             C   s2   |j jr||}|| _ t| | || d S )N)r   r6  r   r3  r~  r  )rL   r  r:  rc   rJ   rJ   rK   r~  k4  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   r6  r   r  )rL   r  r  rJ   rJ   rK   r   r4  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   r~  r   r  rJ   rJ   rJ   rK   r8  i4  s   r8  c               @   s0   e Zd Zdd ZdZdd Zdd Zdd	 Zd
S )rR  c             C   s<   t | | | jj | _| jj| _d| _| jjr8t| _	d S )Nr$   )
r3  r~  r  r   Zas_argument_typerl   r   ra   r   r   )rL   r  rc   rJ   rJ   rK   r~  4  s    
zCoerceToTempNode.__init__z#Creating temporary Python referencec             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   4  s    zCoerceToTempNode.analyse_typesc             C   s4   | j || _ | j  r| j S | j j| _| j| _| S )N)r  r   rT  r   r   )rL   rc   rJ   rJ   rK   r   4  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   r~  r  r   r   r  rJ   rJ   rJ   rK   rR  4  s
   rR  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  r~  rl   _proxy_type)rL   r  )rI   rJ   rK   r~  4  s    zProxyNode.__init__c             C   s   | j || _ |   | S )N)r  r   rH  )rL   rc   rJ   rJ   rK   r   4  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   rH  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  rT  )rL   rJ   rJ   rK   rT  4  s    zProxyNode.is_simplec             C   s
   | j  S )N)r  rU  )rL   rJ   rJ   rK   rU  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   rm  r   r~  r   rr   rH  r  r   rT  rU  r  r
  r  rQ   rJ   rJ   )rI   rK   r  4  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   )r3  r~  rl   r   r   r   r   )rL   r  rJ   rJ   rK   r~  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  rU  )rL   rJ   rJ   rK   rU  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&  5  s    zCloneNode.coerce_toc             C   s   dS )NTrJ   )rL   rJ   rJ   rK   rT  	5  s    zCloneNode.is_simplec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  5  s    z"CloneNode.generate_evaluation_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  5  s    zCloneNode.generate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r
  5  s    z CloneNode.generate_disposal_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  5  s    zCloneNode.free_temps)rN   rO   rP   r   r   r~  r   rU  r   rr   r   r&  rT  r  r  r
  r  rQ   rJ   rJ   )rI   rK   r  4  s   	r  c               @   s   e Zd Zdd ZdS )r9  c             C   s$   |j r| j|r| S t| ||S )N)r  r   r2  r  r&  )rL   r:  rc   rJ   rJ   rK   r&  5  s    zCMethodSelfCloneNode.coerce_toN)rN   rO   rP   r&  rJ   rJ   rJ   rK   r9  5  s   r9  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   ,5  s    zModuleRefNode.analyse_typesc             C   s   dS )NFrJ   )rL   rJ   rJ   rK   rU  /5  s    zModuleRefNode.may_be_nonec             C   s   t jS )N)r   r  )rL   rJ   rJ   rK   r   25  s    z#ModuleRefNode.calculate_result_codec             C   s   d S )NrJ   )rL   r   rJ   rJ   rK   r  55  s    z"ModuleRefNode.generate_result_codeN)rN   rO   rP   r   r   r   r   r   rU  r   r  rJ   rJ   rJ   rK   rI  %5  s   rI  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   r~  r   ra   r   r  )rL   rn   r  rJ   rJ   rK   r~  ?5  s    zDocstringRefNode.__init__c             C   s   | S )NrJ   )rL   rc   rJ   rJ   rK   r   D5  s    zDocstringRefNode.analyse_typesc          
   C   sL   | d|  | j |td||  | jf  ||   d S )Nz%s = __Pyx_GetAttr(%s, %s); %srm  )	r   r   r  r  r	   rM  r   rn   r  )rL   r   rJ   rJ   rK   r  G5  s
    z%DocstringRefNode.generate_result_codeN)
rN   rO   rP   r   r   r   r   r~  r   r  rJ   rJ   rJ   rK   rJ  85  s   rJ  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
}
)rK  rL  requiresZRaiseTooManyValuesToUnpackzObjectHandling.cZRaiseNeedMoreValuesToUnpackZUnpackTupleError)NN)F(4  
__future__r   cythonZdeclarer}  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   __builtin__rB   ImportErrorrg   re   r  rh   version_infor#   rx  r"   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   rp  rq  rr  ru  rw  rE  rz  r|  r   r  r  r  r  r  r  r  r  r%  r  r  r%  rA  rE  rG  rH  rN  rQ  rU  rV  rX  r`  rl  ro  rp  r   r  r  r  r  r  r  r  rr  r  r  rK  r0  rS  rX  r`  rb  rc  rd  rm  rn  rL  r  r  ru  r  r  r  r  r  r  r  r  r$  r"  r  r  r  r  r  r  r  r  r  r$  r  r  r2  r  r  r3  r6  r8  rI  rN  rO  rP  rT  rU  rV  rW  rY  not_invnegrn   r]  r\  rn  rp  rq  rs  rt  ru  rv  rz  r{  r  r  r  r  r  r  r  r  r(  r  r  r  r  r  matmulr  ry   ltler#  negegtis_r  rh  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  r1  r2  r  r3  r'  r  r3  rX  r1  r0  r5  rH  r8  rR  r  r  r9  rI  rJ  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qI2$     $@  5 <0`b       Jo .   mC7    i <b [*     :3      [6)". *0 3$.
72$*  *I f, "2
  `2G&

	 D 	2 1D^; pv  T  _X]I()4'4=