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