
    BVhU                         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	e      Zd
 Z G d dej                        Zd Zy)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                       e Zd Zd Zy)
_LoopScopec                      d | _         d| _        y )Nr   )ast_nodestatements_visited)selfs    a/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/autograph/converters/directives.py__init__z_LoopScope.__init__.   s    DMD    N)__name__
__module____qualname__r    r   r   r	   r	   ,   s     r   r	   c                    | j                   }| j                  D ci c]  }|j                  |j                   }}t	        j
                  |g|i |}g }|D ]6  }||vs||   |vs||   t        j                  us&|j                  |       8 |r,t        dt        ||D cg c]  }||   	 c}      d|      |j                         D ci c]  \  }}|t        j                  us|| c}}S c c}w c c}w c c}}w )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
  z$Unexpected keyword argument values, z, for function )argskeywordsargvaluer   getcallargsr   UNSPECIFIEDappend
ValueErrorzipitems)		call_nodefunctionr   kwdkwds	call_argsunexpected_defaultskvs	            r   	_map_argsr(   3   s    
$(1(:(:	;#''399
	;$	;$$X===)  $a	aL$aL
 6 66  #	$
 
/3FGaYq\GI " # # %??,	P41a9O9O0O!Q$	PP' 
<" H	Ps   C6#C;D -D c                   L    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zy)DirectivesTransformerzBParses compiler directives and converts them into AST annotations.c                     t        |j                        dk  rt        d|j                  z        |j                  d   }t	        j
                  |t        j                  j                        }|D ]  }t        ||      |j                  |<    |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_s         r   _process_symbol_directivez/DirectivesTransformer._process_symbol_directiveY   s    
9>>Q (*3*<*<= > >^^AF<< < <=D C#,Y	#Bdooi Cr   c                    | j                   t           j                  dkD  rt        d|j                  z        | j                   t           j
                  dk  rt        d|j                  z        | j                   t           j                  }t        j                  |t        j                  j                  i       }t        ||      ||<   t        j                  |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    r1   r2   	node_annos        r   _process_statement_directivez2DirectivesTransformer._process_statement_directivec   s    zz*0014
>  "# # zz*##a'
093E3E
EG GZZ
#,,FVTZZ%:%:B?I$Y	:IiLL..	:r   c                    | j                  |      }t        |j                  t        j                        rt        j                  |t
        j                  j                  d      }t        |      }|sr|j                  | j                  j                  j                  v rFt        j                  |t        | j                  j                  j                  |j                            |S )Nr   )generic_visit
isinstancectxgastLoadr   r.   r/   DEFINITIONSboolidinfo	namespacer<   STATIC_VALUE)r   noder3   
is_defineds       r   
visit_Namez DirectivesTransformer.visit_Nameq   s    d#D$((DII&\\$ 7 7<d:jDGGtxx}}'>'>>T<)@)@)IJKr   c                 ,   | j                  |      }t        j                  |j                  t        d       }|Zt        j                  |      rEt        ||j                        r/t        j                  |t        t        ||j                               |S )Ndefault)r@   r   r.   r   rJ   inspectismodulehasattrattrr<   getattr)r   rK   
parent_vals      r   visit_Attributez%DirectivesTransformer.visit_Attributez   si    d#Ddjj,EJ'"2"2:">	TYY	'T<TYY)GHKr   c                 p    | j                   t           xj                  dz  c_        | j                  |      S Nr,   r8   r	   r   r@   r   rK   s     r   visit_Assignz"DirectivesTransformer.visit_Assign   -    JJz--2-d##r   c                 p    | j                   t           xj                  dz  c_        | j                  |      S rY   rZ   r[   s     r   visit_AugAssignz%DirectivesTransformer.visit_AugAssign   r]   r   c                    | j                   t           xj                  dz  c_        | j                  |      }t	        |j
                  t        j                        r~|j
                  }t        j                  |j                  t        d       }|J|t        j                  u r| j                  ||       y |t        j                  u r| j!                  ||       y |S )Nr,   rO   )r8   r	   r   r@   rA   r   rC   Callr   r.   funcrJ   r   set_element_typer5   set_loop_optionsr>   )r   rK   r    
static_vals       r   
visit_Exprz DirectivesTransformer.visit_Expr   s    JJz--2-d#D$**dii(**i<<	dKj		 444

(
(J
?:666

+
+Iz
BKr   c                 (   | j                   t           j                          || j                   t           _        | j	                  |      }|j
                  st        j                         g|_        | j                   t           j                          |S N)	r8   r	   enterr   r@   bodyrC   Passexitr[   s     r   _track_and_visit_loopz+DirectivesTransformer._track_and_visit_loop   sh    JJz  "&*DJJz#d#D9999;-diJJz!Kr   c                 $    | j                  |      S rh   rm   r[   s     r   visit_Whilez!DirectivesTransformer.visit_While       %%d++r   c                 $    | j                  |      S rh   ro   r[   s     r   	visit_ForzDirectivesTransformer.visit_For   rq   r   N)r   r   r   __doc__r5   r>   rM   rW   r\   r_   rf   rm   rp   rs   r   r   r   r*   r*   V   s7    J$$*,,r   r*   c                 6    t        |      j                  |       S rh   )r*   visit)rK   rB   s     r   	transformrw      s    	s	#	)	)$	//r   )rt   rQ   rC    tensorflow.python.autograph.corer    tensorflow.python.autograph.langr    tensorflow.python.autograph.pyctr   tensorflow.python.utilr   rJ   objectr	   r(   Baser*   rw   r   r   r   <module>r~      sS      6 7 1 -  /    QFW,INN W,t0r   