o
    TdR                     @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZ 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 d dlm Z  G dd dej!Z"G dd dej!Z#e$ej%e" e$ej&e# e$ej'ej( dd Z)dZ*dd Z+eG dd de,Z-dd Z.dd Z/dd Z0dd Z1G d d! d!ej2Z3d"d# Z4G d$d% d%e5Z6ej7G d&d' d'ej8Z9e6j:;ej%d(d) Z<e6j:=ej%d*d+ Z>d,d- Z?e6j:@ej'eAejBd.d/ ZCdS )0    N)OrderedDict)Sequence)irnjit)cgutilserrorsimputilstypesutils)default_managermodels)
cpu_target)	templates)as_numba_type)disable_pickling)_boxc                          e Zd Z fddZ  ZS )InstanceModelc                    sP   t |}t dt| }dt |fdt |fg}tt| ||| d S )NzOpaque.meminfodata)	r
   ClassDataTypeOpaquestrMemInfoPointerCPointersuperr   __init__)selfdmmfe_typcls_data_tydtypemembers	__class__ s/home/ncw/WWW/www-new/content/articles/pi-bbp/venv/lib/python3.10/site-packages/numba/experimental/jitclass/base.pyr      s   
zInstanceModel.__init____name__
__module____qualname__r   __classcell__r&   r&   r$   r'   r          r   c                       r   )InstanceDataModelc                    s2   |j }dd |j D }tt| ||| d S )Nc                 S   s   g | ]
\}}t ||fqS r&   )_mangle_attr.0kvr&   r&   r'   
<listcomp>(   s    z.InstanceDataModel.__init__.<locals>.<listcomp>)
class_typestructitemsr   r.   r   )r   r   r    clstyr#   r$   r&   r'   r   &   s   zInstanceDataModel.__init__r(   r&   r&   r$   r'   r.   %   r-   r.   c                 C   s   d|  S )zZ
    Mangle attributes.
    The resulting name does not startswith an underscore '_'.
    m_r&   )namer&   r&   r'   r/   1   s   r/   z3
def ctor({args}):
    return __numba_cls_({args})
c                 C   sP   | j }g }| D ]\}}|j|j@ |jkr|| q	d|j }t||S )zI
    Returns list of positional and keyword argument names in order.
    z(%s argument type unsupported in jitclass)
parametersr7   kindPOSITIONAL_OR_KEYWORDappendr   UnsupportedError)fn_sigparamsargsr2   r3   msgr&   r&   r'   _getargsB   s   

rD   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )JitClassTypez#
    The type of any jitclass.
    c                 C   sX   t |dkr
td|\}t|trtdd|v sJ dt| |||}|  |S )N   z must have exactly one base classzcannot subclass from a jitclassr5   zmissing "class_type" attr)len	TypeError
isinstancerE   type__new__	_set_init)clsr:   basesdctbaseoutclsr&   r&   r'   rK   V   s   
zJitClassType.__new__c                 C   sh   | j jjd }t|}t|dd }|| _tjd	|d}d| i}t
|| |d }t|| _dS )z
        Generate a wrapper for calling the constructor from pure Python.
        Note the wrapper will only accept positional arguments.
        r   rF   N, rB   __numba_cls_ctor)r5   instance_typemethodsr   pysignaturerD   	_ctor_sig_ctor_templateformatjoinexecr   _ctor)rM   initinit_sigrB   ctor_sourceglblsrU   r&   r&   r'   rL   a   s   

zJitClassType._set_initc                 C   s   t |tjr|jj| ju S dS )NF)rI   r   Box_numba_type_r5   )rM   instancer&   r&   r'   __instancecheck__r   s   zJitClassType.__instancecheck__c                 O   s>   | j jd g|R i |}|  | j|jdd  i |jS )NrF   )rY   bindapply_defaultsr^   rB   kwargs)rM   rB   ri   rg   r&   r&   r'   __call__w   s   zJitClassType.__call__N)r)   r*   r+   __doc__rK   rL   rf   rj   r&   r&   r&   r'   rE   Q   s    rE   c                 C   sH   |   D ]\}}t|tstd|f t|tjs!td|f qd S )Nz#spec keys should be strings, got %rz2spec values should be Numba type instances, got %r)r7   rI   r   rH   r
   Type)specr2   r3   r&   r&   r'   _validate_spec   s   
