From 5eb6d19803ebcb5279f8a42b584a4d81152fa82d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 2 May 2013 18:33:33 -0400 Subject: [PATCH] syntax: Use the new `for` protocol --- src/libsyntax/abi.rs | 29 +++++++++++ src/libsyntax/ast.rs | 83 ++++++++++++++++++++++++++++++++ src/libsyntax/ast_util.rs | 9 ++++ src/libsyntax/codemap.rs | 14 ++++++ src/libsyntax/ext/expand.rs | 14 +----- src/libsyntax/ext/pipes/proto.rs | 16 ++++++ src/libsyntax/opt_vec.rs | 14 ++++++ src/libsyntax/parse/obsolete.rs | 8 +++ src/libsyntax/parse/token.rs | 7 +++ 9 files changed, 182 insertions(+), 12 deletions(-) diff --git a/src/libsyntax/abi.rs b/src/libsyntax/abi.rs index 75782e9ca67..f266b8871a2 100644 --- a/src/libsyntax/abi.rs +++ b/src/libsyntax/abi.rs @@ -81,6 +81,7 @@ pub struct AbiSet { AbiData {abi: RustIntrinsic, name: "rust-intrinsic", abi_arch: RustArch}, ]; +#[cfg(stage0)] fn each_abi(op: &fn(abi: Abi) -> bool) { /*! * @@ -93,6 +94,15 @@ fn each_abi(op: &fn(abi: Abi) -> bool) { } } } +#[cfg(not(stage0))] +fn each_abi(op: &fn(abi: Abi) -> bool) -> bool { + /*! + * + * Iterates through each of the defined ABIs. + */ + + AbiDatas.each(|abi_data| op(abi_data.abi)) +} pub fn lookup(name: &str) -> Option { /*! @@ -189,6 +199,7 @@ fn add(&mut self, abi: Abi) { self.bits |= (1 << abi.index()); } + #[cfg(stage0)] fn each(&self, op: &fn(abi: Abi) -> bool) { for each_abi |abi| { if self.contains(abi) { @@ -198,6 +209,10 @@ fn each(&self, op: &fn(abi: Abi) -> bool) { } } } + #[cfg(not(stage0))] + fn each(&self, op: &fn(abi: Abi) -> bool) -> bool { + each_abi(|abi| !self.contains(abi) || op(abi)) + } fn is_empty(&self) -> bool { self.bits == 0 @@ -252,17 +267,31 @@ fn check_valid(&self) -> Option<(Abi, Abi)> { } } +#[cfg(stage0)] impl to_bytes::IterBytes for Abi { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { self.index().iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for Abi { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + self.index().iter_bytes(lsb0, f) + } +} +#[cfg(stage0)] impl to_bytes::IterBytes for AbiSet { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { self.bits.iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for AbiSet { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + self.bits.iter_bytes(lsb0, f) + } +} impl ToStr for Abi { fn to_str(&self) -> ~str { diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index c8fc04eaea1..c516ebc402f 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -97,11 +97,18 @@ fn decode(d: &mut D) -> ident { } } +#[cfg(stage0)] impl to_bytes::IterBytes for ident { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { self.repr.iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for ident { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + self.repr.iter_bytes(lsb0, f) + } +} // Functions may or may not have names. pub type fn_ident = Option; @@ -284,6 +291,7 @@ pub enum binding_mode { bind_infer } +#[cfg(stage0)] impl to_bytes::IterBytes for binding_mode { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { match *self { @@ -297,6 +305,18 @@ fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { } } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for binding_mode { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + match *self { + bind_by_copy => 0u8.iter_bytes(lsb0, f), + + bind_by_ref(ref m) => to_bytes::iter_bytes_2(&1u8, m, lsb0, f), + + bind_infer => 2u8.iter_bytes(lsb0, f), + } + } +} #[auto_encode] #[auto_decode] @@ -330,11 +350,18 @@ pub enum pat_ { #[deriving(Eq)] pub enum mutability { m_mutbl, m_imm, m_const, } +#[cfg(stage0)] impl to_bytes::IterBytes for mutability { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (*self as u8).iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for mutability { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (*self as u8).iter_bytes(lsb0, f) + } +} #[auto_encode] #[auto_decode] @@ -345,11 +372,18 @@ pub enum Sigil { ManagedSigil } +#[cfg(stage0)] impl to_bytes::IterBytes for Sigil { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (*self as uint).iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for Sigil { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (*self as uint).iter_bytes(lsb0, f) + } +} impl ToStr for Sigil { fn to_str(&self) -> ~str { @@ -744,11 +778,18 @@ fn to_str(&self) -> ~str { } } +#[cfg(stage0)] impl to_bytes::IterBytes for int_ty { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (*self as u8).iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for int_ty { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (*self as u8).iter_bytes(lsb0, f) + } +} #[auto_encode] #[auto_decode] @@ -761,11 +802,18 @@ fn to_str(&self) -> ~str { } } +#[cfg(stage0)] impl to_bytes::IterBytes for uint_ty { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (*self as u8).iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for uint_ty { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (*self as u8).iter_bytes(lsb0, f) + } +} #[auto_encode] #[auto_decode] @@ -778,11 +826,18 @@ fn to_str(&self) -> ~str { } } +#[cfg(stage0)] impl to_bytes::IterBytes for float_ty { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (*self as u8).iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for float_ty { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (*self as u8).iter_bytes(lsb0, f) + } +} // NB Eq method appears below. #[auto_encode] @@ -823,11 +878,18 @@ fn to_str(&self) -> ~str { } } +#[cfg(stage0)] impl to_bytes::IterBytes for Onceness { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (*self as uint).iter_bytes(lsb0, f); } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for Onceness { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (*self as uint).iter_bytes(lsb0, f) + } +} #[auto_encode] #[auto_decode] @@ -874,11 +936,18 @@ pub enum ty_ { ty_infer, } +#[cfg(stage0)] impl to_bytes::IterBytes for Ty { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.span.lo, &self.span.hi, lsb0, f); } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for Ty { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + to_bytes::iter_bytes_2(&self.span.lo, &self.span.hi, lsb0, f) + } +} #[auto_encode] #[auto_decode] @@ -941,11 +1010,18 @@ fn to_str(&self) -> ~str { } } +#[cfg(stage0)] impl to_bytes::IterBytes for purity { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (*self as u8).iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for purity { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (*self as u8).iter_bytes(lsb0, f) + } +} #[auto_encode] #[auto_decode] @@ -956,11 +1032,18 @@ pub enum ret_style { return_val, // everything else } +#[cfg(stage0)] impl to_bytes::IterBytes for ret_style { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (*self as u8).iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for ret_style { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (*self as u8).iter_bytes(lsb0, f) + } +} #[auto_encode] #[auto_decode] diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 77277dea814..ceff868d11f 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -191,12 +191,21 @@ pub fn is_call_expr(e: @expr) -> bool { } // This makes def_id hashable +#[cfg(stage0)] impl to_bytes::IterBytes for def_id { #[inline(always)] fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.crate, &self.node, lsb0, f); } } +// This makes def_id hashable +#[cfg(not(stage0))] +impl to_bytes::IterBytes for def_id { + #[inline(always)] + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + to_bytes::iter_bytes_2(&self.crate, &self.node, lsb0, f) + } +} pub fn block_from_expr(e: @expr) -> blk { let blk_ = default_block(~[], option::Some::<@expr>(e), e.id); diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 846097550d1..053ed76d66b 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -65,11 +65,18 @@ fn sub(&self, rhs: &BytePos) -> BytePos { } } +#[cfg(stage0)] impl to_bytes::IterBytes for BytePos { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (**self).iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for BytePos { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (**self).iter_bytes(lsb0, f) + } +} impl Pos for CharPos { fn from_uint(n: uint) -> CharPos { CharPos(n) } @@ -83,11 +90,18 @@ fn ge(&self, other: &CharPos) -> bool { **self >= **other } fn gt(&self, other: &CharPos) -> bool { **self > **other } } +#[cfg(stage0)] impl to_bytes::IterBytes for CharPos { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (**self).iter_bytes(lsb0, f) } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for CharPos { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (**self).iter_bytes(lsb0, f) + } +} impl Add for CharPos { fn add(&self, rhs: &CharPos) -> CharPos { diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 68c74c2d12b..5129fa6ebd2 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -195,18 +195,8 @@ pub fn expand_item(extsbox: @mut SyntaxEnv, } // does this attribute list contain "macro_escape" ? -pub fn contains_macro_escape (attrs: &[ast::attribute]) -> bool{ - let mut accum = false; - do attrs.each |attr| { - let mname = attr::get_attr_name(attr); - if (mname == @~"macro_escape") { - accum = true; - false - } else { - true - } - } - accum +pub fn contains_macro_escape (attrs: &[ast::attribute]) -> bool { + attrs.any(|attr| "macro_escape" == *attr::get_attr_name(attr)) } // this macro disables (one layer of) macro diff --git a/src/libsyntax/ext/pipes/proto.rs b/src/libsyntax/ext/pipes/proto.rs index 647c7741bd8..7c78ec066d0 100644 --- a/src/libsyntax/ext/pipes/proto.rs +++ b/src/libsyntax/ext/pipes/proto.rs @@ -100,6 +100,7 @@ fn to_ty(&self, cx: @ext_ctxt) -> @ast::Ty { /// Iterate over the states that can be reached in one message /// from this state. + #[cfg(stage0)] fn reachable(&self, f: &fn(state) -> bool) { for self.messages.each |m| { match *m { @@ -111,6 +112,21 @@ fn reachable(&self, f: &fn(state) -> bool) { } } } + /// Iterate over the states that can be reached in one message + /// from this state. + #[cfg(not(stage0))] + fn reachable(&self, f: &fn(state) -> bool) -> bool { + for self.messages.each |m| { + match *m { + message(_, _, _, _, Some(next_state { state: ref id, _ })) => { + let state = self.proto.get_state((*id)); + if !f(state) { return false; } + } + _ => () + } + } + return true; + } } pub type protocol = @mut protocol_; diff --git a/src/libsyntax/opt_vec.rs b/src/libsyntax/opt_vec.rs index 600ab964e52..6110579863d 100644 --- a/src/libsyntax/opt_vec.rs +++ b/src/libsyntax/opt_vec.rs @@ -132,12 +132,20 @@ fn ne(&self, other: &OptVec) -> bool { } impl BaseIter for OptVec { + #[cfg(stage0)] fn each(&self, blk: &fn(v: &A) -> bool) { match *self { Empty => {} Vec(ref v) => v.each(blk) } } + #[cfg(not(stage0))] + fn each(&self, blk: &fn(v: &A) -> bool) -> bool { + match *self { + Empty => true, + Vec(ref v) => v.each(blk) + } + } fn size_hint(&self) -> Option { Some(self.len()) @@ -146,10 +154,16 @@ fn size_hint(&self) -> Option { impl old_iter::ExtendedIter for OptVec { #[inline(always)] + #[cfg(stage0)] fn eachi(&self, blk: &fn(v: uint, v: &A) -> bool) { old_iter::eachi(self, blk) } #[inline(always)] + #[cfg(not(stage0))] + fn eachi(&self, blk: &fn(v: uint, v: &A) -> bool) -> bool { + old_iter::eachi(self, blk) + } + #[inline(always)] fn all(&self, blk: &fn(&A) -> bool) -> bool { old_iter::all(self, blk) } diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index e486a6254e7..a4ac038cf46 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -62,12 +62,20 @@ pub enum ObsoleteSyntax { ObsoleteFixedLengthVectorType, } +#[cfg(stage0)] impl to_bytes::IterBytes for ObsoleteSyntax { #[inline(always)] fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (*self as uint).iter_bytes(lsb0, f); } } +#[cfg(not(stage0))] +impl to_bytes::IterBytes for ObsoleteSyntax { + #[inline(always)] + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (*self as uint).iter_bytes(lsb0, f) + } +} pub impl Parser { /// Reports an obsolete syntax non-fatal error. diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index fe7bd5b3bc1..15441b6fcfc 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -355,11 +355,18 @@ impl<'self> Equiv<@~str> for StringRef<'self> { fn equiv(&self, other: &@~str) -> bool { str::eq_slice(**self, **other) } } +#[cfg(stage0)] impl<'self> to_bytes::IterBytes for StringRef<'self> { fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) { (**self).iter_bytes(lsb0, f); } } +#[cfg(not(stage0))] +impl<'self> to_bytes::IterBytes for StringRef<'self> { + fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool { + (**self).iter_bytes(lsb0, f) + } +} /** * Maps a token to a record specifying the corresponding binary