]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | ||
9346a6ac | 16 | use syntax::abi; |
1a4d82fc | 17 | use syntax::ast; |
b039eaaf SL |
18 | use syntax::ast::{Name, NodeId}; |
19 | use syntax::attr; | |
1a4d82fc | 20 | use syntax::ptr::P; |
3157f602 XL |
21 | use syntax_pos::{self, Span}; |
22 | ||
54a0048b | 23 | use rustc::hir; |
9e0c209e | 24 | use rustc::hir::def_id::CrateNum; |
1a4d82fc JJ |
25 | |
26 | pub struct Module { | |
b039eaaf | 27 | pub name: Option<Name>, |
9cc50fc6 | 28 | pub attrs: hir::HirVec<ast::Attribute>, |
1a4d82fc JJ |
29 | pub where_outer: Span, |
30 | pub where_inner: Span, | |
85aaf69f SL |
31 | pub extern_crates: Vec<ExternCrate>, |
32 | pub imports: Vec<Import>, | |
1a4d82fc | 33 | pub structs: Vec<Struct>, |
9e0c209e | 34 | pub unions: Vec<Union>, |
1a4d82fc JJ |
35 | pub enums: Vec<Enum>, |
36 | pub fns: Vec<Function>, | |
37 | pub mods: Vec<Module>, | |
38 | pub id: NodeId, | |
39 | pub typedefs: Vec<Typedef>, | |
40 | pub statics: Vec<Static>, | |
41 | pub constants: Vec<Constant>, | |
42 | pub traits: Vec<Trait>, | |
e9174d1e | 43 | pub vis: hir::Visibility, |
1a4d82fc | 44 | pub stab: Option<attr::Stability>, |
9cc50fc6 | 45 | pub depr: Option<attr::Deprecation>, |
1a4d82fc | 46 | pub impls: Vec<Impl>, |
e9174d1e | 47 | pub foreigns: Vec<hir::ForeignMod>, |
1a4d82fc JJ |
48 | pub macros: Vec<Macro>, |
49 | pub is_crate: bool, | |
50 | } | |
51 | ||
52 | impl Module { | |
b039eaaf | 53 | pub fn new(name: Option<Name>) -> Module { |
1a4d82fc JJ |
54 | Module { |
55 | name : name, | |
9e0c209e | 56 | id: ast::CRATE_NODE_ID, |
e9174d1e | 57 | vis: hir::Inherited, |
1a4d82fc | 58 | stab: None, |
9cc50fc6 | 59 | depr: None, |
3157f602 XL |
60 | where_outer: syntax_pos::DUMMY_SP, |
61 | where_inner: syntax_pos::DUMMY_SP, | |
9cc50fc6 | 62 | attrs : hir::HirVec::new(), |
85aaf69f SL |
63 | extern_crates: Vec::new(), |
64 | imports : Vec::new(), | |
1a4d82fc | 65 | structs : Vec::new(), |
9e0c209e | 66 | unions : Vec::new(), |
1a4d82fc JJ |
67 | enums : Vec::new(), |
68 | fns : Vec::new(), | |
69 | mods : Vec::new(), | |
70 | typedefs : Vec::new(), | |
71 | statics : Vec::new(), | |
72 | constants : Vec::new(), | |
73 | traits : Vec::new(), | |
74 | impls : Vec::new(), | |
1a4d82fc JJ |
75 | foreigns : Vec::new(), |
76 | macros : Vec::new(), | |
77 | is_crate : false, | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
85aaf69f | 82 | #[derive(Debug, Clone, RustcEncodable, RustcDecodable, Copy)] |
1a4d82fc | 83 | pub enum StructType { |
9e0c209e | 84 | /// A braced struct |
1a4d82fc JJ |
85 | Plain, |
86 | /// A tuple struct | |
87 | Tuple, | |
1a4d82fc | 88 | /// A unit struct |
9e0c209e | 89 | Unit, |
1a4d82fc JJ |
90 | } |
91 | ||
92 | pub enum TypeBound { | |
93 | RegionBound, | |
e9174d1e | 94 | TraitBound(hir::TraitRef) |
1a4d82fc JJ |
95 | } |
96 | ||
97 | pub struct Struct { | |
e9174d1e | 98 | pub vis: hir::Visibility, |
1a4d82fc | 99 | pub stab: Option<attr::Stability>, |
9cc50fc6 | 100 | pub depr: Option<attr::Deprecation>, |
1a4d82fc JJ |
101 | pub id: NodeId, |
102 | pub struct_type: StructType, | |
b039eaaf | 103 | pub name: Name, |
e9174d1e | 104 | pub generics: hir::Generics, |
9cc50fc6 SL |
105 | pub attrs: hir::HirVec<ast::Attribute>, |
106 | pub fields: hir::HirVec<hir::StructField>, | |
1a4d82fc JJ |
107 | pub whence: Span, |
108 | } | |
109 | ||
9e0c209e SL |
110 | pub struct Union { |
111 | pub vis: hir::Visibility, | |
112 | pub stab: Option<attr::Stability>, | |
113 | pub depr: Option<attr::Deprecation>, | |
114 | pub id: NodeId, | |
115 | pub struct_type: StructType, | |
116 | pub name: Name, | |
117 | pub generics: hir::Generics, | |
118 | pub attrs: hir::HirVec<ast::Attribute>, | |
119 | pub fields: hir::HirVec<hir::StructField>, | |
120 | pub whence: Span, | |
121 | } | |
122 | ||
1a4d82fc | 123 | pub struct Enum { |
e9174d1e | 124 | pub vis: hir::Visibility, |
1a4d82fc | 125 | pub stab: Option<attr::Stability>, |
9cc50fc6 SL |
126 | pub depr: Option<attr::Deprecation>, |
127 | pub variants: hir::HirVec<Variant>, | |
e9174d1e | 128 | pub generics: hir::Generics, |
9cc50fc6 | 129 | pub attrs: hir::HirVec<ast::Attribute>, |
1a4d82fc JJ |
130 | pub id: NodeId, |
131 | pub whence: Span, | |
b039eaaf | 132 | pub name: Name, |
1a4d82fc JJ |
133 | } |
134 | ||
135 | pub struct Variant { | |
b039eaaf | 136 | pub name: Name, |
9cc50fc6 | 137 | pub attrs: hir::HirVec<ast::Attribute>, |
b039eaaf | 138 | pub def: hir::VariantData, |
1a4d82fc | 139 | pub stab: Option<attr::Stability>, |
9cc50fc6 | 140 | pub depr: Option<attr::Deprecation>, |
1a4d82fc JJ |
141 | pub whence: Span, |
142 | } | |
143 | ||
144 | pub struct Function { | |
e9174d1e | 145 | pub decl: hir::FnDecl, |
9cc50fc6 | 146 | pub attrs: hir::HirVec<ast::Attribute>, |
1a4d82fc | 147 | pub id: NodeId, |
b039eaaf | 148 | pub name: Name, |
e9174d1e | 149 | pub vis: hir::Visibility, |
1a4d82fc | 150 | pub stab: Option<attr::Stability>, |
9cc50fc6 | 151 | pub depr: Option<attr::Deprecation>, |
e9174d1e SL |
152 | pub unsafety: hir::Unsafety, |
153 | pub constness: hir::Constness, | |
1a4d82fc | 154 | pub whence: Span, |
e9174d1e | 155 | pub generics: hir::Generics, |
9346a6ac | 156 | pub abi: abi::Abi, |
32a655c1 | 157 | pub body: hir::BodyId, |
1a4d82fc JJ |
158 | } |
159 | ||
160 | pub struct Typedef { | |
e9174d1e SL |
161 | pub ty: P<hir::Ty>, |
162 | pub gen: hir::Generics, | |
b039eaaf | 163 | pub name: Name, |
1a4d82fc | 164 | pub id: ast::NodeId, |
9cc50fc6 | 165 | pub attrs: hir::HirVec<ast::Attribute>, |
1a4d82fc | 166 | pub whence: Span, |
e9174d1e | 167 | pub vis: hir::Visibility, |
1a4d82fc | 168 | pub stab: Option<attr::Stability>, |
9cc50fc6 | 169 | pub depr: Option<attr::Deprecation>, |
1a4d82fc JJ |
170 | } |
171 | ||
85aaf69f | 172 | #[derive(Debug)] |
1a4d82fc | 173 | pub struct Static { |
e9174d1e SL |
174 | pub type_: P<hir::Ty>, |
175 | pub mutability: hir::Mutability, | |
32a655c1 | 176 | pub expr: hir::BodyId, |
b039eaaf | 177 | pub name: Name, |
9cc50fc6 | 178 | pub attrs: hir::HirVec<ast::Attribute>, |
e9174d1e | 179 | pub vis: hir::Visibility, |
1a4d82fc | 180 | pub stab: Option<attr::Stability>, |
9cc50fc6 | 181 | pub depr: Option<attr::Deprecation>, |
1a4d82fc JJ |
182 | pub id: ast::NodeId, |
183 | pub whence: Span, | |
184 | } | |
185 | ||
186 | pub struct Constant { | |
e9174d1e | 187 | pub type_: P<hir::Ty>, |
32a655c1 | 188 | pub expr: hir::BodyId, |
b039eaaf | 189 | pub name: Name, |
9cc50fc6 | 190 | pub attrs: hir::HirVec<ast::Attribute>, |
e9174d1e | 191 | pub vis: hir::Visibility, |
1a4d82fc | 192 | pub stab: Option<attr::Stability>, |
9cc50fc6 | 193 | pub depr: Option<attr::Deprecation>, |
1a4d82fc JJ |
194 | pub id: ast::NodeId, |
195 | pub whence: Span, | |
196 | } | |
197 | ||
198 | pub struct Trait { | |
2c00a5a8 | 199 | pub is_auto: hir::IsAuto, |
e9174d1e | 200 | pub unsafety: hir::Unsafety, |
b039eaaf | 201 | pub name: Name, |
9cc50fc6 | 202 | pub items: hir::HirVec<hir::TraitItem>, |
e9174d1e | 203 | pub generics: hir::Generics, |
9cc50fc6 SL |
204 | pub bounds: hir::HirVec<hir::TyParamBound>, |
205 | pub attrs: hir::HirVec<ast::Attribute>, | |
1a4d82fc JJ |
206 | pub id: ast::NodeId, |
207 | pub whence: Span, | |
e9174d1e | 208 | pub vis: hir::Visibility, |
1a4d82fc | 209 | pub stab: Option<attr::Stability>, |
9cc50fc6 | 210 | pub depr: Option<attr::Deprecation>, |
1a4d82fc JJ |
211 | } |
212 | ||
213 | pub struct Impl { | |
e9174d1e SL |
214 | pub unsafety: hir::Unsafety, |
215 | pub polarity: hir::ImplPolarity, | |
7cac9316 | 216 | pub defaultness: hir::Defaultness, |
e9174d1e SL |
217 | pub generics: hir::Generics, |
218 | pub trait_: Option<hir::TraitRef>, | |
219 | pub for_: P<hir::Ty>, | |
9cc50fc6 SL |
220 | pub items: hir::HirVec<hir::ImplItem>, |
221 | pub attrs: hir::HirVec<ast::Attribute>, | |
1a4d82fc | 222 | pub whence: Span, |
e9174d1e | 223 | pub vis: hir::Visibility, |
1a4d82fc | 224 | pub stab: Option<attr::Stability>, |
9cc50fc6 | 225 | pub depr: Option<attr::Deprecation>, |
1a4d82fc JJ |
226 | pub id: ast::NodeId, |
227 | } | |
228 | ||
32a655c1 SL |
229 | // For Macro we store the DefId instead of the NodeId, since we also create |
230 | // these imported macro_rules (which only have a DUMMY_NODE_ID). | |
1a4d82fc | 231 | pub struct Macro { |
b039eaaf | 232 | pub name: Name, |
32a655c1 | 233 | pub def_id: hir::def_id::DefId, |
9cc50fc6 | 234 | pub attrs: hir::HirVec<ast::Attribute>, |
1a4d82fc | 235 | pub whence: Span, |
9cc50fc6 | 236 | pub matchers: hir::HirVec<Span>, |
1a4d82fc | 237 | pub stab: Option<attr::Stability>, |
9cc50fc6 | 238 | pub depr: Option<attr::Deprecation>, |
b039eaaf | 239 | pub imported_from: Option<Name>, |
1a4d82fc JJ |
240 | } |
241 | ||
85aaf69f | 242 | pub struct ExternCrate { |
b039eaaf | 243 | pub name: Name, |
9e0c209e | 244 | pub cnum: CrateNum, |
85aaf69f | 245 | pub path: Option<String>, |
e9174d1e | 246 | pub vis: hir::Visibility, |
9cc50fc6 | 247 | pub attrs: hir::HirVec<ast::Attribute>, |
85aaf69f SL |
248 | pub whence: Span, |
249 | } | |
250 | ||
251 | pub struct Import { | |
476ff2be | 252 | pub name: Name, |
85aaf69f | 253 | pub id: NodeId, |
e9174d1e | 254 | pub vis: hir::Visibility, |
9cc50fc6 | 255 | pub attrs: hir::HirVec<ast::Attribute>, |
476ff2be SL |
256 | pub path: hir::Path, |
257 | pub glob: bool, | |
85aaf69f SL |
258 | pub whence: Span, |
259 | } | |
260 | ||
9e0c209e SL |
261 | pub fn struct_type_from_def(vdata: &hir::VariantData) -> StructType { |
262 | match *vdata { | |
263 | hir::VariantData::Struct(..) => Plain, | |
264 | hir::VariantData::Tuple(..) => Tuple, | |
265 | hir::VariantData::Unit(..) => Unit, | |
1a4d82fc JJ |
266 | } |
267 | } |