Skip to content

Commit

Permalink
save-analysis: some refinements to JSON data
Browse files Browse the repository at this point in the history
Split variable and function kinds to give more information. Give children for methods, structs, enums, and traits.
  • Loading branch information
nrc committed Jun 11, 2016
1 parent a9234c1 commit c28374e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
18 changes: 16 additions & 2 deletions src/librustc_save_analysis/data.rs
Expand Up @@ -102,6 +102,8 @@ pub struct EnumData {
pub qualname: String,
pub span: Span,
pub scope: NodeId,
pub variants: Vec<NodeId>,

}

/// Data for extern crates.
Expand Down Expand Up @@ -223,6 +225,7 @@ pub struct ModData {
pub span: Span,
pub scope: NodeId,
pub filename: String,
pub items: Vec<NodeId>,
}

/// Data for a reference to a module.
Expand All @@ -242,7 +245,8 @@ pub struct StructData {
pub ctor_id: NodeId,
pub qualname: String,
pub scope: NodeId,
pub value: String
pub value: String,
pub fields: Vec<NodeId>,
}

#[derive(Debug, RustcEncodable)]
Expand All @@ -263,7 +267,8 @@ pub struct TraitData {
pub name: String,
pub qualname: String,
pub scope: NodeId,
pub value: String
pub value: String,
pub items: Vec<NodeId>,
}

