Skip to content

Commit

Permalink
save-analysis: emit info about impls and super-traits in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Feb 13, 2017
1 parent 956e2bc commit 530d09c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 20 deletions.
17 changes: 1 addition & 16 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,21 +747,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
trait_ref: &'l Option<ast::TraitRef>,
typ: &'l ast::Ty,
impl_items: &'l [ast::ImplItem]) {
let mut has_self_ref = false;
if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
down_cast_data!(impl_data, ImplData, item.span);
if let Some(ref self_ref) = impl_data.self_ref {
has_self_ref = true;
if !self.span.filter_generated(Some(self_ref.span), item.span) {
self.dumper.type_ref(self_ref.clone().lower(self.tcx));
}
}
if let Some(ref trait_ref_data) = impl_data.trait_ref {
if !self.span.filter_generated(Some(trait_ref_data.span), item.span) {
self.dumper.type_ref(trait_ref_data.clone().lower(self.tcx));
}
}

if !self.span.filter_generated(Some(impl_data.span), item.span) {
self.dumper.impl_data(ImplData {
id: impl_data.id,
Expand All @@ -772,9 +759,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
}.lower(self.tcx));
}
}
if !has_self_ref {
self.visit_ty(&typ);
}
self.visit_ty(&typ);
if let &Some(ref trait_ref) = trait_ref {
self.process_path(trait_ref.ref_id, &trait_ref.path, Some(recorder::TypeRef));
}
Expand Down
47 changes: 47 additions & 0 deletions src/librustc_save_analysis/json_api_dumper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ impl<'b, W: Write + 'b> Dump for JsonApiDumper<'b, W> {
impl_fn!(mod_data, ModData, defs);
impl_fn!(typedef, TypeDefData, defs);
impl_fn!(variable, VariableData, defs);

fn impl_data(&mut self, data: ImplData) {
if data.self_ref.is_some() {
self.result.relations.push(From::from(data));
}
}
fn inheritance(&mut self, data: InheritanceData) {
self.result.relations.push(From::from(data));
}
}

// FIXME methods. The defs have information about possible overriding and the
Expand All @@ -87,6 +96,7 @@ struct Analysis {
prelude: Option<CratePreludeData>,
imports: Vec<Import>,
defs: Vec<Def>,
relations: Vec<Relation>,
// These two fields are dummies so that clients can parse the two kinds of
// JSON data in the same way.
refs: Vec<()>,
Expand All @@ -100,6 +110,7 @@ impl Analysis {
prelude: None,
imports: vec![],
defs: vec![],
relations: vec![],
refs: vec![],
macro_refs: vec![],
}
Expand Down Expand Up @@ -427,6 +438,42 @@ impl From<VariableData> for Option<Def> {
}
}

#[derive(Debug, RustcEncodable)]
struct Relation {
span: SpanData,
kind: RelationKind,
from: Id,
to: Id,
}

#[derive(Debug, RustcEncodable)]
enum RelationKind {
Impl,
SuperTrait,
}

impl From<ImplData> for Relation {
fn from(data: ImplData) -> Relation {
Relation {
span: data.span,
kind: RelationKind::Impl,
from: From::from(data.self_ref.unwrap_or(null_def_id())),
to: From::from(data.trait_ref.unwrap_or(null_def_id())),
}
}
}

impl From<InheritanceData> for Relation {
fn from(data: InheritanceData) -> Relation {
Relation {
span: data.span,
kind: RelationKind::SuperTrait,
from: From::from(data.base_id),
to: From::from(data.deriv_id),
}
}
}

#[derive(Debug, RustcEncodable)]
pub struct JsonSignature {
span: SpanData,
Expand Down
49 changes: 46 additions & 3 deletions src/librustc_save_analysis/json_dumper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ impl<'b, W: Write + 'b> Dump for JsonDumper<'b, W> {
self.result.defs.push(def);
}

// FIXME store this instead of throwing it away.
fn impl_data(&mut self, _data: ImplData) {}
fn inheritance(&mut self, _data: InheritanceData) {}
fn impl_data(&mut self, data: ImplData) {
if data.self_ref.is_some() {
self.result.relations.push(From::from(data));
}
}
fn inheritance(&mut self, data: InheritanceData) {
self.result.relations.push(From::from(data));
}
}

// FIXME do we want to change ExternalData to this mode? It will break DXR.
Expand All @@ -131,6 +136,7 @@ struct Analysis {
defs: Vec<Def>,
refs: Vec<Ref>,
macro_refs: Vec<MacroRef>,
relations: Vec<Relation>,
}

impl Analysis {
Expand All @@ -142,6 +148,7 @@ impl Analysis {
defs: vec![],
refs: vec![],
macro_refs: vec![],
relations: vec![],
}
}
}
Expand Down Expand Up @@ -508,6 +515,42 @@ impl From<MacroUseData> for MacroRef {
}
}

#[derive(Debug, RustcEncodable)]
struct Relation {
span: SpanData,
kind: RelationKind,
from: Id,
to: Id,
}

#[derive(Debug, RustcEncodable)]
enum RelationKind {
Impl,
SuperTrait,
}

impl From<ImplData> for Relation {
fn from(data: ImplData) -> Relation {
Relation {
span: data.span,
kind: RelationKind::Impl,
from: From::from(data.self_ref.unwrap_or(null_def_id())),
to: From::from(data.trait_ref.unwrap_or(null_def_id())),
}
}
}

impl From<InheritanceData> for Relation {
fn from(data: InheritanceData) -> Relation {
Relation {
span: data.span,
kind: RelationKind::SuperTrait,
from: From::from(data.base_id),
to: From::from(data.deriv_id),
}
}
}

#[derive(Debug, RustcEncodable)]
pub struct JsonSignature {
span: SpanData,
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
match typ.node {
// Common case impl for a struct or something basic.
ast::TyKind::Path(None, ref path) => {
filter!(self.span_utils, None, path.span, None);
if generated_code(path.span) {
return None;
}
sub_span = self.span_utils.sub_span_for_type_name(path.span);
type_data = self.lookup_ref_id(typ.id).map(|id| {
TypeRefData {
Expand Down

0 comments on commit 530d09c

Please sign in to comment.