Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save-analysis: add signature info #38529

Merged
merged 7 commits into from
Dec 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -372,6 +372,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 @@ -444,6 +445,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 @@ -515,6 +517,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 @@ -587,6 +590,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 @@ -630,11 +634,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 @@ -663,6 +666,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 @@ -684,6 +699,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 @@ -709,6 +725,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 @@ -793,6 +810,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 @@ -1058,6 +1076,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 @@ -1272,10 +1291,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 @@ -1298,6 +1317,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 @@ -1507,6 +1527,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