From 89110fdf55c000096fc24a78d35256544c7b523f Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Fri, 21 Jun 2013 20:37:03 -0400 Subject: [PATCH] Use more deriving(IterBytes) in librustc. --- src/librustc/middle/trans/common.rs | 42 +--- src/librustc/middle/ty.rs | 229 ++---------------- .../middle/typeck/infer/region_inference.rs | 27 +-- 3 files changed, 23 insertions(+), 275 deletions(-) diff --git a/src/librustc/middle/trans/common.rs b/src/librustc/middle/trans/common.rs index 7624fb13903f..b255f2ca78c7 100644 --- a/src/librustc/middle/trans/common.rs +++ b/src/librustc/middle/trans/common.rs @@ -36,6 +36,8 @@ use core::cast; use core::hashmap::{HashMap}; use core::libc::{c_uint, c_longlong, c_ulonglong}; use core::to_bytes; +use core::str; +use core::vec::raw::to_ptr; use core::vec; use syntax::ast::ident; use syntax::ast_map::{path, path_elt}; @@ -860,7 +862,7 @@ pub fn is_null(val: ValueRef) -> bool { } // Used to identify cached monomorphized functions and vtables -#[deriving(Eq)] +#[deriving(Eq,IterBytes)] pub enum mono_param_id { mono_precise(ty::t, Option<@~[mono_id]>), mono_any, @@ -870,7 +872,7 @@ pub enum mono_param_id { datum::DatumMode), } -#[deriving(Eq)] +#[deriving(Eq,IterBytes)] pub enum MonoDataClass { MonoBits, // Anything not treated differently from arbitrary integer data MonoNonNull, // Non-null pointers (used for optional-pointer optimization) @@ -895,7 +897,7 @@ pub fn mono_data_classify(t: ty::t) -> MonoDataClass { } -#[deriving(Eq)] +#[deriving(Eq,IterBytes)] pub struct mono_id_ { def: ast::def_id, params: ~[mono_param_id], @@ -904,40 +906,6 @@ pub struct mono_id_ { pub type mono_id = @mono_id_; -impl to_bytes::IterBytes for mono_param_id { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - match *self { - mono_precise(t, ref mids) => { - 0u8.iter_bytes(lsb0, f) && - ty::type_id(t).iter_bytes(lsb0, f) && - mids.iter_bytes(lsb0, f) - } - - mono_any => 1u8.iter_bytes(lsb0, f), - - mono_repr(ref a, ref b, ref c, ref d) => { - 2u8.iter_bytes(lsb0, f) && - a.iter_bytes(lsb0, f) && - b.iter_bytes(lsb0, f) && - c.iter_bytes(lsb0, f) && - d.iter_bytes(lsb0, f) - } - } - } -} - -impl to_bytes::IterBytes for MonoDataClass { - fn iter_bytes(&self, lsb0: bool, f:to_bytes::Cb) -> bool { - (*self as u8).iter_bytes(lsb0, f) - } -} - -impl to_bytes::IterBytes for mono_id_ { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.def.iter_bytes(lsb0, f) && self.params.iter_bytes(lsb0, f) - } -} - pub fn umax(cx: block, a: ValueRef, b: ValueRef) -> ValueRef { let cond = build::ICmp(cx, lib::llvm::IntULT, a, b); return build::Select(cx, cond, b, a); diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index f1172fb1da65..a990e4c70a94 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -52,7 +52,7 @@ use syntax; // Data types -#[deriving(Eq)] +#[deriving(Eq, IterBytes)] pub struct field { ident: ast::ident, mt: mt @@ -96,13 +96,13 @@ impl Method { } } -#[deriving(Eq)] +#[deriving(Eq, IterBytes)] pub struct mt { ty: t, mutbl: ast::mutability, } -#[deriving(Eq, Encodable, Decodable)] +#[deriving(Eq, Encodable, Decodable, IterBytes)] pub enum vstore { vstore_fixed(uint), vstore_uniq, @@ -133,7 +133,7 @@ pub struct field_ty { // Contains information needed to resolve types and (in the future) look up // the types of AST nodes. -#[deriving(Eq)] +#[deriving(Eq,IterBytes)] pub struct creader_cache_key { cnum: int, pos: uint, @@ -142,14 +142,6 @@ pub struct creader_cache_key { type creader_cache = @mut HashMap; -impl to_bytes::IterBytes for creader_cache_key { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.cnum.iter_bytes(lsb0, f) && - self.pos.iter_bytes(lsb0, f) && - self.len.iter_bytes(lsb0, f) - } -} - struct intern_key { sty: *sty, } @@ -168,6 +160,8 @@ impl cmp::Eq for intern_key { } } +// NB: Do not replace this with #[deriving(IterBytes)], as above. (Figured +// this out the hard way.) impl to_bytes::IterBytes for intern_key { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { unsafe { @@ -372,14 +366,14 @@ pub fn type_has_regions(t: t) -> bool { } pub fn type_id(t: t) -> uint { get(t).id } -#[deriving(Eq)] +#[deriving(Eq,IterBytes)] pub struct BareFnTy { purity: ast::purity, abis: AbiSet, sig: FnSig } -#[deriving(Eq)] +#[deriving(Eq,IterBytes)] pub struct ClosureTy { purity: ast::purity, sigil: ast::Sigil, @@ -396,32 +390,13 @@ pub struct ClosureTy { * - `lifetimes` is the list of region names bound in this fn. * - `inputs` is the list of arguments and their modes. * - `output` is the return type. */ -#[deriving(Eq)] +#[deriving(Eq, IterBytes)] pub struct FnSig { bound_lifetime_names: OptVec, inputs: ~[t], output: t } -impl to_bytes::IterBytes for BareFnTy { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.purity.iter_bytes(lsb0, f) && - self.abis.iter_bytes(lsb0, f) && - self.sig.iter_bytes(lsb0, f) - } -} - -impl to_bytes::IterBytes for ClosureTy { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.purity.iter_bytes(lsb0, f) && - self.sigil.iter_bytes(lsb0, f) && - self.onceness.iter_bytes(lsb0, f) && - self.region.iter_bytes(lsb0, f) && - self.sig.iter_bytes(lsb0, f) && - self.bounds.iter_bytes(lsb0, f) - } -} - #[deriving(Eq, IterBytes)] pub struct param_ty { idx: uint, @@ -526,7 +501,7 @@ type opt_region = Option; * - `self_ty` is the type to which `self` should be remapped, if any. The * `self` type is rather funny in that it can only appear on traits and is * always substituted away to the implementing type for a trait. */ -#[deriving(Eq)] +#[deriving(Eq, IterBytes)] pub struct substs { self_r: opt_region, self_ty: Option, @@ -582,7 +557,7 @@ mod primitives { // NB: If you change this, you'll probably want to change the corresponding // AST structure in libsyntax/ast.rs as well. -#[deriving(Eq)] +#[deriving(Eq, IterBytes)] pub enum sty { ty_nil, ty_bot, @@ -714,62 +689,33 @@ impl CLike for BuiltinBound { } } -#[deriving(Eq)] +#[deriving(Eq, IterBytes)] pub struct TyVid(uint); -#[deriving(Eq)] +#[deriving(Eq, IterBytes)] pub struct IntVid(uint); -#[deriving(Eq)] +#[deriving(Eq, IterBytes)] pub struct FloatVid(uint); -#[deriving(Eq, Encodable, Decodable)] +#[deriving(Eq, Encodable, Decodable, IterBytes)] pub struct RegionVid { id: uint } -#[deriving(Eq)] +#[deriving(Eq, IterBytes)] pub enum InferTy { TyVar(TyVid), IntVar(IntVid), FloatVar(FloatVid) } -impl to_bytes::IterBytes for InferTy { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - match *self { - TyVar(ref tv) => { - 0u8.iter_bytes(lsb0, f) && tv.iter_bytes(lsb0, f) - } - IntVar(ref iv) => { - 1u8.iter_bytes(lsb0, f) && iv.iter_bytes(lsb0, f) - } - FloatVar(ref fv) => { - 2u8.iter_bytes(lsb0, f) && fv.iter_bytes(lsb0, f) - } - } - } -} - -#[deriving(Encodable, Decodable)] +#[deriving(Encodable, Decodable, IterBytes)] pub enum InferRegion { ReVar(RegionVid), ReSkolemized(uint, bound_region) } -impl to_bytes::IterBytes for InferRegion { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - match *self { - ReVar(ref rv) => { - 0u8.iter_bytes(lsb0, f) && rv.iter_bytes(lsb0, f) - } - ReSkolemized(ref v, _) => { - 1u8.iter_bytes(lsb0, f) && v.iter_bytes(lsb0, f) - } - } - } -} - impl cmp::Eq for InferRegion { fn eq(&self, other: &InferRegion) -> bool { match ((*self), *other) { @@ -849,30 +795,6 @@ impl ToStr for IntVarValue { } } -impl to_bytes::IterBytes for TyVid { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.to_uint().iter_bytes(lsb0, f) - } -} - -impl to_bytes::IterBytes for IntVid { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.to_uint().iter_bytes(lsb0, f) - } -} - -impl to_bytes::IterBytes for FloatVid { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.to_uint().iter_bytes(lsb0, f) - } -} - -impl to_bytes::IterBytes for RegionVid { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.to_uint().iter_bytes(lsb0, f) - } -} - pub struct TypeParameterDef { def_id: ast::def_id, bounds: @ParamBounds @@ -2744,123 +2666,6 @@ impl cmp::TotalEq for bound_region { } } -impl to_bytes::IterBytes for vstore { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - match *self { - vstore_fixed(ref u) => { - 0u8.iter_bytes(lsb0, f) && u.iter_bytes(lsb0, f) - } - vstore_uniq => 1u8.iter_bytes(lsb0, f), - vstore_box => 2u8.iter_bytes(lsb0, f), - - vstore_slice(ref r) => { - 3u8.iter_bytes(lsb0, f) && r.iter_bytes(lsb0, f) - } - } - } -} - -impl to_bytes::IterBytes for substs { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.self_r.iter_bytes(lsb0, f) && - self.self_ty.iter_bytes(lsb0, f) && - self.tps.iter_bytes(lsb0, f) - } -} - -impl to_bytes::IterBytes for mt { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.ty.iter_bytes(lsb0, f) && self.mutbl.iter_bytes(lsb0, f) - } -} - -impl to_bytes::IterBytes for field { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.ident.iter_bytes(lsb0, f) && self.mt.iter_bytes(lsb0, f) - } -} - -impl to_bytes::IterBytes for FnSig { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - self.inputs.iter_bytes(lsb0, f) && self.output.iter_bytes(lsb0, f) - } -} - -impl to_bytes::IterBytes for sty { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - match *self { - ty_nil => 0u8.iter_bytes(lsb0, f), - ty_bool => 1u8.iter_bytes(lsb0, f), - - ty_int(ref t) => 2u8.iter_bytes(lsb0, f) && t.iter_bytes(lsb0, f), - - ty_uint(ref t) => 3u8.iter_bytes(lsb0, f) && t.iter_bytes(lsb0, f), - - ty_float(ref t) => 4u8.iter_bytes(lsb0, f) && t.iter_bytes(lsb0, f), - - ty_estr(ref v) => 5u8.iter_bytes(lsb0, f) && v.iter_bytes(lsb0, f), - - ty_enum(ref did, ref substs) => { - 6u8.iter_bytes(lsb0, f) && - did.iter_bytes(lsb0, f) && - substs.iter_bytes(lsb0, f) - } - - ty_box(ref mt) => 7u8.iter_bytes(lsb0, f) && mt.iter_bytes(lsb0, f), - - ty_evec(ref mt, ref v) => { - 8u8.iter_bytes(lsb0, f) && - mt.iter_bytes(lsb0, f) && - v.iter_bytes(lsb0, f) - } - - ty_unboxed_vec(ref mt) => 9u8.iter_bytes(lsb0, f) && mt.iter_bytes(lsb0, f), - - ty_tup(ref ts) => 10u8.iter_bytes(lsb0, f) && ts.iter_bytes(lsb0, f), - - ty_bare_fn(ref ft) => 12u8.iter_bytes(lsb0, f) && ft.iter_bytes(lsb0, f), - - ty_self(ref did) => 13u8.iter_bytes(lsb0, f) && did.iter_bytes(lsb0, f), - - ty_infer(ref v) => 14u8.iter_bytes(lsb0, f) && v.iter_bytes(lsb0, f), - - ty_param(ref p) => 15u8.iter_bytes(lsb0, f) && p.iter_bytes(lsb0, f), - - ty_type => 16u8.iter_bytes(lsb0, f), - ty_bot => 17u8.iter_bytes(lsb0, f), - - ty_ptr(ref mt) => 18u8.iter_bytes(lsb0, f) && mt.iter_bytes(lsb0, f), - - ty_uniq(ref mt) => 19u8.iter_bytes(lsb0, f) && mt.iter_bytes(lsb0, f), - - ty_trait(ref did, ref substs, ref v, ref mutbl, bounds) => { - 20u8.iter_bytes(lsb0, f) && - did.iter_bytes(lsb0, f) && - substs.iter_bytes(lsb0, f) && - v.iter_bytes(lsb0, f) && - mutbl.iter_bytes(lsb0, f) && - bounds.iter_bytes(lsb0, f) - } - - ty_opaque_closure_ptr(ref ck) => 21u8.iter_bytes(lsb0, f) && ck.iter_bytes(lsb0, f), - - ty_opaque_box => 22u8.iter_bytes(lsb0, f), - - ty_struct(ref did, ref substs) => { - 23u8.iter_bytes(lsb0, f) && did.iter_bytes(lsb0, f) && substs.iter_bytes(lsb0, f) - } - - ty_rptr(ref r, ref mt) => { - 24u8.iter_bytes(lsb0, f) && r.iter_bytes(lsb0, f) && mt.iter_bytes(lsb0, f) - } - - ty_err => 25u8.iter_bytes(lsb0, f), - - ty_closure(ref ct) => 26u8.iter_bytes(lsb0, f) && ct.iter_bytes(lsb0, f), - } - } -} - pub fn node_id_to_trait_ref(cx: ctxt, id: ast::node_id) -> @ty::TraitRef { match cx.trait_refs.find(&id) { Some(&t) => t, diff --git a/src/librustc/middle/typeck/infer/region_inference.rs b/src/librustc/middle/typeck/infer/region_inference.rs index 0aad161a6788..4380711b78e8 100644 --- a/src/librustc/middle/typeck/infer/region_inference.rs +++ b/src/librustc/middle/typeck/infer/region_inference.rs @@ -548,43 +548,18 @@ use util::ppaux::note_and_explain_region; use core::cell::Cell; use core::hashmap::{HashMap, HashSet}; -use core::to_bytes; use core::uint; use core::vec; use syntax::codemap::span; use syntax::ast; -#[deriving(Eq)] +#[deriving(Eq,IterBytes)] enum Constraint { ConstrainVarSubVar(RegionVid, RegionVid), ConstrainRegSubVar(Region, RegionVid), ConstrainVarSubReg(RegionVid, Region) } -impl to_bytes::IterBytes for Constraint { - fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { - match *self { - ConstrainVarSubVar(ref v0, ref v1) => { - 0u8.iter_bytes(lsb0, f) && - v0.iter_bytes(lsb0, f) && - v1.iter_bytes(lsb0, f) - } - - ConstrainRegSubVar(ref ra, ref va) => { - 1u8.iter_bytes(lsb0, f) && - ra.iter_bytes(lsb0, f) && - va.iter_bytes(lsb0, f) - } - - ConstrainVarSubReg(ref va, ref ra) => { - 2u8.iter_bytes(lsb0, f) && - va.iter_bytes(lsb0, f) && - ra.iter_bytes(lsb0, f) - } - } - } -} - #[deriving(Eq, IterBytes)] struct TwoRegions { a: Region,