2020-06-24 08:16:21 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2020-12-31 23:25:30 -05:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-06-24 08:16:21 -05:00
|
|
|
use rustc_span::edition::Edition;
|
|
|
|
|
|
|
|
|
|
use crate::clean;
|
|
|
|
|
use crate::config::{RenderInfo, RenderOptions};
|
|
|
|
|
use crate::error::Error;
|
|
|
|
|
use crate::formats::cache::{Cache, CACHE_KEY};
|
|
|
|
|
|
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.
|
2020-12-16 14:34:08 -05:00
|
|
|
crate trait FormatRenderer<'tcx>: Clone {
|
2021-01-22 10:38:30 -05:00
|
|
|
/// Gives a description of the renderer. Used for performance profiling.
|
|
|
|
|
fn descr() -> &'static str;
|
|
|
|
|
|
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,
|
2020-06-29 18:22:58 -05:00
|
|
|
render_info: RenderInfo,
|
2020-06-24 08:16:21 -05:00
|
|
|
edition: Edition,
|
|
|
|
|
cache: &mut Cache,
|
2020-12-31 23:25:30 -05: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
|
|
|
|
|
|
|
|
/// Renders a single non-module item. This means no recursive sub-item rendering is required.
|
|
|
|
|
fn item(&mut self, item: clean::Item, cache: &Cache) -> Result<(), Error>;
|
|
|
|
|
|
2020-06-29 18:22:58 -05:00
|
|
|
/// Renders a module (should not handle recursing into children).
|
2020-06-24 08:16:21 -05:00
|
|
|
fn mod_item_in(
|
|
|
|
|
&mut self,
|
|
|
|
|
item: &clean::Item,
|
|
|
|
|
item_name: &str,
|
|
|
|
|
cache: &Cache,
|
|
|
|
|
) -> Result<(), Error>;
|
|
|
|
|
|
|
|
|
|
/// Runs after recursively rendering all sub-items of a module.
|
2020-06-26 08:18:20 -05:00
|
|
|
fn mod_item_out(&mut self, item_name: &str) -> Result<(), Error>;
|
2020-06-24 08:16:21 -05:00
|
|
|
|
|
|
|
|
/// Post processing hook for cleanup and dumping output to files.
|
2021-01-19 01:59:45 -05:00
|
|
|
///
|
|
|
|
|
/// A handler is available if the renderer wants to report errors.
|
|
|
|
|
fn after_krate(
|
|
|
|
|
&mut self,
|
|
|
|
|
krate: &clean::Crate,
|
|
|
|
|
cache: &Cache,
|
|
|
|
|
diag: &rustc_errors::Handler,
|
|
|
|
|
) -> Result<(), Error>;
|
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,
|
|
|
|
|
render_info: RenderInfo,
|
|
|
|
|
diag: &rustc_errors::Handler,
|
|
|
|
|
edition: Edition,
|
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 (krate, mut cache) = tcx.sess.time("create_format_cache", || {
|
|
|
|
|
Cache::from_krate(
|
|
|
|
|
render_info.clone(),
|
|
|
|
|
options.document_private,
|
|
|
|
|
&options.extern_html_root_urls,
|
|
|
|
|
&options.output,
|
|
|
|
|
krate,
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
let prof = &tcx.sess.prof;
|
|
|
|
|
|
|
|
|
|
let (mut format_renderer, mut krate) = prof
|
|
|
|
|
.extra_verbose_generic_activity("create_renderer", T::descr())
|
|
|
|
|
.run(|| T::init(krate, options, render_info, edition, &mut cache, tcx))?;
|
2020-07-27 17:34:17 -05:00
|
|
|
|
|
|
|
|
let cache = Arc::new(cache);
|
|
|
|
|
// Freeze the cache now that the index has been built. Put an Arc into TLS for future
|
|
|
|
|
// parallelization opportunities
|
|
|
|
|
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
|
|
|
|
|
|
|
|
|
|
let mut item = match krate.module.take() {
|
|
|
|
|
Some(i) => i,
|
|
|
|
|
None => return Ok(()),
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-14 23:23:58 -05:00
|
|
|
item.name = Some(krate.name);
|
2020-07-27 17:34:17 -05:00
|
|
|
|
|
|
|
|
// Render the crate documentation
|
|
|
|
|
let mut work = vec![(format_renderer.clone(), item)];
|
|
|
|
|
|
2021-01-22 10:38:30 -05:00
|
|
|
let unknown = rustc_span::Symbol::intern("<unknown item>");
|
2020-07-27 17:34:17 -05:00
|
|
|
while let Some((mut cx, item)) = work.pop() {
|
|
|
|
|
if item.is_mod() {
|
|
|
|
|
// 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
|
|
|
|
2020-07-27 17:34:17 -05:00
|
|
|
cx.mod_item_in(&item, &name, &cache)?;
|
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);
|
|
|
|
|
work.push((cx.clone(), it));
|
2020-06-24 08:16:21 -05:00
|
|
|
}
|
|
|
|
|
|
2020-07-27 17:34:17 -05:00
|
|
|
cx.mod_item_out(&name)?;
|
|
|
|
|
} else if item.name.is_some() {
|
2021-01-22 10:38:30 -05:00
|
|
|
prof.generic_activity_with_arg("render_item", &*item.name.unwrap_or(unknown).as_str())
|
|
|
|
|
.run(|| cx.item(item, &cache))?;
|
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())
|
|
|
|
|
.run(|| format_renderer.after_krate(&krate, &cache, diag))
|
2020-06-24 08:16:21 -05:00
|
|
|
}
|