
    `i$%                        d Z ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZmZ d.dZd/dZd0dZd1dZd2dZd3dZd4d)Z	 d5d6d,Zd-S )7z5Code generation logic for the `tvm-ffi-stubgen` tool.    )annotations)Callable   )consts)	CodeBlock)FuncInfo
ImportItem
InitConfig
ObjectInfoOptionsty_mapdict[str, str]importslist[ImportItem]returnCallable[[str], str]c                     d fd}|S )Nnamestrr   c                                         | |           } d| v r%                    t          | dd                      |                     dd          d         S )N.Ttype_checking_onlyaliasr   )getappendr	   rsplit)r   r   r   s    h/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/tvm_ffi/stub/codegen.py_runz%_type_suffix_and_record.<locals>._run   sY    zz$%%$;;NN:dt4PPPQQQ{{3""2&&    )r   r   r   r    )r   r   r    s   `` r   _type_suffix_and_recordr#      s.    ' ' ' ' ' ' ' Kr!   coder   global_funcslist[FuncInfo]optr   Nonec                  	 t          | j                  dk    sJ |sdS t          | j        t                    sJ | j        \  }}|sd}|                    t          | ddd          t          dd	          g           t          ||          d
d| ddgfd|D             d}d| j        z  	| j        d         g	fd|D             | j        d         | _        dS )ztGenerate function signatures for global functions.

    It processes: global/${prefix}@${import_from="tvm_ffi")
       Ntvm_ffiz.init_ffi_apiF_FFI_INIT_FUNCr   typing.TYPE_CHECKINGr   
# fmt: offz_FFI_INIT_FUNC("z", __name__)if TYPE_CHECKING:c                H    g | ]}|                     j                   S )indent)genr3   ).0func	fn_ty_mapr'   s     r   
<listcomp>z)generate_global_funcs.<locals>.<listcomp>M   s+    	J	J	JT$((9SZ(
0
0	J	J	Jr!   	# fmt: on r   c                    g | ]}|z   S r"   r"   r5   liner3   s     r   r8   z)generate_global_funcs.<locals>.<listcomp>S       	,	,	,D&4-	,	,	,r!   r   )	lenlines
isinstanceparamtupleextendr	   r#   r3   )
r$   r%   r   r   r'   prefiximport_fromresultsr7   r3   s
       `   @@r   generate_global_funcsrH   )   s_    tz??a dj%(((((*FK  NN---#(&  
 &#(  
	
   (88I/6/// 
K	J	J	J	J\	J	J	J	
 	G 4;F
1	,	,	,	,G	,	,	, 	
2DJJJr!   obj_infor   c                   t          | j                  dk    sJ |}t          ||          }|j        r_|                    t          dd                     dg|                    |d          d|                    ||j                  d	}ndg|                    |d          d	}d
| j        z  | j        d         gfd|D             | j        d         | _        dS )zZGenerate a class definition for an object type.

    It processes: object/${type_key}
    r*   r-   Fr.   r/   r   r2   r0   r9   r:   c                    g | ]}|z   S r"   r"   r<   s     r   r8   z#generate_object.<locals>.<listcomp>}   r>   r!   r   N)	r?   r@   r#   methodsr   r	   
gen_fieldsgen_methodsr3   )	r$   r   r   r'   rI   infor7   rG   r3   s	           @r   generate_objectrP   X   sA    tz??aD'88I| 
&#(  	
 	
 	
 
__Yq_11
  
 i
;;	

 
 
__Yq_11
 

 4;F
1	,	,	,	,G	,	,	, 	
2DJJJr!   c                   i i |D ]f}|j         r/                    |j        g                               |           8                    |j        g                               |           gr8                    dg                               t	          dd                     ddg }r/|                    fdt                    D                        rE|                    d           |                    fdt                    D                        |r+| j        d         dddg|dd| j        d         | _        dS dS )zaGenerate import statements for the types used in the stub.

    It processes: import-section
    typingr-   Tr.   modr   itemsr   r3   intr   c                    |                     d            d                    t          t          d |D                                           }d|z  }| r
