Skip to content

Commit 4dc7b58

Browse files
committed
save-analysis: add parent info to api dumps
The parent id is used for constructing rustdoc URLs by clients
1 parent 4e4306c commit 4dc7b58

File tree

7 files changed

+113
-70
lines changed

7 files changed

+113
-70
lines changed

src/librustc_driver/driver.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ fn keep_hygiene_data(sess: &Session) -> bool {
248248
fn keep_ast(sess: &Session) -> bool {
249249
sess.opts.debugging_opts.keep_ast ||
250250
sess.opts.debugging_opts.save_analysis ||
251-
sess.opts.debugging_opts.save_analysis_csv
251+
sess.opts.debugging_opts.save_analysis_csv ||
252+
sess.opts.debugging_opts.save_analysis_api
252253
}
253254

254255
/// The name used for source code that doesn't originate in a file

src/librustc_save_analysis/data.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ pub struct FunctionData {
166166
pub scope: NodeId,
167167
pub value: String,
168168
pub visibility: Visibility,
169+
pub parent: Option<NodeId>,
169170
}
170171

171172
/// Data about a function call.
@@ -292,7 +293,8 @@ pub struct StructVariantData {
292293
pub qualname: String,
293294
pub type_value: String,
294295
pub value: String,
295-
pub scope: NodeId
296+
pub scope: NodeId,
297+
pub parent: Option<NodeId>,
296298
}
297299

298300
#[derive(Debug, RustcEncodable)]
@@ -315,7 +317,8 @@ pub struct TupleVariantData {
315317
pub qualname: String,
316318
pub type_value: String,
317319
pub value: String,
318-
pub scope: NodeId
320+
pub scope: NodeId,
321+
pub parent: Option<NodeId>,
319322
}
320323

321324
/// Data for a typedef.
@@ -327,6 +330,7 @@ pub struct TypeDefData {
327330
pub qualname: String,
328331
pub value: String,
329332
pub visibility: Visibility,
333+
pub parent: Option<NodeId>,
330334
}
331335

332336
/// Data for a reference to a type or trait.
@@ -366,6 +370,7 @@ pub struct VariableData {
366370
pub qualname: String,
367371
pub span: Span,
368372
pub scope: NodeId,
373+
pub parent: Option<NodeId>,
369374
pub value: String,
370375
pub type_value: String,
371376
pub visibility: Visibility,

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
365365
type_value: typ,
366366
value: String::new(),
367367
scope: 0,
368+
parent: None,
368369
visibility: Visibility::Inherited,
369370
}.lower(self.tcx));
370371
}
@@ -488,6 +489,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
488489
qualname: qualname,
489490
value: String::new(),
490491
visibility: Visibility::Inherited,
492+
parent: None,
491493
}.lower(self.tcx));
492494
}
493495
}
@@ -531,13 +533,14 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
531533
self.visit_expr(expr);
532534
}
533535

534-
fn process_const(&mut self,
535-
id: ast::NodeId,
536-
name: ast::Name,
537-
span: Span,
538-
typ: &ast::Ty,
539-
expr: &ast::Expr,
540-
vis: Visibility) {
536+
fn process_assoc_const(&mut self,
537+
id: ast::NodeId,
538+
name: ast::Name,
539+
span: Span,
540+
typ: &ast::Ty,
541+
expr: &ast::Expr,
542+
parent_id: NodeId,
543+
vis: Visibility) {
541544
let qualname = format!("::{}", self.tcx.node_path_str(id));
542545

543546
let sub_span = self.span.sub_span_after_keyword(span, keywords::Const);
@@ -552,6 +555,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
552555
value: self.span.snippet(expr.span),
553556
type_value: ty_to_string(&typ),
554557
scope: self.cur_scope,
558+
parent: Some(parent_id),
555559
visibility: vis,
556560
}.lower(self.tcx));
557561
}
@@ -646,7 +650,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
646650
qualname: qualname,
647651
type_value: enum_data.qualname.clone(),
648652
value: val,
649-
scope: enum_data.scope
653+
scope: enum_data.scope,
654+
parent: Some(item.id),
650655
}.lower(self.tcx));
651656
}
652657
}
@@ -669,7 +674,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
669674
qualname: qualname,
670675
type_value: enum_data.qualname.clone(),
671676
value: val,
672-
scope: enum_data.scope
677+
scope: enum_data.scope,
678+
parent: Some(item.id),
673679
}.lower(self.tcx));
674680
}
675681
}
@@ -722,7 +728,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
722728
}
723729
self.process_generic_params(type_parameters, item.span, "", item.id);
724730
for impl_item in impl_items {
725-
self.visit_impl_item(impl_item);
731+
self.process_impl_item(impl_item, item.id);
726732
}
727733
}
728734

