]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/mod.rs
Update E0253.rs
[rust.git] / src / librustc / hir / mod.rs
1 // Copyright 2015 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 HIR.
12
13 pub use self::BindingMode::*;
14 pub use self::BinOp_::*;
15 pub use self::BlockCheckMode::*;
16 pub use self::CaptureClause::*;
17 pub use self::Decl_::*;
18 pub use self::Expr_::*;
19 pub use self::FunctionRetTy::*;
20 pub use self::ForeignItem_::*;
21 pub use self::Item_::*;
22 pub use self::Mutability::*;
23 pub use self::PathListItem_::*;
24 pub use self::PrimTy::*;
25 pub use self::Stmt_::*;
26 pub use self::TraitItem_::*;
27 pub use self::Ty_::*;
28 pub use self::TyParamBound::*;
29 pub use self::UnOp::*;
30 pub use self::UnsafeSource::*;
31 pub use self::ViewPath_::*;
32 pub use self::Visibility::{Public, Inherited};
33 pub use self::PathParameters::*;
34
35 use hir::def::Def;
36 use hir::def_id::DefId;
37 use util::nodemap::{NodeMap, FnvHashSet};
38
39 use syntax_pos::{BytePos, mk_sp, Span, ExpnId};
40 use syntax::codemap::{self, respan, Spanned};
41 use syntax::abi::Abi;
42 use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, AsmDialect};
43 use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
44 use syntax::parse::token::{keywords, InternedString};
45 use syntax::ptr::P;
46 use syntax::tokenstream::TokenTree;
47 use syntax::util::ThinVec;
48
49 use std::collections::BTreeMap;
50 use std::fmt;
51
52 /// HIR doesn't commit to a concrete storage type and have its own alias for a vector.
53 /// It can be `Vec`, `P<[T]>` or potentially `Box<[T]>`, or some other container with similar
54 /// behavior. Unlike AST, HIR is mostly a static structure, so we can use an owned slice instead
55 /// of `Vec` to avoid keeping extra capacity.
56 pub type HirVec<T> = P<[T]>;
57
58 macro_rules! hir_vec {
59     ($elem:expr; $n:expr) => (
60         $crate::hir::HirVec::from(vec![$elem; $n])
61     );
62     ($($x:expr),*) => (
63         $crate::hir::HirVec::from(vec![$($x),*])
64     );
65     ($($x:expr,)*) => (hir_vec![$($x),*])
66 }
67
68 pub mod check_attr;
69 pub mod def;
70 pub mod def_id;
71 pub mod fold;
72 pub mod intravisit;
73 pub mod lowering;
74 pub mod map;
75 pub mod pat_util;
76 pub mod print;
77 pub mod svh;
78
79 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
80 pub struct Lifetime {
81     pub id: NodeId,
82     pub span: Span,
83     pub name: Name,
84 }
85
86 impl fmt::Debug for Lifetime {
87     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
88         write!(f,
89                "lifetime({}: {})",
90                self.id,
91                print::lifetime_to_string(self))
92     }
93 }
94
95 /// A lifetime definition, eg `'a: 'b+'c+'d`
96 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
97 pub struct LifetimeDef {
98     pub lifetime: Lifetime,
99     pub bounds: HirVec<Lifetime>,
100 }
101
102 /// A "Path" is essentially Rust's notion of a name; for instance:
103 /// std::cmp::PartialEq  .  It's represented as a sequence of identifiers,
104 /// along with a bunch of supporting information.
105 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
106 pub struct Path {
107     pub span: Span,
108     /// A `::foo` path, is relative to the crate root rather than current
109     /// module (like paths in an import).
110     pub global: bool,
111     /// The segments in the path: the things separated by `::`.
112     pub segments: HirVec<PathSegment>,
113 }
114
115 impl fmt::Debug for Path {
116     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117         write!(f, "path({})", print::path_to_string(self))
118     }
119 }
120
121 impl fmt::Display for Path {
122     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123         write!(f, "{}", print::path_to_string(self))
124     }
125 }
126
127 impl Path {
128     /// Convert a span and an identifier to the corresponding
129     /// 1-segment path.
130     pub fn from_name(s: Span, name: Name) -> Path {
131         Path {
132             span: s,
133             global: false,
134             segments: hir_vec![PathSegment {
135                 name: name,
136                 parameters: PathParameters::none()
137             }],
138         }
139     }
140 }
141
142 /// A segment of a path: an identifier, an optional lifetime, and a set of
143 /// types.
144 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
145 pub struct PathSegment {
146     /// The identifier portion of this path segment.
147     pub name: Name,
148
149     /// Type/lifetime parameters attached to this path. They come in
150     /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
151     /// this is more than just simple syntactic sugar; the use of
152     /// parens affects the region binding rules, so we preserve the
153     /// distinction.
154     pub parameters: PathParameters,
155 }
156
157 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
158 pub enum PathParameters {
159     /// The `<'a, A,B,C>` in `foo::bar::baz::<'a, A,B,C>`
160     AngleBracketedParameters(AngleBracketedParameterData),
161     /// The `(A,B)` and `C` in `Foo(A,B) -> C`
162     ParenthesizedParameters(ParenthesizedParameterData),
163 }
164
165 impl PathParameters {
166     pub fn none() -> PathParameters {
167         AngleBracketedParameters(AngleBracketedParameterData {
168             lifetimes: HirVec::new(),
169             types: HirVec::new(),
170             bindings: HirVec::new(),
171         })
172     }
173
174     pub fn is_empty(&self) -> bool {
175         match *self {
176             AngleBracketedParameters(ref data) => data.is_empty(),
177
178             // Even if the user supplied no types, something like
179             // `X()` is equivalent to `X<(),()>`.
180             ParenthesizedParameters(..) => false,
181         }
182     }
183
184     pub fn has_lifetimes(&self) -> bool {
185         match *self {
186             AngleBracketedParameters(ref data) => !data.lifetimes.is_empty(),
187             ParenthesizedParameters(_) => false,
188         }
189     }
190
191     pub fn has_types(&self) -> bool {
192         match *self {
193             AngleBracketedParameters(ref data) => !data.types.is_empty(),
194             ParenthesizedParameters(..) => true,
195         }
196     }
197
198     /// Returns the types that the user wrote. Note that these do not necessarily map to the type
199     /// parameters in the parenthesized case.
200     pub fn types(&self) -> HirVec<&P<Ty>> {
201         match *self {
202             AngleBracketedParameters(ref data) => {
203                 data.types.iter().collect()
204             }
205             ParenthesizedParameters(ref data) => {
206                 data.inputs
207                     .iter()
208                     .chain(data.output.iter())
209                     .collect()
210             }
211         }
212     }
213
214     pub fn lifetimes(&self) -> HirVec<&Lifetime> {
215         match *self {
216             AngleBracketedParameters(ref data) => {
217                 data.lifetimes.iter().collect()
218             }
219             ParenthesizedParameters(_) => {
220                 HirVec::new()
221             }
222         }
223     }
224
225     pub fn bindings(&self) -> HirVec<&TypeBinding> {
226         match *self {
227             AngleBracketedParameters(ref data) => {
228                 data.bindings.iter().collect()
229             }
230             ParenthesizedParameters(_) => {
231                 HirVec::new()
232             }
233         }
234     }
235 }
236
237 /// A path like `Foo<'a, T>`
238 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
239 pub struct AngleBracketedParameterData {
240     /// The lifetime parameters for this path segment.
241     pub lifetimes: HirVec<Lifetime>,
242     /// The type parameters for this path segment, if present.
243     pub types: HirVec<P<Ty>>,
244     /// Bindings (equality constraints) on associated types, if present.
245     /// E.g., `Foo<A=Bar>`.
246     pub bindings: HirVec<TypeBinding>,
247 }
248
249 impl AngleBracketedParameterData {
250     fn is_empty(&self) -> bool {
251         self.lifetimes.is_empty() && self.types.is_empty() && self.bindings.is_empty()
252     }
253 }
254
255 /// A path like `Foo(A,B) -> C`
256 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
257 pub struct ParenthesizedParameterData {
258     /// Overall span
259     pub span: Span,
260
261     /// `(A,B)`
262     pub inputs: HirVec<P<Ty>>,
263
264     /// `C`
265     pub output: Option<P<Ty>>,
266 }
267
268 /// The AST represents all type param bounds as types.
269 /// typeck::collect::compute_bounds matches these against
270 /// the "special" built-in traits (see middle::lang_items) and
271 /// detects Copy, Send and Sync.
272 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
273 pub enum TyParamBound {
274     TraitTyParamBound(PolyTraitRef, TraitBoundModifier),
275     RegionTyParamBound(Lifetime),
276 }
277
278 /// A modifier on a bound, currently this is only used for `?Sized`, where the
279 /// modifier is `Maybe`. Negative bounds should also be handled here.
280 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
281 pub enum TraitBoundModifier {
282     None,
283     Maybe,
284 }
285
286 pub type TyParamBounds = HirVec<TyParamBound>;
287
288 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
289 pub struct TyParam {
290     pub name: Name,
291     pub id: NodeId,
292     pub bounds: TyParamBounds,
293     pub default: Option<P<Ty>>,
294     pub span: Span,
295 }
296
297 /// Represents lifetimes and type parameters attached to a declaration
298 /// of a function, enum, trait, etc.
299 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
300 pub struct Generics {
301     pub lifetimes: HirVec<LifetimeDef>,
302     pub ty_params: HirVec<TyParam>,
303     pub where_clause: WhereClause,
304 }
305
306 impl Generics {
307     pub fn empty() -> Generics {
308         Generics {
309             lifetimes: HirVec::new(),
310             ty_params: HirVec::new(),
311             where_clause: WhereClause {
312                 id: DUMMY_NODE_ID,
313                 predicates: HirVec::new(),
314             },
315         }
316     }
317
318     pub fn is_lt_parameterized(&self) -> bool {
319         !self.lifetimes.is_empty()
320     }
321
322     pub fn is_type_parameterized(&self) -> bool {
323         !self.ty_params.is_empty()
324     }
325
326     pub fn is_parameterized(&self) -> bool {
327         self.is_lt_parameterized() || self.is_type_parameterized()
328     }
329
330     // Does return a span which includes lifetimes and type parameters,
331     // not where clause.
332     pub fn span(&self) -> Option<Span> {
333         if !self.is_parameterized() {
334             None
335         } else {
336             let mut span: Option<Span> = None;
337             for lifetime in self.lifetimes.iter() {
338                 if let Some(ref mut span) = span {
339                     let life_span = lifetime.lifetime.span;
340                     span.hi = if span.hi > life_span.hi { span.hi } else { life_span.hi };
341                     span.lo = if span.lo < life_span.lo { span.lo } else { life_span.lo };
342                 } else {
343                     span = Some(lifetime.lifetime.span.clone());
344                 }
345             }
346             for ty_param in self.ty_params.iter() {
347                 if let Some(ref mut span) = span {
348                     span.lo = if span.lo < ty_param.span.lo { span.lo } else { ty_param.span.lo };
349                     span.hi = if span.hi > ty_param.span.hi { span.hi } else { ty_param.span.hi };
350                 } else {
351                     span = Some(ty_param.span.clone());
352                 }
353             }
354             if let Some(ref mut span) = span {
355                 span.lo = span.lo - BytePos(1);
356                 span.hi = span.hi + BytePos(1);
357             }
358             span
359         }
360     }
361 }
362
363 /// A `where` clause in a definition
364 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
365 pub struct WhereClause {
366     pub id: NodeId,
367     pub predicates: HirVec<WherePredicate>,
368 }
369
370 /// A single predicate in a `where` clause
371 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
372 pub enum WherePredicate {
373     /// A type binding, eg `for<'c> Foo: Send+Clone+'c`
374     BoundPredicate(WhereBoundPredicate),
375     /// A lifetime predicate, e.g. `'a: 'b+'c`
376     RegionPredicate(WhereRegionPredicate),
377     /// An equality predicate (unsupported)
378     EqPredicate(WhereEqPredicate),
379 }
380
381 /// A type bound, eg `for<'c> Foo: Send+Clone+'c`
382 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
383 pub struct WhereBoundPredicate {
384     pub span: Span,
385     /// Any lifetimes from a `for` binding
386     pub bound_lifetimes: HirVec<LifetimeDef>,
387     /// The type being bounded
388     pub bounded_ty: P<Ty>,
389     /// Trait and lifetime bounds (`Clone+Send+'static`)
390     pub bounds: TyParamBounds,
391 }
392
393 /// A lifetime predicate, e.g. `'a: 'b+'c`
394 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
395 pub struct WhereRegionPredicate {
396     pub span: Span,
397     pub lifetime: Lifetime,
398     pub bounds: HirVec<Lifetime>,
399 }
400
401 /// An equality predicate (unsupported), e.g. `T=int`
402 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
403 pub struct WhereEqPredicate {
404     pub id: NodeId,
405     pub span: Span,
406     pub path: Path,
407     pub ty: P<Ty>,
408 }
409
410 pub type CrateConfig = HirVec<P<MetaItem>>;
411
412 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
413 pub struct Crate {
414     pub module: Mod,
415     pub attrs: HirVec<Attribute>,
416     pub config: CrateConfig,
417     pub span: Span,
418     pub exported_macros: HirVec<MacroDef>,
419
420     // NB: We use a BTreeMap here so that `visit_all_items` iterates
421     // over the ids in increasing order. In principle it should not
422     // matter what order we visit things in, but in *practice* it
423     // does, because it can affect the order in which errors are
424     // detected, which in turn can make compile-fail tests yield
425     // slightly different results.
426     pub items: BTreeMap<NodeId, Item>,
427 }
428
429 impl Crate {
430     pub fn item(&self, id: NodeId) -> &Item {
431         &self.items[&id]
432     }
433
434     /// Visits all items in the crate in some determinstic (but
435     /// unspecified) order. If you just need to process every item,
436     /// but don't care about nesting, this method is the best choice.
437     ///
438     /// If you do care about nesting -- usually because your algorithm
439     /// follows lexical scoping rules -- then you want a different
440     /// approach. You should override `visit_nested_item` in your
441     /// visitor and then call `intravisit::walk_crate` instead.
442     pub fn visit_all_items<'hir, V>(&'hir self, visitor: &mut V)
443         where V: intravisit::Visitor<'hir>
444     {
445         for (_, item) in &self.items {
446             visitor.visit_item(item);
447         }
448     }
449 }
450
451 /// A macro definition, in this crate or imported from another.
452 ///
453 /// Not parsed directly, but created on macro import or `macro_rules!` expansion.
454 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
455 pub struct MacroDef {
456     pub name: Name,
457     pub attrs: HirVec<Attribute>,
458     pub id: NodeId,
459     pub span: Span,
460     pub imported_from: Option<Name>,
461     pub export: bool,
462     pub use_locally: bool,
463     pub allow_internal_unstable: bool,
464     pub body: HirVec<TokenTree>,
465 }
466
467 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
468 pub struct Block {
469     /// Statements in a block
470     pub stmts: HirVec<Stmt>,
471     /// An expression at the end of the block
472     /// without a semicolon, if any
473     pub expr: Option<P<Expr>>,
474     pub id: NodeId,
475     /// Distinguishes between `unsafe { ... }` and `{ ... }`
476     pub rules: BlockCheckMode,
477     pub span: Span,
478 }
479
480 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
481 pub struct Pat {
482     pub id: NodeId,
483     pub node: PatKind,
484     pub span: Span,
485 }
486
487 impl fmt::Debug for Pat {
488     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
489         write!(f, "pat({}: {})", self.id, print::pat_to_string(self))
490     }
491 }
492
493 impl Pat {
494     // FIXME(#19596) this is a workaround, but there should be a better way
495     fn walk_<G>(&self, it: &mut G) -> bool
496         where G: FnMut(&Pat) -> bool
497     {
498         if !it(self) {
499             return false;
500         }
501
502         match self.node {
503             PatKind::Binding(_, _, Some(ref p)) => p.walk_(it),
504             PatKind::Struct(_, ref fields, _) => {
505                 fields.iter().all(|field| field.node.pat.walk_(it))
506             }
507             PatKind::TupleStruct(_, ref s, _) | PatKind::Tuple(ref s, _) => {
508                 s.iter().all(|p| p.walk_(it))
509             }
510             PatKind::Box(ref s) | PatKind::Ref(ref s, _) => {
511                 s.walk_(it)
512             }
513             PatKind::Vec(ref before, ref slice, ref after) => {
514                 before.iter().all(|p| p.walk_(it)) &&
515                 slice.iter().all(|p| p.walk_(it)) &&
516                 after.iter().all(|p| p.walk_(it))
517             }
518             PatKind::Wild |
519             PatKind::Lit(_) |
520             PatKind::Range(_, _) |
521             PatKind::Binding(..) |
522             PatKind::Path(..) => {
523                 true
524             }
525         }
526     }
527
528     pub fn walk<F>(&self, mut it: F) -> bool
529         where F: FnMut(&Pat) -> bool
530     {
531         self.walk_(&mut it)
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 name: Name,
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     BindByRef(Mutability),
552     BindByValue(Mutability),
553 }
554
555 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
556 pub enum PatKind {
557     /// Represents a wildcard pattern (`_`)
558     Wild,
559
560     /// A fresh binding `ref mut binding @ OPT_SUBPATTERN`.
561     Binding(BindingMode, Spanned<Name>, Option<P<Pat>>),
562
563     /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
564     /// The `bool` is `true` in the presence of a `..`.
565     Struct(Path, HirVec<Spanned<FieldPat>>, bool),
566
567     /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
568     /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
569     /// 0 <= position <= subpats.len()
570     TupleStruct(Path, HirVec<P<Pat>>, Option<usize>),
571
572     /// A possibly qualified path pattern.
573     /// Such pattern can be resolved to a unit struct/variant or a constant.
574     Path(Option<QSelf>, Path),
575
576     /// A tuple pattern `(a, b)`.
577     /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
578     /// 0 <= position <= subpats.len()
579     Tuple(HirVec<P<Pat>>, Option<usize>),
580     /// A `box` pattern
581     Box(P<Pat>),
582     /// A reference pattern, e.g. `&mut (a, b)`
583     Ref(P<Pat>, Mutability),
584     /// A literal
585     Lit(P<Expr>),
586     /// A range pattern, e.g. `1...2`
587     Range(P<Expr>, P<Expr>),
588     /// `[a, b, ..i, y, z]` is represented as:
589     ///     `PatKind::Vec(box [a, b], Some(i), box [y, z])`
590     Vec(HirVec<P<Pat>>, Option<P<Pat>>, HirVec<P<Pat>>),
591 }
592
593 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
594 pub enum Mutability {
595     MutMutable,
596     MutImmutable,
597 }
598
599 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
600 pub enum BinOp_ {
601     /// The `+` operator (addition)
602     BiAdd,
603     /// The `-` operator (subtraction)
604     BiSub,
605     /// The `*` operator (multiplication)
606     BiMul,
607     /// The `/` operator (division)
608     BiDiv,
609     /// The `%` operator (modulus)
610     BiRem,
611     /// The `&&` operator (logical and)
612     BiAnd,
613     /// The `||` operator (logical or)
614     BiOr,
615     /// The `^` operator (bitwise xor)
616     BiBitXor,
617     /// The `&` operator (bitwise and)
618     BiBitAnd,
619     /// The `|` operator (bitwise or)
620     BiBitOr,
621     /// The `<<` operator (shift left)
622     BiShl,
623     /// The `>>` operator (shift right)
624     BiShr,
625     /// The `==` operator (equality)
626     BiEq,
627     /// The `<` operator (less than)
628     BiLt,
629     /// The `<=` operator (less than or equal to)
630     BiLe,
631     /// The `!=` operator (not equal to)
632     BiNe,
633     /// The `>=` operator (greater than or equal to)
634     BiGe,
635     /// The `>` operator (greater than)
636     BiGt,
637 }
638
639 impl BinOp_ {
640     pub fn as_str(self) -> &'static str {
641         match self {
642             BiAdd => "+",
643             BiSub => "-",
644             BiMul => "*",
645             BiDiv => "/",
646             BiRem => "%",
647             BiAnd => "&&",
648             BiOr => "||",
649             BiBitXor => "^",
650             BiBitAnd => "&",
651             BiBitOr => "|",
652             BiShl => "<<",
653             BiShr => ">>",
654             BiEq => "==",
655             BiLt => "<",
656             BiLe => "<=",
657             BiNe => "!=",
658             BiGe => ">=",
659             BiGt => ">",
660         }
661     }
662
663     pub fn is_lazy(self) -> bool {
664         match self {
665             BiAnd | BiOr => true,
666             _ => false,
667         }
668     }
669
670     pub fn is_shift(self) -> bool {
671         match self {
672             BiShl | BiShr => true,
673             _ => false,
674         }
675     }
676
677     pub fn is_comparison(self) -> bool {
678         match self {
679             BiEq | BiLt | BiLe | BiNe | BiGt | BiGe => true,
680             BiAnd |
681             BiOr |
682             BiAdd |
683             BiSub |
684             BiMul |
685             BiDiv |
686             BiRem |
687             BiBitXor |
688             BiBitAnd |
689             BiBitOr |
690             BiShl |
691             BiShr => false,
692         }
693     }
694
695     /// Returns `true` if the binary operator takes its arguments by value
696     pub fn is_by_value(self) -> bool {
697         !self.is_comparison()
698     }
699 }
700
701 pub type BinOp = Spanned<BinOp_>;
702
703 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
704 pub enum UnOp {
705     /// The `*` operator for dereferencing
706     UnDeref,
707     /// The `!` operator for logical inversion
708     UnNot,
709     /// The `-` operator for negation
710     UnNeg,
711 }
712
713 impl UnOp {
714     pub fn as_str(self) -> &'static str {
715         match self {
716             UnDeref => "*",
717             UnNot => "!",
718             UnNeg => "-",
719         }
720     }
721
722     /// Returns `true` if the unary operator takes its argument by value
723     pub fn is_by_value(self) -> bool {
724         match self {
725             UnNeg | UnNot => true,
726             _ => false,
727         }
728     }
729 }
730
731 /// A statement
732 pub type Stmt = Spanned<Stmt_>;
733
734 impl fmt::Debug for Stmt_ {
735     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
736         // Sadness.
737         let spanned = codemap::dummy_spanned(self.clone());
738         write!(f,
739                "stmt({}: {})",
740                spanned.node.id(),
741                print::stmt_to_string(&spanned))
742     }
743 }
744
745 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
746 pub enum Stmt_ {
747     /// Could be an item or a local (let) binding:
748     StmtDecl(P<Decl>, NodeId),
749
750     /// Expr without trailing semi-colon (must have unit type):
751     StmtExpr(P<Expr>, NodeId),
752
753     /// Expr with trailing semi-colon (may have any type):
754     StmtSemi(P<Expr>, NodeId),
755 }
756
757 impl Stmt_ {
758     pub fn attrs(&self) -> &[Attribute] {
759         match *self {
760             StmtDecl(ref d, _) => d.node.attrs(),
761             StmtExpr(ref e, _) |
762             StmtSemi(ref e, _) => &e.attrs,
763         }
764     }
765
766     pub fn id(&self) -> NodeId {
767         match *self {
768             StmtDecl(_, id) => id,
769             StmtExpr(_, id) => id,
770             StmtSemi(_, id) => id,
771         }
772     }
773 }
774
775 // FIXME (pending discussion of #1697, #2178...): local should really be
776 // a refinement on pat.
777 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
778 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
779 pub struct Local {
780     pub pat: P<Pat>,
781     pub ty: Option<P<Ty>>,
782     /// Initializer expression to set the value, if any
783     pub init: Option<P<Expr>>,
784     pub id: NodeId,
785     pub span: Span,
786     pub attrs: ThinVec<Attribute>,
787 }
788
789 pub type Decl = Spanned<Decl_>;
790
791 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
792 pub enum Decl_ {
793     /// A local (let) binding:
794     DeclLocal(P<Local>),
795     /// An item binding:
796     DeclItem(ItemId),
797 }
798
799 impl Decl_ {
800     pub fn attrs(&self) -> &[Attribute] {
801         match *self {
802             DeclLocal(ref l) => &l.attrs,
803             DeclItem(_) => &[]
804         }
805     }
806 }
807
808 /// represents one arm of a 'match'
809 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
810 pub struct Arm {
811     pub attrs: HirVec<Attribute>,
812     pub pats: HirVec<P<Pat>>,
813     pub guard: Option<P<Expr>>,
814     pub body: P<Expr>,
815 }
816
817 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
818 pub struct Field {
819     pub name: Spanned<Name>,
820     pub expr: P<Expr>,
821     pub span: Span,
822 }
823
824 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
825 pub enum BlockCheckMode {
826     DefaultBlock,
827     UnsafeBlock(UnsafeSource),
828     PushUnsafeBlock(UnsafeSource),
829     PopUnsafeBlock(UnsafeSource),
830     // Within this block (but outside a PopUnstableBlock), we suspend checking of stability.
831     PushUnstableBlock,
832     PopUnstableBlock,
833 }
834
835 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
836 pub enum UnsafeSource {
837     CompilerGenerated,
838     UserProvided,
839 }
840
841 /// An expression
842 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
843 pub struct Expr {
844     pub id: NodeId,
845     pub node: Expr_,
846     pub span: Span,
847     pub attrs: ThinVec<Attribute>,
848 }
849
850 impl fmt::Debug for Expr {
851     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
852         write!(f, "expr({}: {})", self.id, print::expr_to_string(self))
853     }
854 }
855
856 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
857 pub enum Expr_ {
858     /// A `box x` expression.
859     ExprBox(P<Expr>),
860     /// An array (`[a, b, c, d]`)
861     ExprVec(HirVec<P<Expr>>),
862     /// A function call
863     ///
864     /// The first field resolves to the function itself (usually an `ExprPath`),
865     /// and the second field is the list of arguments
866     ExprCall(P<Expr>, HirVec<P<Expr>>),
867     /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
868     ///
869     /// The `Spanned<Name>` is the identifier for the method name.
870     /// The vector of `Ty`s are the ascripted type parameters for the method
871     /// (within the angle brackets).
872     ///
873     /// The first element of the vector of `Expr`s is the expression that
874     /// evaluates to the object on which the method is being called on (the
875     /// receiver), and the remaining elements are the rest of the arguments.
876     ///
877     /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
878     /// `ExprMethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
879     ExprMethodCall(Spanned<Name>, HirVec<P<Ty>>, HirVec<P<Expr>>),
880     /// A tuple (`(a, b, c ,d)`)
881     ExprTup(HirVec<P<Expr>>),
882     /// A binary operation (For example: `a + b`, `a * b`)
883     ExprBinary(BinOp, P<Expr>, P<Expr>),
884     /// A unary operation (For example: `!x`, `*x`)
885     ExprUnary(UnOp, P<Expr>),
886     /// A literal (For example: `1`, `"foo"`)
887     ExprLit(P<Lit>),
888     /// A cast (`foo as f64`)
889     ExprCast(P<Expr>, P<Ty>),
890     ExprType(P<Expr>, P<Ty>),
891     /// An `if` block, with an optional else block
892     ///
893     /// `if expr { block } else { expr }`
894     ExprIf(P<Expr>, P<Block>, Option<P<Expr>>),
895     /// A while loop, with an optional label
896     ///
897     /// `'label: while expr { block }`
898     ExprWhile(P<Expr>, P<Block>, Option<Spanned<Name>>),
899     /// Conditionless loop (can be exited with break, continue, or return)
900     ///
901     /// `'label: loop { block }`
902     ExprLoop(P<Block>, Option<Spanned<Name>>),
903     /// A `match` block, with a source that indicates whether or not it is
904     /// the result of a desugaring, and if so, which kind.
905     ExprMatch(P<Expr>, HirVec<Arm>, MatchSource),
906     /// A closure (for example, `move |a, b, c| {a + b + c}`).
907     ///
908     /// The final span is the span of the argument block `|...|`
909     ExprClosure(CaptureClause, P<FnDecl>, P<Block>, Span),
910     /// A block (`{ ... }`)
911     ExprBlock(P<Block>),
912
913     /// An assignment (`a = foo()`)
914     ExprAssign(P<Expr>, P<Expr>),
915     /// An assignment with an operator
916     ///
917     /// For example, `a += 1`.
918     ExprAssignOp(BinOp, P<Expr>, P<Expr>),
919     /// Access of a named struct field (`obj.foo`)
920     ExprField(P<Expr>, Spanned<Name>),
921     /// Access of an unnamed field of a struct or tuple-struct
922     ///
923     /// For example, `foo.0`.
924     ExprTupField(P<Expr>, Spanned<usize>),
925     /// An indexing operation (`foo[2]`)
926     ExprIndex(P<Expr>, P<Expr>),
927
928     /// Variable reference, possibly containing `::` and/or type
929     /// parameters, e.g. foo::bar::<baz>.
930     ///
931     /// Optionally "qualified",
932     /// e.g. `<HirVec<T> as SomeTrait>::SomeType`.
933     ExprPath(Option<QSelf>, Path),
934
935     /// A referencing operation (`&a` or `&mut a`)
936     ExprAddrOf(Mutability, P<Expr>),
937     /// A `break`, with an optional label to break
938     ExprBreak(Option<Spanned<Name>>),
939     /// A `continue`, with an optional label
940     ExprAgain(Option<Spanned<Name>>),
941     /// A `return`, with an optional value to be returned
942     ExprRet(Option<P<Expr>>),
943
944     /// Inline assembly (from `asm!`), with its outputs and inputs.
945     ExprInlineAsm(InlineAsm, Vec<P<Expr>>, Vec<P<Expr>>),
946
947     /// A struct or struct-like variant literal expression.
948     ///
949     /// For example, `Foo {x: 1, y: 2}`, or
950     /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
951     ExprStruct(Path, HirVec<Field>, Option<P<Expr>>),
952
953     /// An array literal constructed from one repeated element.
954     ///
955     /// For example, `[1; 5]`. The first expression is the element
956     /// to be repeated; the second is the number of times to repeat it.
957     ExprRepeat(P<Expr>, P<Expr>),
958 }
959
960 /// The explicit Self type in a "qualified path". The actual
961 /// path, including the trait and the associated item, is stored
962 /// separately. `position` represents the index of the associated
963 /// item qualified with this Self type.
964 ///
965 ///     <HirVec<T> as a::b::Trait>::AssociatedItem
966 ///      ^~~~~     ~~~~~~~~~~~~~~^
967 ///      ty        position = 3
968 ///
969 ///     <HirVec<T>>::AssociatedItem
970 ///      ^~~~~    ^
971 ///      ty       position = 0
972 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
973 pub struct QSelf {
974     pub ty: P<Ty>,
975     pub position: usize,
976 }
977
978 /// Hints at the original code for a `match _ { .. }`
979 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
980 pub enum MatchSource {
981     /// A `match _ { .. }`
982     Normal,
983     /// An `if let _ = _ { .. }` (optionally with `else { .. }`)
984     IfLetDesugar {
985         contains_else_clause: bool,
986     },
987     /// A `while let _ = _ { .. }` (which was desugared to a
988     /// `loop { match _ { .. } }`)
989     WhileLetDesugar,
990     /// A desugared `for _ in _ { .. }` loop
991     ForLoopDesugar,
992     /// A desugared `?` operator
993     TryDesugar,
994 }
995
996 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
997 pub enum CaptureClause {
998     CaptureByValue,
999     CaptureByRef,
1000 }
1001
1002 // NB: If you change this, you'll probably want to change the corresponding
1003 // type structure in middle/ty.rs as well.
1004 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1005 pub struct MutTy {
1006     pub ty: P<Ty>,
1007     pub mutbl: Mutability,
1008 }
1009
1010 /// Represents a method's signature in a trait declaration or implementation.
1011 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1012 pub struct MethodSig {
1013     pub unsafety: Unsafety,
1014     pub constness: Constness,
1015     pub abi: Abi,
1016     pub decl: P<FnDecl>,
1017     pub generics: Generics,
1018 }
1019
1020 /// Represents an item declaration within a trait declaration,
1021 /// possibly including a default implementation. A trait item is
1022 /// either required (meaning it doesn't have an implementation, just a
1023 /// signature) or provided (meaning it has a default implementation).
1024 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1025 pub struct TraitItem {
1026     pub id: NodeId,
1027     pub name: Name,
1028     pub attrs: HirVec<Attribute>,
1029     pub node: TraitItem_,
1030     pub span: Span,
1031 }
1032
1033 /// Represents a trait method or associated constant or type
1034 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1035 pub enum TraitItem_ {
1036     /// An associated constant with an optional value (otherwise `impl`s
1037     /// must contain a value)
1038     ConstTraitItem(P<Ty>, Option<P<Expr>>),
1039     /// A method with an optional body
1040     MethodTraitItem(MethodSig, Option<P<Block>>),
1041     /// An associated type with (possibly empty) bounds and optional concrete
1042     /// type
1043     TypeTraitItem(TyParamBounds, Option<P<Ty>>),
1044 }
1045
1046 /// Represents anything within an `impl` block
1047 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1048 pub struct ImplItem {
1049     pub id: NodeId,
1050     pub name: Name,
1051     pub vis: Visibility,
1052     pub defaultness: Defaultness,
1053     pub attrs: HirVec<Attribute>,
1054     pub node: ImplItemKind,
1055     pub span: Span,
1056 }
1057
1058 /// Represents different contents within `impl`s
1059 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1060 pub enum ImplItemKind {
1061     /// An associated constant of the given type, set to the constant result
1062     /// of the expression
1063     Const(P<Ty>, P<Expr>),
1064     /// A method implementation with the given signature and body
1065     Method(MethodSig, P<Block>),
1066     /// An associated type
1067     Type(P<Ty>),
1068 }
1069
1070 // Bind a type to an associated type: `A=Foo`.
1071 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1072 pub struct TypeBinding {
1073     pub id: NodeId,
1074     pub name: Name,
1075     pub ty: P<Ty>,
1076     pub span: Span,
1077 }
1078
1079
1080 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
1081 pub struct Ty {
1082     pub id: NodeId,
1083     pub node: Ty_,
1084     pub span: Span,
1085 }
1086
1087 impl fmt::Debug for Ty {
1088     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1089         write!(f, "type({})", print::ty_to_string(self))
1090     }
1091 }
1092
1093 /// Not represented directly in the AST, referred to by name through a ty_path.
1094 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1095 pub enum PrimTy {
1096     TyInt(IntTy),
1097     TyUint(UintTy),
1098     TyFloat(FloatTy),
1099     TyStr,
1100     TyBool,
1101     TyChar,
1102 }
1103
1104 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1105 pub struct BareFnTy {
1106     pub unsafety: Unsafety,
1107     pub abi: Abi,
1108     pub lifetimes: HirVec<LifetimeDef>,
1109     pub decl: P<FnDecl>,
1110 }
1111
1112 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1113 /// The different kinds of types recognized by the compiler
1114 pub enum Ty_ {
1115     TyVec(P<Ty>),
1116     /// A fixed length array (`[T; n]`)
1117     TyFixedLengthVec(P<Ty>, P<Expr>),
1118     /// A raw pointer (`*const T` or `*mut T`)
1119     TyPtr(MutTy),
1120     /// A reference (`&'a T` or `&'a mut T`)
1121     TyRptr(Option<Lifetime>, MutTy),
1122     /// A bare function (e.g. `fn(usize) -> bool`)
1123     TyBareFn(P<BareFnTy>),
1124     /// A tuple (`(A, B, C, D,...)`)
1125     TyTup(HirVec<P<Ty>>),
1126     /// A path (`module::module::...::Type`), optionally
1127     /// "qualified", e.g. `<HirVec<T> as SomeTrait>::SomeType`.
1128     ///
1129     /// Type parameters are stored in the Path itself
1130     TyPath(Option<QSelf>, Path),
1131     /// Something like `A+B`. Note that `B` must always be a path.
1132     TyObjectSum(P<Ty>, TyParamBounds),
1133     /// A type like `for<'a> Foo<&'a Bar>`
1134     TyPolyTraitRef(TyParamBounds),
1135     /// Unused for now
1136     TyTypeof(P<Expr>),
1137     /// TyInfer means the type should be inferred instead of it having been
1138     /// specified. This can appear anywhere in a type.
1139     TyInfer,
1140 }
1141
1142 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1143 pub struct InlineAsmOutput {
1144     pub constraint: InternedString,
1145     pub is_rw: bool,
1146     pub is_indirect: bool,
1147 }
1148
1149 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1150 pub struct InlineAsm {
1151     pub asm: InternedString,
1152     pub asm_str_style: StrStyle,
1153     pub outputs: HirVec<InlineAsmOutput>,
1154     pub inputs: HirVec<InternedString>,
1155     pub clobbers: HirVec<InternedString>,
1156     pub volatile: bool,
1157     pub alignstack: bool,
1158     pub dialect: AsmDialect,
1159     pub expn_id: ExpnId,
1160 }
1161
1162 /// represents an argument in a function header
1163 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1164 pub struct Arg {
1165     pub ty: P<Ty>,
1166     pub pat: P<Pat>,
1167     pub id: NodeId,
1168 }
1169
1170 /// Alternative representation for `Arg`s describing `self` parameter of methods.
1171 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1172 pub enum SelfKind {
1173     /// `self`, `mut self`
1174     Value(Mutability),
1175     /// `&'lt self`, `&'lt mut self`
1176     Region(Option<Lifetime>, Mutability),
1177     /// `self: TYPE`, `mut self: TYPE`
1178     Explicit(P<Ty>, Mutability),
1179 }
1180
1181 pub type ExplicitSelf = Spanned<SelfKind>;
1182
1183 impl Arg {
1184     pub fn to_self(&self) -> Option<ExplicitSelf> {
1185         if let PatKind::Binding(BindByValue(mutbl), name, _) = self.pat.node {
1186             if name.node == keywords::SelfValue.name() {
1187                 return match self.ty.node {
1188                     TyInfer => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
1189                     TyRptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyInfer => {
1190                         Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
1191                     }
1192                     _ => Some(respan(mk_sp(self.pat.span.lo, self.ty.span.hi),
1193                                      SelfKind::Explicit(self.ty.clone(), mutbl)))
1194                 }
1195             }
1196         }
1197         None
1198     }
1199
1200     pub fn is_self(&self) -> bool {
1201         if let PatKind::Binding(_, name, _) = self.pat.node {
1202             name.node == keywords::SelfValue.name()
1203         } else {
1204             false
1205         }
1206     }
1207 }
1208
1209 /// Represents the header (not the body) of a function declaration
1210 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1211 pub struct FnDecl {
1212     pub inputs: HirVec<Arg>,
1213     pub output: FunctionRetTy,
1214     pub variadic: bool,
1215 }
1216
1217 impl FnDecl {
1218     pub fn get_self(&self) -> Option<ExplicitSelf> {
1219         self.inputs.get(0).and_then(Arg::to_self)
1220     }
1221     pub fn has_self(&self) -> bool {
1222         self.inputs.get(0).map(Arg::is_self).unwrap_or(false)
1223     }
1224 }
1225
1226 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1227 pub enum Unsafety {
1228     Unsafe,
1229     Normal,
1230 }
1231
1232 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1233 pub enum Constness {
1234     Const,
1235     NotConst,
1236 }
1237
1238 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1239 pub enum Defaultness {
1240     Default,
1241     Final,
1242 }
1243
1244 impl Defaultness {
1245     pub fn is_final(&self) -> bool {
1246         *self == Defaultness::Final
1247     }
1248
1249     pub fn is_default(&self) -> bool {
1250         *self == Defaultness::Default
1251     }
1252 }
1253
1254 impl fmt::Display for Unsafety {
1255     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1256         fmt::Display::fmt(match *self {
1257                               Unsafety::Normal => "normal",
1258                               Unsafety::Unsafe => "unsafe",
1259                           },
1260                           f)
1261     }
1262 }
1263
1264 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
1265 pub enum ImplPolarity {
1266     /// `impl Trait for Type`
1267     Positive,
1268     /// `impl !Trait for Type`
1269     Negative,
1270 }
1271
1272 impl fmt::Debug for ImplPolarity {
1273     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1274         match *self {
1275             ImplPolarity::Positive => "positive".fmt(f),
1276             ImplPolarity::Negative => "negative".fmt(f),
1277         }
1278     }
1279 }
1280
1281
1282 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1283 pub enum FunctionRetTy {
1284     /// Functions with return type `!`that always
1285     /// raise an error or exit (i.e. never return to the caller)
1286     NoReturn(Span),
1287     /// Return type is not specified.
1288     ///
1289     /// Functions default to `()` and
1290     /// closures default to inference. Span points to where return
1291     /// type would be inserted.
1292     DefaultReturn(Span),
1293     /// Everything else
1294     Return(P<Ty>),
1295 }
1296
1297 impl FunctionRetTy {
1298     pub fn span(&self) -> Span {
1299         match *self {
1300             NoReturn(span) => span,
1301             DefaultReturn(span) => span,
1302             Return(ref ty) => ty.span,
1303         }
1304     }
1305 }
1306
1307 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1308 pub struct Mod {
1309     /// A span from the first token past `{` to the last token until `}`.
1310     /// For `mod foo;`, the inner span ranges from the first token
1311     /// to the last token in the external file.
1312     pub inner: Span,
1313     pub item_ids: HirVec<ItemId>,
1314 }
1315
1316 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1317 pub struct ForeignMod {
1318     pub abi: Abi,
1319     pub items: HirVec<ForeignItem>,
1320 }
1321
1322 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1323 pub struct EnumDef {
1324     pub variants: HirVec<Variant>,
1325 }
1326
1327 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1328 pub struct Variant_ {
1329     pub name: Name,
1330     pub attrs: HirVec<Attribute>,
1331     pub data: VariantData,
1332     /// Explicit discriminant, eg `Foo = 1`
1333     pub disr_expr: Option<P<Expr>>,
1334 }
1335
1336 pub type Variant = Spanned<Variant_>;
1337
1338 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1339 pub enum PathListItem_ {
1340     PathListIdent {
1341         name: Name,
1342         /// renamed in list, eg `use foo::{bar as baz};`
1343         rename: Option<Name>,
1344         id: NodeId,
1345     },
1346     PathListMod {
1347         /// renamed in list, eg `use foo::{self as baz};`
1348         rename: Option<Name>,
1349         id: NodeId,
1350     },
1351 }
1352
1353 impl PathListItem_ {
1354     pub fn id(&self) -> NodeId {
1355         match *self {
1356             PathListIdent { id, .. } | PathListMod { id, .. } => id,
1357         }
1358     }
1359
1360     pub fn name(&self) -> Option<Name> {
1361         match *self {
1362             PathListIdent { name, .. } => Some(name),
1363             PathListMod { .. } => None,
1364         }
1365     }
1366
1367     pub fn rename(&self) -> Option<Name> {
1368         match *self {
1369             PathListIdent { rename, .. } | PathListMod { rename, .. } => rename,
1370         }
1371     }
1372 }
1373
1374 pub type PathListItem = Spanned<PathListItem_>;
1375
1376 pub type ViewPath = Spanned<ViewPath_>;
1377
1378 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1379 pub enum ViewPath_ {
1380     /// `foo::bar::baz as quux`
1381     ///
1382     /// or just
1383     ///
1384     /// `foo::bar::baz` (with `as baz` implicitly on the right)
1385     ViewPathSimple(Name, Path),
1386
1387     /// `foo::bar::*`
1388     ViewPathGlob(Path),
1389
1390     /// `foo::bar::{a,b,c}`
1391     ViewPathList(Path, HirVec<PathListItem>),
1392 }
1393
1394 /// TraitRef's appear in impls.
1395 ///
1396 /// resolve maps each TraitRef's ref_id to its defining trait; that's all
1397 /// that the ref_id is for. Note that ref_id's value is not the NodeId of the
1398 /// trait being referred to but just a unique NodeId that serves as a key
1399 /// within the DefMap.
1400 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1401 pub struct TraitRef {
1402     pub path: Path,
1403     pub ref_id: NodeId,
1404 }
1405
1406 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1407 pub struct PolyTraitRef {
1408     /// The `'a` in `<'a> Foo<&'a T>`
1409     pub bound_lifetimes: HirVec<LifetimeDef>,
1410
1411     /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
1412     pub trait_ref: TraitRef,
1413
1414     pub span: Span,
1415 }
1416
1417 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1418 pub enum Visibility {
1419     Public,
1420     Crate,
1421     Restricted { path: P<Path>, id: NodeId },
1422     Inherited,
1423 }
1424
1425 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1426 pub struct StructField {
1427     pub span: Span,
1428     pub name: Name,
1429     pub vis: Visibility,
1430     pub id: NodeId,
1431     pub ty: P<Ty>,
1432     pub attrs: HirVec<Attribute>,
1433 }
1434
1435 impl StructField {
1436     // Still necessary in couple of places
1437     pub fn is_positional(&self) -> bool {
1438         let first = self.name.as_str().as_bytes()[0];
1439         first >= b'0' && first <= b'9'
1440     }
1441 }
1442
1443 /// Fields and Ids of enum variants and structs
1444 ///
1445 /// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all
1446 /// variant kinds) and an Id of the variant's constructor (not relevant for `Struct`-variants).
1447 /// One shared Id can be successfully used for these two purposes.
1448 /// Id of the whole enum lives in `Item`.
1449 ///
1450 /// For structs: `NodeId` represents an Id of the structure's constructor, so it is not actually
1451 /// used for `Struct`-structs (but still presents). Structures don't have an analogue of "Id of
1452 /// the variant itself" from enum variants.
1453 /// Id of the whole struct lives in `Item`.
1454 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1455 pub enum VariantData {
1456     Struct(HirVec<StructField>, NodeId),
1457     Tuple(HirVec<StructField>, NodeId),
1458     Unit(NodeId),
1459 }
1460
1461 impl VariantData {
1462     pub fn fields(&self) -> &[StructField] {
1463         match *self {
1464             VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => fields,
1465             _ => &[],
1466         }
1467     }
1468     pub fn id(&self) -> NodeId {
1469         match *self {
1470             VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id,
1471         }
1472     }
1473     pub fn is_struct(&self) -> bool {
1474         if let VariantData::Struct(..) = *self {
1475             true
1476         } else {
1477             false
1478         }
1479     }
1480     pub fn is_tuple(&self) -> bool {
1481         if let VariantData::Tuple(..) = *self {
1482             true
1483         } else {
1484             false
1485         }
1486     }
1487     pub fn is_unit(&self) -> bool {
1488         if let VariantData::Unit(..) = *self {
1489             true
1490         } else {
1491             false
1492         }
1493     }
1494 }
1495
1496 // The bodies for items are stored "out of line", in a separate
1497 // hashmap in the `Crate`. Here we just record the node-id of the item
1498 // so it can fetched later.
1499 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1500 pub struct ItemId {
1501     pub id: NodeId,
1502 }
1503
1504 //  FIXME (#3300): Should allow items to be anonymous. Right now
1505 //  we just use dummy names for anon items.
1506 /// An item
1507 ///
1508 /// The name might be a dummy name in case of anonymous items
1509 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1510 pub struct Item {
1511     pub name: Name,
1512     pub attrs: HirVec<Attribute>,
1513     pub id: NodeId,
1514     pub node: Item_,
1515     pub vis: Visibility,
1516     pub span: Span,
1517 }
1518
1519 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1520 pub enum Item_ {
1521     /// An`extern crate` item, with optional original crate name,
1522     ///
1523     /// e.g. `extern crate foo` or `extern crate foo_bar as foo`
1524     ItemExternCrate(Option<Name>),
1525     /// A `use` or `pub use` item
1526     ItemUse(P<ViewPath>),
1527
1528     /// A `static` item
1529     ItemStatic(P<Ty>, Mutability, P<Expr>),
1530     /// A `const` item
1531     ItemConst(P<Ty>, P<Expr>),
1532     /// A function declaration
1533     ItemFn(P<FnDecl>, Unsafety, Constness, Abi, Generics, P<Block>),
1534     /// A module
1535     ItemMod(Mod),
1536     /// An external module
1537     ItemForeignMod(ForeignMod),
1538     /// A type alias, e.g. `type Foo = Bar<u8>`
1539     ItemTy(P<Ty>, Generics),
1540     /// An enum definition, e.g. `enum Foo<A, B> {C<A>, D<B>}`
1541     ItemEnum(EnumDef, Generics),
1542     /// A struct definition, e.g. `struct Foo<A> {x: A}`
1543     ItemStruct(VariantData, Generics),
1544     /// Represents a Trait Declaration
1545     ItemTrait(Unsafety, Generics, TyParamBounds, HirVec<TraitItem>),
1546
1547     // Default trait implementations
1548     ///
1549     /// `impl Trait for .. {}`
1550     ItemDefaultImpl(Unsafety, TraitRef),
1551     /// An implementation, eg `impl<A> Trait for Foo { .. }`
1552     ItemImpl(Unsafety,
1553              ImplPolarity,
1554              Generics,
1555              Option<TraitRef>, // (optional) trait this impl implements
1556              P<Ty>, // self
1557              HirVec<ImplItem>),
1558 }
1559
1560 impl Item_ {
1561     pub fn descriptive_variant(&self) -> &str {
1562         match *self {
1563             ItemExternCrate(..) => "extern crate",
1564             ItemUse(..) => "use",
1565             ItemStatic(..) => "static item",
1566             ItemConst(..) => "constant item",
1567             ItemFn(..) => "function",
1568             ItemMod(..) => "module",
1569             ItemForeignMod(..) => "foreign module",
1570             ItemTy(..) => "type alias",
1571             ItemEnum(..) => "enum",
1572             ItemStruct(..) => "struct",
1573             ItemTrait(..) => "trait",
1574             ItemImpl(..) |
1575             ItemDefaultImpl(..) => "item",
1576         }
1577     }
1578 }
1579
1580 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1581 pub struct ForeignItem {
1582     pub name: Name,
1583     pub attrs: HirVec<Attribute>,
1584     pub node: ForeignItem_,
1585     pub id: NodeId,
1586     pub span: Span,
1587     pub vis: Visibility,
1588 }
1589
1590 /// An item within an `extern` block
1591 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1592 pub enum ForeignItem_ {
1593     /// A foreign function
1594     ForeignItemFn(P<FnDecl>, Generics),
1595     /// A foreign static item (`static ext: u8`), with optional mutability
1596     /// (the boolean is true when mutable)
1597     ForeignItemStatic(P<Ty>, bool),
1598 }
1599
1600 impl ForeignItem_ {
1601     pub fn descriptive_variant(&self) -> &str {
1602         match *self {
1603             ForeignItemFn(..) => "foreign function",
1604             ForeignItemStatic(..) => "foreign static item",
1605         }
1606     }
1607 }
1608
1609 /// A free variable referred to in a function.
1610 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
1611 pub struct Freevar {
1612     /// The variable being accessed free.
1613     pub def: Def,
1614
1615     // First span where it is accessed (there can be multiple).
1616     pub span: Span
1617 }
1618
1619 pub type FreevarMap = NodeMap<Vec<Freevar>>;
1620
1621 pub type CaptureModeMap = NodeMap<CaptureClause>;
1622
1623 #[derive(Clone)]
1624 pub struct TraitCandidate {
1625     pub def_id: DefId,
1626     pub import_id: Option<NodeId>,
1627 }
1628
1629 // Trait method resolution
1630 pub type TraitMap = NodeMap<Vec<TraitCandidate>>;
1631
1632 // Map from the NodeId of a glob import to a list of items which are actually
1633 // imported.
1634 pub type GlobMap = NodeMap<FnvHashSet<Name>>;