
    `i                       d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ej         G d	 d
                      Zej         G d d                      Z ej        dd           G d d                      Z ej        d           G d de
                      Zej         G d d                      Zej         G d d                      ZdS )z0Common utilities for the `tvm-ffi-stubgen` tool.    )annotationsN)StringIO)Callable)TypeInfo
TypeSchema   )constsc                  6    e Zd ZU dZded<   	 ded<   	 ded<   dS )
InitConfiga  Configuration for generating new stubs.

    Examples
    --------
    If we are generating type stubs for Python package `my-ffi-extension`,
    and the CMake target that generates the shared library is `my_ffi_extension_shared`,
    then we can run the following command to generate the stubs:

    --init-pypkg my-ffi-extension --init-lib my_ffi_extension_shared --init-prefix my_ffi_extension.

    strpkgshared_targetprefixN)__name__
__module____qualname____doc____annotations__     f/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/tvm_ffi/stub/utils.pyr   r      sF         
 
 HHHd KKKddr   r   c                      e Zd ZU dZ ej        e          Zded<    ej        e          Z	ded<   dZ
ded<   d	Zd
ed<    ej        e          Zded<   dZded<   dZded<   dS )Optionsz)Command line options for stub generation.)default_factory	list[str]importsdllsNzInitConfig | Noneinit   intindentfilesFboolverbosedry_run)r   r   r   r   dataclassesfieldlistr   r   r   r   r!   r"   r$   r%   r   r   r   r   r   <   s         33**4@@@G@@@@'k'===D===="D""""FOOOO({(>>>E>>>>GGr   r   T)frozeneqc                      e Zd ZU dZded<   ded<   dZded<   dZd	ed
<   	 	 dddZedd            Z	edd            Z
ddZddZdS )
ImportItemzAn import statement item.r   modnameFr#   type_checking_onlyN
str | NonealiasreturnNonec                   d|v rl|                     dd          \  }}t          j                                        D ]3\  }}|                    |          r|                    ||d          } n4nd}t                              | d|           t                              | d|           t                              | d|           t                              | d|           dS )	zIInitialize an `ImportItem` with the given module name and optional alias..r    r-   r.   r/   r1   N)rsplitCMOD_MAPitems
startswithreplaceobject__setattr__)selfr.   r/   r1   r-   
mod_prefixmod_replacements          r   __init__zImportItem.__init__R   s     $;;C++IC/0y/@/@  +
O>>*-- ++j/1EECE C4,,,4...4!57IJJJ4%00000r   c                @    | j         r| j         d| j          n| j        S )zXGenerate a string of the form `name as alias` if an alias is set, otherwise just `name`.z as )r1   r.   r?   s    r   name_with_aliaszImportItem.name_with_aliasf   s*     26J$)-----Jr   c                @    | j         r| j          d| j         n| j        S )zGGenerate a string of the form `mod.name` or `name` if no module is set.r5   )r-   r.   rD   s    r   	full_namezImportItem.full_namek   s*     -1HC$(((TY((($)Cr   c                     t          |           S )2Generate an import statement string for this item.)r   rD   s    r   __repr__zImportItem.__repr__p   s    4yyr   c                N    | j         rd| j          d| j         }n
d| j         }|S )rI   zfrom z import zimport )r-   rE   )r?   rets     r   __str__zImportItem.__str__t   s=    8 	3B$(BBD,@BBCC2D022C
r   )FN)r.   r   r/   r#   r1   r0   r2   r3   )r2   r   )r   r   r   r   r   r/   r1   rB   propertyrE   rG   rJ   rM   r   r   r   r,   r,   I   s         ##HHHIII$$$$$E
 $) 	1 1 1 1 1( K K K XK D D D XD        r   r,   F)r   c                  0     e Zd ZU dZded<   d	 fdZ xZS )
NamedTypeSchemaz&A type schema with an associated name.r   r.   schemar   r2   r3   c                p    t                                          |j        |j                   || _        dS )zCInitialize a `NamedTypeSchema` with the given name and type schema.)originargsN)superrB   rS   rT   r.   )r?   r.   rQ   	__class__s      r   rB   zNamedTypeSchema.__init__   s/    FK@@@			r   )r.   r   rQ   r   r2   r3   )r   r   r   r   r   rB   __classcell__)rV   s   @r   rP   rP   }   sN         00III         r   rP   c                  N    e Zd ZU dZded<   ded<   edddd            ZddZdS )FuncInfozInformation of a function.rP   rQ   r#   	is_memberF)rZ   r.   r   r   r2   c               B    t          t          | |          |          S )z7Construct a `FuncInfo` from a name and its type schema.r.   rQ   rQ   rZ   )rY   rP   )r.   rQ   rZ   s      r   from_schemazFuncInfo.from_schema   s%     DHHHT]^^^^r   ty_mapCallable[[str], str]r!   r    c                2   	 | j         j                            dd          \  }}n# t          $ r | j         j        }Y nw xY wt	                      }|                    d|z             |                    d| d           | j         j        dk    rt          d| j                    | j         j        s4 |d           |                    d	           |                                S | j         j        d
         }| j         j        dd         }t          |          D ]~\  }}	| j
        r|d
k    r|                    d           (|                    d| d           |                    |	                    |                     |                    d           |r|                    d           |                    d           |                    |                    |                     |                    d           |                                S )z7Generate a function signature string for this function.r5   r    zdef (r   z(Expected Callable type schema, but got: Anyz*args: Any) -> Any: ...r   Nzself, _: z, /z) -> z: ...)rQ   r.   r7   
ValueErrorr   writerS   rT   getvalue	enumeraterZ   repr)
r?   r_   r!   re   	func_namebufarg_retarg_argsiargs
             r   genzFuncInfo.gen   s   	);+223::LAyy 	) 	) 	)(III	)jj		#,		%%%%&&&;++UUUVVV{ 	"F5MMMII/000<<>>!+"1%;#ABB')) 	  	 FAs~  !q&&		(####		)a)))$$$		#((6**+++		$ 	IIcNNN		'		',,v&&'''		'||~~s   #& ??N)r.   r   rQ   r   rZ   r#   r2   rY   )r_   r`   r!   r    r2   r   )r   r   r   r   r   staticmethodr^   rs   r   r   r   rY   rY      sv         $$OOOHM _ _ _ _ _ \_     r   rY   c                  l    e Zd ZU dZded<   ded<   dZded<   dZded	<   edd            ZddZ	ddZ
