Skip to content

Commit

Permalink
Auto merge of #38529 - nrc:save-sig, r=nikomatsakis
Browse files Browse the repository at this point in the history
save-analysis: add signature info

These 'signatures' for definitions contain enough info for the RLS to create Rustdoc-style info on the fly.
  • Loading branch information
bors committed Dec 23, 2016
2 parents ce4461f + c24b192 commit 00b4019
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 16 deletions.
35 changes: 35 additions & 0 deletions src/librustc_save_analysis/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub struct EnumData {
pub variants: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}

/// Data for extern crates.
Expand Down Expand Up @@ -169,6 +170,7 @@ pub struct FunctionData {
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
}

/// Data about a function call.
Expand Down Expand Up @@ -253,6 +255,7 @@ pub struct MethodData {
pub parent: Option<DefId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}

/// Data for modules.
Expand All @@ -267,6 +270,7 @@ pub struct ModData {
pub items: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}

/// Data for a reference to a module.
Expand All @@ -290,6 +294,7 @@ pub struct StructData {
pub fields: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}

#[derive(Debug, RustcEncodable)]
Expand All @@ -303,6 +308,7 @@ pub struct StructVariantData {
pub scope: NodeId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
}

#[derive(Debug, RustcEncodable)]
Expand All @@ -316,6 +322,7 @@ pub struct TraitData {
pub items: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}

#[derive(Debug, RustcEncodable)]
Expand All @@ -329,6 +336,7 @@ pub struct TupleVariantData {
pub scope: NodeId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
}

/// Data for a typedef.
Expand All @@ -342,6 +350,7 @@ pub struct TypeDefData {
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Option<Signature>,
}

/// Data for a reference to a type or trait.
Expand Down Expand Up @@ -386,6 +395,7 @@ pub struct VariableData {
pub type_value: String,
pub visibility: Visibility,
pub docs: String,
pub sig: Option<Signature>,
}

#[derive(Debug, RustcEncodable)]
Expand All @@ -405,3 +415,28 @@ pub struct VariableRefData {
pub scope: NodeId,
pub ref_id: DefId,
}


/// Encodes information about the signature of a definition. This should have
/// enough information to create a nice display about a definition without
/// access to the source code.
#[derive(Clone, Debug, RustcEncodable)]
pub struct Signature {
pub span: Span,
pub text: String,
// These identify the main identifier for the defintion as byte offsets into
// `text`. E.g., of `foo` in `pub fn foo(...)`
pub ident_start: usize,
pub ident_end: usize,
pub defs: Vec<SigElement>,
pub refs: Vec<SigElement>,
}

/// An element of a signature. `start` and `end` are byte offsets into the `text`
/// of the parent `Signature`.
#[derive(Clone, Debug, RustcEncodable)]
pub struct SigElement {
pub id: DefId,
pub start: usize,
pub end: usize,
}
33 changes: 27 additions & 6 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
parent: None,
visibility: Visibility::Inherited,
docs: String::new(),
sig: None,
}.lower(self.tcx));
}
}
Expand Down Expand Up @@ -429,6 +430,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
parent: trait_id,
visibility: vis,
docs: docs_for_attrs(attrs),
sig: method_data.sig,
}.lower(self.tcx));
}

Expand Down Expand Up @@ -500,6 +502,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
visibility: Visibility::Inherited,
parent: None,
docs: String::new(),
sig: None,
}.lower(self.tcx));
}
}
Expand Down Expand Up @@ -572,6 +575,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
parent: Some(parent_id),
visibility: vis,
docs: docs_for_attrs(attrs),
sig: None,
}.lower(self.tcx));
}

Expand Down Expand Up @@ -615,11 +619,10 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
fields: fields,
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: self.save_ctxt.sig_base(item),
}.lower(self.tcx));
}


// fields
for field in def.fields() {
self.process_struct_field_def(field, item.id);
self.visit_ty(&field.ty);
Expand Down Expand Up @@ -648,6 +651,18 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
qualname.push_str("::");
qualname.push_str(&name);

let text = self.span.signature_string_for_span(variant.span);
let ident_start = text.find(&name).unwrap();
let ident_end = ident_start + name.len();
let sig = Signature {
span: variant.span,
text: text,
ident_start: ident_start,
ident_end: ident_end,
defs: vec![],
refs: vec![],
};

match variant.node.data {
ast::VariantData::Struct(ref fields, _) => {
let sub_span = self.span.span_for_first_ident(variant.span);
Expand All @@ -669,6 +684,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
scope: enum_data.scope,
parent: Some(make_def_id(item.id, &self.tcx.map)),
docs: docs_for_attrs(&variant.node.attrs),
sig: sig,
}.lower(self.tcx));
}
}
Expand All @@ -694,6 +710,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
scope: enum_data.scope,
parent: Some(make_def_id(item.id, &self.tcx.map)),
docs: docs_for_attrs(&variant.node.attrs),
sig: sig,
}.lower(self.tcx));
}
}
Expand Down Expand Up @@ -778,6 +795,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
items: methods.iter().map(|i| i.id).collect(),
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: self.save_ctxt.sig_base(item),
}.lower(self.tcx));
}

Expand Down Expand Up @@ -1043,6 +1061,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
parent: None,
visibility: Visibility::Inherited,
docs: String::new(),
sig: None,
}.lower(self.tcx));
}
}
Expand Down Expand Up @@ -1257,10 +1276,10 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
Struct(ref def, ref ty_params) => self.process_struct(item, def, ty_params),
Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
Impl(..,
ref ty_params,
ref trait_ref,
ref typ,
ref impl_items) => {
ref ty_params,
ref trait_ref,
ref typ,
ref impl_items) => {
self.process_impl(item, ty_params, trait_ref, &typ, impl_items)
}
Trait(_, ref generics, ref trait_refs, ref methods) =>
Expand All @@ -1283,6 +1302,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
visibility: From::from(&item.vis),
parent: None,
docs: docs_for_attrs(&item.attrs),
sig: Some(self.save_ctxt.sig_base(item)),
}.lower(self.tcx));
}

Expand Down Expand Up @@ -1492,6 +1512,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
parent: None,
visibility: Visibility::Inherited,
docs: String::new(),
sig: None,
}.lower(self.tcx));
}
}
Expand Down
Loading

0 comments on commit 00b4019

Please sign in to comment.