Skip to content

Commit c28374e

Browse files
committed
save-analysis: some refinements to JSON data
Split variable and function kinds to give more information. Give children for methods, structs, enums, and traits.
1 parent a9234c1 commit c28374e

File tree

5 files changed

+77
-18
lines changed

5 files changed

+77
-18
lines changed

src/librustc_save_analysis/data.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ pub struct EnumData {
102102
pub qualname: String,
103103
pub span: Span,
104104
pub scope: NodeId,
105+
pub variants: Vec<NodeId>,
106+
105107
}
106108

107109
/// Data for extern crates.
@@ -223,6 +225,7 @@ pub struct ModData {
223225
pub span: Span,
224226
pub scope: NodeId,
225227
pub filename: String,
228+
pub items: Vec<NodeId>,
226229
}
227230

228231
/// Data for a reference to a module.
@@ -242,7 +245,8 @@ pub struct StructData {
242245
pub ctor_id: NodeId,
243246
pub qualname: String,
244247
pub scope: NodeId,
245-
pub value: String
248+
pub value: String,
249+
pub fields: Vec<NodeId>,
246250
}
247251

248252
#[derive(Debug, RustcEncodable)]
@@ -263,7 +267,8 @@ pub struct TraitData {
263267
pub name: String,
264268
pub qualname: String,
265269
pub scope: NodeId,
266-
pub value: String
270+
pub value: String,
271+
pub items: Vec<NodeId>,
267272
}
268273

