Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
save-analysis: add Signature info to structs
  • Loading branch information
nrc committed Dec 22, 2016
1 parent c217ab6 commit 5a6ca7a
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 6 deletions.
26 changes: 26 additions & 0 deletions src/librustc_save_analysis/data.rs
Expand Up @@ -290,6 +290,7 @@ pub struct StructData {
pub fields: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}

#[derive(Debug, RustcEncodable)]
Expand Down Expand Up @@ -405,3 +406,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(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(Debug, RustcEncodable)]
pub struct SigElement {
pub id: DefId,
pub start: usize,
pub end: usize,
}
18 changes: 16 additions & 2 deletions src/librustc_save_analysis/dump_visitor.rs
Expand Up @@ -619,6 +619,9 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
};

if !self.span.filter_generated(sub_span, item.span) {
let mut sig = self.sig_base(item);
sig.ident_start = sig.text.find(&name).expect("Name not in struct signature?");
sig.ident_end = sig.ident_start + name.len();
self.dumper.struct_data(StructData {
span: sub_span.expect("No span found for struct"),
id: item.id,
Expand All @@ -630,11 +633,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: sig,
}.lower(self.tcx));
}


// fields
for field in def.fields() {
self.process_struct_field_def(field, item.id);
self.visit_ty(&field.ty);
Expand All @@ -643,6 +645,18 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
self.process_generic_params(ty_params, item.span, &qualname, item.id);
}

fn sig_base(&self, item: &ast::Item) -> Signature {
let text = self.span.signature_string_for_span(item.span).expect("Couldn't make signature");
Signature {
span: mk_sp(item.span.lo, item.span.lo + BytePos(text.len() as u32)),
text: text,
ident_start: 0,
ident_end: 0,
defs: vec![],
refs: vec![],
}
}

fn process_enum(&mut self,
item: &'l ast::Item,
enum_definition: &'l ast::EnumDef,
Expand Down
31 changes: 30 additions & 1 deletion src/librustc_save_analysis/external_data.rs
Expand Up @@ -15,7 +15,7 @@ use syntax::ast::NodeId;
use syntax::codemap::CodeMap;
use syntax_pos::Span;

use data::{self, Visibility};
use data::{self, Visibility, SigElement};

// FIXME: this should be pub(crate), but the current snapshot doesn't allow it yet
pub trait Lower {
Expand Down Expand Up @@ -428,6 +428,7 @@ pub struct StructData {
pub fields: Vec<DefId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}

impl Lower for data::StructData {
Expand All @@ -445,6 +446,7 @@ impl Lower for data::StructData {
fields: self.fields.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
visibility: self.visibility,
docs: self.docs,
sig: self.sig.lower(tcx),
}
}
}
Expand Down Expand Up @@ -700,3 +702,30 @@ impl Lower for data::VariableRefData {
}
}
}

#[derive(Debug, RustcEncodable)]
pub struct Signature {
pub span: SpanData,
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>,
}

impl Lower for data::Signature {
type Target = Signature;

fn lower(self, tcx: TyCtxt) -> Signature {
Signature {
span: SpanData::from_span(self.span, tcx.sess.codemap()),
text: self.text,
ident_start: self.ident_start,
ident_end: self.ident_end,
defs: self.defs,
refs: self.refs,
}
}
}
12 changes: 12 additions & 0 deletions src/librustc_save_analysis/json_api_dumper.rs
Expand Up @@ -179,6 +179,7 @@ struct Def {
children: Vec<Id>,
decl_id: Option<Id>,
docs: String,
sig: Option<Signature>,
}

