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