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