269274
#[derive(Debug, RustcEncodable)]
@@ -317,6 +322,7 @@ pub struct UseGlobData {
317322
#[derive(Debug, RustcEncodable)]
318323
pub struct VariableData {
319324
pub id: NodeId,
325+
pub kind: VariableKind,
320326
pub name: String,
321327
pub qualname: String,
322328
pub span: Span,
@@ -325,6 +331,14 @@ pub struct VariableData {
325331
pub type_value: String,
326332
}
327333

334+
#[derive(Debug, RustcEncodable)]
335+
pub enum VariableKind {
336+
Static,
337+
Const,
338+
Local,
339+
Field,
340+
}
341+
328342
/// Data for the use of some item (e.g., the use of a local variable, which
329343
/// will refer to that variables declaration (by ref_id)).
330344
#[derive(Debug, RustcEncodable)]

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
356356
if !self.span.filter_generated(sub_span, p.span) {
357357
self.dumper.variable(VariableData {
358358
id: id,
359+
kind: VariableKind::Local,
359360
span: sub_span.expect("No span found for variable"),
360361
name: path_to_string(p),
361362
qualname: format!("{}::{}", qualname, path_to_string(p)),
@@ -519,6 +520,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
519520
if !self.span.filter_generated(sub_span, span) {
520521
self.dumper.variable(VariableData {
521522
span: sub_span.expect("No span found for variable"),
523+
kind: VariableKind::Const,
522524
id: id,
523525
name: name.to_string(),
524526
qualname: qualname,
@@ -542,17 +544,18 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
542544
let qualname = format!("::{}", self.tcx.node_path_str(item.id));
543545

544546
let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
545-
let val = if let ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) =
546-
item.node {
547+
let (val, fields) =
548+
if let ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) = item.node
549+
{
547550
let fields_str = fields.iter()
548551
.enumerate()
549552
.map(|(i, f)| f.ident.map(|i| i.to_string())
550553
.unwrap_or(i.to_string()))
551554
.collect::<Vec<_>>()
552555
.join(", ");
553-
format!("{} {{ {} }}", name, fields_str)
556+
(format!("{} {{ {} }}", name, fields_str), fields.iter().map(|f| f.id).collect())
554557
} else {
555-
String::new()
558+
(String::new(), vec![])
556559
};
557560

558561
if !self.span.filter_generated(sub_span, item.span) {
@@ -563,7 +566,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
563566
ctor_id: def.id(),
564567
qualname: qualname.clone(),
565568
scope: self.cur_scope,
566-
value: val
569+
value: val,
570+
fields: fields,
567571
}.lower(self.tcx));
568572
}
569573

@@ -718,7 +722,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
718722
name: name,
719723
qualname: qualname.clone(),
720724
scope: self.cur_scope,
721-
value: val
725+
value: val,
726+
items: methods.iter().map(|i| i.id).collect(),
722727
}.lower(self.tcx));
723728
}
724729

@@ -958,6 +963,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
958963
if !self.span.filter_generated(sub_span, p.span) {
959964
self.dumper.variable(VariableData {
960965
span: sub_span.expect("No span found for variable"),
966+
kind: VariableKind::Local,
961967
id: id,
962968
name: path_to_string(p),
963969
qualname: format!("{}${}", path_to_string(p), id),
@@ -1366,6 +1372,7 @@ impl<'v, 'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'v> for DumpVisitor<'l, 'tcx,
13661372
if !self.span.filter_generated(Some(p.span), p.span) {
13671373
self.dumper.variable(VariableData {
13681374
span: p.span,
1375+
kind: VariableKind::Local,
13691376
id: id,
13701377
name: path_to_string(p),
13711378
qualname: format!("{}${}", path_to_string(p), id),

src/librustc_save_analysis/external_data.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc::ty::TyCtxt;
1414
use syntax::ast::{CrateNum, NodeId};
1515
use syntax::codemap::{Span, CodeMap};
1616

17-
use super::data;
17+
use data;
1818

1919
// FIXME: this should be pub(crate), but the current snapshot doesn't allow it yet
2020
pub trait Lower {
@@ -90,6 +90,7 @@ pub struct EnumData {
9090
pub qualname: String,
9191
pub span: SpanData,
9292
pub scope: DefId,
93+
pub variants: Vec<DefId>
9394
}
9495

9596
impl Lower for data::EnumData {
@@ -103,6 +104,7 @@ impl Lower for data::EnumData {
103104
qualname: self.qualname,
104105
span: SpanData::from_span(self.span, tcx.sess.codemap()),
105106
scope: make_def_id(self.scope, &tcx.map),
107+
variants: self.variants.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
106108
}
107109
}
108110
}
@@ -345,6 +347,7 @@ pub struct ModData {
345347
pub span: SpanData,
346348
pub scope: DefId,
347349
pub filename: String,
350+
pub items: Vec<DefId>,
348351
}
349352

350353
impl Lower for data::ModData {
@@ -358,6 +361,7 @@ impl Lower for data::ModData {
358361
span: SpanData::from_span(self.span, tcx.sess.codemap()),
359362
scope: make_def_id(self.scope, &tcx.map),
360363
filename: self.filename,
364+
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
361365
}
362366
}
363367
}
@@ -392,7 +396,8 @@ pub struct StructData {
392396
pub ctor_id: DefId,
393397
pub qualname: String,
394398
pub scope: DefId,
395-
pub value: String
399+
pub value: String,
400+
pub fields: Vec<DefId>,
396401
}
397402

398403
impl Lower for data::StructData {
@@ -406,7 +411,8 @@ impl Lower for data::StructData {
406411
ctor_id: make_def_id(self.ctor_id, &tcx.map),
407412
qualname: self.qualname,
408413
scope: make_def_id(self.scope, &tcx.map),
409-
value: self.value
414+
value: self.value,
415+
fields: self.fields.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
410416
}
411417
}
412418
}
@@ -445,7 +451,8 @@ pub struct TraitData {
445451
pub id: DefId,
446452
pub qualname: String,
447453
pub scope: DefId,
448-
pub value: String
454+
pub value: String,
455+
pub items: Vec<DefId>,
449456
}
450457

451458
impl Lower for data::TraitData {
@@ -459,6 +466,7 @@ impl Lower for data::TraitData {
459466
qualname: self.qualname,
460467
scope: make_def_id(self.scope, &tcx.map),
461468
value: self.value,
469+
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
462470
}
463471
}
464472
}
@@ -585,6 +593,7 @@ impl Lower for data::UseGlobData {
585593
pub struct VariableData {
586594
pub id: DefId,
587595
pub name: String,
596+
pub kind: data::VariableKind,
588597
pub qualname: String,
589598
pub span: SpanData,
590599
pub scope: DefId,
@@ -598,6 +607,7 @@ impl Lower for data::VariableData {
598607
fn lower(self, tcx: TyCtxt) -> VariableData {
599608
VariableData {
600609
id: make_def_id(self.id, &tcx.map),
610+
kind: self.kind,
601611
name: self.name,
602612
qualname: self.qualname,
603613
span: SpanData::from_span(self.span, tcx.sess.codemap()),

src/librustc_save_analysis/json_dumper.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use std::io::Write;
1313
use rustc::hir::def_id::DefId;
1414
use rustc_serialize::json::as_json;
1515

16-
use super::external_data::*;
17-
use super::dump::Dump;
16+
use external_data::*;
17+
use data::VariableKind;
18+
use dump::Dump;
1819

1920
pub struct JsonDumper<'b, W: Write + 'b> {
2021
output: &'b mut W,
@@ -180,6 +181,7 @@ struct Def {
180181
name: String,
181182
qualname: String,
182183
value: String,
184+
children: Vec<Id>,
183185
}
184186

185187
#[derive(Debug, RustcEncodable)]
@@ -194,14 +196,19 @@ enum DefKind {
194196
Trait,
195197
// value = type + generics
196198
Function,
199+
// value = type + generics
200+
Method,
197201
// No id, no value.
198202
Macro,
199203
// value = file_name
200204
Mod,
201205
// value = aliased type
202206
Type,
203-
// value = type and init expression
204-
Variable,
207+
// value = type and init expression (for all variable kinds).
208+
Local,
209+
Static,
210+
Const,
211+
Field,
205212
}
206213

207214
impl From<EnumData> for Def {
@@ -213,6 +220,7 @@ impl From<EnumData> for Def {
213220
name: data.name,
214221
qualname: data.qualname,
215222
value: data.value,
223+
children: data.variants.into_iter().map(|id| From::from(id)).collect(),
216224
}
217225
}
218226
}
@@ -226,6 +234,7 @@ impl From<TupleVariantData> for Def {
226234
name: data.name,
227235
qualname: data.qualname,
228236
value: data.value,
237+
children: vec![],
229238
}
230239
}
231240
}
@@ -238,6 +247,7 @@ impl From<StructVariantData> for Def {
238247
name: data.name,
239248
qualname: data.qualname,
240249
value: data.value,
250+
children: vec![],
241251
}
242252
}
243253
}
@@ -250,6 +260,7 @@ impl From<StructData> for Def {
250260
name: data.name,
251261
qualname: data.qualname,
252262
value: data.value,
263+
children: data.fields.into_iter().map(|id| From::from(id)).collect(),
253264
}
254265
}
255266
}
@@ -262,6 +273,7 @@ impl From<TraitData> for Def {
262273
name: data.name,
263274
qualname: data.qualname,
264275
value: data.value,
276+
children: data.items.into_iter().map(|id| From::from(id)).collect(),
265277
}
266278
}
267279
}
@@ -274,18 +286,20 @@ impl From<FunctionData> for Def {
274286
name: data.name,
275287
qualname: data.qualname,
276288
value: data.value,
289+
children: vec![],
277290
}
278291
}
279292
}
280293
impl From<MethodData> for Def {
281294
fn from(data: MethodData) -> Def {
282295
Def {
283-
kind: DefKind::Function,
296+
kind: DefKind::Method,
284297
id: From::from(data.id),
285298
span: data.span,
286299
name: data.name,
287300
qualname: data.qualname,
288301
value: data.value,
302+
children: vec![],
289303
}
290304
}
291305
}
@@ -298,6 +312,7 @@ impl From<MacroData> for Def {
298312
name: data.name,
299313
qualname: data.qualname,
300314
value: String::new(),
315+
children: vec![],
301316
}
302317
}
303318
}
@@ -310,6 +325,7 @@ impl From<ModData> for Def {
310325
name: data.name,
311326
qualname: data.qualname,
312327
value: data.filename,
328+
children: data.items.into_iter().map(|id| From::from(id)).collect(),
313329
}
314330
}
315331
}
@@ -322,18 +338,25 @@ impl From<TypeDefData> for Def {
322338
name: data.name,
323339
qualname: data.qualname,
324340
value: data.value,
341+
children: vec![],
325342
}
326343
}
327344
}
328345
impl From<VariableData> for Def {
329346
fn from(data: VariableData) -> Def {
330347
Def {
331-
kind: DefKind::Variable,
348+
kind: match data.kind {
349+
VariableKind::Static => DefKind::Static,
350+
VariableKind::Const => DefKind::Const,
351+
VariableKind::Local => DefKind::Local,
352+
VariableKind::Field => DefKind::Field,
353+
},
332354
id: From::from(data.id),
333355
span: data.span,
334356
name: data.name,
335357
qualname: data.qualname,
336358
value: data.value,
359+
children: vec![],
337360
}
338361
}
339362
}

0 commit comments

Comments
 (0)