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