o
    @a                     @   sP   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
ZG dd deZdS )z5 ImportedIds gathers identifiers imported by a node.     )Globals)Locals)NodeAnalysisNc                       s   e Zd ZdZ fddZdd Zdd Zdd	 ZeZeZ	eZ
eZd
d Zdd Zdd Zdd Zdd Zdd ZeZdd Zdd Z fddZ fddZ  ZS )ImportedIdsz9Gather ids referenced by a node and not declared locally.c                    s2   t  | _t  | _d| _d| _tt| tt	 d S )NF)
setresultcurrent_localsis_listin_augassignsuperr   __init__r   r   )self	__class__ ?/usr/lib/python3/dist-packages/pythran/analyses/imported_ids.pyr      s
   zImportedIds.__init__c                 C   sX   t |jtjr| js| j|j d S |j| jvr(|j| jvr*| j	|j d S d S d S N)

isinstancectxastZStorer
   r   addidvisible_globalsr   r   noder   r   r   
visit_Name   s   
zImportedIds.visit_Namec                 C   sR   | j |j | j  }| j dd |jjD  |jD ]}| | q|| _ d S )Nc                 s       | ]}|j V  qd S r   r   .0argr   r   r   	<genexpr>        z0ImportedIds.visit_FunctionDef.<locals>.<genexpr>)r   r   namecopyupdateargsbodyvisit)r   r   r   Zstmtr   r   r   visit_FunctionDef   s   


zImportedIds.visit_FunctionDefc                 C   s6   | j  }|jD ]}| | q| |j || _ d S r   )r   r$   
generatorsr(   elt)r   r   r   	generatorr   r   r   visit_AnyComp%   s
   


zImportedIds.visit_AnyCompc                 C   s2   t | | | |j |jD ]}| | qd S r   )mdr(   valuetargets)r   r   targetr   r   r   visit_Assign1   s
   
zImportedIds.visit_Assignc                 C   s   d| _ | | d| _ d S )NTF)r
   generic_visitr   r   r   r   visit_AugAssign9   s   

zImportedIds.visit_AugAssignc                 C   s:   | j  }| j dd |jjD  | |j || _ d S )Nc                 s   r   r   r   r   r   r   r   r!   @   r"   z+ImportedIds.visit_Lambda.<locals>.<genexpr>)r   r$   r%   r&   r(   r'   )r   r   r   r   r   r   visit_Lambda>   s   

zImportedIds.visit_Lambdac                 C      | j dd |jD  d S )Nc                 s   r   r   r#   r   aliasr   r   r   r!   E   r"   z+ImportedIds.visit_Import.<locals>.<genexpr>r   r%   namesr   r   r   r   visit_ImportD      zImportedIds.visit_Importc                 C   sX   |j D ]&}t|tjr| j|j qt|tjr| | t|tj	r)| 
| qd S r   )eltsr   r   Namer   r   r   Z	Subscriptr(   ZTuplevisit_StoredTuple)r   r   r+   r   r   r   r@   G   s   


zImportedIds.visit_StoredTuplec                 C   s*   t |jtjr| | d S | | d S r   )r   r   r   ZLoadr3   r@   r   r   r   r   visit_TupleQ   s   zImportedIds.visit_Tuplec                 C   r6   )Nc                 s   r   r   r7   r8   r   r   r   r!   Z   r"   z/ImportedIds.visit_ImportFrom.<locals>.<genexpr>r:   r   r   r   r   visit_ImportFromY   r=   zImportedIds.visit_ImportFromc                 C   s   d S r   r   r   r   r   r   visit_Attribute\   s   zImportedIds.visit_Attributec                    s:   t t| | | jr|jd }t| j| j|  | _d S )Nr   )	r   r   preparer	   r'   r   globalslocalsr   r   r   r   r   rD   _   s   
zImportedIds.preparec                    s6   t |trd| _ttdd |g }tt| |S )NT   )	r   listr	   r   ZIfZConstantr   r   runr   r   r   r   rI   e   s   
zImportedIds.run)__name__
__module____qualname____doc__r   r   r)   r-   Zvisit_ListCompZvisit_SetCompZvisit_DictCompZvisit_GeneratorExpr2   r4   r5   r<   r@   rA   Z
visit_ListrB   rC   rD   rI   __classcell__r   r   r   r   r      s*    
r   )rM   Z!pythran.analyses.globals_analysisr   Z pythran.analyses.locals_analysisr   Zpythran.passmanagerr   Zpythran.metadataZmetadatar.   Zgastr   r   r   r   r   r   <module>   s    