about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSteven Stewart-Gallus <sstewartgallus00@mylangara.bc.ca>2013-06-22 18:58:41 -0700
committerSteven Stewart-Gallus <sstewartgallus00@mylangara.bc.ca>2013-06-22 18:58:41 -0700
commit0f55c9cc7ec4613bc3f85c25799b6020d57e7605 (patch)
tree729142867039898673dec51425c431505c991a5b
parentdc4560dc26498573516d833cb2333b54071070e3 (diff)
downloadrust-0f55c9cc7ec4613bc3f85c25799b6020d57e7605.tar.gz
rust-0f55c9cc7ec4613bc3f85c25799b6020d57e7605.zip
Cleaned up middle a bit.
The only really tricky change is that a long chain of ifs, and elses
was turned into a single if, and a match in astencode.rs. Some methods
can only be called in certain cases, and so have to come after the if.
-rw-r--r--src/librustc/metadata/common.rs16
-rw-r--r--src/librustc/middle/astencode.rs175
-rw-r--r--src/librustc/middle/check_const.rs29
-rw-r--r--src/librustc/middle/const_eval.rs71
4 files changed, 128 insertions, 163 deletions
diff --git a/src/librustc/metadata/common.rs b/src/librustc/metadata/common.rs
index 9426cd6041d..f487c73372f 100644
--- a/src/librustc/metadata/common.rs
+++ b/src/librustc/metadata/common.rs
@@ -1,4 +1,4 @@
-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -7,7 +7,8 @@
 // <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.
-
+use core::prelude::*;
+use core::cast;
 
 // EBML enum definitions and utils shared by the encoder and decoder
 
@@ -111,6 +112,7 @@ pub static tag_items_data_item_reexport_def_id: uint = 0x4e;
 pub static tag_items_data_item_reexport_name: uint = 0x4f;
 
 // used to encode crate_ctxt side tables
+#[deriving(Eq)]
 pub enum astencode_tag { // Reserves 0x50 -- 0x6f
     tag_ast = 0x50,
 
@@ -136,6 +138,16 @@ pub enum astencode_tag { // Reserves 0x50 -- 0x6f
     tag_table_moves_map = 0x63,
     tag_table_capture_map = 0x64
 }
+static first_astencode_tag : uint = tag_ast as uint;
+static last_astencode_tag : uint = tag_table_capture_map as uint;
+impl astencode_tag {
+    pub fn from_uint(value : uint) -> Option<astencode_tag> {
+        let is_a_tag = first_astencode_tag <= value && value <= last_astencode_tag;
+        if !is_a_tag { None } else {
+            Some(unsafe { cast::transmute(value as int) })
+        }
+    }
+}
 
 pub static tag_item_trait_method_sort: uint = 0x70;
 
diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs
index 925b1f506d7..d405dc0bc20 100644
--- a/src/librustc/middle/astencode.rs
+++ b/src/librustc/middle/astencode.rs
@@ -1,4 +1,4 @@
-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -319,15 +319,10 @@ fn simplify_ast(ii: &ast::inlined_item) -> ast::inlined_item {
     });
 
     match *ii {
-      ast::ii_item(i) => {
-        ast::ii_item(fld.fold_item(i).get()) //hack: we're not dropping items
-      }
-      ast::ii_method(d, m) => {
-        ast::ii_method(d, fld.fold_method(m))
-      }
-      ast::ii_foreign(i) => {
-        ast::ii_foreign(fld.fold_foreign_item(i))
-      }
+        //hack: we're not dropping items
+        ast::ii_item(i) => ast::ii_item(fld.fold_item(i).get()),
+        ast::ii_method(d, m) => ast::ii_method(d, fld.fold_method(m)),
+        ast::ii_foreign(i) => ast::ii_foreign(fld.fold_foreign_item(i))
     }
 }
 
@@ -346,16 +341,10 @@ fn renumber_ast(xcx: @ExtendedDecodeContext, ii: ast::inlined_item)
     });
 
     match ii {
-      ast::ii_item(i) => {
-        ast::ii_item(fld.fold_item(i).get())
-      }
-      ast::ii_method(d, m) => {
-        ast::ii_method(xcx.tr_def_id(d), fld.fold_method(m))
-      }
-      ast::ii_foreign(i) => {
-        ast::ii_foreign(fld.fold_foreign_item(i))
-      }
-     }
+        ast::ii_item(i) => ast::ii_item(fld.fold_item(i).get()),
+        ast::ii_method(d, m) => ast::ii_method(xcx.tr_def_id(d), fld.fold_method(m)),
+        ast::ii_foreign(i) => ast::ii_foreign(fld.fold_foreign_item(i)),
+    }
 }
 
 // ______________________________________________________________________
