2021-01-22 21:17:32 +01:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2021-04-22 19:35:20 -04:00
|
|
|
use rustc_span::Symbol;
|
2020-06-24 08:16:21 -05:00
|
|
|
|
|
|
|
|
use crate::clean;
|
2021-02-11 21:29:22 -05:00
|
|
|
use crate::config::RenderOptions;
|
2020-06-24 08:16:21 -05:00
|
|
|
use crate::error::Error;
|
2021-01-12 23:36:04 +01:00
|
|
|
use crate::formats::cache::Cache;
|
2020-06-24 08:16:21 -05:00
|
|
|
|
2020-07-27 17:34:17 -05:00
|
|
|
/// Allows for different backends to rustdoc to be used with the `run_format()` function. Each
|
2020-06-29 18:22:58 -05:00
|
|
|
/// backend renderer has hooks for initialization, documenting an item, entering and exiting a
|
|
|
|
|
/// module, and cleanup/finalizing output.
|
2021-03-01 19:12:03 -08:00
|
|
|
crate trait FormatRenderer<'tcx>: Sized {
|
2021-01-22 10:38:30 -05:00
|
|
|
/// Gives a description of the renderer. Used for performance profiling.
|
|
|
|
|
fn descr() -> &'static str;
|
|
|
|
|
|
2021-03-24 15:54:20 +00:00
|
|
|
/// Whether to call `item` recursivly for modules
|
|
|
|
|
///
|
|
|
|
|
/// This is true for html, and false for json. See #80664
|
|
|
|
|
const RUN_ON_MODULE: bool;
|
|
|
|
|
|
2020-06-29 18:22:58 -05:00
|
|
|
/// Sets up any state required for the renderer. When this is called the cache has already been
|
2020-06-26 08:18:20 -05:00
|
|
|
/// populated.
|
2020-06-24 08:16:21 -05:00
|
|
|
fn init(
|
|
|
|
|
krate: clean::Crate,
|
|
|
|
|
options: RenderOptions,
|
2021-01-12 23:36:04 +01:00
|
|
|
cache: Cache,
|
2021-01-22 21:17:32 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-06-29 18:22:58 -05:00
|
|
|
) -> Result<(Self, clean::Crate), Error>;
|
2020-06-24 08:16:21 -05:00
|
|
|
|
2021-03-01 19:12:03 -08:00
|
|
|
/// Make a new renderer to render a child of the item currently being rendered.
|
|
|
|
|
fn make_child_renderer(&self) -> Self;
|
|
|
|
|
|
2020-06-24 08:16:21 -05:00
|
|
|
/// Renders a single non-module item. This means no recursive sub-item rendering is required.
|
2021-01-12 23:36:04 +01:00
|
|
|
fn item(&mut self, item: clean::Item) -> Result<(), Error>;
|
2020-06-24 08:16:21 -05:00
|
|
|
|
2020-06-29 18:22:58 -05:00
|
|
|
/// Renders a module (should not handle recursing into children).
|
2021-01-12 23:36:04 +01:00
|
|
|
fn mod_item_in(&mut self, item: &clean::Item, item_name: &str) -> Result<(), Error>;
|
2020-06-24 08:16:21 -05:00
|
|
|
|
|
|
|
|
/// Runs after recursively rendering all sub-items of a module.
|
2021-04-22 19:46:53 -04:00
|
|
|
fn mod_item_out(&mut self) -> Result<(), Error> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2020-06-24 08:16:21 -05:00
|
|
|
|
|
|
|
|
/// Post processing hook for cleanup and dumping output to files.
|
2021-04-22 19:32:24 -04:00
|
|
|
fn after_krate(&mut self) -> Result<(), Error>;
|
2021-01-12 23:36:04 +01:00
|
|
|
|
|
|
|
|
fn cache(&self) -> &Cache;
|
2020-06-24 08:16:21 -05:00
|
|
|
}
|
|
|
|
|
|
2020-07-27 17:34:17 -05:00
|
|
|
/// Main method for rendering a crate.
|
2020-12-16 14:34:08 -05:00
|
|
|
crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
|
2020-07-27 17:34:17 -05:00
|
|
|
krate: clean::Crate,
|
|
|
|
|
options: RenderOptions,
|
2021-02-11 21:29:22 -05:00
|
|
|
cache: Cache,
|
2020-12-31 23:25:30 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-07-27 17:34:17 -05:00
|
|
|
) -> Result<(), Error> {
|
2021-01-22 10:38:30 -05:00
|
|
|
let prof = &tcx.sess.prof;
|
|
|
|
|
|
2021-03-25 12:46:35 -04:00
|
|
|
let emit_crate = options.should_emit_crate();
|
2021-03-23 11:04:09 -07:00
|
|
|
let (mut format_renderer, krate) = prof
|
2021-01-22 10:38:30 -05:00
|
|
|
.extra_verbose_generic_activity("create_renderer", T::descr())
|
2021-04-22 19:35:20 -04:00
|
|
|
.run(|| T::init(krate, options, cache, tcx))?;
|
2020-07-27 17:34:17 -05:00
|
|
|
|
2021-03-25 12:46:35 -04:00
|
|
|
if !emit_crate {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 17:34:17 -05:00
|
|
|
// Render the crate documentation
|
2021-03-23 11:04:09 -07:00
|
|
|
let mut work = vec![(format_renderer.make_child_renderer(), krate.module)];
|
2020-07-27 17:34:17 -05:00
|
|
|
|
2021-03-23 11:04:09 -07:00
|
|
|
let unknown = Symbol::intern("<unknown item>");
|
2020-07-27 17:34:17 -05:00
|
|
|
while let Some((mut cx, item)) = work.pop() {
|
2021-03-24 15:54:20 +00:00
|
|
|
if item.is_mod() && T::RUN_ON_MODULE {
|
2020-07-27 17:34:17 -05:00
|
|
|
// modules are special because they add a namespace. We also need to
|
|
|
|
|
// recurse into the items of the module as well.
|
|
|
|
|
let name = item.name.as_ref().unwrap().to_string();
|
|
|
|
|
if name.is_empty() {
|
|
|
|
|
panic!("Unexpected module with empty name");
|
|
|
|
|
}
|
2021-01-22 10:38:30 -05:00
|
|
|
let _timer = prof.generic_activity_with_arg("render_mod_item", name.as_str());
|
2020-06-24 08:16:21 -05:00
|
|
|
|
2021-01-12 23:36:04 +01:00
|
|
|
cx.mod_item_in(&item, &name)?;
|
2020-12-13 11:32:57 -05:00
|
|
|
let module = match *item.kind {
|
2020-07-27 17:34:17 -05:00
|
|
|
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
|
|
|
|
for it in module.items {
|
|
|
|
|
debug!("Adding {:?} to worklist", it.name);
|
2021-03-01 19:12:03 -08:00
|
|
|
work.push((cx.make_child_renderer(), it));
|
2020-06-24 08:16:21 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-22 19:46:53 -04:00
|
|
|
cx.mod_item_out()?;
|
2021-03-05 16:04:24 +01:00
|
|
|
// FIXME: checking `item.name.is_some()` is very implicit and leads to lots of special
|
|
|
|
|
// cases. Use an explicit match instead.
|
|
|
|
|
} else if item.name.is_some() && !item.is_extern_crate() {
|
2021-01-22 10:38:30 -05:00
|
|
|
prof.generic_activity_with_arg("render_item", &*item.name.unwrap_or(unknown).as_str())
|
2021-01-12 23:36:04 +01:00
|
|
|
.run(|| cx.item(item))?;
|
2020-07-27 17:34:17 -05:00
|
|
|
}
|
2020-06-24 08:16:21 -05:00
|
|
|
}
|
2021-01-22 10:38:30 -05:00
|
|
|
prof.extra_verbose_generic_activity("renderer_after_krate", T::descr())
|
2021-04-22 19:32:24 -04:00
|
|
|
.run(|| format_renderer.after_krate())
|
2020-06-24 08:16:21 -05:00
|
|
|
}
|