o
    e!                     @   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__ @D:\Projects\ConvertPro\env\Lib\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   ast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/   r0   r   r,   r1   r(   r   r+   r*   r   r2   )r   r   r4   r6   r3   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   StoreParamr   r+   idLoadr-   add_referencer   set_name_for_noder2   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   ClassDefr   r+   r   create_scopeparent_scoperA   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defaultsrD   )varargkwargrM   rN   )r$   six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+   argr2   rA   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 )NrC   basesrF   )r$   r   r+   r   rI   rJ   rA   r   r   r   visit_ClassDef   s   zScopeVisitor.visit_ClassDefc                 C   sH   |  | | j|j}|r"||j}| j|| || d S d S r   )r2   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$   r7   r9   rB   rK   rQ   rS   rU   rX   __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   )rJ   r(   r   )r   rJ   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(   r^   rJ   r_   r-   r   r   r`   r   r   r   r-      s   
zScope.lookup_namec                 C   s
   | j  S r   )rJ   get_root_scoper   r   r   r   rb      s   
zScope.get_root_scopec                 C   s   |   |S r   )rb   lookup_scoperA   r   r   r   rc         zScope.lookup_scopec                 C   s   t | |}|  || |S r   )r]   rb   _set_scope_for_node)r   r   Zsubscoper   r   r   rI      s   
zScope.create_scopeN)	rY   rZ   r[   r   r+   r-   rb   rc   rI   r   r   r   r   r]      s    
r]   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_scopesrA   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   rf   append)r   r   r   r	   refr   r   r   r,      s   
z RootScope.add_external_referencec                 C   s   | S r   r   r   r   r   r   rb      s   zRootScope.get_root_scopec                 C      | j |d S r   )rg   getrA   r   r   r   rG      rd   zRootScope.parentc                 C   s$   || j |< |d u r| | j|< d S d S r   )rg   ri   )r   r   rG   r   r   r   r      s   
zRootScope.set_parentc                 C   rl   r   )rh   rm   rA   r   r   r   rV      rd   zRootScope.get_name_for_nodec                 C      || j |< d S r   )rh   )r   r   r   r   r   r   r@      rd   zRootScope.set_name_for_nodec                 C   s6   |rz| j | W S  ty   | |}Y nw |sd S r   )ri   r^   rG   rA   r   r   r   rc      s   zRootScope.lookup_scopec                 C   rn   r   )ri   )r   r   Z
node_scoper   r   r   re      rd   zRootScope._set_scope_for_noder   )rY   rZ   r[   r   r,   rb   rG   r   rV   r@   rc   re   r\   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 )
r_   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   )rp   rj   rA   r   r   r   r?     s   zName.add_referencec                 C   s    | j r| j| d S || _ d S r   )ro   rp   rj   rA   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    r^   r_   r1   r=   ra   r   r   r   r-   
  s   zName.lookup_nameN)rY   rZ   r[   r   r?   r.   r-   r   r   r   r   r_      s
    r_   c                 C   s   t  }||  |jS r   )r
   r   r   )treevr   r   r   analyze  s   
rs   )__doc__
__future__r   r   r   r   collectionsrO   
namedtupler   NodeVisitorr
   objectr]   r   r_   rs   r   r   r   r   <module>   s    
 %.