]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/doctree.rs
Rollup merge of #77844 - RalfJung:zst-box, r=nikomatsakis
[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, 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 structs: Vec<Struct<'hir>>,
21     crate unions: Vec<Union<'hir>>,
22     crate enums: Vec<Enum<'hir>>,
23     crate fns: Vec<Function<'hir>>,
24     crate mods: Vec<Module<'hir>>,
25     crate id: hir::HirId,
26     crate typedefs: Vec<Typedef<'hir>>,
27     crate opaque_tys: Vec<OpaqueTy<'hir>>,
28     crate statics: Vec<Static<'hir>>,
29     crate constants: Vec<Constant<'hir>>,
30     crate traits: Vec<Trait<'hir>>,
31     crate impls: Vec<Impl<'hir>>,
32     crate foreigns: Vec<ForeignItem<'hir>>,
33     crate macros: Vec<Macro>,
34     crate proc_macros: Vec<ProcMacro>,
35     crate trait_aliases: Vec<TraitAlias<'hir>>,
36     crate is_crate: bool,
37 }
38
39 impl Module<'hir> {
40     crate fn new(name: Option<Symbol>, attrs: &'hir [ast::Attribute]) -> Module<'hir> {
41         Module {
42             name,
43             id: hir::CRATE_HIR_ID,
44             where_outer: rustc_span::DUMMY_SP,
45             where_inner: rustc_span::DUMMY_SP,
46             attrs,
47             extern_crates: Vec::new(),
48             imports: Vec::new(),
49             structs: Vec::new(),
50             unions: Vec::new(),
51             enums: Vec::new(),
52             fns: Vec::new(),
53             mods: Vec::new(),
54             typedefs: Vec::new(),
55             opaque_tys: Vec::new(),
56             statics: Vec::new(),
57             constants: Vec::new(),
58             traits: Vec::new(),
59             impls: Vec::new(),
60             foreigns: Vec::new(),
61             macros: Vec::new(),
62             proc_macros: Vec::new(),
63             trait_aliases: Vec::new(),
64             is_crate: false,
65         }
66     }
67 }
68
69 #[derive(Debug, Clone, Copy)]
70 crate enum StructType {
71     /// A braced struct
72     Plain,
73     /// A tuple struct
74     Tuple,
75     /// A unit struct
76     Unit,
77 }
78
79 crate struct Struct<'hir> {
80     crate id: hir::HirId,
81     crate struct_type: StructType,
82     crate name: Symbol,
83     crate generics: &'hir hir::Generics<'hir>,
84     crate fields: &'hir [hir::StructField<'hir>],
85 }
86
87 crate struct Union<'hir> {
88     crate id: hir::HirId,
89     crate struct_type: StructType,
90     crate name: Symbol,
91     crate generics: &'hir hir::Generics<'hir>,
92     crate fields: &'hir [hir::StructField<'hir>],
93 }
94
95 crate struct Enum<'hir> {
96     crate variants: Vec<Variant<'hir>>,
97     crate generics: &'hir hir::Generics<'hir>,
98     crate id: hir::HirId,
99     crate name: Symbol,
100 }
101
102 crate struct Variant<'hir> {
103     crate name: Symbol,
104     crate id: hir::HirId,
105     crate def: &'hir hir::VariantData<'hir>,
106 }
107
108 crate struct Function<'hir> {
109     crate decl: &'hir hir::FnDecl<'hir>,
110     crate id: hir::HirId,
111     crate name: Symbol,
112     crate header: hir::FnHeader,
113     crate generics: &'hir hir::Generics<'hir>,
114     crate body: hir::BodyId,
115 }
116
117 crate struct Typedef<'hir> {
118     crate ty: &'hir hir::Ty<'hir>,
119     crate gen: &'hir hir::Generics<'hir>,
120     crate name: Symbol,
121     crate id: hir::HirId,
122 }
123
124 crate struct OpaqueTy<'hir> {
125     crate opaque_ty: &'hir hir::OpaqueTy<'hir>,
126     crate name: Symbol,
127     crate id: hir::HirId,
128 }
129
130 #[derive(Debug)]
131 crate struct Static<'hir> {
132     crate type_: &'hir hir::Ty<'hir>,
133     crate mutability: hir::Mutability,
134     crate expr: hir::BodyId,
135     crate name: Symbol,
136     crate attrs: &'hir [ast::Attribute],
137     crate vis: &'hir hir::Visibility<'hir>,
138     crate id: hir::HirId,
139     crate span: Span,
140 }
141
142 crate struct Constant<'hir> {
143     crate type_: &'hir hir::Ty<'hir>,
144     crate expr: hir::BodyId,
145     crate name: Symbol,
146     crate id: hir::HirId,
147 }
148
149 crate struct Trait<'hir> {
150     crate is_auto: hir::IsAuto,
151     crate unsafety: hir::Unsafety,
152     crate name: Symbol,
153     crate items: Vec<&'hir hir::TraitItem<'hir>>,
154     crate generics: &'hir hir::Generics<'hir>,
155     crate bounds: &'hir [hir::GenericBound<'hir>],
156     crate attrs: &'hir [ast::Attribute],
157     crate id: hir::HirId,
158 }
159
160 crate struct TraitAlias<'hir> {
161     crate name: Symbol,
162     crate generics: &'hir hir::Generics<'hir>,
163     crate bounds: &'hir [hir::GenericBound<'hir>],
164     crate id: hir::HirId,
165 }
166
167 #[derive(Debug)]
168 crate struct Impl<'hir> {
169     crate unsafety: hir::Unsafety,
170     crate polarity: hir::ImplPolarity,
171     crate defaultness: hir::Defaultness,
172     crate constness: hir::Constness,
173     crate generics: &'hir hir::Generics<'hir>,
174     crate trait_: &'hir Option<hir::TraitRef<'hir>>,
175     crate for_: &'hir hir::Ty<'hir>,
176     crate items: Vec<&'hir hir::ImplItem<'hir>>,
177     crate attrs: &'hir [ast::Attribute],
178     crate span: Span,
179     crate vis: &'hir hir::Visibility<'hir>,
180     crate id: hir::HirId,
181 }
182
183 crate struct ForeignItem<'hir> {
184     crate id: hir::HirId,
185     crate name: Symbol,
186     crate kind: &'hir hir::ForeignItemKind<'hir>,
187 }
188
189 // For Macro we store the DefId instead of the NodeId, since we also create
190 // these imported macro_rules (which only have a DUMMY_NODE_ID).
191 crate struct Macro {
192     crate name: Symbol,
193     crate def_id: hir::def_id::DefId,
194     crate matchers: Vec<Span>,
195     crate imported_from: Option<Symbol>,
196 }
197
198 crate struct ExternCrate<'hir> {
199     crate name: Symbol,
200     crate hir_id: HirId,
201     crate cnum: CrateNum,
202     crate path: Option<String>,
203     crate vis: &'hir hir::Visibility<'hir>,
204     crate attrs: &'hir [ast::Attribute],
205     crate span: Span,
206 }
207
208 #[derive(Debug)]
209 crate struct Import<'hir> {
210     crate name: Symbol,
211     crate id: hir::HirId,
212     crate vis: &'hir hir::Visibility<'hir>,
213     crate attrs: &'hir [ast::Attribute],
214     crate path: &'hir hir::Path<'hir>,
215     crate glob: bool,
216     crate span: Span,
217 }
218
219 crate struct ProcMacro {
220     crate name: Symbol,
221     crate id: hir::HirId,
222     crate kind: MacroKind,
223     crate helpers: Vec<Symbol>,
224 }
225
226 crate fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType {
227     match *vdata {
228         hir::VariantData::Struct(..) => Plain,
229         hir::VariantData::Tuple(..) => Tuple,
230         hir::VariantData::Unit(..) => Unit,
231     }
232 }