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.
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.
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;
18 use data::{self, Visibility, SigElement};
20 // FIXME: this should be pub(crate), but the current snapshot doesn't allow it yet
23 fn lower(self, tcx: TyCtxt) -> Self::Target;
26 pub fn make_def_id(id: NodeId, map: &Map) -> DefId {
27 map.opt_local_def_id(id).unwrap_or(null_def_id())
30 pub fn null_def_id() -> DefId {
32 krate: CrateNum::from_u32(u32::max_value()),
33 index: DefIndex::from_u32(u32::max_value())
37 #[derive(Clone, Debug, RustcEncodable)]
39 pub file_name: String,
43 pub line_start: usize,
45 /// 1-based, character offset.
46 pub column_start: usize,
47 pub column_end: usize,
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);
56 file_name: start.file.name.clone(),
57 byte_start: span.lo.0,
59 line_start: start.line,
61 column_start: start.col.0 + 1,
62 column_end: end.col.0 + 1,
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>,
75 impl Lower for data::CratePreludeData {
76 type Target = CratePreludeData;
78 fn lower(self, tcx: TyCtxt) -> 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()),
88 /// Data for enum declarations.
89 #[derive(Clone, Debug, RustcEncodable)]
97 pub variants: Vec<DefId>,
98 pub visibility: Visibility,
102 impl Lower for data::EnumData {
103 type Target = EnumData;
105 fn lower(self, tcx: TyCtxt) -> EnumData {
107 id: make_def_id(self.id, &tcx.map),
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,
120 /// Data for extern crates.
121 #[derive(Debug, RustcEncodable)]
122 pub struct ExternCrateData {
125 pub crate_num: CrateNum,
126 pub location: String,
131 impl Lower for data::ExternCrateData {
132 type Target = ExternCrateData;
134 fn lower(self, tcx: TyCtxt) -> ExternCrateData {
136 id: make_def_id(self.id, &tcx.map),
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),
146 /// Data about a function call.
147 #[derive(Debug, RustcEncodable)]
148 pub struct FunctionCallData {
154 impl Lower for data::FunctionCallData {
155 type Target = FunctionCallData;
157 fn lower(self, tcx: TyCtxt) -> FunctionCallData {
159 span: SpanData::from_span(self.span, tcx.sess.codemap()),
160 scope: make_def_id(self.scope, &tcx.map),
166 /// Data for all kinds of functions and methods.
167 #[derive(Clone, Debug, RustcEncodable)]
168 pub struct FunctionData {
171 pub qualname: String,
172 pub declaration: Option<DefId>,
176 pub visibility: Visibility,
177 pub parent: Option<DefId>,
181 impl Lower for data::FunctionData {
182 type Target = FunctionData;
184 fn lower(self, tcx: TyCtxt) -> FunctionData {
186 id: make_def_id(self.id, &tcx.map),
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),
193 visibility: self.visibility,
200 /// Data about a function call.
201 #[derive(Debug, RustcEncodable)]
202 pub struct FunctionRefData {
208 impl Lower for data::FunctionRefData {
209 type Target = FunctionRefData;
211 fn lower(self, tcx: TyCtxt) -> FunctionRefData {
213 span: SpanData::from_span(self.span, tcx.sess.codemap()),
214 scope: make_def_id(self.scope, &tcx.map),
219 #[derive(Debug, RustcEncodable)]
220 pub struct ImplData {
224 pub trait_ref: Option<DefId>,
225 pub self_ref: Option<DefId>,
228 impl Lower for data::ImplData {
229 type Target = ImplData;
231 fn lower(self, tcx: TyCtxt) -> 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,
242 #[derive(Debug, RustcEncodable)]
243 pub struct InheritanceData {
249 impl Lower for data::InheritanceData {
250 type Target = InheritanceData;
252 fn lower(self, tcx: TyCtxt) -> 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)
261 /// Data about a macro declaration.
262 #[derive(Debug, RustcEncodable)]
263 pub struct MacroData {
266 pub qualname: String,
270 impl Lower for data::MacroData {
271 type Target = MacroData;
273 fn lower(self, tcx: TyCtxt) -> MacroData {
275 span: SpanData::from_span(self.span, tcx.sess.codemap()),
277 qualname: self.qualname,
283 /// Data about a macro use.
284 #[derive(Debug, RustcEncodable)]
285 pub struct MacroUseData {
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,
295 impl Lower for data::MacroUseData {
296 type Target = MacroUseData;
298 fn lower(self, tcx: TyCtxt) -> MacroUseData {
300 span: SpanData::from_span(self.span, tcx.sess.codemap()),
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),
309 /// Data about a method call.
310 #[derive(Debug, RustcEncodable)]
311 pub struct MethodCallData {
314 pub ref_id: Option<DefId>,
315 pub decl_id: Option<DefId>,
318 impl Lower for data::MethodCallData {
319 type Target = MethodCallData;
321 fn lower(self, tcx: TyCtxt) -> MethodCallData {
323 span: SpanData::from_span(self.span, tcx.sess.codemap()),
324 scope: make_def_id(self.scope, &tcx.map),
326 decl_id: self.decl_id,
331 /// Data for method declarations (methods with a body are treated as functions).
332 #[derive(Clone, Debug, RustcEncodable)]
333 pub struct MethodData {
336 pub qualname: String,
340 pub decl_id: Option<DefId>,
341 pub visibility: Visibility,
342 pub parent: Option<DefId>,
346 impl Lower for data::MethodData {
347 type Target = MethodData;
349 fn lower(self, tcx: TyCtxt) -> MethodData {
351 span: SpanData::from_span(self.span, tcx.sess.codemap()),
353 scope: make_def_id(self.scope, &tcx.map),
354 id: make_def_id(self.id, &tcx.map),
355 qualname: self.qualname,
357 decl_id: self.decl_id,
358 visibility: self.visibility,
365 /// Data for modules.
366 #[derive(Debug, RustcEncodable)]
370 pub qualname: String,
373 pub filename: String,
374 pub items: Vec<DefId>,
375 pub visibility: Visibility,
379 impl Lower for data::ModData {
380 type Target = ModData;
382 fn lower(self, tcx: TyCtxt) -> ModData {
384 id: make_def_id(self.id, &tcx.map),
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,
397 /// Data for a reference to a module.
398 #[derive(Debug, RustcEncodable)]
399 pub struct ModRefData {
402 pub ref_id: Option<DefId>,
406 impl Lower for data::ModRefData {
407 type Target = ModRefData;
409 fn lower(self, tcx: TyCtxt) -> ModRefData {
411 span: SpanData::from_span(self.span, tcx.sess.codemap()),
412 scope: make_def_id(self.scope, &tcx.map),
414 qualname: self.qualname,
419 #[derive(Debug, RustcEncodable)]
420 pub struct StructData {
425 pub qualname: String,
428 pub fields: Vec<DefId>,
429 pub visibility: Visibility,
434 impl Lower for data::StructData {
435 type Target = StructData;
437 fn lower(self, tcx: TyCtxt) -> StructData {
439 span: SpanData::from_span(self.span, tcx.sess.codemap()),
441 id: make_def_id(self.id, &tcx.map),
442 ctor_id: make_def_id(self.ctor_id, &tcx.map),
443 qualname: self.qualname,
444 scope: make_def_id(self.scope, &tcx.map),
446 fields: self.fields.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
447 visibility: self.visibility,
449 sig: self.sig.lower(tcx),
454 #[derive(Debug, RustcEncodable)]
455 pub struct StructVariantData {
459 pub qualname: String,
460 pub type_value: String,
463 pub parent: Option<DefId>,
467 impl Lower for data::StructVariantData {
468 type Target = StructVariantData;
470 fn lower(self, tcx: TyCtxt) -> StructVariantData {
472 span: SpanData::from_span(self.span, tcx.sess.codemap()),
474 id: make_def_id(self.id, &tcx.map),
475 qualname: self.qualname,
476 type_value: self.type_value,
478 scope: make_def_id(self.scope, &tcx.map),
485 #[derive(Debug, RustcEncodable)]
486 pub struct TraitData {
490 pub qualname: String,
493 pub items: Vec<DefId>,
494 pub visibility: Visibility,
498 impl Lower for data::TraitData {
499 type Target = TraitData;
501 fn lower(self, tcx: TyCtxt) -> TraitData {
503 span: SpanData::from_span(self.span, tcx.sess.codemap()),
505 id: make_def_id(self.id, &tcx.map),
506 qualname: self.qualname,
507 scope: make_def_id(self.scope, &tcx.map),
509 items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
510 visibility: self.visibility,
516 #[derive(Debug, RustcEncodable)]
517 pub struct TupleVariantData {
521 pub qualname: String,
522 pub type_value: String,
525 pub parent: Option<DefId>,
529 impl Lower for data::TupleVariantData {
530 type Target = TupleVariantData;
532 fn lower(self, tcx: TyCtxt) -> TupleVariantData {
534 span: SpanData::from_span(self.span, tcx.sess.codemap()),
535 id: make_def_id(self.id, &tcx.map),
537 qualname: self.qualname,
538 type_value: self.type_value,
540 scope: make_def_id(self.scope, &tcx.map),
547 /// Data for a typedef.
548 #[derive(Debug, RustcEncodable)]
549 pub struct TypeDefData {
553 pub qualname: String,
555 pub visibility: Visibility,
556 pub parent: Option<DefId>,
560 impl Lower for data::TypeDefData {
561 type Target = TypeDefData;
563 fn lower(self, tcx: TyCtxt) -> TypeDefData {
565 id: make_def_id(self.id, &tcx.map),
567 span: SpanData::from_span(self.span, tcx.sess.codemap()),
568 qualname: self.qualname,
570 visibility: self.visibility,
577 /// Data for a reference to a type or trait.
578 #[derive(Clone, Debug, RustcEncodable)]
579 pub struct TypeRefData {
582 pub ref_id: Option<DefId>,
583 pub qualname: String,
586 impl Lower for data::TypeRefData {
587 type Target = TypeRefData;
589 fn lower(self, tcx: TyCtxt) -> TypeRefData {
591 span: SpanData::from_span(self.span, tcx.sess.codemap()),
592 scope: make_def_id(self.scope, &tcx.map),
594 qualname: self.qualname,
599 #[derive(Debug, RustcEncodable)]
604 pub mod_id: Option<DefId>,
606 pub visibility: Visibility,
609 impl Lower for data::UseData {
610 type Target = UseData;
612 fn lower(self, tcx: TyCtxt) -> UseData {
614 id: make_def_id(self.id, &tcx.map),
615 span: SpanData::from_span(self.span, tcx.sess.codemap()),
618 scope: make_def_id(self.scope, &tcx.map),
619 visibility: self.visibility,
624 #[derive(Debug, RustcEncodable)]
625 pub struct UseGlobData {
628 pub names: Vec<String>,
630 pub visibility: Visibility,
633 impl Lower for data::UseGlobData {
634 type Target = UseGlobData;
636 fn lower(self, tcx: TyCtxt) -> UseGlobData {
638 id: make_def_id(self.id, &tcx.map),
639 span: SpanData::from_span(self.span, tcx.sess.codemap()),
641 scope: make_def_id(self.scope, &tcx.map),
642 visibility: self.visibility,
647 /// Data for local and global variables (consts and statics).
648 #[derive(Debug, RustcEncodable)]
649 pub struct VariableData {
652 pub kind: data::VariableKind,
653 pub qualname: String,
657 pub type_value: String,
658 pub parent: Option<DefId>,
659 pub visibility: Visibility,
663 impl Lower for data::VariableData {
664 type Target = VariableData;
666 fn lower(self, tcx: TyCtxt) -> VariableData {
668 id: make_def_id(self.id, &tcx.map),
671 qualname: self.qualname,
672 span: SpanData::from_span(self.span, tcx.sess.codemap()),
673 scope: make_def_id(self.scope, &tcx.map),
675 type_value: self.type_value,
677 visibility: self.visibility,
683 /// Data for the use of some item (e.g., the use of a local variable, which
684 /// will refer to that variables declaration (by ref_id)).
685 #[derive(Debug, RustcEncodable)]
686 pub struct VariableRefData {
693 impl Lower for data::VariableRefData {
694 type Target = VariableRefData;
696 fn lower(self, tcx: TyCtxt) -> VariableRefData {
699 span: SpanData::from_span(self.span, tcx.sess.codemap()),
700 scope: make_def_id(self.scope, &tcx.map),
706 #[derive(Debug, RustcEncodable)]
707 pub struct Signature {
710 // These identify the main identifier for the defintion as byte offsets into
711 // `text`. E.g., of `foo` in `pub fn foo(...)`
712 pub ident_start: usize,
713 pub ident_end: usize,
714 pub defs: Vec<SigElement>,
715 pub refs: Vec<SigElement>,
718 impl Lower for data::Signature {
719 type Target = Signature;
721 fn lower(self, tcx: TyCtxt) -> Signature {
723 span: SpanData::from_span(self.span, tcx.sess.codemap()),
725 ident_start: self.ident_start,
726 ident_end: self.ident_end,