Skip to content

Commit

Permalink
cleanup: Remove DefIndexAddressSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed May 8, 2019
1 parent e630580 commit a49aa9b
Show file tree
Hide file tree
Showing 38 changed files with 242 additions and 399 deletions.
67 changes: 11 additions & 56 deletions src/librustc/hir/def_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::ty::{self, TyCtxt};
use crate::hir::map::definitions::FIRST_FREE_HIGH_DEF_INDEX;
use crate::hir::map::definitions::FIRST_FREE_DEF_INDEX;
use rustc_data_structures::indexed_vec::Idx;
use serialize;
use std::fmt;
Expand Down Expand Up @@ -99,17 +99,6 @@ impl serialize::UseSpecializedDecodable for CrateNum {}
/// A DefIndex is an index into the hir-map for a crate, identifying a
/// particular definition. It should really be considered an interned
/// shorthand for a particular DefPath.
///
/// At the moment we are allocating the numerical values of DefIndexes from two
/// address spaces: DefIndexAddressSpace::Low and DefIndexAddressSpace::High.
/// This allows us to allocate the DefIndexes of all item-likes
/// (Items, TraitItems, and ImplItems) into one of these spaces and
/// consequently use a simple array for lookup tables keyed by DefIndex and
/// known to be densely populated. This is especially important for the HIR map.
///
/// Since the DefIndex is mostly treated as an opaque ID, you probably
/// don't have to care about these address spaces.

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub struct DefIndex(u32);

Expand All @@ -119,67 +108,49 @@ pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);

impl fmt::Debug for DefIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,
"DefIndex({}:{})",
self.address_space().index(),
self.as_array_index())
write!(f, "DefIndex({})", self.as_array_index())
}
}

