1 use rustc_middle::ty::TyCtxt;
2 use rustc_span::Symbol;
5 use crate::config::RenderOptions;
6 use crate::error::Error;
7 use crate::formats::cache::Cache;
9 /// Allows for different backends to rustdoc to be used with the `run_format()` function. Each
10 /// backend renderer has hooks for initialization, documenting an item, entering and exiting a
11 /// module, and cleanup/finalizing output.
12 crate trait FormatRenderer<'tcx>: Sized {
13 /// Gives a description of the renderer. Used for performance profiling.
14 fn descr() -> &'static str;
16 /// Whether to call `item` recursivly for modules
18 /// This is true for html, and false for json. See #80664
19 const RUN_ON_MODULE: bool;
21 /// Sets up any state required for the renderer. When this is called the cache has already been
25 options: RenderOptions,
28 ) -> Result<(Self, clean::Crate), Error>;
30 /// Make a new renderer to render a child of the item currently being rendered.
31 fn make_child_renderer(&self) -> Self;
33 /// Renders a single non-module item. This means no recursive sub-item rendering is required.
34 fn item(&mut self, item: clean::Item) -> Result<(), Error>;
36 /// Renders a module (should not handle recursing into children).
37 fn mod_item_in(&mut self, item: &clean::Item) -> Result<(), Error>;
39 /// Runs after recursively rendering all sub-items of a module.
40 fn mod_item_out(&mut self) -> Result<(), Error> {
44 /// Post processing hook for cleanup and dumping output to files.
45 fn after_krate(&mut self) -> Result<(), Error>;
47 fn cache(&self) -> &Cache;
50 /// Main method for rendering a crate.
51 crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
53 options: RenderOptions,
56 ) -> Result<(), Error> {
57 let prof = &tcx.sess.prof;
59 let emit_crate = options.should_emit_crate();
60 let (mut format_renderer, krate) = prof
61 .extra_verbose_generic_activity("create_renderer", T::descr())
62 .run(|| T::init(krate, options, cache, tcx))?;
68 // Render the crate documentation
69 let mut work = vec![(format_renderer.make_child_renderer(), krate.module)];
71 let unknown = Symbol::intern("<unknown item>");
72 while let Some((mut cx, item)) = work.pop() {
73 if item.is_mod() && T::RUN_ON_MODULE {
74 // modules are special because they add a namespace. We also need to
75 // recurse into the items of the module as well.
77 prof.generic_activity_with_arg("render_mod_item", item.name.unwrap().to_string());
79 cx.mod_item_in(&item)?;
80 let module = match *item.kind {
81 clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
84 for it in module.items {
85 debug!("Adding {:?} to worklist", it.name);
86 work.push((cx.make_child_renderer(), it));
90 // FIXME: checking `item.name.is_some()` is very implicit and leads to lots of special
91 // cases. Use an explicit match instead.
92 } else if item.name.is_some() && !item.is_extern_crate() {
93 prof.generic_activity_with_arg("render_item", &*item.name.unwrap_or(unknown).as_str())
94 .run(|| cx.item(item))?;
97 prof.extra_verbose_generic_activity("renderer_after_krate", T::descr())
98 .run(|| format_renderer.after_krate())