]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
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 | // The Rust abstract syntax tree. | |
12 | ||
1a4d82fc | 13 | pub use self::TyParamBound::*; |
1a4d82fc | 14 | pub use self::UnsafeSource::*; |
1a4d82fc | 15 | pub use self::ViewPath_::*; |
1a4d82fc | 16 | pub use self::PathParameters::*; |
476ff2be | 17 | pub use symbol::Symbol as Name; |
3157f602 | 18 | pub use util::ThinVec; |
1a4d82fc | 19 | |
3157f602 XL |
20 | use syntax_pos::{mk_sp, Span, DUMMY_SP, ExpnId}; |
21 | use codemap::{respan, Spanned}; | |
1a4d82fc | 22 | use abi::Abi; |
5bcae85e | 23 | use ext::hygiene::SyntaxContext; |
62682a34 | 24 | use print::pprust; |
1a4d82fc | 25 | use ptr::P; |
476ff2be | 26 | use symbol::{Symbol, keywords}; |
3157f602 | 27 | use tokenstream::{TokenTree}; |
1a4d82fc | 28 | |
476ff2be | 29 | use std::collections::HashSet; |
1a4d82fc | 30 | use std::fmt; |
1a4d82fc | 31 | use std::rc::Rc; |
9e0c209e SL |
32 | use std::u32; |
33 | ||
34 | use serialize::{self, Encodable, Decodable, Encoder, Decoder}; | |
1a4d82fc | 35 | |
1a4d82fc JJ |
36 | /// An identifier contains a Name (index into the interner |
37 | /// table) and a SyntaxContext to track renaming and | |
b039eaaf | 38 | /// macro expansion per Flatt et al., "Macros That Work Together" |
5bcae85e | 39 | #[derive(Clone, Copy, PartialEq, Eq, Hash)] |
1a4d82fc | 40 | pub struct Ident { |
476ff2be | 41 | pub name: Symbol, |
1a4d82fc JJ |
42 | pub ctxt: SyntaxContext |
43 | } | |
44 | ||
b039eaaf | 45 | impl Ident { |
a7813a04 | 46 | pub const fn with_empty_ctxt(name: Name) -> Ident { |
5bcae85e | 47 | Ident { name: name, ctxt: SyntaxContext::empty() } |
1a4d82fc | 48 | } |
476ff2be SL |
49 | |
50 | /// Maps a string to an identifier with an empty syntax context. | |
51 | pub fn from_str(s: &str) -> Ident { | |
52 | Ident::with_empty_ctxt(Symbol::intern(s)) | |
53 | } | |
b039eaaf | 54 | } |
1a4d82fc | 55 | |
b039eaaf SL |
56 | impl fmt::Debug for Ident { |
57 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
5bcae85e | 58 | write!(f, "{}{:?}", self.name, self.ctxt) |
1a4d82fc | 59 | } |
b039eaaf | 60 | } |
1a4d82fc | 61 | |
b039eaaf SL |
62 | impl fmt::Display for Ident { |
63 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
64 | fmt::Display::fmt(&self.name, f) | |
223e47cc LB |
65 | } |
66 | } | |
67 | ||
1a4d82fc JJ |
68 | impl Encodable for Ident { |
69 | fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { | |
b039eaaf | 70 | self.name.encode(s) |
223e47cc LB |
71 | } |
72 | } | |
73 | ||
1a4d82fc JJ |
74 | impl Decodable for Ident { |
75 | fn decode<D: Decoder>(d: &mut D) -> Result<Ident, D::Error> { | |
54a0048b | 76 | Ok(Ident::with_empty_ctxt(Name::decode(d)?)) |
1a4d82fc JJ |
77 | } |
78 | } | |
223e47cc | 79 | |
62682a34 | 80 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] |
223e47cc | 81 | pub struct Lifetime { |
1a4d82fc JJ |
82 | pub id: NodeId, |
83 | pub span: Span, | |
84 | pub name: Name | |
85 | } | |
86 | ||
62682a34 SL |
87 | impl fmt::Debug for Lifetime { |
88 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
89 | write!(f, "lifetime({}: {})", self.id, pprust::lifetime_to_string(self)) | |
90 | } | |
91 | } | |
92 | ||
3157f602 | 93 | /// A lifetime definition, e.g. `'a: 'b+'c+'d` |
85aaf69f | 94 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc | 95 | pub struct LifetimeDef { |
c30ab7b3 | 96 | pub attrs: ThinVec<Attribute>, |
1a4d82fc JJ |
97 | pub lifetime: Lifetime, |
98 | pub bounds: Vec<Lifetime> | |
223e47cc LB |
99 | } |
100 | ||
3157f602 XL |
101 | /// A "Path" is essentially Rust's notion of a name. |
102 | /// | |
103 | /// It's represented as a sequence of identifiers, | |
1a4d82fc | 104 | /// along with a bunch of supporting information. |
3157f602 XL |
105 | /// |
106 | /// E.g. `std::cmp::PartialEq` | |
62682a34 | 107 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] |
970d7e83 | 108 | pub struct Path { |
1a4d82fc JJ |
109 | pub span: Span, |
110 | /// A `::foo` path, is relative to the crate root rather than current | |
111 | /// module (like paths in an import). | |
112 | pub global: bool, | |
113 | /// The segments in the path: the things separated by `::`. | |
114 | pub segments: Vec<PathSegment>, | |
115 | } | |
116 | ||
62682a34 SL |
117 | impl fmt::Debug for Path { |
118 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
119 | write!(f, "path({})", pprust::path_to_string(self)) | |
120 | } | |
121 | } | |
122 | ||
123 | impl fmt::Display for Path { | |
124 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
125 | write!(f, "{}", pprust::path_to_string(self)) | |
126 | } | |
127 | } | |
128 | ||
54a0048b SL |
129 | impl Path { |
130 | // convert a span and an identifier to the corresponding | |
131 | // 1-segment path | |
132 | pub fn from_ident(s: Span, identifier: Ident) -> Path { | |
133 | Path { | |
134 | span: s, | |
135 | global: false, | |
c30ab7b3 | 136 | segments: vec![ |
54a0048b SL |
137 | PathSegment { |
138 | identifier: identifier, | |
139 | parameters: PathParameters::none() | |
140 | } | |
c30ab7b3 | 141 | ], |
54a0048b SL |
142 | } |
143 | } | |
144 | } | |
145 | ||
3157f602 XL |
146 | /// A segment of a path: an identifier, an optional lifetime, and a set of types. |
147 | /// | |
148 | /// E.g. `std`, `String` or `Box<T>` | |
85aaf69f | 149 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
150 | pub struct PathSegment { |
151 | /// The identifier portion of this path segment. | |
152 | pub identifier: Ident, | |
153 | ||
154 | /// Type/lifetime parameters attached to this path. They come in | |
155 | /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that | |
156 | /// this is more than just simple syntactic sugar; the use of | |
157 | /// parens affects the region binding rules, so we preserve the | |
158 | /// distinction. | |
159 | pub parameters: PathParameters, | |
160 | } | |
161 | ||
3157f602 XL |
162 | /// Parameters of a path segment. |
163 | /// | |
164 | /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)` | |
85aaf69f | 165 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc | 166 | pub enum PathParameters { |
c34b1796 | 167 | /// The `<'a, A,B,C>` in `foo::bar::baz::<'a, A,B,C>` |
9cc50fc6 | 168 | AngleBracketed(AngleBracketedParameterData), |
c34b1796 | 169 | /// The `(A,B)` and `C` in `Foo(A,B) -> C` |
9cc50fc6 | 170 | Parenthesized(ParenthesizedParameterData), |
1a4d82fc JJ |
171 | } |
172 | ||
173 | impl PathParameters { | |
174 | pub fn none() -> PathParameters { | |
9cc50fc6 | 175 | PathParameters::AngleBracketed(AngleBracketedParameterData { |
1a4d82fc | 176 | lifetimes: Vec::new(), |
a7813a04 XL |
177 | types: P::new(), |
178 | bindings: P::new(), | |
1a4d82fc JJ |
179 | }) |
180 | } | |
181 | ||
182 | pub fn is_empty(&self) -> bool { | |
183 | match *self { | |
9cc50fc6 | 184 | PathParameters::AngleBracketed(ref data) => data.is_empty(), |
1a4d82fc JJ |
185 | |
186 | // Even if the user supplied no types, something like | |
187 | // `X()` is equivalent to `X<(),()>`. | |
9cc50fc6 | 188 | PathParameters::Parenthesized(..) => false, |
1a4d82fc JJ |
189 | } |
190 | } | |
191 | ||
192 | pub fn has_lifetimes(&self) -> bool { | |
193 | match *self { | |
9cc50fc6 SL |
194 | PathParameters::AngleBracketed(ref data) => !data.lifetimes.is_empty(), |
195 | PathParameters::Parenthesized(_) => false, | |
1a4d82fc JJ |
196 | } |
197 | } | |
198 | ||
199 | pub fn has_types(&self) -> bool { | |
200 | match *self { | |
9cc50fc6 SL |
201 | PathParameters::AngleBracketed(ref data) => !data.types.is_empty(), |
202 | PathParameters::Parenthesized(..) => true, | |
1a4d82fc JJ |
203 | } |
204 | } | |
205 | ||
206 | /// Returns the types that the user wrote. Note that these do not necessarily map to the type | |
207 | /// parameters in the parenthesized case. | |
208 | pub fn types(&self) -> Vec<&P<Ty>> { | |
209 | match *self { | |
9cc50fc6 | 210 | PathParameters::AngleBracketed(ref data) => { |
1a4d82fc JJ |
211 | data.types.iter().collect() |
212 | } | |
9cc50fc6 | 213 | PathParameters::Parenthesized(ref data) => { |
1a4d82fc JJ |
214 | data.inputs.iter() |
215 | .chain(data.output.iter()) | |
216 | .collect() | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
221 | pub fn lifetimes(&self) -> Vec<&Lifetime> { | |
222 | match *self { | |
9cc50fc6 | 223 | PathParameters::AngleBracketed(ref data) => { |
1a4d82fc JJ |
224 | data.lifetimes.iter().collect() |
225 | } | |
9cc50fc6 | 226 | PathParameters::Parenthesized(_) => { |
1a4d82fc JJ |
227 | Vec::new() |
228 | } | |
229 | } | |
230 | } | |
231 | ||
7453a54e | 232 | pub fn bindings(&self) -> Vec<&TypeBinding> { |
1a4d82fc | 233 | match *self { |
9cc50fc6 | 234 | PathParameters::AngleBracketed(ref data) => { |
1a4d82fc JJ |
235 | data.bindings.iter().collect() |
236 | } | |
9cc50fc6 | 237 | PathParameters::Parenthesized(_) => { |
1a4d82fc JJ |
238 | Vec::new() |
239 | } | |
240 | } | |
241 | } | |
242 | } | |
243 | ||
244 | /// A path like `Foo<'a, T>` | |
85aaf69f | 245 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
246 | pub struct AngleBracketedParameterData { |
247 | /// The lifetime parameters for this path segment. | |
248 | pub lifetimes: Vec<Lifetime>, | |
249 | /// The type parameters for this path segment, if present. | |
9cc50fc6 | 250 | pub types: P<[P<Ty>]>, |
1a4d82fc | 251 | /// Bindings (equality constraints) on associated types, if present. |
3157f602 XL |
252 | /// |
253 | /// E.g., `Foo<A=Bar>`. | |
7453a54e | 254 | pub bindings: P<[TypeBinding]>, |
223e47cc LB |
255 | } |
256 | ||
1a4d82fc JJ |
257 | impl AngleBracketedParameterData { |
258 | fn is_empty(&self) -> bool { | |
259 | self.lifetimes.is_empty() && self.types.is_empty() && self.bindings.is_empty() | |
260 | } | |
261 | } | |
262 | ||
263 | /// A path like `Foo(A,B) -> C` | |
85aaf69f | 264 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc | 265 | pub struct ParenthesizedParameterData { |
85aaf69f SL |
266 | /// Overall span |
267 | pub span: Span, | |
268 | ||
1a4d82fc JJ |
269 | /// `(A,B)` |
270 | pub inputs: Vec<P<Ty>>, | |
271 | ||
272 | /// `C` | |
273 | pub output: Option<P<Ty>>, | |
274 | } | |
275 | ||
9e0c209e SL |
276 | #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)] |
277 | pub struct NodeId(u32); | |
278 | ||
279 | impl NodeId { | |
280 | pub fn new(x: usize) -> NodeId { | |
281 | assert!(x < (u32::MAX as usize)); | |
282 | NodeId(x as u32) | |
283 | } | |
284 | ||
285 | pub fn from_u32(x: u32) -> NodeId { | |
286 | NodeId(x) | |
287 | } | |
223e47cc | 288 | |
9e0c209e SL |
289 | pub fn as_usize(&self) -> usize { |
290 | self.0 as usize | |
291 | } | |
292 | ||
293 | pub fn as_u32(&self) -> u32 { | |
294 | self.0 | |
295 | } | |
296 | } | |
297 | ||
298 | impl fmt::Display for NodeId { | |
299 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
300 | fmt::Display::fmt(&self.0, f) | |
301 | } | |
302 | } | |
303 | ||
304 | impl serialize::UseSpecializedEncodable for NodeId { | |
305 | fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { | |
306 | s.emit_u32(self.0) | |
307 | } | |
308 | } | |
309 | ||
310 | impl serialize::UseSpecializedDecodable for NodeId { | |
311 | fn default_decode<D: Decoder>(d: &mut D) -> Result<NodeId, D::Error> { | |
312 | d.read_u32().map(NodeId) | |
313 | } | |
314 | } | |
223e47cc | 315 | |
e9174d1e | 316 | /// Node id used to represent the root of the crate. |
9e0c209e | 317 | pub const CRATE_NODE_ID: NodeId = NodeId(0); |
223e47cc | 318 | |
1a4d82fc JJ |
319 | /// When parsing and doing expansions, we initially give all AST nodes this AST |
320 | /// node value. Then later, in the renumber pass, we renumber them to have | |
321 | /// small, positive ids. | |
9e0c209e | 322 | pub const DUMMY_NODE_ID: NodeId = NodeId(!0); |
1a4d82fc JJ |
323 | |
324 | /// The AST represents all type param bounds as types. | |
325 | /// typeck::collect::compute_bounds matches these against | |
326 | /// the "special" built-in traits (see middle::lang_items) and | |
327 | /// detects Copy, Send and Sync. | |
85aaf69f | 328 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
223e47cc | 329 | pub enum TyParamBound { |
1a4d82fc JJ |
330 | TraitTyParamBound(PolyTraitRef, TraitBoundModifier), |
331 | RegionTyParamBound(Lifetime) | |
223e47cc LB |
332 | } |
333 | ||
1a4d82fc JJ |
334 | /// A modifier on a bound, currently this is only used for `?Sized`, where the |
335 | /// modifier is `Maybe`. Negative bounds should also be handled here. | |
85aaf69f | 336 | #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
337 | pub enum TraitBoundModifier { |
338 | None, | |
339 | Maybe, | |
340 | } | |
341 | ||
9cc50fc6 | 342 | pub type TyParamBounds = P<[TyParamBound]>; |
1a4d82fc | 343 | |
85aaf69f | 344 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
223e47cc | 345 | pub struct TyParam { |
c30ab7b3 | 346 | pub attrs: ThinVec<Attribute>, |
1a4d82fc JJ |
347 | pub ident: Ident, |
348 | pub id: NodeId, | |
349 | pub bounds: TyParamBounds, | |
350 | pub default: Option<P<Ty>>, | |
9e0c209e | 351 | pub span: Span, |
223e47cc LB |
352 | } |
353 | ||
1a4d82fc JJ |
354 | /// Represents lifetimes and type parameters attached to a declaration |
355 | /// of a function, enum, trait, etc. | |
85aaf69f | 356 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
223e47cc | 357 | pub struct Generics { |
1a4d82fc | 358 | pub lifetimes: Vec<LifetimeDef>, |
9cc50fc6 | 359 | pub ty_params: P<[TyParam]>, |
1a4d82fc | 360 | pub where_clause: WhereClause, |
9e0c209e | 361 | pub span: Span, |
223e47cc LB |
362 | } |
363 | ||
970d7e83 | 364 | impl Generics { |
970d7e83 | 365 | pub fn is_lt_parameterized(&self) -> bool { |
9346a6ac | 366 | !self.lifetimes.is_empty() |
223e47cc | 367 | } |
970d7e83 | 368 | pub fn is_type_parameterized(&self) -> bool { |
9346a6ac AL |
369 | !self.ty_params.is_empty() |
370 | } | |
371 | pub fn is_parameterized(&self) -> bool { | |
372 | self.is_lt_parameterized() || self.is_type_parameterized() | |
223e47cc | 373 | } |
476ff2be SL |
374 | pub fn span_for_name(&self, name: &str) -> Option<Span> { |
375 | for t in &self.ty_params { | |
376 | if t.ident.name == name { | |
377 | return Some(t.span); | |
378 | } | |
379 | } | |
380 | None | |
381 | } | |
223e47cc LB |
382 | } |
383 | ||
9cc50fc6 | 384 | impl Default for Generics { |
9e0c209e | 385 | /// Creates an instance of `Generics`. |
9cc50fc6 SL |
386 | fn default() -> Generics { |
387 | Generics { | |
388 | lifetimes: Vec::new(), | |
a7813a04 | 389 | ty_params: P::new(), |
9cc50fc6 SL |
390 | where_clause: WhereClause { |
391 | id: DUMMY_NODE_ID, | |
392 | predicates: Vec::new(), | |
9e0c209e SL |
393 | }, |
394 | span: DUMMY_SP, | |
9cc50fc6 SL |
395 | } |
396 | } | |
397 | } | |
398 | ||
c34b1796 | 399 | /// A `where` clause in a definition |
85aaf69f | 400 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
401 | pub struct WhereClause { |
402 | pub id: NodeId, | |
403 | pub predicates: Vec<WherePredicate>, | |
223e47cc LB |
404 | } |
405 | ||
c34b1796 | 406 | /// A single predicate in a `where` clause |
85aaf69f | 407 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc | 408 | pub enum WherePredicate { |
9cc50fc6 | 409 | /// A type binding, e.g. `for<'c> Foo: Send+Clone+'c` |
1a4d82fc | 410 | BoundPredicate(WhereBoundPredicate), |
c34b1796 | 411 | /// A lifetime predicate, e.g. `'a: 'b+'c` |
1a4d82fc | 412 | RegionPredicate(WhereRegionPredicate), |
c34b1796 | 413 | /// An equality predicate (unsupported) |
e9174d1e | 414 | EqPredicate(WhereEqPredicate), |
223e47cc LB |
415 | } |
416 | ||
3157f602 XL |
417 | /// A type bound. |
418 | /// | |
419 | /// E.g. `for<'c> Foo: Send+Clone+'c` | |
85aaf69f | 420 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
421 | pub struct WhereBoundPredicate { |
422 | pub span: Span, | |
c34b1796 | 423 | /// Any lifetimes from a `for` binding |
85aaf69f | 424 | pub bound_lifetimes: Vec<LifetimeDef>, |
c34b1796 | 425 | /// The type being bounded |
1a4d82fc | 426 | pub bounded_ty: P<Ty>, |
c34b1796 | 427 | /// Trait and lifetime bounds (`Clone+Send+'static`) |
9cc50fc6 | 428 | pub bounds: TyParamBounds, |
223e47cc LB |
429 | } |
430 | ||
3157f602 XL |
431 | /// A lifetime predicate. |
432 | /// | |
433 | /// E.g. `'a: 'b+'c` | |
85aaf69f | 434 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
435 | pub struct WhereRegionPredicate { |
436 | pub span: Span, | |
437 | pub lifetime: Lifetime, | |
438 | pub bounds: Vec<Lifetime>, | |
439 | } | |
223e47cc | 440 | |
3157f602 XL |
441 | /// An equality predicate (unsupported). |
442 | /// | |
443 | /// E.g. `T=int` | |
85aaf69f | 444 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
445 | pub struct WhereEqPredicate { |
446 | pub id: NodeId, | |
447 | pub span: Span, | |
448 | pub path: Path, | |
449 | pub ty: P<Ty>, | |
450 | } | |
451 | ||
452 | /// The set of MetaItems that define the compilation environment of the crate, | |
453 | /// used to drive conditional compilation | |
476ff2be | 454 | pub type CrateConfig = HashSet<(Name, Option<Symbol>)>; |
223e47cc | 455 | |
85aaf69f | 456 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
457 | pub struct Crate { |
458 | pub module: Mod, | |
459 | pub attrs: Vec<Attribute>, | |
1a4d82fc JJ |
460 | pub span: Span, |
461 | pub exported_macros: Vec<MacroDef>, | |
970d7e83 | 462 | } |
223e47cc | 463 | |
9e0c209e SL |
464 | /// A spanned compile-time attribute list item. |
465 | pub type NestedMetaItem = Spanned<NestedMetaItemKind>; | |
466 | ||
467 | /// Possible values inside of compile-time attribute lists. | |
468 | /// | |
469 | /// E.g. the '..' in `#[name(..)]`. | |
470 | #[derive(Clone, Eq, RustcEncodable, RustcDecodable, Hash, Debug, PartialEq)] | |
471 | pub enum NestedMetaItemKind { | |
472 | /// A full MetaItem, for recursive meta items. | |
476ff2be | 473 | MetaItem(MetaItem), |
9e0c209e SL |
474 | /// A literal. |
475 | /// | |
476 | /// E.g. "foo", 64, true | |
477 | Literal(Lit), | |
478 | } | |
479 | ||
3157f602 XL |
480 | /// A spanned compile-time attribute item. |
481 | /// | |
482 | /// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]` | |
476ff2be SL |
483 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
484 | pub struct MetaItem { | |
485 | pub name: Name, | |
486 | pub node: MetaItemKind, | |
487 | pub span: Span, | |
488 | } | |
1a4d82fc | 489 | |
3157f602 XL |
490 | /// A compile-time attribute item. |
491 | /// | |
492 | /// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]` | |
476ff2be | 493 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
7453a54e | 494 | pub enum MetaItemKind { |
3157f602 XL |
495 | /// Word meta item. |
496 | /// | |
497 | /// E.g. `test` as in `#[test]` | |
476ff2be | 498 | Word, |
3157f602 XL |
499 | /// List meta item. |
500 | /// | |
501 | /// E.g. `derive(..)` as in `#[derive(..)]` | |
476ff2be | 502 | List(Vec<NestedMetaItem>), |
3157f602 XL |
503 | /// Name value meta item. |
504 | /// | |
505 | /// E.g. `feature = "foo"` as in `#[feature = "foo"]` | |
476ff2be | 506 | NameValue(Lit) |
223e47cc LB |
507 | } |
508 | ||
3157f602 XL |
509 | /// A Block (`{ .. }`). |
510 | /// | |
511 | /// E.g. `{ .. }` as in `fn foo() { .. }` | |
85aaf69f | 512 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc | 513 | pub struct Block { |
c34b1796 | 514 | /// Statements in a block |
7453a54e | 515 | pub stmts: Vec<Stmt>, |
1a4d82fc | 516 | pub id: NodeId, |
c34b1796 | 517 | /// Distinguishes between `unsafe { ... }` and `{ ... }` |
1a4d82fc JJ |
518 | pub rules: BlockCheckMode, |
519 | pub span: Span, | |
520 | } | |
521 | ||
62682a34 | 522 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] |
1a4d82fc JJ |
523 | pub struct Pat { |
524 | pub id: NodeId, | |
7453a54e | 525 | pub node: PatKind, |
1a4d82fc JJ |
526 | pub span: Span, |
527 | } | |
528 | ||
62682a34 SL |
529 | impl fmt::Debug for Pat { |
530 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
531 | write!(f, "pat({}: {})", self.id, pprust::pat_to_string(self)) | |
532 | } | |
533 | } | |
534 | ||
a7813a04 XL |
535 | impl Pat { |
536 | pub fn walk<F>(&self, it: &mut F) -> bool | |
537 | where F: FnMut(&Pat) -> bool | |
538 | { | |
539 | if !it(self) { | |
540 | return false; | |
541 | } | |
542 | ||
543 | match self.node { | |
544 | PatKind::Ident(_, _, Some(ref p)) => p.walk(it), | |
545 | PatKind::Struct(_, ref fields, _) => { | |
546 | fields.iter().all(|field| field.node.pat.walk(it)) | |
547 | } | |
3157f602 | 548 | PatKind::TupleStruct(_, ref s, _) | PatKind::Tuple(ref s, _) => { |
a7813a04 XL |
549 | s.iter().all(|p| p.walk(it)) |
550 | } | |
551 | PatKind::Box(ref s) | PatKind::Ref(ref s, _) => { | |
552 | s.walk(it) | |
553 | } | |
c30ab7b3 | 554 | PatKind::Slice(ref before, ref slice, ref after) => { |
a7813a04 XL |
555 | before.iter().all(|p| p.walk(it)) && |
556 | slice.iter().all(|p| p.walk(it)) && | |
557 | after.iter().all(|p| p.walk(it)) | |
558 | } | |
559 | PatKind::Wild | | |
560 | PatKind::Lit(_) | | |
9e0c209e SL |
561 | PatKind::Range(..) | |
562 | PatKind::Ident(..) | | |
a7813a04 | 563 | PatKind::Path(..) | |
a7813a04 XL |
564 | PatKind::Mac(_) => { |
565 | true | |
566 | } | |
567 | } | |
568 | } | |
569 | } | |
570 | ||
c34b1796 AL |
571 | /// A single field in a struct pattern |
572 | /// | |
573 | /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` | |
574 | /// are treated the same as` x: x, y: ref y, z: ref mut z`, | |
575 | /// except is_shorthand is true | |
85aaf69f | 576 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc | 577 | pub struct FieldPat { |
c34b1796 | 578 | /// The identifier for the field |
1a4d82fc | 579 | pub ident: Ident, |
c34b1796 | 580 | /// The pattern the field is destructured to |
1a4d82fc JJ |
581 | pub pat: P<Pat>, |
582 | pub is_shorthand: bool, | |
583 | } | |
584 | ||
85aaf69f | 585 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
1a4d82fc | 586 | pub enum BindingMode { |
9cc50fc6 SL |
587 | ByRef(Mutability), |
588 | ByValue(Mutability), | |
1a4d82fc JJ |
589 | } |
590 | ||
85aaf69f | 591 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
7453a54e | 592 | pub enum PatKind { |
92a42be0 | 593 | /// Represents a wildcard pattern (`_`) |
7453a54e | 594 | Wild, |
1a4d82fc | 595 | |
3157f602 XL |
596 | /// A `PatKind::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`), |
597 | /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third | |
598 | /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens | |
599 | /// during name resolution. | |
7453a54e | 600 | Ident(BindingMode, SpannedIdent, Option<P<Pat>>), |
1a4d82fc | 601 | |
7453a54e SL |
602 | /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`. |
603 | /// The `bool` is `true` in the presence of a `..`. | |
604 | Struct(Path, Vec<Spanned<FieldPat>>, bool), | |
605 | ||
3157f602 XL |
606 | /// A tuple struct/variant pattern `Variant(x, y, .., z)`. |
607 | /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position. | |
608 | /// 0 <= position <= subpats.len() | |
609 | TupleStruct(Path, Vec<P<Pat>>, Option<usize>), | |
1a4d82fc | 610 | |
3157f602 XL |
611 | /// A possibly qualified path pattern. |
612 | /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants | |
613 | /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can | |
614 | /// only legally refer to associated constants. | |
615 | Path(Option<QSelf>, Path), | |
d9579d0f | 616 | |
3157f602 XL |
617 | /// A tuple pattern `(a, b)`. |
618 | /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position. | |
619 | /// 0 <= position <= subpats.len() | |
620 | Tuple(Vec<P<Pat>>, Option<usize>), | |
c34b1796 | 621 | /// A `box` pattern |
7453a54e | 622 | Box(P<Pat>), |
c34b1796 | 623 | /// A reference pattern, e.g. `&mut (a, b)` |
7453a54e | 624 | Ref(P<Pat>, Mutability), |
c34b1796 | 625 | /// A literal |
7453a54e | 626 | Lit(P<Expr>), |
c34b1796 | 627 | /// A range pattern, e.g. `1...2` |
7453a54e | 628 | Range(P<Expr>, P<Expr>), |
92a42be0 | 629 | /// `[a, b, ..i, y, z]` is represented as: |
c30ab7b3 SL |
630 | /// `PatKind::Slice(box [a, b], Some(i), box [y, z])` |
631 | Slice(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>), | |
c34b1796 | 632 | /// A macro pattern; pre-expansion |
7453a54e | 633 | Mac(Mac), |
1a4d82fc JJ |
634 | } |
635 | ||
85aaf69f | 636 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
1a4d82fc | 637 | pub enum Mutability { |
7453a54e SL |
638 | Mutable, |
639 | Immutable, | |
1a4d82fc JJ |
640 | } |
641 | ||
85aaf69f | 642 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
7453a54e | 643 | pub enum BinOpKind { |
c34b1796 | 644 | /// The `+` operator (addition) |
7453a54e | 645 | Add, |
c34b1796 | 646 | /// The `-` operator (subtraction) |
7453a54e | 647 | Sub, |
c34b1796 | 648 | /// The `*` operator (multiplication) |
7453a54e | 649 | Mul, |
c34b1796 | 650 | /// The `/` operator (division) |
7453a54e | 651 | Div, |
c34b1796 | 652 | /// The `%` operator (modulus) |
7453a54e | 653 | Rem, |
c34b1796 | 654 | /// The `&&` operator (logical and) |
7453a54e | 655 | And, |
c34b1796 | 656 | /// The `||` operator (logical or) |
7453a54e | 657 | Or, |
c34b1796 | 658 | /// The `^` operator (bitwise xor) |
7453a54e | 659 | BitXor, |
c34b1796 | 660 | /// The `&` operator (bitwise and) |
7453a54e | 661 | BitAnd, |
c34b1796 | 662 | /// The `|` operator (bitwise or) |
7453a54e | 663 | BitOr, |
c34b1796 | 664 | /// The `<<` operator (shift left) |
7453a54e | 665 | Shl, |
c34b1796 | 666 | /// The `>>` operator (shift right) |
7453a54e | 667 | Shr, |
c34b1796 | 668 | /// The `==` operator (equality) |
7453a54e | 669 | Eq, |
c34b1796 | 670 | /// The `<` operator (less than) |
7453a54e | 671 | Lt, |
c34b1796 | 672 | /// The `<=` operator (less than or equal to) |
7453a54e | 673 | Le, |
c34b1796 | 674 | /// The `!=` operator (not equal to) |
7453a54e | 675 | Ne, |
c34b1796 | 676 | /// The `>=` operator (greater than or equal to) |
7453a54e | 677 | Ge, |
c34b1796 | 678 | /// The `>` operator (greater than) |
7453a54e | 679 | Gt, |
1a4d82fc JJ |
680 | } |
681 | ||
7453a54e | 682 | impl BinOpKind { |
9cc50fc6 | 683 | pub fn to_string(&self) -> &'static str { |
7453a54e | 684 | use self::BinOpKind::*; |
9cc50fc6 | 685 | match *self { |
7453a54e SL |
686 | Add => "+", |
687 | Sub => "-", | |
688 | Mul => "*", | |
689 | Div => "/", | |
690 | Rem => "%", | |
691 | And => "&&", | |
692 | Or => "||", | |
693 | BitXor => "^", | |
694 | BitAnd => "&", | |
695 | BitOr => "|", | |
696 | Shl => "<<", | |
697 | Shr => ">>", | |
698 | Eq => "==", | |
699 | Lt => "<", | |
700 | Le => "<=", | |
701 | Ne => "!=", | |
702 | Ge => ">=", | |
703 | Gt => ">", | |
9cc50fc6 SL |
704 | } |
705 | } | |
706 | pub fn lazy(&self) -> bool { | |
707 | match *self { | |
7453a54e | 708 | BinOpKind::And | BinOpKind::Or => true, |
9cc50fc6 SL |
709 | _ => false |
710 | } | |
711 | } | |
712 | ||
713 | pub fn is_shift(&self) -> bool { | |
714 | match *self { | |
7453a54e | 715 | BinOpKind::Shl | BinOpKind::Shr => true, |
9cc50fc6 SL |
716 | _ => false |
717 | } | |
718 | } | |
719 | pub fn is_comparison(&self) -> bool { | |
7453a54e | 720 | use self::BinOpKind::*; |
9cc50fc6 | 721 | match *self { |
7453a54e | 722 | Eq | Lt | Le | Ne | Gt | Ge => |
9cc50fc6 | 723 | true, |
7453a54e SL |
724 | And | Or | Add | Sub | Mul | Div | Rem | |
725 | BitXor | BitAnd | BitOr | Shl | Shr => | |
9cc50fc6 SL |
726 | false, |
727 | } | |
728 | } | |
729 | /// Returns `true` if the binary operator takes its arguments by value | |
730 | pub fn is_by_value(&self) -> bool { | |
7453a54e | 731 | !self.is_comparison() |
9cc50fc6 SL |
732 | } |
733 | } | |
734 | ||
7453a54e | 735 | pub type BinOp = Spanned<BinOpKind>; |
85aaf69f SL |
736 | |
737 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] | |
1a4d82fc | 738 | pub enum UnOp { |
c34b1796 | 739 | /// The `*` operator for dereferencing |
7453a54e | 740 | Deref, |
c34b1796 | 741 | /// The `!` operator for logical inversion |
7453a54e | 742 | Not, |
c34b1796 | 743 | /// The `-` operator for negation |
7453a54e | 744 | Neg, |
1a4d82fc JJ |
745 | } |
746 | ||
9cc50fc6 SL |
747 | impl UnOp { |
748 | /// Returns `true` if the unary operator takes its argument by value | |
749 | pub fn is_by_value(u: UnOp) -> bool { | |
750 | match u { | |
7453a54e | 751 | UnOp::Neg | UnOp::Not => true, |
9cc50fc6 SL |
752 | _ => false, |
753 | } | |
754 | } | |
755 | ||
756 | pub fn to_string(op: UnOp) -> &'static str { | |
757 | match op { | |
7453a54e SL |
758 | UnOp::Deref => "*", |
759 | UnOp::Not => "!", | |
760 | UnOp::Neg => "-", | |
9cc50fc6 SL |
761 | } |
762 | } | |
763 | } | |
764 | ||
c34b1796 | 765 | /// A statement |
3157f602 XL |
766 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] |
767 | pub struct Stmt { | |
768 | pub id: NodeId, | |
769 | pub node: StmtKind, | |
770 | pub span: Span, | |
771 | } | |
1a4d82fc | 772 | |
5bcae85e SL |
773 | impl Stmt { |
774 | pub fn add_trailing_semicolon(mut self) -> Self { | |
775 | self.node = match self.node { | |
776 | StmtKind::Expr(expr) => StmtKind::Semi(expr), | |
777 | StmtKind::Mac(mac) => StmtKind::Mac(mac.map(|(mac, _style, attrs)| { | |
778 | (mac, MacStmtStyle::Semicolon, attrs) | |
779 | })), | |
780 | node @ _ => node, | |
781 | }; | |
782 | self | |
783 | } | |
784 | } | |
785 | ||
62682a34 SL |
786 | impl fmt::Debug for Stmt { |
787 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
3157f602 | 788 | write!(f, "stmt({}: {})", self.id.to_string(), pprust::stmt_to_string(self)) |
62682a34 SL |
789 | } |
790 | } | |
791 | ||
792 | ||
793 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] | |
7453a54e | 794 | pub enum StmtKind { |
3157f602 XL |
795 | /// A local (let) binding. |
796 | Local(P<Local>), | |
1a4d82fc | 797 | |
3157f602 XL |
798 | /// An item definition. |
799 | Item(P<Item>), | |
1a4d82fc | 800 | |
3157f602 XL |
801 | /// Expr without trailing semi-colon. |
802 | Expr(P<Expr>), | |
92a42be0 | 803 | |
3157f602 | 804 | Semi(P<Expr>), |
9cc50fc6 | 805 | |
3157f602 | 806 | Mac(P<(Mac, MacStmtStyle, ThinVec<Attribute>)>), |
92a42be0 SL |
807 | } |
808 | ||
85aaf69f | 809 | #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
810 | pub enum MacStmtStyle { |
811 | /// The macro statement had a trailing semicolon, e.g. `foo! { ... };` | |
812 | /// `foo!(...);`, `foo![...];` | |
7453a54e | 813 | Semicolon, |
1a4d82fc | 814 | /// The macro statement had braces; e.g. foo! { ... } |
7453a54e | 815 | Braces, |
1a4d82fc JJ |
816 | /// The macro statement had parentheses or brackets and no semicolon; e.g. |
817 | /// `foo!(...)`. All of these will end up being converted into macro | |
818 | /// expressions. | |
7453a54e | 819 | NoBraces, |
1a4d82fc JJ |
820 | } |
821 | ||
1a4d82fc JJ |
822 | // FIXME (pending discussion of #1697, #2178...): local should really be |
823 | // a refinement on pat. | |
824 | /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;` | |
85aaf69f | 825 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
826 | pub struct Local { |
827 | pub pat: P<Pat>, | |
828 | pub ty: Option<P<Ty>>, | |
c34b1796 | 829 | /// Initializer expression to set the value, if any |
1a4d82fc JJ |
830 | pub init: Option<P<Expr>>, |
831 | pub id: NodeId, | |
832 | pub span: Span, | |
3157f602 | 833 | pub attrs: ThinVec<Attribute>, |
1a4d82fc JJ |
834 | } |
835 | ||
3157f602 XL |
836 | /// An arm of a 'match'. |
837 | /// | |
838 | /// E.g. `0...10 => { println!("match!") }` as in | |
839 | /// | |
840 | /// ```rust,ignore | |
841 | /// match n { | |
842 | /// 0...10 => { println!("match!") }, | |
843 | /// // .. | |
844 | /// } | |
845 | /// ``` | |
85aaf69f | 846 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
847 | pub struct Arm { |
848 | pub attrs: Vec<Attribute>, | |
849 | pub pats: Vec<P<Pat>>, | |
850 | pub guard: Option<P<Expr>>, | |
851 | pub body: P<Expr>, | |
852 | } | |
853 | ||
85aaf69f | 854 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
855 | pub struct Field { |
856 | pub ident: SpannedIdent, | |
857 | pub expr: P<Expr>, | |
858 | pub span: Span, | |
c30ab7b3 | 859 | pub is_shorthand: bool, |
1a4d82fc JJ |
860 | } |
861 | ||
862 | pub type SpannedIdent = Spanned<Ident>; | |
863 | ||
85aaf69f | 864 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
1a4d82fc | 865 | pub enum BlockCheckMode { |
7453a54e SL |
866 | Default, |
867 | Unsafe(UnsafeSource), | |
1a4d82fc JJ |
868 | } |
869 | ||
85aaf69f | 870 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
1a4d82fc JJ |
871 | pub enum UnsafeSource { |
872 | CompilerGenerated, | |
873 | UserProvided, | |
874 | } | |
875 | ||
c34b1796 | 876 | /// An expression |
62682a34 | 877 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash,)] |
1a4d82fc JJ |
878 | pub struct Expr { |
879 | pub id: NodeId, | |
7453a54e | 880 | pub node: ExprKind, |
1a4d82fc | 881 | pub span: Span, |
3157f602 | 882 | pub attrs: ThinVec<Attribute> |
1a4d82fc JJ |
883 | } |
884 | ||
62682a34 SL |
885 | impl fmt::Debug for Expr { |
886 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
887 | write!(f, "expr({}: {})", self.id, pprust::expr_to_string(self)) | |
888 | } | |
889 | } | |
890 | ||
54a0048b SL |
891 | /// Limit types of a range (inclusive or exclusive) |
892 | #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] | |
893 | pub enum RangeLimits { | |
894 | /// Inclusive at the beginning, exclusive at the end | |
895 | HalfOpen, | |
896 | /// Inclusive at the beginning and end | |
897 | Closed, | |
898 | } | |
899 | ||
85aaf69f | 900 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
7453a54e | 901 | pub enum ExprKind { |
b039eaaf | 902 | /// A `box x` expression. |
7453a54e | 903 | Box(P<Expr>), |
1a4d82fc | 904 | /// First expr is the place; second expr is the value. |
7453a54e | 905 | InPlace(P<Expr>, P<Expr>), |
c34b1796 | 906 | /// An array (`[a, b, c, d]`) |
7453a54e | 907 | Vec(Vec<P<Expr>>), |
c34b1796 AL |
908 | /// A function call |
909 | /// | |
910 | /// The first field resolves to the function itself, | |
911 | /// and the second field is the list of arguments | |
7453a54e | 912 | Call(P<Expr>, Vec<P<Expr>>), |
c34b1796 AL |
913 | /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`) |
914 | /// | |
915 | /// The `SpannedIdent` is the identifier for the method name. | |
916 | /// The vector of `Ty`s are the ascripted type parameters for the method | |
917 | /// (within the angle brackets). | |
918 | /// | |
919 | /// The first element of the vector of `Expr`s is the expression that evaluates | |
920 | /// to the object on which the method is being called on (the receiver), | |
921 | /// and the remaining elements are the rest of the arguments. | |
922 | /// | |
923 | /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as | |
7453a54e SL |
924 | /// `ExprKind::MethodCall(foo, [Bar, Baz], [x, a, b, c, d])`. |
925 | MethodCall(SpannedIdent, Vec<P<Ty>>, Vec<P<Expr>>), | |
c34b1796 | 926 | /// A tuple (`(a, b, c ,d)`) |
7453a54e | 927 | Tup(Vec<P<Expr>>), |
c34b1796 | 928 | /// A binary operation (For example: `a + b`, `a * b`) |
7453a54e | 929 | Binary(BinOp, P<Expr>, P<Expr>), |
c34b1796 | 930 | /// A unary operation (For example: `!x`, `*x`) |
7453a54e | 931 | Unary(UnOp, P<Expr>), |
54a0048b | 932 | /// A literal (For example: `1`, `"foo"`) |
7453a54e | 933 | Lit(P<Lit>), |
c34b1796 | 934 | /// A cast (`foo as f64`) |
7453a54e SL |
935 | Cast(P<Expr>, P<Ty>), |
936 | Type(P<Expr>, P<Ty>), | |
c34b1796 AL |
937 | /// An `if` block, with an optional else block |
938 | /// | |
939 | /// `if expr { block } else { expr }` | |
7453a54e | 940 | If(P<Expr>, P<Block>, Option<P<Expr>>), |
c34b1796 AL |
941 | /// An `if let` expression with an optional else block |
942 | /// | |
943 | /// `if let pat = expr { block } else { expr }` | |
944 | /// | |
945 | /// This is desugared to a `match` expression. | |
7453a54e | 946 | IfLet(P<Pat>, P<Expr>, P<Block>, Option<P<Expr>>), |
c34b1796 AL |
947 | /// A while loop, with an optional label |
948 | /// | |
949 | /// `'label: while expr { block }` | |
3157f602 | 950 | While(P<Expr>, P<Block>, Option<SpannedIdent>), |
c34b1796 AL |
951 | /// A while-let loop, with an optional label |
952 | /// | |
953 | /// `'label: while let pat = expr { block }` | |
954 | /// | |
955 | /// This is desugared to a combination of `loop` and `match` expressions. | |
3157f602 | 956 | WhileLet(P<Pat>, P<Expr>, P<Block>, Option<SpannedIdent>), |
c34b1796 AL |
957 | /// A for loop, with an optional label |
958 | /// | |
959 | /// `'label: for pat in expr { block }` | |
960 | /// | |
961 | /// This is desugared to a combination of `loop` and `match` expressions. | |
3157f602 | 962 | ForLoop(P<Pat>, P<Expr>, P<Block>, Option<SpannedIdent>), |
c34b1796 AL |
963 | /// Conditionless loop (can be exited with break, continue, or return) |
964 | /// | |
965 | /// `'label: loop { block }` | |
3157f602 | 966 | Loop(P<Block>, Option<SpannedIdent>), |
b039eaaf | 967 | /// A `match` block. |
7453a54e | 968 | Match(P<Expr>, Vec<Arm>), |
476ff2be | 969 | /// A closure (for example, `move |a, b, c| a + b + c`) |
a7813a04 XL |
970 | /// |
971 | /// The final span is the span of the argument block `|...|` | |
476ff2be | 972 | Closure(CaptureBy, P<FnDecl>, P<Expr>, Span), |
c34b1796 | 973 | /// A block (`{ ... }`) |
7453a54e | 974 | Block(P<Block>), |
1a4d82fc | 975 | |
c34b1796 | 976 | /// An assignment (`a = foo()`) |
7453a54e | 977 | Assign(P<Expr>, P<Expr>), |
c34b1796 AL |
978 | /// An assignment with an operator |
979 | /// | |
980 | /// For example, `a += 1`. | |
7453a54e | 981 | AssignOp(BinOp, P<Expr>, P<Expr>), |
c34b1796 | 982 | /// Access of a named struct field (`obj.foo`) |
7453a54e | 983 | Field(P<Expr>, SpannedIdent), |
c34b1796 AL |
984 | /// Access of an unnamed field of a struct or tuple-struct |
985 | /// | |
986 | /// For example, `foo.0`. | |
7453a54e | 987 | TupField(P<Expr>, Spanned<usize>), |
c34b1796 | 988 | /// An indexing operation (`foo[2]`) |
7453a54e | 989 | Index(P<Expr>, P<Expr>), |
54a0048b SL |
990 | /// A range (`1..2`, `1..`, `..2`, `1...2`, `1...`, `...2`) |
991 | Range(Option<P<Expr>>, Option<P<Expr>>, RangeLimits), | |
1a4d82fc | 992 | |
c34b1796 AL |
993 | /// Variable reference, possibly containing `::` and/or type |
994 | /// parameters, e.g. foo::bar::<baz>. | |
995 | /// | |
996 | /// Optionally "qualified", | |
3157f602 | 997 | /// E.g. `<Vec<T> as SomeTrait>::SomeType`. |
7453a54e | 998 | Path(Option<QSelf>, Path), |
1a4d82fc | 999 | |
c34b1796 | 1000 | /// A referencing operation (`&a` or `&mut a`) |
7453a54e | 1001 | AddrOf(Mutability, P<Expr>), |
476ff2be SL |
1002 | /// A `break`, with an optional label to break, and an optional expression |
1003 | Break(Option<SpannedIdent>, Option<P<Expr>>), | |
c34b1796 | 1004 | /// A `continue`, with an optional label |
3157f602 | 1005 | Continue(Option<SpannedIdent>), |
c34b1796 | 1006 | /// A `return`, with an optional value to be returned |
7453a54e | 1007 | Ret(Option<P<Expr>>), |
1a4d82fc | 1008 | |
c34b1796 | 1009 | /// Output of the `asm!()` macro |
c30ab7b3 | 1010 | InlineAsm(P<InlineAsm>), |
1a4d82fc | 1011 | |
c34b1796 | 1012 | /// A macro invocation; pre-expansion |
7453a54e | 1013 | Mac(Mac), |
1a4d82fc JJ |
1014 | |
1015 | /// A struct literal expression. | |
c34b1796 AL |
1016 | /// |
1017 | /// For example, `Foo {x: 1, y: 2}`, or | |
1018 | /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`. | |
7453a54e | 1019 | Struct(Path, Vec<Field>, Option<P<Expr>>), |
1a4d82fc | 1020 | |
e9174d1e | 1021 | /// An array literal constructed from one repeated element. |
c34b1796 | 1022 | /// |
54a0048b | 1023 | /// For example, `[1; 5]`. The first expression is the element |
c34b1796 | 1024 | /// to be repeated; the second is the number of times to repeat it. |
7453a54e | 1025 | Repeat(P<Expr>, P<Expr>), |
1a4d82fc JJ |
1026 | |
1027 | /// No-op: used solely so we can pretty-print faithfully | |
7453a54e | 1028 | Paren(P<Expr>), |
54a0048b SL |
1029 | |
1030 | /// `expr?` | |
1031 | Try(P<Expr>), | |
1a4d82fc JJ |
1032 | } |
1033 | ||
c34b1796 AL |
1034 | /// The explicit Self type in a "qualified path". The actual |
1035 | /// path, including the trait and the associated item, is stored | |
1036 | /// separately. `position` represents the index of the associated | |
1037 | /// item qualified with this Self type. | |
1038 | /// | |
3157f602 | 1039 | /// ```rust,ignore |
92a42be0 SL |
1040 | /// <Vec<T> as a::b::Trait>::AssociatedItem |
1041 | /// ^~~~~ ~~~~~~~~~~~~~~^ | |
1042 | /// ty position = 3 | |
1a4d82fc | 1043 | /// |
92a42be0 SL |
1044 | /// <Vec<T>>::AssociatedItem |
1045 | /// ^~~~~ ^ | |
1046 | /// ty position = 0 | |
1047 | /// ``` | |
85aaf69f | 1048 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
c34b1796 AL |
1049 | pub struct QSelf { |
1050 | pub ty: P<Ty>, | |
1051 | pub position: usize | |
1a4d82fc JJ |
1052 | } |
1053 | ||
7453a54e | 1054 | /// A capture clause |
85aaf69f | 1055 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
7453a54e SL |
1056 | pub enum CaptureBy { |
1057 | Value, | |
1058 | Ref, | |
1a4d82fc JJ |
1059 | } |
1060 | ||
1a4d82fc | 1061 | pub type Mac = Spanned<Mac_>; |
223e47cc | 1062 | |
1a4d82fc JJ |
1063 | /// Represents a macro invocation. The Path indicates which macro |
1064 | /// is being invoked, and the vector of token-trees contains the source | |
1065 | /// of the macro invocation. | |
c34b1796 | 1066 | /// |
b039eaaf SL |
1067 | /// NB: the additional ident for a macro_rules-style macro is actually |
1068 | /// stored in the enclosing item. Oog. | |
85aaf69f | 1069 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
b039eaaf SL |
1070 | pub struct Mac_ { |
1071 | pub path: Path, | |
1072 | pub tts: Vec<TokenTree>, | |
1a4d82fc | 1073 | } |
223e47cc | 1074 | |
85aaf69f | 1075 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
1a4d82fc | 1076 | pub enum StrStyle { |
c34b1796 | 1077 | /// A regular string, like `"foo"` |
7453a54e | 1078 | Cooked, |
c34b1796 AL |
1079 | /// A raw string, like `r##"foo"##` |
1080 | /// | |
1081 | /// The uint is the number of `#` symbols used | |
7453a54e | 1082 | Raw(usize) |
223e47cc LB |
1083 | } |
1084 | ||
c34b1796 | 1085 | /// A literal |
7453a54e | 1086 | pub type Lit = Spanned<LitKind>; |
9346a6ac | 1087 | |
85aaf69f | 1088 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
1a4d82fc | 1089 | pub enum LitIntType { |
7453a54e SL |
1090 | Signed(IntTy), |
1091 | Unsigned(UintTy), | |
1092 | Unsuffixed, | |
223e47cc LB |
1093 | } |
1094 | ||
3157f602 XL |
1095 | /// Literal kind. |
1096 | /// | |
1097 | /// E.g. `"foo"`, `42`, `12.34` or `bool` | |
85aaf69f | 1098 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
7453a54e | 1099 | pub enum LitKind { |
c34b1796 | 1100 | /// A string literal (`"foo"`) |
476ff2be | 1101 | Str(Symbol, StrStyle), |
c34b1796 | 1102 | /// A byte string (`b"foo"`) |
7453a54e | 1103 | ByteStr(Rc<Vec<u8>>), |
c34b1796 | 1104 | /// A byte char (`b'f'`) |
7453a54e | 1105 | Byte(u8), |
c34b1796 | 1106 | /// A character literal (`'a'`) |
7453a54e | 1107 | Char(char), |
54a0048b | 1108 | /// An integer literal (`1`) |
7453a54e | 1109 | Int(u64, LitIntType), |
c34b1796 | 1110 | /// A float literal (`1f64` or `1E10f64`) |
476ff2be | 1111 | Float(Symbol, FloatTy), |
c34b1796 | 1112 | /// A float literal without a suffix (`1.0 or 1.0E10`) |
476ff2be | 1113 | FloatUnsuffixed(Symbol), |
c34b1796 | 1114 | /// A boolean literal |
7453a54e | 1115 | Bool(bool), |
223e47cc LB |
1116 | } |
1117 | ||
7453a54e | 1118 | impl LitKind { |
9cc50fc6 SL |
1119 | /// Returns true if this literal is a string and false otherwise. |
1120 | pub fn is_str(&self) -> bool { | |
1121 | match *self { | |
7453a54e | 1122 | LitKind::Str(..) => true, |
9cc50fc6 SL |
1123 | _ => false, |
1124 | } | |
1125 | } | |
9e0c209e SL |
1126 | |
1127 | /// Returns true if this literal has no suffix. Note: this will return true | |
1128 | /// for literals with prefixes such as raw strings and byte strings. | |
1129 | pub fn is_unsuffixed(&self) -> bool { | |
1130 | match *self { | |
1131 | // unsuffixed variants | |
1132 | LitKind::Str(..) => true, | |
1133 | LitKind::ByteStr(..) => true, | |
1134 | LitKind::Byte(..) => true, | |
1135 | LitKind::Char(..) => true, | |
1136 | LitKind::Int(_, LitIntType::Unsuffixed) => true, | |
1137 | LitKind::FloatUnsuffixed(..) => true, | |
1138 | LitKind::Bool(..) => true, | |
1139 | // suffixed variants | |
1140 | LitKind::Int(_, LitIntType::Signed(..)) => false, | |
1141 | LitKind::Int(_, LitIntType::Unsigned(..)) => false, | |
1142 | LitKind::Float(..) => false, | |
1143 | } | |
1144 | } | |
1145 | ||
1146 | /// Returns true if this literal has a suffix. | |
1147 | pub fn is_suffixed(&self) -> bool { | |
1148 | !self.is_unsuffixed() | |
1149 | } | |
9cc50fc6 SL |
1150 | } |
1151 | ||
223e47cc LB |
1152 | // NB: If you change this, you'll probably want to change the corresponding |
1153 | // type structure in middle/ty.rs as well. | |
85aaf69f | 1154 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1155 | pub struct MutTy { |
1156 | pub ty: P<Ty>, | |
1157 | pub mutbl: Mutability, | |
1158 | } | |
1159 | ||
c34b1796 AL |
1160 | /// Represents a method's signature in a trait declaration, |
1161 | /// or in an implementation. | |
85aaf69f | 1162 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
c34b1796 | 1163 | pub struct MethodSig { |
1a4d82fc | 1164 | pub unsafety: Unsafety, |
9e0c209e | 1165 | pub constness: Spanned<Constness>, |
1a4d82fc JJ |
1166 | pub abi: Abi, |
1167 | pub decl: P<FnDecl>, | |
1168 | pub generics: Generics, | |
1a4d82fc JJ |
1169 | } |
1170 | ||
54a0048b SL |
1171 | /// Represents an item declaration within a trait declaration, |
1172 | /// possibly including a default implementation. A trait item is | |
1173 | /// either required (meaning it doesn't have an implementation, just a | |
1174 | /// signature) or provided (meaning it has a default implementation). | |
85aaf69f | 1175 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
c34b1796 AL |
1176 | pub struct TraitItem { |
1177 | pub id: NodeId, | |
1178 | pub ident: Ident, | |
1179 | pub attrs: Vec<Attribute>, | |
7453a54e | 1180 | pub node: TraitItemKind, |
c34b1796 | 1181 | pub span: Span, |
1a4d82fc JJ |
1182 | } |
1183 | ||
85aaf69f | 1184 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
7453a54e SL |
1185 | pub enum TraitItemKind { |
1186 | Const(P<Ty>, Option<P<Expr>>), | |
1187 | Method(MethodSig, Option<P<Block>>), | |
1188 | Type(TyParamBounds, Option<P<Ty>>), | |
3157f602 | 1189 | Macro(Mac), |
1a4d82fc JJ |
1190 | } |
1191 | ||
85aaf69f | 1192 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
c34b1796 | 1193 | pub struct ImplItem { |
1a4d82fc | 1194 | pub id: NodeId, |
1a4d82fc JJ |
1195 | pub ident: Ident, |
1196 | pub vis: Visibility, | |
54a0048b | 1197 | pub defaultness: Defaultness, |
1a4d82fc | 1198 | pub attrs: Vec<Attribute>, |
92a42be0 | 1199 | pub node: ImplItemKind, |
c34b1796 AL |
1200 | pub span: Span, |
1201 | } | |
1202 | ||
1203 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] | |
92a42be0 SL |
1204 | pub enum ImplItemKind { |
1205 | Const(P<Ty>, P<Expr>), | |
1206 | Method(MethodSig, P<Block>), | |
1207 | Type(P<Ty>), | |
1208 | Macro(Mac), | |
1a4d82fc JJ |
1209 | } |
1210 | ||
c34b1796 | 1211 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] |
1a4d82fc | 1212 | pub enum IntTy { |
7453a54e SL |
1213 | Is, |
1214 | I8, | |
1215 | I16, | |
1216 | I32, | |
1217 | I64, | |
1a4d82fc JJ |
1218 | } |
1219 | ||
85aaf69f | 1220 | impl fmt::Debug for IntTy { |
1a4d82fc | 1221 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
85aaf69f | 1222 | fmt::Display::fmt(self, f) |
1a4d82fc | 1223 | } |
223e47cc LB |
1224 | } |
1225 | ||
85aaf69f | 1226 | impl fmt::Display for IntTy { |
1a4d82fc | 1227 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
9cc50fc6 | 1228 | write!(f, "{}", self.ty_to_string()) |
1a4d82fc JJ |
1229 | } |
1230 | } | |
223e47cc | 1231 | |
1a4d82fc | 1232 | impl IntTy { |
9cc50fc6 SL |
1233 | pub fn ty_to_string(&self) -> &'static str { |
1234 | match *self { | |
7453a54e SL |
1235 | IntTy::Is => "isize", |
1236 | IntTy::I8 => "i8", | |
1237 | IntTy::I16 => "i16", | |
1238 | IntTy::I32 => "i32", | |
1239 | IntTy::I64 => "i64" | |
9cc50fc6 SL |
1240 | } |
1241 | } | |
1242 | ||
1243 | pub fn val_to_string(&self, val: i64) -> String { | |
1244 | // cast to a u64 so we can correctly print INT64_MIN. All integral types | |
1245 | // are parsed as u64, so we wouldn't want to print an extra negative | |
1246 | // sign. | |
1247 | format!("{}{}", val as u64, self.ty_to_string()) | |
1248 | } | |
1249 | ||
1250 | pub fn ty_max(&self) -> u64 { | |
1251 | match *self { | |
7453a54e SL |
1252 | IntTy::I8 => 0x80, |
1253 | IntTy::I16 => 0x8000, | |
1254 | IntTy::Is | IntTy::I32 => 0x80000000, // FIXME: actually ni about Is | |
1255 | IntTy::I64 => 0x8000000000000000 | |
9cc50fc6 SL |
1256 | } |
1257 | } | |
1258 | ||
e9174d1e SL |
1259 | pub fn bit_width(&self) -> Option<usize> { |
1260 | Some(match *self { | |
7453a54e SL |
1261 | IntTy::Is => return None, |
1262 | IntTy::I8 => 8, | |
1263 | IntTy::I16 => 16, | |
1264 | IntTy::I32 => 32, | |
1265 | IntTy::I64 => 64, | |
e9174d1e | 1266 | }) |
1a4d82fc | 1267 | } |
223e47cc LB |
1268 | } |
1269 | ||
c34b1796 | 1270 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] |
1a4d82fc | 1271 | pub enum UintTy { |
7453a54e SL |
1272 | Us, |
1273 | U8, | |
1274 | U16, | |
1275 | U32, | |
1276 | U64, | |
1a4d82fc JJ |
1277 | } |
1278 | ||
1a4d82fc | 1279 | impl UintTy { |
9cc50fc6 SL |
1280 | pub fn ty_to_string(&self) -> &'static str { |
1281 | match *self { | |
7453a54e SL |
1282 | UintTy::Us => "usize", |
1283 | UintTy::U8 => "u8", | |
1284 | UintTy::U16 => "u16", | |
1285 | UintTy::U32 => "u32", | |
1286 | UintTy::U64 => "u64" | |
9cc50fc6 SL |
1287 | } |
1288 | } | |
1289 | ||
1290 | pub fn val_to_string(&self, val: u64) -> String { | |
1291 | format!("{}{}", val, self.ty_to_string()) | |
1292 | } | |
1293 | ||
1294 | pub fn ty_max(&self) -> u64 { | |
1295 | match *self { | |
7453a54e SL |
1296 | UintTy::U8 => 0xff, |
1297 | UintTy::U16 => 0xffff, | |
1298 | UintTy::Us | UintTy::U32 => 0xffffffff, // FIXME: actually ni about Us | |
1299 | UintTy::U64 => 0xffffffffffffffff | |
9cc50fc6 SL |
1300 | } |
1301 | } | |
1302 | ||
e9174d1e SL |
1303 | pub fn bit_width(&self) -> Option<usize> { |
1304 | Some(match *self { | |
7453a54e SL |
1305 | UintTy::Us => return None, |
1306 | UintTy::U8 => 8, | |
1307 | UintTy::U16 => 16, | |
1308 | UintTy::U32 => 32, | |
1309 | UintTy::U64 => 64, | |
e9174d1e | 1310 | }) |
1a4d82fc JJ |
1311 | } |
1312 | } | |
223e47cc | 1313 | |
85aaf69f | 1314 | impl fmt::Debug for UintTy { |
1a4d82fc | 1315 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
85aaf69f | 1316 | fmt::Display::fmt(self, f) |
223e47cc LB |
1317 | } |
1318 | } | |
1319 | ||
85aaf69f | 1320 | impl fmt::Display for UintTy { |
1a4d82fc | 1321 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
9cc50fc6 | 1322 | write!(f, "{}", self.ty_to_string()) |
1a4d82fc JJ |
1323 | } |
1324 | } | |
1325 | ||
1326 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] | |
1327 | pub enum FloatTy { | |
7453a54e SL |
1328 | F32, |
1329 | F64, | |
1a4d82fc | 1330 | } |
223e47cc | 1331 | |
85aaf69f | 1332 | impl fmt::Debug for FloatTy { |
1a4d82fc | 1333 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
85aaf69f | 1334 | fmt::Display::fmt(self, f) |
223e47cc LB |
1335 | } |
1336 | } | |
1337 | ||
85aaf69f | 1338 | impl fmt::Display for FloatTy { |
1a4d82fc | 1339 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
9cc50fc6 | 1340 | write!(f, "{}", self.ty_to_string()) |
1a4d82fc JJ |
1341 | } |
1342 | } | |
223e47cc | 1343 | |
1a4d82fc | 1344 | impl FloatTy { |
9cc50fc6 SL |
1345 | pub fn ty_to_string(&self) -> &'static str { |
1346 | match *self { | |
7453a54e SL |
1347 | FloatTy::F32 => "f32", |
1348 | FloatTy::F64 => "f64", | |
9cc50fc6 SL |
1349 | } |
1350 | } | |
1351 | ||
e9174d1e | 1352 | pub fn bit_width(&self) -> usize { |
1a4d82fc | 1353 | match *self { |
7453a54e SL |
1354 | FloatTy::F32 => 32, |
1355 | FloatTy::F64 => 64, | |
1a4d82fc | 1356 | } |
223e47cc LB |
1357 | } |
1358 | } | |
1359 | ||
1a4d82fc | 1360 | // Bind a type to an associated type: `A=Foo`. |
85aaf69f | 1361 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1362 | pub struct TypeBinding { |
1363 | pub id: NodeId, | |
1364 | pub ident: Ident, | |
1365 | pub ty: P<Ty>, | |
1366 | pub span: Span, | |
1367 | } | |
1368 | ||
62682a34 | 1369 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] |
223e47cc | 1370 | pub struct Ty { |
1a4d82fc | 1371 | pub id: NodeId, |
7453a54e | 1372 | pub node: TyKind, |
1a4d82fc | 1373 | pub span: Span, |
223e47cc LB |
1374 | } |
1375 | ||
62682a34 SL |
1376 | impl fmt::Debug for Ty { |
1377 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
1378 | write!(f, "type({})", pprust::ty_to_string(self)) | |
1379 | } | |
1380 | } | |
1381 | ||
85aaf69f | 1382 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1383 | pub struct BareFnTy { |
1384 | pub unsafety: Unsafety, | |
1385 | pub abi: Abi, | |
1386 | pub lifetimes: Vec<LifetimeDef>, | |
1387 | pub decl: P<FnDecl> | |
1388 | } | |
1389 | ||
1a4d82fc | 1390 | /// The different kinds of types recognized by the compiler |
3157f602 | 1391 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
7453a54e | 1392 | pub enum TyKind { |
c30ab7b3 SL |
1393 | /// A variable-length slice (`[T]`) |
1394 | Slice(P<Ty>), | |
c34b1796 | 1395 | /// A fixed length array (`[T; n]`) |
c30ab7b3 | 1396 | Array(P<Ty>, P<Expr>), |
1a4d82fc | 1397 | /// A raw pointer (`*const T` or `*mut T`) |
7453a54e | 1398 | Ptr(MutTy), |
1a4d82fc | 1399 | /// A reference (`&'a T` or `&'a mut T`) |
7453a54e | 1400 | Rptr(Option<Lifetime>, MutTy), |
85aaf69f | 1401 | /// A bare function (e.g. `fn(usize) -> bool`) |
7453a54e | 1402 | BareFn(P<BareFnTy>), |
5bcae85e SL |
1403 | /// The never type (`!`) |
1404 | Never, | |
1a4d82fc | 1405 | /// A tuple (`(A, B, C, D,...)`) |
7453a54e | 1406 | Tup(Vec<P<Ty>> ), |
c34b1796 AL |
1407 | /// A path (`module::module::...::Type`), optionally |
1408 | /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`. | |
1a4d82fc JJ |
1409 | /// |
1410 | /// Type parameters are stored in the Path itself | |
7453a54e | 1411 | Path(Option<QSelf>, Path), |
1a4d82fc | 1412 | /// Something like `A+B`. Note that `B` must always be a path. |
7453a54e | 1413 | ObjectSum(P<Ty>, TyParamBounds), |
1a4d82fc | 1414 | /// A type like `for<'a> Foo<&'a Bar>` |
7453a54e | 1415 | PolyTraitRef(TyParamBounds), |
5bcae85e SL |
1416 | /// An `impl TraitA+TraitB` type. |
1417 | ImplTrait(TyParamBounds), | |
1a4d82fc | 1418 | /// No-op; kept solely so that we can pretty-print faithfully |
7453a54e | 1419 | Paren(P<Ty>), |
1a4d82fc | 1420 | /// Unused for now |
7453a54e SL |
1421 | Typeof(P<Expr>), |
1422 | /// TyKind::Infer means the type should be inferred instead of it having been | |
1a4d82fc | 1423 | /// specified. This can appear anywhere in a type. |
7453a54e | 1424 | Infer, |
3157f602 XL |
1425 | /// Inferred type of a `self` or `&self` argument in a method. |
1426 | ImplicitSelf, | |
e9174d1e | 1427 | // A macro in the type position. |
7453a54e | 1428 | Mac(Mac), |
1a4d82fc JJ |
1429 | } |
1430 | ||
3157f602 XL |
1431 | /// Inline assembly dialect. |
1432 | /// | |
1433 | /// E.g. `"intel"` as in `asm!("mov eax, 2" : "={eax}"(result) : : : "intel")`` | |
85aaf69f | 1434 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
1a4d82fc | 1435 | pub enum AsmDialect { |
b039eaaf SL |
1436 | Att, |
1437 | Intel, | |
1a4d82fc JJ |
1438 | } |
1439 | ||
3157f602 XL |
1440 | /// Inline assembly. |
1441 | /// | |
1442 | /// E.g. `"={eax}"(result)` as in `asm!("mov eax, 2" : "={eax}"(result) : : : "intel")`` | |
9cc50fc6 SL |
1443 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1444 | pub struct InlineAsmOutput { | |
476ff2be | 1445 | pub constraint: Symbol, |
9cc50fc6 SL |
1446 | pub expr: P<Expr>, |
1447 | pub is_rw: bool, | |
1448 | pub is_indirect: bool, | |
1449 | } | |
1450 | ||
3157f602 XL |
1451 | /// Inline assembly. |
1452 | /// | |
1453 | /// E.g. `asm!("NOP");` | |
85aaf69f | 1454 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc | 1455 | pub struct InlineAsm { |
476ff2be | 1456 | pub asm: Symbol, |
1a4d82fc | 1457 | pub asm_str_style: StrStyle, |
9cc50fc6 | 1458 | pub outputs: Vec<InlineAsmOutput>, |
476ff2be SL |
1459 | pub inputs: Vec<(Symbol, P<Expr>)>, |
1460 | pub clobbers: Vec<Symbol>, | |
1a4d82fc JJ |
1461 | pub volatile: bool, |
1462 | pub alignstack: bool, | |
1463 | pub dialect: AsmDialect, | |
1464 | pub expn_id: ExpnId, | |
1465 | } | |
1466 | ||
3157f602 XL |
1467 | /// An argument in a function header. |
1468 | /// | |
1469 | /// E.g. `bar: usize` as in `fn foo(bar: usize)` | |
85aaf69f | 1470 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1471 | pub struct Arg { |
1472 | pub ty: P<Ty>, | |
1473 | pub pat: P<Pat>, | |
1474 | pub id: NodeId, | |
1475 | } | |
1476 | ||
3157f602 XL |
1477 | /// Alternative representation for `Arg`s describing `self` parameter of methods. |
1478 | /// | |
1479 | /// E.g. `&mut self` as in `fn foo(&mut self)` | |
a7813a04 XL |
1480 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1481 | pub enum SelfKind { | |
a7813a04 | 1482 | /// `self`, `mut self` |
3157f602 | 1483 | Value(Mutability), |
a7813a04 | 1484 | /// `&'lt self`, `&'lt mut self` |
3157f602 | 1485 | Region(Option<Lifetime>, Mutability), |
a7813a04 | 1486 | /// `self: TYPE`, `mut self: TYPE` |
3157f602 | 1487 | Explicit(P<Ty>, Mutability), |
a7813a04 XL |
1488 | } |
1489 | ||
1490 | pub type ExplicitSelf = Spanned<SelfKind>; | |
1491 | ||
1a4d82fc | 1492 | impl Arg { |
a7813a04 | 1493 | pub fn to_self(&self) -> Option<ExplicitSelf> { |
3157f602 | 1494 | if let PatKind::Ident(BindingMode::ByValue(mutbl), ident, _) = self.pat.node { |
a7813a04 XL |
1495 | if ident.node.name == keywords::SelfValue.name() { |
1496 | return match self.ty.node { | |
3157f602 XL |
1497 | TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))), |
1498 | TyKind::Rptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyKind::ImplicitSelf => { | |
1499 | Some(respan(self.pat.span, SelfKind::Region(lt, mutbl))) | |
a7813a04 XL |
1500 | } |
1501 | _ => Some(respan(mk_sp(self.pat.span.lo, self.ty.span.hi), | |
3157f602 | 1502 | SelfKind::Explicit(self.ty.clone(), mutbl))), |
a7813a04 XL |
1503 | } |
1504 | } | |
1505 | } | |
1506 | None | |
1507 | } | |
1508 | ||
3157f602 XL |
1509 | pub fn is_self(&self) -> bool { |
1510 | if let PatKind::Ident(_, ident, _) = self.pat.node { | |
1511 | ident.node.name == keywords::SelfValue.name() | |
1512 | } else { | |
1513 | false | |
1514 | } | |
1515 | } | |
1516 | ||
1517 | pub fn from_self(eself: ExplicitSelf, eself_ident: SpannedIdent) -> Arg { | |
a7813a04 XL |
1518 | let infer_ty = P(Ty { |
1519 | id: DUMMY_NODE_ID, | |
3157f602 | 1520 | node: TyKind::ImplicitSelf, |
a7813a04 XL |
1521 | span: DUMMY_SP, |
1522 | }); | |
3157f602 XL |
1523 | let arg = |mutbl, ty, span| Arg { |
1524 | pat: P(Pat { | |
1525 | id: DUMMY_NODE_ID, | |
1526 | node: PatKind::Ident(BindingMode::ByValue(mutbl), eself_ident, None), | |
1527 | span: span, | |
1528 | }), | |
a7813a04 XL |
1529 | ty: ty, |
1530 | id: DUMMY_NODE_ID, | |
1531 | }; | |
1532 | match eself.node { | |
3157f602 XL |
1533 | SelfKind::Explicit(ty, mutbl) => { |
1534 | arg(mutbl, ty, mk_sp(eself.span.lo, eself_ident.span.hi)) | |
1535 | } | |
1536 | SelfKind::Value(mutbl) => arg(mutbl, infer_ty, eself.span), | |
1537 | SelfKind::Region(lt, mutbl) => arg(Mutability::Immutable, P(Ty { | |
a7813a04 XL |
1538 | id: DUMMY_NODE_ID, |
1539 | node: TyKind::Rptr(lt, MutTy { ty: infer_ty, mutbl: mutbl }), | |
1540 | span: DUMMY_SP, | |
1541 | }), eself.span), | |
1542 | } | |
1543 | } | |
223e47cc LB |
1544 | } |
1545 | ||
3157f602 XL |
1546 | /// Header (not the body) of a function declaration. |
1547 | /// | |
1548 | /// E.g. `fn foo(bar: baz)` | |
85aaf69f | 1549 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1550 | pub struct FnDecl { |
1551 | pub inputs: Vec<Arg>, | |
1552 | pub output: FunctionRetTy, | |
1553 | pub variadic: bool | |
1554 | } | |
1555 | ||
3157f602 XL |
1556 | impl FnDecl { |
1557 | pub fn get_self(&self) -> Option<ExplicitSelf> { | |
1558 | self.inputs.get(0).and_then(Arg::to_self) | |
1559 | } | |
1560 | pub fn has_self(&self) -> bool { | |
1561 | self.inputs.get(0).map(Arg::is_self).unwrap_or(false) | |
1562 | } | |
1563 | } | |
1564 | ||
85aaf69f | 1565 | #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1566 | pub enum Unsafety { |
1567 | Unsafe, | |
1568 | Normal, | |
1569 | } | |
1570 | ||
62682a34 SL |
1571 | #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1572 | pub enum Constness { | |
1573 | Const, | |
1574 | NotConst, | |
1575 | } | |
1576 | ||
54a0048b SL |
1577 | #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1578 | pub enum Defaultness { | |
1579 | Default, | |
1580 | Final, | |
1581 | } | |
1582 | ||
85aaf69f | 1583 | impl fmt::Display for Unsafety { |
1a4d82fc | 1584 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
85aaf69f | 1585 | fmt::Display::fmt(match *self { |
1a4d82fc JJ |
1586 | Unsafety::Normal => "normal", |
1587 | Unsafety::Unsafe => "unsafe", | |
1588 | }, f) | |
1589 | } | |
1590 | } | |
1591 | ||
1592 | #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] | |
1593 | pub enum ImplPolarity { | |
c34b1796 | 1594 | /// `impl Trait for Type` |
1a4d82fc | 1595 | Positive, |
c34b1796 | 1596 | /// `impl !Trait for Type` |
1a4d82fc JJ |
1597 | Negative, |
1598 | } | |
1599 | ||
85aaf69f | 1600 | impl fmt::Debug for ImplPolarity { |
1a4d82fc | 1601 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
223e47cc | 1602 | match *self { |
1a4d82fc JJ |
1603 | ImplPolarity::Positive => "positive".fmt(f), |
1604 | ImplPolarity::Negative => "negative".fmt(f), | |
223e47cc LB |
1605 | } |
1606 | } | |
1607 | } | |
1608 | ||
1a4d82fc | 1609 | |
85aaf69f | 1610 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc | 1611 | pub enum FunctionRetTy { |
c34b1796 AL |
1612 | /// Return type is not specified. |
1613 | /// | |
1614 | /// Functions default to `()` and | |
85aaf69f SL |
1615 | /// closures default to inference. Span points to where return |
1616 | /// type would be inserted. | |
7453a54e | 1617 | Default(Span), |
1a4d82fc | 1618 | /// Everything else |
7453a54e | 1619 | Ty(P<Ty>), |
1a4d82fc JJ |
1620 | } |
1621 | ||
1622 | impl FunctionRetTy { | |
1623 | pub fn span(&self) -> Span { | |
1624 | match *self { | |
7453a54e SL |
1625 | FunctionRetTy::Default(span) => span, |
1626 | FunctionRetTy::Ty(ref ty) => ty.span, | |
1a4d82fc JJ |
1627 | } |
1628 | } | |
223e47cc LB |
1629 | } |
1630 | ||
3157f602 XL |
1631 | /// Module declaration. |
1632 | /// | |
1633 | /// E.g. `mod foo;` or `mod foo { .. }` | |
85aaf69f | 1634 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1635 | pub struct Mod { |
1636 | /// A span from the first token past `{` to the last token until `}`. | |
1637 | /// For `mod foo;`, the inner span ranges from the first token | |
1638 | /// to the last token in the external file. | |
1639 | pub inner: Span, | |
1a4d82fc JJ |
1640 | pub items: Vec<P<Item>>, |
1641 | } | |
223e47cc | 1642 | |
3157f602 XL |
1643 | /// Foreign module declaration. |
1644 | /// | |
1645 | /// E.g. `extern { .. }` or `extern C { .. }` | |
85aaf69f | 1646 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1647 | pub struct ForeignMod { |
1648 | pub abi: Abi, | |
7453a54e | 1649 | pub items: Vec<ForeignItem>, |
223e47cc LB |
1650 | } |
1651 | ||
85aaf69f | 1652 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc | 1653 | pub struct EnumDef { |
7453a54e | 1654 | pub variants: Vec<Variant>, |
223e47cc LB |
1655 | } |
1656 | ||
85aaf69f | 1657 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1658 | pub struct Variant_ { |
1659 | pub name: Ident, | |
1660 | pub attrs: Vec<Attribute>, | |
b039eaaf | 1661 | pub data: VariantData, |
3157f602 | 1662 | /// Explicit discriminant, e.g. `Foo = 1` |
1a4d82fc | 1663 | pub disr_expr: Option<P<Expr>>, |
223e47cc LB |
1664 | } |
1665 | ||
1a4d82fc | 1666 | pub type Variant = Spanned<Variant_>; |
223e47cc | 1667 | |
85aaf69f | 1668 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
9e0c209e SL |
1669 | pub struct PathListItem_ { |
1670 | pub name: Ident, | |
1671 | /// renamed in list, e.g. `use foo::{bar as baz};` | |
1672 | pub rename: Option<Ident>, | |
1673 | pub id: NodeId, | |
1a4d82fc | 1674 | } |
223e47cc | 1675 | |
9e0c209e | 1676 | pub type PathListItem = Spanned<PathListItem_>; |
223e47cc | 1677 | |
1a4d82fc | 1678 | pub type ViewPath = Spanned<ViewPath_>; |
223e47cc | 1679 | |
85aaf69f | 1680 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc | 1681 | pub enum ViewPath_ { |
223e47cc | 1682 | |
1a4d82fc JJ |
1683 | /// `foo::bar::baz as quux` |
1684 | /// | |
1685 | /// or just | |
1686 | /// | |
1687 | /// `foo::bar::baz` (with `as baz` implicitly on the right) | |
85aaf69f | 1688 | ViewPathSimple(Ident, Path), |
223e47cc | 1689 | |
1a4d82fc | 1690 | /// `foo::bar::*` |
85aaf69f | 1691 | ViewPathGlob(Path), |
1a4d82fc JJ |
1692 | |
1693 | /// `foo::bar::{a,b,c}` | |
85aaf69f | 1694 | ViewPathList(Path, Vec<PathListItem>) |
223e47cc LB |
1695 | } |
1696 | ||
3157f602 XL |
1697 | impl ViewPath_ { |
1698 | pub fn path(&self) -> &Path { | |
1699 | match *self { | |
1700 | ViewPathSimple(_, ref path) | | |
1701 | ViewPathGlob (ref path) | | |
1702 | ViewPathList(ref path, _) => path | |
1703 | } | |
1704 | } | |
1705 | } | |
1706 | ||
223e47cc | 1707 | |
1a4d82fc JJ |
1708 | /// Distinguishes between Attributes that decorate items and Attributes that |
1709 | /// are contained as statements within items. These two cases need to be | |
1710 | /// distinguished for pretty-printing. | |
85aaf69f | 1711 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
1a4d82fc | 1712 | pub enum AttrStyle { |
b039eaaf SL |
1713 | Outer, |
1714 | Inner, | |
1a4d82fc | 1715 | } |
223e47cc | 1716 | |
85aaf69f SL |
1717 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] |
1718 | pub struct AttrId(pub usize); | |
1a4d82fc | 1719 | |
476ff2be | 1720 | /// Meta-data associated with an item |
1a4d82fc | 1721 | /// Doc-comments are promoted to attributes that have is_sugared_doc = true |
85aaf69f | 1722 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
476ff2be | 1723 | pub struct Attribute { |
1a4d82fc JJ |
1724 | pub id: AttrId, |
1725 | pub style: AttrStyle, | |
476ff2be | 1726 | pub value: MetaItem, |
1a4d82fc | 1727 | pub is_sugared_doc: bool, |
476ff2be | 1728 | pub span: Span, |
223e47cc LB |
1729 | } |
1730 | ||
1a4d82fc | 1731 | /// TraitRef's appear in impls. |
c34b1796 | 1732 | /// |
1a4d82fc JJ |
1733 | /// resolve maps each TraitRef's ref_id to its defining trait; that's all |
1734 | /// that the ref_id is for. The impl_id maps to the "self type" of this impl. | |
7453a54e | 1735 | /// If this impl is an ItemKind::Impl, the impl_id is redundant (it could be the |
1a4d82fc | 1736 | /// same as the impl's node id). |
85aaf69f | 1737 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1738 | pub struct TraitRef { |
1739 | pub path: Path, | |
1740 | pub ref_id: NodeId, | |
223e47cc LB |
1741 | } |
1742 | ||
85aaf69f | 1743 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1744 | pub struct PolyTraitRef { |
1745 | /// The `'a` in `<'a> Foo<&'a T>` | |
1746 | pub bound_lifetimes: Vec<LifetimeDef>, | |
223e47cc | 1747 | |
1a4d82fc JJ |
1748 | /// The `Foo<&'a T>` in `<'a> Foo<&'a T>` |
1749 | pub trait_ref: TraitRef, | |
85aaf69f SL |
1750 | |
1751 | pub span: Span, | |
1a4d82fc JJ |
1752 | } |
1753 | ||
54a0048b | 1754 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1755 | pub enum Visibility { |
1756 | Public, | |
a7813a04 | 1757 | Crate(Span), |
54a0048b | 1758 | Restricted { path: P<Path>, id: NodeId }, |
1a4d82fc JJ |
1759 | Inherited, |
1760 | } | |
1761 | ||
3157f602 XL |
1762 | /// Field of a struct. |
1763 | /// | |
1764 | /// E.g. `bar: usize` as in `struct Foo { bar: usize }` | |
85aaf69f | 1765 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
54a0048b SL |
1766 | pub struct StructField { |
1767 | pub span: Span, | |
1768 | pub ident: Option<Ident>, | |
1769 | pub vis: Visibility, | |
1a4d82fc JJ |
1770 | pub id: NodeId, |
1771 | pub ty: P<Ty>, | |
1772 | pub attrs: Vec<Attribute>, | |
1773 | } | |
1774 | ||
b039eaaf SL |
1775 | /// Fields and Ids of enum variants and structs |
1776 | /// | |
1777 | /// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all | |
1778 | /// variant kinds) and an Id of the variant's constructor (not relevant for `Struct`-variants). | |
1779 | /// One shared Id can be successfully used for these two purposes. | |
1780 | /// Id of the whole enum lives in `Item`. | |
1781 | /// | |
1782 | /// For structs: `NodeId` represents an Id of the structure's constructor, so it is not actually | |
1783 | /// used for `Struct`-structs (but still presents). Structures don't have an analogue of "Id of | |
1784 | /// the variant itself" from enum variants. | |
1785 | /// Id of the whole struct lives in `Item`. | |
85aaf69f | 1786 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
b039eaaf | 1787 | pub enum VariantData { |
3157f602 XL |
1788 | /// Struct variant. |
1789 | /// | |
1790 | /// E.g. `Bar { .. }` as in `enum Foo { Bar { .. } }` | |
b039eaaf | 1791 | Struct(Vec<StructField>, NodeId), |
3157f602 XL |
1792 | /// Tuple variant. |
1793 | /// | |
1794 | /// E.g. `Bar(..)` as in `enum Foo { Bar(..) }` | |
b039eaaf | 1795 | Tuple(Vec<StructField>, NodeId), |
3157f602 XL |
1796 | /// Unit variant. |
1797 | /// | |
1798 | /// E.g. `Bar = ..` as in `enum Foo { Bar = .. }` | |
b039eaaf SL |
1799 | Unit(NodeId), |
1800 | } | |
1801 | ||
1802 | impl VariantData { | |
1803 | pub fn fields(&self) -> &[StructField] { | |
1804 | match *self { | |
1805 | VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => fields, | |
1806 | _ => &[], | |
1807 | } | |
1808 | } | |
1809 | pub fn id(&self) -> NodeId { | |
1810 | match *self { | |
1811 | VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id | |
1812 | } | |
1813 | } | |
1814 | pub fn is_struct(&self) -> bool { | |
1815 | if let VariantData::Struct(..) = *self { true } else { false } | |
1816 | } | |
1817 | pub fn is_tuple(&self) -> bool { | |
1818 | if let VariantData::Tuple(..) = *self { true } else { false } | |
1819 | } | |
1820 | pub fn is_unit(&self) -> bool { | |
1821 | if let VariantData::Unit(..) = *self { true } else { false } | |
1822 | } | |
223e47cc LB |
1823 | } |
1824 | ||
1825 | /* | |
1826 | FIXME (#3300): Should allow items to be anonymous. Right now | |
1827 | we just use dummy names for anon items. | |
1828 | */ | |
c34b1796 AL |
1829 | /// An item |
1830 | /// | |
1831 | /// The name might be a dummy name in case of anonymous items | |
85aaf69f | 1832 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1833 | pub struct Item { |
1834 | pub ident: Ident, | |
1835 | pub attrs: Vec<Attribute>, | |
1836 | pub id: NodeId, | |
7453a54e | 1837 | pub node: ItemKind, |
1a4d82fc JJ |
1838 | pub vis: Visibility, |
1839 | pub span: Span, | |
1840 | } | |
1841 | ||
85aaf69f | 1842 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
7453a54e | 1843 | pub enum ItemKind { |
3157f602 | 1844 | /// An`extern crate` item, with optional original crate name. |
c34b1796 | 1845 | /// |
3157f602 | 1846 | /// E.g. `extern crate foo` or `extern crate foo_bar as foo` |
7453a54e | 1847 | ExternCrate(Option<Name>), |
3157f602 XL |
1848 | /// A use declaration (`use` or `pub use`) item. |
1849 | /// | |
1850 | /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;` | |
7453a54e | 1851 | Use(P<ViewPath>), |
3157f602 XL |
1852 | /// A static item (`static` or `pub static`). |
1853 | /// | |
1854 | /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";` | |
7453a54e | 1855 | Static(P<Ty>, Mutability, P<Expr>), |
3157f602 XL |
1856 | /// A constant item (`const` or `pub const`). |
1857 | /// | |
1858 | /// E.g. `const FOO: i32 = 42;` | |
7453a54e | 1859 | Const(P<Ty>, P<Expr>), |
3157f602 XL |
1860 | /// A function declaration (`fn` or `pub fn`). |
1861 | /// | |
1862 | /// E.g. `fn foo(bar: usize) -> usize { .. }` | |
9e0c209e | 1863 | Fn(P<FnDecl>, Unsafety, Spanned<Constness>, Abi, Generics, P<Block>), |
3157f602 XL |
1864 | /// A module declaration (`mod` or `pub mod`). |
1865 | /// | |
1866 | /// E.g. `mod foo;` or `mod foo { .. }` | |
7453a54e | 1867 | Mod(Mod), |
3157f602 XL |
1868 | /// An external module (`extern` or `pub extern`). |
1869 | /// | |
1870 | /// E.g. `extern {}` or `extern "C" {}` | |
7453a54e | 1871 | ForeignMod(ForeignMod), |
3157f602 XL |
1872 | /// A type alias (`type` or `pub type`). |
1873 | /// | |
1874 | /// E.g. `type Foo = Bar<u8>;` | |
7453a54e | 1875 | Ty(P<Ty>, Generics), |
3157f602 XL |
1876 | /// An enum definition (`enum` or `pub enum`). |
1877 | /// | |
1878 | /// E.g. `enum Foo<A, B> { C<A>, D<B> }` | |
7453a54e | 1879 | Enum(EnumDef, Generics), |
3157f602 XL |
1880 | /// A struct definition (`struct` or `pub struct`). |
1881 | /// | |
1882 | /// E.g. `struct Foo<A> { x: A }` | |
7453a54e | 1883 | Struct(VariantData, Generics), |
9e0c209e SL |
1884 | /// A union definition (`union` or `pub union`). |
1885 | /// | |
1886 | /// E.g. `union Foo<A, B> { x: A, y: B }` | |
1887 | Union(VariantData, Generics), | |
3157f602 XL |
1888 | /// A Trait declaration (`trait` or `pub trait`). |
1889 | /// | |
1890 | /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }` | |
a7813a04 | 1891 | Trait(Unsafety, Generics, TyParamBounds, Vec<TraitItem>), |
3157f602 | 1892 | // Default trait implementation. |
c34b1796 | 1893 | /// |
3157f602 | 1894 | /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}` |
7453a54e | 1895 | DefaultImpl(Unsafety, TraitRef), |
3157f602 XL |
1896 | /// An implementation. |
1897 | /// | |
1898 | /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }` | |
7453a54e | 1899 | Impl(Unsafety, |
1a4d82fc JJ |
1900 | ImplPolarity, |
1901 | Generics, | |
1902 | Option<TraitRef>, // (optional) trait this impl implements | |
1903 | P<Ty>, // self | |
7453a54e | 1904 | Vec<ImplItem>), |
3157f602 XL |
1905 | /// A macro invocation (which includes macro definition). |
1906 | /// | |
1907 | /// E.g. `macro_rules! foo { .. }` or `foo!(..)` | |
7453a54e | 1908 | Mac(Mac), |
1a4d82fc JJ |
1909 | } |
1910 | ||
7453a54e | 1911 | impl ItemKind { |
1a4d82fc JJ |
1912 | pub fn descriptive_variant(&self) -> &str { |
1913 | match *self { | |
7453a54e SL |
1914 | ItemKind::ExternCrate(..) => "extern crate", |
1915 | ItemKind::Use(..) => "use", | |
1916 | ItemKind::Static(..) => "static item", | |
1917 | ItemKind::Const(..) => "constant item", | |
1918 | ItemKind::Fn(..) => "function", | |
1919 | ItemKind::Mod(..) => "module", | |
1920 | ItemKind::ForeignMod(..) => "foreign module", | |
1921 | ItemKind::Ty(..) => "type alias", | |
1922 | ItemKind::Enum(..) => "enum", | |
1923 | ItemKind::Struct(..) => "struct", | |
9e0c209e | 1924 | ItemKind::Union(..) => "union", |
7453a54e SL |
1925 | ItemKind::Trait(..) => "trait", |
1926 | ItemKind::Mac(..) | | |
1927 | ItemKind::Impl(..) | | |
1928 | ItemKind::DefaultImpl(..) => "item" | |
1a4d82fc | 1929 | } |
970d7e83 | 1930 | } |
1a4d82fc | 1931 | } |
970d7e83 | 1932 | |
85aaf69f | 1933 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1934 | pub struct ForeignItem { |
1935 | pub ident: Ident, | |
1936 | pub attrs: Vec<Attribute>, | |
7453a54e | 1937 | pub node: ForeignItemKind, |
1a4d82fc JJ |
1938 | pub id: NodeId, |
1939 | pub span: Span, | |
1940 | pub vis: Visibility, | |
1941 | } | |
970d7e83 | 1942 | |
c34b1796 | 1943 | /// An item within an `extern` block |
85aaf69f | 1944 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
7453a54e | 1945 | pub enum ForeignItemKind { |
c34b1796 | 1946 | /// A foreign function |
7453a54e | 1947 | Fn(P<FnDecl>, Generics), |
c34b1796 AL |
1948 | /// A foreign static item (`static ext: u8`), with optional mutability |
1949 | /// (the boolean is true when mutable) | |
7453a54e | 1950 | Static(P<Ty>, bool), |
1a4d82fc | 1951 | } |
970d7e83 | 1952 | |
7453a54e | 1953 | impl ForeignItemKind { |
1a4d82fc JJ |
1954 | pub fn descriptive_variant(&self) -> &str { |
1955 | match *self { | |
7453a54e SL |
1956 | ForeignItemKind::Fn(..) => "foreign function", |
1957 | ForeignItemKind::Static(..) => "foreign static item" | |
1a4d82fc | 1958 | } |
223e47cc | 1959 | } |
1a4d82fc | 1960 | } |
970d7e83 | 1961 | |
1a4d82fc JJ |
1962 | /// A macro definition, in this crate or imported from another. |
1963 | /// | |
1964 | /// Not parsed directly, but created on macro import or `macro_rules!` expansion. | |
85aaf69f | 1965 | #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc JJ |
1966 | pub struct MacroDef { |
1967 | pub ident: Ident, | |
1968 | pub attrs: Vec<Attribute>, | |
1969 | pub id: NodeId, | |
1970 | pub span: Span, | |
1971 | pub imported_from: Option<Ident>, | |
c34b1796 | 1972 | pub allow_internal_unstable: bool, |
1a4d82fc JJ |
1973 | pub body: Vec<TokenTree>, |
1974 | } | |
1975 | ||
1976 | #[cfg(test)] | |
d9579d0f | 1977 | mod tests { |
1a4d82fc JJ |
1978 | use serialize; |
1979 | use super::*; | |
1980 | ||
1981 | // are ASTs encodable? | |
1982 | #[test] | |
1983 | fn check_asts_encodable() { | |
1984 | fn assert_encodable<T: serialize::Encodable>() {} | |
1985 | assert_encodable::<Crate>(); | |
1986 | } | |
1987 | } |