o
    o^!                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZeddZ	G dd	 d	ej
ZG d
d deZG dd deZG dd deZdd ZdS )z/Perform static analysis on python syntax trees.    )absolute_import)division)print_functionNExternalReferencenamenodename_refc                       sp   e 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dd Z  ZS )ScopeVisitorc                    s(   t t|   d | _td  | _| _d S N)superr
   __init___parent	RootScope
root_scopescopeself	__class__ 0lib/python3.10/site-packages/pasta/base/scope.pyr   '   s   zScopeVisitor.__init__c                    sV   |d u rd S | j jd u r|| j _| j || j | j}|| _tt| | || _d S r   )r   r   
set_parentr   r   r
   visit)r   r   tmpr   r   r   r   ,   s   
zScopeVisitor.visitc                 G   sZ   |D ](}t ||d }|d u rqt|tr|D ]}| | qqt|tjr*| | qd S r   )getattr
isinstancelistr   astZAST)r   r   attrsattrvalitemr   r   r   visit_in_order7   s   

zScopeVisitor.visit_in_orderc           	   	   C   s   |j D ]p}|jd}|jsI| j|d |}| jj|d ||d |d }|dd  D ]}|d| 7 }||}|	| | jj|||d q,q| j|j|}t
dt|D ]}| jd|d | | qX| jj|j||d q| | d S )N.r   r	      )namesr   splitasnamer   define_namer   add_external_referencelookup_namedefinerangelenjoingeneric_visit)	r   r   alias
name_partsZcur_nameZpartial_namepartr   ir   r   r   visit_ImportB   s.   



	zScopeVisitor.visit_Importc                 C   s   |j r#|j d}tdt|d D ]}| jd|d | | q|jD ] }| j	|j
p0|j|}|j rF| jjd|j |jf||d q&| | d S )Nr$   r&   r%   )moduler(   r.   r/   r   r+   r0   r'   r   r*   r)   r   r1   )r   r   r3   r5   r2   r   r   r   r   visit_ImportFroma   s   
zScopeVisitor.visit_ImportFromc                 C   sn   t |jtjtjfr| j|j| nt |jtjr0| j	|j
| | j|| j	|j | | d S r   )r   ctxr   ZStoreZParamr   r*   idZLoadr,   add_referencer   set_name_for_noder1   r   r   r   r   r   
visit_Namen   s   zScopeVisitor.visit_Namec                 C   sn   |  |d t| j|tjrn| j|j| z| j	|| _|  |ddd W | jj
| _d S | jj
| _w )Ndecorator_listargsreturnsbody)r#   r   r   parentr   ZClassDefr   r*   r   create_scopeparent_scoper=   r   r   r   visit_FunctionDefv   s   zScopeVisitor.visit_FunctionDefc                 C   sV   |  |dd tjr"dD ]}t||d }|d ur| j|| qd S |  |dd d S )Ndefaultsr@   )varargkwargrH   rI   )r#   sixZPY2r   r   r*   )r   r   Zarg_attr_nameZarg_namer   r   r   visit_arguments   s   zScopeVisitor.visit_argumentsc                 C   s   | j |j| | | d S r   )r   r*   argr1   r=   r   r   r   	visit_arg   s   zScopeVisitor.visit_argc                 C   sV   |  |dd | j|j| z| j|| _|  |d W | jj| _d S | jj| _w )Nr?   basesrB   )r#   r   r*   r   rD   rE   r=   r   r   r   visit_ClassDef   s   zScopeVisitor.visit_ClassDefc                 C   sH   |  | | j|j}|r"||j}| j|| || d S d S r   )r1   r   get_name_for_nodevaluer,   r    r<   r;   )r   r   Znode_value_nameZ	node_namer   r   r   visit_Attribute   s   
zScopeVisitor.visit_Attribute)__name__
__module____qualname__r   r   r#   r6   r8   r>   rF   rK   rM   rO   rR   __classcell__r   r   r   r   r
   %   s    	r
   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )Scopec                 C   s   || _ i | _|| _d S r   )rE   r'   r   )r   rE   r   r   r   r   r      s   
zScope.__init__c                 C   sB   z| j | }W n ty   t| }| j |< Y nw || |S r   )r'   KeyErrorNamer-   )r   r   r   name_objr   r   r   r*      s   
zScope.define_namec                 C   sL   z| j | W S  ty   Y nw | jd u r t| }| j |< |S | j|S r   )r'   rX   rE   rY   r,   r   r   rZ   r   r   r   r,      s   
zScope.lookup_namec                 C   s
   | j  S r   )rE   get_root_scoper   r   r   r   r\      s   
zScope.get_root_scopec                 C   s   |   |S r   )r\   lookup_scoper=   r   r   r   r]         zScope.lookup_scopec                 C   s   t | |}|  || |S r   )rW   r\   _set_scope_for_node)r   r   Zsubscoper   r   r   rD      s   
zScope.create_scopeN)	rS   rT   rU   r   r*   r,   r\   r]   rD   r   r   r   r   rW      s    
rW   c                       s^   e Zd Z fddZdddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Z  ZS )r   c                    s.   t t| d | i | _i | _i | _i | _d S r   )r   r   r   external_references_parents_nodes_to_names_node_scopesr=   r   r   r   r      s
   
zRootScope.__init__Nc                 C   s<   t |||d}|| jv r| j| | d S |g| j|< d S )Nr   )r   r`   append)r   r   r   r	   refr   r   r   r+      s   
z RootScope.add_external_referencec                 C   s   | S r   r   r   r   r   r   r\      s   zRootScope.get_root_scopec                 C      | j |d S r   )ra   getr=   r   r   r   rC      r^   zRootScope.parentc                 C   s$   || j |< |d u r| | j|< d S d S r   )ra   rc   )r   r   rC   r   r   r   r      s   
zRootScope.set_parentc                 C   rf   r   )rb   rg   r=   r   r   r   rP      r^   zRootScope.get_name_for_nodec                 C      || j |< d S r   )rb   )r   r   r   r   r   r   r<      r^   zRootScope.set_name_for_nodec                 C   s6   |rz| j | W S  ty   | |}Y nw |sd S r   )rc   rX   rC   r=   r   r   r   r]      s   zRootScope.lookup_scopec                 C   rh   r   )rc   )r   r   Z
node_scoper   r   r   r_      r^   zRootScope._set_scope_for_noder   )rS   rT   rU   r   r+   r\   rC   r   rP   r<   r]   r_   rV   r   r   r   r   r      s    
r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
rY   c                 C   s   || _ d | _g | _i | _d S r   )r:   
definitionreadsr   )r   r:   r   r   r   r      s   
zName.__init__c                 C   s   | j | d S r   )rj   rd   r=   r   r   r   r;     s   zName.add_referencec                 C   s    | j r| j| d S || _ d S r   )ri   rj   rd   r=   r   r   r   r-     s   
zName.definec                 C   sB   z| j | W S  ty    td| j|f }| j |< | Y S w )Nr$   )r   rX   rY   r0   r:   r[   r   r   r   r,   
  s   zName.lookup_nameN)rS   rT   rU   r   r;   r-   r,   r   r   r   r   rY      s
    rY   c                 C   s   t  }||  |jS r   )r
   r   r   )Ztreevr   r   r   analyze  s   
rl   )__doc__Z
__future__r   r   r   r   collectionsrJ   
namedtupler   ZNodeVisitorr
   objectrW   r   rY   rl   r   r   r   r   <module>   s    
 %.