#[derive(Debug, RustcEncodable)]
Expand Down Expand Up @@ -317,6 +322,7 @@ pub struct UseGlobData {
#[derive(Debug, RustcEncodable)]
pub struct VariableData {
pub id: NodeId,
pub kind: VariableKind,
pub name: String,
pub qualname: String,
pub span: Span,
Expand All @@ -325,6 +331,14 @@ pub struct VariableData {
pub type_value: String,
}

#[derive(Debug, RustcEncodable)]
pub enum VariableKind {
Static,
Const,
Local,
Field,
}

/// 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, RustcEncodable)]
Expand Down
19 changes: 13 additions & 6 deletions src/librustc_save_analysis/dump_visitor.rs
Expand Up @@ -356,6 +356,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
if !self.span.filter_generated(sub_span, p.span) {
self.dumper.variable(VariableData {
id: id,
kind: VariableKind::Local,
span: sub_span.expect("No span found for variable"),
name: path_to_string(p),
qualname: format!("{}::{}", qualname, path_to_string(p)),
Expand Down Expand Up @@ -519,6 +520,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
if !self.span.filter_generated(sub_span, span) {
self.dumper.variable(VariableData {
span: sub_span.expect("No span found for variable"),
kind: VariableKind::Const,
id: id,
name: name.to_string(),
qualname: qualname,
Expand All @@ -542,17 +544,18 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
let qualname = format!("::{}", self.tcx.node_path_str(item.id));

let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
let val = if let ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) =
item.node {
let (val, fields) =
if let ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) = item.node
{
let fields_str = fields.iter()
.enumerate()
.map(|(i, f)| f.ident.map(|i| i.to_string())
.unwrap_or(i.to_string()))
.collect::<Vec<_>>()
.join(", ");
format!("{} {{ {} }}", name, fields_str)
(format!("{} {{ {} }}", name, fields_str), fields.iter().map(|f| f.id).collect())
} else {
String::new()
(String::new(), vec![])
};

if !self.span.filter_generated(sub_span, item.span) {
Expand All @@ -563,7 +566,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
ctor_id: def.id(),
qualname: qualname.clone(),
scope: self.cur_scope,
value: val
value: val,
fields: fields,
}.lower(self.tcx));
}

Expand Down Expand Up @@ -718,7 +722,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
name: name,
qualname: qualname.clone(),
scope: self.cur_scope,
value: val
value: val,
items: methods.iter().map(|i| i.id).collect(),
}.lower(self.tcx));
}

Expand Down Expand Up @@ -958,6 +963,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
if !self.span.filter_generated(sub_span, p.span) {
self.dumper.variable(VariableData {
span: sub_span.expect("No span found for variable"),
kind: VariableKind::Local,
id: id,
name: path_to_string(p),
qualname: format!("{}${}", path_to_string(p), id),
Expand Down Expand Up @@ -1366,6 +1372,7 @@ impl<'v, 'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'v> for DumpVisitor<'l, 'tcx,
if !self.span.filter_generated(Some(p.span), p.span) {
self.dumper.variable(VariableData {
span: p.span,
kind: VariableKind::Local,
id: id,
name: path_to_string(p),
qualname: format!("{}${}", path_to_string(p), id),
Expand Down
18 changes: 14 additions & 4 deletions src/librustc_save_analysis/external_data.rs
Expand Up @@ -14,7 +14,7 @@ use rustc::ty::TyCtxt;
use syntax::ast::{CrateNum, NodeId};
use syntax::codemap::{Span, CodeMap};

use super::data;
use data;

// FIXME: this should be pub(crate), but the current snapshot doesn't allow it yet
pub trait Lower {
Expand Down Expand Up @@ -90,6 +90,7 @@ pub struct EnumData {
pub qualname: String,
pub span: SpanData,
pub scope: DefId,
pub variants: Vec<DefId>
}

impl Lower for data::EnumData {
Expand All @@ -103,6 +104,7 @@ impl Lower for data::EnumData {
qualname: self.qualname,
span: SpanData::from_span(self.span, tcx.sess.codemap()),
scope: make_def_id(self.scope, &tcx.map),
variants: self.variants.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
}
}
}
Expand Down Expand Up @@ -345,6 +347,7 @@ pub struct ModData {
pub span: SpanData,
pub scope: DefId,
pub filename: String,
pub items: Vec<DefId>,
}

impl Lower for data::ModData {
Expand All @@ -358,6 +361,7 @@ impl Lower for data::ModData {
span: SpanData::from_span(self.span, tcx.sess.codemap()),
scope: make_def_id(self.scope, &tcx.map),
filename: self.filename,
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
}
}
}
Expand Down Expand Up @@ -392,7 +396,8 @@ pub struct StructData {
pub ctor_id: DefId,
pub qualname: String,
pub scope: DefId,
pub value: String
pub value: String,
pub fields: Vec<DefId>,
}

impl Lower for data::StructData {
Expand All @@ -406,7 +411,8 @@ impl Lower for data::StructData {
ctor_id: make_def_id(self.ctor_id, &tcx.map),
qualname: self.qualname,
scope: make_def_id(self.scope, &tcx.map),
value: self.value
value: self.value,
fields: self.fields.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
}
}
}
Expand Down Expand Up @@ -445,7 +451,8 @@ pub struct TraitData {
pub id: DefId,
pub qualname: String,
pub scope: DefId,
pub value: String
pub value: String,
pub items: Vec<DefId>,
}

impl Lower for data::TraitData {
Expand All @@ -459,6 +466,7 @@ impl Lower for data::TraitData {
qualname: self.qualname,
scope: make_def_id(self.scope, &tcx.map),
value: self.value,
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
}
}
}
Expand Down Expand Up @@ -585,6 +593,7 @@ impl Lower for data::UseGlobData {
pub struct VariableData {
pub id: DefId,
pub name: String,
pub kind: data::VariableKind,
pub qualname: String,
pub span: SpanData,
pub scope: DefId,
Expand All @@ -598,6 +607,7 @@ impl Lower for data::VariableData {
fn lower(self, tcx: TyCtxt) -> VariableData {
VariableData {
id: make_def_id(self.id, &tcx.map),
kind: self.kind,
name: self.name,
qualname: self.qualname,
span: SpanData::from_span(self.span, tcx.sess.codemap()),
Expand Down
35 changes: 29 additions & 6 deletions src/librustc_save_analysis/json_dumper.rs
Expand Up @@ -13,8 +13,9 @@ use std::io::Write;
use rustc::hir::def_id::DefId;
use rustc_serialize::json::as_json;

use super::external_data::*;
use super::dump::Dump;
use external_data::*;
use data::VariableKind;
use dump::Dump;

pub struct JsonDumper<'b, W: Write + 'b> {
output: &'b mut W,
Expand Down Expand Up @@ -180,6 +181,7 @@ struct Def {
name: String,
qualname: String,
value: String,
children: Vec<Id>,
}

#[derive(Debug, RustcEncodable)]
Expand All @@ -194,14 +196,19 @@ enum DefKind {
Trait,
// value = type + generics
Function,
// value = type + generics
Method,
// No id, no value.
Macro,
// value = file_name
Mod,
// value = aliased type
Type,
// value = type and init expression
Variable,
// value = type and init expression (for all variable kinds).
Local,
Static,
Const,
Field,
}

impl From<EnumData> for Def {
Expand All @@ -213,6 +220,7 @@ impl From<EnumData> for Def {
name: data.name,
qualname: data.qualname,
value: data.value,
children: data.variants.into_iter().map(|id| From::from(id)).collect(),
}
}
}
Expand All @@ -226,6 +234,7 @@ impl From<TupleVariantData> for Def {
name: data.name,
qualname: data.qualname,
value: data.value,
children: vec![],
}
}
}
Expand All @@ -238,6 +247,7 @@ impl From<StructVariantData> for Def {
name: data.name,
qualname: data.qualname,
value: data.value,
children: vec![],
}
}
}
Expand All @@ -250,6 +260,7 @@ impl From<StructData> for Def {
name: data.name,
qualname: data.qualname,
value: data.value,
children: data.fields.into_iter().map(|id| From::from(id)).collect(),
}
}
}
Expand All @@ -262,6 +273,7 @@ impl From<TraitData> for Def {
name: data.name,
qualname: data.qualname,
value: data.value,
children: data.items.into_iter().map(|id| From::from(id)).collect(),
}
}
}
Expand All @@ -274,18 +286,20 @@ impl From<FunctionData> for Def {
name: data.name,
qualname: data.qualname,
value: data.value,
children: vec![],
}
}
}
impl From<MethodData> for Def {
fn from(data: MethodData) -> Def {
Def {
kind: DefKind::Function,
kind: DefKind::Method,
id: From::from(data.id),
span: data.span,
name: data.name,
qualname: data.qualname,
value: data.value,
children: vec![],
}
}
}
Expand All @@ -298,6 +312,7 @@ impl From<MacroData> for Def {
name: data.name,
qualname: data.qualname,
value: String::new(),
children: vec![],
}
}
}
Expand All @@ -310,6 +325,7 @@ impl From<ModData> for Def {
name: data.name,
qualname: data.qualname,
value: data.filename,
children: data.items.into_iter().map(|id| From::from(id)).collect(),
}
}
}
Expand All @@ -322,18 +338,25 @@ impl From<TypeDefData> for Def {
name: data.name,
qualname: data.qualname,
value: data.value,
children: vec![],
}
}
}
impl From<VariableData> for Def {
fn from(data: VariableData) -> Def {
Def {
kind: DefKind::Variable,
kind: match data.kind {
VariableKind::Static => DefKind::Static,
VariableKind::Const => DefKind::Const,
VariableKind::Local => DefKind::Local,
VariableKind::Field => DefKind::Field,
},
id: From::from(data.id),
span: data.span,
name: data.name,
qualname: data.qualname,
value: data.value,
children: vec![],
}
}
}
Expand Down

0 comments on commit c28374e

Please sign in to comment.