rn   c                 C   sD   t  }| D ]\}}|dr|dsd|  | }|||< q|S )zB
    Apply the same changes to dunder names as CPython would.
    ___)r   r7   
startswithendswith)clsnamerm   outr2   r3   r&   r&   r'   _fix_up_private_attr   s   
ru   c                 C   s"   t |dd}|r| | dS dS )zC
    Add the required libs for the callable to allow inlining.
    libsr&   N)getattradd_linking_libs)contextcallrv   r&   r&   r'   _add_linking_libs   s   r{   c                 C   sl  |du rt  }n	t|trt |}t|  D ]\}}||vr&t|||< qt| t| j	|}i }t
t| D ]}||j q:i i i i f\}}	}
}| D ]'\}}t|tjr`|||< qQt|trj||	|< qQt|trt||
|< qQ|||< qQt|t|	B t|
B t|@ }|rtdd||dd}t| |rd}d| }t|||	 D ]\}}|jdurtd|qdd	 | D }i }|	 D ]\}}i }|jrt|j|d
< |jrt|j|d< |||< qdd	 |
 D }|| t||||}t ||d}|| t!| j	| f|} t"j#}|$| | t"j%}||||&  t&| |j' | S )a$  
    Internal function to create a jitclass.

    Args
    ----
    cls: the original class object (used as the prototype)
    spec: the structural specification contains the field types.
    class_ctor: the numba type to represent the jitclass
    builder: the internal jitclass builder
    Nzname shadowing: {0}rR   rk    z(class members are not yet supported: {0}zdeleter is not supported: {0}c                 S   s   i | ]	\}}|t |qS r&   r   r0   r&   r&   r'   
<dictcomp>   s    z'register_class_type.<locals>.<dictcomp>getsetc                 S   s   i | ]
\}}|t |jqS r&   )r   __func__r0   r&   r&   r'   r}      s    )r5   rk   )(r   rI   r   ptget_type_hintsr7   r   rn   ru   r)   reversedinspectgetmroupdate__dict__pytypesFunctionTypepropertystaticmethodr   	NameErrorr[   r\   pop_drop_ignored_attrskeysrH   fdelfgetr   fsetConstructorTemplatedictrE   r   typing_contextinsert_globaltarget_contextregisterrV   )rM   rm   
class_ctorbuilderattrpy_typeclsdctbaseclsrW   propsstatic_methodsothersr2   r3   shadowed	docstringrC   r#   jit_methods	jit_propsrO   jit_static_methodsr5   jit_class_dct	typingctx	targetctxr&   r&   r'   register_class_type   s   






 


r   c                   @   s   e Zd ZdZdd ZdS )r   z8
    Base class for jitclass constructor templates.
    c           	      C   s|   | j j}|jd }| f| }t|}|| j||}t|j	tj
s.td|j	 dtj|g|jdd  R  }|S )Nr   z$__init__() should return None, not ''rF   )keyrV   r   get_reference_typer
   
Dispatcherget_call_typery   rI   return_typeNoneTyper   NumbaTypeErrorr   	signaturerB   )	r   rB   kwsrV   rU   	boundargs	disp_typesigrt   r&   r&   r'   generic  s   

zConstructorTemplate.genericN)r)   r*   r+   rk   r   r&   r&   r&   r'   r     s    r   c                 C   s   t g d}d| v r|d |  D ] \}}t|tjtjfr&|| qt|dd tu r3|| qd| v rC| d d u rC|d |D ]}| |= qEd S )N)__weakref__r*   r   __annotations____objclass____hash__)	r   addr7   rI   r   BuiltinFunctionTypeBuiltinMethodTyperw   object)rO   dropr2   r3   r&   r&   r'   r     s    


