]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/def_id.rs
move direct accesses of `node` to go through `as_local_node_id`, unless
[rust.git] / src / librustc / middle / 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 metadata::cstore::LOCAL_CRATE;
12 use middle::ty;
13 use syntax::ast::{CrateNum, NodeId};
14 use std::fmt;
15
16 #[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
17            RustcDecodable, Hash, Copy)]
18 pub struct DefId {
19     pub krate: CrateNum,
20     pub xxx_node: NodeId,
21 }
22
23 impl fmt::Debug for DefId {
24     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25         try!(write!(f, "DefId {{ krate: {}, node: {}",
26                     self.krate, self.xxx_node));
27
28         // Unfortunately, there seems to be no way to attempt to print
29         // a path for a def-id, so I'll just make a best effort for now
30         // and otherwise fallback to just printing the crate/node pair
31         try!(ty::tls::with_opt(|opt_tcx| {
32             if let Some(tcx) = opt_tcx {
33                 try!(write!(f, " => {}", tcx.item_path_str(*self)));
34             }
35             Ok(())
36         }));
37
38         write!(f, " }}")
39     }
40 }
41
42
43 impl DefId {
44     pub fn xxx_local(id: NodeId) -> DefId {
45         DefId { krate: LOCAL_CRATE, xxx_node: id }
46     }
47
48     pub fn is_local(&self) -> bool {
49         self.krate == LOCAL_CRATE
50     }
51 }
52
53