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.
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.
13 use rustc_data_structures
::indexed_vec
::Idx
;
14 use serialize
::{self, Encoder, Decoder}
;
19 #[derive(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Hash, Debug)]
20 pub struct CrateNum(u32);
22 impl Idx
for CrateNum
{
23 fn new(value
: usize) -> Self {
24 assert
!(value
< (u32::MAX
) as usize);
25 CrateNum(value
as u32)
28 fn index(self) -> usize {
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);
38 pub fn new(x
: usize) -> CrateNum
{
39 assert
!(x
< (u32::MAX
as usize));
43 pub fn from_u32(x
: u32) -> CrateNum
{
47 pub fn as_usize(&self) -> usize {
51 pub fn as_u32(&self) -> u32 {
56 impl fmt
::Display
for CrateNum
{
57 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
58 fmt
::Display
::fmt(&self.0, f
)
62 impl serialize
::UseSpecializedEncodable
for CrateNum
{
63 fn default_encode
<S
: Encoder
>(&self, s
: &mut S
) -> Result
<(), S
::Error
> {
68 impl serialize
::UseSpecializedDecodable
for CrateNum
{
69 fn default_decode
<D
: Decoder
>(d
: &mut D
) -> Result
<CrateNum
, D
::Error
> {
70 d
.read_u32().map(CrateNum
)
74 /// A DefIndex is an index into the hir-map for a crate, identifying a
75 /// particular definition. It should really be considered an interned
76 /// shorthand for a particular DefPath.
77 #[derive(Clone, Debug, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
78 RustcDecodable
, Hash
, Copy
)]
79 pub struct DefIndex(u32);
82 pub fn new(x
: usize) -> DefIndex
{
83 assert
!(x
< (u32::MAX
as usize));
87 pub fn from_u32(x
: u32) -> DefIndex
{
91 pub fn as_usize(&self) -> usize {
95 pub fn as_u32(&self) -> u32 {
100 /// The crate root is always assigned index 0 by the AST Map code,
101 /// thanks to `NodeCollector::new`.
102 pub const CRATE_DEF_INDEX
: DefIndex
= DefIndex(0);
104 /// A DefId identifies a particular *definition*, by combining a crate
105 /// index and a def index.
106 #[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, RustcDecodable, Hash, Copy)]
112 impl fmt
::Debug
for DefId
{
113 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
114 write
!(f
, "DefId {{ krate: {:?}, node: {:?}",
115 self.krate
, self.index
)?
;
117 ty
::tls
::with_opt(|opt_tcx
| {
118 if let Some(tcx
) = opt_tcx
{
119 if let Some(def_path
) = tcx
.opt_def_path(*self) {
120 write
!(f
, " => {}", def_path
.to_string(tcx
))?
;
132 pub fn local(index
: DefIndex
) -> DefId
{
133 DefId { krate: LOCAL_CRATE, index: index }
136 pub fn is_local(&self) -> bool
{
137 self.krate
== LOCAL_CRATE