use tvm_macros::Object;
use crate::runtime::String as TString;
use crate::runtime::{self, external, IsObject, IsObjectRef, Object, ObjectPtr, ObjectRef};
use crate::DataType;
use super::relay;
use super::span::Span;
#[repr(C)]
#[derive(Object, Debug)]
#[ref_name = "BaseExpr"]
#[type_key = "Expr"]
pub struct BaseExprNode {
pub base: Object,
pub span: Span,
}
impl BaseExprNode {
pub fn base<T: IsObject>(span: Span) -> BaseExprNode {
BaseExprNode {
base: Object::base::<T>(),
span,
}
}
}
#[repr(C)]
#[derive(Object, Debug)]
#[ref_name = "PrimExpr"]
#[type_key = "PrimExpr"]
pub struct PrimExprNode {
pub base: BaseExprNode,
pub datatype: DataType,
}
impl PrimExprNode {
pub fn base<T: IsObject>(datatype: DataType, span: Span) -> PrimExprNode {
PrimExprNode {
base: BaseExprNode::base::<T>(span),
datatype,
}
}
}
#[repr(C)]
#[derive(Object, Debug)]
#[ref_name = "GlobalVar"]
#[type_key = "GlobalVar"]
pub struct GlobalVarNode {
pub base: relay::ExprNode,
pub name_hint: TString,
}
impl GlobalVar {
pub fn new(name_hint: String, span: Span) -> GlobalVar {
let node = GlobalVarNode {
base: relay::ExprNode::base::<GlobalVarNode>(span),
name_hint: name_hint.into(),
};
GlobalVar(Some(ObjectPtr::new(node)))
}
}
external! {
#[name("ir.AsText")]
fn _as_text(object: ObjectRef, show_meta_data: i32, annotate: runtime::Function) -> TString;
}
pub fn as_text<T: IsObjectRef>(object: T) -> String {
let no_func = unsafe { runtime::Function::null() };
_as_text(object.upcast(), 0, no_func)
.unwrap()
.as_str()
.unwrap()
.into()
}