]> git.proxmox.com Git - rustc.git/blob - src/librustdoc/doctree.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustdoc / doctree.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! This module is used to store stuff from Rust's AST in a more convenient
12 //! manner (and with prettier names) before cleaning.
13 pub use self::StructType::*;
14 pub use self::TypeBound::*;
15
16 use syntax;
17 use syntax::codemap::Span;
18 use syntax::abi;
19 use syntax::ast;
20 use syntax::ast::{Name, NodeId};
21 use syntax::attr;
22 use syntax::ptr::P;
23 use rustc::hir;
24
25 pub struct Module {
26 pub name: Option<Name>,
27 pub attrs: hir::HirVec<ast::Attribute>,
28 pub where_outer: Span,
29 pub where_inner: Span,
30 pub extern_crates: Vec<ExternCrate>,
31 pub imports: Vec<Import>,
32 pub structs: Vec<Struct>,
33 pub enums: Vec<Enum>,
34 pub fns: Vec<Function>,
35 pub mods: Vec<Module>,
36 pub id: NodeId,
37 pub typedefs: Vec<Typedef>,
38 pub statics: Vec<Static>,
39 pub constants: Vec<Constant>,
40 pub traits: Vec<Trait>,
41 pub vis: hir::Visibility,
42 pub stab: Option<attr::Stability>,
43 pub depr: Option<attr::Deprecation>,
44 pub impls: Vec<Impl>,
45 pub def_traits: Vec<DefaultImpl>,
46 pub foreigns: Vec<hir::ForeignMod>,
47 pub macros: Vec<Macro>,
48 pub is_crate: bool,
49 }
50
51 impl Module {
52 pub fn new(name: Option<Name>) -> Module {
53 Module {
54 name : name,
55 id: 0,
56 vis: hir::Inherited,
57 stab: None,
58 depr: None,
59 where_outer: syntax::codemap::DUMMY_SP,
60 where_inner: syntax::codemap::DUMMY_SP,
61 attrs : hir::HirVec::new(),
62 extern_crates: Vec::new(),
63 imports : Vec::new(),
64 structs : Vec::new(),
65 enums : Vec::new(),
66 fns : Vec::new(),
67 mods : Vec::new(),
68 typedefs : Vec::new(),
69 statics : Vec::new(),
70 constants : Vec::new(),
71 traits : Vec::new(),
72 impls : Vec::new(),
73 def_traits : Vec::new(),
74 foreigns : Vec::new(),
75 macros : Vec::new(),
76 is_crate : false,
77 }
78 }
79 }
80
81 #[derive(Debug, Clone, RustcEncodable, RustcDecodable, Copy)]
82 pub enum StructType {
83 /// A normal struct
84 Plain,
85 /// A tuple struct
86 Tuple,
87 /// A newtype struct (tuple struct with one element)
88 Newtype,
89 /// A unit struct
90 Unit
91 }
92
93 pub enum TypeBound {
94 RegionBound,
95 TraitBound(hir::TraitRef)
96 }
97
98 pub struct Struct {
99 pub vis: hir::Visibility,
100 pub stab: Option<attr::Stability>,
101 pub depr: Option<attr::Deprecation>,
102 pub id: NodeId,
103 pub struct_type: StructType,
104 pub name: Name,
105 pub generics: hir::Generics,
106 pub attrs: hir::HirVec<ast::Attribute>,
107 pub fields: hir::HirVec<hir::StructField>,
108 pub whence: Span,
109 }
110
111 pub struct Enum {
112 pub vis: hir::Visibility,
113 pub stab: Option<attr::Stability>,
114 pub depr: Option<attr::Deprecation>,
115 pub variants: hir::HirVec<Variant>,
116 pub generics: hir::Generics,
117 pub attrs: hir::HirVec<ast::Attribute>,
118 pub id: NodeId,
119 pub whence: Span,
120 pub name: Name,
121 }
122
123 pub struct Variant {
124 pub name: Name,
125 pub attrs: hir::HirVec<ast::Attribute>,
126 pub def: hir::VariantData,
127 pub stab: Option<attr::Stability>,
128 pub depr: Option<attr::Deprecation>,
129 pub whence: Span,
130 }
131
132 pub struct Function {
133 pub decl: hir::FnDecl,
134 pub attrs: hir::HirVec<ast::Attribute>,
135 pub id: NodeId,
136 pub name: Name,
137 pub vis: hir::Visibility,
138 pub stab: Option<attr::Stability>,
139 pub depr: Option<attr::Deprecation>,
140 pub unsafety: hir::Unsafety,
141 pub constness: hir::Constness,
142 pub whence: Span,
143 pub generics: hir::Generics,
144 pub abi: abi::Abi,
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 #[derive(Debug)]
160 pub struct Static {
161 pub type_: P<hir::Ty>,
162 pub mutability: hir::Mutability,
163 pub expr: P<hir::Expr>,
164 pub name: Name,
165 pub attrs: hir::HirVec<ast::Attribute>,
166 pub vis: hir::Visibility,
167 pub stab: Option<attr::Stability>,
168 pub depr: Option<attr::Deprecation>,
169 pub id: ast::NodeId,
170 pub whence: Span,
171 }
172
173 pub struct Constant {
174 pub type_: P<hir::Ty>,
175 pub expr: P<hir::Expr>,
176 pub name: Name,
177 pub attrs: hir::HirVec<ast::Attribute>,
178 pub vis: hir::Visibility,
179 pub stab: Option<attr::Stability>,
180 pub depr: Option<attr::Deprecation>,
181 pub id: ast::NodeId,
182 pub whence: Span,
183 }
184
185 pub struct Trait {
186 pub unsafety: hir::Unsafety,
187 pub name: Name,
188 pub items: hir::HirVec<hir::TraitItem>,
189 pub generics: hir::Generics,
190 pub bounds: hir::HirVec<hir::TyParamBound>,
191 pub attrs: hir::HirVec<ast::Attribute>,
192 pub id: ast::NodeId,
193 pub whence: Span,
194 pub vis: hir::Visibility,
195 pub stab: Option<attr::Stability>,
196 pub depr: Option<attr::Deprecation>,
197 }
198
199 pub struct Impl {
200 pub unsafety: hir::Unsafety,
201 pub polarity: hir::ImplPolarity,
202 pub generics: hir::Generics,
203 pub trait_: Option<hir::TraitRef>,
204 pub for_: P<hir::Ty>,
205 pub items: hir::HirVec<hir::ImplItem>,
206 pub attrs: hir::HirVec<ast::Attribute>,
207 pub whence: Span,
208 pub vis: hir::Visibility,
209 pub stab: Option<attr::Stability>,
210 pub depr: Option<attr::Deprecation>,
211 pub id: ast::NodeId,
212 }
213
214 pub struct DefaultImpl {
215 pub unsafety: hir::Unsafety,
216 pub trait_: hir::TraitRef,
217 pub id: ast::NodeId,
218 pub attrs: hir::HirVec<ast::Attribute>,
219 pub whence: Span,
220 }
221
222 pub struct Macro {
223 pub name: Name,
224 pub id: ast::NodeId,
225 pub attrs: hir::HirVec<ast::Attribute>,
226 pub whence: Span,
227 pub matchers: hir::HirVec<Span>,
228 pub stab: Option<attr::Stability>,
229 pub depr: Option<attr::Deprecation>,
230 pub imported_from: Option<Name>,
231 }
232
233 pub struct ExternCrate {
234 pub name: Name,
235 pub path: Option<String>,
236 pub vis: hir::Visibility,
237 pub attrs: hir::HirVec<ast::Attribute>,
238 pub whence: Span,
239 }
240
241 pub struct Import {
242 pub id: NodeId,
243 pub vis: hir::Visibility,
244 pub attrs: hir::HirVec<ast::Attribute>,
245 pub node: hir::ViewPath_,
246 pub whence: Span,
247 }
248
249 pub fn struct_type_from_def(sd: &hir::VariantData) -> StructType {
250 if !sd.is_struct() {
251 // We are in a tuple-struct
252 match sd.fields().len() {
253 0 => Unit,
254 1 => Newtype,
255 _ => Tuple
256 }
257 } else {
258 Plain
259 }
260 }