]>
git.proxmox.com Git - rustc.git/blob - src/librustdoc/formats/renderer.rs
1 use rustc_middle
::ty
::TyCtxt
;
2 use rustc_span
::edition
::Edition
;
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 /// Sets up any state required for the renderer. When this is called the cache has already been
20 options
: RenderOptions
,
24 ) -> Result
<(Self, clean
::Crate
), Error
>;
26 /// Make a new renderer to render a child of the item currently being rendered.
27 fn make_child_renderer(&self) -> Self;
29 /// Renders a single non-module item. This means no recursive sub-item rendering is required.
30 fn item(&mut self, item
: clean
::Item
) -> Result
<(), Error
>;
32 /// Renders a module (should not handle recursing into children).
33 fn mod_item_in(&mut self, item
: &clean
::Item
, item_name
: &str) -> Result
<(), Error
>;
35 /// Runs after recursively rendering all sub-items of a module.
36 fn mod_item_out(&mut self, item_name
: &str) -> Result
<(), Error
>;
38 /// Post processing hook for cleanup and dumping output to files.
40 /// A handler is available if the renderer wants to report errors.
44 diag
: &rustc_errors
::Handler
,
45 ) -> 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
,
55 diag
: &rustc_errors
::Handler
,
58 ) -> Result
<(), Error
> {
59 let prof
= &tcx
.sess
.prof
;
61 let (mut format_renderer
, mut krate
) = prof
62 .extra_verbose_generic_activity("create_renderer", T
::descr())
63 .run(|| T
::init(krate
, options
, edition
, cache
, tcx
))?
;
65 let mut item
= match krate
.module
.take() {
67 None
=> return Ok(()),
70 item
.name
= Some(krate
.name
);
72 // Render the crate documentation
73 let mut work
= vec
![(format_renderer
.make_child_renderer(), item
)];
75 let unknown
= rustc_span
::Symbol
::intern("<unknown item>");
76 while let Some((mut cx
, item
)) = work
.pop() {
78 // modules are special because they add a namespace. We also need to
79 // recurse into the items of the module as well.
80 let name
= item
.name
.as_ref().unwrap().to_string();
82 panic
!("Unexpected module with empty name");
84 let _timer
= prof
.generic_activity_with_arg("render_mod_item", name
.as_str());
86 cx
.mod_item_in(&item
, &name
)?
;
87 let module
= match *item
.kind
{
88 clean
::StrippedItem(box clean
::ModuleItem(m
)) | clean
::ModuleItem(m
) => m
,
91 for it
in module
.items
{
92 debug
!("Adding {:?} to worklist", it
.name
);
93 work
.push((cx
.make_child_renderer(), it
));
96 cx
.mod_item_out(&name
)?
;
97 // FIXME: checking `item.name.is_some()` is very implicit and leads to lots of special
98 // cases. Use an explicit match instead.
99 } else if item
.name
.is_some() && !item
.is_extern_crate() {
100 prof
.generic_activity_with_arg("render_item", &*item
.name
.unwrap_or(unknown
).as_str())
101 .run(|| cx
.item(item
))?
;
104 prof
.extra_verbose_generic_activity("renderer_after_krate", T
::descr())
105 .run(|| format_renderer
.after_krate(&krate
, diag
))