]> git.proxmox.com Git - rustc.git/blobdiff - src/librustc_save_analysis/data.rs
Imported Upstream version 1.10.0+dfsg1
[rustc.git] / src / librustc_save_analysis / data.rs
index 7f2f2618c3c96aa19f85966eba741cbb47112c19..2275a1c5c65f2720714d9a5861602d7bbdfc4bc3 100644 (file)
 //! 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),
@@ -64,7 +62,7 @@ pub enum Data {
     /// 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.
@@ -79,24 +77,27 @@ pub enum Data {
 }
 
 /// 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,
@@ -104,7 +105,7 @@ pub struct EnumData {
 }
 
 /// Data for extern crates.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
 pub struct ExternCrateData {
     pub id: NodeId,
     pub name: String,
@@ -115,7 +116,7 @@ pub struct ExternCrateData {
 }
 
 /// Data about a function call.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
 pub struct FunctionCallData {
     pub span: Span,
     pub scope: NodeId,
@@ -123,7 +124,7 @@ pub struct FunctionCallData {
 }
 
 /// Data for all kinds of functions and methods.
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, RustcEncodable)]
 pub struct FunctionData {
     pub id: NodeId,
     pub name: String,
@@ -131,17 +132,18 @@ pub struct FunctionData {
     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,
@@ -150,7 +152,7 @@ pub struct ImplData {
     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 {
@@ -164,7 +166,7 @@ pub struct ImplData2 {
     pub self_ref: Option<TypeRefData>,
 }
 
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
 pub struct InheritanceData {
     pub span: Span,
     pub base_id: DefId,
@@ -172,7 +174,7 @@ pub struct InheritanceData {
 }
 
 /// Data about a macro declaration.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
 pub struct MacroData {
     pub span: Span,
     pub name: String,
@@ -180,7 +182,7 @@ pub struct MacroData {
 }
 
 /// Data about a macro use.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
 pub struct MacroUseData {
     pub span: Span,
     pub name: String,
@@ -193,7 +195,7 @@ pub struct MacroUseData {
 }
 
 /// Data about a method call.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
 pub struct MethodCallData {
     pub span: Span,
     pub scope: NodeId,
@@ -202,16 +204,18 @@ pub struct MethodCallData {
 }
 
 /// 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,
@@ -222,7 +226,7 @@ pub struct ModData {
 }
 
 /// Data for a reference to a module.
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
 pub struct ModRefData {
     pub span: Span,
     pub scope: NodeId,
@@ -230,9 +234,10 @@ pub struct ModRefData {
     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,
@@ -240,9 +245,10 @@ pub struct StructData {
     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,
@@ -250,16 +256,17 @@ pub struct StructVariantData {
     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,
@@ -271,16 +278,17 @@ pub struct TupleVariantData {
 }
 
 /// 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,
@@ -288,7 +296,7 @@ pub struct TypeRefData {
     pub qualname: String,
 }
 
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
 pub struct UseData {
     pub id: NodeId,
     pub span: Span,
@@ -297,7 +305,7 @@ pub struct UseData {
     pub scope: NodeId
 }
 
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
 pub struct UseGlobData {
     pub id: NodeId,
     pub span: Span,
@@ -306,7 +314,7 @@ pub struct UseGlobData {
 }
 
 /// Data for local and global variables (consts and statics).
-#[derive(Debug)]
+#[derive(Debug, RustcEncodable)]
 pub struct VariableData {
     pub id: NodeId,
     pub name: String,
@@ -319,65 +327,10 @@ pub struct VariableData {
 
 /// 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
-}