o
    ?e(                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ G dd deZddd	Z	G d
d dej
Zdd Zdd ZG dd dejZdd Zdd Zdd ZdS )zAST manipulation utilities.    N)anno)parser)
qual_namesc                       s(   e Zd ZdZ fddZdd Z  ZS )CleanCopierz0NodeTransformer-like visitor that copies an AST.c                    s   t t|   || _d S N)superr   __init__preserve_annos)selfr	   	__class__ j/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/autograph/pyct/ast_util.pyr      s   
zCleanCopier.__init__c                    s   t |tr fdd|D S t |trt fdd|D S t |tjtjfs)|S t |tjtjfs4J i }|jD ]}|dsOt||rO 	t
||||< q9t|di |} jri jD ]	}t||| q_|S )zDReturns a deep copy of node (excluding some fields, see copy_clean).c                       g | ]}  |qS r   copy.0nr
   r   r   
<listcomp>%       z$CleanCopier.copy.<locals>.<listcomp>c                 3       | ]}  |V  qd S r   r   r   r   r   r   	<genexpr>'       z#CleanCopier.copy.<locals>.<genexpr>__Nr   )
isinstancelisttuplegastASTast_fields
startswithhasattrr   getattrtyper	   r   copyanno)r
   node
new_fieldsfnew_nodekr   r   r   r   !   s"   



zCleanCopier.copy)__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r      s    r   c                 C   s   t || S )a"  Creates a deep copy of an AST.

  The copy will not include fields that are prefixed by '__', with the
  exception of user-specified annotations.

  Args:
    node: ast.AST
    preserve_annos: Optional[Set[Hashable]], annotation keys to include in the
        copy
  Returns:
    ast.AST
  )r   r   )r(   r	   r   r   r   
copy_clean;   s   r2   c                   @   sP   e Zd Z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S )SymbolRenamerz6Transformer that can rename symbols to a simple names.c                 C   s
   || _ d S r   )name_map)r
   r4   r   r   r   r   N      
zSymbolRenamer.__init__c                 C   sd   t |t jj}|| jv r-tjt| j| |jd d d}t 	|D ]	}t 
||| q!|S | |S )N)ctx
annotationtype_comment)r   ZgetannoBasicQNr4   r   Namestrr6   keysr'   generic_visit)r
   r(   qnr+   r,   r   r   r   _process_name_nodeQ   s   

z SymbolRenamer._process_name_nodec                 C   s@   t t|D ]}t|| }|| jv rt| j| ||< q|S r   )rangelenr   r:   r4   r<   )r
   namesir?   r   r   r   _process_list_of_strings_   s   
z&SymbolRenamer._process_list_of_stringsc                 C      |  |j|_|S r   rE   rC   r
   r(   r   r   r   visit_Nonlocalf      zSymbolRenamer.visit_Nonlocalc                 C   rF   r   rG   rH   r   r   r   visit_Globalj   rJ   zSymbolRenamer.visit_Globalc                 C   s
   |  |S r   )r@   rH   r   r   r   
visit_Namen   r5   zSymbolRenamer.visit_Namec                 C   s$   t |t jjr| |S | |S r   )r   Zhasannor9   r:   r@   r>   rH   r   r   r   visit_Attributeq   s   

zSymbolRenamer.visit_Attributec                 C   s0   t |j}|| jv rt| j| |_| |S r   )r   r:   namer4   r<   r>   )r
   r(   r?   r   r   r   visit_FunctionDefw   s   

zSymbolRenamer.visit_FunctionDefN)r-   r.   r/   r0   r   r@   rE   rI   rK   rL   rM   rO   r   r   r   r   r3   K   s    r3   c                    sN   t | t| tr fdd| D S t| tr"t fdd| D S  | S )z;Renames symbols in an AST. Requires qual_names annotations.c                    r   r   visitr   Zrenamerr   r   r      r   z"rename_symbols.<locals>.<listcomp>c                 3   r   r   rP   r   rR   r   r   r      r   z!rename_symbols.<locals>.<genexpr>)r3   r   r   r   rQ   )r(   r4   r   rR   r   rename_symbols~   s   


rS   c                 C   sB   g }g }| D ]}| tj|jdd | |j qtj||dS )z1Converts a list of ast.keyword objects to a dict.N)kind)r=   values)appendr   ConstantargvalueDict)keywordsr=   rU   kwr   r   r   keywords_to_dict   s   r]   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )PatternMatcherz7Matches a node against a pattern represented by a node.c                 C   s   || _ g | _d| _d S )NT)patternpattern_stackmatches)r
   r_   r   r   r   r      s   
