2020-08-03 12:45:27 -05:00
|
|
|
//! Rustdoc's JSON backend
|
|
|
|
//!
|
|
|
|
//! This module contains the logic for rendering a crate as JSON rather than the normal static HTML
|
|
|
|
//! output. See [the RFC](https://github.com/rust-lang/rfcs/pull/2963) and the [`types`] module
|
|
|
|
//! docs for usage and details.
|
|
|
|
|
|
|
|
mod conversions;
|
2022-08-28 02:35:44 +01:00
|
|
|
mod import_finder;
|
2020-08-03 12:45:27 -05:00
|
|
|
|
|
|
|
use std::cell::RefCell;
|
2022-01-19 00:59:44 -08:00
|
|
|
use std::fs::{create_dir_all, File};
|
2022-02-13 01:25:54 +00:00
|
|
|
use std::io::{BufWriter, Write};
|
2020-08-03 12:45:27 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-08-28 02:35:44 +01:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2021-05-08 10:04:03 +02:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-12-31 23:25:30 -05:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-12-11 22:36:51 -05:00
|
|
|
use rustc_session::Session;
|
2022-05-31 21:47:19 +02:00
|
|
|
use rustc_span::def_id::LOCAL_CRATE;
|
2020-08-03 12:45:27 -05:00
|
|
|
|
2021-01-25 16:23:43 -05:00
|
|
|
use rustdoc_json_types as types;
|
2021-01-22 16:09:24 -05:00
|
|
|
|
2021-12-27 18:57:07 -08:00
|
|
|
use crate::clean::types::{ExternalCrate, ExternalLocation};
|
2022-07-15 17:37:07 +02:00
|
|
|
use crate::clean::ItemKind;
|
2021-02-11 21:29:22 -05:00
|
|
|
use crate::config::RenderOptions;
|
2022-01-19 00:59:44 -08:00
|
|
|
use crate::docfs::PathError;
|
2020-07-30 13:54:26 -05:00
|
|
|
use crate::error::Error;
|
|
|
|
use crate::formats::cache::Cache;
|
|
|
|
use crate::formats::FormatRenderer;
|
2022-05-31 21:47:19 +02:00
|
|
|
use crate::json::conversions::{from_item_id, from_item_id_with_name, IntoWithTcx};
|
2022-01-19 00:59:44 -08:00
|
|
|
use crate::{clean, try_err};
|
2020-07-30 13:54:26 -05:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) struct JsonRenderer<'tcx> {
|
2020-12-31 23:25:30 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-08-03 12:45:27 -05:00
|
|
|
/// A mapping of IDs that contains all local items for this crate which gets output as a top
|
|
|
|
/// level field of the JSON blob.
|
|
|
|
index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>,
|
|
|
|
/// The directory where the blob will be written to.
|
|
|
|
out_path: PathBuf,
|
2021-01-12 23:36:04 +01:00
|
|
|
cache: Rc<Cache>,
|
2022-08-28 02:35:44 +01:00
|
|
|
imported_items: FxHashSet<DefId>,
|
2020-08-03 12:45:27 -05:00
|
|
|
}
|
|
|
|
|
2021-12-28 14:27:31 -08:00
|
|
|
impl<'tcx> JsonRenderer<'tcx> {
|
2021-02-12 01:59:20 -05:00
|
|
|
fn sess(&self) -> &'tcx Session {
|
2020-12-16 14:34:08 -05:00
|
|
|
self.tcx.sess
|
|
|
|
}
|
|
|
|
|
2021-05-08 10:04:03 +02:00
|
|
|
fn get_trait_implementors(&mut self, id: DefId) -> Vec<types::Id> {
|
2021-01-24 17:41:21 +01:00
|
|
|
Rc::clone(&self.cache)
|
2020-08-03 12:45:27 -05:00
|
|
|
.implementors
|
|
|
|
.get(&id)
|
|
|
|
.map(|implementors| {
|
|
|
|
implementors
|
|
|
|
.iter()
|
|
|
|
.map(|i| {
|
|
|
|
let item = &i.impl_item;
|
2021-01-12 23:36:04 +01:00
|
|
|
self.item(item.clone()).unwrap();
|
2022-05-31 21:47:19 +02:00
|
|
|
from_item_id_with_name(item.item_id, self.tcx, item.name)
|
2020-08-03 12:45:27 -05:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2021-05-08 10:04:03 +02:00
|
|
|
fn get_impls(&mut self, id: DefId) -> Vec<types::Id> {
|
2021-01-24 17:41:21 +01:00
|
|
|
Rc::clone(&self.cache)
|
2020-08-03 12:45:27 -05:00
|
|
|
.impls
|
2021-05-08 10:04:03 +02:00
|
|
|
.get(&id)
|
2020-08-03 12:45:27 -05:00
|
|
|
.map(|impls| {
|
|
|
|
impls
|
|
|
|
.iter()
|
|
|
|
.filter_map(|i| {
|
|
|
|
let item = &i.impl_item;
|
2021-08-28 22:01:56 +09:00
|
|
|
|
|
|
|
// HACK(hkmatsumoto): For impls of primitive types, we index them
|
|
|
|
// regardless of whether they're local. This is because users can
|
|
|
|
// document primitive items in an arbitrary crate by using
|
|
|
|
// `doc(primitive)`.
|
|
|
|
let mut is_primitive_impl = false;
|
|
|
|
if let clean::types::ItemKind::ImplItem(ref impl_) = *item.kind {
|
|
|
|
if impl_.trait_.is_none() {
|
|
|
|
if let clean::types::Type::Primitive(_) = impl_.for_ {
|
|
|
|
is_primitive_impl = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 14:28:09 +02:00
|
|
|
if item.item_id.is_local() || is_primitive_impl {
|
2021-01-12 23:36:04 +01:00
|
|
|
self.item(item.clone()).unwrap();
|
2022-05-31 21:47:19 +02:00
|
|
|
Some(from_item_id_with_name(item.item_id, self.tcx, item.name))
|
2020-08-03 12:45:27 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:36:04 +01:00
|
|
|
fn get_trait_items(&mut self) -> Vec<(types::Id, types::Item)> {
|
2022-09-13 18:34:15 +01:00
|
|
|
debug!("Adding foreign trait items");
|
2021-01-24 17:41:21 +01:00
|
|
|
Rc::clone(&self.cache)
|
2020-08-03 12:45:27 -05:00
|
|
|
.traits
|
|
|
|
.iter()
|
2021-02-12 14:33:32 +01:00
|
|
|
.filter_map(|(&id, trait_item)| {
|
2020-08-03 12:45:27 -05:00
|
|
|
// only need to synthesize items for external traits
|
|
|
|
if !id.is_local() {
|
2022-08-13 15:50:01 +02:00
|
|
|
for item in &trait_item.items {
|
2022-09-13 18:34:15 +01:00
|
|
|
trace!("Adding subitem to {id:?}: {:?}", item.item_id);
|
2022-08-13 15:50:01 +02:00
|
|
|
self.item(item.clone()).unwrap();
|
|
|
|
}
|
2022-05-31 21:47:19 +02:00
|
|
|
let item_id = from_item_id(id.into(), self.tcx);
|
2020-08-03 12:45:27 -05:00
|
|
|
Some((
|
2022-05-31 21:47:19 +02:00
|
|
|
item_id.clone(),
|
2020-08-03 12:45:27 -05:00
|
|
|
types::Item {
|
2022-05-31 21:47:19 +02:00
|
|
|
id: item_id,
|
2020-08-03 12:45:27 -05:00
|
|
|
crate_id: id.krate.as_u32(),
|
2021-01-12 23:36:04 +01:00
|
|
|
name: self
|
|
|
|
.cache
|
2020-08-03 12:45:27 -05:00
|
|
|
.paths
|
|
|
|
.get(&id)
|
|
|
|
.unwrap_or_else(|| {
|
2021-01-12 23:36:04 +01:00
|
|
|
self.cache
|
2020-08-03 12:45:27 -05:00
|
|
|
.external_paths
|
|
|
|
.get(&id)
|
|
|
|
.expect("Trait should either be in local or external paths")
|
|
|
|
})
|
|
|
|
.0
|
|
|
|
.last()
|
2021-12-15 06:18:18 +11:00
|
|
|
.map(|s| s.to_string()),
|
2020-08-03 12:45:27 -05:00
|
|
|
visibility: types::Visibility::Public,
|
2021-03-07 18:09:35 +01:00
|
|
|
inner: types::ItemEnum::Trait(trait_item.clone().into_tcx(self.tcx)),
|
2021-03-09 21:25:54 -08:00
|
|
|
span: None,
|
2020-08-03 12:45:27 -05:00
|
|
|
docs: Default::default(),
|
|
|
|
links: Default::default(),
|
|
|
|
attrs: Default::default(),
|
|
|
|
deprecation: Default::default(),
|
|
|
|
},
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 13:54:26 -05:00
|
|
|
|
2020-12-16 14:34:08 -05:00
|
|
|
impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
|
2021-01-22 10:38:30 -05:00
|
|
|
fn descr() -> &'static str {
|
|
|
|
"json"
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:54:20 +00:00
|
|
|
const RUN_ON_MODULE: bool = false;
|
|
|
|
|
2020-07-30 13:54:26 -05:00
|
|
|
fn init(
|
2020-08-03 12:45:27 -05:00
|
|
|
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-07-30 13:54:26 -05:00
|
|
|
) -> Result<(Self, clean::Crate), Error> {
|
2020-08-03 12:45:27 -05:00
|
|
|
debug!("Initializing json renderer");
|
2022-08-28 02:35:44 +01:00
|
|
|
|
|
|
|
let (krate, imported_items) = import_finder::get_imports(krate);
|
|
|
|
|
2020-08-03 12:45:27 -05:00
|
|
|
Ok((
|
|
|
|
JsonRenderer {
|
2020-12-16 14:34:08 -05:00
|
|
|
tcx,
|
2020-08-03 12:45:27 -05:00
|
|
|
index: Rc::new(RefCell::new(FxHashMap::default())),
|
|
|
|
out_path: options.output,
|
2021-01-12 23:36:04 +01:00
|
|
|
cache: Rc::new(cache),
|
2022-08-28 02:35:44 +01:00
|
|
|
imported_items,
|
2020-08-03 12:45:27 -05:00
|
|
|
},
|
|
|
|
krate,
|
|
|
|
))
|
2020-07-30 13:54:26 -05:00
|
|
|
}
|
|
|
|
|
2021-03-01 19:12:03 -08:00
|
|
|
fn make_child_renderer(&self) -> Self {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:45:27 -05:00
|
|
|
/// Inserts an item into the index. This should be used rather than directly calling insert on
|
|
|
|
/// the hashmap because certain items (traits and types) need to have their mappings for trait
|
|
|
|
/// implementations filled out before they're inserted.
|
2021-01-12 23:36:04 +01:00
|
|
|
fn item(&mut self, item: clean::Item) -> Result<(), Error> {
|
2022-09-13 18:34:15 +01:00
|
|
|
let item_type = item.type_();
|
|
|
|
let item_name = item.name;
|
|
|
|
trace!("rendering {} {:?}", item_type, item_name);
|
2022-07-15 17:37:07 +02:00
|
|
|
|
|
|
|
// Flatten items that recursively store other items. We include orphaned items from
|
|
|
|
// stripped modules and etc that are otherwise reachable.
|
|
|
|
if let ItemKind::StrippedItem(inner) = &*item.kind {
|
|
|
|
inner.inner_items().for_each(|i| self.item(i.clone()).unwrap());
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:45:27 -05:00
|
|
|
// Flatten items that recursively store other items
|
2022-01-21 13:01:58 -05:00
|
|
|
item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap());
|
2020-08-03 12:45:27 -05:00
|
|
|
|
2022-05-31 21:47:19 +02:00
|
|
|
let name = item.name;
|
2022-04-16 14:28:09 +02:00
|
|
|
let item_id = item.item_id;
|
2020-12-11 23:57:18 -05:00
|
|
|
if let Some(mut new_item) = self.convert_item(item) {
|
2022-06-13 15:03:00 +02:00
|
|
|
let can_be_ignored = match new_item.inner {
|
|
|
|
types::ItemEnum::Trait(ref mut t) => {
|
|
|
|
t.implementations = self.get_trait_implementors(item_id.expect_def_id());
|
|
|
|
false
|
|
|
|
}
|
|
|
|
types::ItemEnum::Struct(ref mut s) => {
|
|
|
|
s.impls = self.get_impls(item_id.expect_def_id());
|
|
|
|
false
|
|
|
|
}
|
|
|
|
types::ItemEnum::Enum(ref mut e) => {
|
|
|
|
e.impls = self.get_impls(item_id.expect_def_id());
|
|
|
|
false
|
|
|
|
}
|
|
|
|
types::ItemEnum::Union(ref mut u) => {
|
|
|
|
u.impls = self.get_impls(item_id.expect_def_id());
|
|
|
|
false
|
|
|
|
}
|
2022-09-26 18:06:48 +01:00
|
|
|
types::ItemEnum::Primitive(ref mut p) => {
|
|
|
|
p.impls = self.get_impls(item_id.expect_def_id());
|
|
|
|
false
|
|
|
|
}
|
2022-06-13 15:03:00 +02:00
|
|
|
|
2022-11-16 20:13:45 +01:00
|
|
|
types::ItemEnum::Function(_)
|
2022-08-16 15:37:31 +02:00
|
|
|
| types::ItemEnum::Module(_)
|
2022-06-13 15:03:00 +02:00
|
|
|
| types::ItemEnum::AssocConst { .. }
|
2022-09-26 18:06:48 +01:00
|
|
|
| types::ItemEnum::AssocType { .. } => true,
|
2022-08-16 15:37:31 +02:00
|
|
|
types::ItemEnum::ExternCrate { .. }
|
2022-06-13 15:03:00 +02:00
|
|
|
| types::ItemEnum::Import(_)
|
|
|
|
| types::ItemEnum::StructField(_)
|
|
|
|
| types::ItemEnum::Variant(_)
|
|
|
|
| types::ItemEnum::TraitAlias(_)
|
|
|
|
| types::ItemEnum::Impl(_)
|
|
|
|
| types::ItemEnum::Typedef(_)
|
|
|
|
| types::ItemEnum::OpaqueTy(_)
|
|
|
|
| types::ItemEnum::Constant(_)
|
|
|
|
| types::ItemEnum::Static(_)
|
|
|
|
| types::ItemEnum::ForeignType
|
|
|
|
| types::ItemEnum::Macro(_)
|
2022-06-17 14:41:07 +02:00
|
|
|
| types::ItemEnum::ProcMacro(_) => false,
|
2022-06-13 15:03:00 +02:00
|
|
|
};
|
2022-05-31 21:47:19 +02:00
|
|
|
let removed = self
|
|
|
|
.index
|
|
|
|
.borrow_mut()
|
|
|
|
.insert(from_item_id_with_name(item_id, self.tcx, name), new_item.clone());
|
2021-03-24 15:54:20 +00:00
|
|
|
|
2020-12-05 22:23:17 +00:00
|
|
|
// FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check
|
2021-03-24 15:54:20 +00:00
|
|
|
// to make sure the items are unique. The main place this happens is when an item, is
|
|
|
|
// reexported in more than one place. See `rustdoc-json/reexport/in_root_and_mod`
|
2020-12-05 22:23:17 +00:00
|
|
|
if let Some(old_item) = removed {
|
2022-06-13 15:03:00 +02:00
|
|
|
// In case of generic implementations (like `impl<T> Trait for T {}`), all the
|
|
|
|
// inner items will be duplicated so we can ignore if they are slightly different.
|
|
|
|
if !can_be_ignored {
|
|
|
|
assert_eq!(old_item, new_item);
|
|
|
|
}
|
2020-12-05 22:23:17 +00:00
|
|
|
}
|
2020-08-03 12:45:27 -05:00
|
|
|
}
|
|
|
|
|
2022-09-13 18:34:15 +01:00
|
|
|
trace!("done rendering {} {:?}", item_type, item_name);
|
2020-08-03 12:45:27 -05:00
|
|
|
Ok(())
|
2020-07-30 13:54:26 -05:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:47:58 -04:00
|
|
|
fn mod_item_in(&mut self, _item: &clean::Item) -> Result<(), Error> {
|
|
|
|
unreachable!("RUN_ON_MODULE = false should never call mod_item_in")
|
2020-07-30 13:54:26 -05:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:32:24 -04:00
|
|
|
fn after_krate(&mut self) -> Result<(), Error> {
|
2020-08-03 12:45:27 -05:00
|
|
|
debug!("Done with crate");
|
2021-08-28 22:01:56 +09:00
|
|
|
|
2022-09-13 18:34:15 +01:00
|
|
|
debug!("Adding Primitve impls");
|
2021-08-28 22:01:56 +09:00
|
|
|
for primitive in Rc::clone(&self.cache).primitive_locations.values() {
|
2021-10-07 23:18:39 +02:00
|
|
|
self.get_impls(*primitive);
|
2021-08-28 22:01:56 +09:00
|
|
|
}
|
|
|
|
|
2022-05-31 21:47:19 +02:00
|
|
|
let e = ExternalCrate { crate_num: LOCAL_CRATE };
|
|
|
|
|
2022-11-13 15:26:17 +08:00
|
|
|
// FIXME(adotinthevoid): Remove this, as it's not consistent with not
|
2022-09-13 18:34:15 +01:00
|
|
|
// inlining foreign items.
|
|
|
|
let foreign_trait_items = self.get_trait_items();
|
2020-08-03 12:45:27 -05:00
|
|
|
let mut index = (*self.index).clone().into_inner();
|
2022-09-13 18:34:15 +01:00
|
|
|
index.extend(foreign_trait_items);
|
|
|
|
|
|
|
|
debug!("Constructing Output");
|
2021-01-22 16:46:19 -05:00
|
|
|
// This needs to be the default HashMap for compatibility with the public interface for
|
2022-02-13 01:25:54 +00:00
|
|
|
// rustdoc-json-types
|
2021-01-22 16:46:19 -05:00
|
|
|
#[allow(rustc::default_hash_types)]
|
2020-08-03 12:45:27 -05:00
|
|
|
let output = types::Crate {
|
2022-05-31 21:47:19 +02:00
|
|
|
root: types::Id(format!("0:0:{}", e.name(self.tcx).as_u32())),
|
2021-02-25 00:16:47 -05:00
|
|
|
crate_version: self.cache.crate_version.clone(),
|
2021-01-12 23:36:04 +01:00
|
|
|
includes_private: self.cache.document_private,
|
2021-01-22 18:30:01 -05:00
|
|
|
index: index.into_iter().collect(),
|
2021-01-12 23:36:04 +01:00
|
|
|
paths: self
|
|
|
|
.cache
|
2020-08-03 12:45:27 -05:00
|
|
|
.paths
|
2022-08-13 15:50:01 +02:00
|
|
|
.iter()
|
|
|
|
.chain(&self.cache.external_paths)
|
|
|
|
.map(|(&k, &(ref path, kind))| {
|
2020-08-03 12:45:27 -05:00
|
|
|
(
|
2022-05-31 21:47:19 +02:00
|
|
|
from_item_id(k.into(), self.tcx),
|
2021-03-07 18:09:35 +01:00
|
|
|
types::ItemSummary {
|
|
|
|
crate_id: k.krate.as_u32(),
|
2021-12-15 06:18:18 +11:00
|
|
|
path: path.iter().map(|s| s.to_string()).collect(),
|
2021-03-07 18:09:35 +01:00
|
|
|
kind: kind.into_tcx(self.tcx),
|
|
|
|
},
|
2020-08-03 12:45:27 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
2021-01-12 23:36:04 +01:00
|
|
|
external_crates: self
|
|
|
|
.cache
|
2020-08-03 12:45:27 -05:00
|
|
|
.extern_locations
|
|
|
|
.iter()
|
2021-04-29 19:14:29 +02:00
|
|
|
.map(|(crate_num, external_location)| {
|
|
|
|
let e = ExternalCrate { crate_num: *crate_num };
|
2020-08-03 12:45:27 -05:00
|
|
|
(
|
2021-04-29 19:14:29 +02:00
|
|
|
crate_num.as_u32(),
|
2020-08-03 12:45:27 -05:00
|
|
|
types::ExternalCrate {
|
2021-04-29 19:14:29 +02:00
|
|
|
name: e.name(self.tcx).to_string(),
|
|
|
|
html_root_url: match external_location {
|
2020-08-03 12:45:27 -05:00
|
|
|
ExternalLocation::Remote(s) => Some(s.clone()),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
2021-10-15 12:27:42 +03:00
|
|
|
format_version: types::FORMAT_VERSION,
|
2020-08-03 12:45:27 -05:00
|
|
|
};
|
2022-01-19 00:59:44 -08:00
|
|
|
let out_dir = self.out_path.clone();
|
|
|
|
try_err!(create_dir_all(&out_dir), out_dir);
|
|
|
|
|
|
|
|
let mut p = out_dir;
|
2020-08-03 12:45:27 -05:00
|
|
|
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
|
|
|
|
p.set_extension("json");
|
2022-02-13 01:25:54 +00:00
|
|
|
let mut file = BufWriter::new(try_err!(File::create(&p), p));
|
|
|
|
serde_json::ser::to_writer(&mut file, &output).unwrap();
|
|
|
|
try_err!(file.flush(), p);
|
|
|
|
|
2020-08-03 12:45:27 -05:00
|
|
|
Ok(())
|
2020-07-30 13:54:26 -05:00
|
|
|
}
|
2021-01-12 23:36:04 +01:00
|
|
|
|
|
|
|
fn cache(&self) -> &Cache {
|
|
|
|
&self.cache
|
|
|
|
}
|
2020-07-30 13:54:26 -05:00
|
|
|
}
|