o
    ۾i                     @   sH   d dl mZ d dlmZmZ d dlmZ edddgZG dd dZd	S )
    )
namedtuple)typesir)	signature	BoundFuncfuncsigc                   @   s   e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	i fddZ
d!dejfddZd"dejfddZd#dejfddZdejfddZi fdejfddZdejfddZdejfddZd S )$ParforLoweringBuilderz@Helper class for building Numba-IR and lowering for Parfor.
    c                 C   s   || _ || _|| _d S N)_lowerer_scope_loc)selflowererscopeloc r   W/home/ubuntu/.local/lib/python3.10/site-packages/numba/parfors/parfor_lowering_utils.py__init__   s   
zParforLoweringBuilder.__init__c                 C      | j jS r
   )r   contextr   r   r   r   _context      zParforLoweringBuilder._contextc                 C   r   r
   )r   typing_contextr   r   r   r   
_typingctx   r   z ParforLoweringBuilder._typingctxc                 C   
   | j jjS r
   )r   fndesctypemapr   r   r   r   _typemap      
zParforLoweringBuilder._typemapc                 C   r   r
   )r   r   	calltypesr   r   r   r   
_calltypes   r    z ParforLoweringBuilder._calltypesc           
      C   sR   | j }|j d}|j }| j|||}| jtj|||d||d}	t|	|dS )a9  Binds a global function to a variable.

        Parameters
        ----------
        fobj : object
            The function to be bound.
        ftype : types.Type
        args : Sequence[types.Type]
        kws : Mapping[str, types.Type]

        Returns
        -------
        callable: _CallableNode
        _funcr   rhstypname)r   r   )r   __name__r   resolve_function_typeassignr   Global_CallableNode)
r   fobjftypeargskwsr   varnamegvnamefunc_sigfunc_varr   r   r   bind_global_function"   s   z*ParforLoweringBuilder.bind_global_functionpf_constreturnc                 C   s   | j tj|| jd||dS )a$  Makes a constant variable

        Parameters
        ----------
        cval : object
            The constant value
        typ : types.Type
            type of the value
        name : str
            variable name to store to

        Returns
        -------
        res : ir.Var
        r$   r%   )r+   r   Constr   )r   cvalr'   r(   r   r   r   make_const_variable;   s   z)ParforLoweringBuilder.make_const_variablepf_tuplec                    s>    j } fdd|D }tj|} jtj||||dS )a  Makes a tuple variable

        Parameters
        ----------
        varlist : Sequence[ir.Var]
            Variables containing the values to be stored.
        name : str
            variable name to store to

        Returns
        -------
        res : ir.Var
        c                    s   g | ]} j |j qS r   )r   r(   ).0xr   r   r   
<listcomp>^   s    z=ParforLoweringBuilder.make_tuple_variable.<locals>.<listcomp>r%   )r   r   Tuple
from_typesr+   r   Exprbuild_tuple)r   varlistr(   r   vartystuptyr   r   r   make_tuple_variableO   s   z)ParforLoweringBuilder.make_tuple_variable	pf_assignc                 C   s>   | j }| j||}|| j|j< t|||}| j| |S )a!  Assign a value to a new variable

        Parameters
        ----------
        rhs : object
            The value
        typ : types.Type
            type of the value
        name : str
            variable name to store to

        Returns
        -------
        res : ir.Var
        )	r   r   redefiner   r(   r   Assignr   
lower_instr   r&   r'   r(   r   varr+   r   r   r   r+   d   s   zParforLoweringBuilder.assignc                 C   sD   | j }t| j||}t|||}| j|j| | j	| |S )a@  Assign a value to a new variable or inplace if it already exist

        Parameters
        ----------
        rhs : object
            The value
        typ : types.Type
            type of the value
        name : str
            variable name to store to

        Returns
        -------
        res : ir.Var
        )
r   r   Varr   rJ   r   
setdefaultr(   r   rK   rL   r   r   r   assign_inplace{   s   z$ParforLoweringBuilder.assign_inplacec                 C   s(   t jj|j||| jd}|j| j|< |S )a=  Call a bound callable

        Parameters
        ----------
        callable_node : _CallableNode
            The callee
        args : Sequence[ir.Var]
        kws : Mapping[str, ir.Var]

        Returns
        -------
        res : ir.Expr
            The expression node for the return value of the call
        r$   )r   rB   callr   r   r   r"   )r   callable_noder0   r1   rQ   r   r   r   rQ      s   zParforLoweringBuilder.callc                 C   sZ   | j }| j}tj||||d}ttj||j ||j ||j | jj	j
|< | j| |S )a  Makes a setitem call

        Parameters
        ----------
        obj : ir.Var
            the object being indexed
        index : ir.Var
            the index
        val : ir.Var
            the value to be stored

        Returns
        -------
        res : ir.SetItem
        r$   )r   r   r   SetItemr   r   noner(   r   r   r!   rK   )r   objindexvalr   tmsetitemr   r   r   rY      s   zParforLoweringBuilder.setitemc                 C   s@   | j }tjj||| jd}t|||j ||j | jjj	|< |S )a*  Makes a getitem call

        Parameters
        ----------
        obj : ir.Var
            the object being indexed
        index : ir.Var
            the index
        val : ir.Var
            the ty

        Returns
        -------
        res : ir.Expr
            the retrieved value
        r$   )
r   r   rB   getitemr   r   r(   r   r   r!   )r   rU   rV   r'   rX   rZ   r   r   r   rZ      s   zParforLoweringBuilder.getitemN)r7   )r<   )rH   )r)   
__module____qualname____doc__r   propertyr   r   r   r"   r6   r   rN   r;   rG   r+   rP   rB   rQ   rS   rY   rZ   r   r   r   r   r	   
   s&    



r	   N)	collectionsr   
numba.corer   r   numba.core.typingr   r-   r	   r   r   r   r   <module>   s
    