o
    8ήc                     @   s
  d Z ddlmZmZ 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mZ ddlZddlmZmZmZ ddlmZmZmZ dd	lmZ e
d
dZG dd deZdd Zd=ddZdd Zdd 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&	"d>d#d$Z'G d%d& d&eZ(G d'd( d(e(e"Z)d)d* Z*G d+d, d,eZ+G d-d. d.e(e+Z,G d/d0 d0e,Z-d"e,fd1d2Z.	"d>d3d4Z/d5d6 Z0G d7d8 d8eZ1G d9d: d:eZ2G d;d< d<e2Z3e1 Z4e4j5Z6e4j7Z8e4j9Z:dS )?z
Define typing templates
    )ABCabstractmethodN)
namedtuple)Sequence)
MethodTypeFunctionType)typesutilstargetconfig)TypingErrorInternalErrorInternalTargetMismatchError)InlineOptionsinline_infoz#func_ir typemap calltypes signaturec                   @   s   e Zd ZdZdZd+ddZedd Zedd	 Zed
d 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edd Zdd Zd d! Zd"d# Zd$d% Zd,d'd(Zd)d* ZdS )-	Signatureze
    The signature of a function call or operation, i.e. its argument types
    and return type.
    _return_type_args_recvr_pysigNc                 C   s.   t |tr	t|}|| _|| _|| _|| _d S N)
isinstancelisttupler   r   r   r   )selfreturn_typeargsrecvrpysig r   B/tmp/pip-target-vg8gfxp4/lib/python/numba/core/typing/templates.py__init__&   s   

zSignature.__init__c                 C      | j S r   )r   r   r   r   r    r   .      zSignature.return_typec                 C   r"   r   )r   r#   r   r   r    r   2   r$   zSignature.argsc                 C   r"   r   )r   r#   r   r   r    r   6   r$   zSignature.recvrc                 C   r"   r   )r   r#   r   r   r    r   :   r$   zSignature.pysigc                 K   s0   t | j| j| j| jd}|| tdi |S )znCopy and replace the given attributes provided as keyword arguments.
        Returns an updated copy.
        )r   r   r   r   Nr   )dictr   r   r   r   updater   )r   kwargscurstater   r   r    replace>   s   
zSignature.replacec                 C   s   | j | j| j| jfS ).
        Needed because of __slots__.
        r   r#   r   r   r    __getstate__I   s   zSignature.__getstate__c                 C   s   |\| _ | _| _| _dS )r*   Nr   )r   stater   r   r    __setstate__O   s   zSignature.__setstate__c                 C   s   t | j| jfS r   )hashr   r   r#   r   r   r    __hash__U      zSignature.__hash__c                 C   s>   t |tr| j|jko| j|jko| j|jko| j|jkS d S r   )r   r   r   r   r   r   r   otherr   r   r    __eq__X   s   



zSignature.__eq__c                 C   s
   | |k S r   r   r1   r   r   r    __ne___      
zSignature.__ne__c                 C   s   d| j | jf S )Nz%s -> %s)r   r   r#   r   r   r    __repr__b   r0   zSignature.__repr__c                 C   s
   | j duS )za
        Whether this signature represents a bound method or a regular
        function.
        Nr   r#   r   r   r    	is_methode   s   
zSignature.is_methodc                 C   sl   | j dur| S t| jg| jdd R d| jd i}t| jj dd }|jt	j
|| jjdd}|S )zE
        Convert this signature to a bound method signature.
        N   r   r   )
parametersreturn_annotationr   )r   	signaturer   r   r   r   r:   valuesr)   r	   pySignaturer;   )r   sigparamsr   r   r    	as_methodm   s   
zSignature.as_methodc                 C   s.   | j du r| S t| jg| j f| j R  }|S )zI
        Convert this signature to a regular function signature.
        N)r   r=   r   r   r   r@   r   r   r    as_function   s   
zSignature.as_functionc                 C   s
   t | S )zH
        Convert this signature to a first-class function type.
        )r   r   r#   r   r   r    as_type   s   
zSignature.as_typec                 C   s"   t t| jgttj| jR  S r   )r=   r   	unliteralr   mapr   r#   r   r   r    __unliteral__   s   zSignature.__unliteral__ c                 C   s   |   j}t| dt| j d| d t| d | jD ]
}|j|d d qt| d | jj|d d t| d d S )	NzDUMP z [type code: ]z  Argument types:z  | )tabz  Return type:zEND DUMP)rE   _codeprinttype__name__r   dumpr   )r   rK   car   r   r    rP      s   
 
