Improve OwnedSlice and use it in HIR

This commit is contained in:
Vadim Petrochenkov 2015-12-19 04:20:11 +03:00
parent 29ea4eef9f
commit e3da2a9003
9 changed files with 69 additions and 34 deletions

View File

@ -1153,11 +1153,11 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
} }
fn rebuild_ty_params(&self, fn rebuild_ty_params(&self,
ty_params: P<[hir::TyParam]>, ty_params: hir::HirVec<hir::TyParam>,
lifetime: hir::Lifetime, lifetime: hir::Lifetime,
region_names: &HashSet<ast::Name>) region_names: &HashSet<ast::Name>)
-> P<[hir::TyParam]> { -> hir::HirVec<hir::TyParam> {
ty_params.map(|ty_param| { ty_params.iter().map(|ty_param| {
let bounds = self.rebuild_ty_param_bounds(ty_param.bounds.clone(), let bounds = self.rebuild_ty_param_bounds(ty_param.bounds.clone(),
lifetime, lifetime,
region_names); region_names);
@ -1168,7 +1168,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
default: ty_param.default.clone(), default: ty_param.default.clone(),
span: ty_param.span, span: ty_param.span,
} }
}) }).collect()
} }
fn rebuild_ty_param_bounds(&self, fn rebuild_ty_param_bounds(&self,
@ -1176,7 +1176,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
lifetime: hir::Lifetime, lifetime: hir::Lifetime,
region_names: &HashSet<ast::Name>) region_names: &HashSet<ast::Name>)
-> hir::TyParamBounds { -> hir::TyParamBounds {
ty_param_bounds.map(|tpb| { ty_param_bounds.iter().map(|tpb| {
match tpb { match tpb {
&hir::RegionTyParamBound(lt) => { &hir::RegionTyParamBound(lt) => {
// FIXME -- it's unclear whether I'm supposed to // FIXME -- it's unclear whether I'm supposed to
@ -1212,7 +1212,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
}, modifier) }, modifier)
} }
} }
}) }).collect()
} }
fn rebuild_expl_self(&self, fn rebuild_expl_self(&self,
@ -1248,7 +1248,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
add: &Vec<hir::Lifetime>, add: &Vec<hir::Lifetime>,
keep: &HashSet<ast::Name>, keep: &HashSet<ast::Name>,
remove: &HashSet<ast::Name>, remove: &HashSet<ast::Name>,
ty_params: P<[hir::TyParam]>, ty_params: hir::HirVec<hir::TyParam>,
where_clause: hir::WhereClause) where_clause: hir::WhereClause)
-> hir::Generics { -> hir::Generics {
let mut lifetimes = Vec::new(); let mut lifetimes = Vec::new();
@ -1498,10 +1498,10 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
} }
} }
} }
let new_types = data.types.map(|t| { let new_types = data.types.iter().map(|t| {
self.rebuild_arg_ty_or_output(&**t, lifetime, anon_nums, region_names) self.rebuild_arg_ty_or_output(&**t, lifetime, anon_nums, region_names)
}); }).collect();
let new_bindings = data.bindings.map(|b| { let new_bindings = data.bindings.iter().map(|b| {
hir::TypeBinding { hir::TypeBinding {
id: b.id, id: b.id,
name: b.name, name: b.name,
@ -1511,7 +1511,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
region_names), region_names),
span: b.span span: b.span
} }
}); }).collect();
hir::AngleBracketedParameters(hir::AngleBracketedParameterData { hir::AngleBracketedParameters(hir::AngleBracketedParameterData {
lifetimes: new_lts.into(), lifetimes: new_lts.into(),
types: new_types, types: new_types,

View File

@ -210,7 +210,7 @@ pub trait Folder : Sized {
noop_fold_ty_param(tp, self) noop_fold_ty_param(tp, self)
} }
fn fold_ty_params(&mut self, tps: P<[TyParam]>) -> P<[TyParam]> { fn fold_ty_params(&mut self, tps: HirVec<TyParam>) -> HirVec<TyParam> {
noop_fold_ty_params(tps, self) noop_fold_ty_params(tps, self)
} }
@ -575,9 +575,9 @@ pub fn noop_fold_ty_param<T: Folder>(tp: TyParam, fld: &mut T) -> TyParam {
} }
} }
pub fn noop_fold_ty_params<T: Folder>(tps: P<[TyParam]>, pub fn noop_fold_ty_params<T: Folder>(tps: HirVec<TyParam>,
fld: &mut T) fld: &mut T)
-> P<[TyParam]> { -> HirVec<TyParam> {
tps.move_map(|tp| fld.fold_ty_param(tp)) tps.move_map(|tp| fld.fold_ty_param(tp))
} }

View File

