tvm_tirx/macro/def_expr_op_overloads.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
//! Expression operators for existing reference wrappers.
/// Add primitive-expression operators to an existing reference wrapper.
///
/// Pass a module-scope path to a type implementing `Clone` and `Into<Expr>`. Define its object
/// layout, reference wrapper and ancestor upcasts separately, using `tvm_ffi::def_object_ref!` or
/// generated bindings. For a deeper parent, also provide the upcast to `Expr`.
///
/// Owned operands move their handle; borrowed operands clone the handle. Operators return
/// [`ExprResult`](crate::ir::prim::ExprResult), preserving contextual literal typing and the
/// first construction error. Import [`PrimOps`](crate::ir::prim::PrimOps) for named operations
/// such as `floordiv` and `less`. Assignment operators apply only to `ExprResult`.
///
/// ```text
/// tvm_ffi::def_object_ref!(pub MyExpr, Expr, MyExprObj);
/// tvm_tirx::def_expr_op_over_loads!(MyExpr);
/// ```
#[macro_export]
macro_rules! def_expr_op_over_loads {
(crate::$($ty:ident)::+ $(,)?) => {
$crate::def_expr_op_over_loads!(@impl crate::$($ty)::+);
};
(self::$($ty:ident)::+ $(,)?) => {
$crate::def_expr_op_over_loads!(@impl self::$($ty)::+);
};
(super::$($ty:ident)::+ $(,)?) => {
$crate::def_expr_op_over_loads!(@impl super::$($ty)::+);
};
(::$($ty:ident)::+ $(,)?) => {
$crate::def_expr_op_over_loads!(@impl ::$($ty)::+);
};
($($ty:ident)::+ $(,)?) => {
// Anchor caller names before introducing generic operator parameters.
$crate::def_expr_op_over_loads!(@impl self::$($ty)::+);
};
(@impl $ty:ty) => {
impl ::core::convert::From<$ty> for $crate::ir::prim::ExprResult {
fn from(value: $ty) -> Self {
Self::from(::core::result::Result::<$crate::ir::Expr, $crate::tvm_ffi::Error>::Ok(
::core::convert::Into::<$crate::ir::Expr>::into(value),
))
}
}
impl ::core::convert::From<&$ty> for $crate::ir::prim::ExprResult {
fn from(value: &$ty) -> Self {
Self::from(<$ty as ::core::clone::Clone>::clone(value))
}
}
impl $crate::ir::prim::PrimOps for $ty {
fn to_expr_result(&self) -> $crate::ir::prim::ExprResult {
::core::convert::From::from(self)
}
}
$crate::def_expr_op_over_loads!(@binary $ty;
Add::add, Sub::sub, Mul::mul, BitAnd::bitand, BitOr::bitor,
BitXor::bitxor, Shl::shl, Shr::shr
);
$crate::def_expr_op_over_loads!(@unary $ty; Neg::neg, Not::not);
};
(@binary $ty:ty; $($trait:ident::$method:ident),+ $(,)?) => {
$(
impl<R: $crate::ir::prim::Operand> ::core::ops::$trait<R> for $ty {
type Output = $crate::ir::prim::ExprResult;
fn $method(self, rhs: R) -> Self::Output {
<Self::Output as ::core::ops::$trait<R>>::$method(
::core::convert::From::from(self), rhs,
)
}
}
impl<R: $crate::ir::prim::Operand> ::core::ops::$trait<R> for &$ty {
type Output = $crate::ir::prim::ExprResult;
fn $method(self, rhs: R) -> Self::Output {
<Self::Output as ::core::ops::$trait<R>>::$method(
::core::convert::From::from(self), rhs,
)
}
}
$crate::def_expr_op_over_loads!(@literal $ty, $trait::$method;
::core::primitive::i32, ::core::primitive::i64,
::core::primitive::f32, ::core::primitive::f64);
)+
};
(@literal $ty:ty, $trait:ident::$method:ident; $($literal:ty),+ $(,)?) => {
$(
impl ::core::ops::$trait<$ty> for $literal {
type Output = $crate::ir::prim::ExprResult;
fn $method(self, rhs: $ty) -> Self::Output {
<Self as ::core::ops::$trait<Self::Output>>::$method(
self, ::core::convert::From::from(rhs),
)
}
}
impl ::core::ops::$trait<&$ty> for $literal {
type Output = $crate::ir::prim::ExprResult;
fn $method(self, rhs: &$ty) -> Self::Output {
<Self as ::core::ops::$trait<Self::Output>>::$method(
self, ::core::convert::From::from(rhs),
)
}
}
)+
};
(@unary $ty:ty; $($trait:ident::$method:ident),+ $(,)?) => {
$(
impl ::core::ops::$trait for $ty {
type Output = $crate::ir::prim::ExprResult;
fn $method(self) -> Self::Output {
<Self::Output as ::core::ops::$trait>::$method(
::core::convert::From::from(self),
)
}
}
impl ::core::ops::$trait for &$ty {
type Output = $crate::ir::prim::ExprResult;
fn $method(self) -> Self::Output {
<Self::Output as ::core::ops::$trait>::$method(
::core::convert::From::from(self),
)
}
}
)+
};
}