o
    ٰi                     @  s   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
 d
ZejG dd dZejdddG dd dZejddG dd de
ZejG dd dZejG dd dZdS )z0Common utilities for the `tvm-ffi-stubgen` tool.    )annotationsN)StringIO)Callable)TypeInfo
TypeSchema   )constsc                   @  s.   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__ r   r   F/home/ubuntu/.local/lib/python3.10/site-packages/tvm_ffi/stub/utils.pyr	      s   
 r	   c                   @  s~   e Zd ZU dZejedZded< ejedZ	ded< dZ
ded< d	Zd
ed< ejed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   
 r   T)frozeneqc                   @  sz   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dZedddZ	edddZ
dddZdddZdS )
ImportItemzAn import statement item.r
   modnameFr   type_checking_onlyN
str | NonealiasreturnNonec                 C  s   d|v r%| dd\}}tj D ]\}}||r#|||d} nqn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*   r,   N)rsplitCMOD_MAPitems
startswithreplaceobject__setattr__)selfr)   r*   r,   r(   
mod_prefixmod_replacementr   r   r   __init__R   s   
zImportItem.__init__c                 C  s   | j r| j d| j  S | jS )zXGenerate a string of the form `name as alias` if an alias is set, otherwise just `name`.z as )r,   r)   r9   r   r   r   name_with_aliasf      zImportItem.name_with_aliasc                 C  s   | j r| j  d| j S | jS )zGGenerate a string of the form `mod.name` or `name` if no module is set.r/   )r(   r)   r=   r   r   r   	full_namek   r?   zImportItem.full_namec                 C  s   t | S )2Generate an import statement string for this item.)r
   r=   r   r   r   __repr__p   s   zImportItem.__repr__c                 C  s.   | j rd| j  d| j }|S d| j }|S )rA   zfrom z import zimport )r(   r>   )r9   retr   r   r   __str__t   s
   zImportItem.__str__)FN)r)   r
   r*   r   r,   r+   r-   r.   )r-   r
   )r   r   r   r   r   r*   r,   r<   propertyr>   r@   rB   rD   r   r   r   r   r'   I   s   
 
r'   F)r   c                      s,   e Zd ZU dZded< d
 fdd	Z  ZS )NamedTypeSchemaz&A type schema with an associated name.r
   r)   schemar   r-   r.   c                   s   t  j|j|jd || _dS )zCInitialize a `NamedTypeSchema` with the given name and type schema.)originargsN)superr<   rH   rI   r)   )r9   r)   rG   	__class__r   r   r<      s   
zNamedTypeSchema.__init__)r)   r
   rG   r   r-   r.   )r   r   r   r   r   r<   __classcell__r   r   rK   r   rF   }   s   
 rF   c                   @  s@   e Zd ZU dZded< ded< edddddZdddZdS )FuncInfozInformation of a function.rF   rG   r   	is_memberF)rO   r)   r
   r   r-   c                C  s   t t| |d|dS )z7Construct a `FuncInfo` from a name and its type schema.r)   rG   rG   rO   )rN   rF   )r)   rG   rO   r   r   r   from_schema   s   zFuncInfo.from_schematy_mapCallable[[str], str]r   r   c           
      C  sF  z| j jdd\}}W n ty   | j j}Y nw t }|d|  |d| d | j jdkr;td| j  | j jsL|d |d	 | S | j jd
 }| j jdd }t	|D ]'\}}	| j
ro|d
kro|d q^|d| d ||	| |d q^|r|d |d ||| |d | S )z7Generate a function signature string for this function.r/   r    zdef (r   z(Expected Callable type schema, but got: Anyz*args: Any) -> Any: ...r   Nzself, _: z, /z) -> z: ...)rG   r)   r1   
ValueErrorr   writerH   rI   getvalue	enumeraterO   repr)
r9   rS   r   rX   	func_namebufarg_retarg_argsiargr   r   r   gen   s8   



zFuncInfo.genN)r)   r
   rG   r   rO   r   r-   rN   )rS   rT   r   r   r-   r
   )r   r   r   r   r   staticmethodrR   rf   r   r   r   r   rN      s   
 rN   c                   @  s\   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dZdddZ	dddZ
dS )
ObjectInfoz@Information of an object type, including its fields and methods.zlist[NamedTypeSchema]fieldszlist[FuncInfo]methodsNr+   type_keyparent_type_key	type_infor   r-   c                 C  s@   d}| j dur| j j}tdd | jD dd | jD | j|dS )z5Construct an `ObjectInfo` from a `TypeInfo` instance.Nc                 S  s&   g | ]}t |jt|jd  dqS )type_schemarP   )rF   r)   r   from_json_strmetadata.0r#   r   r   r   
<listcomp>   s    z-ObjectInfo.from_type_info.<locals>.<listcomp>c              	   S  s>   g | ]}t ttj|j|jt|jd  d|j	 dqS )rn   rP   rQ   )
rN   rF   r2   FN_NAME_MAPgetr)   r   ro   rp   	is_static)rr   methodr   r   r   rs      s    )ri   rj   rk   rl   )parent_type_infork   rh   ri   rj   )rm   rl   r   r   r   from_type_info   s   

zObjectInfo.from_type_inforS   rT   r   r   r   c                   s   d|   fdd| j D S )z+Generate field definitions for this object.rU   c                   s&   g | ]}  |j  d | qS )rY   )r)   r_   rq   
indent_strrS   r   r   rs      s   & z)ObjectInfo.gen_fields.<locals>.<listcomp>)ri   )r9   rS   r   r   rz   r   
gen_fields   s   zObjectInfo.gen_fieldsc                 C  sD   d| }g }| j D ]}|js|| d |||| q	|S )z,Generate method definitions for this object.rU   z@staticmethod)rj   rO   appendrf   )r9   rS   r   r{   rC   rw   r   r   r   gen_methods   s   
zObjectInfo.gen_methods)rm   r   r-   rh   )rS   rT   r   r   r-   r   )r   r   r   r   r   rk   rl   rg   ry   r|   r~   r   r   r   r   rh      s   
 
rh   )r   
__future__r   r"   ior   typingr   tvm_ffi.corer   r   r0   r   r2   	dataclassr	   r   r'   rF   rN   rh   r   r   r   r   <module>   s&   
3+