U
    ef                 $   @   s  d dl mZ d dlZd dlZd dlZzd dlZW n ek
rL   d dlZY nX 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 dd	lmZ dd
lmZ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 ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1h$Zd2d3 Zd^d4d5Z G d6d7 d7e!Z"G d8d9 d9e!Z#G d:d; d;e#Z$G d<d= d=e!Z%G d>d? d?e%Z&G d@dA dAe%Z'dZ(G dBdC dCe%Z)G dDdE dEe%Z*G dFdG dGe%Z+G dHdI dIe*Z,G dJdK dKe,Z-G dLdM dMe%Z.G dNdO dOe%Z/G dPdQ dQe/Z0G dRdS dSe/Z1G dTdU dUe%Z2G dVdW dWe%Z3G dXdY dYe%Z4G dZd[ d[e%Z5G d\d] d]e%Z6dS )_    )absolute_importN   )try_finally_contextmanager   )warningerrorInternalError)EncodedString)OptionsNaming)
PyrexTypes)py_object_typeunspecified_type)pyfunction_signaturepymethod_signaturerichcmp_special_methodsget_slot_tableget_property_accessor_signature)Future)CodeautobreakZcasecharconstcontinuedefaultZdodoubleelseenumexternfloatforgotoifintlongregisterreturnshortsignedsizeofZstaticstructswitchZtypedefunionZunsignedvoidZvolatilewhileZ_Boolz_Complex, _ImaginaryinlineZrestrictc                 C   s:   | d d dkr$|  tjs$| dkr,| tkr6tj|  } | S )Nr   __)__weakref____dict__)
startswithr   pyrex_prefixiso_c99_keywordscname r9   :/tmp/pip-unpacked-wheel-7k3cqui1/Cython/Compiler/Symtab.pyc_safe_identifier'   s    
r;   c                 C   sp   z|  d W n\ tk
rj   |  dddd} |rHd|| f } n| tjrf| tjtjd} Y nX | S )Nasciipunycode   -   _z%s_%sr   )encodeUnicodeEncodeErrorreplacedecoder4   r   r5   Zpyunicode_identifier_prefix)r8   Zmangle_withr9   r9   r:   punycodify_name/   s     
rD   c                   @   s    e Zd ZdZdd Zdd ZdS )	BufferAuxFc                 C   s   || _ || _d S N)buflocal_nd_var	rcbuf_var)selfrG   rH   r9   r9   r:   __init__I   s    zBufferAux.__init__c                 C   s
   d| j  S )Nz<BufferAux %r>)r3   rI   r9   r9   r:   __repr__M   s    zBufferAux.__repr__N)__name__
__module____qualname__Zwritable_neededrJ   rL   r9   r9   r9   r:   rE   F   s   rE   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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(g Z)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3dZ4dZ5dZ6dZ7dZ8dZ9dZ:dZ;dZ<dZ=dZ>dZ?dZ@dZAdZBdZCdZDdZEdZFdZGdZHdZIdZJdZKdddZLd	d
 ZMdd ZNdd ZOdd ZPdd ZQdd ZReSdd ZTdd ZUdd ZVdS )EntryFr    NprivateTc                 C   s@   || _ || _|| _|| _|| _g | _g | _g | _g | _| | _	d S rF   )
namer8   typeposinitoverloaded_alternativescf_assignmentscf_referencesinner_entriesdefining_entry)rI   rS   r8   rT   rU   rV   r9   r9   r:   rJ      s    zEntry.__init__c                 C   s   dt | jt| | j| j f S )Nz%s(<%x>, name=%s, type=%s))rT   rM   idrS   rK   r9   r9   r:   rL     s    zEntry.__repr__c                 C   s   t | jd d S )NPrevious declaration is here)r   rU   rK   r9   r9   r:   already_declared_here  s    zEntry.already_declared_herec                 C   s   t |d| j  |   d S )Nz('%s' does not match previous declaration)r   rS   r^   )rI   rU   r9   r9   r:   
redeclared  s    zEntry.redeclaredc                 C   s   | g| j  S rF   )rW   rK   r9   r9   r:   all_alternatives  s    zEntry.all_alternativesc                 C   s   | g| j  S rF   )rZ   rK   r9   r9   r:   all_entries  s    zEntry.all_entriesc                 C   s4   t | tr,t |tr,| j| jf|j|jfk S tS d S rF   )
isinstancerP   rS   r8   NotImplemented)leftrightr9   r9   r:   __lt__  s    zEntry.__lt__c                 C   s   t | jdkS Nr   )lenrX   rK   r9   r9   r:   cf_is_reassigned  s    zEntry.cf_is_reassignedc                 C   s0   | j jstd| _| jrttjdd| _d S )NTZOptionalLocalsCppSupport.cpp)	rT   is_cpp_classAssertionErroris_cpp_optionalutility_coder   UtilityCodeload_cachedutility_code_definitionrK   r9   r9   r:   make_cpp_optional  s    
zEntry.make_cpp_optionalc                 C   s   | j r|| j kS dS NF)pytyping_modifiers)rI   Zmodifier_namer9   r9   r:   declared_with_pytyping_modifier"  s    z%Entry.declared_with_pytyping_modifier)NN)WrM   rN   rO   inline_func_in_pxdZborrowedrV   
annotationZpep563_annotation
visibility
is_builtin
is_cglobalis_pyglobal	is_memberis_pyclass_attris_variableis_cfunction
is_cmethodis_builtin_cmethodZis_unbound_cmethodis_final_cmethodis_inline_cmethodis_anonymousis_type	is_cclassrk   is_constis_propertyis_cproperty	doc_cnameZgetter_cnameZsetter_cnameZis_self_argis_argis_local
in_closurefrom_closurein_subscopeZis_declared_genericis_readonlyZpyfunc_cname
func_cnamefunc_modifiersfinal_func_cnamedocas_variableZxdecref_cleanupin_cinclude	as_moduleis_inheritedZpystring_cnameZis_identifierZis_internedused
is_specialdefined_in_pxdis_implementedapirn   is_overridableZ
buffer_aux
prev_entryZmight_overflowZfused_cfunctionZis_fused_specializedrq   needs_propertyin_with_gil_blockZfrom_cython_utility_codeZerror_on_uninitializedZcf_usedouter_entry
is_cgetterrm   known_standard_library_importrt   Zenum_int_valuerJ   rL   r^   r_   r`   ra   rf   propertyri   rr   ru   r9   r9   r9   r:   rP   Q   s   [

rP   c                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )

InnerEntryzK
    An entry in a closure scope that represents the real outer Entry.
    Tc                 C   sv   t | |j|j|j|j || _|| _|}|jr8|j}q*|| _|j	| _	|j
| _
|j| _|j| _|j| _| j	|  d S rF   )rP   rJ   rS   r8   rT   rU   r   scoper[   rZ   rX   rY   rW   rm   append)rI   r   r   Zoutermost_entryr9   r9   r:   rJ   ,  s"    
zInnerEntry.__init__c                 C   s   | drt|t| j|S )Nr1   )r4   AttributeErrorgetattrr[   rI   rS   r9   r9   r:   __getattr__@  s    
zInnerEntry.__getattr__c                 C   s
   | j  S rF   )r[   ra   rK   r9   r9   r:   ra   F  s    zInnerEntry.all_entriesN)rM   rN   rO   __doc__r   rJ   r   ra   r9   r9   r9   r:   r   &  s
   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g ZdZdd Zdd	 Zdxd
dZdd Zdd ZdyddZdd Zdd ZdzddZdd Z dd Z!dd Z"e#d{ddZ$d|d d!Z%d"d# Z&d}d%d&Z'd~d(d)Z(dd*d+Z)dd,d-Z*dd0d1Z+d2d3 Z,d4d5 Z-dd6d7Z.d8d9 Z/dd:d;Z0dd<d=Z1d>d? Z2d@dA Z3ddBdCZ4ddDdEZ5dFdG Z6dHdI Z7dJdK Z8ddLdMZ9ddNdOZ:ddPdQZ;dRdS Z<dTdU Z=dVdW Z>dXdY Z?dZd[ Z@d\d] ZAd^d_ ZBd`da ZCdbdc ZDddde ZEdfdg ZFdhdi ZGdjdk ZHdldm ZIdndo ZJdpdq ZKdrds ZLdtdu ZMddvdwZNdS )Scoper   FrQ   NTc                 C   s   || _ || _|| _dt||ddf }|  }|rP||| _|j| | _nt	|| _|| _i | _
t | _g | _g | _g | _g | _g | _g | _g | _g | _g | _i | _i | _i | _i | _i | _g | _g | _i | _| jD ]}| jt	|t d d qd S )Nz%d%s_.Z_dot_rU   )!rS   outer_scopeparent_scoperh   rB   qualifying_scopequalify_namequalified_namescope_prefixr	   entriesset	subscopesconst_entriestype_entriessue_entriesarg_entriesvar_entriespyfunc_entriescfunc_entriesc_class_entriesZdefined_c_classesZimported_c_classesZcname_to_entryZidentifier_to_entryZnum_to_entryZobj_to_entryZbuffer_entrieslambda_defsid_countersscope_predefined_namesdeclare_varr   )rI   rS   r   r   mangled_nameZ
qual_scopevar_namer9   r9   r:   rJ     s>    

