]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/doctree.rs
Rollup merge of #79354 - ssomers:btree_bereave_BoxedNode, r=Mark-Simulacrum
[rust.git] / src / librustdoc / doctree.rs
1 //! This module is used to store stuff from Rust's AST in a more convenient
2 //! manner (and with prettier names) before cleaning.
3 crate use self::StructType::*;
4
5 use rustc_ast as ast;
6 use rustc_span::hygiene::MacroKind;
7 use rustc_span::{self, symbol::Ident, Span, Symbol};
8
9 use rustc_hir as hir;
10 use rustc_hir::def_id::CrateNum;
11 use rustc_hir::HirId;
12
13 crate struct Module<'hir> {
14     crate name: Option<Symbol>,
15     crate attrs: &'hir [ast::Attribute],
16     crate where_outer: Span,
17     crate where_inner: Span,
18     crate extern_crates: Vec<ExternCrate<'hir>>,
19     crate imports: Vec<Import<'hir>>,
20     crate fns: Vec<Function<'hir>>,
21     crate mods: Vec<Module<'hir>>,
22     crate id: hir::HirId,
23     // (item, renamed)
24     crate items: Vec<(&'hir hir::Item<'hir>, Option<Ident>)>,
25     crate traits: Vec<Trait<'hir>>,
26     crate foreigns: Vec<ForeignItem<'hir>>,
27     crate macros: Vec<Macro>,
28     crate proc_macros: Vec<ProcMacro>,
29     crate is_crate: bool,
30 }
31
32 impl Module<'hir> {
33     crate fn new(name: Option<Symbol>, attrs: &'hir [ast::Attribute]) -> Module<'hir> {
34         Module {
35             name,
36             id: hir::CRATE_HIR_ID,
37             where_outer: rustc_span::DUMMY_SP,
38             where_inner: rustc_span::DUMMY_SP,
39             attrs,
40             extern_crates: Vec::new(),
41             imports: Vec::new(),
42             fns: Vec::new(),
43             mods: Vec::new(),
44             items: Vec::new(),
45             traits: Vec::new(),
46             foreigns: Vec::new(),
47             macros: Vec::new(),
48             proc_macros: Vec::new(),
49             is_crate: false,
50         }
51     }
52 }
53
54 #[derive(Debug, Clone, Copy)]
55 crate enum StructType {
56     /// A braced struct
57     Plain,
58     /// A tuple struct
59     Tuple,
60     /// A unit struct
61     Unit,
62 }
63
64 crate struct Variant<'hir> {
65     crate name: Symbol,
66     crate id: hir::HirId,
67     crate def: &'hir hir::VariantData<'hir>,
68 }
69
70 crate struct Function<'hir> {
71     crate decl: &'hir hir::FnDecl<'hir>,
72     crate id: hir::HirId,
73     crate name: Symbol,
74     crate header: hir::FnHeader,
75     crate generics: &'hir hir::Generics<'hir>,
76     crate body: hir::BodyId,
77 }
78
79 crate struct Trait<'hir> {
80     crate is_auto: hir::IsAuto,
81     crate unsafety: hir::Unsafety,
82     crate name: Symbol,
83     crate items: Vec<&'hir hir::TraitItem<'hir>>,
84     crate generics: &'hir hir::Generics<'hir>,
85     crate bounds: &'hir [hir::GenericBound<'hir>],
86     crate attrs: &'hir [ast::Attribute],
87     crate id: hir::HirId,
88 }
89
90 crate struct ForeignItem<'hir> {
91     crate id: hir::HirId,
92     crate name: Symbol,
93     crate kind: &'hir hir::ForeignItemKind<'hir>,
94 }
95
96 // For Macro we store the DefId instead of the NodeId, since we also create
97 // these imported macro_rules (which only have a DUMMY_NODE_ID).
98 crate struct Macro {
99     crate name: Symbol,
100     crate def_id: hir::def_id::DefId,
101     crate matchers: Vec<Span>,
102     crate imported_from: Option<Symbol>,
103 }
104
105 crate struct ExternCrate<'hir> {
106     crate name: Symbol,
107     crate hir_id: HirId,
108     crate cnum: CrateNum,
109     crate path: Option<String>,
110     crate vis: &'hir hir::Visibility<'hir>,
111     crate attrs: &'hir [ast::Attribute],
112     crate span: Span,
113 }
114
115 #[derive(Debug)]
116 crate struct Import<'hir> {
117     crate name: Symbol,
118     crate id: hir::HirId,
119     crate vis: &'hir hir::Visibility<'hir>,
120     crate attrs: &'hir [ast::Attribute],
121     crate path: &'hir hir::Path<'hir>,
122     crate glob: bool,
123     crate span: Span,
124 }
125
126 crate struct ProcMacro {
127     crate name: Symbol,
128     crate id: hir::HirId,
129     crate kind: MacroKind,
130     crate helpers: Vec<Symbol>,
131 }
132
133 crate fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType {
134     match *vdata {
135         hir::VariantData::Struct(..) => Plain,
136         hir::VariantData::Tuple(..) => Tuple,
137         hir::VariantData::Unit(..) => Unit,
138     }
139 }