dS )
ObjectInfoz@Information of an object type, including its fields and methods.zlist[NamedTypeSchema]fieldszlist[FuncInfo]methodsNr0   type_keyparent_type_key	type_infor   r2   c                    d}| j         | j         j        }t          d | j        D             d | j        D             | j        |          S )z5Construct an `ObjectInfo` from a `TypeInfo` instance.Nc           	     t    g | ]5}t          |j        t          j        |j        d                              6S )type_schemar\   )rP   r.   r   from_json_strmetadata).0r'   s     r   
<listcomp>z-ObjectInfo.from_type_info.<locals>.<listcomp>   sS       
 	  %3EN=4QRR    r   c                    g | ]m}t          t          t          j                            |j        |j                  t          j        |j        d                              |j	                   nS )r~   r\   r]   )
rY   rP   r8   FN_NAME_MAPgetr.   r   r   r   	is_static)r   methods     r   r   z-ObjectInfo.from_type_info.<locals>.<listcomp>   s     	 	 	  *]..v{FKHH)78VWW   #)"22  	 	 	r   )rw   rx   ry   rz   )parent_type_infory   rv   rw   rx   )r{   rz   s     r   from_type_infozObjectInfo.from_type_info   s     '+%1'8AO 
 '-  	 	 (/	 	 	 '+'
 
 
 	
r   r_   r`   r!   r    r   c                8    d|z  fd| j         D             S )z+Generate field definitions for this object.rb   c                R    g | ]#} |j          d |                               $S )rf   )r.   rl   )r   r'   
indent_strr_   s     r   r   z)ObjectInfo.gen_fields.<locals>.<listcomp>   s:    [[[e:AuzAAUZZ-?-?AA[[[r   )rw   )r?   r_   r!   r   s    ` @r   
gen_fieldszObjectInfo.gen_fields   s,    6\
[[[[[t{[[[[r   c                    d|z  }g }| j         D ]J}|j        s|                    | d           |                    |                    ||                     K|S )z,Generate method definitions for this object.rb   z@staticmethod)rx   rZ   appendrs   )r?   r_   r!   r   rL   r   s         r   gen_methodszObjectInfo.gen_methods   so    6\
l 	3 	3F# 9

j777888JJvzz&&112222
r   )r{   r   r2   rv   )r_   r`   r!   r    r2   r   )r   r   r   r   r   ry   rz   rt   r   r   r   r   r   r   rv   rv      s         JJ!!!!H"&O&&&&
 
 
 \
6\ \ \ \
     r   rv   )r   
__future__r   r&   ior   typingr   tvm_ffi.corer   r   r6   r	   r8   	dataclassr   r   r,   rP   rY   rv   r   r   r   <module>r      s  " 7 6 " " " " " "                 - - - - - - - -       e e e e e e e e: 	 	 	 	 	 	 	 	 dt,,,0 0 0 0 0 0 0 -,0f E"""    j   #" ( ( ( ( ( ( ( (V 1 1 1 1 1 1 1 1 1 1r   