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