o
    "j6                     @   sh   d dl mZ ddlmZmZmZmZmZ g ZG dd dZ	G dd dZ
G dd	 d	ZG d
d dZdS )    )gast   )NodeVarTypeindex_in_listis_dygraph_apiis_numpy_apiis_paddle_apic                   @   s   e Zd ZdZdd ZdS )AstNodeWrapperz
    Wrapper for python gast.node. We need a node wrapper because gast.node
    doesn't store all required information when we are transforming AST.
    We should collect additional information which the actual transformation
    needs.
    c                 C   s    || _ d | _g | _tjh| _d S N)nodeparentchildrenr   UNKNOWNnode_var_typeselfr    r   e/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/jit/dy2static/static_analysis.py__init__$   s   zAstNodeWrapper.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r	      s    r	   c                   @   sD   e Zd ZdZdZdZdZdedfddZd	d
 Zdd Z	dd Z
dS )AstVarScopez]
    AstVarScope is a class holding the map from current scope variable to its
    type.
    r   r       Nc                 C   sF   g | _ i | _i | _d| _|| _|| _|| _|d ur!|j |  d S d S )Nr   )Z
sub_scopes
name_to_id
id_to_typecur_id
scope_name
scope_typeparent_scopeappend)r   r   r    r!   r   r   r   r   5   s   zAstVarScope.__init__c                 C   sL   |  |}|tjhkr| || d S t|tr|| d S || d S r
   )get_var_typer   r   set_var_type
isinstancesetupdateadd)r   var_namer   var_typer   r   r   add_var_typeC   s   

zAstVarScope.add_var_typec                 C   sR   || j v r| j | }n| j}|  jd7  _|| j |< t|tr!|n|h| j|< d S )Nr   )r   r   r%   r&   r   )r   r)   r   num_idr   r   r   r$   M   s   

zAstVarScope.set_var_typec                 C   s<   || j v r| j | }| j| S | jd u rtjhS | j|S r
   )r   r   r!   r   r   r#   )r   r)   r,   r   r   r   r#   X   s   



zAstVarScope.get_var_type)r   r   r   r   ZSCOPE_TYPE_SCRIPTSCOPE_TYPE_FUNCTIONZSCOPE_TYPE_CLASSr   r+   r$   r#   r   r   r   r   r   +   s    

r   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 )	AstVarEnvzI
    A class maintains scopes and mapping from name strings to type.
    c                 C   s   t  | _d S r
   r   	cur_scoper   r   r   r   r   f      zAstVarEnv.__init__c                 C   s   t ||| jd| _| jS )N)r!   r/   )r   r   r    r   r   r   enter_scopei   s   zAstVarEnv.enter_scopec                 C   s$   | j jd us
J d| j j| _ | j S )NzJCall exit_scope in AstVarEnv when current scope doesn't have parent scope.r0   r!   r1   r   r   r   
exit_scopeo   s
   
zAstVarEnv.exit_scopec                 C   s   | j jd us
J d| j jS )NzLCall parent_scope in AstVarEnv when current scope doesn't have parent scope.r4   r1   r   r   r   get_parent_scopew   s   zAstVarEnv.get_parent_scopec                 C      | j || d S r
   )r0   r+   r   r)   r   r   r   r   r+   ~      zAstVarEnv.add_var_typec                 C   r7   r
   )r0   r$   r8   r   r   r   r$      r9   zAstVarEnv.set_var_typec                 C   s   | j |S r
   )r0   r#   )r   r)   r   r   r   r#      r2   zAstVarEnv.get_var_typec                 C   s*   i }| j jD ]}| j |}|||< q|S )ze
        Returns a dict mapping from variable name to type. Used for debug and
        test.
        )r0   r   r#   )r   Zcur_scope_dictnamer   r   r   r   get_scope_var_type   s
   
zAstVarEnv.get_scope_var_typeN)r   r   r   r   r   r3   r5   r6   r+   r$   r#   r;   r   r   r   r   r.   a   s    r.   c                   @   sb   e Zd Z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dd Zdd ZdS )StaticAnalysisVisitorz+
    A class that does static analysis
    Nc                 C   s   |d ur|  | d S d S r
   )runr   Zast_rootr   r   r   r      s   zStaticAnalysisVisitor.__init__c                 C   s(   d | _ g | _i | _t | _| | d S r
   )node_wrapper_rootancestor_wrappersnode_to_wrapper_mapr.   var_env	dfs_visitr>   r   r   r   r=      s
   zStaticAnalysisVisitor.runc                 C   s   || j vrt|}|| j |< n| j | }| jd u r|| _t| jdkr1| jd }|j| ||_| j| t	|D ]$}t
|tjtjfr[| j|jtj | |}| j  q<| | q<| j  | ||_|jS )Nr   )rA   r	   r?   lenr@   r   r"   r   r   iter_child_nodesr%   FunctionDefAsyncFunctionDefrB   r3   r:   r   r-   rC   r5   pop_get_node_var_typer   )r   r   cur_wrapperZlast_wrapperchild	func_typer   r   r   rC      s,   