zScope.__init__c                 C   s   | S rF   r9   )rI   memor9   r9   r:   __deepcopy__  s    zScope.__deepcopy__c           
      C   s   g }|j  D ],\}}|r"||kr|js,|r|||f q| j | dD ]N}t| |}tdd |D }	t||D ]"}|js|rv|j|	krv|| qvqLd S )N)r   r   r   r   r   r   r   r   c                 s   s   | ]}|j V  qd S rF   rS   ).0er9   r9   r:   	<genexpr>  s     z!Scope.merge_in.<locals>.<genexpr>)r   itemsr   r   updater   r   rS   )
rI   otherZmerge_unusedZ	allowlistr   rS   entryattrZself_entriesnamesr9   r9   r:   merge_in  s    

zScope.merge_inc                 C   s   d| j j| jf S )Nz<%s %s>)	__class__rM   r   rK   r9   r9   r:   __str__  s    zScope.__str__c                 C   s   | j S rF   r   rK   r9   r9   r:   r     s    zScope.qualifying_scopec                 C   s,   |rt d|| j|f S | j|| jS d S )Nz%s%s%s)rD   r   r   manglerS   rI   prefixrS   r9   r9   r:   r     s    zScope.manglec                 C   s   dt j|f }| |S )Nz%s%s_)r   r5   r   )rI   rS   r   r9   r9   r:   mangle_internal  s    zScope.mangle_internalc                 C   s   | j r| j |S |S rF   )r   mangle_class_private_namer   r9   r9   r:   r     s    zScope.mangle_class_private_namec                 C   s`   |   j}z|| d }W n tk
r2   d}Y nX |||< |rT|sH|S d||f S d| S d S )Nr   r   z%s%dz%d)global_scoper   KeyError)rI   rS   Zcounterscountr9   r9   r:   next_id  s    

zScope.next_idc                 C   s
   | j  S z6 Return the module-level scope containing this scope. )r   r   rK   r9   r9   r:   r     s    zScope.global_scopec                 C   s
   | j  S r   )r   builtin_scoperK   r9   r9   r:   r     s    zScope.builtin_scopec                 c   s0   | V  | j r,t| j tddD ]
}|V  q d S )Nr   )key)r   sortedoperator
attrgetter)rI   r   r9   r9   r:   iter_local_scopes  s    zScope.iter_local_scopesc                 c   s$   | j }|d k	r|| _ d V  || _ d S rF   )in_c_type_context)rI   r   Zold_c_type_contextr9   r9   r:   new_c_type_context  s
    zScope.new_c_type_contextc	                 C   sv  |j rt| tst|d | js@|r@td|r@t|d| d | j}	|r||	kr|s| j	s|	| }
d}|j
r|
jj
r|  r|
 D ]$}||jkr|dkr|jsd} qqd}|rnZ| jr|	| jrnH|dkrt|d	| | jrd
nd n$|dkrt|d	|  |	|   t||||d}| j|_||_|rV| ||_|sV||	|< |jrf|j|_| |_||_|S )Nz5Buffer types only allowed as function local variablesz
^_[_A-Z]+$z'%s' is a reserved name in C.F<init>Tr   '%s' redeclared r   r   ignorer   )Z	is_bufferrb   
LocalScoper   r   rematchr   r   is_builtin_scoper   rT   is_cppr`   argsis_cpp_class_scoper   r^   rP   create_wrapperr   r   is_memoryviewslicedefault_valuerV   r   rx   )rI   rS   r8   rT   rU   rx   shadowr   r   r   Z	old_entryZcpp_override_allowed	alt_entryr   r9   r9   r:   declare   sH    


zScope.declarec                 C   s   t d| j|f S Nz%s.%s)r	   r   r   r9   r9   r:   r   :  s    zScope.qualify_namerR   c	           
      C   sP   |s*| j s|dks|r|}n| tj|}| j||||||d}	d|	_||	_|	S )Npublic)r   r   )r   r   r   Zenum_prefixr   r   Z
value_node)
rI   rS   rT   valuerU   r8   rx   r   r   r   r9   r9   r:   declare_const=  s    zScope.declare_constr   c
              	   C   sZ   |s|}| j ||||||dd}
d|
_||
_|r<| j|
 |	sVt|dd d krV|
|_|
S )NT)r   r   r   )r   r   r   r   r   r   r   )rI   rS   rT   rU   r8   rx   r   definingr   templater   r9   r9   r:   declare_typeI  s    zScope.declare_typec              
   C   s   |s*| j s|dks|r|}n| tj|}z6| jrD| j| jj}nd }t	
||||dk|}W n8 tk
r }	 zt||	jd  t	j}W 5 d }	~	X Y nX | j||||||d}
|
j|_|
S )NrR   r   r   )rx   r   )r   r   r   type_prefixr   r   lookuprS   rT   r   Zcreate_typedef_type
ValueErrorr   r   
error_typer  r   )rI   rS   	base_typerU   r8   rx   r   	namespacerT   r   r   r9   r9   r:   declare_typedef]  s*    
 zScope.declare_typedefc
              	   C   s(  |s*| j s|dks|r|}n| tj|}| |}
|
s|  }tj||||||	|d}| j|||||||d k	d}
| j	
|
 |
|_n|
jr|
jjr|
jj|kst|d| d nT|r|
jjrt|d| d n6| |
|| | |
|| |r||
j_| j
|
 | jr$| j| jj|
j_|
S )Nr   )in_cpp)rx   r   r   '%s' redeclared  r   2'%s' already defined  (ignoring second definition))r   r   r   r  lookup_herer   r   ZCStructOrUnionTyper  r   r   r   r   rT   Zis_struct_or_unionkindr   r   check_previous_typedef_flagcheck_previous_visibilityr   r   r   r  rS   r  )rI   rS   r  r   typedef_flagrU   r8   rx   r   packedr   r	  rT   r9   r9   r:   declare_struct_or_uniont  sJ    
      
zScope.declare_struct_or_unionr9   r   c           
         s  |d kr*| j s|dkr|}n| tj|}t|}| |}|s|tj|||||d}	| j||	|||d k	d}| j	
| n|jr|jjstd|  |  d S |r|jjrtd| d n|r||j_| j
| |r|jjr|jj|krtd |  n||j_|s$|jjrD||jjkrDtd |   fd	d
 |r~ || |jddt|j|jd | jr| j| jj|j_|S )NrR   )	templates)rx   r   r   r  r   -Base type does not match previous declarationz5Template parameters do not match previous declarationc                    sJ   |D ]@}|t jkrq|jd kr*td q | |j | jj| qd S )Nz#Cannot inherit from incomplete type)r   r  r   r   base_classesrT    declare_inherited_cpp_attributes)r   r  
base_classdeclare_inherited_attributesrU   r9   r:   r    s    

z=Scope.declare_cpp_class.<locals>.declare_inherited_attributesthis)rS   r8   rT   rU   )r   r   r   r  listr  r   ZCppClassTyper  r   r   r   rT   rk   r   r^   r   r   r   r  r  r   CPtrTyperU   r   r   r  rS   r  )
rI   rS   r   rU   r8   r  rx   r  r   rT   r9   r  r:   declare_cpp_class  sX    
     


	
zScope.declare_cpp_classc                 C   s,   ||j jkr(t|d|jd|j j f  d S )Nz#'%s' previously declared using '%s')cdefZctypedef)rT   r  r   rS   )rI   r   r  rU   r9   r9   r:   r    s
     
z!Scope.check_previous_typedef_flagc                 C   s$   |j |kr t|d|j|j f  d S )Nz '%s' previously declared as '%s')rx   r   rS   )rI   r   rx   rU   r9   r9   r:   r    s
    
 zScope.check_previous_visibilityc
                 C   s   |r|s6| j s"|dks"|dks"|r(|}n| tj|}| jrN| j| jj}
nd }
|rjt	j
|||
|	d}qt	j||||
|	d}nt	j}| j||||||d}|rtjdd|_| | ||_g |_| j| |S )Nr   r   )r   )r8   rx   r   ZEnumClassDeclrj   )r   r   r   r  r   r   r  rS   rT   r   ZCppScopedEnumTypeZ	CEnumTypeZc_anon_enum_typer  r   ro   rp   rn   use_entry_utility_coder   Zenum_valuesr   r   )rI   rS   rU   r8   scopedr  rx   r   r   r   r  rT   r   r9   r9   r:   declare_enum  s6     
zScope.declare_enumc                 C   s   | j ||S rF   )r   declare_tuple_type)rI   rU   
componentsr9   r9   r:   r"    s    zScope.declare_tuple_typec
                 C   s   |s$|dks|r|}n|  tj|}| |||||}
d|
_|jrh|dkrh| jd r^|
  n
|| |r|dkrd|
_	d|
_
|rd|
_d|
_
|	r|	|
_|
S )NrR   r   r   
cpp_locals)r   r   
var_prefixr   r~   rk   
directivesrr   check_nullary_constructorr   r   r   rt   rI   rS   rT   rU   r8   rx   r   in_pxdis_cdefrt   r   r9   r9   r:   r     s&    