zSignature.dumpc                 C   s$   | j D ]	}| s dS q| j S )NF)r   
is_preciser   )r   atyper   r   r    rS      s
   

zSignature.is_preciser   )rI   )rO   
__module____qualname____doc__	__slots__r!   propertyr   r   r   r   r)   r+   r-   r/   r3   r4   r6   r8   rB   rD   rE   rH   rP   rS   r   r   r   r    r      s6    





	

r   c                 C   s"   t f}t|t|d}t| ||S )N)keycases)ConcreteTemplater%   r   rN   )namerZ   
signaturesbaseclassesgvarsr   r   r    make_concrete_template   s   ra   c                    s6    fdd}d| f }t f}t| ||d}t|||S )zK
    Create a callable template with the given key and typer function.
    c                    s    S r   r   r#   typerr   r    generic   s   z'make_callable_template.<locals>.genericz%s_CallableTemplate)rZ   rd   r   )CallableTemplater%   rN   )rZ   rc   r   rd   r]   bases
class_dictr   rb   r    make_callable_template   s
   
rh   c                 O   s"   | dd }|r
J t| ||dS )Nr   r7   )popr   )r   r   kwsr   r   r   r    r=      s   r=   c                    s  t |tr	t|}| j}g }| D ]\}}	|	j|	jkr!|| q|r.|dt|  }
n|}
|	 }|rIt
|D ]\}}|t||  ||< q:| j|
i | t
| j D ]b\}}|j}|j}|j|jkr| jv r j| }t|dkrt |d tjtjfrt|d }nd}||||}| j|< qX| jv r||| j|  j|< qX||jusJ |||| j|< qXt fdd| j D }|S )a  
    Given the signature *pysig*, explicit *args* and *kws*, resolve
    omitted arguments and keyword arguments. A tuple of positional
    arguments is returned.
    Various handlers allow to process arguments:
    - normal_handler(index, param, value) is called for normal arguments
    - default_handler(index, param, default) is called for omitted arguments
    - stararg_handler(index, param, values) is called for a "*args" argument
    Nr9   r   r   c                 3   s    | ]	} j |j V  qd S r   )	argumentsr]   ).0parambar   r    	<genexpr>   s    z!fold_arguments.<locals>.<genexpr>)r   r   r%   r:   itemskindKEYWORD_ONLYappendlencopy	enumeratebindr>   r]   defaultVAR_POSITIONALrk   r   StarArgTupleStarArgUniTupler   empty)r   r   rj   normal_handlerdefault_handlerstararg_handlerrA   kwonlyr]   p	bind_argsbind_kwsidxnirm   ry   argvaloutr   rn   r    fold_arguments   sP   




r   c                   @   sX   e Zd ZdZdZdZi Zdd Zdd Zdd Z	e
d	d
 Zedd Zdd ZeZdS )FunctionTemplateTFc                 C   
   || _ d S r   contextr   r   r   r   r    r!     r5   zFunctionTemplate.__init__c                 C   s.   | j | jd}| jj| j|||fi |}|S )N)unsafe_castingexact_match_required)r   r   r   resolve_overloadrZ   )r   r[   r   rj   optionsselectedr   r   r    _select  s   zFunctionTemplate._selectc                 C   s,   t | j}t|tr|jdu sJ |j}|S )y
        Return the key for looking up the implementation for the given
        signature on the target context.
        N)rN   rZ   r   r   im_selfim_func)r   r@   rZ   r   r   r    get_impl_key  s
   

zFunctionTemplate.get_impl_keyc                 C   sN   z	t |\}}W n ty   d}d}Y nw t |}|du r"d}|||fS )aH  
        Gets the source information about function impl.
        Returns:

        code - str: source code as a string
        firstlineno - int: the first line number of the function impl
        path - str: the path to file containing impl

        if any of the above are not available something generic is returned
        z#None available (built from string?)r   Nz<unknown> (built from string?))inspectgetsourcelinesOSErrorgetsourcefile)clsimplcodefirstlinenopathr   r   r    get_source_code_info&  s   