@@ -374,22 +363,22 @@ fn decode_def(xcx: @ExtendedDecodeContext, doc: ebml::Doc) -> ast::def {
 impl tr for ast::def {
     fn tr(&self, xcx: @ExtendedDecodeContext) -> ast::def {
         match *self {
-          ast::def_fn(did, p) => { ast::def_fn(did.tr(xcx), p) }
+          ast::def_fn(did, p) => ast::def_fn(did.tr(xcx), p),
           ast::def_static_method(did, did2_opt, p) => {
             ast::def_static_method(did.tr(xcx),
                                    did2_opt.map(|did2| did2.tr(xcx)),
                                    p)
-          }
-          ast::def_self_ty(nid) => { ast::def_self_ty(xcx.tr_id(nid)) }
-          ast::def_self(nid, i) => { ast::def_self(xcx.tr_id(nid), i) }
-          ast::def_mod(did) => { ast::def_mod(did.tr(xcx)) }
-          ast::def_foreign_mod(did) => { ast::def_foreign_mod(did.tr(xcx)) }
-          ast::def_const(did) => { ast::def_const(did.tr(xcx)) }
-          ast::def_arg(nid, b) => { ast::def_arg(xcx.tr_id(nid), b) }
-          ast::def_local(nid, b) => { ast::def_local(xcx.tr_id(nid), b) }
+          },
+          ast::def_self_ty(nid) => ast::def_self_ty(xcx.tr_id(nid)),
+          ast::def_self(nid, i) => ast::def_self(xcx.tr_id(nid), i),
+          ast::def_mod(did) => ast::def_mod(did.tr(xcx)),
+          ast::def_foreign_mod(did) => ast::def_foreign_mod(did.tr(xcx)),
+          ast::def_const(did) => ast::def_const(did.tr(xcx)),
+          ast::def_arg(nid, b) => ast::def_arg(xcx.tr_id(nid), b),
+          ast::def_local(nid, b) => ast::def_local(xcx.tr_id(nid), b),
           ast::def_variant(e_did, v_did) => {
             ast::def_variant(e_did.tr(xcx), v_did.tr(xcx))
-          }
+          },
           ast::def_trait(did) => ast::def_trait(did.tr(xcx)),
           ast::def_ty(did) => ast::def_ty(did.tr(xcx)),
           ast::def_prim_ty(p) => ast::def_prim_ty(p),
@@ -402,9 +391,7 @@ impl tr for ast::def {
                            xcx.tr_id(nid2),
                            xcx.tr_id(nid3))
           }
-          ast::def_struct(did) => {
-            ast::def_struct(did.tr(xcx))
-          }
+          ast::def_struct(did) => ast::def_struct(did.tr(xcx)),
           ast::def_region(nid) => ast::def_region(xcx.tr_id(nid)),
           ast::def_typaram_binder(nid) => {
             ast::def_typaram_binder(xcx.tr_id(nid))
@@ -419,12 +406,9 @@ impl tr for ast::def {
 
 impl tr for ty::AutoAdjustment {
     fn tr(&self, xcx: @ExtendedDecodeContext) -> ty::AutoAdjustment {
-        match self {
-            &ty::AutoAddEnv(r, s) => {
-                ty::AutoAddEnv(r.tr(xcx), s)
-            }
-
-            &ty::AutoDerefRef(ref adr) => {
+        match *self {
+            ty::AutoAddEnv(r, s) => ty::AutoAddEnv(r.tr(xcx), s),
+            ty::AutoDerefRef(ref adr) => {
                 ty::AutoDerefRef(ty::AutoDerefRef {
                     autoderefs: adr.autoderefs,
                     autoref: adr.autoref.map(|ar| ar.tr(xcx)),
@@ -1110,56 +1094,75 @@ fn decode_side_tables(xcx: @ExtendedDecodeContext,
                 found for id %d (orig %d)",
                tag, id, id0);
 
-        if tag == (c::tag_table_moves_map as uint) {
-            dcx.maps.moves_map.insert(id);
-        } else {
-            let val_doc = entry_doc.get(c::tag_table_val as uint);
-            let mut val_dsr = reader::Decoder(val_doc);
-            let val_dsr = &mut val_dsr;
-            if tag == (c::tag_table_def as uint) {
-                let def = decode_def(xcx, val_doc);
-                dcx.tcx.def_map.insert(id, def);
-            } else if tag == (c::tag_table_node_type as uint) {
-                let ty = val_dsr.read_ty(xcx);
-                debug!("inserting ty for node %?: %s",
-                       id, ty_to_str(dcx.tcx, ty));
-                dcx.tcx.node_types.insert(id as uint, ty);
-            } else if tag == (c::tag_table_node_type_subst as uint) {
-                let tys = val_dsr.read_tys(xcx);
-                dcx.tcx.node_type_substs.insert(id, tys);
-            } else if tag == (c::tag_table_freevars as uint) {
-                let fv_info = @val_dsr.read_to_vec(|val_dsr| {
-                    @val_dsr.read_freevar_entry(xcx)
-                });
-                dcx.tcx.freevars.insert(id, fv_info);
-            } else if tag == (c::tag_table_tcache as uint) {
-                let tpbt = val_dsr.read_ty_param_bounds_and_ty(xcx);
-                let lid = ast::def_id { crate: ast::local_crate, node: id };
-                dcx.tcx.tcache.insert(lid, tpbt);
-            } else if tag == (c::tag_table_param_defs as uint) {
-                let bounds = val_dsr.read_type_param_def(xcx);
-                dcx.tcx.ty_param_defs.insert(id, bounds);
-            } else if tag == (c::tag_table_method_map as uint) {
-                dcx.maps.method_map.insert(
-                    id,
-                    val_dsr.read_method_map_entry(xcx));
-            } else if tag == (c::tag_table_vtable_map as uint) {
-                dcx.maps.vtable_map.insert(id,
-                                           val_dsr.read_vtable_res(xcx));
-            } else if tag == (c::tag_table_adjustments as uint) {
-                let adj: @ty::AutoAdjustment = @Decodable::decode(val_dsr);
-                adj.tr(xcx);
-                dcx.tcx.adjustments.insert(id, adj);
-            } else if tag == (c::tag_table_capture_map as uint) {
-                let cvars =
-                    at_vec::to_managed_consume(
-                        val_dsr.read_to_vec(
-                            |val_dsr| val_dsr.read_capture_var(xcx)));
-                dcx.maps.capture_map.insert(id, cvars);
-            } else {
+        match c::astencode_tag::from_uint(tag) {
+            None => {
                 xcx.dcx.tcx.sess.bug(
                     fmt!("unknown tag found in side tables: %x", tag));
             }
+            Some(value) => if value == c::tag_table_moves_map {
+                dcx.maps.moves_map.insert(id);
+            } else {
+                let val_doc = entry_doc.get(c::tag_table_val as uint);
+                let mut val_dsr = reader::Decoder(val_doc);
+                let val_dsr = &mut val_dsr;
+
+                match value {
+                    c::tag_table_def => {
+                        let def = decode_def(xcx, val_doc);
+                        dcx.tcx.def_map.insert(id, def);
+                    }
+                    c::tag_table_node_type => {
+                        let ty = val_dsr.read_ty(xcx);
+                        debug!("inserting ty for node %?: %s",
+                               id, ty_to_str(dcx.tcx, ty));
+                        dcx.tcx.node_types.insert(id as uint, ty);
+                    }
+                    c::tag_table_node_type_subst => {
+                        let tys = val_dsr.read_tys(xcx);
+                        dcx.tcx.node_type_substs.insert(id, tys);
+                    }
+                    c::tag_table_freevars => {
+                        let fv_info = @val_dsr.read_to_vec(|val_dsr| {
+                            @val_dsr.read_freevar_entry(xcx)
+                        });
+                        dcx.tcx.freevars.insert(id, fv_info);
+                    }
+                    c::tag_table_tcache => {
+                        let tpbt = val_dsr.read_ty_param_bounds_and_ty(xcx);
+                        let lid = ast::def_id { crate: ast::local_crate, node: id };
+                        dcx.tcx.tcache.insert(lid, tpbt);
+                    }
+                    c::tag_table_param_defs => {
+                        let bounds = val_dsr.read_type_param_def(xcx);
+                        dcx.tcx.ty_param_defs.insert(id, bounds);
+                    }
+                    c::tag_table_method_map => {
+                        dcx.maps.method_map.insert(
+                            id,
+                            val_dsr.read_method_map_entry(xcx));
+                    }
+                    c::tag_table_vtable_map => {
+                        dcx.maps.vtable_map.insert(id,
+                                                   val_dsr.read_vtable_res(xcx));
+                    }
+                    c::tag_table_adjustments => {
+                        let adj: @ty::AutoAdjustment = @Decodable::decode(val_dsr);
+                        adj.tr(xcx);
+                        dcx.tcx.adjustments.insert(id, adj);
+                    }
+                    c::tag_table_capture_map => {
+                        let cvars =
+                            at_vec::to_managed_consume(
+                                val_dsr.read_to_vec(
+                                    |val_dsr| val_dsr.read_capture_var(xcx)));
+                        dcx.maps.capture_map.insert(id, cvars);
+                    }
+                    _ => {
+                        xcx.dcx.tcx.sess.bug(
+                            fmt!("unknown tag found in side tables: %x", tag));
+                    }
+                }
+            }
         }
 
         debug!(">< Side table doc loaded");
diff --git a/src/librustc/middle/check_const.rs b/src/librustc/middle/check_const.rs
index 79fe6420e27..3722a5ba5a9 100644
--- a/src/librustc/middle/check_const.rs
+++ b/src/librustc/middle/check_const.rs
@@ -1,4 +1,4 @@
-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -235,22 +235,17 @@ pub fn check_item_recursion(sess: Session,
 
     fn visit_expr(e: @expr, (env, v): (env, visit::vt<env>)) {
         match e.node {
-          expr_path(*) => {
-            match env.def_map.find(&e.id) {
-              Some(&def_const(def_id)) => {
-                if ast_util::is_local(def_id) {
-                  match env.ast_map.get_copy(&def_id.node) {
-                    ast_map::node_item(it, _) => {
-                      (v.visit_item)(it, (env, v));
-                    }
-                    _ => fail!("const not bound to an item")
-                  }
-                }
-              }
-              _ => ()
-            }
-          }
-          _ => ()
+            expr_path(*) => match env.def_map.find(&e.id) {
+                Some(&def_const(def_id)) if ast_util::is_local(def_id) =>
+                    match env.ast_map.get_copy(&def_id.node) {
+                        ast_map::node_item(it, _) => {
+                            (v.visit_item)(it, (env, v));
+                        }
+                        _ => fail!("const not bound to an item")
+                    },
+                _ => ()
+            },
+            _ => ()
         }
         visit::visit_expr(e, (env, v));
     }
diff --git a/src/librustc/middle/const_eval.rs b/src/librustc/middle/const_eval.rs
index 988ad519f42..78bc59c5647 100644
--- a/src/librustc/middle/const_eval.rs
+++ b/src/librustc/middle/const_eval.rs
@@ -1,4 +1,4 @@
-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -420,57 +420,18 @@ pub fn lit_to_const(lit: @lit) -> const_val {
     }
 }
 
+fn compare_vals<T : Eq + Ord>(a: T, b: T) -> Option<int> {
+    Some(if a == b { 0 } else if a < b { -1 } else { 1 })
+}
 pub fn compare_const_vals(a: &const_val, b: &const_val) -> Option<int> {
-  match (a, b) {
-    (&const_int(a), &const_int(b)) => {
-        if a == b {
-            Some(0)
-        } else if a < b {
-            Some(-1)
-        } else {
-            Some(1)
-        }
-    }
-    (&const_uint(a), &const_uint(b)) => {
-        if a == b {
-            Some(0)
-        } else if a < b {
-            Some(-1)
-        } else {
-            Some(1)
-        }
-    }
-    (&const_float(a), &const_float(b)) => {
-        if a == b {
-            Some(0)
-        } else if a < b {
-            Some(-1)
-        } else {
-            Some(1)
-        }
-    }
-    (&const_str(ref a), &const_str(ref b)) => {
-        if (*a) == (*b) {
-            Some(0)
-        } else if (*a) < (*b) {
-            Some(-1)
-        } else {
-            Some(1)
-        }
-    }
-    (&const_bool(a), &const_bool(b)) => {
-        if a == b {
-            Some(0)
-        } else if a < b {
-            Some(-1)
-        } else {
-            Some(1)
-        }
-    }
-    _ => {
-        None
+    match (a, b) {
+        (&const_int(a), &const_int(b)) => compare_vals(a, b),
+        (&const_uint(a), &const_uint(b)) => compare_vals(a, b),
+        (&const_float(a), &const_float(b)) => compare_vals(a, b),
+        (&const_str(a), &const_str(b)) => compare_vals(a, b),
+        (&const_bool(a), &const_bool(b)) => compare_vals(a, b),
+        _ => None
     }
-  }
 }
 
 pub fn compare_lit_exprs(tcx: middle::ty::ctxt, a: @expr, b: @expr) -> Option<int> {
@@ -478,15 +439,9 @@ pub fn compare_lit_exprs(tcx: middle::ty::ctxt, a: @expr, b: @expr) -> Option<in
 }
 
 pub fn lit_expr_eq(tcx: middle::ty::ctxt, a: @expr, b: @expr) -> Option<bool> {
-    match compare_lit_exprs(tcx, a, b) {
-        Some(val) => Some(val == 0),
-        None =>  None,
-    }
+    compare_lit_exprs(tcx, a, b).map(|&val| val == 0)
 }
 
 pub fn lit_eq(a: @lit, b: @lit) -> Option<bool> {
-    match compare_const_vals(&lit_to_const(a), &lit_to_const(b)) {
-        Some(val) => Some(val == 0),
-        None =>  None,
-    }
+    compare_const_vals(&lit_to_const(a), &lit_to_const(b)).map(|&val| val == 0)
 }