zScope.declare_varc                 C   s,   |sd S |D ]}||krt |d|  qd S )Nz"Modifier '%s' is not allowed here.)r   )rI   rU   	modifiersallowedmodifierr9   r9   r:   _reject_pytyping_modifiers  s
    z Scope._reject_pytyping_modifiersc                 C   s   |  |||S rF   r   rI   rS   rT   rU   r9   r9   r:   $declare_assignment_expression_target  s    z*Scope.declare_assignment_expression_targetc                 C   s   |  |}| j||S rF   )r   r   declare_builtinrI   rS   rU   r9   r9   r:   r2  !  s    
zScope.declare_builtinc                 C   sN   |r&|j js&t|d|  t|jd | j|t||d}t|_| j	| |S )Nz'%s' already declaredr]   rx   )
rT   r   r   rU   r   r   r   	signaturer   r   )rI   rS   rU   rx   r   r9   r9   r:   _declare_pyfunction%  s    zScope._declare_pyfunctionc                 C   s   |  |}|s | j||||dS |rR|jjr4t|_qd|jtk	rd| j||||dS n| j|t||d | jd t||dd}t||_| ||_	t
|_d|_|S )N)rx   r   r4  rR   r8   rx   T)r  r6  rT   is_unspecifiedr   r   r	   rS   r   r   r   r5  r   )rI   rS   rU   allow_redefinerx   r   r9   r9   r:   declare_pyfunction.  s$    


 
zScope.declare_pyfunctionc                 C   sl   |  tjd |}|  tjd |}| |}| d |t|d}t||_||_||_	||_
t|_d|_|S )NZfuncdef_Zmethdef_rR   T)r   r   Zlambda_func_prefixr   r   r   r	   rS   r   pymethdef_cnamer   r   r5  r   )rI   Zlambda_namerU   r   r;  r   r   r9   r9   r:   declare_lambda_functionB  s    

zScope.declare_lambda_functionc                 C   s   | j | d S rF   )r   r   rI   Zdef_noder9   r9   r:   add_lambda_defQ  s    zScope.add_lambda_defc                 C   s   | j | d S rF   )r   r   rI   r   r9   r9   r:   register_pyfunctionT  s    zScope.register_pyfunctionc                 C   s  |s$|dks|r|}n|  tj|}d|	ko2|o2|}|r<d}| |}|r|sx||jkrx|dkrxd}|j}||_||_|dkr||jkrt|d||j|f d ||jkrt|d||rd	nd
f d |j	
|r|j	|j|_	n|dkr|jdkr| j}|  rd}n:|rL|sL| D ] }|jr>||jkr& qLq&d}|rv| ||||||	}| |_|}nt|dd ||_	n.|s|jr||j	r||_	n
t|d n | ||||||	}||_||_|rd|_|r|dkrd|_|rd|_|s,|s,|dkr,t|d|  |r8d|_|	rD|	|_|
rh|jrbtd||f |
|_|rt||t}| ||_d|_d|_ |j!|_!||_"||_#|j$r|j%d kr|j&r|d j's|r|s|st(j)|| ||j*j+d |S )NrR   r0   Fr   Tz6Function '%s' previously declared as '%s', now as '%s'r   z)Function '%s' previously declared as '%s'Zcpdefr  6Function signature does not match previous declaration3Non-extern C function '%s' declared but not definedz2duplicate utility code definition in entry %s (%s)r   )Zfunction_nameZvoid_return),r   r   func_prefixr  rx   r8   r   r   r   rT   same_aswith_with_gilwith_gilr   r   r`   add_cfunctionrW   r   compatible_signature_withr   rv   r   r   r   rn   rl   rP   r   r   r   r~   r{   r   r   r   exception_checkexception_valuenogilZin_utility_coder   Zwrite_noexcept_performance_hintreturn_typeis_void)rI   rS   rT   rU   r8   rx   r   r)  r   r+  rn   overridableZinline_in_pxdr   Zcan_overrider   temp	var_entryr9   r9   r:   declare_cfunctionW  s    
  
 



   zScope.declare_cfunctionc              	   K   sd   t dd |D sttj|td| jd gf|}	| j||	|d |||d}
d|
_|d k	r`||
_|
S )Nc                 s   s   | ]}|d kV  qdS ))rJ  rI  rK  rF  Zis_const_methodis_static_methodNr9   )r   kr9   r9   r:   r     s   z(Scope.declare_cgetter.<locals>.<genexpr>rI   )r8   rx   r+  r   T)	allrl   r   	CFuncTypeCFuncTypeArgparent_typerQ  r   r   )rI   rS   rL  rU   r8   rx   r+  r   Zcfunc_type_configZ
cfunc_typer   r9   r9   r:   declare_cgetter  s,          zScope.declare_cgetterc                 C   s   |  |||||}d|_|r"||_|s,|jr:| j| nBt| j}	t| jD ]}
|
js`|
j	jsd qn|	d8 }	qN| j
|	| |S rg   )r   r   r   is_fusedr   r   rh   reversedr   rT   insert)rI   rS   rT   rU   r8   rx   r+  	inheritedr   icfunc_entryr9   r9   r:   rG    s    


zScope.add_cfunctionc                 C   s$   |  |}|r|S t|d|  d S )Nz'%s' is not declared)r  r   rI   rS   rU   r   r9   r9   r:   find  s    
z
Scope.findc                 C   sP   | }|D ]B}| ||}|s" d S |jr0|j}qt|dd|   d S q|S )Nz'%s' is not a cimported moduler   )r`  r   r   join)rI   pathrU   r   rS   r   r9   r9   r:   find_imported_module  s    zScope.find_imported_modulec                 C   sd   |  |}| |p(| jr&| j|p(d }|r2|S | jrD| j|pFd }|r`|jr`| |j| |S rF   )r   r  r   r  r{   _emit_class_private_warningrU   )rI   rS   r   r   r9   r9   r:   r    s    


zScope.lookupc                 C   s*   | j | |d }|r|S | j |d S rF   )r   getr   rI   rS   r   r9   r9   r:   r    s    zScope.lookup_herec                 C   s   | j |d S rF   )r   re  r   r9   r9   r:   lookup_here_unmangled  s    zScope.lookup_here_unmangledc                 C   s
   |  |S rF   )r  r   r9   r9   r:   #lookup_assignment_expression_target  s    z)Scope.lookup_assignment_expression_targetc                 C   sF   |  |}|s0| |}|r0|jr0| |j| |sB| |td }|S rF   )r  rg  r{   rd  rU   r   r   rf  r9   r9   r:   lookup_target  s    


zScope.lookup_targetc                 C   s0   |r,|j r,|jjr&| jr&|j| jS |jS d S rF   )r   rT   rY  fused_to_specific
specializer?  r9   r9   r:   $_type_or_specialized_type_from_entry   s    
z*Scope._type_or_specialized_type_from_entryc                 C   sF   |  |}| |}|r|S |r<|jr<ddlm} ||j}| |S )Nr   ) get_known_standard_library_entry)r  rl  r   Builtinrm  )rI   rS   r   tprm  r9   r9   r:   lookup_type&  s    



zScope.lookup_typec                 C   s  |d j jr`|d j }|jd| }|d k	r`dd |dd  D }t|| }|d k	r`|S | d| }g }|d k	r| }g }	t|dkrtdD ]>}
||
 j jr||
 j }|jd| }|d k	r|	| 7 }	q|	s|sd S t	t
|	| }tdd |D |S )Nr   z
operator%sc                 S   s   g | ]
}|j qS r9   rT   r   argr9   r9   r:   
<listcomp>;  s     z)Scope.lookup_operator.<locals>.<listcomp>r   r   c                 S   s   g | ]
}|j qS r9   rq  rr  r9   r9   r:   rt  T  s     )rT   rk   r   r  r   
best_matchr`   rh   ranger  r   )rI   r   operandsZobj_typemethodZ	arg_typesresfunctionZfunction_alternativesZmethod_alternativesnr`   r9   r9   r:   lookup_operator6  s4    

zScope.lookup_operatorc                    s<   ddl m} G dd d|  fdd|D }| ||S )Nr   )Nodec                   @   s   e Zd ZdS )z4Scope.lookup_operator_for_types.<locals>.FakeOperandN)rM   rN   rO   r9   r9   r9   r:   FakeOperandY  s   r~  c                    s   g | ]} |d qS )rq  r9   )r   rT   r~  rU   r9   r:   rt  [  s     z3Scope.lookup_operator_for_types.<locals>.<listcomp>)ZNodesr}  r|  )rI   rU   r   typesr}  rw  r9   r  r:   lookup_operator_for_typesW  s    zScope.lookup_operator_for_typesc                 C   s   t |d| d d S )NzGlobal name %s matched from within class scope in contradiction to to Python 'class private name' rules. This may change in a future release.r   )r   )rI   rU   rS   r9   r9   r:   rd  ^  s
    z!Scope._emit_class_private_warningc                 C   s   |   | d S rF   )r   use_utility_coderI   Znew_coder9   r9   r:   r  c  s    zScope.use_utility_codec                 C   s   |   | d S rF   )r   r  r?  r9   r9   r:   r  f  s    zScope.use_entry_utility_codec                 C   s   |D ]}|| j kr dS qdS Nr   r   )r   rI   r   rS   r9   r9   r:   defines_anyi  s    
zScope.defines_anyc                 C   s*   |D ] }|| j kr| j | jr dS qdS r  )r   r   r  r9   r9   r:   defines_any_specialp  s    zScope.defines_any_specialc                 C   s   ddl m} | |  d S )Nr   )get_type_inferer)TypeInferencer  infer_types)rI   r  r9   r9   r:   r  w  s    zScope.infer_typesc                 C   s   | j }|d krdS | S d S rs   )r   r   )rI   outerr9   r9   r:   r   {  s    zScope.is_cppc                 C   s   | j ||| d S rF   )r   add_include_file)rI   filenameverbatim_includelater9   r9   r:   r    s    zScope.add_include_file)TN)N)N)N)r   r   r   )NrR   r   r   )NrR   r   r   r   r   )NrR   r   )NrR   r   F)Nr9   r   N)rR   r   r   N)NrR   FFFN)r9   )r   N)Fr   )NrR   r   r   r   r9   NF)NNrR   r9   F)F)NF)OrM   rN   rO   r   is_py_class_scopeis_c_class_scopeis_closure_scopeis_local_scopeis_generator_expression_scopeis_comprehension_scopeis_passthroughr   is_property_scopeis_module_scopeis_c_dataclass_scopeZis_internalr   r   rK  rj  rL  r   r   rJ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r!  r"  r   r.  r1  r2  r6  r:  r<  r>  r@  rQ  rX  rG  r`  rc  r  r  rg  rh  ri  rl  rp  r|  r  rd  r  r  r  r  r  r   r  r9   r9   r9   r:   r   J  s   #$



