rustc_middle/ty/consts/
kind.rs

1use std::assert_matches::assert_matches;
2
3use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
4
5use super::Const;
6use crate::mir;
7use crate::ty::abstract_const::CastKind;
8use crate::ty::{self, Ty, TyCtxt};
9
10#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
11#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
12pub enum ExprKind {
13    Binop(mir::BinOp),
14    UnOp(mir::UnOp),
15    FunctionCall,
16    Cast(CastKind),
17}
18#[derive(Copy, Clone, Eq, PartialEq, Hash)]
19#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
20pub struct Expr<'tcx> {
21    pub kind: ExprKind,
22    args: ty::GenericArgsRef<'tcx>,
23}
24
25impl<'tcx> rustc_type_ir::inherent::ExprConst<TyCtxt<'tcx>> for Expr<'tcx> {
26    fn args(self) -> ty::GenericArgsRef<'tcx> {
27        self.args
28    }
29}
30
31impl<'tcx> Expr<'tcx> {
32    pub fn new_binop(
33        tcx: TyCtxt<'tcx>,
34        binop: mir::BinOp,
35        lhs_ty: Ty<'tcx>,
36        rhs_ty: Ty<'tcx>,
37        lhs_ct: Const<'tcx>,
38        rhs_ct: Const<'tcx>,
39    ) -> Self {
40        let args = tcx.mk_args_from_iter::<_, ty::GenericArg<'tcx>>(
41            [lhs_ty.into(), rhs_ty.into(), lhs_ct.into(), rhs_ct.into()].into_iter(),
42        );
43
44        Self { kind: ExprKind::Binop(binop), args }
45    }
46
47    pub fn binop_args(self) -> (Ty<'tcx>, Ty<'tcx>, Const<'tcx>, Const<'tcx>) {
48        assert_matches!(self.kind, ExprKind::Binop(_));
49
50        match self.args().as_slice() {
51            [lhs_ty, rhs_ty, lhs_ct, rhs_ct] => (
52                lhs_ty.expect_ty(),
53                rhs_ty.expect_ty(),
54                lhs_ct.expect_const(),
55                rhs_ct.expect_const(),
56            ),
57            _ => bug!("Invalid args for `Binop` expr {self:?}"),
58        }
59    }
60
61    pub fn new_unop(tcx: TyCtxt<'tcx>, unop: mir::UnOp, ty: Ty<'tcx>, ct: Const<'tcx>) -> Self {
62        let args =
63            tcx.mk_args_from_iter::<_, ty::GenericArg<'tcx>>([ty.into(), ct.into()].into_iter());
64
65        Self { kind: ExprKind::UnOp(unop), args }
66    }
67
68    pub fn unop_args(self) -> (Ty<'tcx>, Const<'tcx>) {
69        assert_matches!(self.kind, ExprKind::UnOp(_));
70
71        match self.args().as_slice() {
72            [ty, ct] => (ty.expect_ty(), ct.expect_const()),
73            _ => bug!("Invalid args for `UnOp` expr {self:?}"),
74        }
75    }
76
77    pub fn new_call(
78        tcx: TyCtxt<'tcx>,
79        func_ty: Ty<'tcx>,
80        func_expr: Const<'tcx>,
81        arguments: impl IntoIterator<Item = Const<'tcx>>,
82    ) -> Self {
83        let args = tcx.mk_args_from_iter::<_, ty::GenericArg<'tcx>>(
84            [func_ty.into(), func_expr.into()]
85                .into_iter()
86                .chain(arguments.into_iter().map(|ct| ct.into())),
87        );
88
89        Self { kind: ExprKind::FunctionCall, args }
90    }
91
92    pub fn call_args(self) -> (Ty<'tcx>, Const<'tcx>, impl Iterator<Item = Const<'tcx>>) {
93        assert_matches!(self.kind, ExprKind::FunctionCall);
94
95        match self.args().as_slice() {
96            [func_ty, func, rest @ ..] => (
97                func_ty.expect_ty(),
98                func.expect_const(),
99                rest.iter().map(|arg| arg.expect_const()),
100            ),
101            _ => bug!("Invalid args for `Call` expr {self:?}"),
102        }
103    }
104
105    pub fn new_cast(
106        tcx: TyCtxt<'tcx>,
107        cast: CastKind,
108        value_ty: Ty<'tcx>,
109        value: Const<'tcx>,
110        to_ty: Ty<'tcx>,
111    ) -> Self {
112        let args = tcx.mk_args_from_iter::<_, ty::GenericArg<'tcx>>(
113            [value_ty.into(), value.into(), to_ty.into()].into_iter(),
114        );
115
116        Self { kind: ExprKind::Cast(cast), args }
117    }
118
119    pub fn cast_args(self) -> (Ty<'tcx>, Const<'tcx>, Ty<'tcx>) {
120        assert_matches!(self.kind, ExprKind::Cast(_));
121
122        match self.args().as_slice() {
123            [value_ty, value, to_ty] => {
124                (value_ty.expect_ty(), value.expect_const(), to_ty.expect_ty())
125            }
126            _ => bug!("Invalid args for `Cast` expr {self:?}"),
127        }
128    }
129
130    pub fn new(kind: ExprKind, args: ty::GenericArgsRef<'tcx>) -> Self {
131        Self { kind, args }
132    }
133
134    pub fn args(self) -> ty::GenericArgsRef<'tcx> {
135        self.args
136    }
137}
138
139#[cfg(target_pointer_width = "64")]
140rustc_data_structures::static_assert_size!(Expr<'_>, 16);