| d|  d| S | d| S )	Nc                    | j         S Nr   )items    r   <lambda>z=generate_import_section.<locals>._make_line.<locals>.<lambda>   s    DI r!   keyz, c              3  $   K   | ]}|j         V  d S rX   )name_with_alias)r5   rZ   s     r   	<genexpr>z>generate_import_section.<locals>._make_line.<locals>.<genexpr>   s%      $L$LdT%9$L$L$L$L$L$Lr!   r:   zfrom z import zimport )sortjoinsortedset)rS   rT   r3   names
indent_strs        r   
_make_linez+generate_import_section.<locals>._make_line   s    

--
...		&$L$Le$L$L$L!L!LMMNN6\
 	1 ;;s;;E;;; 00000r!   c              3  @   K   | ]} ||         d           V  dS )r   r2   Nr"   )r5   rS   rg   imports_concretes     r   r`   z*generate_import_section.<locals>.<genexpr>   sK       
 
ADJJs,S1!<<<
 
 
 
 
 
r!   r0   c              3  H   K   | ]} ||         j                   V  d S rX   r2   )r5   rS   rg   imports_ty_checkr'   s     r   r`   z*generate_import_section.<locals>.<genexpr>   sJ       
 
CFJJs,S13:>>
 
 
 
 
 
r!   r   r/   # isort: offz"from __future__ import annotations# isort: onr9   r   N)rS   r   rT   r   r3   rU   r   r   )r   
setdefaultrS   r   r	   rD   rc   r@   )r$   r   r'   rZ   rG   rg   ri   rk   s     `  @@@r   generate_import_sectionro      s    5746 C C" 	C''"55<<TBBBB''"55<<TBBBB 
##Hb1188-$GGG	
 	
 	
1 1 1 1 G 
 
 
 
 
 
HNO_H`H`
 
 
 	
 	
 	
  
*+++ 
 
 
 
 
 
JPQaJbJb
 
 
 	
 	
 	
  

JqM0		

 	
 	
 	
 JrN	





 

r!   re   set[str]c                    t          | j                  dk    sJ |sdS d| j        z  d |D             }dd	}| j        d
         gfdt          ||          D             | j        d         | _        dS )3Generate an `__all__` variable for the given names.r*   Nr:   c                F    h | ]}|                     d d          d         S )r   r   r   )r   )r5   fs     r   	<setcomp>zgenerate_all.<locals>.<setcomp>   s+    111aQXXc1b!111r!   r   r   r   tuple[int, str]c                    |                                  rd| fS | r"| d                                          rd| vrd| fS d| fS )Nr   _r   r*   )isupperrY   s    r   	_sort_keyzgenerate_all.<locals>._sort_key   sU    <<>> 	t9 	DGOO%% 	cTkkt94yr!   r   c                     g | ]
} d | dS )"z",r"   )r5   r   r3   s     r   r8   z generate_all.<locals>.<listcomp>   s*    	H	H	H4V

d


	H	H	Hr!   r\   r   )r   r   r   rv   )r?   r@   r3   rc   )r$   re   r'   rz   r3   s       @r   generate_allr}      s    tz??a 4;F115111E    	
1	H	H	H	H6%Y+G+G+G	H	H	H 	
2DJJJr!   c                    t          | j                  dk    sJ | j        }| j        d         ddd| dd| d| dd	d
d| ddd| j        d         g| _        dS )rr   r*   r   r/   rl   zfrom .z import *  # noqa: F403z import __all__ as __all__zif "__all__" not in globals():z    __all__ = []z__all__.extend(z__all__)rm   r9   r   N)r?   r@   rB   )r$   rS   s     r   generate_exportr      s    tz??a
*C
1----555555('#'''
2DJJJr!   code_blockslist[CodeBlock]module_namer   object_infoslist[ObjectInfo]init_cfgr
   is_rootboolc                p   d}| s	|d| dz  }t          d | D                       s|t          j        z  }|r.|t          j        dd          z  }|d|j         d|j         d	z  }t          d
 | D                       s|t          j        |          z  }|r|t          j        dd          z  }d |D             }|D ]}|j        }	|j        }