z%FunctionTemplate.get_source_code_infoc                 C   s   dS )a  
        Returns a dictionary with information specific to the template that will
        govern how error messages are displayed to users. The dictionary must
        be of the form:
        info = {
            'kind': "unknown", # str: The kind of template, e.g. "Overload"
            'name': "unknown", # str: The name of the source function
            'sig': "unknown",  # str: The signature(s) of the source function
            'filename': "unknown", # str: The filename of the source function
            'lines': ("start", "end"), # tuple(int, int): The start and
                                         end line of the source function.
            'docstring': "unknown" # str: The docstring of the source function
        }
        Nr   r#   r   r   r    get_template_info<  s   z"FunctionTemplate.get_template_infoc                 C   s8   |   }|d  d|d d  }d| jj d| dS )Nfilename:linesr   < >)r   	__class__rO   )r   infosrcinfor   r   r    __str__N  s   zFunctionTemplate.__str__N)rO   rU   rV   r   r   prefer_literalmetadatar!   r   r   classmethodr   r   r   r   r6   r   r   r   r    r      s    	

r   c                   @       e Zd ZdZdd Zdd ZdS )AbstractTemplatez
    Defines method ``generic(self, args, kws)`` which compute a possible
    signature base on input types.  The signature does not have to match the
    input types. It is compared against the input types afterwards.
    c                 C   s|   t | d}|||}|d urt|tstd|t||s<tdd |D r<dd }tt||}|r7J |||}|S )Nrd   z9generic() must return a Signature or None. {} returned {}c                 s       | ]	}t |tjV  qd S r   r   r   Optionalrl   xr   r   r    rp   i      z)AbstractTemplate.apply.<locals>.<genexpr>c                 S      t | tjr	| jS | S r   r   r   r   rN   r   r   r   r    
unpack_optj     z*AbstractTemplate.apply.<locals>.unpack_opt)	getattrr   r   AssertionErrorformatrN   anyr   rG   )r   r   rj   rd   r@   r   r   r   r    apply]  s   



zAbstractTemplate.applyc                 C   sz   t | d}tjtjtj}| |\}}}tt	|}dt |d|j
|tj||d||t| d f|jd}|S )Nrd   overloadrV   startr9   rr   r]   r@   r   r   	docstring)r   osr   dirnamenumba__file__r   strr	   pysignaturerO   safe_relpathru   rW   r   r   basepathr   r   r   r@   r   r   r   r    r   v  s   
z"AbstractTemplate.get_template_infoNrO   rU   rV   rW   r   r   r   r   r   r    r   V  s    r   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )re   ay  
    Base class for a template defining a ``generic(self)`` method
    returning a callable to be called with the actual ``*args`` and
    ``**kwargs`` representing the call signature.  The callable has
    to return a return type, a full signature, or None.  The signature
    does not have to match the input types. It is compared against the
    input types afterwards.
    Nc              
   C   s  t | d}| }t|}z
|j|i | W n ty* } ztt||d }~ww ||i |}|d u rWtdd |D rQdd }tt	||}||i |}|d u rWd S z|j
}	W n tyj   t|}	Y nw |	j|i |}
|
jrztdt|tst|tjstd|f t|g|
jR  }| jd ur|j| jd}t|
jt|	jk rt|	j d t|
j }|	j|d	}	|j|	d
}|g}| ||
j|
jS )Nrd   c                 s   r   r   r   r   r   r   r    rp     r   z)CallableTemplate.apply.<locals>.<genexpr>c                 S   r   r   r   r   r   r   r    r     r   z*CallableTemplate.apply.<locals>.unpack_optzunsupported call signaturez1invalid return type for callable template: got %rr7   r:   r<   )r   r   r=   rx   	TypeErrorr   r   r   r   rG   r   AttributeErrorr	   r   r'   r   r   r   Typer   r   r)   ru   r:   r>   r   )r   r   rj   rd   rc   	match_siger@   r   r   boundr:   r[   r   r   r    r     sN   




