o
    í@Ëa#
  ã                   @   s\   d 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
 ddlZG dd	„ d	eƒZdS )
z9
PureExpressions detects functions without side-effects.
é    )ÚAliases)ÚArgumentEffects)ÚGlobalEffects)ÚModuleAnalysis)Ú	IntrinsicNc                       sD   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡ fd
d„Z‡  Z	S )ÚPureExpressionsz"Yields the set of pure expressionsc                    s    t ƒ | _tt| ƒ ttt¡ d S )N)ÚsetÚresultÚsuperr   Ú__init__r   r   r   ©Úself©Ú	__class__© úC/usr/lib/python3/dist-packages/pythran/analyses/pure_expressions.pyr      s   ÿzPureExpressions.__init__c                 C   s   |j D ]}|  |¡ qdS )NF)ÚbodyÚvisit)r   ÚnodeZstmtr   r   r   Úvisit_FunctionDef   s   
z!PureExpressions.visit_FunctionDefc                    s0   t ‡ fdd„t |¡D ƒƒ}|rˆ j |¡ |S )Nc                    ó   g | ]}ˆ   |¡‘qS r   ©r   )Ú.0Úxr   r   r   Ú
<listcomp>   ó    z1PureExpressions.generic_visit.<locals>.<listcomp>)ÚallÚastZiter_child_nodesr	   Úadd)r   r   Úis_purer   r   r   Úgeneric_visit   s   zPureExpressions.generic_visitc              
      sÜ   t ‡ fdd„|jD ƒƒ}ˆ j|j }|rZ|D ]B}t|tƒr$||j M }n||ˆ jv M }|ˆ jv rVˆ j| }t	|j|ƒD ]\}}|rTzt
 |¡ W q; tyS   d}Y q;w q;qd}qnd}|ˆ  |j¡M }|rlˆ j |¡ |S )Nc                    r   r   r   )r   Úargr   r   r   r   %   r   z.PureExpressions.visit_Call.<locals>.<listcomp>F)r   ÚargsÚaliasesÚfuncÚ
isinstancer   Úglobal_effectsr	   Úargument_effectsÚzipr   Zliteral_evalÚ
ValueErrorr   r   )r   r   r   Zfunc_aliasesZ
func_aliasZfunc_aesr!   Úaer   r   r   Ú
visit_Call#   s2   


ÿýÿîzPureExpressions.visit_Callc                    s.   t tˆ ƒ |¡ ‡ fdd„ˆ j ¡ D ƒˆ _d S )Nc                    s&   h | ]\}}|ˆ j vrt|ƒs|’qS r   )r&   Úany)r   r$   r*   r   r   r   Ú	<setcomp>H   s
    ÿ
ÿz*PureExpressions.prepare.<locals>.<setcomp>)r
   r   Úpreparer'   Úitemsr	   )r   r   r   r   r   r.   F   s   zPureExpressions.prepare)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r+   r.   Ú__classcell__r   r   r   r   r      s    #r   )r3   Zpythran.analyses.aliasesr   Z!pythran.analyses.argument_effectsr   Zpythran.analyses.global_effectsr   Zpythran.passmanagerr   Zpythran.intrinsicr   Zgastr   r   r   r   r   r   Ú<module>   s    