Skip to content

Commit 4e4306c

Browse files
committed
Thread visibility info through save-analysis and filter save-analysis-api on it.
1 parent c7dfc89 commit 4e4306c

File tree

5 files changed

+235
-122
lines changed

5 files changed

+235
-122
lines changed

src/librustc_save_analysis/data.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
//! The `Dump` trait can be used together with `DumpVisitor` in order to
1414
//! retrieve the data from a crate.
1515
16+
use rustc::hir;
1617
use rustc::hir::def_id::DefId;
17-
use syntax::ast::{CrateNum, NodeId};
18+
use syntax::ast::{self, CrateNum, NodeId};
1819
use syntax_pos::Span;
1920

2021
pub struct CrateData {
@@ -76,6 +77,35 @@ pub enum Data {
7677
VariableRefData(VariableRefData),
7778
}
7879

80+
#[derive(Eq, PartialEq, Clone, Copy, Debug, RustcEncodable)]
81+
pub enum Visibility {
82+
Public,
83+
Restricted,
84+
Inherited,
85+
}
86+
87+
impl<'a> From<&'a ast::Visibility> for Visibility {
88+
fn from(v: &'a ast::Visibility) -> Visibility {
89+
match *v {
90+
ast::Visibility::Public => Visibility::Public,
91+
ast::Visibility::Crate(_) => Visibility::Restricted,
92+
ast::Visibility::Restricted { .. } => Visibility::Restricted,
93+
ast::Visibility::Inherited => Visibility::Inherited,
94+
}
95+
}
96+
}
97+
98+
impl<'a> From<&'a hir::Visibility> for Visibility {
99+
fn from(v: &'a hir::Visibility) -> Visibility {
100+
match *v {
101+
hir::Visibility::Public => Visibility::Public,
102+
hir::Visibility::Crate => Visibility::Restricted,
103+
hir::Visibility::Restricted { .. } => Visibility::Restricted,
104+
hir::Visibility::Inherited => Visibility::Inherited,
105+
}
106+
}
107+
}
108+
79109
/// Data for the prelude of a crate.
80110
#[derive(Debug, RustcEncodable)]
81111
pub struct CratePreludeData {
@@ -103,7 +133,7 @@ pub struct EnumData {
103133
pub span: Span,
104134
pub scope: NodeId,
105135
pub variants: Vec<NodeId>,
106-
136+
pub visibility: Visibility,
107137
}
108138

109139
/// Data for extern crates.
@@ -135,6 +165,7 @@ pub struct FunctionData {
135165
pub span: Span,
136166
pub scope: NodeId,
137167
pub value: String,
168+
pub visibility: Visibility,
138169
}
139170

140171
/// Data about a function call.
@@ -215,6 +246,7 @@ pub struct MethodData {
215246
pub scope: NodeId,
216247
pub value: String,
217248
pub decl_id: Option<DefId>,
249+
pub visibility: Visibility,
218250
}
219251

220252
/// Data for modules.
@@ -227,6 +259,7 @@ pub struct ModData {
227259
pub scope: NodeId,
228260
pub filename: String,
229261
pub items: Vec<NodeId>,
262+
pub visibility: Visibility,
230263
}
231264

232265
/// Data for a reference to a module.
@@ -248,6 +281,7 @@ pub struct StructData {
248281
pub scope: NodeId,
249282
pub value: String,
250283
pub fields: Vec<NodeId>,
284+
pub visibility: Visibility,
251285
}
252286

253287
#[derive(Debug, RustcEncodable)]
@@ -270,6 +304,7 @@ pub struct TraitData {
270304
pub scope: NodeId,
271305
pub value: String,
272306
pub items: Vec<NodeId>,
307+
pub visibility: Visibility,
273308
}
274309

275310
#[derive(Debug, RustcEncodable)]
@@ -291,6 +326,7 @@ pub struct TypeDefData {
291326
pub span: Span,
292327
pub qualname: String,
293328
pub value: String,
329+
pub visibility: Visibility,
294330
}
295331

296332
/// Data for a reference to a type or trait.
@@ -308,15 +344,17 @@ pub struct UseData {
308344
pub span: Span,
309345
pub name: String,
310346
pub mod_id: Option<DefId>,
311-
pub scope: NodeId
347+
pub scope: NodeId,
348+
pub visibility: Visibility,
312349
}
313350

314351
#[derive(Debug, RustcEncodable)]
315352
pub struct UseGlobData {
316353
pub id: NodeId,
317354
pub span: Span,
318355
pub names: Vec<String>,
319-
pub scope: NodeId
356+
pub scope: NodeId,
357+
pub visibility: Visibility,
320358
}
321359

322360
/// Data for local and global variables (consts and statics).
@@ -330,6 +368,7 @@ pub struct VariableData {
330368
pub scope: NodeId,
331369
pub value: String,
332370
pub type_value: String,
371+
pub visibility: Visibility,
333372
}
334373

335374
#[derive(Debug, RustcEncodable)]

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
364364
qualname: format!("{}::{}", qualname, path_to_string(p)),
365365
type_value: typ,
366366
value: String::new(),
367-
scope: 0
367+
scope: 0,
368+
visibility: Visibility::Inherited,
368369
}.lower(self.tcx));
369370
}
370371
}
@@ -376,6 +377,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
376377
body: Option<&ast::Block>,
377378
id: ast::NodeId,
378379
name: ast::Name,
380+
vis: Visibility,
379381
span: Span) {
380382
debug!("process_method: {}:{}", id, name);
381383

@@ -416,6 +418,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
416418
qualname: method_data.qualname.clone(),
417419
value: sig_str,
418420
decl_id: decl_id,
421+
visibility: vis,
419422
}.lower(self.tcx));
420423
}
421424

