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