1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 * Methods for the various MIR types. These are intended for use after
13 * building is complete.
17 use ty::subst::{Subst, Substs};
18 use ty::{self, AdtDef, Ty, TyCtxt};
19 use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
22 #[derive(Copy, Clone, Debug)]
23 pub enum LvalueTy<'tcx> {
27 /// Downcast to a particular variant of an enum.
28 Downcast { adt_def: AdtDef<'tcx>,
29 substs: &'tcx Substs<'tcx>,
30 variant_index: usize },
33 impl<'a, 'gcx, 'tcx> LvalueTy<'tcx> {
34 pub fn from_ty(ty: Ty<'tcx>) -> LvalueTy<'tcx> {
35 LvalueTy::Ty { ty: ty }
38 pub fn to_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
40 LvalueTy::Ty { ty } =>
42 LvalueTy::Downcast { adt_def, substs, variant_index: _ } =>
43 tcx.mk_enum(adt_def, substs),
47 pub fn projection_ty(self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
48 elem: &LvalueElem<'tcx>)
52 ProjectionElem::Deref =>
54 ty: self.to_ty(tcx).builtin_deref(true, ty::LvaluePreference::NoPreference)
58 ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } =>
60 ty: self.to_ty(tcx).builtin_index().unwrap()
62 ProjectionElem::Subslice { from, to } => {
63 let ty = self.to_ty(tcx);
66 ty::TyArray(inner, size) => {
67 tcx.mk_array(inner, size-(from as usize)-(to as usize))
69 ty::TySlice(..) => ty,
71 bug!("cannot subslice non-array type: `{:?}`", self)
76 ProjectionElem::Downcast(adt_def1, index) =>
77 match self.to_ty(tcx).sty {
78 ty::TyEnum(adt_def, substs) => {
79 assert!(index < adt_def.variants.len());
80 assert_eq!(adt_def, adt_def1);
81 LvalueTy::Downcast { adt_def: adt_def,
83 variant_index: index }
86 bug!("cannot downcast non-enum type: `{:?}`", self)
89 ProjectionElem::Field(_, fty) => LvalueTy::Ty { ty: fty }
94 impl<'tcx> TypeFoldable<'tcx> for LvalueTy<'tcx> {
95 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
97 LvalueTy::Ty { ty } => LvalueTy::Ty { ty: ty.fold_with(folder) },
98 LvalueTy::Downcast { adt_def, substs, variant_index } => {
101 substs: substs.fold_with(folder),
102 variant_index: variant_index
108 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
110 LvalueTy::Ty { ty } => ty.visit_with(visitor),
111 LvalueTy::Downcast { substs, .. } => substs.visit_with(visitor)
116 impl<'a, 'gcx, 'tcx> Mir<'tcx> {
117 pub fn operand_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
118 operand: &Operand<'tcx>)
122 Operand::Consume(ref l) => self.lvalue_ty(tcx, l).to_ty(tcx),
123 Operand::Constant(ref c) => c.ty,
127 pub fn binop_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
133 // FIXME: handle SIMD correctly
135 BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Rem |
136 BinOp::BitXor | BinOp::BitAnd | BinOp::BitOr => {
137 // these should be integers or floats of the same size.
138 assert_eq!(lhs_ty, rhs_ty);
141 BinOp::Shl | BinOp::Shr => {
142 lhs_ty // lhs_ty can be != rhs_ty
144 BinOp::Eq | BinOp::Lt | BinOp::Le |
145 BinOp::Ne | BinOp::Ge | BinOp::Gt => {
151 pub fn lvalue_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
152 lvalue: &Lvalue<'tcx>)
156 Lvalue::Var(index) =>
157 LvalueTy::Ty { ty: self.var_decls[index].ty },
158 Lvalue::Temp(index) =>
159 LvalueTy::Ty { ty: self.temp_decls[index].ty },
160 Lvalue::Arg(index) =>
161 LvalueTy::Ty { ty: self.arg_decls[index].ty },
162 Lvalue::Static(def_id) =>
163 LvalueTy::Ty { ty: tcx.lookup_item_type(def_id).ty },
164 Lvalue::ReturnPointer =>
165 LvalueTy::Ty { ty: self.return_ty.unwrap() },
166 Lvalue::Projection(ref proj) =>
167 self.lvalue_ty(tcx, &proj.base).projection_ty(tcx, &proj.elem)
171 pub fn rvalue_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
172 rvalue: &Rvalue<'tcx>)
176 Rvalue::Use(ref operand) => Some(self.operand_ty(tcx, operand)),
177 Rvalue::Repeat(ref operand, ref count) => {
178 let op_ty = self.operand_ty(tcx, operand);
179 let count = count.value.as_u64(tcx.sess.target.uint_type);
180 assert_eq!(count as usize as u64, count);
181 Some(tcx.mk_array(op_ty, count as usize))
183 Rvalue::Ref(reg, bk, ref lv) => {
184 let lv_ty = self.lvalue_ty(tcx, lv).to_ty(tcx);
189 mutbl: bk.to_mutbl_lossy()
193 Rvalue::Len(..) => Some(tcx.types.usize),
194 Rvalue::Cast(_, _, ty) => Some(ty),
195 Rvalue::BinaryOp(op, ref lhs, ref rhs) => {
196 let lhs_ty = self.operand_ty(tcx, lhs);
197 let rhs_ty = self.operand_ty(tcx, rhs);
198 Some(self.binop_ty(tcx, op, lhs_ty, rhs_ty))
200 Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) => {
201 let lhs_ty = self.operand_ty(tcx, lhs);
202 let rhs_ty = self.operand_ty(tcx, rhs);
203 let ty = self.binop_ty(tcx, op, lhs_ty, rhs_ty);
204 let ty = tcx.mk_tup(vec![ty, tcx.types.bool]);
207 Rvalue::UnaryOp(_, ref operand) => {
208 Some(self.operand_ty(tcx, operand))
213 Rvalue::Aggregate(ref ak, ref ops) => {
215 AggregateKind::Vec => {
216 if let Some(operand) = ops.get(0) {
217 let ty = self.operand_ty(tcx, operand);
218 Some(tcx.mk_array(ty, ops.len()))
223 AggregateKind::Tuple => {
225 ops.iter().map(|op| self.operand_ty(tcx, op)).collect()
228 AggregateKind::Adt(def, _, substs) => {
229 Some(tcx.lookup_item_type(def.did).ty.subst(tcx, substs))
231 AggregateKind::Closure(did, substs) => {
232 Some(tcx.mk_closure_from_closure_substs(did, substs))
236 Rvalue::InlineAsm { .. } => None
242 pub fn to_mutbl_lossy(self) -> hir::Mutability {
244 BorrowKind::Mut => hir::MutMutable,
245 BorrowKind::Shared => hir::MutImmutable,
247 // We have no type corresponding to a unique imm borrow, so
248 // use `&mut`. It gives all the capabilities of an `&uniq`
249 // and hence is a safe "over approximation".
250 BorrowKind::Unique => hir::MutMutable,
256 pub fn to_hir_binop(self) -> hir::BinOp_ {
258 BinOp::Add => hir::BinOp_::BiAdd,
259 BinOp::Sub => hir::BinOp_::BiSub,
260 BinOp::Mul => hir::BinOp_::BiMul,
261 BinOp::Div => hir::BinOp_::BiDiv,
262 BinOp::Rem => hir::BinOp_::BiRem,
263 BinOp::BitXor => hir::BinOp_::BiBitXor,
264 BinOp::BitAnd => hir::BinOp_::BiBitAnd,
265 BinOp::BitOr => hir::BinOp_::BiBitOr,
266 BinOp::Shl => hir::BinOp_::BiShl,
267 BinOp::Shr => hir::BinOp_::BiShr,
268 BinOp::Eq => hir::BinOp_::BiEq,
269 BinOp::Ne => hir::BinOp_::BiNe,
270 BinOp::Lt => hir::BinOp_::BiLt,
271 BinOp::Gt => hir::BinOp_::BiGt,
272 BinOp::Le => hir::BinOp_::BiLe,
273 BinOp::Ge => hir::BinOp_::BiGe