Skip to content

Commit e8aeb83

Browse files
committed
hir: add HirId methods
1 parent 28fec68 commit e8aeb83

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

src/librustc/hir/map/collector.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
use dep_graph::{DepGraph, DepKind, DepNodeIndex};
3+
use hir;
34
use hir::def_id::{LOCAL_CRATE, CrateNum};
45
use hir::intravisit::{Visitor, NestedVisitorMap};
56
use rustc_data_structures::svh::Svh;
@@ -28,6 +29,8 @@ pub(super) struct NodeCollector<'a, 'hir> {
2829
/// The parent of this node
2930
parent_node: NodeId,
3031

32+
parent_hir: hir::HirId,
33+
3134
// These fields keep track of the currently relevant DepNodes during
3235
// the visitor's traversal.
3336
current_dep_node_owner: DefIndex,
@@ -145,6 +148,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
145148
source_map: sess.source_map(),
146149
map: repeat(None).take(sess.current_node_id_count()).collect(),
147150
parent_node: CRATE_NODE_ID,
151+
parent_hir: hir::CRATE_HIR_ID,
148152
current_signature_dep_index: root_mod_sig_dep_index,
149153
current_full_dep_index: root_mod_full_dep_index,
150154
current_dep_node_owner: CRATE_DEF_INDEX,
@@ -156,6 +160,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
156160
};
157161
collector.insert_entry(CRATE_NODE_ID, Entry {
158162
parent: CRATE_NODE_ID,
163+
parent_hir: hir::CRATE_HIR_ID,
159164
dep_node: root_mod_sig_dep_index,
160165
node: Node::Crate,
161166
});
@@ -226,6 +231,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
226231
fn insert(&mut self, span: Span, id: NodeId, node: Node<'hir>) {
227232
let entry = Entry {
228233
parent: self.parent_node,
234+
parent_hir: self.parent_hir,
229235
dep_node: if self.currently_in_body {
230236
self.current_full_dep_index
231237
} else {

src/librustc/hir/map/definitions.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use syntax::ast;
2020
use syntax::ext::hygiene::Mark;
2121
use syntax::symbol::{Symbol, InternedString};
2222
use syntax_pos::{Span, DUMMY_SP};
23-
use util::nodemap::NodeMap;
23+
use util::nodemap::{HirIdMap, NodeMap};
2424

2525
/// The DefPathTable maps DefIndexes to DefKeys and vice versa.
2626
/// Internally the DefPathTable holds a tree of DefKeys, where each DefKey
@@ -147,6 +147,7 @@ impl Decodable for DefPathTable {
147147
pub struct Definitions {
148148
table: DefPathTable,
149149
node_to_def_index: NodeMap<DefIndex>,
150+
hir_to_def_index: HirIdMap<DefIndex>,
150151
def_index_to_node: [Vec<ast::NodeId>; 2],
151152
pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
152153
/// If `Mark` is an ID of some macro expansion,
@@ -441,16 +442,34 @@ impl Definitions {
441442
self.node_to_def_index.get(&node).cloned()
442443
}
443444

445+
// FIXME(@ljedrz): replace the NodeId variant
446+
#[inline]
447+
pub fn opt_def_index_from_hir_id(&self, hir: hir::HirId) -> Option<DefIndex> {
448+
self.hir_to_def_index.get(&hir).cloned()
449+
}
450+
444451
#[inline]
445452
pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
446453
self.opt_def_index(node).map(DefId::local)
447454
}
448455

456+
// FIXME(@ljedrz): replace the NodeId variant
457+
#[inline]
458+
pub fn opt_local_def_id_from_hir_id(&self, hir: hir::HirId) -> Option<DefId> {
459+
self.opt_def_index_from_hir_id(hir).map(DefId::local)
460+
}
461+
449462
#[inline]
450463
pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
451464
self.opt_local_def_id(node).unwrap()
452465
}
453466

467+
// FIXME(@ljedrz): replace the NodeId variant
468+
#[inline]
469+
pub fn local_def_id_from_hir_id(&self, hir: hir::HirId) -> DefId {
470+
self.opt_local_def_id_from_hir_id(hir).unwrap()
471+
}
472+
454473
#[inline]
455474
pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
456475
if def_id.krate == LOCAL_CRATE {
@@ -467,6 +486,21 @@ impl Definitions {
467486
}
468487
}
469488

489+
// FIXME(@ljedrz): replace the NodeId variant
490+
#[inline]
491+
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<hir::HirId> {
492+
if def_id.krate == LOCAL_CRATE {
493+
let hir_id = self.def_index_to_hir_id(def_id.index);
494+
if hir_id != hir::DUMMY_HIR_ID {
495+
Some(hir_id)
496+
} else {
497+
None
498+
}
499+
} else {
500+
None
501+
}
502+
}
503+
470504
#[inline]
471505
pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
472506
self.node_to_hir_id[node_id]
@@ -515,6 +549,7 @@ impl Definitions {
515549
assert!(self.def_index_to_node[address_space.index()].is_empty());
516550
self.def_index_to_node[address_space.index()].push(ast::CRATE_NODE_ID);
517551
self.node_to_def_index.insert(ast::CRATE_NODE_ID, root_index);
552+
self.hir_to_def_index.insert(hir::CRATE_HIR_ID, root_index);
518553

519554
// Allocate some other DefIndices that always must exist.
520555
GlobalMetaDataKind::allocate_def_indices(self);
@@ -575,6 +610,9 @@ impl Definitions {
575610
if node_id != ast::DUMMY_NODE_ID {
576611
debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
577612
self.node_to_def_index.insert(node_id, index);
613+
if let Some(hir_id) = self.node_to_hir_id.get(node_id) {
614+
self.hir_to_def_index.insert(*hir_id, index);
615+
}
578616
}
579617

580618
if expansion != Mark::root() {

src/librustc/hir/map/mod.rs

+87
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub const REGULAR_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;
4040
#[derive(Copy, Clone, Debug)]
4141
pub struct Entry<'hir> {
4242
parent: NodeId,
43+
parent_hir: HirId,
4344
dep_node: DepNodeIndex,
4445
node: Node<'hir>,
4546
}
@@ -208,6 +209,12 @@ impl<'hir> Map<'hir> {
208209
}
209210
}
210211

212+
// FIXME(@ljedrz): replace the NodeId variant
213+
pub fn read_by_hir_id(&self, hir_id: HirId) {
214+
let node_id = self.hir_to_node_id(hir_id);
215+
self.read(node_id);
216+
}
217+
211218
#[inline]
212219
pub fn definitions(&self) -> &'hir Definitions {
213220
self.definitions
@@ -224,6 +231,11 @@ impl<'hir> Map<'hir> {
224231
})
225232
}
226233

234+
// FIXME(@ljedrz): replace the NodeId variant
235+
pub fn def_path_from_hir_id(&self, id: HirId) -> DefPath {
236+
self.def_path(self.local_def_id_from_hir_id(id))
237+
}
238+
227239
pub fn def_path(&self, def_id: DefId) -> DefPath {
228240
assert!(def_id.is_local());
229241
self.definitions.def_path(def_id.index)
@@ -237,6 +249,22 @@ impl<'hir> Map<'hir> {
237249
})
238250
}
239251

252+
// FIXME(@ljedrz): replace the NodeId variant
253+
#[inline]
254+
pub fn local_def_id_from_hir_id(&self, hir_id: HirId) -> DefId {
255+
self.opt_local_def_id_from_hir_id(hir_id).unwrap_or_else(|| {
256+
let node_id = self.hir_to_node_id(hir_id);
257+
bug!("local_def_id_from_hir_id: no entry for `{:?}`, which has a map of `{:?}`",
258+
hir_id, self.find_entry(node_id))
259+
})
260+
}
261+
262+
// FIXME(@ljedrz): replace the NodeId variant
263+
#[inline]
264+
pub fn opt_local_def_id_from_hir_id(&self, hir_id: HirId) -> Option<DefId> {
265+
self.definitions.opt_local_def_id_from_hir_id(hir_id)
266+
}
267+
240268
#[inline]
241269
pub fn opt_local_def_id(&self, node: NodeId) -> Option<DefId> {
242270
self.definitions.opt_local_def_id(node)
@@ -247,6 +275,12 @@ impl<'hir> Map<'hir> {
247275
self.definitions.as_local_node_id(def_id)
248276
}
249277

278+
// FIXME(@ljedrz): replace the NodeId variant
279+
#[inline]
280+
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
281+
self.definitions.as_local_hir_id(def_id)
282+
}
283+
250284
#[inline]
251285
pub fn hir_to_node_id(&self, hir_id: HirId) -> NodeId {
252286
self.hir_to_node_id[&hir_id]
@@ -566,6 +600,12 @@ impl<'hir> Map<'hir> {
566600
self.find(id).unwrap_or_else(|| bug!("couldn't find node id {} in the AST map", id))
567601
}
568602

603+
// FIXME(@ljedrz): replace the NodeId variant
604+
pub fn get_by_hir_id(&self, id: HirId) -> Node<'hir> {
605+
let node_id = self.hir_to_node_id(id);
606+
self.get(node_id)
607+
}
608+
569609
pub fn get_if_local(&self, id: DefId) -> Option<Node<'hir>> {
570610
self.as_local_node_id(id).map(|id| self.get(id)) // read recorded by `get`
571611
}
@@ -613,6 +653,12 @@ impl<'hir> Map<'hir> {
613653
result
614654
}
615655