#[derive(Debug, RustcEncodable)]
Expand Down Expand Up @@ -221,6 +222,7 @@ impl From<EnumData> for Option<Def> {
children: data.variants.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: None,
}),
_ => None,
}
Expand All @@ -240,6 +242,7 @@ impl From<TupleVariantData> for Option<Def> {
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
})
}
}
Expand All @@ -256,6 +259,7 @@ impl From<StructVariantData> for Option<Def> {
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
})
}
}
Expand All @@ -273,6 +277,7 @@ impl From<StructData> for Option<Def> {
children: data.fields.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: Some(data.sig),
}),
_ => None,
}
Expand All @@ -292,6 +297,7 @@ impl From<TraitData> for Option<Def> {
parent: None,
decl_id: None,
docs: data.docs,
sig: None,
}),
_ => None,
}
Expand All @@ -311,6 +317,7 @@ impl From<FunctionData> for Option<Def> {
parent: data.parent.map(|id| From::from(id)),
decl_id: None,
docs: data.docs,
sig: None,
}),
_ => None,
}
Expand All @@ -330,6 +337,7 @@ impl From<MethodData> for Option<Def> {
parent: data.parent.map(|id| From::from(id)),
decl_id: data.decl_id.map(|id| From::from(id)),
docs: data.docs,
sig: None,
}),
_ => None,
}
Expand All @@ -348,6 +356,7 @@ impl From<MacroData> for Option<Def> {
parent: None,
decl_id: None,
docs: data.docs,
sig: None,
})
}
}
Expand All @@ -365,6 +374,7 @@ impl From<ModData> for Option<Def> {
parent: None,
decl_id: None,
docs: data.docs,
sig: None,
}),
_ => None,
}
Expand All @@ -384,6 +394,7 @@ impl From<TypeDefData> for Option<Def> {
parent: data.parent.map(|id| From::from(id)),
decl_id: None,
docs: String::new(),
sig: None,
}),
_ => None,
}
Expand All @@ -408,6 +419,7 @@ impl From<VariableData> for Option<Def> {
parent: data.parent.map(|id| From::from(id)),
decl_id: None,
docs: data.docs,
sig: None,
}),
_ => None,
}
Expand Down
13 changes: 12 additions & 1 deletion src/librustc_save_analysis/json_dumper.rs
Expand Up @@ -86,6 +86,7 @@ impl<'b, W: Write + 'b> Dump for JsonDumper<'b, W> {
children: data.items.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: None,
};
if def.span.file_name != def.value {
// If the module is an out-of-line defintion, then we'll make the
Expand Down Expand Up @@ -223,6 +224,7 @@ struct Def {
children: Vec<Id>,
decl_id: Option<Id>,
docs: String,
sig: Option<Signature>,
}

#[derive(Debug, RustcEncodable)]
Expand Down Expand Up @@ -264,6 +266,7 @@ impl From<EnumData> for Def {
children: data.variants.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: None,
}
}
}
Expand All @@ -280,6 +283,7 @@ impl From<TupleVariantData> for Def {
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
}
}
}
Expand All @@ -295,6 +299,7 @@ impl From<StructVariantData> for Def {
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
}
}
}
Expand All @@ -310,6 +315,7 @@ impl From<StructData> for Def {
children: data.fields.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: Some(data.sig),
}
}
}
Expand All @@ -325,6 +331,7 @@ impl From<TraitData> for Def {
children: data.items.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: None,
}
}
}
Expand All @@ -340,6 +347,7 @@ impl From<FunctionData> for Def {
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
}
}
}
Expand All @@ -355,6 +363,7 @@ impl From<MethodData> for Def {
children: vec![],
decl_id: data.decl_id.map(|id| From::from(id)),
docs: data.docs,
sig: None,
}
}
}
Expand All @@ -370,10 +379,10 @@ impl From<MacroData> for Def {
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
}
}
}

impl From<TypeDefData> for Def {
fn from(data: TypeDefData) -> Def {
Def {
Expand All @@ -386,6 +395,7 @@ impl From<TypeDefData> for Def {
children: vec![],
decl_id: None,
docs: String::new(),
sig: None,
}
}
}
Expand All @@ -406,6 +416,7 @@ impl From<VariableData> for Def {
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
}
}
}
Expand Down
45 changes: 43 additions & 2 deletions src/librustc_save_analysis/span_utils.rs
Expand Up @@ -18,8 +18,7 @@ use std::path::Path;

use syntax::ast;
use syntax::parse::lexer::{self, Reader, StringReader};
use syntax::parse::token::{self, Token};
use syntax::symbol::keywords;
use syntax::tokenstream::TokenTree;
use syntax_pos::*;

#[derive(Clone)]
Expand Down Expand Up @@ -87,6 +86,12 @@ impl<'a> SpanUtils<'a> {
lexer::StringReader::new(s.diagnostic(), filemap)
}

fn span_to_tts(&self, span: Span) -> Vec<TokenTree> {
let srdr = self.retokenise_span(span);
let mut p = Parser::new(&self.sess.parse_sess, Box::new(srdr));
p.parse_all_token_trees().expect("Couldn't re-parse span")
}

// Re-parses a path and returns the span for the last identifier in the path
pub fn span_for_last_ident(&self, span: Span) -> Option<Span> {
let mut result = None;
Expand Down Expand Up @@ -308,6 +313,42 @@ impl<'a> SpanUtils<'a> {
}
}

/// `span` must be the span for an item such as a function or struct. This
/// function returns the program text from the start of the span until the
/// end of the 'signature' part, that is up to, but not including an opening
/// brace or semicolon.
pub fn signature_string_for_span(&self, span: Span) -> Option<String> {
let mut toks = self.span_to_tts(span).into_iter();
let mut prev = toks.next().unwrap();
let first_span = prev.get_span();
let mut angle_count = 0;
for tok in toks {
if let TokenTree::Token(_, ref tok) = prev {
angle_count += match *tok {
token::Eof => { return None; }
token::Lt => 1,
token::Gt => -1,
token::BinOp(token::Shl) => 2,
token::BinOp(token::Shr) => -2,
_ => 0,
};
}
if angle_count > 0 {
prev = tok;
continue;
}
if let TokenTree::Token(_, token::Semi) = tok {
return Some(self.snippet(mk_sp(first_span.lo, prev.get_span().hi)));
} else if let TokenTree::Delimited(_, ref d) = tok {
if d.delim == token::Brace {
return Some(self.snippet(mk_sp(first_span.lo, prev.get_span().hi)));
}
}
prev = tok;
}
None
}

pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {
let mut toks = self.retokenise_span(span);
let mut prev = toks.real_token();
Expand Down

0 comments on commit 5a6ca7a

Please sign in to comment.