1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use rustc_middle::ty::TyCtxt;
use rustc_span::Symbol;
use crate::clean;
use crate::config::RenderOptions;
use crate::error::Error;
use crate::formats::cache::Cache;
pub(crate) trait FormatRenderer<'tcx>: Sized {
fn descr() -> &'static str;
const RUN_ON_MODULE: bool;
fn init(
krate: clean::Crate,
options: RenderOptions,
cache: Cache,
tcx: TyCtxt<'tcx>,
) -> Result<(Self, clean::Crate), Error>;
fn make_child_renderer(&self) -> Self;
fn item(&mut self, item: clean::Item) -> Result<(), Error>;
fn mod_item_in(&mut self, item: &clean::Item) -> Result<(), Error>;
fn mod_item_out(&mut self) -> Result<(), Error> {
Ok(())
}
fn after_krate(&mut self) -> Result<(), Error>;
fn cache(&self) -> &Cache;
}
pub(crate) fn run_format<'tcx, T: FormatRenderer<'tcx>>(
krate: clean::Crate,
options: RenderOptions,
cache: Cache,
tcx: TyCtxt<'tcx>,
) -> Result<(), Error> {
let prof = &tcx.sess.prof;
let emit_crate = options.should_emit_crate();
let (mut format_renderer, krate) = prof
.verbose_generic_activity_with_arg("create_renderer", T::descr())
.run(|| T::init(krate, options, cache, tcx))?;
if !emit_crate {
return Ok(());
}
let mut work = vec![(format_renderer.make_child_renderer(), krate.module)];
let unknown = Symbol::intern("<unknown item>");
while let Some((mut cx, item)) = work.pop() {
if item.is_mod() && T::RUN_ON_MODULE {
let _timer =
prof.generic_activity_with_arg("render_mod_item", item.name.unwrap().to_string());
cx.mod_item_in(&item)?;
let (clean::StrippedItem(box clean::ModuleItem(module)) | clean::ModuleItem(module)) = *item.kind
else { unreachable!() };
for it in module.items {
debug!("Adding {:?} to worklist", it.name);
work.push((cx.make_child_renderer(), it));
}
cx.mod_item_out()?;
} else if item.name.is_some() && !item.is_extern_crate() {
prof.generic_activity_with_arg("render_item", item.name.unwrap_or(unknown).as_str())
.run(|| cx.item(item))?;
}
}
prof.verbose_generic_activity_with_arg("renderer_after_krate", T::descr())
.run(|| format_renderer.after_krate())
}