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};
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,
433 impl Lower for data::StructData {
434 type Target = StructData;
436 fn lower(self, tcx: TyCtxt) -> StructData {
438 span: SpanData::from_span(self.span, tcx.sess.codemap()),
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),
445 fields: self.fields.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
446 visibility: self.visibility,
452 #[derive(Debug, RustcEncodable)]
453 pub struct StructVariantData {
457 pub qualname: String,
458 pub type_value: String,
461 pub parent: Option<DefId>,
465 impl Lower for data::StructVariantData {
466 type Target = StructVariantData;
468 fn lower(self, tcx: TyCtxt) -> StructVariantData {
470 span: SpanData::from_span(self.span, tcx.sess.codemap()),
472 id: make_def_id(self.id, &tcx.map),
473 qualname: self.qualname,
474 type_value: self.type_value,
476 scope: make_def_id(self.scope, &tcx.map),
483 #[derive(Debug, RustcEncodable)]
484 pub struct TraitData {
488 pub qualname: String,
491 pub items: Vec<DefId>,
492 pub visibility: Visibility,
496 impl Lower for data::TraitData {
497 type Target = TraitData;
499 fn lower(self, tcx: TyCtxt) -> TraitData {
501 span: SpanData::from_span(self.span, tcx.sess.codemap()),
503 id: make_def_id(self.id, &tcx.map),
504 qualname: self.qualname,
505 scope: make_def_id(self.scope, &tcx.map),
507 items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
508 visibility: self.visibility,
514 #[derive(Debug, RustcEncodable)]
515 pub struct TupleVariantData {
519 pub qualname: String,
520 pub type_value: String,
523 pub parent: Option<DefId>,
527 impl Lower for data::TupleVariantData {
528 type Target = TupleVariantData;
530 fn lower(self, tcx: TyCtxt) -> TupleVariantData {
532 span: SpanData::from_span(self.span, tcx.sess.codemap()),
533 id: make_def_id(self.id, &tcx.map),
535 qualname: self.qualname,
536 type_value: self.type_value,
538 scope: make_def_id(self.scope, &tcx.map),
545 /// Data for a typedef.
546 #[derive(Debug, RustcEncodable)]
547 pub struct TypeDefData {
551 pub qualname: String,
553 pub visibility: Visibility,
554 pub parent: Option<DefId>,
558 impl Lower for data::TypeDefData {
559 type Target = TypeDefData;
561 fn lower(self, tcx: TyCtxt) -> TypeDefData {
563 id: make_def_id(self.id, &tcx.map),
565 span: SpanData::from_span(self.span, tcx.sess.codemap()),
566 qualname: self.qualname,
568 visibility: self.visibility,
575 /// Data for a reference to a type or trait.
576 #[derive(Clone, Debug, RustcEncodable)]
577 pub struct TypeRefData {
580 pub ref_id: Option<DefId>,
581 pub qualname: String,
584 impl Lower for data::TypeRefData {
585 type Target = TypeRefData;
587 fn lower(self, tcx: TyCtxt) -> TypeRefData {
589 span: SpanData::from_span(self.span, tcx.sess.codemap()),
590 scope: make_def_id(self.scope, &tcx.map),
592 qualname: self.qualname,
597 #[derive(Debug, RustcEncodable)]
602 pub mod_id: Option<DefId>,
604 pub visibility: Visibility,
607 impl Lower for data::UseData {
608 type Target = UseData;
610 fn lower(self, tcx: TyCtxt) -> UseData {
612 id: make_def_id(self.id, &tcx.map),
613 span: SpanData::from_span(self.span, tcx.sess.codemap()),
616 scope: make_def_id(self.scope, &tcx.map),
617 visibility: self.visibility,
622 #[derive(Debug, RustcEncodable)]
623 pub struct UseGlobData {
626 pub names: Vec<String>,
628 pub visibility: Visibility,
631 impl Lower for data::UseGlobData {
632 type Target = UseGlobData;
634 fn lower(self, tcx: TyCtxt) -> UseGlobData {
636 id: make_def_id(self.id, &tcx.map),
637 span: SpanData::from_span(self.span, tcx.sess.codemap()),
639 scope: make_def_id(self.scope, &tcx.map),
640 visibility: self.visibility,
645 /// Data for local and global variables (consts and statics).
646 #[derive(Debug, RustcEncodable)]
647 pub struct VariableData {
650 pub kind: data::VariableKind,
651 pub qualname: String,
655 pub type_value: String,
656 pub parent: Option<DefId>,
657 pub visibility: Visibility,
661 impl Lower for data::VariableData {
662 type Target = VariableData;
664 fn lower(self, tcx: TyCtxt) -> VariableData {
666 id: make_def_id(self.id, &tcx.map),
669 qualname: self.qualname,
670 span: SpanData::from_span(self.span, tcx.sess.codemap()),
671 scope: make_def_id(self.scope, &tcx.map),
673 type_value: self.type_value,
675 visibility: self.visibility,
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 {
691 impl Lower for data::VariableRefData {
692 type Target = VariableRefData;
694 fn lower(self, tcx: TyCtxt) -> VariableRefData {
697 span: SpanData::from_span(self.span, tcx.sess.codemap()),
698 scope: make_def_id(self.scope, &tcx.map),