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