Skip to content

Commit fc363cb

Browse files
committed
rustc_metadata: go only through rustc_serialize in astencode.
1 parent 91e7239 commit fc363cb

File tree

70 files changed

+606
-1213
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+606
-1213
lines changed

src/librustc/hir/def_id.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,59 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use middle::cstore::LOCAL_CRATE;
1211
use ty;
13-
use syntax::ast::CrateNum;
12+
13+
use rustc_data_structures::indexed_vec::Idx;
14+
use serialize;
15+
1416
use std::fmt;
1517
use std::u32;
1618

19+
#[derive(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, Hash, Debug)]
20+
pub struct CrateNum(u32);
21+
22+
impl Idx for CrateNum {
23+
fn new(value: usize) -> Self {
24+
assert!(value < (u32::MAX) as usize);
25+
CrateNum(value as u32)
26+
}
27+
28+
fn index(self) -> usize {
29+
self.0 as usize
30+
}
31+
}
32+
33+
/// Item definitions in the currently-compiled crate would have the CrateNum
34+
/// LOCAL_CRATE in their DefId.
35+
pub const LOCAL_CRATE: CrateNum = CrateNum(0);
36+
37+
impl CrateNum {
38+
pub fn new(x: usize) -> CrateNum {
39+
assert!(x < (u32::MAX as usize));
40+
CrateNum(x as u32)
41+
}
42+
43+
pub fn from_u32(x: u32) -> CrateNum {
44+
CrateNum(x)
45+
}
46+
47+
pub fn as_usize(&self) -> usize {
48+
self.0 as usize
49+
}
50+
51+
pub fn as_u32(&self) -> u32 {
52+
self.0
53+
}
54+
}
55+
56+
impl fmt::Display for CrateNum {
57+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58+
fmt::Display::fmt(&self.0, f)
59+
}
60+
}
61+
62+
impl serialize::UseSpecializedDecodable for CrateNum {}
63+
1764
/// A DefIndex is an index into the hir-map for a crate, identifying a
1865
/// particular definition. It should really be considered an interned
1966
/// shorthand for a particular DefPath.
@@ -46,8 +93,7 @@ pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);
4693

