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