zCallableTemplate.applyc                 C   s   t | d}tjtjtj}| |\}}}tt	|}dt | j
dt |d|j|tj||d||t| d f|jd}|S )Nrd   r   rO   rV   r   r9   r   )r   r   r   r   r   r   r   r   r	   r   rZ   rO   r   ru   rW   r   r   r   r    r     s   
	z"CallableTemplate.get_template_info)rO   rU   rV   rW   r   r   r   r   r   r   r    re     s
    8re   c                   @   r   )r\   zg
    Defines attributes "cases" as a list of signature to match against the
    given input types.
    c                 C   s   t | d}| |||S )Nr[   )r   r   )r   r   rj   r[   r   r   r    r     s   
zConcreteTemplate.applyc                 C   sR   dd l }t| jdd}t||d }d}|d ur| j|u rd}||ddddd}|S )Nr   rO   unknownzType restricted functionzoperator overload)r   r   r   )operatorr   rZ   )r   r   r]   op_funcrr   r   r   r   r    r     s   
z"ConcreteTemplate.get_template_infoNr   r   r   r   r    r\     s    r\   c                       s   e Zd Z fddZ  ZS )_EmptyImplementationEntryc                    s   t t| d| d S )Nz_EmptyImplementationEntry({!r}))superr   r!   r   )r   reasonr   r   r    r!     s   
z"_EmptyImplementationEntry.__init__)rO   rU   rV   r!   __classcell__r   r   r   r    r     s    r   c                   @   sT   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
dd Zdd ZdS )_OverloadFunctionTemplatez;
    A base class of templates for overload functions.
    c                 C   s  t |}t |}dd }||\}}}||\}	}
}d}|||f }d}|}|	}|r9|s8d||f }t|n2|rk|	d |	| }z|d ||d d  }W n tyj   d}||||d f  }t|w d	d
 }||krd||||f }t|| dd |D }dd |
D }||krd}||||||
f  }t|dd |D dd |
D k}|sd}||||||
f  }t|d S )Nc                 S   s|   g }g }d }| j  D ]-}|jtjjkr3|| |jtjjkr#|}q|jtjj	kr2d}t
|| q|| q|||fS )NzXThe use of VAR_KEYWORD (e.g. **kwargs) is unsupported. (offending argument name is '%s'))r:   r>   ry   r	   pyParameterr}   rt   rr   rz   VAR_KEYWORDr   )r@   rj   r   pos_argr   msgr   r   r    get_args_kwargs  s   

zA_OverloadFunctionTemplate._validate_sigs.<locals>.get_args_kwargsz9Typing signature:         %s
Implementation signature: %sz.Typing and implementation arguments differ in zVAR_POSITIONAL (e.g. *args) argument kind (offending argument name is '%s') found in the typing function signature, but is not in the implementing function signature.
%sr9   z)argument names.
%s
First difference: '%s'c                 S   s   t | t |A }d| S )NzDifference: %s)set)typingimplementingdiffr   r   r    gen_diffJ  s   z:_OverloadFunctionTemplate._validate_sigs.<locals>.gen_diffzargument names.
%s
%sc                 S      g | ]}|j qS r   r]   r   r   r   r    
<listcomp>S      z<_OverloadFunctionTemplate._validate_sigs.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r    r   T  r   zkeyword argument names.
%s
%sc                 S   r   r   )ry   r   r   r   r    r   Y  r   z&keyword argument default values.
%s
%s)r	   r   r   index
ValueError)r   typing_func	impl_func
typing_sigimpl_sigr   ty_argsty_kwsty_posim_argsim_kwsim_possig_fmtsig_str
err_prefixrR   br   specializedr   tyimsamer   r   r    _validate_sigs  sT   

z(_OverloadFunctionTemplate._validate_sigsc              	   C   s  ddl m} | ||\}}|du rdS t|}| jjsddlm}m	} ddl
m}	 |j}
| }|
jj}|
jj}|
||ddd|d}|	|||
j||d}|jj}|||\}}}}|j|jjdd}|| j|||d\}}}}| |}tjj|j|_t ||d}d|i| j!|j"< t#d	}|| j$|j"< | jj%s|&| j||}|'|| j$|j"< t(||||}||d
| j!|j"< |S |&| j||}|du rdS |'|| j$|j"< |S )zv
        Type the overloaded function by compiling the appropriate
        implementation for the given args.
        r   )PreLowerStripPhisN)typed_passescompiler)InlineWorkerT)
enable_ssafolded_argszalways inlined)r  iinfo))numba.core.typed_passesr  	_get_implr   
Dispatcher_inlineis_never_inline
numba.corer  r	  numba.core.inline_closurecallr
  	_compilerFlagstargetdescrtyping_contexttarget_contextpipeline_classlocals