4794
/// A DefId identifies a particular *definition*, by combining a crate
4895
/// index and a def index.
49-
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
50-
RustcDecodable, Hash, Copy)]
96+
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, RustcDecodable, Hash, Copy)]
5197
pub struct DefId {
5298
pub krate: CrateNum,
5399
pub index: DefIndex,

src/librustc/hir/intravisit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,8 @@ pub struct IdRange {
881881
impl IdRange {
882882
pub fn max() -> IdRange {
883883
IdRange {
884-
min: u32::MAX,
885-
max: u32::MIN,
884+
min: NodeId::from_u32(u32::MAX),
885+
max: NodeId::from_u32(u32::MIN),
886886
}
887887
}
888888

@@ -896,7 +896,7 @@ impl IdRange {
896896

897897
pub fn add(&mut self, id: NodeId) {
898898
self.min = cmp::min(self.min, id);
899-
self.max = cmp::max(self.max, id + 1);
899+
self.max = cmp::max(self.max, NodeId::from_u32(id.as_u32() + 1));
900900
}
901901

902902
}

src/librustc/hir/lowering.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use syntax_pos::Span;
6161
pub struct LoweringContext<'a> {
6262
crate_root: Option<&'static str>,
6363
// Use to assign ids to hir nodes that do not directly correspond to an ast node
64-
sess: Option<&'a Session>,
64+
sess: &'a Session,
6565
// As we walk the AST we must keep track of the current 'parent' def id (in
6666
// the form of a DefIndex) so that if we create a new node which introduces
6767
// a definition, then we can properly create the def id.
@@ -101,22 +101,13 @@ pub fn lower_crate(sess: &Session,
101101
} else {
102102
Some("std")
103103
},
104-
sess: Some(sess),
104+
sess: sess,
105105
parent_def: None,
106106
resolver: resolver,
107107
}.lower_crate(krate)
108108
}
109109

110110
impl<'a> LoweringContext<'a> {
111-
pub fn testing_context(resolver: &'a mut Resolver) -> Self {
112-
LoweringContext {
113-
crate_root: None,
114-
sess: None,
115-
parent_def: None,
116-
resolver: resolver,
117-
}
118-
}
119-
120111
fn lower_crate(&mut self, c: &Crate) -> hir::Crate {
121112
struct ItemLowerer<'lcx, 'interner: 'lcx> {
122113
items: BTreeMap<NodeId, hir::Item>,
@@ -147,12 +138,11 @@ impl<'a> LoweringContext<'a> {
147138
}
148139

149140
fn next_id(&self) -> NodeId {
150-
self.sess.map(Session::next_node_id).unwrap_or(0)
141+
self.sess.next_node_id()
151142
}
152143

153144
fn diagnostic(&self) -> &errors::Handler {
154-
self.sess.map(Session::diagnostic)
155-
.unwrap_or_else(|| panic!("this lowerer cannot emit diagnostics"))
145+
self.sess.diagnostic()
156146
}
157147

158148
fn str_to_ident(&self, s: &'static str) -> Name {

src/librustc/hir/map/collector.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ impl<'ast> NodeCollector<'ast> {
6363
fn insert_entry(&mut self, id: NodeId, entry: MapEntry<'ast>) {
6464
debug!("ast_map: {:?} => {:?}", id, entry);
6565
let len = self.map.len();
66-
if id as usize >= len {
67-
self.map.extend(repeat(NotPresent).take(id as usize - len + 1));
66+
if id.as_usize() >= len {
67+
self.map.extend(repeat(NotPresent).take(id.as_usize() - len + 1));
6868
}
69-
self.map[id as usize] = entry;
69+
self.map[id.as_usize()] = entry;
7070
}
7171

7272
fn insert(&mut self, id: NodeId, node: Node<'ast>) {

src/librustc/hir/map/definitions.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use middle::cstore::LOCAL_CRATE;
12-
use hir::def_id::{DefId, DefIndex};
11+
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
1312
use hir::map::def_collector::DefCollector;
1413
use rustc_data_structures::fnv::FnvHashMap;
1514
use std::fmt::Write;
@@ -70,15 +69,15 @@ pub struct DefPath {
7069
pub data: Vec<DisambiguatedDefPathData>,
7170

7271
/// what krate root is this path relative to?
73-
pub krate: ast::CrateNum,
72+
pub krate: CrateNum,
7473
}
7574

7675
impl DefPath {
7776
pub fn is_local(&self) -> bool {
7877
self.krate == LOCAL_CRATE
7978
}
8079

81-
pub fn make<FN>(start_krate: ast::CrateNum,
80+
pub fn make<FN>(start_krate: CrateNum,
8281
start_index: DefIndex,
8382
mut get_key: FN) -> DefPath
8483
where FN: FnMut(DefIndex) -> DefKey

src/librustc/hir/map/mod.rs

Lines changed: 16 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ use middle::cstore::InlinedItem as II;
2222
use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex};
2323

2424
use syntax::abi::Abi;
25-
use syntax::ast::{self, Name, NodeId, DUMMY_NODE_ID, };
25+
use syntax::ast::{self, Name, NodeId, CRATE_NODE_ID};
2626
use syntax::codemap::Spanned;
2727
use syntax_pos::Span;
2828

2929
use hir::*;
30-
use hir::fold::Folder;
3130
use hir::print as pprust;
3231

3332
use arena::TypedArena;
3433
use std::cell::RefCell;
35-
use std::cmp;
3634
use std::io;
3735
use std::mem;
3836

@@ -240,7 +238,7 @@ impl<'ast> Map<'ast> {
240238
let mut id = id0;
241239
if !self.is_inlined_node_id(id) {
242240
loop {
243-
match map[id as usize] {
241+
match map[id.as_usize()] {
244242
EntryItem(_, item) => {
245243
let def_id = self.local_def_id(item.id);
246244
// NB ^~~~~~~
@@ -295,7 +293,7 @@ impl<'ast> Map<'ast> {
295293
// reading from an inlined def-id is really a read out of
296294
// the metadata from which we loaded the item.
297295
loop {
298-
match map[id as usize] {
296+
match map[id.as_usize()] {
299297
EntryItem(p, _) |
300298
EntryForeignItem(p, _) |
301299
EntryTraitItem(p, _) |
@@ -373,7 +371,7 @@ impl<'ast> Map<'ast> {
373371
}
374372

375373
fn find_entry(&self, id: NodeId) -> Option<MapEntry<'ast>> {
376-
self.map.borrow().get(id as usize).cloned()
374+
self.map.borrow().get(id.as_usize()).cloned()
377375
}
378376

379377
pub fn krate(&self) -> &'ast Crate {
@@ -456,8 +454,8 @@ impl<'ast> Map<'ast> {
456454
let mut id = start_id;
457455
loop {
458456
let parent_node = self.get_parent_node(id);
459-
if parent_node == 0 {
460-
return Ok(0);
457+
if parent_node == CRATE_NODE_ID {
458+
return Ok(CRATE_NODE_ID);
461459
}
462460
if parent_node == id {
463461
return Err(id);
@@ -680,7 +678,7 @@ impl<'ast> Map<'ast> {
680678
map: self,
681679
item_name: parts.last().unwrap(),
682680
in_which: &parts[..parts.len() - 1],
683-
idx: 0,
681+
idx: CRATE_NODE_ID,
684682
}
685683
}
686684

@@ -801,10 +799,10 @@ impl<'a, 'ast> Iterator for NodesMatchingSuffix<'a, 'ast> {
801799
fn next(&mut self) -> Option<NodeId> {
802800
loop {
803801
let idx = self.idx;
804-
if idx as usize >= self.map.entry_count() {
802+
if idx.as_usize() >= self.map.entry_count() {
805803
return None;
806804
}
807-
self.idx += 1;
805+
self.idx = NodeId::from_u32(self.idx.as_u32() + 1);
808806
let name = match self.map.find_entry(idx) {
809807
Some(EntryItem(_, n)) => n.name(),
810808
Some(EntryForeignItem(_, n))=> n.name(),
@@ -832,57 +830,6 @@ impl Named for Variant_ { fn name(&self) -> Name { self.name } }
832830
impl Named for TraitItem { fn name(&self) -> Name { self.name } }
833831
impl Named for ImplItem { fn name(&self) -> Name { self.name } }
834832

835-
pub trait FoldOps {
836-
fn new_id(&self, id: NodeId) -> NodeId {
837-
id
838-
}
839-
fn new_def_id(&self, def_id: DefId) -> DefId {
840-
def_id
841-
}
842-
fn new_span(&self, span: Span) -> Span {
843-
span
844-
}
845-
}
846-
847-
/// A Folder that updates IDs and Span's according to fold_ops.
848-
pub struct IdAndSpanUpdater<F> {
849-
fold_ops: F,
850-
min_id_assigned: NodeId,
851-
max_id_assigned: NodeId,
852-
}
853-
854-
impl<F: FoldOps> IdAndSpanUpdater<F> {
855-
pub fn new(fold_ops: F) -> IdAndSpanUpdater<F> {
856-
IdAndSpanUpdater {
857-
fold_ops: fold_ops,
858-
min_id_assigned: ::std::u32::MAX,
859-
max_id_assigned: ::std::u32::MIN,
860-
}
861-
}
862-
863-
pub fn id_range(&self) -> intravisit::IdRange {
864-
intravisit::IdRange {
865-
min: self.min_id_assigned,
866-
max: self.max_id_assigned + 1,
867-
}
868-
}
869-
}
870-
871-
impl<F: FoldOps> Folder for IdAndSpanUpdater<F> {
872-
fn new_id(&mut self, id: NodeId) -> NodeId {
873-
let id = self.fold_ops.new_id(id);
874-
875-
self.min_id_assigned = cmp::min(self.min_id_assigned, id);
876-
self.max_id_assigned = cmp::max(self.max_id_assigned, id);
877-
878-
id
879-
}
880-
881-
fn new_span(&mut self, span: Span) -> Span {
882-
self.fold_ops.new_span(span)
883-
}
884-
}
885-
886833
pub fn map_crate<'ast>(forest: &'ast mut Forest,
887834
definitions: Definitions)
888835
-> Map<'ast> {
@@ -906,7 +853,7 @@ pub fn map_crate<'ast>(forest: &'ast mut Forest,
906853
entries, vector_length, (entries as f64 / vector_length as f64) * 100.);
907854
}
908855

909-
let local_node_id_watermark = map.len() as NodeId;
856+
let local_node_id_watermark = NodeId::new(map.len());
910857
let local_def_id_watermark = definitions.len();
911858

912859
Map {
@@ -921,36 +868,15 @@ pub fn map_crate<'ast>(forest: &'ast mut Forest,
921868

922869
/// Used for items loaded from external crate that are being inlined into this
923870
/// crate.
924-
pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
925-
parent_def_path: DefPath,
926-
parent_def_id: DefId,
927-
ii: InlinedItem,
928-
fold_ops: F)
929-
-> &'ast InlinedItem {
871+
pub fn map_decoded_item<'ast>(map: &Map<'ast>,
872+
parent_def_path: DefPath,
873+
parent_def_id: DefId,
874+
ii: InlinedItem,
875+
ii_parent_id: NodeId)
876+
-> &'ast InlinedItem {
930877
let _ignore = map.forest.dep_graph.in_ignore();
931878

932-
let mut fld = IdAndSpanUpdater::new(fold_ops);
933-
let ii = match ii {
934-
II::Item(d, i) => II::Item(fld.fold_ops.new_def_id(d),
935-
i.map(|i| fld.fold_item(i))),
936-
II::TraitItem(d, ti) => {
937-
II::TraitItem(fld.fold_ops.new_def_id(d),
938-
ti.map(|ti| fld.fold_trait_item(ti)))
939-
}
940-
II::ImplItem(d, ii) => {
941-
II::ImplItem(fld.fold_ops.new_def_id(d),
942-
ii.map(|ii| fld.fold_impl_item(ii)))
943-
}
944-
};
945-
946879
let ii = map.forest.inlined_items.alloc(ii);
947-
let ii_parent_id = fld.new_id(DUMMY_NODE_ID);
948-
949-
// Assert that the ii_parent_id is the last NodeId in our reserved range
950-
assert!(ii_parent_id == fld.max_id_assigned);
951-
// Assert that we did not violate the invariant that all inlined HIR items
952-
// have NodeIds greater than or equal to `local_node_id_watermark`
953-
assert!(fld.min_id_assigned >= map.local_node_id_watermark);
954880

955881
let defs = &mut *map.definitions.borrow_mut();
956882
let mut def_collector = DefCollector::extend(ii_parent_id,

src/librustc/infer/region_inference/graphviz.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ pub fn maybe_print_constraints_for<'a, 'gcx, 'tcx>(
6363
return;
6464
}
6565

66-
let requested_node: Option<ast::NodeId> = env::var("RUST_REGION_GRAPH_NODE")
67-
.ok()
68-
.and_then(|s| s.parse().ok());
66+
let requested_node = env::var("RUST_REGION_GRAPH_NODE")
67+
.ok().and_then(|s| s.parse().map(ast::NodeId::new).ok());
6968

7069
if requested_node.is_some() && requested_node != Some(subject_node) {
7170
return;

0 commit comments

Comments
 (0)