:
          
    
     
&      
6       
          


	
              
]      

!r   c                   @   s"   e Zd ZejZdd Zdd ZdS )PreImportScopec                 C   s   t | tjd d  d S rF   )r   rJ   r
   
pre_importrK   r9   r9   r:   rJ     s    zPreImportScope.__init__c                 C   s"   |  ||t|d}d|_d|_|S )NrR   T)r   r   r~   r{   r_  r9   r9   r:   r2    s    zPreImportScope.declare_builtinN)rM   rN   rO   r   Zpreimport_cnamenamespace_cnamerJ   r2  r9   r9   r9   r:   r    s   r  c                   @   sN   e Zd ZdZdd ZdddZdd Zdd	d
Zddej	fddZ
dd ZdS )BuiltinScopeTc                 C   sH   t jd krt| dd d  nt| dt d  i | _| dtd d d S )N__builtin__boolz((PyObject*)&PyBool_Type))r
   r  r   rJ   r  
type_namesr   r   rK   r9   r9   r:   rJ     s
    
zBuiltinScope.__init__Nc                 C   s,   |dkr |d kr|dk}|s d}t | |S )Nstr)Nr   unicode)r   r  )rI   rS   language_level
str_is_strr9   r9   r:   r    s    zBuiltinScope.lookupc                 C   sL   t t|sH| jd k	r"| j||S tjr8t|d|  nt|d| d d S )Nundeclared name not builtin: %sr   )hasattrbuiltinsr   r2  r
   error_on_unknown_namesr   r   r3  r9   r9   r:   r2    s    

zBuiltinScope.declare_builtinc                 C   st   t |}| j||d |d|d}|rp|dkr0|}nt |}t||t}| ||_d|_d|_||_|j	|_	||_
|S )Nr   )rx   rn   *r   )r	   rQ  rP   r   r   r   r~   ry   rn   r   r   )rI   rS   rT   r8   Zpython_equivrn   r   rP  r9   r9   r:   declare_builtin_cfunction  s    z&BuiltinScope.declare_builtin_cfunctionc           
      C   s   t |}||||}t|d d|d}i |_|dkr8d|_|| d| j|< | j||d dd}||_t|j	| 
dj|j|jjd}	| ||	_d|	_d|	_d|	_d|	_||	_| |	_tjrd|	_|	|_|S )	Nr   )r   rx   rW  r  Tr   r4  rT   rS   rT   rU   r8   )r	   CClassScoper&  is_final_type	set_scoper  r  rn   rP   rS   r  rT   rU   typeptr_cnamer   r   r~   rz   r   ry   r   r
   cache_builtinsr   r   )
rI   rS   r8   rn   objstruct_cnameZ
type_classrT   r   r   rP  r9   r9   r:   declare_builtin_type  s6    


z!BuiltinScope.declare_builtin_typec                 C   s   | S rF   r9   rK   r9   r9   r:   r     s    zBuiltinScope.builtin_scope)NN)NN)rM   rN   rO   r   rJ   r  r2  r  r   ZBuiltinObjectTyper  r   r9   r9   r9   r:   r    s   
	

 
 r  c                   @   s"  e Zd ZdZdZdZdZddddddd	d
dg	ZdIddZdd Z	dd Z
dJddZdd Zdd ZdKddZdLddZdd  ZdMd!d"Zd#d$ Zd%d& Zd'd( Zd)d* ZdNd,d-ZdOd/d0Zd1d2 Zd3d4 Zd5d6 ZdPd7d8Zd9d: Zd;d< Zd=d> Zd?d@ ZdAdB Z dCdD Z!dEdF Z"dGdH Z#dS )QModuleScoper   r   __builtins__rM   __file__r   __path____spec__
__loader____package__
__cached__Fc                 C   s   ddl m} || _|j}t| ||| || _|| _t| j| _|| _	t
j| _t
j| _t
j| _d| _t
j| _g | _i | _i | _t|j| _d| _g | _t | _g | _d| _g | _g | _| j| _ i | _!| "t#j$ddd d S )Nr   rn  rQ   r   zPython.hT)initial)%rQ   rn  parent_moduler   r   rJ   
is_packagemodule_namer	   contextr   Zmodule_cnameZmoddict_cnameZmodule_dict_cnameZmethtable_cnamemethod_table_cnamer   Zmoddoc_cnamer   utility_code_listmodule_entries
c_includesdictr  Zpxd_file_loadedcimported_modulesr   Ztypes_importedZincluded_filesZhas_extern_classcached_builtinsundeclared_cached_builtinsr  _cached_tuple_typesprocess_includer   IncludeCode)rI   rS   r  r  r  rn  r   r9   r9   r:   rJ     s6    zModuleScope.__init__c                 C   s   | j S rF   )r  rK   r9   r9   r:   r   .  s    zModuleScope.qualifying_scopec                 C   s   | S rF   r9   rK   r9   r9   r:   r   1  s    zModuleScope.global_scopeNc                 C   sn   |  |}|d k	r|S |d kr4| jd k	r0| jjnd}|d kr\|dkpZ| jd k	oZtj| jjk}| jj|||dS )N   r   )r  r  )r  r  r  r   unicode_literalsZfuture_directivesr   r  )rI   rS   r  r  r   r9   r9   r:   r  4  s    
zModuleScope.lookupc           
      C   s   t |}z| j| }W n( tk
r>   t| }| j|< Y nX |j}| |}|st|}t|D ]\}}|j	d| ||d qd| j
|d d|d||d}	| j|	 |	|_| ||||}||_|S )Nzf%s)rS   rT   rU   _structr+   T)r  rU   r8   )tupler  r   r   Zc_tuple_typer8   r  StructOrUnionScope	enumerater   r  r   removestruct_entryr  r   )
rI   rU   r#  ttyper8   r   r   ix	componentr  r9   r9   r:   r"  A  s0    
     zModuleScope.declare_tuple_typec                 C   s4  t t|sx|tjkrx|tjkrx| jr6| |t|}|S tj	rLt
|d|  nt|d| d | |d t|d}d|_|S tjr| jD ]}|j|kr|  S q|dkr| js| jdS | d d t|d}tjr|tjkrd|_d|_||_tj| |_| j| | j| nd|_||_|  ||_|S )Nr  r   rR   r   globalsZ__Pyx_Globals)r  r  r   Znon_portable_builtins_mapZuncachable_builtinshas_import_starr   r   r
   r  r   r   r   ry   r  r  rS   old_style_globalsr   r  r   r   Zbuiltin_prefixr8   r   r  r   r   r   r_  r9   r9   r:   r2  U  s@    



zModuleScope.declare_builtinr   c           	      C   s   |d k	o|dk}d }d}|d k	rX|dkrX| }| j r6dnd}||krj|rj|j}|d8 }q:n|dkrj| j}d}|  }|jj|||||dS )Nr   Fr   T)from_modulerU   absolute_fallbackZrelative_import)r  r  r   r  find_module)	rI   r  rU   Zrelative_levelZis_relative_importr  r  Z	top_levelZmodule_scoper9   r9   r:   r  z  s(        zModuleScope.find_modulec                 C   sf   d|kr| dd\}}nd }| |}|sPt|| | j|r>dn|d}|| j|< |rb|j||d}|S )Nr   r   T)r  r  r  )
as_package)splitlookup_submoduler  r  r  find_submodule)rI   rS   r  	submoduler   r9   r9   r:   r    s    