dispatcherget_call_templaterun_untyped_passespy_functype_inference_stager   _strip_phi_nodesr   coreir_utilsbuild_definitionsblocks_definitionsr   _inline_overloadsr   r   _compiled_overloadsis_always_inlineget_call_typeget_overload_inline_info)r   r   rj   r  dispnew_args	disp_typer  r	  r
  fcompflagstyctxtgctxcompiler_instinline_workerresolvetemplater   r  irtypemapr   	calltypes_r@   	impl_initr  r   r   r    rd   _  sd   

	z!_OverloadFunctionTemplate.genericc                 C   sj   t j }| jt|t| |f}z| j| \}}||fW S  ty'   Y nw | |||\}}||fS )zGet implementation given the argument types.

        Returning a Dispatcher object.  The Dispatcher object is cached
        internally in `self._impl_cache`.
        )	r
   ConfigStacktop_or_noner   r   rq   _impl_cacheKeyError_build_impl)r   r   rj   r1  	cache_keyr   r   r   r    r    s   

z#_OverloadFunctionTemplate._get_implc           	         s   | j dd}|du rddlm   fdd}n7ddlm}m}m} ||d}|du rO||d}|du r?d}t|	||| j
}t||sKd	}|| }|du rWtd
|S )z4Gets a jit decorator suitable for the current targettargetNr   jitc                     s    | ddi|S )NnopythonTr   )r   r'   rD  r   r    <lambda>  r   z>_OverloadFunctionTemplate._get_jit_decorator.<locals>.<lambda>)target_registryget_local_targetjit_registry)z!Unknown target '{}', has it been zregistered?z0No overloads exist for the requested target: {}.z$Cannot find a suitable jit decorator)r   getr   rE  numba.core.target_extensionrH  rI  rJ  r   r   r   
issubclass)	r   
jitter_strjitterrH  rI  rJ  target_classr   	target_hwr   rD  r    _get_jit_decorator  s$   

z,_OverloadFunctionTemplate._get_jit_decoratorc              
   C   s  |   }t| j}z
|j|i | W n ty' } ztt||d}~ww | j|i |}|du r;d| j|< dS t	|t
rL|\}}	|j}i }d}n|}	t	|	ts\d}
t|
|	| jrf| | j|	 |di | j}||	}t|}|| j|| |dur||f| j|< ||fS )aN  Build and cache the implementation.

        Given the positional (`args`) and keyword arguments (`kws`), obtains
        the `overload` implementation and wrap it in a Dispatcher object.
        The expected argument types are returned for use by type-inference.
        The expected argument types are only different from the given argument
        types if there is an imprecise type in the given argument types.

        Parameters
        ----------
        cache_key : hashable
            The key used for caching the implementation.
        args : Tuple[Type]
            Types of positional argument.
        kws : Dict[Type]
            Types of keyword argument.

        Returns
        -------
        disp, args :
            On success, returns `(Dispatcher, Tuple[Type])`.
            On failure, returns `(None, None)`.

        NNNzOImplementation function returned by `@overload` has an unexpected type.  Got {}r   )rR  r   r=   _overload_funcrx   r   r   r   r?  r   r   r   r   r   r   _strictr  _jit_optionsr   r  r*  r   )r   rB  r   rj   rO  ov_sigr   
ovf_resultr@   pyfuncr   jitdecorr-  r/  r   r   r    rA    s<   