@@ -792,7 +798,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
792798
// walk generics and methods
793799
self.process_generic_params(generics, item.span, &qualname, item.id);
794800
for method in methods {
795-
self.visit_trait_item(method)
801+
self.process_trait_item(method, item.id)
796802
}
797803
}
798804

@@ -998,6 +1004,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
9981004
value: value,
9991005
type_value: typ,
10001006
scope: 0,
1007+
parent: None,
10011008
visibility: Visibility::Inherited,
10021009
}.lower(self.tcx));
10031010
}
@@ -1046,6 +1053,57 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
10461053
}
10471054
}
10481055
}
1056+
1057+
fn process_trait_item(&mut self, trait_item: &ast::TraitItem, trait_id: NodeId) {
1058+
self.process_macro_use(trait_item.span, trait_item.id);
1059+
match trait_item.node {
1060+
ast::TraitItemKind::Const(ref ty, Some(ref expr)) => {
1061+
self.process_assoc_const(trait_item.id,
1062+
trait_item.ident.name,
1063+
trait_item.span,
1064+
&ty,
1065+
&expr,
1066+
trait_id,
1067+
Visibility::Public);
1068+
}
1069+
ast::TraitItemKind::Method(ref sig, ref body) => {
1070+
self.process_method(sig,
1071+
body.as_ref().map(|x| &**x),
1072+
trait_item.id,
1073+
trait_item.ident.name,
1074+
Visibility::Public,
1075+
trait_item.span);
1076+
}
1077+
ast::TraitItemKind::Const(_, None) |
1078+
ast::TraitItemKind::Type(..) |
1079+
ast::TraitItemKind::Macro(_) => {}
1080+
}
1081+
}
1082+
1083+
fn process_impl_item(&mut self, impl_item: &ast::ImplItem, impl_id: NodeId) {
1084+
self.process_macro_use(impl_item.span, impl_item.id);
1085+
match impl_item.node {
1086+
ast::ImplItemKind::Const(ref ty, ref expr) => {
1087+
self.process_assoc_const(impl_item.id,
1088+
impl_item.ident.name,
1089+
impl_item.span,
1090+
&ty,
1091+
&expr,
1092+
impl_id,
1093+
From::from(&impl_item.vis));
1094+
}
1095+
ast::ImplItemKind::Method(ref sig, ref body) => {
1096+
self.process_method(sig,
1097+
Some(body),
1098+
impl_item.id,
1099+
impl_item.ident.name,
1100+
From::from(&impl_item.vis),
1101+
impl_item.span);
1102+
}
1103+
ast::ImplItemKind::Type(_) |
1104+
ast::ImplItemKind::Macro(_) => {}
1105+
}
1106+
}
10491107
}
10501108

10511109
impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D> {
@@ -1180,6 +1238,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
11801238
qualname: qualname.clone(),
11811239
value: value,
11821240
visibility: From::from(&item.vis),
1241+
parent: None,
11831242
}.lower(self.tcx));
11841243
}
11851244

@@ -1204,55 +1263,6 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
12041263
}
12051264
}
12061265

