]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/doctree.rs
Use Names in path fragments and MacroDef
[rust.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::{Ident, Name, NodeId};
21 use syntax::attr;
22 use syntax::ptr::P;
23 use rustc_front::hir;
24
25 pub struct Module {
26     pub name: Option<Name>,
27     pub attrs: Vec<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 impls: Vec<Impl>,
44     pub def_traits: Vec<DefaultImpl>,
45     pub foreigns: Vec<hir::ForeignMod>,
46     pub macros: Vec<Macro>,
47     pub is_crate: bool,
48 }
49
50 impl Module {
51     pub fn new(name: Option<Name>) -> Module {
52         Module {
53             name       : name,
54             id: 0,
55             vis: hir::Inherited,
56             stab: None,
57             where_outer: syntax::codemap::DUMMY_SP,
58             where_inner: syntax::codemap::DUMMY_SP,
59             attrs      : Vec::new(),
60             extern_crates: Vec::new(),
61             imports    : Vec::new(),
62             structs    : Vec::new(),
63             enums      : Vec::new(),
64             fns        : Vec::new(),
65             mods       : Vec::new(),
66             typedefs   : Vec::new(),
67             statics    : Vec::new(),
68             constants  : Vec::new(),
69             traits     : Vec::new(),
70             impls      : Vec::new(),
71             def_traits : Vec::new(),
72             foreigns   : Vec::new(),
73             macros     : Vec::new(),
74             is_crate   : false,
75         }
76     }
77 }
78
79 #[derive(Debug, Clone, RustcEncodable, RustcDecodable, Copy)]
80 pub enum StructType {
81     /// A normal struct
82     Plain,
83     /// A tuple struct
84     Tuple,
85     /// A newtype struct (tuple struct with one element)
86     Newtype,
87     /// A unit struct
88     Unit
89 }
90
91 pub enum TypeBound {
92     RegionBound,
93     TraitBound(hir::TraitRef)
94 }
95
96 pub struct Struct {
97     pub vis: hir::Visibility,
98     pub stab: Option<attr::Stability>,
99     pub id: NodeId,
100     pub struct_type: StructType,
101     pub name: Name,
102     pub generics: hir::Generics,
103     pub attrs: Vec<ast::Attribute>,
104     pub fields: Vec<hir::StructField>,
105     pub whence: Span,
106 }
107
108 pub struct Enum {
109     pub vis: hir::Visibility,
110     pub stab: Option<attr::Stability>,
111     pub variants: Vec<Variant>,
112     pub generics: hir::Generics,
113     pub attrs: Vec<ast::Attribute>,
114     pub id: NodeId,
115     pub whence: Span,
116     pub name: Name,
117 }
118
119 pub struct Variant {
120     pub name: Ident,
121     pub attrs: Vec<ast::Attribute>,
122     pub kind: hir::VariantKind,
123     pub id: ast::NodeId,
124     pub stab: Option<attr::Stability>,
125     pub whence: Span,
126 }
127
128 pub struct Function {
129     pub decl: hir::FnDecl,
130     pub attrs: Vec<ast::Attribute>,
131     pub id: NodeId,
132     pub name: Name,
133     pub vis: hir::Visibility,
134     pub stab: Option<attr::Stability>,
135     pub unsafety: hir::Unsafety,
136     pub constness: hir::Constness,
137     pub whence: Span,
138     pub generics: hir::Generics,
139     pub abi: abi::Abi,
140 }
141
142 pub struct Typedef {
143     pub ty: P<hir::Ty>,
144     pub gen: hir::Generics,
145     pub name: Name,
146     pub id: ast::NodeId,
147     pub attrs: Vec<ast::Attribute>,
148     pub whence: Span,
149     pub vis: hir::Visibility,
150     pub stab: Option<attr::Stability>,
151 }
152
153 #[derive(Debug)]
154 pub struct Static {
155     pub type_: P<hir::Ty>,
156     pub mutability: hir::Mutability,
157     pub expr: P<hir::Expr>,
158     pub name: Name,
159     pub attrs: Vec<ast::Attribute>,
160     pub vis: hir::Visibility,
161     pub stab: Option<attr::Stability>,
162     pub id: ast::NodeId,
163     pub whence: Span,
164 }
165
166 pub struct Constant {
167     pub type_: P<hir::Ty>,
168     pub expr: P<hir::Expr>,
169     pub name: Name,
170     pub attrs: Vec<ast::Attribute>,
171     pub vis: hir::Visibility,
172     pub stab: Option<attr::Stability>,
173     pub id: ast::NodeId,
174     pub whence: Span,
175 }
176
177 pub struct Trait {
178     pub unsafety: hir::Unsafety,
179     pub name: Name,
180     pub items: Vec<P<hir::TraitItem>>, //should be TraitItem
181     pub generics: hir::Generics,
182     pub bounds: Vec<hir::TyParamBound>,
183     pub attrs: Vec<ast::Attribute>,
184     pub id: ast::NodeId,
185     pub whence: Span,
186     pub vis: hir::Visibility,
187     pub stab: Option<attr::Stability>,
188 }
189
190 pub struct Impl {
191     pub unsafety: hir::Unsafety,
192     pub polarity: hir::ImplPolarity,
193     pub generics: hir::Generics,
194     pub trait_: Option<hir::TraitRef>,
195     pub for_: P<hir::Ty>,
196     pub items: Vec<P<hir::ImplItem>>,
197     pub attrs: Vec<ast::Attribute>,
198     pub whence: Span,
199     pub vis: hir::Visibility,
200     pub stab: Option<attr::Stability>,
201     pub id: ast::NodeId,
202 }
203
204 pub struct DefaultImpl {
205     pub unsafety: hir::Unsafety,
206     pub trait_: hir::TraitRef,
207     pub id: ast::NodeId,
208     pub attrs: Vec<ast::Attribute>,
209     pub whence: Span,
210 }
211
212 pub struct Macro {
213     pub name: Name,
214     pub id: ast::NodeId,
215     pub attrs: Vec<ast::Attribute>,
216     pub whence: Span,
217     pub stab: Option<attr::Stability>,
218     pub imported_from: Option<Name>,
219 }
220
221 pub struct ExternCrate {
222     pub name: Name,
223     pub path: Option<String>,
224     pub vis: hir::Visibility,
225     pub attrs: Vec<ast::Attribute>,
226     pub whence: Span,
227 }
228
229 pub struct Import {
230     pub id: NodeId,
231     pub vis: hir::Visibility,
232     pub attrs: Vec<ast::Attribute>,
233     pub node: hir::ViewPath_,
234     pub whence: Span,
235 }
236
237 pub fn struct_type_from_def(sd: &hir::StructDef) -> StructType {
238     if sd.ctor_id.is_some() {
239         // We are in a tuple-struct
240         match sd.fields.len() {
241             0 => Unit,
242             1 => Newtype,
243             _ => Tuple
244         }
245     } else {
246         Plain
247     }
248 }