z%_OverloadFunctionTemplate._build_implc                 C      | j |j S r   )r(  r   rC   r   r   r    r   =     z&_OverloadFunctionTemplate.get_impl_keyc                 C   v   t jt jtj}| j}| |\}}}tt	|}dt
|d|j|tj||d||t| d f|jd}|S )a  Return a dictionary with information about the source code of the
        implementation.

        Returns
        -------
        info : dict
            - "kind" : str
                The implementation kind.
            - "name" : str
                The name of the function that provided the definition.
            - "sig" : str
                The formatted signature of the function.
            - "filename" : str
                The name of the source file.
            - "lines": tuple (int, int)
                First and list line number.
            - "docstring": str
                The docstring of the definition.
        r   rV   r   r9   r   r   r   r   r   r   rT  r   r   r	   r   r   rO   r   ru   rW   )r   r   r   r   r   r   r@   r   r   r   r    get_source_infoD  s   z)_OverloadFunctionTemplate.get_source_infoc                 C   r^  )Nr   rV   r   r9   r   r_  r   r   r   r   r   r   r@   r   r   r   r    r   g     z+_OverloadFunctionTemplate.get_template_infoN)rO   rU   rV   rW   r  rd   r  rR  rA  r   r   r`  r   r   r   r   r    r      s    Z[(I
"r   Fc                 K   sX   t | dt| }d|f }t}	t| t|i i ||tt|i ||d
}
t|	||	f|
S )z
    Make a template class for function *func* overloaded by *overload_func*.
    Compiler options are passed as a dictionary to *jit_options*.
    rO   zOverloadTemplate_%s)
rZ   rT  r?  r(  rV  rU  r  r'  r   r   )r   r   r   r%   staticmethodr   rN   )funcoverload_funcjit_optionsstrictinliner   r'   	func_namer]   basedctr   r   r    make_overload_templatew  s   

rl  c                   @   s   e Zd ZdZdd ZdS )_TemplateTargetHelperMixinzDMixin for helper methods that assist with target/registry resolutionc           
      C   sl   ddl m}m} | jdd}|| j||}|| }|jj}|  t	|j
v r+t	}|S t|j
}	t|	}|S )a  Returns the registry for the current target.

        Parameters
        ----------
        reason: str
            Reason for the resolution. Expects a noun.
        Returns
        -------
        reg : a registry suitable for the current target.
        r   )_get_local_target_checkeddispatcher_registryrC  rd   )rL  rn  ro  r   rK  r   r  r  refreshbuiltin_registry_registriesiternext)
r   r   rn  ro  hwstrrQ  r-  tgtctxreg
registriesr   r   r    _get_target_registry  s   

z/_TemplateTargetHelperMixin._get_target_registryN)rO   rU   rV   rW   ry  r   r   r   r    rm    s    rm  c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_IntrinsicTemplatez<
    A base class of templates for intrinsic definition
    c           
      C   s   |  dj}| j|t| f}z| j| W S  ty   Y nw | j| jg|R i |}|du r3dS |\}}t	| j}t
|j dd }	|j|j|	dd}|| j|< || j|j< ||g|jR  | |S )z6
        Type the intrinsic by the arguments.
        	intrinsicNr9   r   r<   )ry  lowerr   r   rq   r?  r@  _definition_funcr	   r   r   r:   r>   r)   _overload_cacher   )
r   r   rj   lower_builtinrB  resultr@   impr   r:   r   r   r    rd     s$   
z_IntrinsicTemplate.genericc                 C   r[  r\  )r~  r   rC   r   r   r    r     r]  z_IntrinsicTemplate.get_impl_keyc                 C   r^  )Nr{  rV   r   r9   r   )r   r   r   r   r   r}  r   r   r	   r   r   rO   r   ru   rW   ra  r   r   r    r     rb  z$_IntrinsicTemplate.get_template_infoN)rO   rU   rV   rW   rd   r   r   r   r   r   r    rz    s
    rz  c                 C   s4   t }d| }t| t|i i |d}t|||f|S )z
    Make a template class for a intrinsic handle *handle* defined by the
    function *defn*.  The *name* is used for naming the new template class.
    z_IntrinsicTemplate_%s)rZ   r}  r?  r~  r   )rz  r%   rc  rN   )handledefnr]   r'   rj  rk  r   r   r    make_intrinsic_template  s   
r  c                   @   s(   e Zd Zdd Zdd Zdd ZeZdS )AttributeTemplatec                 C   r   r   r   r   r   r   r    r!     r5   zAttributeTemplate.__init__c                 C   s   |  ||S r   )_resolve)r   valueattrr   r   r    r6    s   zAttributeTemplate.resolvec                 C   sV   t | d| d }|d u r'| j}|tu r"t|tjr | j||S d S |||S ||S )Nz
resolve_%s)r   generic_resolveNotImplementedr   r   Moduler   resolve_module_constants)r   r  r  fnr   r   r    r    s   
zAttributeTemplate._resolveN)rO   rU   rV   r!   r6  r  r  r  r   r   r   r    r    s
    r  c                       sL   e Zd ZdZdZ fddZdd Zdd Zed	d
 Z	edd Z
  ZS )_OverloadAttributeTemplatezF
    A base class of templates for @overload_attribute functions.
    Fc                    s"   t t| | || _|   d S r   )r   r  r!   r   
_init_oncer   r   r   r    r!     s   z#_OverloadAttributeTemplate.__init__c                    s6   t |   j}| dj}| j| fdd}d S )N	attributec                    s<   | j } ||} |||fi }| ||}|||fS r   )r  _get_function_type_get_signatureget_function)r   buildertypr  	typingctxfntyr@   callr   r   r    getattr_impl&  s
   z;_OverloadAttributeTemplate._init_once.<locals>.getattr_impl)rN   _attrry  lower_getattrrZ   )r   r  r  r  r   r  r    r     s
   
z%_OverloadAttributeTemplate._init_oncec                 C   sP   | j |krd S | | j|}| | j||fi }|jD ]	}| j|j q|jS r   )r  r  r   r  	templatesr'  r&   r   )r   r  r  r  r@   r7  r   r   r    r  .  s   

z#_OverloadAttributeTemplate._resolvec                 C   s&   | |||}|jt| jd}|S Nr<   )r*  r)   r	   r   rT  )r   r  r  r   rj   r@   r   r   r    r  8  s   z)_OverloadAttributeTemplate._get_signaturec                 C   s   | | jS r   )resolve_value_typerT  )r   r  r  r   r   r    r  >  s   z-_OverloadAttributeTemplate._get_function_type)rO   rU   rV   rW   r8   r!   r  r  r   r  r  r   r   r   r   r    r    s    

r  c                   @   s$   e Zd ZdZdZdd Zdd ZdS )_OverloadMethodTemplatezC
    A base class of templates for @overload_method functions.
    Tc                    s\    j }z d}W n
 ty   Y dS w |j}| j|f jttj fdd}dS )z.
        Overriding parent definition
        methodc                    sV   |j d }| j} ||} |||j i }| ||}| t|dd |||S )Nr   libsr   )r   r  r  r  r  add_linking_libsr   )r   r  r@   r   r  r  r  r  r#   r   r    method_implW  s   

z7_OverloadMethodTemplate._init_once.<locals>.method_implN)r  ry  r   r|  rZ   r   VarArgAny)r   r  registryr  r  r   r#   r    r  I  s   z"_OverloadMethodTemplate._init_oncec                    s^   j  krd S ttjrjksJ ntjsJ G  fdddt}t|S )Nc                       s@   e Zd Zj fZjZejZjZjZfddZ	dS )z8_OverloadMethodTemplate._resolve.<locals>.MethodTemplatec                    sp   ft | }  j}  j|||}|jt jd}|jD ]	} j	
|j	 q$|d ur6| S d S r  )r   r  r   r  r)   r	   r   rT  r  r'  r&   rB   )r;  r   rj   r  r@   r7  )r   r  r   r    rd   r  s   
z@_OverloadMethodTemplate._resolve.<locals>.MethodTemplate.genericN)
rO   rU   rV   rZ   r  rc  rT  r'  r   rd   r   r  r   r  r   r    MethodTemplatek  s    

r  )r  r   r   TypeRefrZ   r   BoundFunction)r   r  r  r  r   r  r    r  b  s   
z _OverloadMethodTemplate._resolveN)rO   rU   rV   rW   r8   r  r  r   r   r   r    r  C  s
    r  c           
   
   K   sb   t | tjst| tjsJ d| |f }t| |i tt|i t|||d}t|||f|}	|	S )z`
    Make a template class for attribute *attr* of *typ* overloaded by
    *overload_func*.
    zOverloadAttributeTemplate_%s_%s)rZ   r  r?  r  r'  rT  r   r   )r   r   r   rM  r%   rc  r   rN   )