zModuleScope.find_submodulec                 C   sF   d|kr| dd\}}nd }| j|d }|rB|d k	rB||}|S )Nr   r   )r  r  re  r  )rI   rS   r  moduler9   r9   r:   r    s    
zModuleScope.lookup_submodulec                 C   s   t j|||d}| | dS )z
        Add `filename` as include file. Add `verbatim_include` as
        verbatim text in the C file.
        Both `filename` and `verbatim_include` can be `None` or empty.
        )r  N)r   r  r  )rI   r  r  r  incr9   r9   r:   r    s    zModuleScope.add_include_filec                 C   s4   |  }|dkr| }|| j| | j| }dS )a  
        Add `inc`, which is an instance of `IncludeCode`, to this
        `ModuleScope`. This either adds a new element to the
        `c_includes` dict or it updates an existing entry.

        In detail: the values of the dict `self.c_includes` are
        instances of `IncludeCode` containing the code to be put in the
        generated C file. The keys of the dict are needed to ensure
        uniqueness in two ways: if an include file is specified in
        multiple "cdef extern" blocks, only one `#include` statement is
        generated. Second, the same include might occur multiple times
        if we find it through multiple "cimport" paths. So we use the
        generated code (of the form `#include "header.h"`) as dict key.

        If verbatim code does not belong to any include file (i.e. it
        was put in a `cdef extern from *` block), then we use a unique
        dict key: namely, the `sortkey()`.

        One `IncludeCode` object can contain multiple pieces of C code:
        one optional "main piece" for the include file and several other
        pieces for the verbatim code. The `IncludeCode.dict_update`
        method merges the pieces of two different `IncludeCode` objects
        if needed.
        N)Z	mainpieceZsortkeyZdict_updater  )rI   r  r   r9   r9   r:   r    s
    zModuleScope.process_includec                 C   sJ   || j krF|j D ]}| | q| j | |j D ]}| | q6d S rF   )r  r  valuesr  r   add_imported_module)rI   r   r  mr9   r9   r:   r    s    

zModuleScope.add_imported_modulec                 C   s6   |j rd|_|| jkr"|| j|< nt|d| d d S )NTr
  r   )r{   r~   r   r   )rI   rS   r   rU   r9   r9   r:   add_imported_entry  s
    
zModuleScope.add_imported_entryc                 C   s\   |  |}|r4|jr"|j|kr"|S |jr.|jrH|S n| |t|}d|_||_| | |S )Nr   )r  r{   r   r   r   r~   r  )rI   rS   r   rU   r   r9   r9   r:   declare_module  s    

zModuleScope.declare_modulerR   c
                 C   sb  |dkrt |d|  | ||	d |sH|tkr4t}|jr@|jrHtd|sz| }
|dksf|dkrl|
rl|}n| tj	|}| 
|}|r|jr|j|s|dkr|jdkrt|d| d ||_|jd	kr| tj	|}|j|kr|}||_|jsd
|_|S tj| |||||||||	d
}|rJd|_|jjr<|jj|_| j| nd|_tjr^d|_|S )N)rR   r   r   z+Module-level variable cannot be declared %s)ztyping.Optionalz7Non-cdef global variable is not a generic Python objectr   r   z6Variable '%s' type does not match previous declarationr   rR   Tr8   rx   r   r)  r*  rt   )r   r.  r   r   is_pyobjectis_extension_typer   r   r   r%  r  r   rT   rD  rx   r   r8   r   r   r   rz   declaration_valuerV   r   r   r{   r
   Zcimport_from_pyxr   )rI   rS   rT   rU   r8   rx   r   r)  r*  rt   r   r   mangled_cnamer9   r9   r:   r     s\    



    

zModuleScope.declare_varr9   c                 C   s   |sd|	krt |dd |sD|dks0|dkr6|r6|}n| tj|}|dkr\|jr\t|d | |}|r|jr|jdkr| tj|}|j	|kr|}||_	||_
tj| |||||||||	|
|d}|S )	Nr0   z+Declarations should not be declared inline.r   r   r   z6Extern functions cannot have default arguments values.rR   )r8   rx   r   r)  r   r+  rn   rN  )r   r   r   rC  Zoptional_arg_countr   r  r   rx   r8   r   r   rQ  )rI   rS   rT   rU   r8   rx   r   r)  r   r+  rn   rN  r   r  r9   r9   r:   rQ  >  s>    




        zModuleScope.declare_cfunctionc                 C   s    |  |}|s| |t| d S rF   )r  r   r   r_  r9   r9   r:   declare_global[  s    
zModuleScope.declare_globalc                 C   s   |d k	r| j | d S rF   )r  r   r  r9   r9   r:   r  `  s    zModuleScope.use_utility_codec                 C   s8   |d krd S |j r | j|j  |jr4| j|j d S rF   )rn   r  r   rq   r?  r9   r9   r:   r  d  s    z"ModuleScope.use_entry_utility_codec                 C   s<  |r.|
dkr.|
dks$|s$t |dd |}d}nd }| |}|r|s|j}|jrV|js\d }n^|j}|r~|rp|jr~| ||| |r|js|r|jr|r||jk	rt	|d |r|js||_|r|rt
j||||
dk|d}||_||_|d k	r||_|
dkr||_n| j|_|	r|	|_n| tj||_| j||||
d|d}d	|_|rX||_n&|jsr| tj||_nt	|jd
 | | | j| |js|s|r4t|| |
|d}| j |_|r|jr| |j |!| | j"| n>|r|jjrt	|d|  n|r4|jj#r4t	|d|  |r@d|_$|rL||_|
dkrv|j%|
krvt	|d||j%f  |rd|_&|r|jr|j|krt	|d ||_|r|j'r|j'|krt	|d ||_'| j(drd	|j_)| j(d}|r ddl*m+} t,| |s t	|d |dkr2d	|j_-d	|_.|S )Nr   r   z6ctypedef only valid for 'extern' , 'public', and 'api'r   r   r  )
check_size)rx   r   r   Tz5Object name required for 'public' or 'extern' C class)rS   r   rx   rW  zC class '%s' already definedz C class '%s' already implementedr   rR   z&Class '%s' previously declared as '%s'z4Object struct name differs from previous declarationz2Type object name differs from previous declarationfinalcollection_type)NonManglingModuleScopez2'collection_type' is not a public cython directivesequence)/r   r  rT   r   r  r   definedr  r  r   r   ZPyExtensionTyperU   buffer_defaultsobjtypedef_cnamer  r   r  r   r   Ztypeptr_prefixr  r   r  r   Zobjstruct_prefixattach_var_entry_to_c_classr   r   r  r&  copydeclare_inherited_c_attributesr  r   implementedr   rx   r   typeobj_cnamere  r  ro   r  rb   Zhas_sequence_flagr   )rI   rS   rU   r   Zimplementingr  r  r  r  r  rx   r  r   r  r  r   r  r   rT   r   r  r  r9   r9   r:   declare_c_classl  s    



    
 





zModuleScope.declare_c_classc                 C   s   |j }|jr*|jjr*dtj|jjf |_nV|jr|jjrt|jj}|j}|rx|jrx|t|jjkrfqx|jrpd S |j}qJtj|_|jr| 	tj
|j|_| 	tj|j|_d S r   )rT   r  vtabslot_cnamer   obj_base_cnamer   r   rh   is_builtin_typer   Zvtabstruct_prefixrS   Zvtabstruct_cnameZvtabptr_prefixZvtabptr_cname)rI   r   rT   Zentry_countr  r9   r9   r:   allocate_vtable_names  s&     
z!ModuleScope.allocate_vtable_namesc                 C   s*   | j D ]}|jjst|jd|j  qd S )N(C class '%s' is declared but not defined)r   rT   r   r   rU   rS   r?  r9   r9   r:   check_c_classes_pxd  s    
zModuleScope.check_c_classes_pxdc                 C   s   |j }|j}|j}|js(t|jd|  |dkrF|jsF| tj	||_|jrx|jj
D ]"}|jsT|jsTt|jd|j  qT|jr| tj|j|_d S )Nr  r   z)C method '%s' is declared but not defined)rT   rS   rx   r   r   rU   r  r   r   Ztypeobj_prefixr   r   r   r  Zvtable_prefixZvtable_cname)rI   r   rT   rS   rx   Zmethod_entryr9   r9   r:   check_c_class  s    zModuleScope.check_c_classc                 C   s^   d}|rt d| j  | jD ]<}|rNt d|j|f  t d|j t d|j | | qd S )Nr   z&Scope.check_c_classes: checking scope z...entry %s %sz......type = z......visibility = )printr   r   rS   rT   rx   r  )rI   Zdebug_check_c_classesr   r9   r9   r:   check_c_classes*  s    
zModuleScope.check_c_classesc                 C   sT   | j  D ]D\}}|jr
|jr
|j| kr
|jdkr
|js
|js
t|j	d|  q
d S )Nr   rB  )
r   r   r   r   r   rx   r   r   r   rU   rf  r9   r9   r:   check_c_functionsB  s    zModuleScope.check_c_functionsc                 C   sR   ddl m} t|j|j|j|jjd}|j|_d|_	d|_
d|_|j|_||_d S )Nr   r  r  )rQ   rn  rP   rS   Z	type_typerU   rT   r  r   r~   rz   r   r   r   )rI   r   rn  rP  r9   r9   r:   r  N  s    z'ModuleScope.attach_var_entry_to_c_classc                 C   s   | j S rF   )cpprK   r9   r9   r:   r   a  s    zModuleScope.is_cppc                 C   s   ddl m} | |  d S )Nr   )PyObjectTypeInferer)r  r  r  )rI   r  r9   r9   r:   r  d  s    zModuleScope.infer_types)F)NN)r   )F)NF)NrR   FFFN)NrR   r   r   r   r9   NF)r   r   NNNNNrR   r   r   NNr   )$rM   rN   rO   r  r  Zis_cython_builtinr  r   rJ   r   r   r  r"  r2  r  r  r  r  r  r  r  r  r   rQ  r  r  r  r  r   r  r  r  r  r  r   r  r9   r9   r9   r:   r    s          

%


		          
:              
                   
zr  c                   @   sd   e Zd ZdZdZdZd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S )r   TFNc                 C   s    |d kr|}t | ||| d S rF   r   rJ   )rI   rS   r   r   r9   r9   r:   rJ   r  s    zLocalScope.__init__c                 C   s   t || S rF   )rD   r   r9   r9   r:   r   w  s    zLocalScope.manglec                 C   sR   |  |}| tj|}| ||||d}d|_|jr<d|_d|_| j	
| |S )NrR   r   0)r   r   r   r%  r   r~   r  rV   r   r   r   )rI   rS   rT   rU   r8   r   r9   r9   r:   declare_argz  s    
zLocalScope.declare_argrR   c
                 C   sn   |  |}|dkr t|d|  tj| |||||||||	d
}
|
jjrP|
jj|
_d|
_| j|
_	| j
|
 |
S )Nr   readonlyz$Local variable cannot be declared %sr  r   )r   r   r   r   rT   r  rV   r   _in_with_gil_blockr   r   r   r(  r9   r9   r:   r     s"    
    
zLocalScope.declare_varc                 C   s4   |  |rt|dd n|  |}|| j|< d S Nr
  r   r  r   r   ri  r   r_  r9   r9   r:   r    s    
zLocalScope.declare_globalc                 C   s`   |  |}|r6|j| kr6|js6t|d|  |  n&| |}|d ksN|js\t|d|  d S Nz'%s' redeclared as nonlocalz"no binding for nonlocal '%s' found)r  r   r   r   r^   r  rI   rS   rU   Z
orig_entryr   r9   r9   r:   declare_nonlocal  s    