656+
// FIXME(@ljedrz): replace the NodeId variant
657+
pub fn find_by_hir_id(&self, hir_id: HirId) -> Option<Node<'hir>> {
658+
let node_id = self.hir_to_node_id(hir_id);
659+
self.find(node_id)
660+
}
661+
616662
/// Similar to `get_parent`; returns the parent node-id, or own `id` if there is
617663
/// no parent. Note that the parent may be `CRATE_NODE_ID`, which is not itself
618664
/// present in the map -- so passing the return value of get_parent_node to
@@ -633,6 +679,13 @@ impl<'hir> Map<'hir> {
633679
self.find_entry(id).and_then(|x| x.parent_node()).unwrap_or(id)
634680
}
635681

682+
// FIXME(@ljedrz): replace the NodeId variant
683+
pub fn get_parent_node_by_hir_id(&self, id: HirId) -> HirId {
684+
let node_id = self.hir_to_node_id(id);
685+
let parent_node_id = self.get_parent_node(node_id);
686+
self.node_to_hir_id(parent_node_id)
687+
}
688+
636689
/// Check if the node is an argument. An argument is a local variable whose
637690
/// immediate parent is an item or a closure.
638691
pub fn is_argument(&self, id: NodeId) -> bool {
@@ -757,6 +810,13 @@ impl<'hir> Map<'hir> {
757810
}
758811
}
759812

