Skip to content

Commit

Permalink
save-analysis: add a decl_id for methods
Browse files Browse the repository at this point in the history
This is non-null if the method is in a (non-inherent) impl and in that case will be the id for the method declaration in the implemented trait.
  • Loading branch information
nrc committed Jun 16, 2016
1 parent c28374e commit a835d74
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/librustc_save_analysis/data.rs
Expand Up @@ -214,6 +214,7 @@ pub struct MethodData {
pub span: Span,
pub scope: NodeId,
pub value: String,
pub decl_id: Option<DefId>,
}

/// Data for modules.
Expand Down
52 changes: 35 additions & 17 deletions src/librustc_save_analysis/dump_visitor.rs
Expand Up @@ -30,7 +30,7 @@
use rustc::hir::def::Def;
use rustc::hir::def_id::DefId;
use rustc::session::Session;
use rustc::ty::{self, TyCtxt};
use rustc::ty::{self, TyCtxt, ImplOrTraitItem, ImplOrTraitItemContainer};

use std::collections::HashSet;
use std::hash::*;
Expand Down Expand Up @@ -381,24 +381,42 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {

let sig_str = ::make_signature(&sig.decl, &sig.generics);
if body.is_some() {
if !self.span.filter_generated(Some(method_data.span), span) {
let mut data = method_data.clone();
data.value = sig_str;
self.dumper.function(data.lower(self.tcx));
}
self.process_formals(&sig.decl.inputs, &method_data.qualname);
} else {
if !self.span.filter_generated(Some(method_data.span), span) {
self.dumper.method(MethodData {
id: method_data.id,
name: method_data.name,
span: method_data.span,
scope: method_data.scope,
qualname: method_data.qualname.clone(),
value: sig_str,
}.lower(self.tcx));
}
}

// If the method is defined in an impl, then try and find the corresponding
// method decl in a trait, and if there is one, make a decl_id for it. This
// requires looking up the impl, then the trait, then searching for a method
// with the right name.
if !self.span.filter_generated(Some(method_data.span), span) {
let container =
self.tcx.impl_or_trait_item(self.tcx.map.local_def_id(id)).container();
let decl_id = if let ImplOrTraitItemContainer::ImplContainer(id) = container {
self.tcx.trait_id_of_impl(id).and_then(|id| {
for item in &**self.tcx.trait_items(id) {
if let &ImplOrTraitItem::MethodTraitItem(ref m) = item {
if m.name == name {
return Some(m.def_id);
}
}
}
None
})
} else {
None
};

self.dumper.method(MethodData {
id: method_data.id,
name: method_data.name,
span: method_data.span,
scope: method_data.scope,
qualname: method_data.qualname.clone(),
value: sig_str,
decl_id: decl_id,
}.lower(self.tcx));
}

self.process_generic_params(&sig.generics, span, &method_data.qualname, id);
}

Expand Down
2 changes: 2 additions & 0 deletions src/librustc_save_analysis/external_data.rs
Expand Up @@ -321,6 +321,7 @@ pub struct MethodData {
pub span: SpanData,
pub scope: DefId,
pub value: String,
pub decl_id: Option<DefId>,
}

impl Lower for data::MethodData {
Expand All @@ -334,6 +335,7 @@ impl Lower for data::MethodData {
id: make_def_id(self.id, &tcx.map),
qualname: self.qualname,
value: self.value,
decl_id: self.decl_id,
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/librustc_save_analysis/json_dumper.rs
Expand Up @@ -182,6 +182,7 @@ struct Def {
qualname: String,
value: String,
children: Vec<Id>,
decl_id: Option<Id>,
}

#[derive(Debug, RustcEncodable)]
Expand Down Expand Up @@ -221,6 +222,7 @@ impl From<EnumData> for Def {
qualname: data.qualname,
value: data.value,
children: data.variants.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
}
}
}
Expand All @@ -235,6 +237,7 @@ impl From<TupleVariantData> for Def {
qualname: data.qualname,
value: data.value,
children: vec![],
decl_id: None,
}
}
}
Expand All @@ -248,6 +251,7 @@ impl From<StructVariantData> for Def {
qualname: data.qualname,
value: data.value,
children: vec![],
decl_id: None,
}
}
}
Expand All @@ -261,6 +265,7 @@ impl From<StructData> for Def {
qualname: data.qualname,
value: data.value,
children: data.fields.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
}
}
}
Expand All @@ -274,6 +279,7 @@ impl From<TraitData> for Def {
qualname: data.qualname,
value: data.value,
children: data.items.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
}
}
}
Expand All @@ -287,6 +293,7 @@ impl From<FunctionData> for Def {
qualname: data.qualname,
value: data.value,
children: vec![],
decl_id: None,
}
}
}
Expand All @@ -300,6 +307,7 @@ impl From<MethodData> for Def {
qualname: data.qualname,
value: data.value,
children: vec![],
decl_id: data.decl_id.map(|id| From::from(id)),
}
}
}
Expand All @@ -313,6 +321,7 @@ impl From<MacroData> for Def {
qualname: data.qualname,
value: String::new(),
children: vec![],
decl_id: None,
}
}
}
Expand All @@ -326,6 +335,7 @@ impl From<ModData> for Def {
qualname: data.qualname,
value: data.filename,
children: data.items.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
}
}
}
Expand All @@ -339,6 +349,7 @@ impl From<TypeDefData> for Def {
qualname: data.qualname,
value: data.value,
children: vec![],
decl_id: None,
}
}
}
Expand All @@ -357,6 +368,7 @@ impl From<VariableData> for Def {
qualname: data.qualname,
value: data.value,
children: vec![],
decl_id: None,
}
}
}
Expand Down

0 comments on commit a835d74

Please sign in to comment.