r  r  re  rh  r   rj  r'   r]   rk  objr   r   r     make_overload_attribute_template  s   
r  c                 K   s   t | ||f|t|d|S )z]
    Make a template class for method *attr* of *typ* overloaded by
    *overload_func*.
    )rh  rj  r   )r  r  )r  r  re  rh  r   r'   r   r   r    make_overload_method_template  s   r  c                    s    fdd}|S )aq  
    Wrap an AttributeTemplate resolve_* method to allow it to
    resolve an instance method's signature rather than a instance attribute.
    The wrapped method must return the resolved method's signature
    according to the given self type, args, and keywords.

    It is used thusly:

        class ComplexAttributes(AttributeTemplate):
            @bound_function("complex.conjugate")
            def resolve_conjugate(self, ty, args, kwds):
                return ty

    *template_key* (e.g. "complex.conjugate" above) will be used by the
    target to look up the method's implementation, as a regular function.
    c                    s   t   fdd}|S )Nc                    s&   G  fdddt }t|S )Nc                       s    e Zd ZZ fddZdS )zSbound_function.<locals>.wrapper.<locals>.attribute_resolver.<locals>.MethodTemplatec                    s0    ||}|d ur|j d u r|jd}|S )Nr7   )r   r)   )r;  r   rj   r@   )method_resolverr   r  r   r    rd     s   z[bound_function.<locals>.wrapper.<locals>.attribute_resolver.<locals>.MethodTemplate.genericN)rO   rU   rV   rZ   rd   r   )r  r   template_keyr  r   r    r    s    r  )r   r   r  )r   r  r  )r  r  )r   r  r    attribute_resolver  s   	z;bound_function.<locals>.wrapper.<locals>.attribute_resolver)	functoolswraps)r  r  r  )r  r    wrapper  s   zbound_function.<locals>.wrapperr   )r  r  r   r  r    bound_function  s   r  c                   @   s2   e Zd ZdZdd Zdd Zdd Zdd	d