zLocalScope.declare_nonlocalc                 C   s$   d|_ t|| }d|_|| j|< |S NT)r   r   r~   r   )rI   rS   r   Zinner_entryr9   r9   r:   _create_inner_entry_for_closure  s
    

z*LocalScope._create_inner_entry_for_closurec                 C   sZ   t | |}|d k	rV|j}|jr(|j}q|| k	rV|jrVt|jdrJtd| ||S |S )NZscope_classz#lookup() after scope class created.)	r   r  r   r  r   r  r  r   r  )rI   rS   r   Zentry_scoper9   r9   r:   r    s    zLocalScope.lookupc                 C   s   |   D ]}|j D ]}|jrh|jj}| jr6||_q|tj	rX|t
tj	d d  }d||f |_q|jr|j|_dtj	|jf |_|jjr|jjd r|  qqd S )Nr   z%s->%sr$  )r   r   r  r   r   r8   r  r4   r   Zcur_scope_cnamerh   r   Zoriginal_cnamerT   rk   r   r&  rr   )rI   Zouter_scope_cnamer   r   r8   r9   r9   r:   mangle_closure_cnames  s    z LocalScope.mangle_closure_cnames)N)NrR   FFFN)rM   rN   rO   r  Zhas_with_gil_blockr  rJ   r   r  r   r  r  r  r  r  r9   r9   r9   r:   r   i  s$   
          
r   c                   @   sX   e Zd ZdZdZdd Zdd Zdd
dZdd ZdddZ	dd Z
dd Zdd ZdS )ComprehensionScopezScope for comprehensions (but not generator expressions, which use ClosureScope).
    As opposed to generators, these can be easily inlined in some cases, so all
    we really need is a scope that holds the loop variable(s).
    Tc                 C   s   |}|j r|j}q| tj}t| ||| |j| _dtj	t
||f | _|j sb|jsb|jrj|j}qP|j| _|j|  d S )Nz%s%d%s)r  r   r   r   r   Zgenexpr_id_refr   rJ   r&  r5   rh   genexp_prefixr  r  r   r   r   add)rI   r   r   rS   r9   r9   r:   rJ     s    zComprehensionScope.__init__c                 C   s   d| j | j||f S N%s%s)r  r   r   r   r9   r9   r:   r     s    zComprehensionScope.mangleNrR   Fc
                 C   s   |t kr$| j|}
|
r$|
jr$|
j}| ||	 d| j| jt	j
|pJ|  f }| |||||}d|_| jjrzd|_nd|_d|_| j| || j|< |S )Nr  T)r   r   r  r~   rT   r.  r  r   r   r   r%  r   r   r  rz   r   r   r   r   r   )rI   rS   rT   rU   r8   rx   r   r)  r*  rt   r   r   r9   r9   r:   r     s    
"
zComprehensionScope.declare_varc                 C   s   | j |||S rF   )r   r   r0  r9   r9   r:   r1    s    z7ComprehensionScope.declare_assignment_expression_targetc                 C   s   | j |||S rF   )r   r:  rI   rS   rU   r9  r9   r9   r:   r:    s
      z%ComprehensionScope.declare_pyfunctionc                 C   s   | j ||S rF   )r   r<  )rI   r   rU   r9   r9   r:   r<    s    z*ComprehensionScope.declare_lambda_functionc                 C   s   | j |S rF   )r   r>  r=  r9   r9   r:   r>    s    z!ComprehensionScope.add_lambda_defc                 C   s   |  |}|s| j|}|S rF   )r  r   rh  rf  r9   r9   r:   rh    s    
z6ComprehensionScope.lookup_assignment_expression_target)NrR   FFTN)F)rM   rN   rO   r   r  rJ   r   r   r1  r:  r<  r>  rh  r9   r9   r9   r:   r    s              

r  c                   @   s,   e Zd ZdZd
ddZdddZdd	 ZdS )ClosureScopeTNc                 C   s$   t | ||| dtj|f | _d S r  )r   rJ   r   Zclosure_scope_prefixZclosure_cname)rI   rS   Z
scope_namer   r   r9   r9   r:   rJ   !  s    zClosureScope.__init__Fc                 C   s   t j| |||ddS )NrR   r4  )r   r:  r  r9   r9   r:   r:  .  s    zClosureScope.declare_pyfunctionc                 C   s   |  |||S rF   r/  r0  r9   r9   r:   r1  1  s    z1ClosureScope.declare_assignment_expression_target)N)F)rM   rN   rO   r  rJ   r:  r1  r9   r9   r9   r:   r    s   

r  c                   @   s    e Zd ZdZdd Zdd ZdS )GeneratorExpressionScopeTc                 C   s   | j |||}| ||S rF   )r   r   r  )rI   rS   rT   rU   r   r9   r9   r:   r1  8  s    z=GeneratorExpressionScope.declare_assignment_expression_targetc                 C   s.   |  |}|s*| j|}|r*| ||S |S rF   )r  r   rh  r  rf  r9   r9   r:   rh  <  s    
z<GeneratorExpressionScope.lookup_assignment_expression_targetN)rM   rN   rO   r  r1  rh  r9   r9   r9   r:   r  5  s   r  c                	   @   s*   e Zd ZdddZdddZdddZdS )r  ?c                 C   s   t j| |d d d d S )N)r   r   r	  r   r9   r9   r:   rJ   H  s    zStructOrUnionScope.__init__NrR   Fc                 C   s   |s|}|dkrt |}|jr(t|}| ||	 | |||||}d|_| j| |j	rn|
st
|d n.|jr|st
|d n|jr|st
|d|  |S )NrR   r   z/C struct/union member cannot be a Python objectz-C struct/union member cannot be a memory viewz;C struct/union member cannot be reference-counted type '%s')r;   r   r   r  r.  r   r~   r   r   r  r   r   Zneeds_refcounting)rI   rS   rT   rU   r8   rx   r   r)  r*  rt   Zallow_pyobjectZallow_memoryviewZallow_refcountedr   r9   r9   r:   r   K  s(    
zStructOrUnionScope.declare_varr   r9   c                 C   s"   |
rt |d | j|||||dS )Nz0C struct/union member cannot be declared 'cpdef'r7  )r   r   )rI   rS   rT   rU   r8   rx   r   r)  r   r+  rN  r9   r9   r:   rQ  f  s    

 z$StructOrUnionScope.declare_cfunction)r  )	NrR   FFFNFFF)NrR   r   r   r   r9   F)rM   rN   rO   rJ   r   rQ  r9   r9   r9   r:   r  E  s&   
               
            r  c                   @   s,   e Zd ZddgZdd Zdd Zdd Zd	S )