impl DefIndex {
#[inline]
pub fn address_space(&self) -> DefIndexAddressSpace {
match self.0 & 1 {
0 => DefIndexAddressSpace::Low,
1 => DefIndexAddressSpace::High,
_ => unreachable!()
}
}

/// Converts this DefIndex into a zero-based array index.
/// This index is the offset within the given DefIndexAddressSpace.
#[inline]
pub fn as_array_index(&self) -> usize {
(self.0 >> 1) as usize
self.0 as usize
}

#[inline]
pub fn from_array_index(i: usize, address_space: DefIndexAddressSpace) -> DefIndex {
DefIndex::from_raw_u32(((i << 1) | (address_space as usize)) as u32)
pub fn from_array_index(i: usize) -> DefIndex {
DefIndex(i as u32)
}

// Proc macros from a proc-macro crate have a kind of virtual DefIndex. This
// function maps the index of the macro within the crate (which is also the
// index of the macro in the CrateMetadata::proc_macros array) to the
// corresponding DefIndex.
pub fn from_proc_macro_index(proc_macro_index: usize) -> DefIndex {
// DefIndex for proc macros start from FIRST_FREE_HIGH_DEF_INDEX,
// because the first FIRST_FREE_HIGH_DEF_INDEX indexes are reserved
// DefIndex for proc macros start from FIRST_FREE_DEF_INDEX,
// because the first FIRST_FREE_DEF_INDEX indexes are reserved
// for internal use.
let def_index = DefIndex::from_array_index(
proc_macro_index.checked_add(FIRST_FREE_HIGH_DEF_INDEX)
.expect("integer overflow adding `proc_macro_index`"),
DefIndexAddressSpace::High);
proc_macro_index.checked_add(FIRST_FREE_DEF_INDEX)
.expect("integer overflow adding `proc_macro_index`"));
assert!(def_index != CRATE_DEF_INDEX);
def_index
}

// This function is the reverse of from_proc_macro_index() above.
pub fn to_proc_macro_index(self: DefIndex) -> usize {
assert_eq!(self.address_space(), DefIndexAddressSpace::High);

self.as_array_index().checked_sub(FIRST_FREE_HIGH_DEF_INDEX)
self.as_array_index().checked_sub(FIRST_FREE_DEF_INDEX)
.unwrap_or_else(|| {
bug!("using local index {:?} as proc-macro index", self)
})
}

// Don't use this if you don't know about the DefIndex encoding.
pub fn from_raw_u32(x: u32) -> DefIndex {
DefIndex(x)
}

// Don't use this if you don't know about the DefIndex encoding.
pub fn as_raw_u32(&self) -> u32 {
self.0
}
Expand All @@ -188,19 +159,6 @@ impl DefIndex {
impl serialize::UseSpecializedEncodable for DefIndex {}
impl serialize::UseSpecializedDecodable for DefIndex {}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum DefIndexAddressSpace {
Low = 0,
High = 1,
}

impl DefIndexAddressSpace {
#[inline]
pub fn index(&self) -> usize {
*self as usize
}
}

/// A `DefId` identifies a particular *definition*, by combining a crate
/// index and a def index.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
Expand All @@ -211,10 +169,7 @@ pub struct DefId {

impl fmt::Debug for DefId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "DefId({}/{}:{}",
self.krate,
self.index.address_space().index(),
self.index.as_array_index())?;
write!(f, "DefId({}:{}", self.krate, self.index.as_array_index())?;

ty::tls::with_opt(|opt_tcx| {
if let Some(tcx) = opt_tcx {
Expand Down
5 changes: 1 addition & 4 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::dep_graph::DepGraph;
use crate::hir::{self, ParamName};
use crate::hir::HirVec;
use crate::hir::map::{DefKey, DefPathData, Definitions};
use crate::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CRATE_DEF_INDEX};
use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
use crate::hir::def::{Res, DefKind, PartialRes, PerNS};
use crate::hir::{GenericArg, ConstArg};
use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
Expand Down Expand Up @@ -418,7 +418,6 @@ impl<'a> LoweringContext<'a> {
owner,
id,
DefPathData::Misc,
DefIndexAddressSpace::High,
Mark::root(),
tree.prefix.span,
);
Expand Down Expand Up @@ -962,7 +961,6 @@ impl<'a> LoweringContext<'a> {
parent_index,
node_id,
DefPathData::LifetimeNs(str_name),
DefIndexAddressSpace::High,
Mark::root(),
span,
);
Expand Down Expand Up @@ -1763,7 +1761,6 @@ impl<'a> LoweringContext<'a> {
self.parent,
def_node_id,
DefPathData::LifetimeNs(name.ident().as_interned_str()),
DefIndexAddressSpace::High,
Mark::root(),
lifetime.span,
);
Expand Down
9 changes: 2 additions & 7 deletions src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,10 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
);
}

let (lo, hi) = definitions.def_index_counts_lo_hi();

let mut collector = NodeCollector {
krate,
source_map: sess.source_map(),
map: [
repeat(None).take(lo).collect(),
repeat(None).take(hi).collect(),
],
map: vec![None; definitions.def_index_count()],
parent_node: hir::CRATE_HIR_ID,
current_signature_dep_index: root_mod_sig_dep_index,
current_full_dep_index: root_mod_full_dep_index,
Expand Down Expand Up @@ -231,7 +226,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {

fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>) {
debug!("hir_map: {:?} => {:?}", id, entry);
let local_map = &mut self.map[id.owner.address_space().index()][id.owner.as_array_index()];
let local_map = &mut self.map[id.owner.as_array_index()];
let i = id.local_id.as_u32() as usize;
if local_map.is_none() {
*local_map = Some(IndexVec::with_capacity(i + 1));
Expand Down
36 changes: 13 additions & 23 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::hir::map::definitions::*;
use crate::hir::def_id::{CRATE_DEF_INDEX, DefIndex, DefIndexAddressSpace};
use crate::hir::def_id::{CRATE_DEF_INDEX, DefIndex};
use crate::session::CrateDisambiguator;

use syntax::ast::*;
Expand All @@ -10,8 +10,6 @@ use syntax::symbol::Symbol;
use syntax::parse::token::{self, Token};
use syntax_pos::Span;

use crate::hir::map::{ITEM_LIKE_SPACE, REGULAR_SPACE};

/// Creates `DefId`s for nodes in the AST.
pub struct DefCollector<'a> {
definitions: &'a mut Definitions,
Expand Down Expand Up @@ -47,13 +45,12 @@ impl<'a> DefCollector<'a> {
fn create_def(&mut self,
node_id: NodeId,
data: DefPathData,
address_space: DefIndexAddressSpace,
span: Span)
-> DefIndex {
let parent_def = self.parent_def.unwrap();
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
self.definitions
.create_def_with_parent(parent_def, node_id, data, address_space, self.expansion, span)
.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
}

pub fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
Expand Down Expand Up @@ -85,9 +82,9 @@ impl<'a> DefCollector<'a> {
// For async functions, we need to create their inner defs inside of a
// closure to match their desugared representation.
let fn_def_data = DefPathData::ValueNs(name.as_interned_str());
let fn_def = self.create_def(id, fn_def_data, ITEM_LIKE_SPACE, span);
let fn_def = self.create_def(id, fn_def_data, span);
return self.with_parent(fn_def, |this| {
this.create_def(*return_impl_trait_id, DefPathData::ImplTrait, REGULAR_SPACE, span);
this.create_def(*return_impl_trait_id, DefPathData::ImplTrait, span);

visit::walk_generics(this, generics);

Expand All @@ -106,7 +103,7 @@ impl<'a> DefCollector<'a> {
visit::walk_fn_ret_ty(this, &decl.output);

let closure_def = this.create_def(
*closure_id, DefPathData::ClosureExpr, REGULAR_SPACE, span,
*closure_id, DefPathData::ClosureExpr, span,
);
this.with_parent(closure_def, |this| {
use visit::Visitor;
Expand Down Expand Up @@ -173,14 +170,14 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
return visit::walk_item(self, i);
}
};
let def = self.create_def(i.id, def_data, ITEM_LIKE_SPACE, i.span);
let def = self.create_def(i.id, def_data, i.span);

self.with_parent(def, |this| {
match i.node {
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
// If this is a unit or tuple-like struct, register the constructor.
if let Some(ctor_hir_id) = struct_def.ctor_id() {
this.create_def(ctor_hir_id, DefPathData::Ctor, REGULAR_SPACE, i.span);
this.create_def(ctor_hir_id, DefPathData::Ctor, i.span);
}
}
_ => {}
Expand All @@ -190,7 +187,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
}

fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
self.create_def(id, DefPathData::Misc, ITEM_LIKE_SPACE, use_tree.span);
self.create_def(id, DefPathData::Misc, use_tree.span);
visit::walk_use_tree(self, use_tree, id);
}

Expand All @@ -201,7 +198,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {

let def = self.create_def(foreign_item.id,
DefPathData::ValueNs(foreign_item.ident.as_interned_str()),
REGULAR_SPACE,
foreign_item.span);

self.with_parent(def, |this| {
Expand All @@ -212,11 +208,10 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
fn visit_variant(&mut self, v: &'a Variant, g: &'a Generics, item_id: NodeId) {
let def = self.create_def(v.node.id,
DefPathData::TypeNs(v.node.ident.as_interned_str()),
REGULAR_SPACE,
v.span);
self.with_parent(def, |this| {
if let Some(ctor_hir_id) = v.node.data.ctor_id() {
this.create_def(ctor_hir_id, DefPathData::Ctor, REGULAR_SPACE, v.span);
this.create_def(ctor_hir_id, DefPathData::Ctor, v.span);
}
visit::walk_variant(this, v, g, item_id)
});
Expand All @@ -229,7 +224,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
let def = self.create_def(field.id,
DefPathData::ValueNs(name.as_interned_str()),
REGULAR_SPACE,
field.span);
self.with_parent(def, |this| this.visit_struct_field(field));
}
Expand All @@ -242,7 +236,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
GenericParamKind::Type { .. } => DefPathData::TypeNs(name),
GenericParamKind::Const { .. } => DefPathData::ValueNs(name),
};
self.create_def(param.id, def_path_data, REGULAR_SPACE, param.ident.span);
self.create_def(param.id, def_path_data, param.ident.span);

visit::walk_generic_param(self, param);
}
Expand All @@ -257,7 +251,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id),
};

let def = self.create_def(ti.id, def_data, ITEM_LIKE_SPACE, ti.span);
let def = self.create_def(ti.id, def_data, ti.span);
self.with_parent(def, |this| visit::walk_trait_item(this, ti));
}

Expand Down Expand Up @@ -286,7 +280,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
};

let def = self.create_def(ii.id, def_data, ITEM_LIKE_SPACE, ii.span);
let def = self.create_def(ii.id, def_data, ii.span);
self.with_parent(def, |this| visit::walk_impl_item(this, ii));
}

Expand All @@ -300,7 +294,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
fn visit_anon_const(&mut self, constant: &'a AnonConst) {
let def = self.create_def(constant.id,
DefPathData::AnonConst,
REGULAR_SPACE,
constant.value.span);
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
}
Expand All @@ -313,7 +306,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
ExprKind::Closure(_, ref asyncness, ..) => {
let closure_def = self.create_def(expr.id,
DefPathData::ClosureExpr,
REGULAR_SPACE,
expr.span);
self.parent_def = Some(closure_def);

Expand All @@ -322,15 +314,13 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
if let IsAsync::Async { closure_id, .. } = asyncness {
let async_def = self.create_def(*closure_id,
DefPathData::ClosureExpr,
REGULAR_SPACE,
expr.span);
self.parent_def = Some(async_def);
}
}
ExprKind::Async(_, async_id, _) => {
let async_def = self.create_def(async_id,
DefPathData::ClosureExpr,
REGULAR_SPACE,
expr.span);
self.parent_def = Some(async_def);
}
Expand All @@ -345,7 +335,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
match ty.node {
TyKind::Mac(..) => return self.visit_macro_invoc(ty.id),
TyKind::ImplTrait(node_id, _) => {
self.create_def(node_id, DefPathData::ImplTrait, REGULAR_SPACE, ty.span);
self.create_def(node_id, DefPathData::ImplTrait, ty.span);
}
_ => {}
}
Expand Down
Loading

0 comments on commit a49aa9b

Please sign in to comment.