o
    í@Ëag  ã                   @   s8   d Z ddlmZ ddlmZ ddlZG dd„ deƒZdS )z'
Locals computes the value of locals()
é    )ÚModuleAnalysisNc                       s    e Zd ZdZ‡ fdd„Z‡ fdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZeZeZeZeZeZeZeZeZeZeZeZeZeZ‡  ZS )ÚLocalsaŒ  
    Statically compute the value of locals() before each statement

    Yields a dictionary binding every node to the set of variable names defined
    *before* this node.

    Following snippet illustrates its behavior:
    >>> import gast as ast
    >>> from pythran import passmanager
    >>> pm = passmanager.PassManager('test')
    >>> code = '''
    ... def b(n):
    ...     m = n + 1
    ...     def b(n):
    ...         return n + 1
    ...     return b(m)'''
    >>> tree = ast.parse(code)
    >>> l = pm.gather(Locals, tree)
    >>> sorted(l[tree.body[0].body[0]])
    ['n']
    >>> sorted(l[tree.body[0].body[1]])
    ['b', 'm', 'n']
    c                    s(   t ƒ | _tƒ | _d| _tt| ƒ ¡  d S )Nr   )ÚdictÚresultÚsetÚlocalsÚnestingÚsuperr   Ú__init__)Úself©Ú	__class__© úB/usr/lib/python3/dist-packages/pythran/analyses/locals_analysis.pyr
   $   s   zLocals.__init__c                    s4   t t| ƒ |¡ || jvr| j| j | j|< d S d S ©N)r	   r   Úgeneric_visitr   Úexpr_parent©r   Únoder   r   r   r   *   s   
ÿzLocals.generic_visitc                 C   s$   || _ | j ¡ | j|< |  |¡ d S r   )r   r   Úcopyr   r   r   r   r   r   Ústore_and_visit/   s   zLocals.store_and_visitc                 C   s    || _ | j| j|< |  |¡ d S r   )r   r   r   r   r   r   r   r   Úvisit_Module4   s   zLocals.visit_Modulec                 C   sÔ   | j r
| j |j¡ |  j d7  _ || _| j ¡ | j|< | j ¡ }|jjD ]}|  	|¡ q%|jjD ]}|j
r<|  	|j
¡ q1|jrF|  	|j¡ | j dd„ |jjD ƒ¡ |jD ]}|  	|¡ qV|| _|  j d8  _ d S )Né   c                 s   ó    | ]}|j V  qd S r   )Úid)Ú.0Úargr   r   r   Ú	<genexpr>H   ó   € z+Locals.visit_FunctionDef.<locals>.<genexpr>)r   r   ÚaddÚnamer   r   r   ÚargsÚdefaultsÚvisitÚ
annotationÚreturnsÚupdateÚbody)r   r   Zparent_localsÚdefaultr   Ústmtr   r   r   Úvisit_FunctionDef9   s&   
€
zLocals.visit_FunctionDefc                 C   s`   || _ | j ¡ | j|< t | |¡ |  |j¡ | j dd„ |jD ƒ¡ |jD ]}|  |¡ q&d S )Nc                 s   s"    | ]}t |tjƒr|jV  qd S r   )Ú
isinstanceÚastÚNamer   )r   Útr   r   r   r   S   s   € 
ÿz&Locals.visit_Assign.<locals>.<genexpr>)	r   r   r   r   Úmdr#   Úvaluer&   Útargets)r   r   Útargetr   r   r   Úvisit_AssignN   s   
ÿzLocals.visit_Assignc                 C   sn   || _ | j ¡ | j|< t | |¡ |  |j¡ | j |jj	¡ |j
D ]}|  |¡ q"|jD ]}|  |¡ q-d S r   )r   r   r   r   r/   r#   Úiterr   r2   r   r'   Úorelse©r   r   r)   r   r   r   Ú	visit_ForX   s   

ÿzLocals.visit_Forc                 C   ó,   | j  ¡ | j|< | j  dd„ |jD ƒ¡ d S )Nc                 s   r   r   ©r    ©r   Úaliasr   r   r   r   e   r   z&Locals.visit_Import.<locals>.<genexpr>©r   r   r   r&   Únamesr   r   r   r   Úvisit_Importc   ó   zLocals.visit_Importc                 C   r8   )Nc                 s   r   r   r9   r:   r   r   r   r   i   r   z*Locals.visit_ImportFrom.<locals>.<genexpr>r<   r   r   r   r   Úvisit_ImportFromg   r?   zLocals.visit_ImportFromc                 C   sX   || _ | j ¡ | j|< |jr| j |jj¡ |jo|  |j¡ |j	D ]}|  |¡ q"d S r   )
r   r   r   r   r    r   r   Útyper#   r'   r6   r   r   r   Úvisit_ExceptHandlerk   s   €
ÿzLocals.visit_ExceptHandler)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   r*   r3   r7   r>   r@   rB   Zvisit_ReturnZvisit_YieldZ	visit_TryZvisit_AugAssignZvisit_PrintZvisit_WhileZvisit_IfZvisit_RaiseZvisit_AssertZ
visit_ExprZ
visit_PassZvisit_BreakZvisit_ContinueÚ__classcell__r   r   r   r   r      s2    

r   )	rF   Zpythran.passmanagerr   Zpythran.metadataZmetadatar/   Zgastr,   r   r   r   r   r   Ú<module>   s
    