r   c                   @   s@   e Zd ZdZedZe Zdd Z	dd Z
dd Zd	d
 ZdS )ClassBuilderz
    A jitclass builder for a mutable jitclass.  This will register
    typing and implementation hooks to the given typing and target contexts.
    zjitclass builderc                 C   s   || _ || _|| _d S N)r5   r   r   )r   r5   r   r   r&   r&   r'   r   :  s   
zClassBuilder.__init__c                 C   s$   |  | j| jj | j| j dS )z7
        Register to the frontend and backend.
        N)_register_methodsclass_impl_registryr5   rV   r   install_registry)r   r&   r&   r'   r   ?  s   zClassBuilder.registerc                 C   sD   t |jt |j }|D ]}|| jvr| || | j| qdS )z
        Register method implementations.
        This simply registers that the method names are valid methods.  Inside
        of imp() below we retrieve the actual method to run from the type of
        the receiver argument (i.e. self).
        N)listr   r   implemented_methods_implement_methodr   )r   registryrV   to_registermethr&   r&   r'   r   J  s   
zClassBuilder._register_methodsc                    s^    fddfdd}  d}|dv r|| d S |tj ftjttj  d S )Nc                     s    fdd} | S )Nc           	         s   |j d } |jv r|j  }n |jv r+|j  }|j|j dd  d}|dd  }t|}| ||}|||}t| | t	| ||j
|S )Nr   rF   rS   )rB   r   r   replacer
   r   get_functionr{   r	   impl_ret_new_refr   )	ry   r   r   rB   rV   methodr   rz   rt   r   r&   r'   imp_  s   






z<ClassBuilder._implement_method.<locals>.get_imp.<locals>.impr&   )r   r   r&   r'   get_imp^  s   z/ClassBuilder._implement_method.<locals>.get_impc                    sx   d|   t t| }t|G  fdddtj}ttj ftjt	tj
  t|tjt	tj
  d S )Nz__%s__c                       s   e Zd Z fddZdS )zKClassBuilder._implement_method.<locals>._getsetitem_gen.<locals>.GetSetItemc                    sN   |d }t |tjr# |jv r%|j  }t|}|| j||}|S d S d S )Nr   )rI   r
   ClassInstanceTyper   r   r   ry   )r   rB   r   re   r   r   r   _dunder_methr&   r'   r   y  s   

zSClassBuilder._implement_method.<locals>._getsetitem_gen.<locals>.GetSetItem.genericN)r)   r*   r+   r   r&   r   r&   r'   
GetSetItemw  s    r   )rw   operatorr   infer_globalAbstractTemplater	   lower_builtinr
   r   VarArgAny)getsetopr   )r   r   r'   _getsetitem_gens  s    


z7ClassBuilder._implement_method.<locals>._getsetitem_genrp   )getitemsetitem)striplowerr
   r   r   r   )r   r   r   r   dunder_strippedr&   )r   r   r'   r   \  s   

zClassBuilder._implement_methodN)r)   r*   r+   rk   r	   Registryr   r   r   r   r   r   r   r&   r&   r&   r'   r   2  s    
r   c                   @   s   e Zd ZejZdd ZdS )ClassAttributec           	         s    j v r
j   S  jv r-j  }t|G  fdddtj}t|S  jv rPj  }t|G  fdddtj}t|S  jv roj  }|d }t|	j
fi }|jS d S )Nc                       $   e Zd Zj fZfddZdS )z6ClassAttribute.generic_resolve.<locals>.MethodTemplatec                    s&   ft | } | j||}| S r   )tupler   ry   	as_methodr   rB   r   r   r   re   r&   r'   r     s   z>ClassAttribute.generic_resolve.<locals>.MethodTemplate.genericNr)   r*   r+   r   r   r&   r   r   re   r   r&   r'   MethodTemplate      
r   c                       r   )z<ClassAttribute.generic_resolve.<locals>.StaticMethodTemplatec                    s     | j||}|jdS )N)recvr)r   ry   r   r   r   r&   r'   r     s   zDClassAttribute.generic_resolve.<locals>.StaticMethodTemplate.genericNr   r&   r   r&   r'   StaticMethodTemplate  r   r   r~   )r6   r   r
   r   r   r   BoundFunctionr   r   r   ry   r   )	r   re   r   r   r   r   impdctgetterr   r&   r   r'   generic_resolve  s&   










