about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2015-10-21 17:30:17 -0400
committerNiko Matsakis <niko@alum.mit.edu>2015-11-03 04:35:00 -0500
commit0a62158a4e1bb012d5b0778701dd67b65a8754c2 (patch)
tree8aedae1f5d35add871746c21055c10ad87dea6ec /src
parent044096b3e9a8d02461d49fb5559bb11c4308e701 (diff)
downloadrust-0a62158a4e1bb012d5b0778701dd67b65a8754c2.tar.gz
rust-0a62158a4e1bb012d5b0778701dd67b65a8754c2.zip
Add helper methods that require tcx; these compute types of
lvalues and operands
Diffstat (limited to 'src')
-rw-r--r--src/librustc_mir/lib.rs2
-rw-r--r--src/librustc_mir/tcx/mod.rs125
2 files changed, 126 insertions, 1 deletions
diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs
index 215f708cadd..5c52dfe2bd6 100644
--- a/src/librustc_mir/lib.rs
+++ b/src/librustc_mir/lib.rs
@@ -34,5 +34,5 @@ pub mod mir_map;
 mod hair;
 pub mod repr;
 mod graphviz;
-
+pub mod tcx;
 
diff --git a/src/librustc_mir/tcx/mod.rs b/src/librustc_mir/tcx/mod.rs
new file mode 100644
index 00000000000..3b9d9228a16
--- /dev/null
+++ b/src/librustc_mir/tcx/mod.rs
@@ -0,0 +1,125 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+/*!
+ * Methods for the various MIR types. These are intended for use after
+ * building is complete.
+ */
+
+use repr::*;
+use rustc::middle::subst::Substs;
+use rustc::middle::ty::{self, AdtDef, Ty};
+
+#[derive(Copy, Clone, Debug)]
+pub enum LvalueTy<'tcx> {
+    /// Normal type.
+    Ty { ty: Ty<'tcx> },
+
+    /// Downcast to a particular variant of an enum.
+    Downcast { adt_def: AdtDef<'tcx>,
+               substs: &'tcx Substs<'tcx>,
+               variant_index: usize },
+}
+
+impl<'tcx> LvalueTy<'tcx> {
+    pub fn from_ty(ty: Ty<'tcx>) -> LvalueTy<'tcx> {
+        LvalueTy::Ty { ty: ty }
+    }
+
+    pub fn to_ty(&self, tcx: &ty::ctxt<'tcx>) -> Ty<'tcx> {
+        match *self {
+            LvalueTy::Ty { ty } =>
+                ty,
+            LvalueTy::Downcast { adt_def, substs, variant_index: _ } =>
+                tcx.mk_enum(adt_def, substs),
+        }
+    }
+
+    pub fn projection_ty(self,
+                         tcx: &ty::ctxt<'tcx>,
+                         elem: &LvalueElem<'tcx>)
+                         -> LvalueTy<'tcx>
+    {
+        match *elem {
+            ProjectionElem::Deref =>
+                LvalueTy::Ty {
+                    ty: self.to_ty(tcx).builtin_deref(true, ty::LvaluePreference::NoPreference)
+                                          .unwrap()
+                                          .ty
+                },
+            ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } =>
+                LvalueTy::Ty {
+                    ty: self.to_ty(tcx).builtin_index().unwrap()
+                },
+            ProjectionElem::Downcast(adt_def1, index) =>
+                match self.to_ty(tcx).sty {
+                    ty::TyEnum(adt_def, substs) => {
+                        assert!(index < adt_def.variants.len());
+                        assert_eq!(adt_def, adt_def1);
+                        LvalueTy::Downcast { adt_def: adt_def,
+                                             substs: substs,
+                                             variant_index: index }
+                    }
+                    _ => {
+                        tcx.sess.bug(&format!("cannot downcast non-enum type: `{:?}`", self))
+                    }
+                },
+            ProjectionElem::Field(field) => {
+                let field_ty = match self {
+                    LvalueTy::Ty { ty } => match ty.sty {
+                        ty::TyStruct(adt_def, substs) =>
+                            adt_def.struct_variant().fields[field.index()].ty(tcx, substs),
+                        ty::TyTuple(ref tys) =>
+                            tys[field.index()],
+                        _ =>
+                            tcx.sess.bug(&format!("cannot get field of type: `{:?}`", ty)),
+                    },
+                    LvalueTy::Downcast { adt_def, substs, variant_index } =>
+                        adt_def.variants[variant_index].fields[field.index()].ty(tcx, substs),
+                };
+                LvalueTy::Ty { ty: field_ty }
+            }
+        }
+    }
+}
+
+impl<'tcx> Mir<'tcx> {
+    pub fn operand_ty(&self,
+                      tcx: &ty::ctxt<'tcx>,
+                      operand: &Operand<'tcx>)
+                      -> Ty<'tcx>
+    {
+        match *operand {
+            Operand::Consume(ref l) => self.lvalue_ty(tcx, l).to_ty(tcx),
+            Operand::Constant(ref c) => c.ty,
+        }
+    }
+
+    pub fn lvalue_ty(&self,
+                     tcx: &ty::ctxt<'tcx>,
+                     lvalue: &Lvalue<'tcx>)
+                     -> LvalueTy<'tcx>
+    {
+        match *lvalue {
+            Lvalue::Var(index) =>
+                LvalueTy::Ty { ty: self.var_decls[index as usize].ty },
+            Lvalue::Temp(index) =>
+                LvalueTy::Ty { ty: self.temp_decls[index as usize].ty },
+            Lvalue::Arg(index) =>
+                LvalueTy::Ty { ty: self.arg_decls[index as usize].ty },
+            Lvalue::Static(def_id) =>
+                LvalueTy::Ty { ty: tcx.lookup_item_type(def_id).ty },
+            Lvalue::ReturnPointer =>
+                LvalueTy::Ty { ty: self.return_ty.unwrap() },
+            Lvalue::Projection(ref proj) =>
+                self.lvalue_ty(tcx, &proj.base).projection_ty(tcx, &proj.elem)
+        }
+    }
+}