]> git.lizzy.rs Git - rust.git/commitdiff
Simplify primitive type reprs.
authorScott Olson <scott@solson.me>
Thu, 17 Mar 2016 09:36:06 +0000 (03:36 -0600)
committerScott Olson <scott@solson.me>
Thu, 17 Mar 2016 09:36:06 +0000 (03:36 -0600)
src/interpreter.rs
src/memory.rs

index 9c5e505671afcfc1447dba6baeb75c17d3ce96bd..ca0a96a1767e3fbe20180cf1a284cd376c2ac949 100644 (file)
@@ -571,19 +571,18 @@ fn make_variant_repr(&self, v: ty::VariantDef<'tcx>, substs: &'tcx Substs<'tcx>)
     fn ty_to_repr(&self, ty: ty::Ty<'tcx>) -> Repr {
         use syntax::ast::{IntTy, UintTy};
         match ty.subst(self.tcx, self.current_substs()).sty {
-            ty::TyBool => Repr::Bool,
-
+            ty::TyBool => Repr::Primitive { size: 1 },
             ty::TyInt(IntTy::Is)  => Repr::isize(),
-            ty::TyInt(IntTy::I8)  => Repr::I8,
-            ty::TyInt(IntTy::I16) => Repr::I16,
-            ty::TyInt(IntTy::I32) => Repr::I32,
-            ty::TyInt(IntTy::I64) => Repr::I64,
+            ty::TyInt(IntTy::I8)  => Repr::Primitive { size: 1 },
+            ty::TyInt(IntTy::I16) => Repr::Primitive { size: 2 },
+            ty::TyInt(IntTy::I32) => Repr::Primitive { size: 4 },
+            ty::TyInt(IntTy::I64) => Repr::Primitive { size: 8 },
 
             ty::TyUint(UintTy::Us)  => Repr::usize(),
-            ty::TyUint(UintTy::U8)  => Repr::U8,
-            ty::TyUint(UintTy::U16) => Repr::U16,
-            ty::TyUint(UintTy::U32) => Repr::U32,
-            ty::TyUint(UintTy::U64) => Repr::U64,
+            ty::TyUint(UintTy::U8)  => Repr::Primitive { size: 1 },
+            ty::TyUint(UintTy::U16) => Repr::Primitive { size: 2 },
+            ty::TyUint(UintTy::U32) => Repr::Primitive { size: 4 },
+            ty::TyUint(UintTy::U64) => Repr::Primitive { size: 8 },
 
             ty::TyTuple(ref fields) => self.make_product_repr(fields.iter().cloned()),
 
index deea5e29deca718e7a1ef94328e7b3ff65c2e222..612f8e4938e61ddcdb46170536f8bad481ba967d 100644 (file)
@@ -40,9 +40,10 @@ pub struct FieldRepr {
 
 #[derive(Clone, Debug, PartialEq, Eq)]
 pub enum Repr {
-    Bool,
-    I8, I16, I32, I64,
-    U8, U16, U32, U64,
+    /// Representation for a primitive type such as a boolean, integer, or character.
+    Primitive {
+        size: usize
+    },
 
     Pointer,
     FatPointer,
@@ -361,29 +362,17 @@ pub fn offset(self, i: isize) -> Self {
 impl Repr {
     // TODO(tsion): Choice is based on host machine's type size. Should this be how miri works?
     pub fn isize() -> Self {
-        match mem::size_of::<isize>() {
-            4 => Repr::I32,
-            8 => Repr::I64,
-            _ => unimplemented!(),
-        }
+        Repr::Primitive { size: mem::size_of::<isize>() }
     }
 
     // TODO(tsion): Choice is based on host machine's type size. Should this be how miri works?
     pub fn usize() -> Self {
-        match mem::size_of::<isize>() {
-            4 => Repr::U32,
-            8 => Repr::U64,
-            _ => unimplemented!(),
-        }
+        Repr::Primitive { size: mem::size_of::<usize>() }
     }
 
     pub fn size(&self) -> usize {
         match *self {
-            Repr::Bool => 1,
-            Repr::I8  | Repr::U8  => 1,
-            Repr::I16 | Repr::U16 => 2,
-            Repr::I32 | Repr::U32 => 4,
-            Repr::I64 | Repr::U64 => 8,
+            Repr::Primitive { size } => size,
             Repr::Product { size, .. } => size,
             Repr::Sum { discr_size, max_variant_size, .. } => discr_size + max_variant_size,
             Repr::Array { elem_size, length } => elem_size * length,