ClassScoperN   rO   c                 C   sJ   |r|  dr|S |rF|drF|dsFtd| jd|f }|S )NZ__pyx_r1   z_%s%s_)lowerr4   endswithr	   
class_namelstripr   r9   r9   r:   r   z  s
    z$ClassScope.mangle_class_private_namec                 C   s    t | ||| || _d | _d S rF   )r   rJ   r$  r   rI   rS   r   r9   r9   r:   rJ     s    zClassScope.__init__c                 C   sd   t | |}|r|S |dkr`tddtttdtd gdd}tj	dd|_
| | d|_|S )NclassmethodZ__Pyx_Method_ClassMethodrQ   r   ZClassMethodzCythonFunction.cr   )r   r  rP   r   rU  r   rV  r   ro   rp   rq   r  r   rf  r9   r9   r:   r    s$      
zClassScope.lookupN)rM   rN   rO   r   r   rJ   r  r9   r9   r9   r:   r   o  s   	r   c                   @   s2   e Zd ZdZdddZdd Zd	d
 Zdd ZdS )PyClassScoper   NrR   Fc
                 C   sD   |  |}|tkrt}tj| |||||||||	d
}
d|
_d|
_|
S )Nr  r   )r   r   r   r   r   r{   r}   r(  r9   r9   r:   r     s    
    zPyClassScope.declare_varc                 C   sf   |  |}|r6|j| kr6|js6t|d|  |  n,| |}|d krXt|d|  n
|| j|< d S r  )r  r   r   r   r^   r  r   r  r9   r9   r:   r    s    


zPyClassScope.declare_nonlocalc                 C   s4   |  |rt|dd n|  |}|| j|< d S r  r  r_  r9   r9   r:   r    s    
zPyClassScope.declare_globalc                 C   s   | j |S rF   )r   add_default_value)rI   rT   r9   r9   r:   r)    s    zPyClassScope.add_default_value)NrR   FFFN)rM   rN   rO   r  r   r  r  r)  r9   r9   r9   r:   r(    s             
r(  c                   @   s   e 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d'ddZd(ddZd)ddZdd Zd*ddZd+ddZd,ddZd-d d!Zd.d#d$Zd%d& ZdS )/r  r   Fc                 C   sj   t | || |dkr6|tj|| _|tj|| _g | _g | _	|| _
|jsT|jrf|jrfd|j | _d S )Nr   z(PyObject *)%s)r   rJ   r   r   Zmethtab_prefixr  Zgstab_prefixZgetset_table_cnameproperty_entriesinherited_var_entriesrW  r  r  r  r  )rI   rS   r   rx   rW  r9   r9   r:   rJ     s    zCClassScope.__init__c                 C   sN   | j r| jddsdS | jj}|r8|jd k	r8|j S | jjrJ| jj S dS )NZno_gcFT)	has_cyclic_pyobject_attrsr&  re  rW  r  r   needs_gcr  is_gc_simple)rI   r  r9   r9   r:   r-    s    

zCClassScope.needs_gcc                 C   sN   | j d}|dkrdS |r&| jr&dS | jj}|rF|jd k	rF|j S | jjS )NZtrashcanFT)r&  re  r,  rW  r  r   needs_trashcanZbuiltin_trashcan)rI   	directiver  r9   r9   r:   r/  	  s    

zCClassScope.needs_trashcanc                 C   s   |   o| jdd S )z
        Do we need to generate an implementation for the tp_clear slot? Can
        be disabled to keep references for the __dealloc__ cleanup function.
        Zno_gc_clearF)r-  r&  re  rK   r9   r9   r:   needs_tp_clear	  s    zCClassScope.needs_tp_clearc                 C   sV   | }|rR| d}|r |jr dS |jjs6|jr6|jjr:dS |jj}|rL|jnd}qdS )z
        This covers cases where we definitely have a __del__ function
        and also cases where one of the base classes could have a __del__
        function but we don't know.
        __del__TNF)r  r   rW  Z	is_externr  Zmultiple_basesr  r   )rI   Zcurrent_type_scopeZ	del_entryZcurrent_base_typer9   r9   r:   may_have_finalize	  s    

zCClassScope.may_have_finalizeTc                 C   s   g }g }g }| j D ]b}|jjrJ|s2| js2|jdkrt|s>|jjst|| q|jtjkrb|| q|jj	r|| q|p|p|}||||ffS )Nr2   )
r   rT   r  is_closure_class_scoperS   r.  r   r   Zc_py_buffer_typer   )rI   Zinclude_weakrefZinclude_gc_simpleZpy_attrsZ
py_buffersZmemoryview_slicesr   Zhave_entriesr9   r9   r:   get_refcounted_entries-	  s    
z"CClassScope.get_refcounted_entriesNrR   c
                 C   s6  |  |}|	rXd|	kr@d}|js@|js:t|d|  t}n|j}d|	krX| jsXt|d |r| jrnt|d | jst	| j
|rt|d|  |s|}|dkrt|}t|tj}| |||||}
d	|
_| j|
 |	|
_|jr|d
kr| j
d r|
  n
|| |jr"d| _nb|jrB| td d| _nB|jr| js\|dkrd| _|j r~|j!r~|j!" rd| _#|dkrt|d|  |dkrd|
_$| js|dkrt|d |js|%| st|d|  nd|
_$|
S |t&krt}t'j(| |||||||||	d
}
d	|
_)d	|
_*|
S d S )Nztyping.ClassVarr   zZClassVar[] requires the type to be a Python object type. Found '%s', using object instead.zdataclasses.InitVarzIUse of cython.dataclasses.InitVar does not make sense outside a dataclasszVC attributes cannot be added in implementation part of extension type defined in a pxdz/The name '%s' is reserved for a special method.rR   r   r   r$  Tz#include <new>r2   )rR   r   r  z1Attribute of extension type cannot be declared %sr  z9Special attribute __weakref__ cannot be exposed to Pythonz7C attribute of type '%s' cannot be accessed from PythonFr  )+r   r  Zequivalent_typer   r   r  r   r  r4  r   r&  get_special_method_signaturer;   rD   r   Zunicode_structmember_prefixr   r~   r   r   rt   rk   rr   r'  r   has_memoryview_attrsZneeds_cpp_constructionr  r   ro   has_cpp_constructable_attrshas_pyobject_attrsr  r   r-  r,  r   Zcan_coerce_to_pyobjectr   r   r   r|   r{   r(  r9   r9   r:   r   @	  s    







    zCClassScope.declare_varc                 C   s   |t kr"| drLt|d|  n*|dkrLt D ]}| |r.t|d|  q.|dkr^t|d | j|t|dd}t| j|}|r||_d|_	nt
|_d|_	| j| |S )	NZ__richcmp__z$Cannot define both % and __richcmp____new__zv__new__ method of extension type will change semantics in a future version of Pyrex and Cython. Use __cinit__ instead.r   r4  r   r   )r   r  r   r   r   r   r&  r6  r5  r   r   r   r   )rI   rS   rU   r9  r{  r   Zspecial_sigr9   r9   r:   r:  	  s(    



zCClassScope.declare_pyfunctionc                 C   sD   | j s|dkrtd}t| |}|r@|jr@| jjs@| jjs@d S |S )Nr:  Z	__cinit__)r4  r	   r   r  r   rW  r  r  rf  r9   r9   r:   r  	  s    
zCClassScope.lookup_herer   r9   c                 C   sL  |  |}t| j|r,| jjs,t|d |j}|jsv|sHt|d n.| j	|d j
svt|d|d j
|| jf  | |}|d krtt|tj}|r|jst|d|  |  n|r|jrt|d|  |jr|jrt|d n|j|j
dd	r |j|j
jkr |j
|j|_
n|j|j
dd	r|j|j
jkr| jr|s|j|j
ddd
st|d|| jf d t|jdd | j||||d|	d}nt|d t|jd n0| jrt|d|| jf  | ||||||	}|r|  tj!||_|
|_"||_#d|	krd|_$| jj%s:|j$s:| j&drHd|_|j|_'|S )Nz7Special methods must be declared with 'def', not 'cdef'zC method has no self argumentr   zCSelf argument (%s) of C method '%s' does not match parent type (%s)r   z'%s' already definedz'Overriding final methods is not allowedr   )
as_cmethod)r;  Zas_pxd_definitionzCompatible but non-identical C method '%s' not redeclared in definition part of extension type '%s'.  This may cause incorrect vtables to be generated.r   r]   r   )rx   r+  z2Signature not compatible with previous declarationzOC method '%s' not previously declared in definition part of extension type '%s'r0   Tr  )(r   r   r&  r6  rW  r  r   r   rR  Zassignable_fromrT   r  rD   r;   r   Zunicode_vtabentry_prefixr   r^   r   r   r   Zsame_c_signature_asrK  rE  rF  rH  r  Z!same_c_signature_as_resolved_typer   r$  rU   rG  r   rC  rn   r   r   r  re  r   )rI   rS   rT   rU   r8   rx   r   r)  r   r+  rn   rN  r   r   r9   r9   r:   rQ  	  s~    




""   

 zCClassScope.declare_cfunctionc           
   
   C   s4   |  |}tj| |||||||d}	d|	_||	_|	S )Nr\  r   )r  r   rG  r   r   )
rI   rS   rT   rU   r8   rx   r+  r\  r   r   r9   r9   r:   rG  	  s    
       zCClassScope.add_cfunctionc                 C   sT   t |}| j||d |d|d}t||t}||_d|_d|_||_|j|_||_	|S )Nr   )rU   r8   rx   rn   r   )
r	   rQ  rP   r   r   r~   ry   rn   r   r   )rI   rS   rT   r8   rn   r   rP  r9   r9   r:   r  
  s"         z%CClassScope.declare_builtin_cfunctionc                 C   sx   |  |}|d kr0| |||d kr&tn||d}d|_|d k	rDd|_||_|d krbt|| d|_n||_| j	| |S )NrR   T)class_scope)
r  r   r   r   r   r   PropertyScoper   r*  r   )rI   rS   r   rU   ctypeZproperty_scoper   r9   r9   r:   declare_property
  s    
zCClassScope.declare_propertyr   c              
   C   sT   | j ||||d}|jj|tj|tjd| jddg|||	|
d||||d}||fS )zPInternal convenience method to declare a C property function in one go.
        )r   r?  rU   rI   Nr   )rK  rF  rJ  rI  )rS   rT   r8   rn   rx   rU   )r@  r   rQ  r   rU  rV  rW  )rI   rS   rT   Z
cfunc_namer   rU   rx   rK  rF  rJ  rI  rn   Zproperty_entryr^  r9   r9   r:   declare_cproperty$
  s"    zCClassScope.declare_cpropertyc              
      s2   fdd}|j |j }|D ]@ |  j| j jd d}d|_d|_ j|_| j 	| q|j
d d  D ]  jjrl j  ql|j
D ]  j} j}|o|j}|s||}| j j j j| j jdd}d|_ jrd|_ j|_| j|jks|jr j|_|rd|_||_ jr j|_qd S )Nc                    s   dt j jf S r   )r   r  r8   r7   
base_entryr9   r:   adapt?
  s    z9CClassScope.declare_inherited_c_attributes.<locals>.adaptrR   r   Tr<  )r+  r   r   rS   r8   rT   r~   r   rw   r   r   rY  Z"get_all_specialized_function_typesr   ry   rG  rU   rx   r   r   r   r   r   r   rn   )rI   
base_scoperD  r   r   r8   rP  ry   r9   rB  r:   r  ;
  sZ       

     z*CClassScope.declare_inherited_c_attributes)FT)NrR   FFFN)F)NrR   r   r   r   r9   NF)F)N)NN)NNr   FFNFN)rM   rN   rO   r  r4  r9  r7  r8  r,  r  r  rJ   r-  r/  r1  r3  r5  r   r:  r  rQ  rG  r  r@  rA  r  r9   r9   r9   r:   r    sZ     
          
R
              
A
	

         
r  c                   @   sF   e Zd ZdZdZdZdddZdddZdddZdd Z	dd Z
dS )CppClassScoper   Nc                 C   sR   t | ||d  |j| _g | _|d k	rN|D ]"}| ||t|d d}d|_q*d S )Nr   r   )r   rJ   r&  r+  r   r   ZTemplatePlaceholderTyper   )rI   rS   r   r  TZtemplate_entryr9   r9   r:   rJ   r
  s        zCppClassScope.__init__r   Fc                 C   s   |s|}|  ||
 | |}|	rn|d k	rn|j|rH|j|j|_q|jrb||jrb||_qt|d n| 	|||||}d|_
|jr| jr| j sd| j |f |_|dkr|	s|dkr| j| |S )NrA  r   z%s::%sr  r   )r.  r  rT   rD  rE  rF  r   rH  r   r   r~   Zget_fused_typesZempty_declaration_coder   r   r   )rI   rS   rT   rU   r8   rx   r   r)  r*  r   rt   r   r9   r9   r:   r   |
  s$    

zCppClassScope.declare_varr   r9   c                    s,  | j dd }||dfkrl|d krldtj|f }td}t |_|j|_	dd   fdd	|jD |_n0|d
kr|d krdtj|f }td}t |_|dkr|j
r| jjD ]2}|j|}|r|jj
st|d t|jd q| |}| j||||||d}|r|s| |_|
|_||_|S )Nz::r   rJ   z%s__init__%sr   c                 S   s2   | j jr*| j js*t| jt| j | jS | S d S rF   )rT   rk   Zis_referencer   rV  rS   Z
c_ref_typerU   )rs  r9   r9   r:   	maybe_ref
  s     
 z2CppClassScope.declare_cfunction.<locals>.maybe_refc                    s   g | ]} |qS r9   r9   rr  rH  r9   r:   rt  
  s     z3CppClassScope.declare_cfunction.<locals>.<listcomp>Z__dealloc__z%s__dealloc__%s<del>r   rJ  zdConstructor cannot be called without GIL unless all base constructors can also be called without GILzBase constructor defined here.)r   r8   rx   )rS   r  r   rC  r	   r   Z	CVoidTyperL  r   Zoriginal_argsrK  rT   r  r   r  r   rU   r  r   r`   rW   rn   r   )rI   rS   rT   rU   r8   rx   r   r)  r   r+  rn   rN  r$  baserC  r   r   r9   rI  r:   rQ  
  s:    




 
