]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/def_id.rs
No need to have tcx::opt_def_path() now that we store all DefPaths
[rust.git] / src / librustc / hir / def_id.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ty;
12
13 use rustc_data_structures::indexed_vec::Idx;
14 use serialize::{self, Encoder, Decoder};
15
16 use std::fmt;
17 use std::u32;
18
19 #[derive(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, 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 /// Virtual crate for builtin macros
38 // FIXME(jseyfried): this is also used for custom derives until proc-macro crates get `CrateNum`s.
39 pub const BUILTIN_MACROS_CRATE: CrateNum = CrateNum(!0);
40
41 impl CrateNum {
42     pub fn new(x: usize) -> CrateNum {
43         assert!(x < (u32::MAX as usize));
44         CrateNum(x as u32)
45     }
46
47     pub fn from_u32(x: u32) -> CrateNum {
48         CrateNum(x)
49     }
50
51     pub fn as_usize(&self) -> usize {
52         self.0 as usize
53     }
54
55     pub fn as_u32(&self) -> u32 {
56         self.0
57     }
58 }
59
60 impl fmt::Display for CrateNum {
61     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62         fmt::Display::fmt(&self.0, f)
63     }
64 }
65
66 impl serialize::UseSpecializedEncodable for CrateNum {
67     fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
68         s.emit_u32(self.0)
69     }
70 }
71
72 impl serialize::UseSpecializedDecodable for CrateNum {
73     fn default_decode<D: Decoder>(d: &mut D) -> Result<CrateNum, D::Error> {
74         d.read_u32().map(CrateNum)
75     }
76 }
77
78 /// A DefIndex is an index into the hir-map for a crate, identifying a
79 /// particular definition. It should really be considered an interned
80 /// shorthand for a particular DefPath.
81 #[derive(Clone, Debug, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
82            RustcDecodable, Hash, Copy)]
83 pub struct DefIndex(u32);
84
85 impl DefIndex {
86     pub fn new(x: usize) -> DefIndex {
87         assert!(x < (u32::MAX as usize));
88         DefIndex(x as u32)
89     }
90
91     pub fn from_u32(x: u32) -> DefIndex {
92         DefIndex(x)
93     }
94
95     pub fn as_usize(&self) -> usize {
96         self.0 as usize
97     }
98
99     pub fn as_u32(&self) -> u32 {
100         self.0
101     }
102 }
103
104 /// The crate root is always assigned index 0 by the AST Map code,
105 /// thanks to `NodeCollector::new`.
106 pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);
107
108 /// A DefId identifies a particular *definition*, by combining a crate
109 /// index and a def index.
110 #[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, RustcDecodable, Hash, Copy)]
111 pub struct DefId {
112     pub krate: CrateNum,
113     pub index: DefIndex,
114 }
115
116 impl fmt::Debug for DefId {
117     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
118         write!(f, "DefId {{ krate: {:?}, node: {:?}",
119                self.krate, self.index)?;
120
121         ty::tls::with_opt(|opt_tcx| {
122             if let Some(tcx) = opt_tcx {
123                 write!(f, " => {}", tcx.def_path(*self).to_string(tcx))?;
124             }
125             Ok(())
126         })?;
127
128         write!(f, " }}")
129     }
130 }
131
132
133 impl DefId {
134     pub fn local(index: DefIndex) -> DefId {
135         DefId { krate: LOCAL_CRATE, index: index }
136     }
137
138     pub fn is_local(&self) -> bool {
139         self.krate == LOCAL_CRATE
140     }
141 }