rust/src/librustc/middle/def_id.rs

54 lines
1.6 KiB
Rust
Raw Normal View History

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;
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 xxx_node: NodeId,
2015-08-16 05:31:58 -05:00
}
impl fmt::Debug for DefId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "DefId {{ krate: {}, node: {}",
self.krate, self.xxx_node));
// 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 05:31:58 -05:00
impl DefId {
pub fn xxx_local(id: NodeId) -> DefId {
DefId { krate: LOCAL_CRATE, xxx_node: id }
2015-08-16 05:31:58 -05:00
}
pub fn is_local(&self) -> bool {
self.krate == LOCAL_CRATE
}
}