]> git.lizzy.rs Git - rust.git/blob - src/librustc_save_analysis/external_data.rs
5847575742342680efc3ed8742c77c1f44c8f436
[rust.git] / src / librustc_save_analysis / external_data.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc::hir::def_id::{CrateNum, DefId, DefIndex};
12 use rustc::hir::map::Map;
13 use rustc::ty::TyCtxt;
14 use syntax::ast::NodeId;
15 use syntax::codemap::CodeMap;
16 use syntax_pos::Span;
17
18 use data::{self, Visibility};
19
20 // FIXME: this should be pub(crate), but the current snapshot doesn't allow it yet
21 pub trait Lower {
22     type Target;
23     fn lower(self, tcx: TyCtxt) -> Self::Target;
24 }
25
26 pub fn make_def_id(id: NodeId, map: &Map) -> DefId {
27     map.opt_local_def_id(id).unwrap_or(null_def_id())
28 }
29
30 pub fn null_def_id() -> DefId {
31     DefId {
32         krate: CrateNum::from_u32(u32::max_value()),
33         index: DefIndex::from_u32(u32::max_value())
34     }
35 }
36
37 #[derive(Clone, Debug, RustcEncodable)]
38 pub struct SpanData {
39     pub file_name: String,
40     pub byte_start: u32,
41     pub byte_end: u32,
42     /// 1-based.
43     pub line_start: usize,
44     pub line_end: usize,
45     /// 1-based, character offset.
46     pub column_start: usize,
47     pub column_end: usize,
48 }
49
50 impl SpanData {
51     pub fn from_span(span: Span, cm: &CodeMap) -> SpanData {
52         let start = cm.lookup_char_pos(span.lo);
53         let end = cm.lookup_char_pos(span.hi);
54
55         SpanData {
56             file_name: start.file.name.clone(),
57             byte_start: span.lo.0,
58             byte_end: span.hi.0,
59             line_start: start.line,
60             line_end: end.line,
61             column_start: start.col.0 + 1,
62             column_end: end.col.0 + 1,
63         }
64     }
65 }
66
67 #[derive(Debug, RustcEncodable)]
68 pub struct CratePreludeData {
69     pub crate_name: String,
70     pub crate_root: String,
71     pub external_crates: Vec<data::ExternalCrateData>,
72     pub span: SpanData,
73 }
74
75 impl Lower for data::CratePreludeData {
76     type Target = CratePreludeData;
77
78     fn lower(self, tcx: TyCtxt) -> CratePreludeData {
79         CratePreludeData {
80             crate_name: self.crate_name,
81             crate_root: self.crate_root,
82             external_crates: self.external_crates,
83             span: SpanData::from_span(self.span, tcx.sess.codemap()),
84         }
85     }
86 }
87
88 /// Data for enum declarations.
89 #[derive(Clone, Debug, RustcEncodable)]
90 pub struct EnumData {
91     pub id: DefId,
92     pub value: String,
93     pub name: String,
94     pub qualname: String,
95     pub span: SpanData,
96     pub scope: DefId,
97     pub variants: Vec<DefId>,
98     pub visibility: Visibility,
99     pub docs: String,
100 }
101
102 impl Lower for data::EnumData {
103     type Target = EnumData;
104
105     fn lower(self, tcx: TyCtxt) -> EnumData {
106         EnumData {
107             id: make_def_id(self.id, &tcx.map),
108             name: self.name,
109             value: self.value,
110             qualname: self.qualname,
111             span: SpanData::from_span(self.span, tcx.sess.codemap()),
112             scope: make_def_id(self.scope, &tcx.map),
113             variants: self.variants.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
114             visibility: self.visibility,
115             docs: self.docs,
116         }
117     }
118 }
119
120 /// Data for extern crates.
121 #[derive(Debug, RustcEncodable)]
122 pub struct ExternCrateData {
123     pub id: DefId,
124     pub name: String,
125     pub crate_num: CrateNum,
126     pub location: String,
127     pub span: SpanData,
128     pub scope: DefId,
129 }
130
131 impl Lower for data::ExternCrateData {
132     type Target = ExternCrateData;
133
134     fn lower(self, tcx: TyCtxt) -> ExternCrateData {
135         ExternCrateData {
136             id: make_def_id(self.id, &tcx.map),
137             name: self.name,
138             crate_num: self.crate_num,
139             location: self.location,
140             span: SpanData::from_span(self.span, tcx.sess.codemap()),
141             scope: make_def_id(self.scope, &tcx.map),
142         }
143     }
144 }
145
146 /// Data about a function call.
147 #[derive(Debug, RustcEncodable)]
148 pub struct FunctionCallData {
149     pub span: SpanData,
150     pub scope: DefId,
151     pub ref_id: DefId,
152 }
153
154 impl Lower for data::FunctionCallData {
155     type Target = FunctionCallData;
156
157     fn lower(self, tcx: TyCtxt) -> FunctionCallData {
158         FunctionCallData {
159             span: SpanData::from_span(self.span, tcx.sess.codemap()),
160             scope: make_def_id(self.scope, &tcx.map),
161             ref_id: self.ref_id,
162         }
163     }
164 }
165
166 /// Data for all kinds of functions and methods.
167 #[derive(Clone, Debug, RustcEncodable)]
168 pub struct FunctionData {
169     pub id: DefId,
170     pub name: String,
171     pub qualname: String,
172     pub declaration: Option<DefId>,
173     pub span: SpanData,
174     pub scope: DefId,
175     pub value: String,
176     pub visibility: Visibility,
177     pub parent: Option<DefId>,
178     pub docs: String,
179 }
180
181 impl Lower for data::FunctionData {
182     type Target = FunctionData;
183
184     fn lower(self, tcx: TyCtxt) -> FunctionData {
185         FunctionData {
186             id: make_def_id(self.id, &tcx.map),
187             name: self.name,
188             qualname: self.qualname,
189             declaration: self.declaration,
190             span: SpanData::from_span(self.span, tcx.sess.codemap()),
191             scope: make_def_id(self.scope, &tcx.map),
192             value: self.value,
193             visibility: self.visibility,
194             parent: self.parent,
195             docs: self.docs,
196         }
197     }
198 }
199
200 /// Data about a function call.
201 #[derive(Debug, RustcEncodable)]
202 pub struct FunctionRefData {
203     pub span: SpanData,
204     pub scope: DefId,
205     pub ref_id: DefId,
206 }
207
208 impl Lower for data::FunctionRefData {
209     type Target = FunctionRefData;
210
211     fn lower(self, tcx: TyCtxt) -> FunctionRefData {
212         FunctionRefData {
213             span: SpanData::from_span(self.span, tcx.sess.codemap()),
214             scope: make_def_id(self.scope, &tcx.map),
215             ref_id: self.ref_id,
216         }
217     }
218 }
219 #[derive(Debug, RustcEncodable)]
220 pub struct ImplData {
221     pub id: DefId,
222     pub span: SpanData,
223     pub scope: DefId,
224     pub trait_ref: Option<DefId>,
225     pub self_ref: Option<DefId>,
226 }
227
228 impl Lower for data::ImplData {
229     type Target = ImplData;
230
231     fn lower(self, tcx: TyCtxt) -> ImplData {
232         ImplData {
233             id: make_def_id(self.id, &tcx.map),
234             span: SpanData::from_span(self.span, tcx.sess.codemap()),
235             scope: make_def_id(self.scope, &tcx.map),
236             trait_ref: self.trait_ref,
237             self_ref: self.self_ref,
238         }
239     }
240 }
241
242 #[derive(Debug, RustcEncodable)]
243 pub struct InheritanceData {
244     pub span: SpanData,
245     pub base_id: DefId,
246     pub deriv_id: DefId
247 }
248
249 impl Lower for data::InheritanceData {
250     type Target = InheritanceData;
251
252     fn lower(self, tcx: TyCtxt) -> InheritanceData {
253         InheritanceData {
254             span: SpanData::from_span(self.span, tcx.sess.codemap()),
255             base_id: self.base_id,
256             deriv_id: make_def_id(self.deriv_id, &tcx.map)
257         }
258     }
259 }
260
261 /// Data about a macro declaration.
262 #[derive(Debug, RustcEncodable)]
263 pub struct MacroData {
264     pub span: SpanData,
265     pub name: String,
266     pub qualname: String,
267     pub docs: String,
268 }
269
270 impl Lower for data::MacroData {
271     type Target = MacroData;
272
273     fn lower(self, tcx: TyCtxt) -> MacroData {
274         MacroData {
275             span: SpanData::from_span(self.span, tcx.sess.codemap()),
276             name: self.name,
277             qualname: self.qualname,
278             docs: self.docs,
279         }
280     }
281 }
282
283 /// Data about a macro use.
284 #[derive(Debug, RustcEncodable)]
285 pub struct MacroUseData {
286     pub span: SpanData,
287     pub name: String,
288     pub qualname: String,
289     // Because macro expansion happens before ref-ids are determined,
290     // we use the callee span to reference the associated macro definition.
291     pub callee_span: SpanData,
292     pub scope: DefId,
293 }
294
295 impl Lower for data::MacroUseData {
296     type Target = MacroUseData;
297
298     fn lower(self, tcx: TyCtxt) -> MacroUseData {
299         MacroUseData {
300             span: SpanData::from_span(self.span, tcx.sess.codemap()),
301             name: self.name,
302             qualname: self.qualname,
303             callee_span: SpanData::from_span(self.callee_span, tcx.sess.codemap()),
304             scope: make_def_id(self.scope, &tcx.map),
305         }
306     }
307 }
308
309 /// Data about a method call.
310 #[derive(Debug, RustcEncodable)]
311 pub struct MethodCallData {
312     pub span: SpanData,
313     pub scope: DefId,
314     pub ref_id: Option<DefId>,
315     pub decl_id: Option<DefId>,
316 }
317
318 impl Lower for data::MethodCallData {
319     type Target = MethodCallData;
320
321     fn lower(self, tcx: TyCtxt) -> MethodCallData {
322         MethodCallData {
323             span: SpanData::from_span(self.span, tcx.sess.codemap()),
324             scope: make_def_id(self.scope, &tcx.map),
325             ref_id: self.ref_id,
326             decl_id: self.decl_id,
327         }
328     }
329 }
330
331 /// Data for method declarations (methods with a body are treated as functions).
332 #[derive(Clone, Debug, RustcEncodable)]
333 pub struct MethodData {
334     pub id: DefId,
335     pub name: String,
336     pub qualname: String,
337     pub span: SpanData,
338     pub scope: DefId,
339     pub value: String,
340     pub decl_id: Option<DefId>,
341     pub visibility: Visibility,
342     pub parent: Option<DefId>,
343     pub docs: String,
344 }
345
346 impl Lower for data::MethodData {
347     type Target = MethodData;
348
349     fn lower(self, tcx: TyCtxt) -> MethodData {
350         MethodData {
351             span: SpanData::from_span(self.span, tcx.sess.codemap()),
352             name: self.name,
353             scope: make_def_id(self.scope, &tcx.map),
354             id: make_def_id(self.id, &tcx.map),
355             qualname: self.qualname,
356             value: self.value,
357             decl_id: self.decl_id,
358             visibility: self.visibility,
359             parent: self.parent,
360             docs: self.docs,
361         }
362     }
363 }
364
365 /// Data for modules.
366 #[derive(Debug, RustcEncodable)]
367 pub struct ModData {
368     pub id: DefId,
369     pub name: String,
370     pub qualname: String,
371     pub span: SpanData,
372     pub scope: DefId,
373     pub filename: String,
374     pub items: Vec<DefId>,
375     pub visibility: Visibility,
376     pub docs: String,
377 }
378
379 impl Lower for data::ModData {
380     type Target = ModData;
381
382     fn lower(self, tcx: TyCtxt) -> ModData {
383         ModData {
384             id: make_def_id(self.id, &tcx.map),
385             name: self.name,
386             qualname: self.qualname,
387             span: SpanData::from_span(self.span, tcx.sess.codemap()),
388             scope: make_def_id(self.scope, &tcx.map),
389             filename: self.filename,
390             items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
391             visibility: self.visibility,
392             docs: self.docs,
393         }
394     }
395 }
396
397 /// Data for a reference to a module.
398 #[derive(Debug, RustcEncodable)]
399 pub struct ModRefData {
400     pub span: SpanData,
401     pub scope: DefId,
402     pub ref_id: Option<DefId>,
403     pub qualname: String
404 }
405
406 impl Lower for data::ModRefData {
407     type Target = ModRefData;
408
409     fn lower(self, tcx: TyCtxt) -> ModRefData {
410         ModRefData {
411             span: SpanData::from_span(self.span, tcx.sess.codemap()),
412             scope: make_def_id(self.scope, &tcx.map),
413             ref_id: self.ref_id,
414             qualname: self.qualname,
415         }
416     }
417 }
418
419 #[derive(Debug, RustcEncodable)]
420 pub struct StructData {
421     pub span: SpanData,
422     pub name: String,
423     pub id: DefId,
424     pub ctor_id: DefId,
425     pub qualname: String,
426     pub scope: DefId,
427     pub value: String,
428     pub fields: Vec<DefId>,
429     pub visibility: Visibility,
430     pub docs: String,
431 }
432
433 impl Lower for data::StructData {
434     type Target = StructData;
435
436     fn lower(self, tcx: TyCtxt) -> StructData {
437         StructData {
438             span: SpanData::from_span(self.span, tcx.sess.codemap()),
439             name: self.name,
440             id: make_def_id(self.id, &tcx.map),
441             ctor_id: make_def_id(self.ctor_id, &tcx.map),
442             qualname: self.qualname,
443             scope: make_def_id(self.scope, &tcx.map),
444             value: self.value,
445             fields: self.fields.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
446             visibility: self.visibility,
447             docs: self.docs,
448         }
449     }
450 }
451
452 #[derive(Debug, RustcEncodable)]
453 pub struct StructVariantData {
454     pub span: SpanData,
455     pub name: String,
456     pub id: DefId,
457     pub qualname: String,
458     pub type_value: String,
459     pub value: String,
460     pub scope: DefId,
461     pub parent: Option<DefId>,
462     pub docs: String,
463 }
464
465 impl Lower for data::StructVariantData {
466     type Target = StructVariantData;
467
468     fn lower(self, tcx: TyCtxt) -> StructVariantData {
469         StructVariantData {
470             span: SpanData::from_span(self.span, tcx.sess.codemap()),
471             name: self.name,
472             id: make_def_id(self.id, &tcx.map),
473             qualname: self.qualname,
474             type_value: self.type_value,
475             value: self.value,
476             scope: make_def_id(self.scope, &tcx.map),
477             parent: self.parent,
478             docs: self.docs,
479         }
480     }
481 }
482
483 #[derive(Debug, RustcEncodable)]
484 pub struct TraitData {
485     pub span: SpanData,
486     pub name: String,
487     pub id: DefId,
488     pub qualname: String,
489     pub scope: DefId,
490     pub value: String,
491     pub items: Vec<DefId>,
492     pub visibility: Visibility,
493     pub docs: String,
494 }
495
496 impl Lower for data::TraitData {
497     type Target = TraitData;
498
499     fn lower(self, tcx: TyCtxt) -> TraitData {
500         TraitData {
501             span: SpanData::from_span(self.span, tcx.sess.codemap()),
502             name: self.name,
503             id: make_def_id(self.id, &tcx.map),
504             qualname: self.qualname,
505             scope: make_def_id(self.scope, &tcx.map),
506             value: self.value,
507             items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
508             visibility: self.visibility,
509             docs: self.docs,
510         }
511     }
512 }
513
514 #[derive(Debug, RustcEncodable)]
515 pub struct TupleVariantData {
516     pub span: SpanData,
517     pub id: DefId,
518     pub name: String,
519     pub qualname: String,
520     pub type_value: String,
521     pub value: String,
522     pub scope: DefId,
523     pub parent: Option<DefId>,
524     pub docs: String,
525 }
526
527 impl Lower for data::TupleVariantData {
528     type Target = TupleVariantData;
529
530     fn lower(self, tcx: TyCtxt) -> TupleVariantData {
531         TupleVariantData {
532             span: SpanData::from_span(self.span, tcx.sess.codemap()),
533             id: make_def_id(self.id, &tcx.map),
534             name: self.name,
535             qualname: self.qualname,
536             type_value: self.type_value,
537             value: self.value,
538             scope: make_def_id(self.scope, &tcx.map),
539             parent: self.parent,
540             docs: self.docs,
541         }
542     }
543 }
544
545 /// Data for a typedef.
546 #[derive(Debug, RustcEncodable)]
547 pub struct TypeDefData {
548     pub id: DefId,
549     pub name: String,
550     pub span: SpanData,
551     pub qualname: String,
552     pub value: String,
553     pub visibility: Visibility,
554     pub parent: Option<DefId>,
555     pub docs: String,
556 }
557
558 impl Lower for data::TypeDefData {
559     type Target = TypeDefData;
560
561     fn lower(self, tcx: TyCtxt) -> TypeDefData {
562         TypeDefData {
563             id: make_def_id(self.id, &tcx.map),
564             name: self.name,
565             span: SpanData::from_span(self.span, tcx.sess.codemap()),
566             qualname: self.qualname,
567             value: self.value,
568             visibility: self.visibility,
569             parent: self.parent,
570             docs: self.docs,
571         }
572     }
573 }
574
575 /// Data for a reference to a type or trait.
576 #[derive(Clone, Debug, RustcEncodable)]
577 pub struct TypeRefData {
578     pub span: SpanData,
579     pub scope: DefId,
580     pub ref_id: Option<DefId>,
581     pub qualname: String,
582 }
583
584 impl Lower for data::TypeRefData {
585     type Target = TypeRefData;
586
587     fn lower(self, tcx: TyCtxt) -> TypeRefData {
588         TypeRefData {
589             span: SpanData::from_span(self.span, tcx.sess.codemap()),
590             scope: make_def_id(self.scope, &tcx.map),
591             ref_id: self.ref_id,
592             qualname: self.qualname,
593         }
594     }
595 }
596
597 #[derive(Debug, RustcEncodable)]
598 pub struct UseData {
599     pub id: DefId,
600     pub span: SpanData,
601     pub name: String,
602     pub mod_id: Option<DefId>,
603     pub scope: DefId,
604     pub visibility: Visibility,
605 }
606
607 impl Lower for data::UseData {
608     type Target = UseData;
609
610     fn lower(self, tcx: TyCtxt) -> UseData {
611         UseData {
612             id: make_def_id(self.id, &tcx.map),
613             span: SpanData::from_span(self.span, tcx.sess.codemap()),
614             name: self.name,
615             mod_id: self.mod_id,
616             scope: make_def_id(self.scope, &tcx.map),
617             visibility: self.visibility,
618         }
619     }
620 }
621
622 #[derive(Debug, RustcEncodable)]
623 pub struct UseGlobData {
624     pub id: DefId,
625     pub span: SpanData,
626     pub names: Vec<String>,
627     pub scope: DefId,
628     pub visibility: Visibility,
629 }
630
631 impl Lower for data::UseGlobData {
632     type Target = UseGlobData;
633
634     fn lower(self, tcx: TyCtxt) -> UseGlobData {
635         UseGlobData {
636             id: make_def_id(self.id, &tcx.map),
637             span: SpanData::from_span(self.span, tcx.sess.codemap()),
638             names: self.names,
639             scope: make_def_id(self.scope, &tcx.map),
640             visibility: self.visibility,
641         }
642     }
643 }
644
645 /// Data for local and global variables (consts and statics).
646 #[derive(Debug, RustcEncodable)]
647 pub struct VariableData {
648     pub id: DefId,
649     pub name: String,
650     pub kind: data::VariableKind,
651     pub qualname: String,
652     pub span: SpanData,
653     pub scope: DefId,
654     pub value: String,
655     pub type_value: String,
656     pub parent: Option<DefId>,
657     pub visibility: Visibility,
658     pub docs: String,
659 }
660
661 impl Lower for data::VariableData {
662     type Target = VariableData;
663
664     fn lower(self, tcx: TyCtxt) -> VariableData {
665         VariableData {
666             id: make_def_id(self.id, &tcx.map),
667             kind: self.kind,
668             name: self.name,
669             qualname: self.qualname,
670             span: SpanData::from_span(self.span, tcx.sess.codemap()),
671             scope: make_def_id(self.scope, &tcx.map),
672             value: self.value,
673             type_value: self.type_value,
674             parent: self.parent,
675             visibility: self.visibility,
676             docs: self.docs,
677         }
678     }
679 }
680
681 /// Data for the use of some item (e.g., the use of a local variable, which
682 /// will refer to that variables declaration (by ref_id)).
683 #[derive(Debug, RustcEncodable)]
684 pub struct VariableRefData {
685     pub name: String,
686     pub span: SpanData,
687     pub scope: DefId,
688     pub ref_id: DefId,
689 }
690
691 impl Lower for data::VariableRefData {
692     type Target = VariableRefData;
693
694     fn lower(self, tcx: TyCtxt) -> VariableRefData {
695         VariableRefData {
696             name: self.name,
697             span: SpanData::from_span(self.span, tcx.sess.codemap()),
698             scope: make_def_id(self.scope, &tcx.map),
699             ref_id: self.ref_id,
700         }
701     }
702 }