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::{self, NodeId};
15 use syntax::codemap::CodeMap;
16 use syntax::print::pprust;
19 use data::{self, Visibility};
21 use rls_data::{SpanData, CratePreludeData, Attribute, Signature};
22 use rls_span::{Column, Row};
24 // FIXME: this should be pub(crate), but the current snapshot doesn't allow it yet
27 fn lower(self, tcx: TyCtxt) -> Self::Target;
30 pub fn make_def_id(id: NodeId, map: &Map) -> DefId {
31 map.opt_local_def_id(id).unwrap_or(null_def_id())
34 pub fn null_def_id() -> DefId {
36 krate: CrateNum::from_u32(u32::max_value()),
37 index: DefIndex::from_u32(u32::max_value())
41 pub fn span_from_span(span: Span, cm: &CodeMap) -> SpanData {
42 let start = cm.lookup_char_pos(span.lo);
43 let end = cm.lookup_char_pos(span.hi);
46 file_name: start.file.name.clone().into(),
47 byte_start: span.lo.0,
49 line_start: Row::new_one_indexed(start.line as u32),
50 line_end: Row::new_one_indexed(end.line as u32),
51 column_start: Column::new_one_indexed(start.col.0 as u32 + 1),
52 column_end: Column::new_one_indexed(end.col.0 as u32 + 1),
56 impl Lower for Vec<ast::Attribute> {
57 type Target = Vec<Attribute>;
59 fn lower(self, tcx: TyCtxt) -> Vec<Attribute> {
61 // Only retain real attributes. Doc comments are lowered separately.
62 .filter(|attr| attr.path != "doc")
64 // Remove the surrounding '#[..]' or '#![..]' of the pretty printed
65 // attribute. First normalize all inner attribute (#![..]) to outer
66 // ones (#[..]), then remove the two leading and the one trailing character.
67 attr.style = ast::AttrStyle::Outer;
68 let value = pprust::attribute_to_string(&attr);
69 // This str slicing works correctly, because the leading and trailing characters
70 // are in the ASCII range and thus exactly one byte each.
71 let value = value[2..value.len()-1].to_string();
75 span: span_from_span(attr.span, tcx.sess.codemap()),
81 impl Lower for data::CratePreludeData {
82 type Target = CratePreludeData;
84 fn lower(self, tcx: TyCtxt) -> CratePreludeData {
86 crate_name: self.crate_name,
87 crate_root: self.crate_root,
88 external_crates: self.external_crates,
89 span: span_from_span(self.span, tcx.sess.codemap()),
94 /// Data for enum declarations.
95 #[derive(Clone, Debug)]
100 pub qualname: String,
103 pub variants: Vec<DefId>,
104 pub visibility: Visibility,
106 pub sig: Option<Signature>,
107 pub attributes: Vec<Attribute>,
110 impl Lower for data::EnumData {
111 type Target = EnumData;
113 fn lower(self, tcx: TyCtxt) -> EnumData {
115 id: make_def_id(self.id, &tcx.hir),
118 qualname: self.qualname,
119 span: span_from_span(self.span, tcx.sess.codemap()),
120 scope: make_def_id(self.scope, &tcx.hir),
121 variants: self.variants.into_iter().map(|id| make_def_id(id, &tcx.hir)).collect(),
122 visibility: self.visibility,
125 attributes: self.attributes.lower(tcx),
130 /// Data for extern crates.
132 pub struct ExternCrateData {
135 pub crate_num: CrateNum,
136 pub location: String,
141 impl Lower for data::ExternCrateData {
142 type Target = ExternCrateData;
144 fn lower(self, tcx: TyCtxt) -> ExternCrateData {
146 id: make_def_id(self.id, &tcx.hir),
148 crate_num: self.crate_num,
149 location: self.location,
150 span: span_from_span(self.span, tcx.sess.codemap()),
151 scope: make_def_id(self.scope, &tcx.hir),
156 /// Data about a function call.
158 pub struct FunctionCallData {
164 impl Lower for data::FunctionCallData {
165 type Target = FunctionCallData;
167 fn lower(self, tcx: TyCtxt) -> FunctionCallData {
169 span: span_from_span(self.span, tcx.sess.codemap()),
170 scope: make_def_id(self.scope, &tcx.hir),
176 /// Data for all kinds of functions and methods.
177 #[derive(Clone, Debug)]
178 pub struct FunctionData {
181 pub qualname: String,
182 pub declaration: Option<DefId>,
186 pub visibility: Visibility,
187 pub parent: Option<DefId>,
189 pub sig: Option<Signature>,
190 pub attributes: Vec<Attribute>,
193 impl Lower for data::FunctionData {
194 type Target = FunctionData;
196 fn lower(self, tcx: TyCtxt) -> FunctionData {
198 id: make_def_id(self.id, &tcx.hir),
200 qualname: self.qualname,
201 declaration: self.declaration,
202 span: span_from_span(self.span, tcx.sess.codemap()),
203 scope: make_def_id(self.scope, &tcx.hir),
205 visibility: self.visibility,
209 attributes: self.attributes.lower(tcx),
214 /// Data about a function call.
216 pub struct FunctionRefData {
222 impl Lower for data::FunctionRefData {
223 type Target = FunctionRefData;
225 fn lower(self, tcx: TyCtxt) -> FunctionRefData {
227 span: span_from_span(self.span, tcx.sess.codemap()),
228 scope: make_def_id(self.scope, &tcx.hir),
234 pub struct ImplData {
238 pub trait_ref: Option<DefId>,
239 pub self_ref: Option<DefId>,
242 impl Lower for data::ImplData {
243 type Target = ImplData;
245 fn lower(self, tcx: TyCtxt) -> ImplData {
247 id: make_def_id(self.id, &tcx.hir),
248 span: span_from_span(self.span, tcx.sess.codemap()),
249 scope: make_def_id(self.scope, &tcx.hir),
250 trait_ref: self.trait_ref,
251 self_ref: self.self_ref,
257 pub struct InheritanceData {
263 impl Lower for data::InheritanceData {
264 type Target = InheritanceData;
266 fn lower(self, tcx: TyCtxt) -> InheritanceData {
268 span: span_from_span(self.span, tcx.sess.codemap()),
269 base_id: self.base_id,
270 deriv_id: make_def_id(self.deriv_id, &tcx.hir)
275 /// Data about a macro declaration.
277 pub struct MacroData {
280 pub qualname: String,
284 impl Lower for data::MacroData {
285 type Target = MacroData;
287 fn lower(self, tcx: TyCtxt) -> MacroData {
289 span: span_from_span(self.span, tcx.sess.codemap()),
291 qualname: self.qualname,
297 /// Data about a macro use.
299 pub struct MacroUseData {
302 pub qualname: String,
303 // Because macro expansion happens before ref-ids are determined,
304 // we use the callee span to reference the associated macro definition.
305 pub callee_span: SpanData,
309 impl Lower for data::MacroUseData {
310 type Target = MacroUseData;
312 fn lower(self, tcx: TyCtxt) -> MacroUseData {
314 span: span_from_span(self.span, tcx.sess.codemap()),
316 qualname: self.qualname,
317 callee_span: span_from_span(self.callee_span, tcx.sess.codemap()),
318 scope: make_def_id(self.scope, &tcx.hir),
323 /// Data about a method call.
325 pub struct MethodCallData {
328 pub ref_id: Option<DefId>,
329 pub decl_id: Option<DefId>,
332 impl Lower for data::MethodCallData {
333 type Target = MethodCallData;
335 fn lower(self, tcx: TyCtxt) -> MethodCallData {
337 span: span_from_span(self.span, tcx.sess.codemap()),
338 scope: make_def_id(self.scope, &tcx.hir),
340 decl_id: self.decl_id,
345 /// Data for method declarations (methods with a body are treated as functions).
346 #[derive(Clone, Debug)]
347 pub struct MethodData {
350 pub qualname: String,
354 pub decl_id: Option<DefId>,
355 pub visibility: Visibility,
356 pub parent: Option<DefId>,
358 pub sig: Option<Signature>,
359 pub attributes: Vec<Attribute>,
362 impl Lower for data::MethodData {
363 type Target = MethodData;
365 fn lower(self, tcx: TyCtxt) -> MethodData {
367 span: span_from_span(self.span, tcx.sess.codemap()),
369 scope: make_def_id(self.scope, &tcx.hir),
370 id: make_def_id(self.id, &tcx.hir),
371 qualname: self.qualname,
373 decl_id: self.decl_id,
374 visibility: self.visibility,
378 attributes: self.attributes.lower(tcx),
383 /// Data for modules.
388 pub qualname: String,
391 pub filename: String,
392 pub items: Vec<DefId>,
393 pub visibility: Visibility,
395 pub sig: Option<Signature>,
396 pub attributes: Vec<Attribute>,
399 impl Lower for data::ModData {
400 type Target = ModData;
402 fn lower(self, tcx: TyCtxt) -> ModData {
404 id: make_def_id(self.id, &tcx.hir),
406 qualname: self.qualname,
407 span: span_from_span(self.span, tcx.sess.codemap()),
408 scope: make_def_id(self.scope, &tcx.hir),
409 filename: self.filename,
410 items: self.items.into_iter().map(|id| make_def_id(id, &tcx.hir)).collect(),
411 visibility: self.visibility,
414 attributes: self.attributes.lower(tcx),
419 /// Data for a reference to a module.
421 pub struct ModRefData {
424 pub ref_id: Option<DefId>,
428 impl Lower for data::ModRefData {
429 type Target = ModRefData;
431 fn lower(self, tcx: TyCtxt) -> ModRefData {
433 span: span_from_span(self.span, tcx.sess.codemap()),
434 scope: make_def_id(self.scope, &tcx.hir),
436 qualname: self.qualname,
442 pub struct StructData {
447 pub qualname: String,
450 pub fields: Vec<DefId>,
451 pub visibility: Visibility,
453 pub sig: Option<Signature>,
454 pub attributes: Vec<Attribute>,
457 impl Lower for data::StructData {
458 type Target = StructData;
460 fn lower(self, tcx: TyCtxt) -> StructData {
462 span: span_from_span(self.span, tcx.sess.codemap()),
464 id: make_def_id(self.id, &tcx.hir),
465 ctor_id: make_def_id(self.ctor_id, &tcx.hir),
466 qualname: self.qualname,
467 scope: make_def_id(self.scope, &tcx.hir),
469 fields: self.fields.into_iter().map(|id| make_def_id(id, &tcx.hir)).collect(),
470 visibility: self.visibility,
473 attributes: self.attributes.lower(tcx),
479 pub struct StructVariantData {
483 pub qualname: String,
484 pub type_value: String,
487 pub parent: Option<DefId>,
489 pub sig: Option<Signature>,
490 pub attributes: Vec<Attribute>,
493 impl Lower for data::StructVariantData {
494 type Target = StructVariantData;
496 fn lower(self, tcx: TyCtxt) -> StructVariantData {
498 span: span_from_span(self.span, tcx.sess.codemap()),
500 id: make_def_id(self.id, &tcx.hir),
501 qualname: self.qualname,
502 type_value: self.type_value,
504 scope: make_def_id(self.scope, &tcx.hir),
508 attributes: self.attributes.lower(tcx),
514 pub struct TraitData {
518 pub qualname: String,
521 pub items: Vec<DefId>,
522 pub visibility: Visibility,
524 pub sig: Option<Signature>,
525 pub attributes: Vec<Attribute>,
528 impl Lower for data::TraitData {
529 type Target = TraitData;
531 fn lower(self, tcx: TyCtxt) -> TraitData {
533 span: span_from_span(self.span, tcx.sess.codemap()),
535 id: make_def_id(self.id, &tcx.hir),
536 qualname: self.qualname,
537 scope: make_def_id(self.scope, &tcx.hir),
539 items: self.items.into_iter().map(|id| make_def_id(id, &tcx.hir)).collect(),
540 visibility: self.visibility,
543 attributes: self.attributes.lower(tcx),
549 pub struct TupleVariantData {
553 pub qualname: String,
554 pub type_value: String,
557 pub parent: Option<DefId>,
559 pub sig: Option<Signature>,
560 pub attributes: Vec<Attribute>,
563 impl Lower for data::TupleVariantData {
564 type Target = TupleVariantData;
566 fn lower(self, tcx: TyCtxt) -> TupleVariantData {
568 span: span_from_span(self.span, tcx.sess.codemap()),
569 id: make_def_id(self.id, &tcx.hir),
571 qualname: self.qualname,
572 type_value: self.type_value,
574 scope: make_def_id(self.scope, &tcx.hir),
578 attributes: self.attributes.lower(tcx),
583 /// Data for a typedef.
585 pub struct TypeDefData {
589 pub qualname: String,
591 pub visibility: Visibility,
592 pub parent: Option<DefId>,
594 pub sig: Option<Signature>,
595 pub attributes: Vec<Attribute>,
598 impl Lower for data::TypeDefData {
599 type Target = TypeDefData;
601 fn lower(self, tcx: TyCtxt) -> TypeDefData {
603 id: make_def_id(self.id, &tcx.hir),
605 span: span_from_span(self.span, tcx.sess.codemap()),
606 qualname: self.qualname,
608 visibility: self.visibility,
612 attributes: self.attributes.lower(tcx),
617 /// Data for a reference to a type or trait.
618 #[derive(Clone, Debug)]
619 pub struct TypeRefData {
622 pub ref_id: Option<DefId>,
623 pub qualname: String,
626 impl Lower for data::TypeRefData {
627 type Target = TypeRefData;
629 fn lower(self, tcx: TyCtxt) -> TypeRefData {
631 span: span_from_span(self.span, tcx.sess.codemap()),
632 scope: make_def_id(self.scope, &tcx.hir),
634 qualname: self.qualname,
644 pub mod_id: Option<DefId>,
646 pub visibility: Visibility,
649 impl Lower for data::UseData {
650 type Target = UseData;
652 fn lower(self, tcx: TyCtxt) -> UseData {
654 id: make_def_id(self.id, &tcx.hir),
655 span: span_from_span(self.span, tcx.sess.codemap()),
658 scope: make_def_id(self.scope, &tcx.hir),
659 visibility: self.visibility,
665 pub struct UseGlobData {
668 pub names: Vec<String>,
670 pub visibility: Visibility,
673 impl Lower for data::UseGlobData {
674 type Target = UseGlobData;
676 fn lower(self, tcx: TyCtxt) -> UseGlobData {
678 id: make_def_id(self.id, &tcx.hir),
679 span: span_from_span(self.span, tcx.sess.codemap()),
681 scope: make_def_id(self.scope, &tcx.hir),
682 visibility: self.visibility,
687 /// Data for local and global variables (consts and statics).
689 pub struct VariableData {
692 pub kind: data::VariableKind,
693 pub qualname: String,
697 pub type_value: String,
698 pub parent: Option<DefId>,
699 pub visibility: Visibility,
701 pub sig: Option<Signature>,
702 pub attributes: Vec<Attribute>,
705 impl Lower for data::VariableData {
706 type Target = VariableData;
708 fn lower(self, tcx: TyCtxt) -> VariableData {
710 id: make_def_id(self.id, &tcx.hir),
713 qualname: self.qualname,
714 span: span_from_span(self.span, tcx.sess.codemap()),
715 scope: make_def_id(self.scope, &tcx.hir),
717 type_value: self.type_value,
719 visibility: self.visibility,
722 attributes: self.attributes.lower(tcx),
727 /// Data for the use of some item (e.g., the use of a local variable, which
728 /// will refer to that variables declaration (by ref_id)).
730 pub struct VariableRefData {
737 impl Lower for data::VariableRefData {
738 type Target = VariableRefData;
740 fn lower(self, tcx: TyCtxt) -> VariableRefData {
743 span: span_from_span(self.span, tcx.sess.codemap()),
744 scope: make_def_id(self.scope, &tcx.hir),