Skip to content

Commit e8a8dfb

Browse files
committedApr 6, 2016
rustc: retire hir::map's paths.
1 parent 20f0f3c commit e8a8dfb

File tree

23 files changed

+192
-525
lines changed

23 files changed

+192
-525
lines changed
 

Diff for: ‎src/librustc/hir/map/collector.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use super::MapEntry::*;
1414
use hir::*;
1515
use hir::intravisit::Visitor;
1616
use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex};
17+
use middle::cstore::InlinedItem;
1718
use std::iter::repeat;
1819
use syntax::ast::{NodeId, CRATE_NODE_ID, DUMMY_NODE_ID};
1920
use syntax::codemap::Span;
@@ -46,7 +47,7 @@ impl<'ast> NodeCollector<'ast> {
4647
}
4748

4849
pub fn extend(krate: &'ast Crate,
49-
parent: &'ast InlinedParent,
50+
parent: &'ast InlinedItem,
5051
parent_node: NodeId,
5152
parent_def_path: DefPath,
5253
parent_def_id: DefId,

Diff for: ‎src/librustc/hir/map/mod.rs

+50-189
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
pub use self::Node::*;
12-
pub use self::PathElem::*;
1312
use self::MapEntry::*;
1413
use self::collector::NodeCollector;
1514
pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData,
@@ -25,93 +24,20 @@ use syntax::abi::Abi;
2524
use syntax::ast::{self, Name, NodeId, DUMMY_NODE_ID};
2625
use syntax::attr::ThinAttributesExt;
2726
use syntax::codemap::{Span, Spanned};
28-
use syntax::parse::token;
2927

3028
use hir::*;
3129
use hir::fold::Folder;
3230
use hir::print as pprust;
3331

3432
use arena::TypedArena;
3533
use std::cell::RefCell;
36-
use std::fmt;
3734
use std::io;
38-
use std::iter;
3935
use std::mem;
40-
use std::slice;
4136

4237
pub mod blocks;
4338
mod collector;
4439
pub mod definitions;
4540

46-
#[derive(Clone, Copy, PartialEq, Debug)]
47-
pub enum PathElem {
48-
PathMod(Name),
49-
PathName(Name)
50-
}
51-
52-
impl PathElem {
53-
pub fn name(&self) -> Name {
54-
match *self {
55-
PathMod(name) | PathName(name) => name
56-
}
57-
}
58-
}
59-
60-
impl fmt::Display for PathElem {
61-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62-
write!(f, "{}", self.name())
63-
}
64-
}
65-
66-
#[derive(Clone)]
67-
pub struct LinkedPathNode<'a> {
68-
node: PathElem,
69-
next: LinkedPath<'a>,
70-
}
71-
72-
#[derive(Copy, Clone)]
73-
pub struct LinkedPath<'a>(Option<&'a LinkedPathNode<'a>>);
74-
75-
impl<'a> LinkedPath<'a> {
76-
pub fn empty() -> LinkedPath<'a> {
77-
LinkedPath(None)
78-
}
79-
80-
pub fn from(node: &'a LinkedPathNode) -> LinkedPath<'a> {
81-
LinkedPath(Some(node))
82-
}
83-
}
84-
85-
impl<'a> Iterator for LinkedPath<'a> {
86-
type Item = PathElem;
87-
88-
fn next(&mut self) -> Option<PathElem> {
89-
match self.0 {
90-
Some(node) => {
91-
*self = node.next;
92-
Some(node.node)
93-
}
94-
None => None
95-
}
96-
}
97-
}
98-
99-
/// The type of the iterator used by with_path.
100-
pub type PathElems<'a, 'b> = iter::Chain<iter::Cloned<slice::Iter<'a, PathElem>>, LinkedPath<'b>>;
101-
102-
pub fn path_to_string<PI: Iterator<Item=PathElem>>(path: PI) -> String {
103-
let itr = token::get_ident_interner();
104-
105-
path.fold(String::new(), |mut s, e| {
106-
let e = itr.get(e.name());
107-
if !s.is_empty() {
108-
s.push_str("::");
109-
}
110-
s.push_str(&e[..]);
111-
s
112-
})
113-
}
114-
11541
#[derive(Copy, Clone, Debug)]
11642
pub enum Node<'ast> {
11743
NodeItem(&'ast Item),
@@ -156,7 +82,7 @@ pub enum MapEntry<'ast> {
15682

15783
/// Roots for node trees.
15884
RootCrate,
159-
RootInlinedParent(&'ast InlinedParent)
85+
RootInlinedParent(&'ast InlinedItem)
16086
}
16187

16288
impl<'ast> Clone for MapEntry<'ast> {
@@ -165,12 +91,6 @@ impl<'ast> Clone for MapEntry<'ast> {
16591
}
16692
}
16793

168-
#[derive(Debug)]
169-
pub struct InlinedParent {
170-
path: Vec<PathElem>,
171-
ii: InlinedItem
172-
}
173-
17494
impl<'ast> MapEntry<'ast> {
17595
fn from_node(p: NodeId, node: Node<'ast>) -> MapEntry<'ast> {
17696
match node {
@@ -233,7 +153,7 @@ impl<'ast> MapEntry<'ast> {
233153
pub struct Forest {
234154
krate: Crate,
235155
pub dep_graph: DepGraph,
236-
inlined_items: TypedArena<InlinedParent>
156+
inlined_items: TypedArena<InlinedItem>
237157
}
238158

239159
impl Forest {
@@ -351,8 +271,10 @@ impl<'ast> Map<'ast> {
351271
self.definitions.borrow().def_key(def_id.index)
352272
}
353273

354-
pub fn def_path_from_id(&self, id: NodeId) -> DefPath {
355-
self.def_path(self.local_def_id(id))
274+
pub fn def_path_from_id(&self, id: NodeId) -> Option<DefPath> {
275+
self.opt_local_def_id(id).map(|def_id| {
276+
self.def_path(def_id)
277+
})
356278
}
357279

358280
pub fn def_path(&self, def_id: DefId) -> DefPath {
@@ -551,8 +473,8 @@ impl<'ast> Map<'ast> {
551473
pub fn get_parent_did(&self, id: NodeId) -> DefId {
552474
let parent = self.get_parent(id);
553475
match self.find_entry(parent) {
554-
Some(RootInlinedParent(&InlinedParent {ii: II::TraitItem(did, _), ..})) => did,
555-
Some(RootInlinedParent(&InlinedParent {ii: II::ImplItem(did, _), ..})) => did,
476+
Some(RootInlinedParent(&II::TraitItem(did, _))) |
477+
Some(RootInlinedParent(&II::ImplItem(did, _))) => did,
556478
_ => self.local_def_id(parent)
557479
}
558480
}
@@ -634,80 +556,21 @@ impl<'ast> Map<'ast> {
634556
}
635557
}
636558

637-
/// returns the name associated with the given NodeId's AST
638-
pub fn get_path_elem(&self, id: NodeId) -> PathElem {
639-
let node = self.get(id);
640-
match node {
641-
NodeItem(item) => {
642-
match item.node {
643-
ItemMod(_) | ItemForeignMod(_) => {
644-
PathMod(item.name)
645-
}
646-
_ => PathName(item.name)
647-
}
648-
}
649-
NodeForeignItem(i) => PathName(i.name),
650-
NodeImplItem(ii) => PathName(ii.name),
651-
NodeTraitItem(ti) => PathName(ti.name),
652-
NodeVariant(v) => PathName(v.node.name),
653-
NodeLifetime(lt) => PathName(lt.name),
654-
NodeTyParam(tp) => PathName(tp.name),
559+
/// Returns the name associated with the given NodeId's AST.
560+
pub fn name(&self, id: NodeId) -> Name {
561+
match self.get(id) {
562+
NodeItem(i) => i.name,
563+
NodeForeignItem(i) => i.name,
564+
NodeImplItem(ii) => ii.name,
565+
NodeTraitItem(ti) => ti.name,
566+
NodeVariant(v) => v.node.name,
567+
NodeLifetime(lt) => lt.name,
568+
NodeTyParam(tp) => tp.name,
655569
NodeLocal(&Pat { node: PatKind::Ident(_,l,_), .. }) => {
656-
PathName(l.node.name)
570+
l.node.name
657571
},
658-
_ => bug!("no path elem for {:?}", node)
659-
}
660-
}
661-
662-
pub fn with_path<T, F>(&self, id: NodeId, f: F) -> T where
663-
F: FnOnce(PathElems) -> T,
664-
{
665-
self.with_path_next(id, LinkedPath::empty(), f)
666-
}
667-
668-
pub fn path_to_string(&self, id: NodeId) -> String {
669-
self.with_path(id, |path| path_to_string(path))
670-
}
671-
672-
fn path_to_str_with_name(&self, id: NodeId, name: Name) -> String {
673-
self.with_path(id, |path| {
674-
path_to_string(path.chain(Some(PathName(name))))
675-
})
676-
}
677-
678-
fn with_path_next<T, F>(&self, id: NodeId, next: LinkedPath, f: F) -> T where
679-
F: FnOnce(PathElems) -> T,
680-
{
681-
// This function reveals the name of the item and hence is a
682-
// kind of read. This is inefficient, since it walks ancestors
683-
// and we are walking them anyhow, but whatever.
684-
self.read(id);
685-
686-
let parent = self.get_parent(id);
687-
let parent = match self.find_entry(id) {
688-
Some(EntryForeignItem(..)) => {
689-
// Anonymous extern items go in the parent scope.
690-
self.get_parent(parent)
691-
}
692-
// But tuple struct ctors don't have names, so use the path of its
693-
// parent, the struct item. Similarly with closure expressions.
694-
Some(EntryStructCtor(..)) | Some(EntryExpr(..)) => {
695-
return self.with_path_next(parent, next, f);
696-
}
697-
_ => parent
698-
};
699-
if parent == id {
700-
match self.find_entry(id) {
701-
Some(RootInlinedParent(data)) => {
702-
f(data.path.iter().cloned().chain(next))
703-
}
704-
_ => f([].iter().cloned().chain(next))
705-
}
706-
} else {
707-
self.with_path_next(parent, LinkedPath::from(&LinkedPathNode {
708-
node: self.get_path_elem(id),
709-
next: next
710-
}), f)
572+
NodeStructCtor(_) => self.name(self.get_parent(id)),
573+
_ => bug!("no name for {}", self.node_to_string(id))
711574
}
712575
}
713576

@@ -958,7 +821,6 @@ pub fn map_crate<'ast>(forest: &'ast mut Forest) -> Map<'ast> {
958821
/// Used for items loaded from external crate that are being inlined into this
959822
/// crate.
960823
pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
961-
parent_path: Vec<PathElem>,
962824
parent_def_path: DefPath,
963825
parent_def_id: DefId,
964826
ii: InlinedItem,
@@ -978,27 +840,24 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
978840
II::Foreign(i) => II::Foreign(i.map(|i| fld.fold_foreign_item(i)))
979841
};
980842

981-
let ii_parent = map.forest.inlined_items.alloc(InlinedParent {
982-
path: parent_path,
983-
ii: ii
984-
});
843+
let ii = map.forest.inlined_items.alloc(ii);
985844

986845
let ii_parent_id = fld.new_id(DUMMY_NODE_ID);
987846
let mut collector =
988847
NodeCollector::extend(
989848
map.krate(),
990-
ii_parent,
849+
ii,
991850
ii_parent_id,
992851
parent_def_path,
993852
parent_def_id,
994853
mem::replace(&mut *map.map.borrow_mut(), vec![]),
995854
mem::replace(&mut *map.definitions.borrow_mut(), Definitions::new()));
996-
ii_parent.ii.visit(&mut collector);
855+
ii.visit(&mut collector);
997856

998857
*map.map.borrow_mut() = collector.map;
999858
*map.definitions.borrow_mut() = collector.definitions;
1000859

1001-
&ii_parent.ii
860+
ii
1002861
}
1003862

1004863
pub trait NodePrinter {
@@ -1032,9 +891,24 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1032891
let id_str = format!(" (id={})", id);
1033892
let id_str = if include_id { &id_str[..] } else { "" };
1034893

894+
let path_str = || {
895+
// This functionality is used for debugging, try to use TyCtxt to get
896+
// the user-friendly path, otherwise fall back to stringifying DefPath.
897+
::ty::tls::with_opt(|tcx| {
898+
if let Some(tcx) = tcx {
899+
tcx.node_path_str(id)
900+
} else if let Some(path) = map.def_path_from_id(id) {
901+
path.data.into_iter().map(|elem| {
902+
elem.data.to_string()
903+
}).collect::<Vec<_>>().join("::")
904+
} else {
905+
String::from("<missing path>")
906+
}
907+
})
908+
};
909+
1035910
match map.find(id) {
1036911
Some(NodeItem(item)) => {
1037-
let path_str = map.path_to_str_with_name(id, item.name);
1038912
let item_str = match item.node {
1039913
ItemExternCrate(..) => "extern crate",
1040914
ItemUse(..) => "use",
@@ -1050,30 +924,21 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1050924
ItemImpl(..) => "impl",
1051925
ItemDefaultImpl(..) => "default impl",
1052926
};
1053-
format!("{} {}{}", item_str, path_str, id_str)
927+
format!("{} {}{}", item_str, path_str(), id_str)
1054928
}
1055-
Some(NodeForeignItem(item)) => {
1056-
let path_str = map.path_to_str_with_name(id, item.name);
1057-
format!("foreign item {}{}", path_str, id_str)
929+
Some(NodeForeignItem(_)) => {
930+
format!("foreign item {}{}", path_str(), id_str)
1058931
}
1059932
Some(NodeImplItem(ii)) => {
1060933
match ii.node {
1061934
ImplItemKind::Const(..) => {
1062-
format!("assoc const {} in {}{}",
1063-
ii.name,
1064-
map.path_to_string(id),
1065-
id_str)
935+
format!("assoc const {} in {}{}", ii.name, path_str(), id_str)
1066936
}
1067937
ImplItemKind::Method(..) => {
1068-
format!("method {} in {}{}",
1069-
ii.name,
1070-
map.path_to_string(id), id_str)
938+
format!("method {} in {}{}", ii.name, path_str(), id_str)
1071939
}
1072940
ImplItemKind::Type(_) => {
1073-
format!("assoc type {} in {}{}",
1074-
ii.name,
1075-
map.path_to_string(id),
1076-
id_str)
941+
format!("assoc type {} in {}{}", ii.name, path_str(), id_str)
1077942
}
1078943
}
1079944
}
@@ -1084,16 +949,12 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1084949
TypeTraitItem(..) => "assoc type",
1085950
};
1086951

1087-
format!("{} {} in {}{}",
1088-
kind,
1089-
ti.name,
1090-
map.path_to_string(id),
1091-
id_str)
952+
format!("{} {} in {}{}", kind, ti.name, path_str(), id_str)
1092953
}
1093954
Some(NodeVariant(ref variant)) => {
1094955
format!("variant {} in {}{}",
1095956
variant.node.name,
1096-
map.path_to_string(id), id_str)
957+
path_str(), id_str)
1097958
}
1098959
Some(NodeExpr(ref expr)) => {
1099960
format!("expr {}{}", pprust::expr_to_string(&expr), id_str)
@@ -1111,7 +972,7 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1111972
format!("block {}{}", pprust::block_to_string(&block), id_str)
1112973
}
1113974
Some(NodeStructCtor(_)) => {
1114-
format!("struct_ctor {}{}", map.path_to_string(id), id_str)
975+
format!("struct_ctor {}{}", path_str(), id_str)
1115976
}
1116977
Some(NodeLifetime(ref l)) => {
1117978
format!("lifetime {}{}",

0 commit comments

Comments
 (0)
Please sign in to comment.