o
    2hU                     @   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_inspect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   l/var/www/html/chatgem/venv/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).0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	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   kwds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StaticORIG_DEFINITIONSr'   r   )r
   r#   	directivetargetdefs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+   Basic
DIRECTIVESr'   setanno)r
   r#   r.   r/   	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,   DEFINITIONSboolidinfo	namespacer8   STATIC_VALUE)r
   noder0   
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)r;   r   r+   r   rE   inspectismodulehasattrattrr8   getattr)r
   rF   
parent_valr   r   r   visit_Attributez   s   
z%DirectivesTransformer.visit_Attributec                 C      | j t  jd7  _| |S Nr)   r4   r   r	   r;   r
   rF   r   r   r   visit_Assign      
z"DirectivesTransformer.visit_Assignc                 C   rR   rS   rT   rU   r   r   r   visit_AugAssign   rW   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)   rI   )r4   r   r	   r;   r<   r   r>   Callr   r+   funcrE   r   set_element_typer2   set_loop_optionsr:   )r
   rF   r#   
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)	r4   r   enterr   r;   bodyr>   PassexitrU   r   r   r   _track_and_visit_loop   s   
z+DirectivesTransformer._track_and_visit_loopc                 C   
   |  |S r_   rd   rU   r   r   r   visit_While      
z!DirectivesTransformer.visit_Whilec                 C   re   r_   rf   rU   r   r   r   	visit_For   rh   zDirectivesTransformer.visit_ForN)r   r   r   __doc__r2   r:   rH   rQ   rV   rX   r^   rd   rg   ri   r   r   r   r   r(   V   s    
	
r(   c                 C   s   t || S r_   )r(   visit)rF   r=   r   r   r   	transform   s   rl   )rj   rK   r>    tensorflow.python.autograph.corer    tensorflow.python.autograph.langr    tensorflow.python.autograph.pyctr   tensorflow.python.utilr   rE   objectr   r'   Baser(   rl   r   r   r   r   <module>   s   #Z