813+
// FIXME(@ljedrz): replace the NodeId variant
814+
pub fn get_parent_item(&self, id: HirId) -> HirId {
815+
let node_id = self.hir_to_node_id(id);
816+
let parent_node_id = self.get_parent(node_id);
817+
self.node_to_hir_id(parent_node_id)
818+
}
819+
760820
/// Returns the `DefId` of `id`'s nearest module parent, or `id` itself if no
761821
/// module parent is in this map.
762822
pub fn get_module_parent(&self, id: NodeId) -> DefId {
@@ -814,6 +874,12 @@ impl<'hir> Map<'hir> {
814874
}
815875
}
816876

877+
// FIXME(@ljedrz): replace the NodeId variant
878+
pub fn expect_item_by_hir_id(&self, id: HirId) -> &'hir Item {
879+
let node_id = self.hir_to_node_id(id);
880+
self.expect_item(node_id)
881+
}
882+
817883
pub fn expect_impl_item(&self, id: NodeId) -> &'hir ImplItem {
818884
match self.find(id) {
819885
Some(Node::ImplItem(item)) => item,
@@ -960,13 +1026,28 @@ impl<'hir> Map<'hir> {
9601026
node_id_to_string(self, id, true)
9611027
}
9621028

1029+
// FIXME(@ljedrz): replace the NodeId variant
1030+
pub fn hir_to_string(&self, id: HirId) -> String {
1031+
hir_id_to_string(self, id, true)
1032+
}
1033+
9631034
pub fn node_to_user_string(&self, id: NodeId) -> String {
9641035
node_id_to_string(self, id, false)
9651036
}
9661037

1038+
// FIXME(@ljedrz): replace the NodeId variant
1039+
pub fn hir_to_user_string(&self, id: HirId) -> String {
1040+
hir_id_to_string(self, id, false)
1041+
}
1042+
9671043
pub fn node_to_pretty_string(&self, id: NodeId) -> String {
9681044
print::to_string(self, |s| s.print_node(self.get(id)))
9691045
}
1046+
1047+
// FIXME(@ljedrz): replace the NodeId variant
1048+
pub fn hir_to_pretty_string(&self, id: HirId) -> String {
1049+
print::to_string(self, |s| s.print_node(self.get_by_hir_id(id)))
1050+
}
9701051
}
9711052

9721053
pub struct NodesMatchingSuffix<'a, 'hir:'a> {
@@ -1310,6 +1391,12 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
13101391
}
13111392
}
13121393

1394+
// FIXME(@ljedrz): replace the NodeId variant
1395+
fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
1396+
let node_id = map.hir_to_node_id(id);
1397+
node_id_to_string(map, node_id, include_id)
1398+
}
1399+
13131400
pub fn describe_def(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<Def> {
13141401
if let Some(node_id) = tcx.hir().as_local_node_id(def_id) {
13151402
tcx.hir().describe_def(node_id)

src/librustc/ty/item_path.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use hir;
12
use hir::map::DefPathData;
23
use hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
34
use ty::{self, DefIdTree, Ty, TyCtxt};
@@ -76,6 +77,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
7677
self.item_path_str(self.hir().local_def_id(id))
7778
}
7879

80+
// FIXME(@ljedrz): replace the NodeId variant
81+
pub fn hir_path_str(self, id: hir::HirId) -> String {
82+
self.item_path_str(self.hir().local_def_id_from_hir_id(id))
83+
}
84+
7985
/// Returns a string identifying this def-id. This string is
8086
/// suitable for user output. It always begins with a crate identifier.
8187
pub fn absolute_item_path_str(self, def_id: DefId) -> String {

0 commit comments

Comments
 (0)