]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/def_id.rs
Rollup merge of #42496 - Razaekel:feature/integer_max-min, r=BurntSushi
[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(u32::MAX);
40
41 /// A CrateNum value that indicates that something is wrong.
42 pub const INVALID_CRATE: CrateNum = CrateNum(u32::MAX - 1);
43
44 impl CrateNum {
45     pub fn new(x: usize) -> CrateNum {
46         assert!(x < (u32::MAX as usize));
47         CrateNum(x as u32)
48     }
49
50     pub fn from_u32(x: u32) -> CrateNum {
51         CrateNum(x)
52     }
53
54     pub fn as_usize(&self) -> usize {
55         self.0 as usize
56     }
57
58     pub fn as_u32(&self) -> u32 {
59         self.0
60     }
61 }
62
63 impl fmt::Display for CrateNum {
64     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65         fmt::Display::fmt(&self.0, f)
66     }
67 }
68
69 impl serialize::UseSpecializedEncodable for CrateNum {
70     fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
71         s.emit_u32(self.0)
72     }
73 }
74
75 impl serialize::UseSpecializedDecodable for CrateNum {
76     fn default_decode<D: Decoder>(d: &mut D) -> Result<CrateNum, D::Error> {
77         d.read_u32().map(CrateNum)
78     }
79 }
80
81 /// A DefIndex is an index into the hir-map for a crate, identifying a
82 /// particular definition. It should really be considered an interned
83 /// shorthand for a particular DefPath.
84 ///
85 /// At the moment we are allocating the numerical values of DefIndexes into two
86 /// ranges: the "low" range (starting at zero) and the "high" range (starting at
87 /// DEF_INDEX_HI_START). This allows us to allocate the DefIndexes of all
88 /// item-likes (Items, TraitItems, and ImplItems) into one of these ranges and
89 /// consequently use a simple array for lookup tables keyed by DefIndex and
90 /// known to be densely populated. This is especially important for the HIR map.
91 ///
92 /// Since the DefIndex is mostly treated as an opaque ID, you probably
93 /// don't have to care about these ranges.
94 #[derive(Clone, Debug, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
95            RustcDecodable, Hash, Copy)]
96 pub struct DefIndex(u32);
97
98 impl DefIndex {
99     #[inline]
100     pub fn new(x: usize) -> DefIndex {
101         assert!(x < (u32::MAX as usize));
102         DefIndex(x as u32)
103     }
104
105     #[inline]
106     pub fn from_u32(x: u32) -> DefIndex {
107         DefIndex(x)
108     }
109
110     #[inline]
111     pub fn as_usize(&self) -> usize {
112         self.0 as usize
113     }
114
115     #[inline]
116     pub fn as_u32(&self) -> u32 {
117         self.0
118     }
119
120     #[inline]
121     pub fn address_space(&self) -> DefIndexAddressSpace {
122         if self.0 < DEF_INDEX_HI_START.0 {
123             DefIndexAddressSpace::Low
124         } else {
125             DefIndexAddressSpace::High
126         }
127     }
128
129     /// Converts this DefIndex into a zero-based array index.
130     /// This index is the offset within the given "range" of the DefIndex,
131     /// that is, if the DefIndex is part of the "high" range, the resulting
132     /// index will be (DefIndex - DEF_INDEX_HI_START).
133     #[inline]
134     pub fn as_array_index(&self) -> usize {
135         (self.0 & !DEF_INDEX_HI_START.0) as usize
136     }
137 }
138
139 /// The start of the "high" range of DefIndexes.
140 const DEF_INDEX_HI_START: DefIndex = DefIndex(1 << 31);
141
142 /// The crate root is always assigned index 0 by the AST Map code,
143 /// thanks to `NodeCollector::new`.
144 pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);
145
146 #[derive(Copy, Clone, Eq, PartialEq, Hash)]
147 pub enum DefIndexAddressSpace {
148     Low = 0,
149     High = 1,
150 }
151
152 impl DefIndexAddressSpace {
153     #[inline]
154     pub fn index(&self) -> usize {
155         *self as usize
156     }
157
158     #[inline]
159     pub fn start(&self) -> usize {
160         self.index() * DEF_INDEX_HI_START.as_usize()
161     }
162 }
163
164 /// A DefId identifies a particular *definition*, by combining a crate
165 /// index and a def index.
166 #[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, RustcDecodable, Hash, Copy)]
167 pub struct DefId {
168     pub krate: CrateNum,
169     pub index: DefIndex,
170 }
171
172 impl fmt::Debug for DefId {
173     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
174         write!(f, "DefId {{ krate: {:?}, node: {:?}",
175                self.krate, self.index)?;
176
177         ty::tls::with_opt(|opt_tcx| {
178             if let Some(tcx) = opt_tcx {
179                 write!(f, " => {}", tcx.def_path(*self).to_string(tcx))?;
180             }
181             Ok(())
182         })?;
183
184         write!(f, " }}")
185     }
186 }
187
188
189 impl DefId {
190     /// Make a local `DefId` with the given index.
191     pub fn local(index: DefIndex) -> DefId {
192         DefId { krate: LOCAL_CRATE, index: index }
193     }
194
195     pub fn is_local(&self) -> bool {
196         self.krate == LOCAL_CRATE
197     }
198 }