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