Skip to content

Commit

Permalink
Auto merge of #67803 - Centril:librustc_hir, r=Zoxc
Browse files Browse the repository at this point in the history
Extract `rustc_hir` out of `rustc`

The new crate contains:
```rust
pub mod def;
pub mod def_id;
mod hir;
pub mod hir_id;
pub mod itemlikevisit;
pub mod pat_util;
pub mod print;
mod stable_hash_impls;

pub use hir::*;
pub use hir_id::*;
pub use stable_hash_impls::HashStableContext;
```

Remains to be done in follow-up PRs:

- Move `rustc::hir::map` into `rustc_hir_map` -- this has to be a separate crate due to the `dep_graph` (blocked on #67761).

- Move references to `rustc::hir` to `rustc_hir` where possible.

cc #65031

r? @Zoxc
  • Loading branch information
bors committed Jan 4, 2020
2 parents 760ce94 + cdf32e1 commit 7494250
Show file tree
Hide file tree
Showing 139 changed files with 1,104 additions and 950 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3091,6 +3091,7 @@ dependencies = [
"rustc_errors",
"rustc_feature",
"rustc_fs_util",
"rustc_hir",
"rustc_index",
"rustc_macros",
"rustc_session",
Expand Down Expand Up @@ -3368,6 +3369,7 @@ dependencies = [
"rustc_error_codes",
"rustc_errors",
"rustc_index",
"rustc_session",
"rustc_span",
"rustc_target",
"smallvec 1.0.0",
Expand Down Expand Up @@ -3562,6 +3564,22 @@ dependencies = [
name = "rustc_fs_util"
version = "0.0.0"

[[package]]
name = "rustc_hir"
version = "0.0.0"
dependencies = [
"rustc_data_structures",
"rustc_errors",
"rustc_index",
"rustc_macros",
"rustc_session",
"rustc_span",
"rustc_target",
"serialize",
"smallvec 1.0.0",
"syntax",
]

[[package]]
name = "rustc_incremental"
version = "0.0.0"
Expand Down Expand Up @@ -3602,6 +3620,7 @@ dependencies = [
"rustc_data_structures",
"rustc_errors",
"rustc_expand",
"rustc_hir",
"rustc_incremental",
"rustc_lint",
"rustc_metadata",
Expand Down Expand Up @@ -3808,6 +3827,7 @@ dependencies = [
"rustc_expand",
"rustc_feature",
"rustc_metadata",
"rustc_session",
"rustc_span",
"smallvec 1.0.0",
"syntax",
Expand Down
1 change: 1 addition & 0 deletions src/librustc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rustc-rayon-core = "0.3.0"
polonius-engine = "0.11.0"
rustc_apfloat = { path = "../librustc_apfloat" }
rustc_feature = { path = "../librustc_feature" }
rustc_hir = { path = "../librustc_hir" }
rustc_target = { path = "../librustc_target" }
rustc_macros = { path = "../librustc_macros" }
rustc_data_structures = { path = "../librustc_data_structures" }
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ macro_rules! arena_types {
rustc::hir::def_id::DefId,
rustc::ty::subst::SubstsRef<$tcx>
)>,
[few, decode] mir_keys: rustc::util::nodemap::DefIdSet,
[few, decode] mir_keys: rustc::hir::def_id::DefIdSet,
[decode] specialization_graph: rustc::traits::specialization_graph::Graph,
[] region_scope_tree: rustc::middle::region::ScopeTree,
[] item_local_set: rustc::util::nodemap::ItemLocalSet,
[] item_local_set: rustc::hir::ItemLocalSet,
[decode] mir_const_qualif: rustc_index::bit_set::BitSet<rustc::mir::Local>,
[] trait_impls_of: rustc::ty::trait_def::TraitImpls,
[] dropck_outlives:
Expand Down Expand Up @@ -87,7 +87,7 @@ macro_rules! arena_types {
>,
[few] crate_inherent_impls: rustc::ty::CrateInherentImpls,
[few] upstream_monomorphizations:
rustc::util::nodemap::DefIdMap<
rustc::hir::def_id::DefIdMap<
rustc_data_structures::fx::FxHashMap<
rustc::ty::subst::SubstsRef<'tcx>,
rustc::hir::def_id::CrateNum
Expand All @@ -113,10 +113,10 @@ macro_rules! arena_types {
>,
[few] get_lib_features: rustc::middle::lib_features::LibFeatures,
[few] defined_lib_features: rustc::middle::lang_items::LanguageItems,
[few] visible_parent_map: rustc::util::nodemap::DefIdMap<rustc::hir::def_id::DefId>,
[few] visible_parent_map: rustc::hir::def_id::DefIdMap<rustc::hir::def_id::DefId>,
[few] foreign_module: rustc::middle::cstore::ForeignModule,
[few] foreign_modules: Vec<rustc::middle::cstore::ForeignModule>,
[few] reachable_non_generics: rustc::util::nodemap::DefIdMap<
[few] reachable_non_generics: rustc::hir::def_id::DefIdMap<
rustc::middle::exported_symbols::SymbolExportLevel
>,
[few] crate_variances: rustc::ty::CrateVariancesMap<'tcx>,
Expand Down
6 changes: 0 additions & 6 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,6 @@ impl DefPathHash {
}
}

impl DefId {
pub fn to_dep_node(self, tcx: TyCtxt<'_>, kind: DepKind) -> DepNode {
DepNode::from_def_path_hash(kind, tcx.def_path_hash(self))
}
}

rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
// We use this for most things when incr. comp. is turned off.
[] Null,
Expand Down
30 changes: 30 additions & 0 deletions src/librustc/hir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! HIR datatypes. See the [rustc guide] for more info.
//!
//! [rustc guide]: https://rust-lang.github.io/rustc-guide/hir.html

pub mod check_attr;
pub use rustc_hir::def;
pub mod exports;
pub use rustc_hir::def_id;
pub use rustc_hir::hir_id::*;
pub mod intravisit;
pub use rustc_hir::itemlikevisit;
pub mod map;
pub use rustc_hir::pat_util;
pub use rustc_hir::print;
pub mod upvars;

pub use rustc_hir::BlockCheckMode::*;
pub use rustc_hir::FunctionRetTy::*;
pub use rustc_hir::PrimTy::*;
pub use rustc_hir::UnOp::*;
pub use rustc_hir::UnsafeSource::*;
pub use rustc_hir::*;

use crate::ty::query::Providers;

pub fn provide(providers: &mut Providers<'_>) {
check_attr::provide(providers);
map::provide(providers);
upvars::provide(providers);
}
3 changes: 2 additions & 1 deletion src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use crate::hir::def_id::DefId;
use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
use crate::hir::DUMMY_HIR_ID;
use crate::hir::{self, Attribute, HirId, Item, ItemKind, TraitItem, TraitItemKind};
use crate::hir::{self, HirId, Item, ItemKind, TraitItem, TraitItemKind};
use crate::lint::builtin::UNUSED_ATTRIBUTES;
use crate::ty::query::Providers;
use crate::ty::TyCtxt;

use rustc_error_codes::*;
use rustc_span::symbol::sym;
use rustc_span::Span;
use syntax::ast::Attribute;
use syntax::attr;

use std::fmt::{self, Display};
Expand Down
32 changes: 32 additions & 0 deletions src/librustc/hir/exports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::hir::def::Res;
use crate::hir::def_id::DefIdMap;
use crate::ty;

use rustc_macros::HashStable;
use rustc_span::Span;
use syntax::ast;

use std::fmt::Debug;

/// This is the replacement export map. It maps a module to all of the exports
/// within.
pub type ExportMap<Id> = DefIdMap<Vec<Export<Id>>>;

#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
pub struct Export<Id> {
/// The name of the target.
pub ident: ast::Ident,
/// The resolution of the target.
pub res: Res<Id>,
/// The span of the target.
pub span: Span,
/// The visibility of the export.
/// We include non-`pub` exports for hygienic macros that get used from extern crates.
pub vis: ty::Visibility,
}

impl<Id> Export<Id> {
pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Export<R> {
Export { ident: self.ident, res: self.res.map_id(map), span: self.span, vis: self.vis }
}
}
59 changes: 56 additions & 3 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,66 @@
//! This order consistency is required in a few places in rustc, for
//! example generator inference, and possibly also HIR borrowck.

use super::itemlikevisit::DeepVisitor;

use crate::hir::itemlikevisit::{ItemLikeVisitor, ParItemLikeVisitor};
use crate::hir::map::Map;
use crate::hir::*;

use rustc_span::Span;
use syntax::ast::{Attribute, Ident, Name};
use syntax::ast::{Attribute, Ident, Label, Name};

pub struct DeepVisitor<'v, V> {
visitor: &'v mut V,
}

impl<'v, 'hir, V> DeepVisitor<'v, V>
where
V: Visitor<'hir> + 'v,
{
pub fn new(base: &'v mut V) -> Self {
DeepVisitor { visitor: base }
}
}

impl<'v, 'hir, V> ItemLikeVisitor<'hir> for DeepVisitor<'v, V>
where
V: Visitor<'hir>,
{
fn visit_item(&mut self, item: &'hir Item<'hir>) {
self.visitor.visit_item(item);
}

fn visit_trait_item(&mut self, trait_item: &'hir TraitItem<'hir>) {
self.visitor.visit_trait_item(trait_item);
}

fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>) {
self.visitor.visit_impl_item(impl_item);
}
}

pub trait IntoVisitor<'hir> {
type Visitor: Visitor<'hir>;
fn into_visitor(&self) -> Self::Visitor;
}

pub struct ParDeepVisitor<V>(pub V);

impl<'hir, V> ParItemLikeVisitor<'hir> for ParDeepVisitor<V>
where
V: IntoVisitor<'hir>,
{
fn visit_item(&self, item: &'hir Item<'hir>) {
self.0.into_visitor().visit_item(item);
}

fn visit_trait_item(&self, trait_item: &'hir TraitItem<'hir>) {
self.0.into_visitor().visit_trait_item(trait_item);
}

fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>) {
self.0.into_visitor().visit_impl_item(impl_item);
}
}

#[derive(Copy, Clone)]
pub enum FnKind<'a> {
Expand Down
7 changes: 3 additions & 4 deletions src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use super::*;
use crate::dep_graph::{DepGraph, DepKind, DepNodeIndex};
use crate::hir;
use crate::hir::def_id::{CrateNum, LOCAL_CRATE};
use crate::hir::def_id::{CrateNum, DefIndex, LOCAL_CRATE};
use crate::hir::intravisit::{NestedVisitorMap, Visitor};
use crate::hir::map::HirEntryMap;
use crate::ich::Fingerprint;
use crate::middle::cstore::CrateStore;
use crate::session::CrateDisambiguator;
use crate::session::Session;
use crate::util::nodemap::FxHashMap;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::svh::Svh;
use rustc_index::vec::IndexVec;
use rustc_session::{CrateDisambiguator, Session};
use rustc_span::source_map::SourceMap;
use rustc_span::Span;
use std::iter::repeat;
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use crate::hir;
use crate::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
use crate::ich::Fingerprint;
use crate::session::CrateDisambiguator;
use crate::util::nodemap::NodeMap;

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_index::vec::IndexVec;
use rustc_session::node_id::NodeMap;
use rustc_session::CrateDisambiguator;
use rustc_span::hygiene::ExpnId;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span;
Expand Down
48 changes: 5 additions & 43 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ pub use self::definitions::{
};

use crate::dep_graph::{DepGraph, DepKind, DepNode, DepNodeIndex};
use crate::hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
use crate::hir::def::{DefKind, Res};
use crate::hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
use crate::hir::itemlikevisit::ItemLikeVisitor;
use crate::hir::print::Nested;
use crate::hir::DefKind;
use crate::hir::*;
use crate::middle::cstore::CrateStoreDyn;
use crate::ty::query::Providers;
use crate::util::common::time;
use crate::util::nodemap::FxHashMap;

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::svh::Svh;
use rustc_index::vec::IndexVec;
use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::Spanned;
use rustc_span::symbol::kw;
use rustc_span::{Span, DUMMY_SP};
use rustc_target::spec::abi::Abi;
use syntax::ast::{self, Name, NodeId};
Expand Down Expand Up @@ -1203,7 +1204,7 @@ impl Named for ImplItem<'_> {
}

pub fn map_crate<'hir>(
sess: &crate::session::Session,
sess: &rustc_session::Session,
cstore: &CrateStoreDyn,
forest: &'hir Forest<'hir>,
definitions: Definitions,
Expand Down Expand Up @@ -1265,45 +1266,6 @@ impl<'hir> print::PpAnn for Map<'hir> {
}
}

impl<'a> print::State<'a> {
pub fn print_node(&mut self, node: Node<'_>) {
match node {
Node::Param(a) => self.print_param(&a),
Node::Item(a) => self.print_item(&a),
Node::ForeignItem(a) => self.print_foreign_item(&a),
Node::TraitItem(a) => self.print_trait_item(a),
Node::ImplItem(a) => self.print_impl_item(a),
Node::Variant(a) => self.print_variant(&a),
Node::AnonConst(a) => self.print_anon_const(&a),
Node::Expr(a) => self.print_expr(&a),
Node::Stmt(a) => self.print_stmt(&a),
Node::PathSegment(a) => self.print_path_segment(&a),
Node::Ty(a) => self.print_type(&a),
Node::TraitRef(a) => self.print_trait_ref(&a),
Node::Binding(a) | Node::Pat(a) => self.print_pat(&a),
Node::Arm(a) => self.print_arm(&a),
Node::Block(a) => {
// Containing cbox, will be closed by print-block at `}`.
self.cbox(print::INDENT_UNIT);
// Head-ibox, will be closed by print-block after `{`.
self.ibox(0);
self.print_block(&a)
}
Node::Lifetime(a) => self.print_lifetime(&a),
Node::Visibility(a) => self.print_visibility(&a),
Node::GenericParam(_) => bug!("cannot print Node::GenericParam"),
Node::Field(_) => bug!("cannot print StructField"),
// These cases do not carry enough information in the
// `hir_map` to reconstruct their full structure for pretty
// printing.
Node::Ctor(..) => bug!("cannot print isolated Ctor"),
Node::Local(a) => self.print_local_decl(&a),
Node::MacroDef(_) => bug!("cannot print MacroDef"),
Node::Crate => bug!("cannot print Crate"),
}
}
}

fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
let id_str = format!(" (hir_id={})", id);
let id_str = if include_id { &id_str[..] } else { "" };
Expand Down
Loading

0 comments on commit 7494250

Please sign in to comment.