|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use hir::map as hir_map; |
| 12 | +use rustc::hir; |
| 13 | +use rustc::hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; |
| 14 | +use rustc::hir::itemlikevisit::ItemLikeVisitor; |
| 15 | +use rustc::ty::maps::Providers; |
| 16 | +use rustc::ty::{self, CratePredicatesMap, TyCtxt}; |
| 17 | +use rustc_data_structures::sync::Lrc; |
| 18 | +use util::nodemap::FxHashMap; |
| 19 | + |
| 20 | +pub fn explicit_predicates<'tcx>( |
| 21 | + tcx: TyCtxt<'_, 'tcx, 'tcx>, |
| 22 | + crate_num: CrateNum, |
| 23 | +) -> FxHashMap<DefId, Lrc<Vec<ty::Predicate<'tcx>>>> { |
| 24 | + assert_eq!(crate_num, LOCAL_CRATE); |
| 25 | + let mut predicates: FxHashMap<DefId, Lrc<Vec<ty::Predicate<'tcx>>>> = FxHashMap(); |
| 26 | + |
| 27 | + // iterate over the entire crate |
| 28 | + tcx.hir.krate().visit_all_item_likes(&mut ExplicitVisitor { |
| 29 | + tcx: tcx, |
| 30 | + explicit_predicates: &mut predicates, |
| 31 | + crate_num: crate_num, |
| 32 | + }); |
| 33 | + |
| 34 | + predicates |
| 35 | +} |
| 36 | + |
| 37 | +pub struct ExplicitVisitor<'cx, 'tcx: 'cx> { |
| 38 | + tcx: TyCtxt<'cx, 'tcx, 'tcx>, |
| 39 | + explicit_predicates: &'cx mut FxHashMap<DefId, Lrc<Vec<ty::Predicate<'tcx>>>>, |
| 40 | + crate_num: CrateNum, |
| 41 | +} |
| 42 | + |
| 43 | +impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for ExplicitVisitor<'cx, 'tcx> { |
| 44 | + fn visit_item(&mut self, item: &'tcx hir::Item) { |
| 45 | + let def_id = DefId { |
| 46 | + krate: self.crate_num, |
| 47 | + index: item.hir_id.owner, |
| 48 | + }; |
| 49 | + |
| 50 | + let local_explicit_predicate = self.tcx.explicit_predicates_of(def_id); |
| 51 | + |
| 52 | + let filtered_predicates = local_explicit_predicate |
| 53 | + .predicates |
| 54 | + .into_iter() |
| 55 | + .filter(|pred| match pred { |
| 56 | + ty::Predicate::TypeOutlives(..) | ty::Predicate::RegionOutlives(..) => true, |
| 57 | + |
| 58 | + ty::Predicate::Trait(..) |
| 59 | + | ty::Predicate::Projection(..) |
| 60 | + | ty::Predicate::WellFormed(..) |
| 61 | + | ty::Predicate::ObjectSafe(..) |
| 62 | + | ty::Predicate::ClosureKind(..) |
| 63 | + | ty::Predicate::Subtype(..) |
| 64 | + | ty::Predicate::ConstEvaluatable(..) => false, |
| 65 | + }) |
| 66 | + .collect(); |
| 67 | + |
| 68 | + match item.node { |
| 69 | + hir::ItemStruct(..) | hir::ItemEnum(..) => { |
| 70 | + self.tcx.adt_def(def_id); |
| 71 | + } |
| 72 | + _ => {} |
| 73 | + } |
| 74 | + |
| 75 | + self.explicit_predicates |
| 76 | + .insert(def_id, Lrc::new(filtered_predicates)); |
| 77 | + } |
| 78 | + |
| 79 | + fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {} |
| 80 | + |
| 81 | + fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {} |
| 82 | +} |
0 commit comments