@ -56,7 +56,7 @@ use serialize::{Encodable, Decodable, Encoder, Decoder};
/// It can be `Vec`, `P<[T]>` or potentially `Box<[T]>`, or some other container with similar /// It can be `Vec`, `P<[T]>` or potentially `Box<[T]>`, or some other container with similar
/// behavior. Unlike AST, HIR is mostly a static structure, so we can use an owned slice instead /// behavior. Unlike AST, HIR is mostly a static structure, so we can use an owned slice instead
/// of `Vec` to avoid keeping extra capacity. /// of `Vec` to avoid keeping extra capacity.
pub type HirVec<T> = Vec<T>; pub type HirVec<T> = P<[T]>;
macro_rules! hir_vec { macro_rules! hir_vec {
($elem:expr; $n:expr) => ( ($elem:expr; $n:expr) => (
@ -208,8 +208,8 @@ impl PathParameters {
pub fn none() -> PathParameters { pub fn none() -> PathParameters {
AngleBracketedParameters(AngleBracketedParameterData { AngleBracketedParameters(AngleBracketedParameterData {
lifetimes: HirVec::new(), lifetimes: HirVec::new(),
types: P::empty(), types: HirVec::new(),
bindings: P::empty(), bindings: HirVec::new(),
}) })
} }
@ -282,10 +282,10 @@ pub struct AngleBracketedParameterData {
/// The lifetime parameters for this path segment. /// The lifetime parameters for this path segment.
pub lifetimes: HirVec<Lifetime>, pub lifetimes: HirVec<Lifetime>,
/// The type parameters for this path segment, if present. /// The type parameters for this path segment, if present.
pub types: P<[P<Ty>]>, pub types: HirVec<P<Ty>>,
/// Bindings (equality constraints) on associated types, if present. /// Bindings (equality constraints) on associated types, if present.
/// E.g., `Foo<A=Bar>`. /// E.g., `Foo<A=Bar>`.
pub bindings: P<[TypeBinding]>, pub bindings: HirVec<TypeBinding>,
} }
impl AngleBracketedParameterData { impl AngleBracketedParameterData {
@ -325,7 +325,7 @@ pub enum TraitBoundModifier {
Maybe, Maybe,
} }
pub type TyParamBounds = P<[TyParamBound]>; pub type TyParamBounds = HirVec<TyParamBound>;
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct TyParam { pub struct TyParam {
@ -341,7 +341,7 @@ pub struct TyParam {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct Generics { pub struct Generics {
pub lifetimes: HirVec<LifetimeDef>, pub lifetimes: HirVec<LifetimeDef>,
pub ty_params: P<[TyParam]>, pub ty_params: HirVec<TyParam>,
pub where_clause: WhereClause, pub where_clause: WhereClause,
} }

View File

@ -434,7 +434,7 @@ pub fn lower_ty_param(lctx: &LoweringContext, tp: &TyParam) -> hir::TyParam {
pub fn lower_ty_params(lctx: &LoweringContext, pub fn lower_ty_params(lctx: &LoweringContext,
tps: &P<[TyParam]>) tps: &P<[TyParam]>)
-> P<[hir::TyParam]> { -> hir::HirVec<hir::TyParam> {
tps.iter().map(|tp| lower_ty_param(lctx, tp)).collect() tps.iter().map(|tp| lower_ty_param(lctx, tp)).collect()
} }
@ -1772,19 +1772,19 @@ fn path_ident(span: Span, id: hir::Ident) -> hir::Path {
} }
fn path(span: Span, strs: Vec<hir::Ident>) -> hir::Path { fn path(span: Span, strs: Vec<hir::Ident>) -> hir::Path {
path_all(span, false, strs, hir::HirVec::new(), Vec::new(), Vec::new()) path_all(span, false, strs, hir::HirVec::new(), hir::HirVec::new(), hir::HirVec::new())
} }
fn path_global(span: Span, strs: Vec<hir::Ident>) -> hir::Path { fn path_global(span: Span, strs: Vec<hir::Ident>) -> hir::Path {
path_all(span, true, strs, hir::HirVec::new(), Vec::new(), Vec::new()) path_all(span, true, strs, hir::HirVec::new(), hir::HirVec::new(), hir::HirVec::new())
} }
fn path_all(sp: Span, fn path_all(sp: Span,
global: bool, global: bool,
mut idents: Vec<hir::Ident>, mut idents: Vec<hir::Ident>,
lifetimes: hir::HirVec<hir::Lifetime>, lifetimes: hir::HirVec<hir::Lifetime>,
types: Vec<P<hir::Ty>>, types: hir::HirVec<P<hir::Ty>>,
bindings: Vec<hir::TypeBinding>) bindings: hir::HirVec<hir::TypeBinding>)
-> hir::Path { -> hir::Path {
let last_identifier = idents.pop().unwrap(); let last_identifier = idents.pop().unwrap();
let mut segments: Vec<hir::PathSegment> = idents.into_iter() let mut segments: Vec<hir::PathSegment> = idents.into_iter()
@ -1799,8 +1799,8 @@ fn path_all(sp: Span,
identifier: last_identifier, identifier: last_identifier,
parameters: hir::AngleBracketedParameters(hir::AngleBracketedParameterData { parameters: hir::AngleBracketedParameters(hir::AngleBracketedParameterData {
lifetimes: lifetimes, lifetimes: lifetimes,
types: P::from_vec(types), types: types,
bindings: P::from_vec(bindings), bindings: bindings,
}), }),
}); });
hir::Path { hir::Path {

View File

@ -518,7 +518,7 @@ impl<'a> State<'a> {
hir::TyBareFn(ref f) => { hir::TyBareFn(ref f) => {
let generics = hir::Generics { let generics = hir::Generics {
lifetimes: f.lifetimes.clone(), lifetimes: f.lifetimes.clone(),
ty_params: P::empty(), ty_params: hir::HirVec::new(),
where_clause: hir::WhereClause { where_clause: hir::WhereClause {
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
predicates: hir::HirVec::new(), predicates: hir::HirVec::new(),
@ -2257,7 +2257,7 @@ impl<'a> State<'a> {
} }
let generics = hir::Generics { let generics = hir::Generics {
lifetimes: hir::HirVec::new(), lifetimes: hir::HirVec::new(),
ty_params: P::empty(), ty_params: hir::HirVec::new(),
where_clause: hir::WhereClause { where_clause: hir::WhereClause {
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
predicates: hir::HirVec::new(), predicates: hir::HirVec::new(),

View File

@ -335,7 +335,7 @@ pub fn is_path(e: P<Expr>) -> bool {
pub fn empty_generics() -> Generics { pub fn empty_generics() -> Generics {
Generics { Generics {
lifetimes: HirVec::new(), lifetimes: HirVec::new(),
ty_params: P::empty(), ty_params: HirVec::new(),
where_clause: WhereClause { where_clause: WhereClause {
id: DUMMY_NODE_ID, id: DUMMY_NODE_ID,
predicates: HirVec::new(), predicates: HirVec::new(),
@ -353,8 +353,8 @@ pub fn ident_to_path(s: Span, ident: Ident) -> Path {
identifier: ident, identifier: ident,
parameters: hir::AngleBracketedParameters(hir::AngleBracketedParameterData { parameters: hir::AngleBracketedParameters(hir::AngleBracketedParameterData {
lifetimes: HirVec::new(), lifetimes: HirVec::new(),
types: P::empty(), types: HirVec::new(),
bindings: P::empty(), bindings: HirVec::new(),
}), }),
}], }],
} }

View File

@ -61,3 +61,13 @@ impl<'a,'tcx:'a,T,U> ToRef for &'tcx Vec<T>
self.iter().map(|expr| expr.to_ref()).collect() self.iter().map(|expr| expr.to_ref()).collect()
} }
} }
impl<'a,'tcx:'a,T,U> ToRef for &'tcx P<[T]>
where &'tcx T: ToRef<Output=U>
{
type Output = Vec<U>;
fn to_ref(self) -> Vec<U> {
self.iter().map(|expr| expr.to_ref()).collect()
}
}

View File

@ -4906,7 +4906,7 @@ pub fn may_break(cx: &ty::ctxt, id: ast::NodeId, b: &hir::Block) -> bool {
} }
pub fn check_bounds_are_used<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, pub fn check_bounds_are_used<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
tps: &P<[hir::TyParam]>, tps: &[hir::TyParam],
ty: Ty<'tcx>) { ty: Ty<'tcx>) {
debug!("check_bounds_are_used(n_tps={}, ty={:?})", debug!("check_bounds_are_used(n_tps={}, ty={:?})",
tps.len(), ty); tps.len(), ty);

View File

@ -130,6 +130,10 @@ impl<T:fmt::Debug> fmt::Debug for P<[T]> {
} }
impl<T> P<[T]> { impl<T> P<[T]> {
pub fn new() -> P<[T]> {
P::empty()
}
pub fn empty() -> P<[T]> { pub fn empty() -> P<[T]> {
P { ptr: Default::default() } P { ptr: Default::default() }
} }
@ -177,12 +181,33 @@ impl<T: Clone> Clone for P<[T]> {
} }
} }
impl<T> From<Vec<T>> for P<[T]> {
fn from(v: Vec<T>) -> Self {
P::from_vec(v)
}
}
impl<T> Into<Vec<T>> for P<[T]> {
fn into(self) -> Vec<T> {
self.into_vec()
}
}
impl<T> FromIterator<T> for P<[T]> { impl<T> FromIterator<T> for P<[T]> {
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> P<[T]> { fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> P<[T]> {
P::from_vec(iter.into_iter().collect()) P::from_vec(iter.into_iter().collect())
} }
} }
impl<T> IntoIterator for P<[T]> {
type Item = T;
type IntoIter = vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.into_vec().into_iter()
}
}
impl<'a, T> IntoIterator for &'a P<[T]> { impl<'a, T> IntoIterator for &'a P<[T]> {
type Item = &'a T; type Item = &'a T;
type IntoIter = slice::Iter<'a, T>; type IntoIter = slice::Iter<'a, T>;