Skip to content

Commit

Permalink
Thread visibility info through save-analysis and filter save-analysis…
Browse files Browse the repository at this point in the history
…-api on it.
  • Loading branch information
nrc committed Sep 1, 2016
1 parent c7dfc89 commit 4e4306c
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 122 deletions.
47 changes: 43 additions & 4 deletions src/librustc_save_analysis/data.rs
Expand Up @@ -13,8 +13,9 @@
//! The `Dump` trait can be used together with `DumpVisitor` in order to
//! retrieve the data from a crate.

use rustc::hir;
use rustc::hir::def_id::DefId;
use syntax::ast::{CrateNum, NodeId};
use syntax::ast::{self, CrateNum, NodeId};
use syntax_pos::Span;

pub struct CrateData {
Expand Down Expand Up @@ -76,6 +77,35 @@ pub enum Data {
VariableRefData(VariableRefData),
}

#[derive(Eq, PartialEq, Clone, Copy, Debug, RustcEncodable)]
pub enum Visibility {
Public,
Restricted,
Inherited,
}

impl<'a> From<&'a ast::Visibility> for Visibility {
fn from(v: &'a ast::Visibility) -> Visibility {
match *v {
ast::Visibility::Public => Visibility::Public,
ast::Visibility::Crate(_) => Visibility::Restricted,
ast::Visibility::Restricted { .. } => Visibility::Restricted,
ast::Visibility::Inherited => Visibility::Inherited,
}
}
}

impl<'a> From<&'a hir::Visibility> for Visibility {
fn from(v: &'a hir::Visibility) -> Visibility {
match *v {
hir::Visibility::Public => Visibility::Public,
hir::Visibility::Crate => Visibility::Restricted,
hir::Visibility::Restricted { .. } => Visibility::Restricted,
hir::Visibility::Inherited => Visibility::Inherited,
}
}
}

/// Data for the prelude of a crate.
#[derive(Debug, RustcEncodable)]
pub struct CratePreludeData {
Expand Down Expand Up @@ -103,7 +133,7 @@ pub struct EnumData {
pub span: Span,
pub scope: NodeId,
pub variants: Vec<NodeId>,

pub visibility: Visibility,
}

/// Data for extern crates.
Expand Down Expand Up @@ -135,6 +165,7 @@ pub struct FunctionData {
pub span: Span,
pub scope: NodeId,
pub value: String,
pub visibility: Visibility,
}

/// Data about a function call.
Expand Down Expand Up @@ -215,6 +246,7 @@ pub struct MethodData {
pub scope: NodeId,
pub value: String,
pub decl_id: Option<DefId>,
pub visibility: Visibility,
}

/// Data for modules.
Expand All @@ -227,6 +259,7 @@ pub struct ModData {
pub scope: NodeId,
pub filename: String,
pub items: Vec<NodeId>,
pub visibility: Visibility,
}

/// Data for a reference to a module.
Expand All @@ -248,6 +281,7 @@ pub struct StructData {
pub scope: NodeId,
pub value: String,
pub fields: Vec<NodeId>,
pub visibility: Visibility,
}

#[derive(Debug, RustcEncodable)]
Expand All @@ -270,6 +304,7 @@ pub struct TraitData {
pub scope: NodeId,
pub value: String,
pub items: Vec<NodeId>,
pub visibility: Visibility,
}

#[derive(Debug, RustcEncodable)]
Expand All @@ -291,6 +326,7 @@ pub struct TypeDefData {
pub span: Span,
pub qualname: String,
pub value: String,
pub visibility: Visibility,
}

/// Data for a reference to a type or trait.
Expand All @@ -308,15 +344,17 @@ pub struct UseData {
pub span: Span,
pub name: String,
pub mod_id: Option<DefId>,
pub scope: NodeId
pub scope: NodeId,
pub visibility: Visibility,
}

#[derive(Debug, RustcEncodable)]
pub struct UseGlobData {
pub id: NodeId,
pub span: Span,
pub names: Vec<String>,
pub scope: NodeId
pub scope: NodeId,
pub visibility: Visibility,
}

/// Data for local and global variables (consts and statics).
Expand All @@ -330,6 +368,7 @@ pub struct VariableData {
pub scope: NodeId,
pub value: String,
pub type_value: String,
pub visibility: Visibility,
}

#[derive(Debug, RustcEncodable)]
Expand Down
39 changes: 28 additions & 11 deletions src/librustc_save_analysis/dump_visitor.rs
Expand Up @@ -364,7 +364,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
qualname: format!("{}::{}", qualname, path_to_string(p)),
type_value: typ,
value: String::new(),
scope: 0
scope: 0,
visibility: Visibility::Inherited,
}.lower(self.tcx));
}
}
Expand All @@ -376,6 +377,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
body: Option<&ast::Block>,
id: ast::NodeId,
name: ast::Name,
vis: Visibility,
span: Span) {
debug!("process_method: {}:{}", id, name);

Expand Down Expand Up @@ -416,6 +418,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
qualname: method_data.qualname.clone(),
value: sig_str,
decl_id: decl_id,
visibility: vis,
}.lower(self.tcx));
}

