//! The `Dump` trait can be used together with `DumpVisitor` in order to
//! retrieve the data from a crate.
-use std::hash::Hasher;
-
use rustc::hir::def_id::DefId;
-use rustc::ty;
use syntax::ast::{CrateNum, NodeId};
use syntax::codemap::Span;
pub struct CrateData {
pub name: String,
pub number: u32,
+ pub span: Span,
}
/// Data for any entity in the Rust language. The actual data contained varies
/// with the kind of entity being queried. See the nested structs for details.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub enum Data {
/// Data for Enums.
EnumData(EnumData),
/// Data for a tuple variant.
TupleVariantData(TupleVariantData),
/// Data for a typedef.
- TypeDefData(TypedefData),
+ TypeDefData(TypeDefData),
/// Data for a reference to a type or trait.
TypeRefData(TypeRefData),
/// Data for a use statement.
}
/// Data for the prelude of a crate.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct CratePreludeData {
pub crate_name: String,
- pub crate_root: Option<String>,
- pub external_crates: Vec<ExternalCrateData>
+ pub crate_root: String,
+ pub external_crates: Vec<ExternalCrateData>,
+ pub span: Span,
}
/// Data for external crates in the prelude of a crate.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct ExternalCrateData {
pub name: String,
- pub num: CrateNum
+ pub num: CrateNum,
+ pub file_name: String,
}
/// Data for enum declarations.
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, RustcEncodable)]
pub struct EnumData {
pub id: NodeId,
+ pub name: String,
pub value: String,
pub qualname: String,
pub span: Span,
}
/// Data for extern crates.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct ExternCrateData {
pub id: NodeId,
pub name: String,
}
/// Data about a function call.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct FunctionCallData {
pub span: Span,
pub scope: NodeId,
}
/// Data for all kinds of functions and methods.
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, RustcEncodable)]
pub struct FunctionData {
pub id: NodeId,
pub name: String,
pub declaration: Option<DefId>,
pub span: Span,
pub scope: NodeId,
+ pub value: String,
}
/// Data about a function call.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct FunctionRefData {
pub span: Span,
pub scope: NodeId,
pub ref_id: DefId,
}
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct ImplData {
pub id: NodeId,
pub span: Span,
pub self_ref: Option<DefId>,
}
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
// FIXME: this struct should not exist. However, removing it requires heavy
// refactoring of dump_visitor.rs. See PR 31838 for more info.
pub struct ImplData2 {
pub self_ref: Option<TypeRefData>,
}
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct InheritanceData {
pub span: Span,
pub base_id: DefId,
}
/// Data about a macro declaration.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct MacroData {
pub span: Span,
pub name: String,
}
/// Data about a macro use.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct MacroUseData {
pub span: Span,
pub name: String,
}
/// Data about a method call.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct MethodCallData {
pub span: Span,
pub scope: NodeId,
}
/// Data for method declarations (methods with a body are treated as functions).
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, RustcEncodable)]
pub struct MethodData {
pub id: NodeId,
+ pub name: String,
pub qualname: String,
pub span: Span,
pub scope: NodeId,
+ pub value: String,
}
/// Data for modules.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct ModData {
pub id: NodeId,
pub name: String,
}
/// Data for a reference to a module.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct ModRefData {
pub span: Span,
pub scope: NodeId,
pub qualname: String
}
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct StructData {
pub span: Span,
+ pub name: String,
pub id: NodeId,
pub ctor_id: NodeId,
pub qualname: String,
pub value: String
}
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct StructVariantData {
pub span: Span,
+ pub name: String,
pub id: NodeId,
pub qualname: String,
pub type_value: String,
pub scope: NodeId
}
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct TraitData {
pub span: Span,
pub id: NodeId,
+ pub name: String,
pub qualname: String,
pub scope: NodeId,
pub value: String
}
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct TupleVariantData {
pub span: Span,
pub id: NodeId,
}
/// Data for a typedef.
-#[derive(Debug)]
-pub struct TypedefData {
+#[derive(Debug, RustcEncodable)]
+pub struct TypeDefData {
pub id: NodeId,
+ pub name: String,
pub span: Span,
pub qualname: String,
pub value: String,
}
/// Data for a reference to a type or trait.
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, RustcEncodable)]
pub struct TypeRefData {
pub span: Span,
pub scope: NodeId,
pub qualname: String,
}
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct UseData {
pub id: NodeId,
pub span: Span,
pub scope: NodeId
}
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct UseGlobData {
pub id: NodeId,
pub span: Span,
}
/// Data for local and global variables (consts and statics).
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct VariableData {
pub id: NodeId,
pub name: String,
/// Data for the use of some item (e.g., the use of a local variable, which
/// will refer to that variables declaration (by ref_id)).
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
pub struct VariableRefData {
pub name: String,
pub span: Span,
pub scope: NodeId,
pub ref_id: DefId,
}
-
-// Emitted ids are used to cross-reference items across crates. DefIds and
-// NodeIds do not usually correspond in any way. The strategy is to use the
-// index from the DefId as a crate-local id. However, within a crate, DefId
-// indices and NodeIds can overlap. So, we must adjust the NodeIds. If an
-// item can be identified by a DefId as well as a NodeId, then we use the
-// DefId index as the id. If it can't, then we have to use the NodeId, but
-// need to adjust it so it will not clash with any possible DefId index.
-pub fn normalize_node_id<'a>(tcx: &ty::TyCtxt<'a>, id: NodeId) -> usize {
- match tcx.map.opt_local_def_id(id) {
- Some(id) => id.index.as_usize(),
- None => id as usize + tcx.map.num_local_def_ids()
- }
-}
-
-// Macro to implement a normalize() function (see below for usage)
-macro_rules! impl_normalize {
- ($($t:ty => $($field:ident),*);*) => {
- $(
- impl $t {
- pub fn normalize<'a>(mut self, tcx: &ty::TyCtxt<'a>) -> $t {
- $(
- self.$field = normalize_node_id(tcx, self.$field) as u32;
- )*
- self
- }
- }
- )*
- }
-}
-
-impl_normalize! {
- EnumData => id, scope;
- ExternCrateData => id, scope;
- FunctionCallData => scope;
- FunctionData => id, scope;
- FunctionRefData => scope;
- ImplData => id, scope;
- InheritanceData => deriv_id;
- MacroUseData => scope;
- MethodCallData => scope;
- MethodData => id, scope;
- ModData => id, scope;
- ModRefData => scope;
- StructData => ctor_id, id, scope;
- StructVariantData => id, scope;
- TupleVariantData => id, scope;
- TraitData => id, scope;
- TypedefData => id;
- TypeRefData => scope;
- UseData => id, scope;
- UseGlobData => id, scope;
- VariableData => id;
- VariableRefData => scope
-}