@@ -483,7 +486,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
483486
name: name,
484487
id: param.id,
485488
qualname: qualname,
486-
value: String::new()
489+
value: String::new(),
490+
visibility: Visibility::Inherited,
487491
}.lower(self.tcx));
488492
}
489493
}
@@ -532,7 +536,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
532536
name: ast::Name,
533537
span: Span,
534538
typ: &ast::Ty,
535-
expr: &ast::Expr) {
539+
expr: &ast::Expr,
540+
vis: Visibility) {
536541
let qualname = format!("::{}", self.tcx.node_path_str(id));
537542

538543
let sub_span = self.span.sub_span_after_keyword(span, keywords::Const);
@@ -546,7 +551,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
546551
qualname: qualname,
547552
value: self.span.snippet(expr.span),
548553
type_value: ty_to_string(&typ),
549-
scope: self.cur_scope
554+
scope: self.cur_scope,
555+
visibility: vis,
550556
}.lower(self.tcx));
551557
}
552558

@@ -588,6 +594,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
588594
scope: self.cur_scope,
589595
value: val,
590596
fields: fields,
597+
visibility: From::from(&item.vis),
591598
}.lower(self.tcx));
592599
}
593600

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

@@ -989,7 +997,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
989997
qualname: format!("{}${}", path_to_string(p), id),
990998
value: value,
991999
type_value: typ,
992-
scope: 0
1000+
scope: 0,
1001+
visibility: Visibility::Inherited,
9931002
}.lower(self.tcx));
9941003
}
9951004
}
@@ -1072,7 +1081,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
10721081
id: item.id,
10731082
mod_id: mod_id,
10741083
name: ident.to_string(),
1075-
scope: self.cur_scope
1084+
scope: self.cur_scope,
1085+
visibility: From::from(&item.vis),
10761086
}.lower(self.tcx));
10771087
}
10781088
self.write_sub_paths_truncated(path, true);
@@ -1095,7 +1105,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
10951105
span: sub_span.expect("No span found for use glob"),
10961106
id: item.id,
10971107
names: names,
1098-
scope: self.cur_scope
1108+
scope: self.cur_scope,
1109+
visibility: From::from(&item.vis),
10991110
}.lower(self.tcx));
11001111
}
11011112
self.write_sub_paths(path, true);
@@ -1167,7 +1178,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
11671178
name: item.ident.to_string(),
11681179
id: item.id,
11691180
qualname: qualname.clone(),
1170-
value: value
1181+
value: value,
1182+
visibility: From::from(&item.vis),
11711183
}.lower(self.tcx));
11721184
}
11731185

@@ -1200,13 +1212,15 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
12001212
trait_item.ident.name,
12011213
trait_item.span,
12021214
&ty,
1203-
&expr);
1215+
&expr,
1216+
Visibility::Public);
12041217
}
12051218
ast::TraitItemKind::Method(ref sig, ref body) => {
12061219
self.process_method(sig,
12071220
body.as_ref().map(|x| &**x),
12081221
trait_item.id,
12091222
trait_item.ident.name,
1223+
Visibility::Public,
12101224
trait_item.span);
12111225
}
12121226
ast::TraitItemKind::Const(_, None) |
@@ -1223,13 +1237,15 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
12231237
impl_item.ident.name,
12241238
impl_item.span,
12251239
&ty,
1226-
&expr);
1240+
&expr,
1241+
From::from(&impl_item.vis));
12271242
}
12281243
ast::ImplItemKind::Method(ref sig, ref body) => {
12291244
self.process_method(sig,
12301245
Some(body),
12311246
impl_item.id,
12321247
impl_item.ident.name,
1248+
From::from(&impl_item.vis),
12331249
impl_item.span);
12341250
}
12351251
ast::ImplItemKind::Type(_) |
@@ -1399,7 +1415,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
13991415
qualname: format!("{}${}", path_to_string(p), id),
14001416
value: value,
14011417
type_value: String::new(),
1402-
scope: 0
1418+
scope: 0,
1419+
visibility: Visibility::Inherited,
14031420
}.lower(self.tcx));
14041421
}
14051422
}

0 commit comments

Comments
 (0)