forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjson_dumper.rs
604 lines (564 loc) · 15.7 KB
/
json_dumper.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::io::Write;
use rustc::hir::def_id::DefId;
use rustc_serialize::json::as_json;
use external_data::*;
use data::{VariableKind, SigElement};
use dump::Dump;
use super::Format;
pub struct JsonDumper<'b, W: Write + 'b> {
output: &'b mut W,
result: Analysis,
}
impl<'b, W: Write> JsonDumper<'b, W> {
pub fn new(writer: &'b mut W) -> JsonDumper<'b, W> {
JsonDumper { output: writer, result: Analysis::new() }
}
}
impl<'b, W: Write> Drop for JsonDumper<'b, W> {
fn drop(&mut self) {
if let Err(_) = write!(self.output, "{}", as_json(&self.result)) {
error!("Error writing output");
}
}
}
macro_rules! impl_fn {
($fn_name: ident, $data_type: ident, $bucket: ident) => {
fn $fn_name(&mut self, data: $data_type) {
self.result.$bucket.push(From::from(data));
}
}
}
impl<'b, W: Write + 'b> Dump for JsonDumper<'b, W> {
fn crate_prelude(&mut self, data: CratePreludeData) {
self.result.prelude = Some(data)
}
impl_fn!(extern_crate, ExternCrateData, imports);
impl_fn!(use_data, UseData, imports);
impl_fn!(use_glob, UseGlobData, imports);
impl_fn!(enum_data, EnumData, defs);
impl_fn!(tuple_variant, TupleVariantData, defs);
impl_fn!(struct_variant, StructVariantData, defs);
impl_fn!(struct_data, StructData, defs);
impl_fn!(trait_data, TraitData, defs);
impl_fn!(function, FunctionData, defs);
impl_fn!(method, MethodData, defs);
impl_fn!(macro_data, MacroData, defs);
impl_fn!(typedef, TypeDefData, defs);
impl_fn!(variable, VariableData, defs);
impl_fn!(function_ref, FunctionRefData, refs);
impl_fn!(function_call, FunctionCallData, refs);
impl_fn!(method_call, MethodCallData, refs);
impl_fn!(mod_ref, ModRefData, refs);
impl_fn!(type_ref, TypeRefData, refs);
impl_fn!(variable_ref, VariableRefData, refs);
impl_fn!(macro_use, MacroUseData, macro_refs);
fn mod_data(&mut self, data: ModData) {
let id: Id = From::from(data.id);
let mut def = Def {
kind: DefKind::Mod,
id: id,
span: data.span,
name: data.name,
qualname: data.qualname,
value: data.filename,
children: data.items.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
};
if def.span.file_name != def.value {
// If the module is an out-of-line defintion, then we'll make the
// defintion the first character in the module's file and turn the
// the declaration into a reference to it.
let rf = Ref {
kind: RefKind::Mod,
span: def.span,
ref_id: id,
};
self.result.refs.push(rf);
def.span = SpanData {
file_name: def.value.clone(),
byte_start: 0,
byte_end: 0,
line_start: 1,
line_end: 1,
column_start: 1,
column_end: 1,
}
}
self.result.defs.push(def);
}
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.
// FIXME methods. The defs have information about possible overriding and the
// refs have decl information (e.g., a trait method where we know the required
// method, but not the supplied method). In both cases, we are currently
// ignoring it.
#[derive(Debug, RustcEncodable)]
struct Analysis {
kind: Format,
prelude: Option<CratePreludeData>,
imports: Vec<Import>,
defs: Vec<Def>,
refs: Vec<Ref>,
macro_refs: Vec<MacroRef>,
relations: Vec<Relation>,
}
impl Analysis {
fn new() -> Analysis {
Analysis {
kind: Format::Json,
prelude: None,
imports: vec![],
defs: vec![],
refs: vec![],
macro_refs: vec![],
relations: vec![],
}
}
}
// DefId::index is a newtype and so the JSON serialisation is ugly. Therefore
// we use our own Id which is the same, but without the newtype.
#[derive(Clone, Copy, Debug, RustcEncodable)]
struct Id {
krate: u32,
index: u32,
}
impl From<DefId> for Id {
fn from(id: DefId) -> Id {
Id {
krate: id.krate.as_u32(),
index: id.index.as_u32(),
}
}
}
#[derive(Debug, RustcEncodable)]
struct Import {
kind: ImportKind,
ref_id: Option<Id>,
span: SpanData,
name: String,
value: String,
}
#[derive(Debug, RustcEncodable)]
enum ImportKind {
ExternCrate,
Use,
GlobUse,
}
impl From<ExternCrateData> for Import {
fn from(data: ExternCrateData) -> Import {
Import {
kind: ImportKind::ExternCrate,
ref_id: None,
span: data.span,
name: data.name,
value: String::new(),
}
}
}
impl From<UseData> for Import {
fn from(data: UseData) -> Import {
Import {
kind: ImportKind::Use,
ref_id: data.mod_id.map(|id| From::from(id)),
span: data.span,
name: data.name,
value: String::new(),
}
}
}
impl From<UseGlobData> for Import {
fn from(data: UseGlobData) -> Import {
Import {
kind: ImportKind::GlobUse,
ref_id: None,
span: data.span,
name: "*".to_owned(),
value: data.names.join(", "),
}
}
}
#[derive(Debug, RustcEncodable)]
struct Def {
kind: DefKind,
id: Id,
span: SpanData,
name: String,
qualname: String,
value: String,
children: Vec<Id>,
decl_id: Option<Id>,
docs: String,
sig: Option<JsonSignature>,
attributes: Vec<Attribute>,
}
#[derive(Debug, RustcEncodable)]
enum DefKind {
// value = variant names
Enum,
// value = enum name + variant name + types
Tuple,
// value = [enum name +] name + fields
Struct,
// value = signature
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 (for all variable kinds).
Local,
Static,
Const,
Field,
}
impl From<EnumData> for Def {
fn from(data: EnumData) -> Def {
Def {
kind: DefKind::Enum,
id: From::from(data.id),
span: data.span,
name: data.name,
qualname: data.qualname,
value: data.value,
children: data.variants.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
impl From<TupleVariantData> for Def {
fn from(data: TupleVariantData) -> Def {
Def {
kind: DefKind::Tuple,
id: From::from(data.id),
span: data.span,
name: data.name,
qualname: data.qualname,
value: data.value,
children: vec![],
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
impl From<StructVariantData> for Def {
fn from(data: StructVariantData) -> Def {
Def {
kind: DefKind::Struct,
id: From::from(data.id),
span: data.span,
name: data.name,
qualname: data.qualname,
value: data.value,
children: vec![],
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
impl From<StructData> for Def {
fn from(data: StructData) -> Def {
Def {
kind: DefKind::Struct,
id: From::from(data.id),
span: data.span,
name: data.name,
qualname: data.qualname,
value: data.value,
children: data.fields.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
impl From<TraitData> for Def {
fn from(data: TraitData) -> Def {
Def {
kind: DefKind::Trait,
id: From::from(data.id),
span: data.span,
name: data.name,
qualname: data.qualname,
value: data.value,
children: data.items.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
impl From<FunctionData> for Def {
fn from(data: FunctionData) -> Def {
Def {
kind: DefKind::Function,
id: From::from(data.id),
span: data.span,
name: data.name,
qualname: data.qualname,
value: data.value,
children: vec![],
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
impl From<MethodData> for Def {
fn from(data: MethodData) -> Def {
Def {
kind: DefKind::Method,
id: From::from(data.id),
span: data.span,
name: data.name,
qualname: data.qualname,
value: data.value,
children: vec![],
decl_id: data.decl_id.map(|id| From::from(id)),
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
impl From<MacroData> for Def {
fn from(data: MacroData) -> Def {
Def {
kind: DefKind::Macro,
id: From::from(null_def_id()),
span: data.span,
name: data.name,
qualname: data.qualname,
value: String::new(),
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
attributes: vec![],
}
}
}
impl From<TypeDefData> for Def {
fn from(data: TypeDefData) -> Def {
Def {
kind: DefKind::Type,
id: From::from(data.id),
span: data.span,
name: data.name,
qualname: data.qualname,
value: data.value,
children: vec![],
decl_id: None,
docs: String::new(),
sig: data.sig.map(|s| From::from(s)),
attributes: data.attributes,
}
}
}
impl From<VariableData> for Def {
fn from(data: VariableData) -> Def {
Def {
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.type_value,
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
attributes: data.attributes,
}
}
}
#[derive(Debug, RustcEncodable)]
enum RefKind {
Function,
Mod,
Type,
Variable,
}
#[derive(Debug, RustcEncodable)]
struct Ref {
kind: RefKind,
span: SpanData,
ref_id: Id,
}
impl From<FunctionRefData> for Ref {
fn from(data: FunctionRefData) -> Ref {
Ref {
kind: RefKind::Function,
span: data.span,
ref_id: From::from(data.ref_id),
}
}
}
impl From<FunctionCallData> for Ref {
fn from(data: FunctionCallData) -> Ref {
Ref {
kind: RefKind::Function,
span: data.span,
ref_id: From::from(data.ref_id),
}
}
}
impl From<MethodCallData> for Ref {
fn from(data: MethodCallData) -> Ref {
Ref {
kind: RefKind::Function,
span: data.span,
ref_id: From::from(data.ref_id.or(data.decl_id).unwrap_or(null_def_id())),
}
}
}
impl From<ModRefData> for Ref {
fn from(data: ModRefData) -> Ref {
Ref {
kind: RefKind::Mod,
span: data.span,
ref_id: From::from(data.ref_id.unwrap_or(null_def_id())),
}
}
}
impl From<TypeRefData> for Ref {
fn from(data: TypeRefData) -> Ref {
Ref {
kind: RefKind::Type,
span: data.span,
ref_id: From::from(data.ref_id.unwrap_or(null_def_id())),
}
}
}
impl From<VariableRefData> for Ref {
fn from(data: VariableRefData) -> Ref {
Ref {
kind: RefKind::Variable,
span: data.span,
ref_id: From::from(data.ref_id),
}
}
}
#[derive(Debug, RustcEncodable)]
struct MacroRef {
span: SpanData,
qualname: String,
callee_span: SpanData,
}
impl From<MacroUseData> for MacroRef {
fn from(data: MacroUseData) -> MacroRef {
MacroRef {
span: data.span,
qualname: data.qualname,
callee_span: data.callee_span,
}
}
}
#[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,
text: String,
ident_start: usize,
ident_end: usize,
defs: Vec<JsonSigElement>,
refs: Vec<JsonSigElement>,
}
impl From<Signature> for JsonSignature {
fn from(data: Signature) -> JsonSignature {
JsonSignature {
span: data.span,
text: data.text,
ident_start: data.ident_start,
ident_end: data.ident_end,
defs: data.defs.into_iter().map(|s| From::from(s)).collect(),
refs: data.refs.into_iter().map(|s| From::from(s)).collect(),
}
}
}
#[derive(Debug, RustcEncodable)]
pub struct JsonSigElement {
id: Id,
start: usize,
end: usize,
}
impl From<SigElement> for JsonSigElement {
fn from(data: SigElement) -> JsonSigElement {
JsonSigElement {
id: From::from(data.id),
start: data.start,
end: data.end,
}
}
}