zPatternMatcher.__init__c                 C   s.   | j | j || _| | | j  | _d S r   )r`   rV   r_   r>   pop)r
   r(   r_   r   r   r   compare_and_visit   s   
z PatternMatcher.compare_and_visitc                 C   s
   d| _ dS )NF)ra   r   r   r   r   no_match   s   zPatternMatcher.no_matchc                 C   sJ   t |ttfrt|dkr|\}t |tjr|jdkrdS |dkr#dS dS )N   _TF)r   r   r   rB   r   r;   id)r
   pr   r   r   is_wildcard   s   zPatternMatcher.is_wildcardc                 C   sD  | j sd S | j}|jD ]}|drqt||s)t||r(t||r(|    S qt||s4|    S t||}t||}| |rDqt|t	t
frqt|t	t
frZt|t|kr`|    S t||D ]
\}}| || qeqt|tjtjfrt|t|st|t|s|    S | || q||kr|    S qd S )Nr   )ra   r_   r"   r#   r$   r%   rd   ri   r   r   r   rB   ziprc   r   r    r!   r&   )r
   r(   r_   r*   vrh   Zv_itemZp_itemr   r   r   r>      s<   






zPatternMatcher.generic_visitN)	r-   r.   r/   r0   r   rc   rd   ri   r>   r   r   r   r   r^      s    	r^   c                 C   s,   t |tr
t|}t|}||  |jS )a5  Basic pattern matcher for AST.

  The pattern may contain wildcards represented by the symbol '_'. A node
  matches a pattern if for every node in the tree, either there is a node of
  the same type in pattern, or a Name node with id='_'.

  Args:
    node: ast.AST
    pattern: ast.AST
  Returns:
    bool
  )r   r<   r   Z	parse_strr^   rQ   ra   )r(   r_   Zmatcherr   r   r   ra      s
   


ra   c                 C   s   t | ttfs
| f} | D ]F}t |tjtjfrMtt|jD ]-}|j| }t |tjtjfr4|j| }nt	
t|}tj||t d}t||| qq||| qdS )a  Applies a function to each individual assignment.

  This function can process a possibly-unpacked (e.g. a, b = c, d) assignment.
  It tries to break down the unpacking if possible. In effect, it has the same
  effect as passing the assigned values in SSA form to apply_fn.

  Examples:

  The following will result in apply_fn(a, c), apply_fn(b, d):

      a, b = c, d

  The following will result in apply_fn(a, c[0]), apply_fn(b, c[1]):

      a, b = c

  The following will result in apply_fn(a, (b, c)):

      a = b, c

  It uses the visitor pattern to allow subclasses to process single
  assignments individually.

  Args:
    targets: Union[List[ast.AST, ...], Tuple[ast.AST, ...], ast.AST, should be
        used with the targets field of an ast.Assign node
    values: ast.AST
    apply_fn: Callable[[ast.AST, ast.AST], None], called with the
        respective nodes of each single assignment
  )r6   N)r   r   r   r   TupleListrA   rB   eltsr   Zparse_expressionr<   	SubscriptLoadapply_to_single_assignments)targetsrU   Zapply_fntargetrD   Z	target_elZvalue_elidxr   r   r   rq      s   
	rq   c           	      c   s   t | ttfrt| }n| g}t |ttfrt|}n|g}|r|rt|t|ks-J | }| }t |tjtjtfsC|dusYt |tjtjtfsQ|dusY|j	j
|j	j
krgtd||j	j
||j	j
||fV  t |tr{t |tszJ dq|du r|du sJ dq|jD ]g}t||d}t||d}|ds|du s|du rqt |ttfrt |ttfrt|t|krtd||||| || qt |tjtjfr|| || q||krtd|||q|r|s#dS dS dS dS )a!  Walks two ASTs in parallel.

  The two trees must have identical structure.

  Args:
    node: Union[ast.AST, Iterable[ast.AST]]
    other: Union[ast.AST, Iterable[ast.AST]]
  Yields:
    Tuple[ast.AST, ast.AST]
  Raises:
    ValueError: if the two trees don't have identical structure.
  Nz'inconsistent nodes: {} ({}) and {} ({})z(The check above should have ensured thisr   z+inconsistent values for field {}: {} and {})r   r   r   rB   rb   r!   r    r   r<   r   r-   
ValueErrorformatr"   r%   r#   extendrV   )	r(   otherZ
node_stackZother_stackr   or*   Zn_childZo_childr   r   r   parallel_walk  sn   






rz   r   )r0   r!   r   Z tensorflow.python.autograph.pyctr   r   r   objectr   r2   NodeTransformerr3   rS   r]   NodeVisitorr^   ra   rq   rz   r   r   r   r   <module>   s   
!3

@/