zStaticAnalysisVisitor.dfs_visitc                 C      | j S r
   )r?   r1   r   r   r   get_node_wrapper_root      z+StaticAnalysisVisitor.get_node_wrapper_rootc                 C   rN   r
   )rA   r1   r   r   r   get_node_to_wrapper_map   rP   z-StaticAnalysisVisitor.get_node_to_wrapper_mapc                 C   rN   r
   )rB   r1   r   r   r   get_var_env   rP   z!StaticAnalysisVisitor.get_var_envc                 C   s8   t jt jh}| j|d }|d u rdS |j|@ rdS d S )NFT)r   TENSORPADDLE_RETURN_TYPESrA   getr   )r   r   Ztensor_typesZnode_wrapperr   r   r   is_tensor_node   s   
z$StaticAnalysisVisitor.is_tensor_nodec                 C   s   t |tjsJ dt| |jd u rtjhS t |jtr!tjhS t |jt	r+tj
hS t |jtr5tjhS t |jtr?tjhS tjhS )Nz;Type of input node should be gast.Constant, but received %s)r%   r   Constanttypevaluer   NONEboolBOOLEANintINTfloatFLOATstrSTRINGr   r   r   r   r   _get_constant_node_type   s    
z-StaticAnalysisVisitor._get_constant_node_typec                 C   sn  |j }t|tjr| |S t|tjrtjhS t|tjr"tjhS t|tj	r,tj
hS t|tjr6tjhS t|tjrC| j|j jS t|tjro| j|j j}| j|j j}t }|D ]}|D ]}|t|| q`q\|S t|tjr| j|j j}|jD ]7}	t|	tjr|| j|	 _| j|	j| qt|	tjr|	jD ]}
t|
tjr|| j|
 _| j|
j| qq|S t|tj rt!|j"h}|jr| j|j j}|tj#tj$h@ s|}t|j%tjr|| j|j% _| j|j%j| |S t|tjr)|jdkrtj&hS |jdv rtjhS |j'}|r"t|j tj(r"| )||S | j*|jS t|tj+r`|jd u r:tj&hS | j|j j}| jj,j-t.j/ksNJ d| jj,j0}| j1 }|2|| |S t|tj3rt4|rt|j5tj6r|j5j7dkrtj8hS t9|rtj:hS t;|rtj<hS t|j5tjr| j*|j5jS t|tj=r| >|jrtj8hS tj$hS )NNone>   FalseTruezReturn at non-function scopeZto_variable)?r   r%   r   rW   rc   BoolOpr   r\   CompareDictDICTSetZSETUnaryOprA   operandr   BinOpleftrightr&   r(   Zbinary_op_output_typeAssignrY   targetsNamerB   r$   idTupleelts	AnnAssigntype_from_annotation
annotationr   Z	STATEMENTtargetrZ   r   	arguments_get_func_argument_typer#   Returnr0   r    r   r-   r   r6   r+   Callr   func	AttributeattrrS   r   rT   r   ZNUMPY_NDARRAY	SubscriptrV   )r   rK   r   Z	left_typeZ
right_typeZresult_typelrZret_typerz   Z
sub_targetZnode_value_typeparent_node_wrapperreturn_type	func_namer!   r   r   r   rJ      s   








z(StaticAnalysisVisitor._get_node_var_typec                 C   s   t |tjsJ |j}tjh}|jdur#t|jh}| j	|j
| |jrXt|j|}t|j}|dkrX|| t|jkrX|j||  }t |tjrX| |}| j	|j
| |S )aj  
        Returns type information by parsing annotation or default values.

        For example:
            1. parse by default values.
                foo(x, y=1, z='s') -> x: UNKNOWN, y: INT, z: STR

            2. parse by Py3 type annotation.
                foo(x: Tensor, y: int, z: str) -> x: Tensor, y: INT, z: STR

            3. parse by type annotation and default values.
                foo(x: Tensor, y: int, z: str = 'abc') -> x: Tensor, y: INT, z: STR

        NOTE: Currently, we only support Tensor, int, bool, float, str et.al.
              Other complicate types will be supported later.
        NrD   )r%   r   rs   r   r   r   ry   rx   rB   r$   rt   defaultsr   argsrE   rW   rc   )r   r   r   Zparent_noder*   indexZargs_lenZdefaults_noder   r   r   r|   W  s   


z-StaticAnalysisVisitor._get_func_argument_typer
   )r   r   r   r   r   r=   rC   rO   rQ   rR   rV   rc   rJ   r|   r   r   r   r   r<      s    
!nr<   N)Zpaddle.utilsr   Zutils_helperr   r   r   r   r   __all__r	   r   r.   r<   r   r   r   r   <module>   s   62