
    `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
mZmZmZ ddlmZ dd	lmZmZmZmZ d
dlmZ d
dlmZ  ed          Z eef          	 ddd            ZddZdS ) a  Helpers for mirroring registered C++ FFI types with Python dataclass syntax.

The :func:`c_class` decorator is the primary entry point.  It inspects the
reflection metadata that the C++ runtime exposes via the TVM FFI registry and
turns it into Python ``dataclass``-style descriptors: annotated attributes become
properties that forward to the underlying C++ object, while an ``__init__``
method is synthesized to call the FFI constructor when requested.
    )annotationsN)Callable)InitVar)ClassVarTypeTypeVar
get_originget_type_hints)dataclass_transform   )	TypeFieldTypeInfo+_lookup_or_register_type_info_from_type_key_set_type_cls   )_utils)field_InputClsType)field_specifiersTtype_keystrinitboolreturn4Callable[[Type[_InputClsType]], Type[_InputClsType]]c                     d fd}|S )a
  (Experimental) Create a dataclass-like proxy for a C++ class registered with TVM FFI.

    The decorator reads the reflection metadata that was registered on the C++
    side using ``tvm::ffi::reflection::ObjectDef`` and binds it to the annotated
    attributes in the decorated Python class. Each field defined in C++ becomes
    a property on the Python class, and optional default values can be provided
    with :func:`tvm_ffi.dataclasses.field` in the same way as Python's native
    ``dataclasses.field``.

    The intent is to offer a familiar dataclass authoring experience while still
    exposing the underlying C++ object.  The ``type_key`` of the C++ class must
    match the string passed to :func:`c_class`, and inheritance relationships are
    preserved-subclasses registered in C++ can subclass the Python proxy defined
    for their parent.

    Parameters
    ----------
    type_key
        The reflection key that identifies the C++ type in the FFI registry,
        e.g. ``"testing.MyClass"`` as registered in
        ``src/ffi/extra/testing.cc``.

    init
        If ``True`` and the Python class does not define ``__init__``, an
        initializer is auto-generated that mirrors the reflected constructor
        signature.  The generated initializer calls the C++ ``__init__``
        function registered with ``ObjectDef`` and invokes ``__post_init__`` if
        it exists on the Python class.

    Returns
    -------
    Callable[[type], type]
        A class decorator that materializes the final proxy class.

    Examples
    --------
    Register the C++ type and its fields with TVM FFI:

    .. code-block:: c++

        TVM_FFI_STATIC_INIT_BLOCK() {
          namespace refl = tvm::ffi::reflection;
          refl::ObjectDef<MyClass>()
              .def_static("__init__", [](int64_t v_i64, int32_t v_i32,
                                         double v_f64, float v_f32) -> Any {
                   return ObjectRef(ffi::make_object<MyClass>(
                       v_i64, v_i32, v_f64, v_f32));
               })
              .def_rw("v_i64", &MyClass::v_i64)
              .def_rw("v_i32", &MyClass::v_i32)
              .def_rw("v_f64", &MyClass::v_f64)
              .def_rw("v_f32", &MyClass::v_f32);
        }

    Mirror the same structure in Python using dataclass-style annotations:

    .. code-block:: python

        from tvm_ffi.dataclasses import c_class, field


        @c_class("example.MyClass")
        class MyClass:
            v_i64: int
            v_i32: int
            v_f64: float = field(default=0.0)
            v_f32: float = field(default_factory=lambda: 1.0)


        obj = MyClass(v_i64=4, v_i32=8)
        obj.v_f64 = 3.14  # transparently forwards to the underlying C++ object

    super_type_clsType[_InputClsType]r   c                :   od| j         vt                    }|j        J t          | |          |_        |j        D ]}t          j        | |           rt          j        | |          nd }t          j        || d|i          }t          ||           |S )N__init__)	type_infoclsmethods)
__dict__r   parent_type_info_inspect_c_class_fieldsfieldsr   fill_dataclass_fieldmethod_inittype_info_to_clsr   )r   r!   
type_fieldfn_inittype_clsr   r   s        o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/tvm_ffi/dataclasses/c_class.py	decoratorzc_class.<locals>.decoratorx   s    A
.*AAI(SS	)5552>9MM	#* 	D 	DJ'
CCCCCGQ&$^Y???T(.(?))
 )
 )

 	i***    )r   r   r   r    )r   r   r/   s   `` r.   c_classr2   *   s0    \      ( r0   r-   typer!   r   list[TypeField]c                h  	 t           j        dk    rt          | d          	nt          |           		fdt          | di                                           D             }	d |j        D             }g }|                                D ]^\  }}|                    d          r|                    |d           }|t          d|  d	| d
          |
                    |           _|rGd                    d |                                D                       }t          d|  d| d          |S )N)   	   T)include_extrasc                h    i | ].}t          |                   t          t          fv%||         /S r1   )r	   r   r   ).0nametype_hints_resolveds     r.   
<dictcomp>z+_inspect_c_class_fields.<locals>.<dictcomp>   sW       )$/00

 
 	!$'
 
 
r0   __annotations__c                    i | ]
}|j         |S r1   r;   r:   fs     r.   r=   z+_inspect_c_class_fields.<locals>.<dictcomp>   s    ,Q,Q,Q1QVQ,Q,Q,Qr0   	__tvm_ffizExtraneous field `.z#`. Defined in Python but not in C++z, c              3  ,   K   | ]}d |j          d V  dS )`Nr@   rA   s     r.   	<genexpr>z*_inspect_c_class_fields.<locals>.<genexpr>   s,       Q Q1QV Q Q Q Q Q Qr0   zMissing fields in `z`: z". Defined in C++ but not in Python)sysversion_infor
   getattrkeysr'   items
startswithpop
ValueErrorappendjoinvalues)
r-   r!   type_hints_pytype_fields_cxxtype_fields
field_name_field_ty_pyr+   extra_fieldsr<   s
            @r.   r&   r&      s   
6!!,XdKKK,X66   H&7<<AACC  M 	,Q,Q	@P,Q,Q,QO#%K$1$7$7$9$9 ' ' 
L  -- 	$((T::
_X__
___   	:&&&& 
yy Q Q8N8N8P8P Q Q QQQ_(__|___
 
 	
 r0   )T)r   r   r   r   r   r   )r-   r3   r!   r   r   r4   )__doc__
__future__r   rH   collections.abcr   dataclassesr   typingr   r   r   r	   r
   typing_extensionsr   corer   r   r   r    r   r   r   r2   r&   r1   r0   r.   <module>ra      s@  "  # " " " " " 



 $ $ $ $ $ $       F F F F F F F F F F F F F F 1 1 1 1 1 1 b b b b b b b b b b b b            (( uh/// $a a a a 0/aH           r0   