ZdS )Registryz~
    A registry of typing declarations.  The registry stores such declarations
    for functions, attributes and globals.
    c                 C   s   g | _ g | _g | _d S r   	functions
attributesglobalsr#   r   r   r    r!     s   
zRegistry.__init__c                 C      t |tsJ | j| |S r   )rM  r   r  rt   r   itemr   r   r    register     zRegistry.registerc                 C   r  r   )rM  r  r  rt   r  r   r   r    register_attr  r  zRegistry.register_attrNc                    s   |durdus
J |rJ j |f dS fdd dus%J |d|r/J u rLtjj }t|jurLtd|jjf  fdd}|S )a#  
        Register the typing of a global value.
        Functional usage with a Numba type::
            register_global(value, typ)

        Decorator usage with a template class::
            @register_global(value, typing_key=None)
            class Template:
                ...
        Nc                    sD   G  fddd| }t rt|}ntdj|f | S )Nc                       s   e Zd Z ZdS )z<Registry.register_global.<locals>.decorate.<locals>.TemplateN)rO   rU   rV   rZ   r   
typing_keyr   r    Template  s    r  z%cannot infer type for global value %r)callabler   Functionr   r  rt   )r   r  r  r  )r   valr  r    decorate  s   z*Registry.register_global.<locals>.decorater  z'%r is not globally reachable as '%s.%s'c                    s
    | S r   r   r  )r  r  r   r    	decorator  r5   z+Registry.register_global.<locals>.decorator)	r  rt   ri   sysmodulesrU   r   rO   r   )r   r  r  r'   modr  r   )r  r   r  r  r    register_global  s    zRegistry.register_globalrS  )rO   rU   rV   rW   r!   r  r  r  r   r   r   r    r    s    r  c                   @   r   )BaseRegistryLoadera  
    An incremental loader for a registry.  Each new call to
    new_registrations() will iterate over the not yet seen registrations.

    The reason for this object is multiple:
    - there can be several contexts
    - each context wants to install all registrations
    - registrations can be added after the first installation, so contexts
      must be able to get the "new" installations

    Therefore each context maintains its own loaders for each existing
    registry, without duplicating the registries themselves.
    c                    s   t  fdd| jD | _d S )Nc                 3   s$    | ]}|t t |fV  qd S r   )r	   stream_listr   )rl   r]   r  r   r    rp     s
    
z.BaseRegistryLoader.__init__.<locals>.<genexpr>)r%   registry_items_registrations)r   r  r   r  r    r!     s   zBaseRegistryLoader.__init__c                 c   s     t | j| D ]}|V  qd S r   )rt  r  )r   r]   r  r   r   r    new_registrations  s   z$BaseRegistryLoader.new_registrationsN)rO   rU   rV   rW   r!   r  r   r   r   r    r    s    r  c                   @   s   e Zd ZdZdZdS )RegistryLoaderz6
    An incremental loader for a typing registry.
    r  N)rO   rU   rV   rW   r  r   r   r   r    r     s    r  r   )F);rW   abcr   r   r  r  r   os.pathr   collectionsr   collections.abcr   r   r   r   r   r  r	   r
   numba.core.errorsr   r   r   numba.core.cpu_optionsr   r,  objectr   ra   rh   r=   r   r   r   re   r\   r   r   rl  rm  rz  r  r  r  r  r  r  r  r  r  r  rq  r  inferr  infer_getattrr  infer_globalr   r   r   r    <module>   sf     	
AX1T  z
54.=

$A
