]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/doctree.rs
Auto merge of #29129 - cuviper:impl-from-for-floats, r=alexcrichton
[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::{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: Name,
121     pub attrs: Vec<ast::Attribute>,
122     pub def: hir::VariantData,
123     pub stab: Option<attr::Stability>,
124     pub whence: Span,
125 }
126
127 pub struct Function {
128     pub decl: hir::FnDecl,
129     pub attrs: Vec<ast::Attribute>,
130     pub id: NodeId,
131     pub name: Name,
132     pub vis: hir::Visibility,
133     pub stab: Option<attr::Stability>,
134     pub unsafety: hir::Unsafety,
135     pub constness: hir::Constness,
136     pub whence: Span,
137     pub generics: hir::Generics,
138     pub abi: abi::Abi,
139 }
140
141 pub struct Typedef {
142     pub ty: P<hir::Ty>,
143     pub gen: hir::Generics,
144     pub name: Name,
145     pub id: ast::NodeId,
146     pub attrs: Vec<ast::Attribute>,
147     pub whence: Span,
148     pub vis: hir::Visibility,
149     pub stab: Option<attr::Stability>,
150 }
151
152 #[derive(Debug)]
153 pub struct Static {
154     pub type_: P<hir::Ty>,
155     pub mutability: hir::Mutability,
156     pub expr: P<hir::Expr>,
157     pub name: Name,
158     pub attrs: Vec<ast::Attribute>,
159     pub vis: hir::Visibility,
160     pub stab: Option<attr::Stability>,
161     pub id: ast::NodeId,
162     pub whence: Span,
163 }
164
165 pub struct Constant {
166     pub type_: P<hir::Ty>,
167     pub expr: P<hir::Expr>,
168     pub name: Name,
169     pub attrs: Vec<ast::Attribute>,
170     pub vis: hir::Visibility,
171     pub stab: Option<attr::Stability>,
172     pub id: ast::NodeId,
173     pub whence: Span,
174 }
175
176 pub struct Trait {
177     pub unsafety: hir::Unsafety,
178     pub name: Name,
179     pub items: Vec<P<hir::TraitItem>>, //should be TraitItem
180     pub generics: hir::Generics,
181     pub bounds: Vec<hir::TyParamBound>,
182     pub attrs: Vec<ast::Attribute>,
183     pub id: ast::NodeId,
184     pub whence: Span,
185     pub vis: hir::Visibility,
186     pub stab: Option<attr::Stability>,
187 }
188
189 pub struct Impl {
190     pub unsafety: hir::Unsafety,
191     pub polarity: hir::ImplPolarity,
192     pub generics: hir::Generics,
193     pub trait_: Option<hir::TraitRef>,
194     pub for_: P<hir::Ty>,
195     pub items: Vec<P<hir::ImplItem>>,
196     pub attrs: Vec<ast::Attribute>,
197     pub whence: Span,
198     pub vis: hir::Visibility,
199     pub stab: Option<attr::Stability>,
200     pub id: ast::NodeId,
201 }
202
203 pub struct DefaultImpl {
204     pub unsafety: hir::Unsafety,
205     pub trait_: hir::TraitRef,
206     pub id: ast::NodeId,
207     pub attrs: Vec<ast::Attribute>,
208     pub whence: Span,
209 }
210
211 pub struct Macro {
212     pub name: Name,
213     pub id: ast::NodeId,
214     pub attrs: Vec<ast::Attribute>,
215     pub whence: Span,
216     pub stab: Option<attr::Stability>,
217     pub imported_from: Option<Name>,
218 }
219
220 pub struct ExternCrate {
221     pub name: Name,
222     pub path: Option<String>,
223     pub vis: hir::Visibility,
224     pub attrs: Vec<ast::Attribute>,
225     pub whence: Span,
226 }
227
228 pub struct Import {
229     pub id: NodeId,
230     pub vis: hir::Visibility,
231     pub attrs: Vec<ast::Attribute>,
232     pub node: hir::ViewPath_,
233     pub whence: Span,
234 }
235
236 pub fn struct_type_from_def(sd: &hir::VariantData) -> StructType {
237     if !sd.is_struct() {
238         // We are in a tuple-struct
239         match sd.fields().len() {
240             0 => Unit,
241             1 => Newtype,
242             _ => Tuple
243         }
244     } else {
245         Plain
246     }
247 }