2015-08-16 05:31:58 -05:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-09-01 11:35:05 -05:00
|
|
|
use metadata::cstore::LOCAL_CRATE;
|
2015-08-16 07:52:36 -05:00
|
|
|
use middle::ty;
|
2015-08-16 05:31:58 -05:00
|
|
|
use syntax::ast::{CrateNum, NodeId};
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
|
|
|
|
RustcDecodable, Hash, Copy)]
|
|
|
|
pub struct DefId {
|
|
|
|
pub krate: CrateNum,
|
|
|
|
pub node: NodeId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for DefId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-08-16 07:52:36 -05:00
|
|
|
try!(write!(f, "DefId {{ krate: {}, node: {}",
|
2015-08-16 05:31:58 -05:00
|
|
|
self.krate, self.node));
|
2015-08-16 07:52:36 -05:00
|
|
|
|
|
|
|
// Unfortunately, there seems to be no way to attempt to print
|
|
|
|
// a path for a def-id, so I'll just make a best effort for now
|
|
|
|
// and otherwise fallback to just printing the crate/node pair
|
|
|
|
try!(ty::tls::with_opt(|opt_tcx| {
|
|
|
|
if let Some(tcx) = opt_tcx {
|
|
|
|
try!(write!(f, " => {}", tcx.item_path_str(*self)));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}));
|
|
|
|
|
|
|
|
write!(f, " }}")
|
2015-08-16 05:31:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 07:52:36 -05:00
|
|
|
|
2015-08-16 05:31:58 -05:00
|
|
|
impl DefId {
|
2015-09-02 15:11:32 -05:00
|
|
|
pub fn xxx_local(id: NodeId) -> DefId {
|
2015-08-16 05:31:58 -05:00
|
|
|
DefId { krate: LOCAL_CRATE, node: id }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the node id, asserting that this def-id is krate-local.
|
|
|
|
pub fn local_id(&self) -> NodeId {
|
|
|
|
assert_eq!(self.krate, LOCAL_CRATE);
|
|
|
|
self.node
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_local(&self) -> bool {
|
|
|
|
self.krate == LOCAL_CRATE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|