zClassAttribute.generic_resolveN)r)   r*   r+   r
   r   r   r   r&   r&   r&   r'   r     s    r   c              	   C   s   ||j v r*| j|||d}|j}| j|| |d}t| ||j | t|t|S ||j	v rd|j	| d }t
d|}	t|}
|
| j|gi }	| |
|	}|||g}t| | t| ||	j|S td|)z4
    Generic getattr() for @jitclass instances.
    valuerefr~   Nattribute {0!r} not implemented)r6   make_helperr   make_data_helperget_data_typer	   impl_ret_borrowedrw   r/   r   r   r   r
   r   r   r   r   r{   r   r   NotImplementedErrorr[   )ry   r   typr   r   instdata_pointerr   r   r   
dispatcherrz   rt   r&   r&   r'   get_attr_impl  s(   



r  c                 C   s   |j \}}|\}}||jv rI| j|||d}	|	j}
| j|| |
d}|j| }t|t|}t|t|| | j	
||| | j	||| dS ||jv rx|j| d }t|}|| j||fi }| ||}||||f t| | dS td|)z4
    Generic setattr() for @jitclass instances.
    r   r   r   r   N)rB   r6   r   r   r   r   rw   r/   setattrnrtincrefdecrefr   r
   r   r   r   r   r{   r   r[   )ry   r   r   rB   r   r  valtytargetvalr  data_ptrr   	attr_typeoldvaluesetterr   rz   r&   r&   r'   set_attr_impl  s2   




r  c                 C   s   |  tj}|  tj}tt |||g}d|j}t	
|||}|jrXt| }| }	|  |	}
||jd |
 }| j||	|d}| j||	|  |  |S )Nz	_Dtor.{0}r   r   )get_value_typer
   voidptruintpllvmirr   VoidTyper[   r:   r   get_or_insert_functionis_declaration	IRBuilderappend_basic_blockr   bitcastrB   
as_pointerr   r  r	  	_getvalueret_void)ry   modulerV   	llvoidptrllsize
dtor_ftypefnamedtor_fnr   alloc_fe_type
alloc_typeptrr   r&   r&   r'   imp_dtor  s    

r(  c                 C   s   |j }| | }| |}| j|| tj|t| |j	|}| j
||}||| }|t|| | ||}	||	_||	_|j f|j }
|jd }t|}| |tj|
 }t| | |	 gt| }||| |	 }t| |||S )z7
    Generic constructor (__new__) for jitclasses.
    r   )r   r   get_abi_sizeofr  meminfo_alloc_dtorget_constantr
   r  r(  r  meminfo_datar  r  storer   get_null_valuer   r   r   rB   r   r   r   voidr{   r  r   r	   r   )ry   r   r   rB   inst_typr&  
alloc_sizer   r  inst_structr`   r_   r   rz   realargsretr&   r&   r'   	ctor_impl&  s6   




r5  )Dr   r   r
   r   typingr   collectionsr   collections.abcr   llvmliter   r  numbar   
numba.corer   r   r	   r   numba.core.datamodelr   r   numba.core.registryr   numba.core.typingr   numba.core.typing.asnumbatyper   numba.core.serializer   numba.experimental.jitclassr   StructModelr   r.   r   r   r   	ClassTypeOpaqueModelr/   rZ   rD   rJ   rE   rn   ru   r{   r   r   r   r   r   r   infer_getattrAttributeTemplater   r   lower_getattr_genericr  lower_setattr_genericr  r(  r   r   r   r5  r&   r&   r&   r'   <module>   sV    0		bb4

'

