]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/json/conversions.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / librustdoc / json / conversions.rs
CommitLineData
fc512014
XL
1//! These from impls are used to create the JSON types which get serialized. They're very close to
2//! the `clean` types but with some fields removed or stringified to simplify the output and not
3//! expose unstable compiler internals.
4
6a06907d
XL
5#![allow(rustc::default_hash_types)]
6
fc512014
XL
7use std::convert::From;
8
9use rustc_ast::ast;
5869c6ff 10use rustc_hir::def::CtorKind;
6a06907d 11use rustc_middle::ty::TyCtxt;
fc512014
XL
12use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
13use rustc_span::Pos;
14
5869c6ff
XL
15use rustdoc_json_types::*;
16
fc512014 17use crate::clean;
6a06907d 18use crate::clean::utils::print_const_expr;
fc512014 19use crate::formats::item_type::ItemType;
fc512014 20use crate::json::JsonRenderer;
6a06907d 21use std::collections::HashSet;
fc512014
XL
22
23impl JsonRenderer<'_> {
24 pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
fc512014 25 let deprecation = item.deprecation(self.tcx);
cdc7bbd5
XL
26 let links = self
27 .cache
28 .intra_doc_links
29 .get(&item.def_id)
30 .into_iter()
31 .flatten()
32 .filter_map(|clean::ItemLink { link, did, .. }| {
33 did.map(|did| (link.clone(), from_def_id(did)))
34 })
35 .collect();
36 let docs = item.attrs.collapsed_doc_value();
37 let attrs = item
38 .attrs
39 .other_attrs
40 .iter()
41 .map(rustc_ast_pretty::pprust::attribute_to_string)
42 .collect();
43 let span = item.span(self.tcx);
44 let clean::Item { name, attrs: _, kind: _, visibility, def_id, cfg: _ } = item;
45 let inner = match *item.kind {
6a06907d 46 clean::StrippedItem(_) => return None,
cdc7bbd5 47 _ => from_clean_item(item, self.tcx),
6a06907d
XL
48 };
49 Some(Item {
50 id: from_def_id(def_id),
51 crate_id: def_id.krate.as_u32(),
52 name: name.map(|sym| sym.to_string()),
cdc7bbd5 53 span: self.convert_span(span),
6a06907d 54 visibility: self.convert_visibility(visibility),
cdc7bbd5
XL
55 docs,
56 attrs,
6a06907d
XL
57 deprecation: deprecation.map(from_deprecation),
58 inner,
cdc7bbd5 59 links,
6a06907d 60 })
fc512014
XL
61 }
62
63 fn convert_span(&self, span: clean::Span) -> Option<Span> {
64 match span.filename(self.sess()) {
65 rustc_span::FileName::Real(name) => {
66 let hi = span.hi(self.sess());
67 let lo = span.lo(self.sess());
68 Some(Span {
69 filename: match name {
70 rustc_span::RealFileName::Named(path) => path,
71 rustc_span::RealFileName::Devirtualized { local_path, virtual_name: _ } => {
72 local_path
73 }
74 },
75 begin: (lo.line, lo.col.to_usize()),
76 end: (hi.line, hi.col.to_usize()),
77 })
78 }
79 _ => None,
80 }
81 }
82
83 fn convert_visibility(&self, v: clean::Visibility) -> Visibility {
84 use clean::Visibility::*;
85 match v {
86 Public => Visibility::Public,
87 Inherited => Visibility::Default,
88 Restricted(did) if did.index == CRATE_DEF_INDEX => Visibility::Crate,
89 Restricted(did) => Visibility::Restricted {
5869c6ff 90 parent: from_def_id(did),
fc512014
XL
91 path: self.tcx.def_path(did).to_string_no_crate_verbose(),
92 },
93 }
94 }
95}
96
cdc7bbd5
XL
97crate trait FromWithTcx<T> {
98 fn from_tcx(f: T, tcx: TyCtxt<'_>) -> Self;
99}
100
101crate trait IntoWithTcx<T> {
102 fn into_tcx(self, tcx: TyCtxt<'_>) -> T;
103}
104
105impl<T, U> IntoWithTcx<U> for T
106where
107 U: FromWithTcx<T>,
108{
109 fn into_tcx(self, tcx: TyCtxt<'_>) -> U {
110 U::from_tcx(self, tcx)
111 }
112}
113
5869c6ff
XL
114crate fn from_deprecation(deprecation: rustc_attr::Deprecation) -> Deprecation {
115 #[rustfmt::skip]
116 let rustc_attr::Deprecation { since, note, is_since_rustc_version: _, suggestion: _ } = deprecation;
117 Deprecation { since: since.map(|s| s.to_string()), note: note.map(|s| s.to_string()) }
fc512014
XL
118}
119
cdc7bbd5
XL
120impl FromWithTcx<clean::GenericArgs> for GenericArgs {
121 fn from_tcx(args: clean::GenericArgs, tcx: TyCtxt<'_>) -> Self {
fc512014
XL
122 use clean::GenericArgs::*;
123 match args {
124 AngleBracketed { args, bindings } => GenericArgs::AngleBracketed {
cdc7bbd5
XL
125 args: args.into_iter().map(|a| a.into_tcx(tcx)).collect(),
126 bindings: bindings.into_iter().map(|a| a.into_tcx(tcx)).collect(),
fc512014
XL
127 },
128 Parenthesized { inputs, output } => GenericArgs::Parenthesized {
cdc7bbd5
XL
129 inputs: inputs.into_iter().map(|a| a.into_tcx(tcx)).collect(),
130 output: output.map(|a| a.into_tcx(tcx)),
fc512014
XL
131 },
132 }
133 }
134}
135
cdc7bbd5
XL
136impl FromWithTcx<clean::GenericArg> for GenericArg {
137 fn from_tcx(arg: clean::GenericArg, tcx: TyCtxt<'_>) -> Self {
fc512014
XL
138 use clean::GenericArg::*;
139 match arg {
140 Lifetime(l) => GenericArg::Lifetime(l.0.to_string()),
cdc7bbd5
XL
141 Type(t) => GenericArg::Type(t.into_tcx(tcx)),
142 Const(c) => GenericArg::Const(c.into_tcx(tcx)),
fc512014
XL
143 }
144 }
145}
146
cdc7bbd5
XL
147impl FromWithTcx<clean::Constant> for Constant {
148 fn from_tcx(constant: clean::Constant, tcx: TyCtxt<'_>) -> Self {
149 let expr = constant.expr(tcx);
150 let value = constant.value(tcx);
151 let is_literal = constant.is_literal(tcx);
152 Constant { type_: constant.type_.into_tcx(tcx), expr, value, is_literal }
fc512014
XL
153 }
154}
155
cdc7bbd5
XL
156impl FromWithTcx<clean::TypeBinding> for TypeBinding {
157 fn from_tcx(binding: clean::TypeBinding, tcx: TyCtxt<'_>) -> Self {
158 TypeBinding { name: binding.name.to_string(), binding: binding.kind.into_tcx(tcx) }
fc512014
XL
159 }
160}
161
cdc7bbd5
XL
162impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind {
163 fn from_tcx(kind: clean::TypeBindingKind, tcx: TyCtxt<'_>) -> Self {
fc512014
XL
164 use clean::TypeBindingKind::*;
165 match kind {
cdc7bbd5 166 Equality { ty } => TypeBindingKind::Equality(ty.into_tcx(tcx)),
fc512014 167 Constraint { bounds } => {
cdc7bbd5 168 TypeBindingKind::Constraint(bounds.into_iter().map(|a| a.into_tcx(tcx)).collect())
fc512014
XL
169 }
170 }
171 }
172}
173
5869c6ff
XL
174crate fn from_def_id(did: DefId) -> Id {
175 Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index)))
fc512014
XL
176}
177
cdc7bbd5 178fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
6a06907d 179 use clean::ItemKind::*;
cdc7bbd5
XL
180 let name = item.name;
181 let is_crate = item.is_crate();
182 match *item.kind {
183 ModuleItem(m) => ItemEnum::Module(Module { is_crate, items: ids(m.items) }),
184 ImportItem(i) => ItemEnum::Import(i.into_tcx(tcx)),
185 StructItem(s) => ItemEnum::Struct(s.into_tcx(tcx)),
186 UnionItem(u) => ItemEnum::Union(u.into_tcx(tcx)),
187 StructFieldItem(f) => ItemEnum::StructField(f.into_tcx(tcx)),
188 EnumItem(e) => ItemEnum::Enum(e.into_tcx(tcx)),
189 VariantItem(v) => ItemEnum::Variant(v.into_tcx(tcx)),
190 FunctionItem(f) => ItemEnum::Function(f.into_tcx(tcx)),
191 ForeignFunctionItem(f) => ItemEnum::Function(f.into_tcx(tcx)),
192 TraitItem(t) => ItemEnum::Trait(t.into_tcx(tcx)),
193 TraitAliasItem(t) => ItemEnum::TraitAlias(t.into_tcx(tcx)),
194 MethodItem(m, _) => ItemEnum::Method(from_function_method(m, true, tcx)),
195 TyMethodItem(m) => ItemEnum::Method(from_function_method(m, false, tcx)),
196 ImplItem(i) => ItemEnum::Impl(i.into_tcx(tcx)),
197 StaticItem(s) => ItemEnum::Static(s.into_tcx(tcx)),
198 ForeignStaticItem(s) => ItemEnum::Static(s.into_tcx(tcx)),
6a06907d 199 ForeignTypeItem => ItemEnum::ForeignType,
cdc7bbd5
XL
200 TypedefItem(t, _) => ItemEnum::Typedef(t.into_tcx(tcx)),
201 OpaqueTyItem(t) => ItemEnum::OpaqueTy(t.into_tcx(tcx)),
202 ConstantItem(c) => ItemEnum::Constant(c.into_tcx(tcx)),
6a06907d 203 MacroItem(m) => ItemEnum::Macro(m.source),
cdc7bbd5
XL
204 ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
205 AssocConstItem(t, s) => ItemEnum::AssocConst { type_: t.into_tcx(tcx), default: s },
6a06907d 206 AssocTypeItem(g, t) => ItemEnum::AssocType {
cdc7bbd5
XL
207 bounds: g.into_iter().map(|x| x.into_tcx(tcx)).collect(),
208 default: t.map(|x| x.into_tcx(tcx)),
6a06907d 209 },
cdc7bbd5
XL
210 // `convert_item` early returns `None` for striped items
211 StrippedItem(_) => unreachable!(),
6a06907d
XL
212 PrimitiveItem(_) | KeywordItem(_) => {
213 panic!("{:?} is not supported for JSON output", item)
fc512014 214 }
6a06907d
XL
215 ExternCrateItem { ref src } => ItemEnum::ExternCrate {
216 name: name.as_ref().unwrap().to_string(),
217 rename: src.map(|x| x.to_string()),
218 },
fc512014
XL
219 }
220}
221
cdc7bbd5
XL
222impl FromWithTcx<clean::Struct> for Struct {
223 fn from_tcx(struct_: clean::Struct, tcx: TyCtxt<'_>) -> Self {
fc512014
XL
224 let clean::Struct { struct_type, generics, fields, fields_stripped } = struct_;
225 Struct {
5869c6ff 226 struct_type: from_ctor_kind(struct_type),
cdc7bbd5 227 generics: generics.into_tcx(tcx),
fc512014
XL
228 fields_stripped,
229 fields: ids(fields),
230 impls: Vec::new(), // Added in JsonRenderer::item
231 }
232 }
233}
234
cdc7bbd5
XL
235impl FromWithTcx<clean::Union> for Union {
236 fn from_tcx(struct_: clean::Union, tcx: TyCtxt<'_>) -> Self {
5869c6ff
XL
237 let clean::Union { generics, fields, fields_stripped } = struct_;
238 Union {
cdc7bbd5 239 generics: generics.into_tcx(tcx),
fc512014
XL
240 fields_stripped,
241 fields: ids(fields),
242 impls: Vec::new(), // Added in JsonRenderer::item
243 }
244 }
245}
246
5869c6ff
XL
247crate fn from_ctor_kind(struct_type: CtorKind) -> StructType {
248 match struct_type {
249 CtorKind::Fictive => StructType::Plain,
250 CtorKind::Fn => StructType::Tuple,
251 CtorKind::Const => StructType::Unit,
fc512014
XL
252 }
253}
254
6a06907d
XL
255crate fn from_fn_header(header: &rustc_hir::FnHeader) -> HashSet<Qualifiers> {
256 let mut v = HashSet::new();
257
258 if let rustc_hir::Unsafety::Unsafe = header.unsafety {
259 v.insert(Qualifiers::Unsafe);
260 }
261
262 if let rustc_hir::IsAsync::Async = header.asyncness {
263 v.insert(Qualifiers::Async);
fc512014 264 }
6a06907d
XL
265
266 if let rustc_hir::Constness::Const = header.constness {
267 v.insert(Qualifiers::Const);
fc512014 268 }
6a06907d
XL
269
270 v
fc512014
XL
271}
272
cdc7bbd5
XL
273impl FromWithTcx<clean::Function> for Function {
274 fn from_tcx(function: clean::Function, tcx: TyCtxt<'_>) -> Self {
6a06907d 275 let clean::Function { decl, generics, header } = function;
fc512014 276 Function {
cdc7bbd5
XL
277 decl: decl.into_tcx(tcx),
278 generics: generics.into_tcx(tcx),
6a06907d 279 header: from_fn_header(&header),
fc512014
XL
280 abi: header.abi.to_string(),
281 }
282 }
283}
284
cdc7bbd5
XL
285impl FromWithTcx<clean::Generics> for Generics {
286 fn from_tcx(generics: clean::Generics, tcx: TyCtxt<'_>) -> Self {
fc512014 287 Generics {
cdc7bbd5
XL
288 params: generics.params.into_iter().map(|x| x.into_tcx(tcx)).collect(),
289 where_predicates: generics
290 .where_predicates
291 .into_iter()
292 .map(|x| x.into_tcx(tcx))
293 .collect(),
fc512014
XL
294 }
295 }
296}
297
cdc7bbd5
XL
298impl FromWithTcx<clean::GenericParamDef> for GenericParamDef {
299 fn from_tcx(generic_param: clean::GenericParamDef, tcx: TyCtxt<'_>) -> Self {
300 GenericParamDef {
301 name: generic_param.name.to_string(),
302 kind: generic_param.kind.into_tcx(tcx),
303 }
fc512014
XL
304 }
305}
306
cdc7bbd5
XL
307impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
308 fn from_tcx(kind: clean::GenericParamDefKind, tcx: TyCtxt<'_>) -> Self {
fc512014
XL
309 use clean::GenericParamDefKind::*;
310 match kind {
311 Lifetime => GenericParamDefKind::Lifetime,
312 Type { did: _, bounds, default, synthetic: _ } => GenericParamDefKind::Type {
cdc7bbd5
XL
313 bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
314 default: default.map(|x| x.into_tcx(tcx)),
fc512014 315 },
cdc7bbd5 316 Const { did: _, ty } => GenericParamDefKind::Const(ty.into_tcx(tcx)),
fc512014
XL
317 }
318 }
319}
320
cdc7bbd5
XL
321impl FromWithTcx<clean::WherePredicate> for WherePredicate {
322 fn from_tcx(predicate: clean::WherePredicate, tcx: TyCtxt<'_>) -> Self {
fc512014
XL
323 use clean::WherePredicate::*;
324 match predicate {
325 BoundPredicate { ty, bounds } => WherePredicate::BoundPredicate {
cdc7bbd5
XL
326 ty: ty.into_tcx(tcx),
327 bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
fc512014
XL
328 },
329 RegionPredicate { lifetime, bounds } => WherePredicate::RegionPredicate {
330 lifetime: lifetime.0.to_string(),
cdc7bbd5 331 bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
fc512014
XL
332 },
333 EqPredicate { lhs, rhs } => {
cdc7bbd5 334 WherePredicate::EqPredicate { lhs: lhs.into_tcx(tcx), rhs: rhs.into_tcx(tcx) }
fc512014
XL
335 }
336 }
337 }
338}
339
cdc7bbd5
XL
340impl FromWithTcx<clean::GenericBound> for GenericBound {
341 fn from_tcx(bound: clean::GenericBound, tcx: TyCtxt<'_>) -> Self {
fc512014
XL
342 use clean::GenericBound::*;
343 match bound {
344 TraitBound(clean::PolyTrait { trait_, generic_params }, modifier) => {
345 GenericBound::TraitBound {
cdc7bbd5
XL
346 trait_: trait_.into_tcx(tcx),
347 generic_params: generic_params.into_iter().map(|x| x.into_tcx(tcx)).collect(),
5869c6ff 348 modifier: from_trait_bound_modifier(modifier),
fc512014
XL
349 }
350 }
351 Outlives(lifetime) => GenericBound::Outlives(lifetime.0.to_string()),
352 }
353 }
354}
355
5869c6ff
XL
356crate fn from_trait_bound_modifier(modifier: rustc_hir::TraitBoundModifier) -> TraitBoundModifier {
357 use rustc_hir::TraitBoundModifier::*;
358 match modifier {
359 None => TraitBoundModifier::None,
360 Maybe => TraitBoundModifier::Maybe,
361 MaybeConst => TraitBoundModifier::MaybeConst,
fc512014
XL
362 }
363}
364
cdc7bbd5
XL
365impl FromWithTcx<clean::Type> for Type {
366 fn from_tcx(ty: clean::Type, tcx: TyCtxt<'_>) -> Self {
fc512014
XL
367 use clean::Type::*;
368 match ty {
369 ResolvedPath { path, param_names, did, is_generic: _ } => Type::ResolvedPath {
370 name: path.whole_name(),
5869c6ff 371 id: from_def_id(did),
cdc7bbd5 372 args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
fc512014 373 param_names: param_names
cdc7bbd5 374 .map(|v| v.into_iter().map(|x| x.into_tcx(tcx)).collect())
fc512014
XL
375 .unwrap_or_default(),
376 },
377 Generic(s) => Type::Generic(s.to_string()),
378 Primitive(p) => Type::Primitive(p.as_str().to_string()),
cdc7bbd5
XL
379 BareFunction(f) => Type::FunctionPointer(Box::new((*f).into_tcx(tcx))),
380 Tuple(t) => Type::Tuple(t.into_iter().map(|x| x.into_tcx(tcx)).collect()),
381 Slice(t) => Type::Slice(Box::new((*t).into_tcx(tcx))),
382 Array(t, s) => Type::Array { type_: Box::new((*t).into_tcx(tcx)), len: s },
383 ImplTrait(g) => Type::ImplTrait(g.into_iter().map(|x| x.into_tcx(tcx)).collect()),
fc512014
XL
384 Never => Type::Never,
385 Infer => Type::Infer,
386 RawPointer(mutability, type_) => Type::RawPointer {
387 mutable: mutability == ast::Mutability::Mut,
cdc7bbd5 388 type_: Box::new((*type_).into_tcx(tcx)),
fc512014
XL
389 },
390 BorrowedRef { lifetime, mutability, type_ } => Type::BorrowedRef {
391 lifetime: lifetime.map(|l| l.0.to_string()),
392 mutable: mutability == ast::Mutability::Mut,
cdc7bbd5 393 type_: Box::new((*type_).into_tcx(tcx)),
fc512014
XL
394 },
395 QPath { name, self_type, trait_ } => Type::QualifiedPath {
396 name: name.to_string(),
cdc7bbd5
XL
397 self_type: Box::new((*self_type).into_tcx(tcx)),
398 trait_: Box::new((*trait_).into_tcx(tcx)),
fc512014
XL
399 },
400 }
401 }
402}
403
cdc7bbd5
XL
404impl FromWithTcx<clean::BareFunctionDecl> for FunctionPointer {
405 fn from_tcx(bare_decl: clean::BareFunctionDecl, tcx: TyCtxt<'_>) -> Self {
fc512014
XL
406 let clean::BareFunctionDecl { unsafety, generic_params, decl, abi } = bare_decl;
407 FunctionPointer {
6a06907d
XL
408 header: if let rustc_hir::Unsafety::Unsafe = unsafety {
409 let mut hs = HashSet::new();
410 hs.insert(Qualifiers::Unsafe);
411 hs
412 } else {
413 HashSet::new()
414 },
cdc7bbd5
XL
415 generic_params: generic_params.into_iter().map(|x| x.into_tcx(tcx)).collect(),
416 decl: decl.into_tcx(tcx),
fc512014
XL
417 abi: abi.to_string(),
418 }
419 }
420}
421
cdc7bbd5
XL
422impl FromWithTcx<clean::FnDecl> for FnDecl {
423 fn from_tcx(decl: clean::FnDecl, tcx: TyCtxt<'_>) -> Self {
424 let clean::FnDecl { inputs, output, c_variadic } = decl;
fc512014
XL
425 FnDecl {
426 inputs: inputs
427 .values
428 .into_iter()
cdc7bbd5 429 .map(|arg| (arg.name.to_string(), arg.type_.into_tcx(tcx)))
fc512014
XL
430 .collect(),
431 output: match output {
cdc7bbd5 432 clean::FnRetTy::Return(t) => Some(t.into_tcx(tcx)),
fc512014
XL
433 clean::FnRetTy::DefaultReturn => None,
434 },
435 c_variadic,
436 }
437 }
438}
439
cdc7bbd5
XL
440impl FromWithTcx<clean::Trait> for Trait {
441 fn from_tcx(trait_: clean::Trait, tcx: TyCtxt<'_>) -> Self {
6a06907d 442 let clean::Trait { unsafety, items, generics, bounds, is_auto } = trait_;
fc512014
XL
443 Trait {
444 is_auto,
445 is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
446 items: ids(items),
cdc7bbd5
XL
447 generics: generics.into_tcx(tcx),
448 bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
fc512014
XL
449 implementors: Vec::new(), // Added in JsonRenderer::item
450 }
451 }
452}
453
cdc7bbd5
XL
454impl FromWithTcx<clean::Impl> for Impl {
455 fn from_tcx(impl_: clean::Impl, tcx: TyCtxt<'_>) -> Self {
fc512014
XL
456 let clean::Impl {
457 unsafety,
458 generics,
459 provided_trait_methods,
460 trait_,
461 for_,
462 items,
5869c6ff 463 negative_polarity,
fc512014
XL
464 synthetic,
465 blanket_impl,
cdc7bbd5 466 span: _span,
fc512014
XL
467 } = impl_;
468 Impl {
469 is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
cdc7bbd5 470 generics: generics.into_tcx(tcx),
fc512014
XL
471 provided_trait_methods: provided_trait_methods
472 .into_iter()
473 .map(|x| x.to_string())
474 .collect(),
cdc7bbd5
XL
475 trait_: trait_.map(|x| x.into_tcx(tcx)),
476 for_: for_.into_tcx(tcx),
fc512014 477 items: ids(items),
5869c6ff 478 negative: negative_polarity,
fc512014 479 synthetic,
cdc7bbd5 480 blanket_impl: blanket_impl.map(|x| x.into_tcx(tcx)),
fc512014
XL
481 }
482 }
483}
484
cdc7bbd5
XL
485crate fn from_function_method(
486 function: clean::Function,
487 has_body: bool,
488 tcx: TyCtxt<'_>,
489) -> Method {
6a06907d 490 let clean::Function { header, decl, generics } = function;
5869c6ff 491 Method {
cdc7bbd5
XL
492 decl: decl.into_tcx(tcx),
493 generics: generics.into_tcx(tcx),
6a06907d
XL
494 header: from_fn_header(&header),
495 abi: header.abi.to_string(),
5869c6ff 496 has_body,
fc512014
XL
497 }
498}
499
cdc7bbd5
XL
500impl FromWithTcx<clean::Enum> for Enum {
501 fn from_tcx(enum_: clean::Enum, tcx: TyCtxt<'_>) -> Self {
fc512014
XL
502 let clean::Enum { variants, generics, variants_stripped } = enum_;
503 Enum {
cdc7bbd5 504 generics: generics.into_tcx(tcx),
fc512014
XL
505 variants_stripped,
506 variants: ids(variants),
507 impls: Vec::new(), // Added in JsonRenderer::item
508 }
509 }
510}
511
cdc7bbd5
XL
512impl FromWithTcx<clean::VariantStruct> for Struct {
513 fn from_tcx(struct_: clean::VariantStruct, _tcx: TyCtxt<'_>) -> Self {
fc512014
XL
514 let clean::VariantStruct { struct_type, fields, fields_stripped } = struct_;
515 Struct {
5869c6ff 516 struct_type: from_ctor_kind(struct_type),
fc512014
XL
517 generics: Default::default(),
518 fields_stripped,
519 fields: ids(fields),
520 impls: Vec::new(),
521 }
522 }
523}
524
cdc7bbd5
XL
525impl FromWithTcx<clean::Variant> for Variant {
526 fn from_tcx(variant: clean::Variant, tcx: TyCtxt<'_>) -> Self {
5869c6ff
XL
527 use clean::Variant::*;
528 match variant {
fc512014 529 CLike => Variant::Plain,
cdc7bbd5 530 Tuple(t) => Variant::Tuple(t.into_iter().map(|x| x.into_tcx(tcx)).collect()),
fc512014
XL
531 Struct(s) => Variant::Struct(ids(s.fields)),
532 }
533 }
534}
535
cdc7bbd5
XL
536impl FromWithTcx<clean::Import> for Import {
537 fn from_tcx(import: clean::Import, _tcx: TyCtxt<'_>) -> Self {
fc512014
XL
538 use clean::ImportKind::*;
539 match import.kind {
540 Simple(s) => Import {
cdc7bbd5 541 source: import.source.path.whole_name(),
fc512014 542 name: s.to_string(),
5869c6ff 543 id: import.source.did.map(from_def_id),
fc512014
XL
544 glob: false,
545 },
546 Glob => Import {
cdc7bbd5 547 source: import.source.path.whole_name(),
fc512014 548 name: import.source.path.last_name().to_string(),
5869c6ff 549 id: import.source.did.map(from_def_id),
fc512014
XL
550 glob: true,
551 },
552 }
553 }
554}
555
cdc7bbd5
XL
556impl FromWithTcx<clean::ProcMacro> for ProcMacro {
557 fn from_tcx(mac: clean::ProcMacro, _tcx: TyCtxt<'_>) -> Self {
fc512014 558 ProcMacro {
5869c6ff 559 kind: from_macro_kind(mac.kind),
fc512014
XL
560 helpers: mac.helpers.iter().map(|x| x.to_string()).collect(),
561 }
562 }
563}
564
5869c6ff
XL
565crate fn from_macro_kind(kind: rustc_span::hygiene::MacroKind) -> MacroKind {
566 use rustc_span::hygiene::MacroKind::*;
567 match kind {
568 Bang => MacroKind::Bang,
569 Attr => MacroKind::Attr,
570 Derive => MacroKind::Derive,
fc512014
XL
571 }
572}
573
cdc7bbd5
XL
574impl FromWithTcx<clean::Typedef> for Typedef {
575 fn from_tcx(typedef: clean::Typedef, tcx: TyCtxt<'_>) -> Self {
fc512014 576 let clean::Typedef { type_, generics, item_type: _ } = typedef;
cdc7bbd5 577 Typedef { type_: type_.into_tcx(tcx), generics: generics.into_tcx(tcx) }
fc512014
XL
578 }
579}
580
cdc7bbd5
XL
581impl FromWithTcx<clean::OpaqueTy> for OpaqueTy {
582 fn from_tcx(opaque: clean::OpaqueTy, tcx: TyCtxt<'_>) -> Self {
fc512014 583 OpaqueTy {
cdc7bbd5
XL
584 bounds: opaque.bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
585 generics: opaque.generics.into_tcx(tcx),
fc512014
XL
586 }
587 }
588}
589
cdc7bbd5
XL
590impl FromWithTcx<clean::Static> for Static {
591 fn from_tcx(stat: clean::Static, tcx: TyCtxt<'_>) -> Self {
592 Static {
593 type_: stat.type_.into_tcx(tcx),
594 mutable: stat.mutability == ast::Mutability::Mut,
595 expr: stat.expr.map(|e| print_const_expr(tcx, e)).unwrap_or_default(),
596 }
fc512014
XL
597 }
598}
599
cdc7bbd5
XL
600impl FromWithTcx<clean::TraitAlias> for TraitAlias {
601 fn from_tcx(alias: clean::TraitAlias, tcx: TyCtxt<'_>) -> Self {
fc512014 602 TraitAlias {
cdc7bbd5
XL
603 generics: alias.generics.into_tcx(tcx),
604 params: alias.bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
fc512014
XL
605 }
606 }
607}
608
cdc7bbd5
XL
609impl FromWithTcx<ItemType> for ItemKind {
610 fn from_tcx(kind: ItemType, _tcx: TyCtxt<'_>) -> Self {
fc512014
XL
611 use ItemType::*;
612 match kind {
613 Module => ItemKind::Module,
614 ExternCrate => ItemKind::ExternCrate,
615 Import => ItemKind::Import,
616 Struct => ItemKind::Struct,
617 Union => ItemKind::Union,
618 Enum => ItemKind::Enum,
619 Function => ItemKind::Function,
620 Typedef => ItemKind::Typedef,
621 OpaqueTy => ItemKind::OpaqueTy,
622 Static => ItemKind::Static,
623 Constant => ItemKind::Constant,
624 Trait => ItemKind::Trait,
625 Impl => ItemKind::Impl,
626 TyMethod | Method => ItemKind::Method,
627 StructField => ItemKind::StructField,
628 Variant => ItemKind::Variant,
629 Macro => ItemKind::Macro,
630 Primitive => ItemKind::Primitive,
631 AssocConst => ItemKind::AssocConst,
632 AssocType => ItemKind::AssocType,
633 ForeignType => ItemKind::ForeignType,
634 Keyword => ItemKind::Keyword,
635 TraitAlias => ItemKind::TraitAlias,
636 ProcAttribute => ItemKind::ProcAttribute,
637 ProcDerive => ItemKind::ProcDerive,
638 }
639 }
640}
641
642fn ids(items: impl IntoIterator<Item = clean::Item>) -> Vec<Id> {
5869c6ff 643 items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_def_id(i.def_id)).collect()
fc512014 644}