]>
Commit | Line | Data |
---|---|---|
0731742a XL |
1 | //! The Rust AST Visitor. Extracts useful information and massages it into a form |
2 | //! usable for `clean`. | |
1a4d82fc | 3 | |
0731742a | 4 | use rustc::hir::{self, Node}; |
48663c56 | 5 | use rustc::hir::def::{Res, DefKind}; |
0731742a XL |
6 | use rustc::hir::def_id::{DefId, LOCAL_CRATE}; |
7 | use rustc::middle::privacy::AccessLevel; | |
8 | use rustc::util::nodemap::{FxHashSet, FxHashMap}; | |
416331ca | 9 | use rustc::ty::TyCtxt; |
1a4d82fc | 10 | use syntax::ast; |
b7449926 | 11 | use syntax::source_map::Spanned; |
48663c56 | 12 | use syntax::symbol::sym; |
e74abb32 | 13 | use syntax_pos::hygiene::MacroKind; |
8faf50e0 | 14 | use syntax_pos::{self, Span}; |
1a4d82fc | 15 | |
0731742a | 16 | use std::mem; |
e9174d1e | 17 | |
9fa01778 | 18 | use crate::core; |
416331ca | 19 | use crate::clean::{self, AttributesExt, NestedAttributesExt}; |
9fa01778 XL |
20 | use crate::doctree::*; |
21 | ||
416331ca XL |
22 | // FIXME: Should this be replaced with tcx.def_path_str? |
23 | fn def_id_to_path( | |
24 | tcx: TyCtxt<'_>, | |
25 | did: DefId, | |
26 | ) -> Vec<String> { | |
27 | let crate_name = tcx.crate_name(did.krate).to_string(); | |
28 | let relative = tcx.def_path(did).data.into_iter().filter_map(|elem| { | |
29 | // extern blocks have an empty name | |
30 | let s = elem.data.to_string(); | |
31 | if !s.is_empty() { | |
32 | Some(s) | |
33 | } else { | |
34 | None | |
35 | } | |
36 | }); | |
37 | std::iter::once(crate_name).chain(relative).collect() | |
38 | } | |
1a4d82fc | 39 | |
0731742a XL |
40 | // Also, is there some reason that this doesn't use the 'visit' |
41 | // framework from syntax?. | |
1a4d82fc | 42 | |
532ac7d7 | 43 | pub struct RustdocVisitor<'a, 'tcx> { |
e1599b0c | 44 | cx: &'a mut core::DocContext<'tcx>, |
48663c56 | 45 | view_item_stack: FxHashSet<hir::HirId>, |
476ff2be | 46 | inlining: bool, |
0731742a | 47 | /// Are the current module and all of its parents public? |
476ff2be | 48 | inside_public_path: bool, |
416331ca | 49 | exact_paths: FxHashMap<DefId, Vec<String>>, |
1a4d82fc JJ |
50 | } |
51 | ||
532ac7d7 | 52 | impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { |
b7449926 | 53 | pub fn new( |
e1599b0c | 54 | cx: &'a mut core::DocContext<'tcx> |
532ac7d7 | 55 | ) -> RustdocVisitor<'a, 'tcx> { |
2c00a5a8 | 56 | // If the root is re-exported, terminate all recursion. |
0bf4aa26 | 57 | let mut stack = FxHashSet::default(); |
48663c56 | 58 | stack.insert(hir::CRATE_HIR_ID); |
1a4d82fc | 59 | RustdocVisitor { |
3b2f2976 | 60 | cx, |
1a4d82fc | 61 | view_item_stack: stack, |
476ff2be SL |
62 | inlining: false, |
63 | inside_public_path: true, | |
416331ca | 64 | exact_paths: FxHashMap::default(), |
1a4d82fc JJ |
65 | } |
66 | } | |
67 | ||
0531ce1d | 68 | fn store_path(&mut self, did: DefId) { |
416331ca XL |
69 | let tcx = self.cx.tcx; |
70 | self.exact_paths.entry(did).or_insert_with(|| def_id_to_path(tcx, did)); | |
9cc50fc6 SL |
71 | } |
72 | ||
416331ca | 73 | pub fn visit(mut self, krate: &'tcx hir::Crate) -> Module<'tcx> { |
dc9dc135 XL |
74 | let mut module = self.visit_mod_contents(krate.span, |
75 | &krate.attrs, | |
76 | &Spanned { span: syntax_pos::DUMMY_SP, | |
8faf50e0 | 77 | node: hir::VisibilityKind::Public }, |
532ac7d7 | 78 | hir::CRATE_HIR_ID, |
1a4d82fc JJ |
79 | &krate.module, |
80 | None); | |
0731742a | 81 | // Attach the crate's exported macros to the top-level module: |
dc9dc135 XL |
82 | module.macros.extend( |
83 | krate.exported_macros.iter().map(|def| self.visit_local_macro(def, None)), | |
84 | ); | |
85 | module.is_crate = true; | |
0531ce1d | 86 | |
e1599b0c | 87 | self.cx.renderinfo.get_mut().exact_paths = self.exact_paths; |
416331ca XL |
88 | |
89 | module | |
1a4d82fc JJ |
90 | } |
91 | ||
416331ca | 92 | fn visit_variant_data(&mut self, item: &'tcx hir::Item, |
dc9dc135 XL |
93 | name: ast::Name, sd: &'tcx hir::VariantData, |
94 | generics: &'tcx hir::Generics) -> Struct<'tcx> { | |
416331ca | 95 | debug!("visiting struct"); |
1a4d82fc JJ |
96 | let struct_type = struct_type_from_def(&*sd); |
97 | Struct { | |
532ac7d7 | 98 | id: item.hir_id, |
3b2f2976 XL |
99 | struct_type, |
100 | name, | |
dc9dc135 | 101 | vis: &item.vis, |
dc9dc135 XL |
102 | attrs: &item.attrs, |
103 | generics, | |
104 | fields: sd.fields(), | |
1a4d82fc JJ |
105 | whence: item.span |
106 | } | |
107 | } | |
108 | ||
416331ca | 109 | fn visit_union_data(&mut self, item: &'tcx hir::Item, |
dc9dc135 XL |
110 | name: ast::Name, sd: &'tcx hir::VariantData, |
111 | generics: &'tcx hir::Generics) -> Union<'tcx> { | |
416331ca | 112 | debug!("visiting union"); |
9e0c209e SL |
113 | let struct_type = struct_type_from_def(&*sd); |
114 | Union { | |
532ac7d7 | 115 | id: item.hir_id, |
3b2f2976 XL |
116 | struct_type, |
117 | name, | |
dc9dc135 | 118 | vis: &item.vis, |
dc9dc135 XL |
119 | attrs: &item.attrs, |
120 | generics, | |
121 | fields: sd.fields(), | |
9e0c209e SL |
122 | whence: item.span |
123 | } | |
124 | } | |
125 | ||
416331ca | 126 | fn visit_enum_def(&mut self, it: &'tcx hir::Item, |
dc9dc135 XL |
127 | name: ast::Name, def: &'tcx hir::EnumDef, |
128 | generics: &'tcx hir::Generics) -> Enum<'tcx> { | |
416331ca | 129 | debug!("visiting enum"); |
1a4d82fc | 130 | Enum { |
3b2f2976 | 131 | name, |
1a4d82fc | 132 | variants: def.variants.iter().map(|v| Variant { |
e1599b0c XL |
133 | name: v.ident.name, |
134 | id: v.id, | |
135 | attrs: &v.attrs, | |
136 | def: &v.data, | |
1a4d82fc JJ |
137 | whence: v.span, |
138 | }).collect(), | |
dc9dc135 | 139 | vis: &it.vis, |
dc9dc135 XL |
140 | generics, |
141 | attrs: &it.attrs, | |
532ac7d7 | 142 | id: it.hir_id, |
1a4d82fc JJ |
143 | whence: it.span, |
144 | } | |
145 | } | |
146 | ||
416331ca | 147 | fn visit_fn(&mut self, om: &mut Module<'tcx>, item: &'tcx hir::Item, |
dc9dc135 | 148 | name: ast::Name, decl: &'tcx hir::FnDecl, |
8faf50e0 | 149 | header: hir::FnHeader, |
dc9dc135 | 150 | generics: &'tcx hir::Generics, |
0bf4aa26 | 151 | body: hir::BodyId) { |
416331ca | 152 | debug!("visiting fn"); |
0bf4aa26 | 153 | let macro_kind = item.attrs.iter().filter_map(|a| { |
48663c56 | 154 | if a.check_name(sym::proc_macro) { |
0bf4aa26 | 155 | Some(MacroKind::Bang) |
48663c56 | 156 | } else if a.check_name(sym::proc_macro_derive) { |
0bf4aa26 | 157 | Some(MacroKind::Derive) |
48663c56 | 158 | } else if a.check_name(sym::proc_macro_attribute) { |
0bf4aa26 XL |
159 | Some(MacroKind::Attr) |
160 | } else { | |
161 | None | |
162 | } | |
163 | }).next(); | |
164 | match macro_kind { | |
165 | Some(kind) => { | |
166 | let name = if kind == MacroKind::Derive { | |
48663c56 | 167 | item.attrs.lists(sym::proc_macro_derive) |
9fa01778 | 168 | .filter_map(|mi| mi.ident()) |
0bf4aa26 XL |
169 | .next() |
170 | .expect("proc-macro derives require a name") | |
9fa01778 | 171 | .name |
0bf4aa26 XL |
172 | } else { |
173 | name | |
174 | }; | |
175 | ||
176 | let mut helpers = Vec::new(); | |
48663c56 XL |
177 | for mi in item.attrs.lists(sym::proc_macro_derive) { |
178 | if !mi.check_name(sym::attributes) { | |
0bf4aa26 XL |
179 | continue; |
180 | } | |
181 | ||
182 | if let Some(list) = mi.meta_item_list() { | |
183 | for inner_mi in list { | |
9fa01778 XL |
184 | if let Some(ident) = inner_mi.ident() { |
185 | helpers.push(ident.name); | |
0bf4aa26 XL |
186 | } |
187 | } | |
188 | } | |
189 | } | |
190 | ||
191 | om.proc_macros.push(ProcMacro { | |
192 | name, | |
532ac7d7 | 193 | id: item.hir_id, |
0bf4aa26 XL |
194 | kind, |
195 | helpers, | |
dc9dc135 | 196 | attrs: &item.attrs, |
0bf4aa26 | 197 | whence: item.span, |
0bf4aa26 XL |
198 | }); |
199 | } | |
200 | None => { | |
201 | om.fns.push(Function { | |
532ac7d7 | 202 | id: item.hir_id, |
dc9dc135 | 203 | vis: &item.vis, |
dc9dc135 XL |
204 | attrs: &item.attrs, |
205 | decl, | |
0bf4aa26 XL |
206 | name, |
207 | whence: item.span, | |
dc9dc135 | 208 | generics, |
0bf4aa26 XL |
209 | header, |
210 | body, | |
211 | }); | |
212 | } | |
1a4d82fc JJ |
213 | } |
214 | } | |
215 | ||
416331ca | 216 | fn visit_mod_contents(&mut self, span: Span, attrs: &'tcx hir::HirVec<ast::Attribute>, |
dc9dc135 XL |
217 | vis: &'tcx hir::Visibility, id: hir::HirId, |
218 | m: &'tcx hir::Mod, | |
219 | name: Option<ast::Name>) -> Module<'tcx> { | |
220 | let mut om = Module::new(name, attrs, vis); | |
1a4d82fc JJ |
221 | om.where_outer = span; |
222 | om.where_inner = m.inner; | |
416331ca | 223 | om.id = id; |
476ff2be SL |
224 | // Keep track of if there were any private modules in the path. |
225 | let orig_inside_public_path = self.inside_public_path; | |
8faf50e0 | 226 | self.inside_public_path &= vis.node.is_pub(); |
92a42be0 | 227 | for i in &m.item_ids { |
dc9dc135 | 228 | let item = self.cx.tcx.hir().expect_item(i.id); |
92a42be0 | 229 | self.visit_item(item, None, &mut om); |
1a4d82fc | 230 | } |
476ff2be | 231 | self.inside_public_path = orig_inside_public_path; |
476ff2be | 232 | om |
1a4d82fc JJ |
233 | } |
234 | ||
54a0048b SL |
235 | /// Tries to resolve the target of a `pub use` statement and inlines the |
236 | /// target if it is defined locally and would not be documented otherwise, | |
237 | /// or when it is specifically requested with `please_inline`. | |
238 | /// (the latter is the case when the import is marked `doc(inline)`) | |
239 | /// | |
240 | /// Cross-crate inlining occurs later on during crate cleaning | |
241 | /// and follows different rules. | |
242 | /// | |
9fa01778 | 243 | /// Returns `true` if the target has been inlined. |
476ff2be | 244 | fn maybe_inline_local(&mut self, |
532ac7d7 | 245 | id: hir::HirId, |
48663c56 | 246 | res: Res, |
0731742a | 247 | renamed: Option<ast::Ident>, |
476ff2be | 248 | glob: bool, |
dc9dc135 | 249 | om: &mut Module<'tcx>, |
476ff2be | 250 | please_inline: bool) -> bool { |
54a0048b | 251 | |
48663c56 | 252 | fn inherits_doc_hidden(cx: &core::DocContext<'_>, mut node: hir::HirId) -> bool { |
0731742a | 253 | while let Some(id) = cx.tcx.hir().get_enclosing_scope(node) { |
54a0048b | 254 | node = id; |
dc9dc135 | 255 | if cx.tcx.hir().attrs(node) |
48663c56 | 256 | .lists(sym::doc).has_word(sym::hidden) { |
54a0048b SL |
257 | return true; |
258 | } | |
48663c56 | 259 | if node == hir::CRATE_HIR_ID { |
54a0048b SL |
260 | break; |
261 | } | |
262 | } | |
263 | false | |
264 | } | |
265 | ||
48663c56 | 266 | debug!("maybe_inline_local res: {:?}", res); |
cc61c64b | 267 | |
476ff2be | 268 | let tcx = self.cx.tcx; |
48663c56 | 269 | let res_did = if let Some(did) = res.opt_def_id() { |
9fa01778 XL |
270 | did |
271 | } else { | |
476ff2be | 272 | return false; |
9fa01778 | 273 | }; |
54a0048b | 274 | |
dc9dc135 | 275 | let use_attrs = tcx.hir().attrs(id); |
0731742a | 276 | // Don't inline `doc(hidden)` imports so they can be stripped at a later stage. |
48663c56 XL |
277 | let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline) || |
278 | use_attrs.lists(sym::doc).has_word(sym::hidden); | |
a7813a04 XL |
279 | |
280 | // For cross-crate impl inlining we need to know whether items are | |
0731742a | 281 | // reachable in documentation -- a previously nonreachable item can be |
a7813a04 | 282 | // made reachable by cross-crate inlining which we're checking here. |
0731742a | 283 | // (this is done here because we need to know this upfront). |
48663c56 XL |
284 | if !res_did.is_local() && !is_no_inline { |
285 | let attrs = clean::inline::load_attrs(self.cx, res_did); | |
286 | let self_is_hidden = attrs.lists(sym::doc).has_word(sym::hidden); | |
287 | match res { | |
288 | Res::Def(DefKind::Trait, did) | | |
289 | Res::Def(DefKind::Struct, did) | | |
290 | Res::Def(DefKind::Union, did) | | |
291 | Res::Def(DefKind::Enum, did) | | |
292 | Res::Def(DefKind::ForeignTy, did) | | |
293 | Res::Def(DefKind::TyAlias, did) if !self_is_hidden => { | |
0bf4aa26 | 294 | self.cx.renderinfo |
e1599b0c | 295 | .get_mut() |
0bf4aa26 XL |
296 | .access_levels.map |
297 | .insert(did, AccessLevel::Public); | |
a7813a04 | 298 | }, |
48663c56 | 299 | Res::Def(DefKind::Mod, did) => if !self_is_hidden { |
9fa01778 | 300 | crate::visit_lib::LibEmbargoVisitor::new(self.cx).visit_mod(did); |
a7813a04 XL |
301 | }, |
302 | _ => {}, | |
303 | } | |
cc61c64b | 304 | |
a7813a04 XL |
305 | return false |
306 | } | |
307 | ||
48663c56 | 308 | let res_hir_id = match tcx.hir().as_local_hir_id(res_did) { |
a7813a04 XL |
309 | Some(n) => n, None => return false |
310 | }; | |
54a0048b | 311 | |
48663c56 XL |
312 | let is_private = !self.cx.renderinfo.borrow().access_levels.is_public(res_did); |
313 | let is_hidden = inherits_doc_hidden(self.cx, res_hir_id); | |
54a0048b | 314 | |
0731742a | 315 | // Only inline if requested or if the item would otherwise be stripped. |
54a0048b | 316 | if (!please_inline && !is_private && !is_hidden) || is_no_inline { |
1a4d82fc JJ |
317 | return false |
318 | } | |
54a0048b | 319 | |
48663c56 | 320 | if !self.view_item_stack.insert(res_hir_id) { return false } |
1a4d82fc | 321 | |
dc9dc135 | 322 | let ret = match tcx.hir().get(res_hir_id) { |
e74abb32 | 323 | Node::Item(&hir::Item { kind: hir::ItemKind::Mod(ref m), .. }) if glob => { |
476ff2be | 324 | let prev = mem::replace(&mut self.inlining, true); |
041b39d2 | 325 | for i in &m.item_ids { |
dc9dc135 | 326 | let i = self.cx.tcx.hir().expect_item(i.id); |
041b39d2 | 327 | self.visit_item(i, None, om); |
1a4d82fc | 328 | } |
476ff2be | 329 | self.inlining = prev; |
041b39d2 XL |
330 | true |
331 | } | |
b7449926 | 332 | Node::Item(it) if !glob => { |
041b39d2 XL |
333 | let prev = mem::replace(&mut self.inlining, true); |
334 | self.visit_item(it, renamed, om); | |
335 | self.inlining = prev; | |
1a4d82fc JJ |
336 | true |
337 | } | |
b7449926 | 338 | Node::ForeignItem(it) if !glob => { |
dc9dc135 XL |
339 | let prev = mem::replace(&mut self.inlining, true); |
340 | self.visit_foreign_item(it, renamed, om); | |
341 | self.inlining = prev; | |
ff7c6d11 XL |
342 | true |
343 | } | |
0731742a XL |
344 | Node::MacroDef(def) if !glob => { |
345 | om.macros.push(self.visit_local_macro(def, renamed.map(|i| i.name))); | |
346 | true | |
347 | } | |
1a4d82fc JJ |
348 | _ => false, |
349 | }; | |
48663c56 | 350 | self.view_item_stack.remove(&res_hir_id); |
c30ab7b3 | 351 | ret |
1a4d82fc JJ |
352 | } |
353 | ||
416331ca | 354 | fn visit_item(&mut self, item: &'tcx hir::Item, |
dc9dc135 | 355 | renamed: Option<ast::Ident>, om: &mut Module<'tcx>) { |
416331ca | 356 | debug!("visiting item {:?}", item); |
0731742a | 357 | let ident = renamed.unwrap_or(item.ident); |
0531ce1d | 358 | |
8faf50e0 | 359 | if item.vis.node.is_pub() { |
416331ca | 360 | let def_id = self.cx.tcx.hir().local_def_id(item.hir_id); |
0531ce1d XL |
361 | self.store_path(def_id); |
362 | } | |
363 | ||
e74abb32 | 364 | match item.kind { |
8faf50e0 | 365 | hir::ItemKind::ForeignMod(ref fm) => { |
dc9dc135 XL |
366 | for item in &fm.items { |
367 | self.visit_foreign_item(item, None, om); | |
368 | } | |
476ff2be SL |
369 | } |
370 | // If we're inlining, skip private items. | |
8faf50e0 XL |
371 | _ if self.inlining && !item.vis.node.is_pub() => {} |
372 | hir::ItemKind::GlobalAsm(..) => {} | |
373 | hir::ItemKind::ExternCrate(orig_name) => { | |
416331ca | 374 | let def_id = self.cx.tcx.hir().local_def_id(item.hir_id); |
85aaf69f | 375 | om.extern_crates.push(ExternCrate { |
ea8adc8c | 376 | cnum: self.cx.tcx.extern_mod_stmt_cnum(def_id) |
9e0c209e | 377 | .unwrap_or(LOCAL_CRATE), |
0731742a | 378 | name: ident.name, |
0531ce1d | 379 | path: orig_name.map(|x|x.to_string()), |
dc9dc135 XL |
380 | vis: &item.vis, |
381 | attrs: &item.attrs, | |
85aaf69f SL |
382 | whence: item.span, |
383 | }) | |
384 | } | |
8faf50e0 XL |
385 | hir::ItemKind::Use(_, hir::UseKind::ListStem) => {} |
386 | hir::ItemKind::Use(ref path, kind) => { | |
476ff2be SL |
387 | let is_glob = kind == hir::UseKind::Glob; |
388 | ||
0731742a XL |
389 | // Struct and variant constructors and proc macro stubs always show up alongside |
390 | // their definitions, we've already processed them so just discard these. | |
416331ca XL |
391 | if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = path.res { |
392 | return; | |
94b46f34 XL |
393 | } |
394 | ||
476ff2be SL |
395 | // If there was a private module in the current path then don't bother inlining |
396 | // anything as it will probably be stripped anyway. | |
8faf50e0 | 397 | if item.vis.node.is_pub() && self.inside_public_path { |
85aaf69f SL |
398 | let please_inline = item.attrs.iter().any(|item| { |
399 | match item.meta_item_list() { | |
48663c56 XL |
400 | Some(ref list) if item.check_name(sym::doc) => { |
401 | list.iter().any(|i| i.check_name(sym::inline)) | |
85aaf69f | 402 | } |
54a0048b | 403 | _ => false, |
85aaf69f SL |
404 | } |
405 | }); | |
0731742a | 406 | let ident = if is_glob { None } else { Some(ident) }; |
532ac7d7 | 407 | if self.maybe_inline_local(item.hir_id, |
48663c56 | 408 | path.res, |
0731742a | 409 | ident, |
476ff2be SL |
410 | is_glob, |
411 | om, | |
412 | please_inline) { | |
413 | return; | |
85aaf69f | 414 | } |
476ff2be SL |
415 | } |
416 | ||
85aaf69f | 417 | om.imports.push(Import { |
0731742a | 418 | name: ident.name, |
532ac7d7 | 419 | id: item.hir_id, |
dc9dc135 XL |
420 | vis: &item.vis, |
421 | attrs: &item.attrs, | |
422 | path, | |
476ff2be | 423 | glob: is_glob, |
85aaf69f SL |
424 | whence: item.span, |
425 | }); | |
426 | } | |
8faf50e0 | 427 | hir::ItemKind::Mod(ref m) => { |
1a4d82fc | 428 | om.mods.push(self.visit_mod_contents(item.span, |
dc9dc135 XL |
429 | &item.attrs, |
430 | &item.vis, | |
532ac7d7 | 431 | item.hir_id, |
1a4d82fc | 432 | m, |
0731742a | 433 | Some(ident.name))); |
1a4d82fc | 434 | }, |
8faf50e0 | 435 | hir::ItemKind::Enum(ref ed, ref gen) => |
0731742a | 436 | om.enums.push(self.visit_enum_def(item, ident.name, ed, gen)), |
8faf50e0 | 437 | hir::ItemKind::Struct(ref sd, ref gen) => |
0731742a | 438 | om.structs.push(self.visit_variant_data(item, ident.name, sd, gen)), |
8faf50e0 | 439 | hir::ItemKind::Union(ref sd, ref gen) => |
0731742a | 440 | om.unions.push(self.visit_union_data(item, ident.name, sd, gen)), |
60c5eb7d XL |
441 | hir::ItemKind::Fn(ref sig, ref gen, body) => |
442 | self.visit_fn(om, item, ident.name, &sig.decl, sig.header, gen, body), | |
416331ca | 443 | hir::ItemKind::TyAlias(ref ty, ref gen) => { |
1a4d82fc | 444 | let t = Typedef { |
dc9dc135 XL |
445 | ty, |
446 | gen, | |
0731742a | 447 | name: ident.name, |
532ac7d7 | 448 | id: item.hir_id, |
dc9dc135 | 449 | attrs: &item.attrs, |
1a4d82fc | 450 | whence: item.span, |
dc9dc135 | 451 | vis: &item.vis, |
1a4d82fc JJ |
452 | }; |
453 | om.typedefs.push(t); | |
454 | }, | |
416331ca XL |
455 | hir::ItemKind::OpaqueTy(ref opaque_ty) => { |
456 | let t = OpaqueTy { | |
457 | opaque_ty, | |
0731742a | 458 | name: ident.name, |
532ac7d7 | 459 | id: item.hir_id, |
dc9dc135 | 460 | attrs: &item.attrs, |
8faf50e0 | 461 | whence: item.span, |
dc9dc135 | 462 | vis: &item.vis, |
8faf50e0 | 463 | }; |
416331ca | 464 | om.opaque_tys.push(t); |
8faf50e0 | 465 | }, |
dc9dc135 | 466 | hir::ItemKind::Static(ref type_, mutability, expr) => { |
1a4d82fc | 467 | let s = Static { |
dc9dc135 XL |
468 | type_, |
469 | mutability, | |
470 | expr, | |
532ac7d7 | 471 | id: item.hir_id, |
0731742a | 472 | name: ident.name, |
dc9dc135 | 473 | attrs: &item.attrs, |
1a4d82fc | 474 | whence: item.span, |
dc9dc135 | 475 | vis: &item.vis, |
1a4d82fc JJ |
476 | }; |
477 | om.statics.push(s); | |
478 | }, | |
dc9dc135 | 479 | hir::ItemKind::Const(ref type_, expr) => { |
1a4d82fc | 480 | let s = Constant { |
dc9dc135 XL |
481 | type_, |
482 | expr, | |
532ac7d7 | 483 | id: item.hir_id, |
0731742a | 484 | name: ident.name, |
dc9dc135 | 485 | attrs: &item.attrs, |
1a4d82fc | 486 | whence: item.span, |
dc9dc135 | 487 | vis: &item.vis, |
1a4d82fc JJ |
488 | }; |
489 | om.constants.push(s); | |
490 | }, | |
dc9dc135 | 491 | hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => { |
32a655c1 | 492 | let items = item_ids.iter() |
dc9dc135 | 493 | .map(|ti| self.cx.tcx.hir().trait_item(ti.id)) |
32a655c1 | 494 | .collect(); |
1a4d82fc | 495 | let t = Trait { |
2c00a5a8 | 496 | is_auto, |
3b2f2976 | 497 | unsafety, |
0731742a | 498 | name: ident.name, |
3b2f2976 | 499 | items, |
dc9dc135 XL |
500 | generics, |
501 | bounds, | |
532ac7d7 | 502 | id: item.hir_id, |
dc9dc135 | 503 | attrs: &item.attrs, |
1a4d82fc | 504 | whence: item.span, |
dc9dc135 | 505 | vis: &item.vis, |
1a4d82fc JJ |
506 | }; |
507 | om.traits.push(t); | |
508 | }, | |
dc9dc135 | 509 | hir::ItemKind::TraitAlias(ref generics, ref bounds) => { |
9fa01778 XL |
510 | let t = TraitAlias { |
511 | name: ident.name, | |
dc9dc135 XL |
512 | generics, |
513 | bounds, | |
532ac7d7 | 514 | id: item.hir_id, |
dc9dc135 | 515 | attrs: &item.attrs, |
9fa01778 | 516 | whence: item.span, |
dc9dc135 | 517 | vis: &item.vis, |
9fa01778 XL |
518 | }; |
519 | om.trait_aliases.push(t); | |
ff7c6d11 | 520 | }, |
476ff2be | 521 | |
8faf50e0 | 522 | hir::ItemKind::Impl(unsafety, |
7cac9316 XL |
523 | polarity, |
524 | defaultness, | |
dc9dc135 XL |
525 | ref generics, |
526 | ref trait_, | |
527 | ref for_, | |
7cac9316 | 528 | ref item_ids) => { |
0bf4aa26 XL |
529 | // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick |
530 | // them up regardless of where they're located. | |
dc9dc135 | 531 | if !self.inlining && trait_.is_none() { |
476ff2be | 532 | let items = item_ids.iter() |
dc9dc135 | 533 | .map(|ii| self.cx.tcx.hir().impl_item(ii.id)) |
476ff2be SL |
534 | .collect(); |
535 | let i = Impl { | |
3b2f2976 XL |
536 | unsafety, |
537 | polarity, | |
538 | defaultness, | |
dc9dc135 XL |
539 | generics, |
540 | trait_, | |
541 | for_, | |
3b2f2976 | 542 | items, |
dc9dc135 | 543 | attrs: &item.attrs, |
532ac7d7 | 544 | id: item.hir_id, |
476ff2be | 545 | whence: item.span, |
dc9dc135 | 546 | vis: &item.vis, |
476ff2be | 547 | }; |
9346a6ac AL |
548 | om.impls.push(i); |
549 | } | |
1a4d82fc | 550 | }, |
1a4d82fc JJ |
551 | } |
552 | } | |
553 | ||
dc9dc135 XL |
554 | fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem, |
555 | renamed: Option<ast::Ident>, om: &mut Module<'tcx>) { | |
556 | // If inlining we only want to include public functions. | |
557 | if self.inlining && !item.vis.node.is_pub() { | |
558 | return; | |
559 | } | |
560 | ||
561 | om.foreigns.push(ForeignItem { | |
562 | id: item.hir_id, | |
563 | name: renamed.unwrap_or(item.ident).name, | |
e74abb32 | 564 | kind: &item.kind, |
dc9dc135 | 565 | vis: &item.vis, |
dc9dc135 XL |
566 | attrs: &item.attrs, |
567 | whence: item.span | |
568 | }); | |
569 | } | |
570 | ||
0731742a XL |
571 | // Convert each `exported_macro` into a doc item. |
572 | fn visit_local_macro( | |
573 | &self, | |
dc9dc135 | 574 | def: &'tcx hir::MacroDef, |
0731742a | 575 | renamed: Option<ast::Name> |
dc9dc135 | 576 | ) -> Macro<'tcx> { |
83c7162d | 577 | debug!("visit_local_macro: {}", def.name); |
8bb4bdeb | 578 | let tts = def.body.trees().collect::<Vec<_>>(); |
92a42be0 | 579 | // Extract the spans of all matchers. They represent the "interface" of the macro. |
8bb4bdeb | 580 | let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect(); |
92a42be0 | 581 | |
1a4d82fc | 582 | Macro { |
416331ca XL |
583 | hid: def.hir_id, |
584 | def_id: self.cx.tcx.hir().local_def_id(def.hir_id), | |
dc9dc135 | 585 | attrs: &def.attrs, |
0731742a | 586 | name: renamed.unwrap_or(def.name), |
1a4d82fc | 587 | whence: def.span, |
3b2f2976 | 588 | matchers, |
32a655c1 | 589 | imported_from: None, |
1a4d82fc JJ |
590 | } |
591 | } | |
592 | } |