|	|	                    |	|	          }	|	
                    dd          d         }|
r|	                    |
|
          n|
}
|
r|

                    dd          d         nd}|
r5|
|vr1d|
                    dd          z   }|t          j        |
|          z  }|t          j        ||	|          z  }t          d | D                       s|t          j        z  }|S )z:Generate the initial FFI API stub code for a given module. z"""FFI API bindings for ."""
c              3  ,   K   | ]}|j         d k    V  dS )zimport-sectionNkindr5   r$   s     r   r`   z#generate_ffi_api.<locals>.<genexpr>   s*      EEty,,EEEEEEr!   ztvm_ffi.libinfo.load_lib_module_FFI_LOAD_LIBzLIB = _FFI_LOAD_LIB("z", "z")
c              3  ,   K   | ]}|j         d k    V  dS )globalNr   r   s     r   r`   z#generate_ffi_api.<locals>.<genexpr>   )      ==tyH$======r!   ztvm_ffi.register_object_FFI_REG_OBJc                *    h | ]}|j         	|j         S r"   )type_key)r5   rO   s     r   ru   z#generate_ffi_api.<locals>.<setcomp>  s!    QQQ44=QQQQr!   Nr   r   r   Objectrx   c              3  ,   K   | ]}|j         d k    V  dS )r   Nr   r   s     r   r`   z#generate_ffi_api.<locals>.<genexpr>  s)      >>$tyI%>>>>>>r!   )anyCPROMPT_IMPORT_SECTION_prompt_import_objectpkgshared_target_prompt_globalsr   parent_type_keyr   r   replace_prompt_class_defPROMPT_ALL_SECTION)r   r   r   r   r   r   r   defined_type_keysrO   r   r   	type_nameparent_type_names                r   generate_ffi_apir      sD    F  KJ+JJJJEEEEEEE *!))  ]!)*K_]]]\HL\\h>T\\\\ ======= 1!#K000  U!)*C^TTTQQ<QQQ 
 
=.::h11OOC++B/	<K`FJJ888Q` 	 BQ^?11#q99"==V^ 	Q6GGG"_%<%<S#%F%FFa-o?OPPPF!%
 
 	
 >>+>>>>> '!&&Mr!   _ffi_api	submodulec                    dt           j         d| dt           j         d}| s	d| d|z   S t          d | D                       s|S dS )z:Generate the `__init__.py` file for the `tvm_ffi` package.
z export/z"""Package r   c              3  ,   K   | ]}|j         d k    V  dS )exportNr   r   s     r   r`   z generate_init.<locals>.<genexpr>+  r   r!   r   )r   
STUB_BEGINSTUB_ENDr   )r   r   r   r$   s       r   generate_initr     s         D  B:+:::TAA======= 2r!   N)r   r   r   r   r   r   )r$   r   r%   r&   r   r   r   r   r'   r   r   r(   )r$   r   r   r   r   r   r'   r   rI   r   r   r(   )r$   r   r   r   r'   r   r   r(   )r$   r   re   rp   r'   r   r   r(   )r$   r   r   r(   )r   r   r   r   r   r   r   r   r   r
   r   r   r   r   )r   )r   r   r   r   r   r   r   r   )__doc__
__future__r   rR   r   r   r   r   
file_utilsr   utilsr   r	   r
   r   r   r#   rH   rP   ro   r}   r   r   r   r"   r!   r   <module>r      s7  " < ; " " " " " "             ! ! ! ! ! ! H H H H H H H H H H H H H H
 
 
 
, , , ,^' ' ' 'T2
 2
 2
 2
j   .   (: : : :@        r!   