o
    í@Ëa4  ã                   @   st   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mZ ddlZddlmZ G d	d
„ d
eƒZdS )z9 GlobalEffects computes function effect on global state. é    )ÚAliases)ÚGlobalDeclarations)ÚModuleAnalysis)ÚMODULES)ÚDiGraphN)Úreducec                       s`   e Zd ZdZG dd„ deƒZ‡ fdd„Z‡ fdd„Z‡ fdd	„Zd
d„ Z	dd„ Z
dd„ Z‡  ZS )ÚGlobalEffectsz;Add a flag on each function that updates a global variable.c                   @   s   e Zd Zdd„ ZdS )zGlobalEffects.FunctionEffectc                 C   sœ   || _ t|tjƒrd| _d S t|tjƒr|j| _d S t|tjƒr%d| _d S t|t	ƒr/d| _d S t|tj
ƒr:d| _d S t|tjƒrEd| _d S tt|ƒ|ƒ t‚)NFT)ÚfuncÚ
isinstanceÚastZFunctionDefÚglobal_effectÚ	intrinsicZ	IntrinsicZglobal_effectsÚaliasÚstrÚClassZUnboundValueTypeÚprintÚtypeÚNotImplementedError©ÚselfÚnode© r   úA/usr/lib/python3/dist-packages/pythran/analyses/global_effects.pyÚ__init__   s   





z%GlobalEffects.FunctionEffect.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   ÚFunctionEffect   s    r   c                    s&   t ƒ | _tƒ | _tt| ƒ tt¡ d S ©N)	r   ÚresultÚdictÚnode_to_functioneffectÚsuperr   r   r   r   ©r   ©Ú	__class__r   r   r   %   s   zGlobalEffects.__init__c                    sV   t tˆƒ |¡ ‡ ‡fdd„‰ ˆ ˆjƒ t ¡ D ]}ˆ |ƒ qt tj¡ˆj	tj< dS )z¿
        Initialise globals effects as this analyse is inter-procedural.

        Initialisation done for Pythonic functions and default value set for
        user defined functions.
        c                    s\   |   ¡ D ]'}t|tƒrˆ |ƒ qt |¡}|ˆj|< ˆj |¡ t|tj	ƒr+ˆ |j
ƒ qdS )z4 Recursively save globals effect for all functions. N)Úvaluesr
   r    r   r   r!   r   Zadd_noder   r   Zfields)ÚmoduleÚvZfe©Úregister_noder   r   r   r*   3   s   




€øz,GlobalEffects.prepare.<locals>.register_nodeN)
r"   r   ÚprepareZglobal_declarationsr   r&   r   r   ÚUnboundValuer!   )r   r   r'   r$   r)   r   r+   *   s   


ÿzGlobalEffects.preparec                    sh   t t| ƒ |¡}d}|r)d}|D ]}|jr&| j |¡D ]
}|js%d }|_qq|sdd„ |D ƒ| _| jS )NTFc                 S   s   h | ]}|j r|j’qS r   )r   r	   )Ú.0Úfr   r   r   Ú	<setcomp>O   s    z$GlobalEffects.run.<locals>.<setcomp>)r"   r   Úrunr   r   Zpredecessors)r   r   r   Z
keep_goingÚfunctionZpredr$   r   r   r0   E   s   
€úzGlobalEffects.runc                 C   s*   | j | | _| j| jv sJ ‚|  |¡ d S r   )r!   Úcurrent_functionr   Úgeneric_visitr   r   r   r   Úvisit_FunctionDefR   s   zGlobalEffects.visit_FunctionDefc                 C   s   d| j _d S )NT)r2   r   )r   Ú_r   r   r   Úvisit_PrintW   s   zGlobalEffects.visit_Printc                    sš   ˆ j |j }t‡ fdd„|tƒ ƒ}|D ]2}t|tjƒr0t |jd |jdd … g ¡}ˆ  |¡ q|ˆ j	vr8t
j}ˆ j	| }ˆ j ˆ j|¡ qˆ  |¡ d S )Nc                    s&   | t |tjƒrtˆ j ¡ ƒ S |g S r   )r
   r   ÚNameÚlistr!   Úkeys)ÚxÚyr#   r   r   Ú<lambda>a   s
    
ÿÿz*GlobalEffects.visit_Call.<locals>.<lambda>r   é   )Úaliasesr	   r   r8   r
   r   ZCallÚargsZvisitr!   r   r,   r   Zadd_edger2   r3   )r   r   Zfunc_aliasesZ
func_aliasZ	fake_callr   r#   r   Ú
visit_CallZ   s$   
ûÿ


zGlobalEffects.visit_Call)r   r   r   Ú__doc__Úobjectr   r   r+   r0   r4   r6   r@   Ú__classcell__r   r   r$   r   r      s    r   )rA   Zpythran.analyses.aliasesr   Z$pythran.analyses.global_declarationsr   Zpythran.passmanagerr   Zpythran.tablesr   Zpythran.graphr   Zpythran.intrinsicr   Zgastr   Ú	functoolsr   r   r   r   r   r   Ú<module>   s    