1207-
fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
1208-
self.process_macro_use(trait_item.span, trait_item.id);
1209-
match trait_item.node {
1210-
ast::TraitItemKind::Const(ref ty, Some(ref expr)) => {
1211-
self.process_const(trait_item.id,
1212-
trait_item.ident.name,
1213-
trait_item.span,
1214-
&ty,
1215-
&expr,
1216-
Visibility::Public);
1217-
}
1218-
ast::TraitItemKind::Method(ref sig, ref body) => {
1219-
self.process_method(sig,
1220-
body.as_ref().map(|x| &**x),
1221-
trait_item.id,
1222-
trait_item.ident.name,
1223-
Visibility::Public,
1224-
trait_item.span);
1225-
}
1226-
ast::TraitItemKind::Const(_, None) |
1227-
ast::TraitItemKind::Type(..) |
1228-
ast::TraitItemKind::Macro(_) => {}
1229-
}
1230-
}
1231-
1232-
fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
1233-
self.process_macro_use(impl_item.span, impl_item.id);
1234-
match impl_item.node {
1235-
ast::ImplItemKind::Const(ref ty, ref expr) => {
1236-
self.process_const(impl_item.id,
1237-
impl_item.ident.name,
1238-
impl_item.span,
1239-
&ty,
1240-
&expr,
1241-
From::from(&impl_item.vis));
1242-
}
1243-
ast::ImplItemKind::Method(ref sig, ref body) => {
1244-
self.process_method(sig,
1245-
Some(body),
1246-
impl_item.id,
1247-
impl_item.ident.name,
1248-
From::from(&impl_item.vis),
1249-
impl_item.span);
1250-
}
1251-
ast::ImplItemKind::Type(_) |
1252-
ast::ImplItemKind::Macro(_) => {}
1253-
}
1254-
}
1255-
12561266
fn visit_ty(&mut self, t: &ast::Ty) {
12571267
self.process_macro_use(t.span, t.id);
12581268
match t.node {
@@ -1416,6 +1426,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
14161426
value: value,
14171427
type_value: String::new(),
14181428
scope: 0,
1429+
parent: None,
14191430
visibility: Visibility::Inherited,
14201431
}.lower(self.tcx));
14211432
}

src/librustc_save_analysis/external_data.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ pub struct FunctionData {
169169
pub scope: DefId,
170170
pub value: String,
171171
pub visibility: Visibility,
172+
pub parent: Option<DefId>,
172173
}
173174

174175
impl Lower for data::FunctionData {
@@ -184,6 +185,7 @@ impl Lower for data::FunctionData {
184185
scope: make_def_id(self.scope, &tcx.map),
185186
value: self.value,
186187
visibility: self.visibility,
188+
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
187189
}
188190
}
189191
}
@@ -328,6 +330,7 @@ pub struct MethodData {
328330
pub value: String,
329331
pub decl_id: Option<DefId>,
330332
pub visibility: Visibility,
333+
pub parent: Option<DefId>
331334
}
332335

333336
impl Lower for data::MethodData {
@@ -343,6 +346,7 @@ impl Lower for data::MethodData {
343346
value: self.value,
344347
decl_id: self.decl_id,
345348
visibility: self.visibility,
349+
parent: Some(make_def_id(self.scope, &tcx.map)),
346350
}
347351
}
348352
}
@@ -438,7 +442,8 @@ pub struct StructVariantData {
438442
pub qualname: String,
439443
pub type_value: String,
440444
pub value: String,
441-
pub scope: DefId
445+
pub scope: DefId,
446+
pub parent: Option<DefId>,
442447
}
443448

444449
impl Lower for data::StructVariantData {
@@ -453,6 +458,7 @@ impl Lower for data::StructVariantData {
453458
type_value: self.type_value,
454459
value: self.value,
455460
scope: make_def_id(self.scope, &tcx.map),
461+
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
456462
}
457463
}
458464
}
@@ -495,6 +501,7 @@ pub struct TupleVariantData {
495501
pub type_value: String,
496502
pub value: String,
497503
pub scope: DefId,
504+
pub parent: Option<DefId>,
498505
}
499506

500507
impl Lower for data::TupleVariantData {
@@ -509,6 +516,7 @@ impl Lower for data::TupleVariantData {
509516
type_value: self.type_value,
510517
value: self.value,
511518
scope: make_def_id(self.scope, &tcx.map),
519+
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
512520
}
513521
}
514522
}
@@ -522,6 +530,7 @@ pub struct TypeDefData {
522530
pub qualname: String,
523531
pub value: String,
524532
pub visibility: Visibility,
533+
pub parent: Option<DefId>,
525534
}
526535

527536
impl Lower for data::TypeDefData {
@@ -535,6 +544,7 @@ impl Lower for data::TypeDefData {
535544
qualname: self.qualname,
536545
value: self.value,
537546
visibility: self.visibility,
547+
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
538548
}
539549
}
540550
}
@@ -620,6 +630,7 @@ pub struct VariableData {
620630
pub scope: DefId,
621631
pub value: String,
622632
pub type_value: String,
633+
pub parent: Option<DefId>,
623634
pub visibility: Visibility,
624635
}
625636

@@ -636,6 +647,7 @@ impl Lower for data::VariableData {
636647
scope: make_def_id(self.scope, &tcx.map),
637648
value: self.value,
638649
type_value: self.type_value,
650+
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
639651
visibility: self.visibility,
640652
}
641653
}

0 commit comments

Comments
 (0)