zCppClassScope.declare_cfunctionc                 C   s  |j }|}t|dd r|j}q
t|dd r<dd |jD }nd}|j|j D ]R}|jdkr\qL|j| jkrn|j | |j|j	|j
d d}d|_d|_| j| qL|jD ]2}| j|j|j
|j|j	|jd	|j|jd
}d|_q|jD ]4}|j|kr| j|j|j
|j|j	|jdd}d|_qd S )Ntemplate_typer  c                 S   s   g | ]
}|j qS r9   r   )r   rG  r9   r9   r:   rt  
  s     zBCppClassScope.declare_inherited_cpp_attributes.<locals>.<listcomp>r9   rK  r   r   r   )r   r+  rn   F)r   )r   r   rM  r  r+  r   rS   r   r   r8   rT   r~   r   r   r   rQ  rU   rx   r   rn   r   r  )rI   r  rE  rM  Zbase_templatesrC  r   r9   r9   r:   r  
  sN    
  
  

  z.CppClassScope.declare_inherited_cpp_attributesc              	   C   s   t | j| j}||_| j D ]}|jrL|j|j|j||j	|j
dd q|jjr| D ](}|j|j|j||j	|j
|jd q\q||j|j||j	|j
|j q|S )Nr   )r   )rn   )rF  rS   r   rT   r   r  r   r  rk  rU   r8   r   r`   rQ  rn   r   rx   )rI   r  Z
type_entryr   r   r   r9   r9   r:   rk  
  s2    



zCppClassScope.specialize)N)Nr   FFFFN)Nr   r   r   r   r9   NF)rM   rN   rO   r   Zdefault_constructorrT   rJ   r   rQ  r  rk  r9   r9   r9   r:   rF  j
  s.   
            
              
&&rF  c                   @   s   e Zd Zdd ZdddZdS )CppScopedEnumScopec                 C   s   t | ||d  d S rF   r	  r&  r9   r9   r:   rJ      s    zCppScopedEnumScope.__init__Nr   c                 C   s0   |s|}|  || | |||||}d|_|S r  )r.  r   r~   )rI   rS   rT   rU   r8   rx   rt   r   r9   r9   r:   r     s    zCppScopedEnumScope.declare_var)Nr   N)rM   rN   rO   rJ   r   r9   r9   r9   r:   rN  
  s
        rN  c                   @   s*   e Zd ZdZdd Zdd Zd
ddZd	S )r>  r   c                 C   s8   |j r| nd }tj| |||d |j| _|j| _d S )Nr   )r   r   r   rJ   rW  r&  )rI   rS   r=  r   r9   r9   r:   rJ     s    zPropertyScope.__init__c                 O   s   |j jrt|d |jr:|jd jtkr:| jj|jd _nFt|jdkrTt|d n,|jd jj	s|jd j| jjkst|d t
j| |||f||}d|_|S )z'Declare a C property function.
        z&C property method cannot return 'void'r   r   z4C property method must have a single (self) argumentz6C property method must have a single (object) argumentT)rL  rM  r   r   rT   r   r   rW  rh   r  r   rQ  r   )rI   rS   rT   rU   r   kwargsr   r9   r9   r:   rQ    s    
"
zPropertyScope.declare_cfunctionFc                 C   s@   t |}|r.| ||t|d}d|_||_|S t|d d S d S )NrR   r   zKOnly __get__, __set__ and __del__ methods allowed in a property declaration)r   r   r   r   r5  r   )rI   rS   rU   r9  r5  r   r9   r9   r:   r:  /  s    
z PropertyScope.declare_pyfunctionN)F)rM   rN   rO   r  rJ   rQ  r:  r9   r9   r9   r:   r>    s   r>  c                   @   s   e Zd ZdddZdd ZdS )CConstOrVolatileScoper   c                 C   s0   t | d|j |j|j || _|| _|| _d S )NZcv_)r   rJ   rS   r   r   base_type_scoper   is_volatile)rI   rQ  r   rR  r9   r9   r:   rJ   ?  s    zCConstOrVolatileScope.__init__c                 C   s<   | j |}|d k	r8t|}t|j| j| j|_|S d S rF   )rQ  r  r  r   Zc_const_or_volatile_typerT   r   rR  rf  r9   r9   r:   r  I  s    
  z!CConstOrVolatileScope.lookup_hereN)r   r   )rM   rN   rO   rJ   r  r9   r9   r9   r:   rP  =  s   

rP  c                   @   s   e Zd Zdd ZdS )TemplateScopec                 C   s   t | ||d  |j| _d S rF   )r   rJ   r&  r&  r9   r9   r:   rJ   S  s    zTemplateScope.__init__N)rM   rN   rO   rJ   r9   r9   r9   r:   rS  R  s   rS  )N)7
__future__r   r   r  r   r  r  ImportErrorZUtilsr   ZErrorsr   r   r   ZStringEncodingr	   rQ   r
   r   r   r   r   Z	TypeSlotsr   r   r   r   r   r   r   r6   r;   rD   objectrE   rP   r   r   r  r  Zconst_counterr  r   r  r  r  r  r   r(  r  rF  rN  r>  rP  rS  r9   r9   r9   r:   <module>   s                                 

 V$      BZ    }mG*-0   ! /