o
    8ήc                     @   s   d dl mZmZmZ d dlmZmZ edG dd deZedG dd deZedG d	d
 d
eZ	edG dd deZ
dS )    )errorsirtypes)register_rewriteRewritezbefore-inferencec                   @       e Zd ZdZdd Zdd ZdS )RewriteConstGetitemsz
    Rewrite IR expressions of the kind `getitem(value=arr, index=$constXX)`
    where `$constXX` is a known constant as
    `static_getitem(value=arr, index=<constant value>)`.
    c              	   C   sf   i  | _ }|| _|jddD ]}|jdkr,z||j}W n
 tjy'   Y qw |||< qt|dkS )Ngetitemopr   )	getitemsblock
find_exprsr   infer_constantindexr   ConstantInferenceErrorlen)selffunc_irr   typemap	calltypesr   exprconst r   I/tmp/pip-target-vg8gfxp4/lib/python/numba/core/rewrites/static_getitem.pymatch   s   

zRewriteConstGetitems.matchc                 C   s   | j  }|  | j jD ]1}t|tjr9|j}|| jv r9| j| }tj	j
|j||j|jd}tj||j|jd}|| q|S )zC
        Rewrite all matching getitems as static_getitems.
        valuer   	index_varlocr   targetr   )r   copyclearbody
isinstancer   Assignr   r   Exprstatic_getitemr   r   r!   append)r   	new_blockinstr   r   new_exprr   r   r   apply   s"   




zRewriteConstGetitems.applyN__name__
__module____qualname____doc__r   r-   r   r   r   r   r          r   zafter-inferencec                   @   r   )RewriteStringLiteralGetitemsz
    Rewrite IR expressions of the kind `getitem(value=arr, index=$XX)`
    where `$XX` is a StringLiteral value as
    `static_getitem(value=arr, index=<literal value>)`.
    c                 C   sf   i  | _ }|| _|| _|jddD ]}|jdkr,||jj }t|tj	r,|j|j
f||< qt|dkS )zh
        Detect all getitem expressions and find which ones have
        string literal indexes
        r	   r
   r   )r   r   r   r   r   r   namer%   r   StringLiteralliteral_valuer   )r   r   r   r   r   r   r   index_tyr   r   r   r   9   s   

z"RewriteStringLiteralGetitems.matchc                 C   s   t | jj| jj}| jjD ];}t|t jrD|j}|| j	v rD| j	| \}}t j
j|j||j|jd}| j| | j|< t j||j|jd}|| q|S )z~
        Rewrite all matching getitems as static_getitems where the index
        is the literal value of the string.
        r   r    )r   Blockr   scoper   r$   r%   r&   r   r   r'   r(   r   r   r!   r)   )r   r*   r+   r   r   lit_valr,   r   r   r   r-   I   s"   


z"RewriteStringLiteralGetitems.applyNr.   r   r   r   r   r4   1   s    r4   c                   @   r   )RewriteStringLiteralSetitemsz
    Rewrite IR expressions of the kind `setitem(value=arr, index=$XX, value=)`
    where `$XX` is a StringLiteral value as
    `static_setitem(value=arr, index=<literal value>, value=)`.
    c                 C   s\   i  | _ }|| _|| _|tjD ]}||jj }t|t	j
r'|j|jf||< qt|dkS )zh
        Detect all setitem expressions and find which ones have
        string literal indexes
        r   )setitemsr   r   
find_instsr   SetItemr   r5   r%   r   r6   r7   r   )r   r   r   r   r   r=   r+   r8   r   r   r   r   g   s   
z"RewriteStringLiteralSetitems.matchc                 C   s   t | jj| jj}| jjD ]1}t|t jr:|| jv r:| j| \}}t j	|j
||j|j|jd}| j| | j|< |}|| q|S )z~
        Rewrite all matching setitems as static_setitems where the index
        is the literal value of the string.
        )r!   r   r   r   r   )r   r9   r   r:   r   r$   r%   r?   r=   StaticSetItemr!   r   r   r   r)   )r   r*   r+   r   r;   new_instr   r   r   r-   v   s   
z"RewriteStringLiteralSetitems.applyNr.   r   r   r   r   r<   _   r3   r<   c                   @   r   )RewriteConstSetitemsz
    Rewrite IR statements of the kind `setitem(target=arr, index=$constXX, ...)`
    where `$constXX` is a known constant as
    `static_setitem(target=arr, index=<constant value>, ...)`.
    c              	   C   s\   i  | _ }|| _|tjD ]}z||j}W n
 tjy"   Y qw |||< qt	|dkS )Nr   )
r=   r   r>   r   r?   r   r   r   r   r   )r   r   r   r   r   r=   r+   r   r   r   r   r      s   

zRewriteConstSetitems.matchc                 C   sh   | j  }|  | j jD ]$}|| jv r,| j| }t|j||j|j	|j
}|| q|| q|S )zC
        Rewrite all matching setitems as static_setitems.
        )r   r"   r#   r$   r=   r   r@   r!   r   r   r   r)   )r   r*   r+   r   rA   r   r   r   r-      s   



zRewriteConstSetitems.applyNr.   r   r   r   r   rB      s    rB   N)
numba.corer   r   r   numba.core.rewritesr   r   r   r4   r<   rB   r   r   r   r   <module>   s    +-+