o
    ?eU                     @   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 ddl	m
Z
 dZ	 G dd	 d	eZd
d ZG dd dejZdd ZdS )a1  Handles directives.

This converter removes the directive functions from the code and moves the
information they specify into AST annotations. It is a specialized form of
static analysis, one that is specific to AutoGraph.

Note that this requires that the actual directive functions are static - that
is, they do not change at runtime. So if you do something like this:

  tf.autograph.set_loop_options = <new function>

Then the directive will may no longer be recognized. Furthermore, if the
converted function is cached, such an action may be irreversible.
    N)	converter)
directives)anno)
tf_inspectZstatic_valuec                   @   s   e Zd Zdd ZdS )
_LoopScopec                 C   s   d | _ d| _d S )Nr   )ast_nodestatements_visited)self r
   r/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/autograph/converters/directives.py__init__.   s   
z_LoopScope.__init__N)__name__
__module____qualname__r   r
   r
   r
   r   r   ,   s    r   c                    s   | j }dd | jD }tj|g|R i | g } D ]}||vr3 | |vr3 | tjur3|| q|rHtdt| fdd|D |f dd  	 D S )aa  Maps AST call nodes to the actual function's arguments.

  Args:
    call_node: ast.Call
    function: Callable[..., Any], the actual function matching call_node
  Returns:
    Dict[Text, ast.AST], mapping each of the function's argument names to
    the respective AST node.
  Raises:
      ValueError: if the default arguments are not correctly set
  c                 S   s   i | ]}|j |jqS r
   )argvalue).0Zkwdr
   r
   r   
<dictcomp>@   s    z_map_args.<locals>.<dictcomp>z7Unexpected keyword argument values, %s, for function %sc                    s   g | ]} | qS r
   r
   )r   kZ	call_argsr
   r   
<listcomp>Q   s    z_map_args.<locals>.<listcomp>c                 S   s    i | ]\}}|t jur||qS r
   )r   UNSPECIFIED)r   r   vr
   r
   r   r   S   s     )
argskeywordsr   getcallargsr   r   append
ValueErrorzipitems)	call_nodefunctionr   kwdsZunexpected_defaultsr   r
   r   r   	_map_args3   s*   

r#   c                   @   s`   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d Zdd ZdS )DirectivesTransformerzBParses compiler directives and converts them into AST annotations.c                 C   sT   t |jdk rtd|j |jd }t|tjj}|D ]
}t|||j	|< q|S )N   z7"%s" requires a positional first argument as the targetr   )
lenr   r   r   r   getannoStaticZORIG_DEFINITIONSr#   r   )r	   r    	directivetargetdefsZdef_r
   r
   r   _process_symbol_directiveY   s   
z/DirectivesTransformer._process_symbol_directivec                 C   s~   | j t jdkrtd|j | j t jdk rtd|j | j t j}t|tj	j
i }t||||< t|tj	j
| |S )Nr%   z2"%s" must be the first statement in the loop block   z$"%s" must be used inside a statement)stater   r   r   r   levelr   r   r'   ZBasicZ
DIRECTIVESr#   setanno)r	   r    r)   r*   Z	node_annor
   r
   r   _process_statement_directivec   s   z2DirectivesTransformer._process_statement_directivec                 C   sd   |  |}t|jtjr0t|tjjd}t	|}|s0|j
| jjjv r0t|t| jjj|j
  |S )Nr
   )generic_visit
isinstancectxgastLoadr   r'   r(   ZDEFINITIONSboolidinfo	namespacer0   STATIC_VALUE)r	   noder+   Z
is_definedr
   r
   r   
visit_Nameq   s   
z DirectivesTransformer.visit_Namec                 C   sT   |  |}tj|jtd d}|d ur(t|r(t||jr(t	|tt
||j |S )Ndefault)r2   r   r'   r   r;   inspectismodulehasattrattrr0   getattr)r	   r<   Z
parent_valr
   r
   r   visit_Attributez   s   
z%DirectivesTransformer.visit_Attributec                 C      | j t  jd7  _| |S Nr%   r.   r   r   r2   r	   r<   r
   r
   r   visit_Assign      
z"DirectivesTransformer.visit_Assignc                 C   rF   rG   rH   rI   r
   r
   r   visit_AugAssign   rK   z%DirectivesTransformer.visit_AugAssignc                 C   s   | j t  jd7  _| |}t|jtjr@|j}tj	|j
td d}|d ur@|tju r3| || d S |tju r@| || d S |S )Nr%   r>   )r.   r   r   r2   r3   r   r5   Callr   r'   funcr;   r   Zset_element_typer,   Zset_loop_optionsr1   )r	   r<   r    Z
static_valr
   r
   r   
visit_Expr   s   


z DirectivesTransformer.visit_Exprc                 C   sH   | j t   || j t _| |}|jst g|_| j t   |S N)	r.   r   Zenterr   r2   bodyr5   PassexitrI   r
   r
   r   _track_and_visit_loop   s   
z+DirectivesTransformer._track_and_visit_loopc                 C   
   |  |S rP   rT   rI   r
   r
   r   visit_While      
z!DirectivesTransformer.visit_Whilec                 C   rU   rP   rV   rI   r
   r
   r   	visit_For   rX   zDirectivesTransformer.visit_ForN)r   r   r   __doc__r,   r1   r=   rE   rJ   rL   rO   rT   rW   rY   r
   r
   r
   r   r$   V   s    
	
r$   c                 C   s   t || S rP   )r$   visit)r<   r4   r
   r
   r   	transform   s   r\   )rZ   r@   r5   Z tensorflow.python.autograph.corer   Z tensorflow.python.autograph.langr   Z tensorflow.python.autograph.pyctr   Ztensorflow.python.utilr   r;   objectr   r#   ZBaser$   r\   r
   r
   r
   r   <module>   s   #Z