Expand Down Expand Up @@ -483,7 +486,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
name: name,
id: param.id,
qualname: qualname,
value: String::new()
value: String::new(),
visibility: Visibility::Inherited,
}.lower(self.tcx));
}
}
Expand Down Expand Up @@ -532,7 +536,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
name: ast::Name,
span: Span,
typ: &ast::Ty,
expr: &ast::Expr) {
expr: &ast::Expr,
vis: Visibility) {
let qualname = format!("::{}", self.tcx.node_path_str(id));

let sub_span = self.span.sub_span_after_keyword(span, keywords::Const);
Expand All @@ -546,7 +551,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
qualname: qualname,
value: self.span.snippet(expr.span),
type_value: ty_to_string(&typ),
scope: self.cur_scope
scope: self.cur_scope,
visibility: vis,
}.lower(self.tcx));
}

Expand Down Expand Up @@ -588,6 +594,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
scope: self.cur_scope,
value: val,
fields: fields,
visibility: From::from(&item.vis),
}.lower(self.tcx));
}

Expand Down Expand Up @@ -744,6 +751,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
scope: self.cur_scope,
value: val,
items: methods.iter().map(|i| i.id).collect(),
visibility: From::from(&item.vis),
}.lower(self.tcx));
}

Expand Down Expand Up @@ -989,7 +997,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
qualname: format!("{}${}", path_to_string(p), id),
value: value,
type_value: typ,
scope: 0
scope: 0,
visibility: Visibility::Inherited,
}.lower(self.tcx));
}
}
Expand Down Expand Up @@ -1072,7 +1081,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
id: item.id,
mod_id: mod_id,
name: ident.to_string(),
scope: self.cur_scope
scope: self.cur_scope,
visibility: From::from(&item.vis),
}.lower(self.tcx));
}
self.write_sub_paths_truncated(path, true);
Expand All @@ -1095,7 +1105,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
span: sub_span.expect("No span found for use glob"),
id: item.id,
names: names,
scope: self.cur_scope
scope: self.cur_scope,
visibility: From::from(&item.vis),
}.lower(self.tcx));
}
self.write_sub_paths(path, true);
Expand Down Expand Up @@ -1167,7 +1178,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
name: item.ident.to_string(),
id: item.id,
qualname: qualname.clone(),
value: value
value: value,
visibility: From::from(&item.vis),
}.lower(self.tcx));
}

Expand Down Expand Up @@ -1200,13 +1212,15 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
trait_item.ident.name,
trait_item.span,
&ty,
&expr);
&expr,
Visibility::Public);
}
ast::TraitItemKind::Method(ref sig, ref body) => {
self.process_method(sig,
body.as_ref().map(|x| &**x),
trait_item.id,
trait_item.ident.name,
Visibility::Public,
trait_item.span);
}
ast::TraitItemKind::Const(_, None) |
Expand All @@ -1223,13 +1237,15 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
impl_item.ident.name,
impl_item.span,
&ty,
&expr);
&expr,
From::from(&impl_item.vis));
}
ast::ImplItemKind::Method(ref sig, ref body) => {
self.process_method(sig,
Some(body),
impl_item.id,
impl_item.ident.name,
From::from(&impl_item.vis),
impl_item.span);
}
ast::ImplItemKind::Type(_) |
Expand Down Expand Up @@ -1399,7 +1415,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
qualname: format!("{}${}", path_to_string(p), id),
value: value,
type_value: String::new(),
scope: 0
scope: 0,
visibility: Visibility::Inherited,
}.lower(self.tcx));
}
}
Expand Down

0 comments on commit 4e4306c

Please sign in to comment.