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.
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.
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::ExplicitSelf_::*;
19 pub use self::Expr_::*;
20 pub use self::FunctionRetTy::*;
21 pub use self::ForeignItem_::*;
22 pub use self::Item_::*;
23 pub use self::Mutability::*;
24 pub use self::PathListItem_::*;
25 pub use self::PrimTy::*;
26 pub use self::Stmt_::*;
27 pub use self::StructFieldKind::*;
28 pub use self::TraitItem_::*;
30 pub use self::TyParamBound::*;
31 pub use self::UnOp::*;
32 pub use self::UnsafeSource::*;
33 pub use self::ViewPath_::*;
34 pub use self::Visibility::*;
35 pub use self::PathParameters::*;
37 use intravisit::Visitor;
38 use std::collections::BTreeMap;
39 use syntax::codemap::{self, Span, Spanned, DUMMY_SP, ExpnId};
41 use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, TokenTree, AsmDialect};
42 use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
43 use syntax::attr::ThinAttributes;
44 use syntax::parse::token::InternedString;
51 use std::hash::{Hash, Hasher};
52 use serialize::{Encodable, Decodable, Encoder, Decoder};
54 /// HIR doesn't commit to a concrete storage type and have its own alias for a vector.
55 /// It can be `Vec`, `P<[T]>` or potentially `Box<[T]>`, or some other container with similar
56 /// behavior. Unlike AST, HIR is mostly a static structure, so we can use an owned slice instead
57 /// of `Vec` to avoid keeping extra capacity.
58 pub type HirVec<T> = P<[T]>;
60 macro_rules! hir_vec {
61 ($elem:expr; $n:expr) => (
62 $crate::hir::HirVec::from(vec![$elem; $n])
65 $crate::hir::HirVec::from(vec![$($x),*])
67 ($($x:expr,)*) => (vec![$($x),*])
71 #[derive(Clone, Copy, Eq)]
73 /// Hygienic name (renamed), should be used by default
75 /// Unhygienic name (original, not renamed), needed in few places in name resolution
76 pub unhygienic_name: Name,
80 /// Creates a HIR identifier with both `name` and `unhygienic_name` initialized with
81 /// the argument. Hygiene properties of the created identifier depend entirely on this
82 /// argument. If the argument is a plain interned string `intern("iter")`, then the result
83 /// is unhygienic and can interfere with other entities named "iter". If the argument is
84 /// a "fresh" name created with `gensym("iter")`, then the result is hygienic and can't
85 /// interfere with other entities having the same string as a name.
86 pub fn from_name(name: Name) -> Ident {
87 Ident { name: name, unhygienic_name: name }
91 impl PartialEq for Ident {
92 fn eq(&self, other: &Ident) -> bool {
93 self.name == other.name
98 fn hash<H: Hasher>(&self, state: &mut H) {
103 impl fmt::Debug for Ident {
104 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105 fmt::Debug::fmt(&self.name, f)
109 impl fmt::Display for Ident {
110 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
111 fmt::Display::fmt(&self.name, f)
115 impl Encodable for Ident {
116 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
121 impl Decodable for Ident {
122 fn decode<D: Decoder>(d: &mut D) -> Result<Ident, D::Error> {
123 Ok(Ident::from_name(try!(Name::decode(d))))
127 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
128 pub struct Lifetime {
134 impl fmt::Debug for Lifetime {
135 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139 pprust::lifetime_to_string(self))
143 /// A lifetime definition, eg `'a: 'b+'c+'d`
144 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
145 pub struct LifetimeDef {
146 pub lifetime: Lifetime,
147 pub bounds: HirVec<Lifetime>,
150 /// A "Path" is essentially Rust's notion of a name; for instance:
151 /// std::cmp::PartialEq . It's represented as a sequence of identifiers,
152 /// along with a bunch of supporting information.
153 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
156 /// A `::foo` path, is relative to the crate root rather than current
157 /// module (like paths in an import).
159 /// The segments in the path: the things separated by `::`.
160 pub segments: HirVec<PathSegment>,
163 impl fmt::Debug for Path {
164 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
165 write!(f, "path({})", pprust::path_to_string(self))
169 impl fmt::Display for Path {
170 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
171 write!(f, "{}", pprust::path_to_string(self))
175 /// A segment of a path: an identifier, an optional lifetime, and a set of
177 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
178 pub struct PathSegment {
179 /// The identifier portion of this path segment.
181 /// Hygiene properties of this identifier are worth noting.
182 /// Most path segments are not hygienic and they are not renamed during
183 /// lowering from AST to HIR (see comments to `fn lower_path`). However segments from
184 /// unqualified paths with one segment originating from `ExprPath` (local-variable-like paths)
185 /// can be hygienic, so they are renamed. You should not normally care about this peculiarity
186 /// and just use `identifier.name` unless you modify identifier resolution code
187 /// (`fn resolve_identifier` and other functions called by it in `rustc_resolve`).
188 pub identifier: Ident,
190 /// Type/lifetime parameters attached to this path. They come in
191 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
192 /// this is more than just simple syntactic sugar; the use of
193 /// parens affects the region binding rules, so we preserve the
195 pub parameters: PathParameters,
198 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
199 pub enum PathParameters {
200 /// The `<'a, A,B,C>` in `foo::bar::baz::<'a, A,B,C>`
201 AngleBracketedParameters(AngleBracketedParameterData),
202 /// The `(A,B)` and `C` in `Foo(A,B) -> C`
203 ParenthesizedParameters(ParenthesizedParameterData),
206 impl PathParameters {
207 pub fn none() -> PathParameters {
208 AngleBracketedParameters(AngleBracketedParameterData {
209 lifetimes: HirVec::new(),
210 types: HirVec::new(),
211 bindings: HirVec::new(),
215 pub fn is_empty(&self) -> bool {
217 AngleBracketedParameters(ref data) => data.is_empty(),
219 // Even if the user supplied no types, something like
220 // `X()` is equivalent to `X<(),()>`.
221 ParenthesizedParameters(..) => false,
225 pub fn has_lifetimes(&self) -> bool {
227 AngleBracketedParameters(ref data) => !data.lifetimes.is_empty(),
228 ParenthesizedParameters(_) => false,
232 pub fn has_types(&self) -> bool {
234 AngleBracketedParameters(ref data) => !data.types.is_empty(),
235 ParenthesizedParameters(..) => true,
239 /// Returns the types that the user wrote. Note that these do not necessarily map to the type
240 /// parameters in the parenthesized case.
241 pub fn types(&self) -> HirVec<&P<Ty>> {
243 AngleBracketedParameters(ref data) => {
244 data.types.iter().collect()
246 ParenthesizedParameters(ref data) => {
249 .chain(data.output.iter())
255 pub fn lifetimes(&self) -> HirVec<&Lifetime> {
257 AngleBracketedParameters(ref data) => {
258 data.lifetimes.iter().collect()
260 ParenthesizedParameters(_) => {
266 pub fn bindings(&self) -> HirVec<&TypeBinding> {
268 AngleBracketedParameters(ref data) => {
269 data.bindings.iter().collect()
271 ParenthesizedParameters(_) => {
278 /// A path like `Foo<'a, T>`
279 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
280 pub struct AngleBracketedParameterData {
281 /// The lifetime parameters for this path segment.
282 pub lifetimes: HirVec<Lifetime>,
283 /// The type parameters for this path segment, if present.
284 pub types: HirVec<P<Ty>>,
285 /// Bindings (equality constraints) on associated types, if present.
286 /// E.g., `Foo<A=Bar>`.
287 pub bindings: HirVec<TypeBinding>,
290 impl AngleBracketedParameterData {
291 fn is_empty(&self) -> bool {
292 self.lifetimes.is_empty() && self.types.is_empty() && self.bindings.is_empty()
296 /// A path like `Foo(A,B) -> C`
297 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
298 pub struct ParenthesizedParameterData {
303 pub inputs: HirVec<P<Ty>>,
306 pub output: Option<P<Ty>>,
309 /// The AST represents all type param bounds as types.
310 /// typeck::collect::compute_bounds matches these against
311 /// the "special" built-in traits (see middle::lang_items) and
312 /// detects Copy, Send and Sync.
313 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
314 pub enum TyParamBound {
315 TraitTyParamBound(PolyTraitRef, TraitBoundModifier),
316 RegionTyParamBound(Lifetime),
319 /// A modifier on a bound, currently this is only used for `?Sized`, where the
320 /// modifier is `Maybe`. Negative bounds should also be handled here.
321 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
322 pub enum TraitBoundModifier {
327 pub type TyParamBounds = HirVec<TyParamBound>;
329 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
333 pub bounds: TyParamBounds,
334 pub default: Option<P<Ty>>,
338 /// Represents lifetimes and type parameters attached to a declaration
339 /// of a function, enum, trait, etc.
340 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
341 pub struct Generics {
342 pub lifetimes: HirVec<LifetimeDef>,
343 pub ty_params: HirVec<TyParam>,
344 pub where_clause: WhereClause,
348 pub fn is_lt_parameterized(&self) -> bool {
349 !self.lifetimes.is_empty()
351 pub fn is_type_parameterized(&self) -> bool {
352 !self.ty_params.is_empty()
354 pub fn is_parameterized(&self) -> bool {
355 self.is_lt_parameterized() || self.is_type_parameterized()
359 /// A `where` clause in a definition
360 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
361 pub struct WhereClause {
363 pub predicates: HirVec<WherePredicate>,
366 /// A single predicate in a `where` clause
367 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
368 pub enum WherePredicate {
369 /// A type binding, eg `for<'c> Foo: Send+Clone+'c`
370 BoundPredicate(WhereBoundPredicate),
371 /// A lifetime predicate, e.g. `'a: 'b+'c`
372 RegionPredicate(WhereRegionPredicate),
373 /// An equality predicate (unsupported)
374 EqPredicate(WhereEqPredicate),
377 /// A type bound, eg `for<'c> Foo: Send+Clone+'c`
378 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
379 pub struct WhereBoundPredicate {
381 /// Any lifetimes from a `for` binding
382 pub bound_lifetimes: HirVec<LifetimeDef>,
383 /// The type being bounded
384 pub bounded_ty: P<Ty>,
385 /// Trait and lifetime bounds (`Clone+Send+'static`)
386 pub bounds: TyParamBounds,
389 /// A lifetime predicate, e.g. `'a: 'b+'c`
390 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
391 pub struct WhereRegionPredicate {
393 pub lifetime: Lifetime,
394 pub bounds: HirVec<Lifetime>,
397 /// An equality predicate (unsupported), e.g. `T=int`
398 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
399 pub struct WhereEqPredicate {
406 pub type CrateConfig = HirVec<P<MetaItem>>;
408 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
411 pub attrs: HirVec<Attribute>,
412 pub config: CrateConfig,
414 pub exported_macros: HirVec<MacroDef>,
416 // NB: We use a BTreeMap here so that `visit_all_items` iterates
417 // over the ids in increasing order. In principle it should not
418 // matter what order we visit things in, but in *practice* it
419 // does, because it can affect the order in which errors are
420 // detected, which in turn can make compile-fail tests yield
421 // slightly different results.
422 pub items: BTreeMap<NodeId, Item>,
426 pub fn item(&self, id: NodeId) -> &Item {
430 /// Visits all items in the crate in some determinstic (but
431 /// unspecified) order. If you just need to process every item,
432 /// but don't care about nesting, this method is the best choice.
434 /// If you do care about nesting -- usually because your algorithm
435 /// follows lexical scoping rules -- then you want a different
436 /// approach. You should override `visit_nested_item` in your
437 /// visitor and then call `intravisit::walk_crate` instead.
438 pub fn visit_all_items<'hir, V:Visitor<'hir>>(&'hir self, visitor: &mut V) {
439 for (_, item) in &self.items {
440 visitor.visit_item(item);
445 /// A macro definition, in this crate or imported from another.
447 /// Not parsed directly, but created on macro import or `macro_rules!` expansion.
448 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
449 pub struct MacroDef {
451 pub attrs: HirVec<Attribute>,
454 pub imported_from: Option<Name>,
456 pub use_locally: bool,
457 pub allow_internal_unstable: bool,
458 pub body: HirVec<TokenTree>,
461 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
463 /// Statements in a block
464 pub stmts: HirVec<Stmt>,
465 /// An expression at the end of the block
466 /// without a semicolon, if any
467 pub expr: Option<P<Expr>>,
469 /// Distinguishes between `unsafe { ... }` and `{ ... }`
470 pub rules: BlockCheckMode,
474 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
481 impl fmt::Debug for Pat {
482 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
483 write!(f, "pat({}: {})", self.id, pprust::pat_to_string(self))
487 /// A single field in a struct pattern
489 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
490 /// are treated the same as` x: x, y: ref y, z: ref mut z`,
491 /// except is_shorthand is true
492 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
493 pub struct FieldPat {
494 /// The identifier for the field
496 /// The pattern the field is destructured to
498 pub is_shorthand: bool,
501 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
502 pub enum BindingMode {
503 BindByRef(Mutability),
504 BindByValue(Mutability),
507 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
509 /// Represents a wildcard pattern (`_`)
512 /// A `PatKind::Ident` may either be a new bound variable,
513 /// or a unit struct/variant pattern, or a const pattern (in the last two cases
514 /// the third field must be `None`).
516 /// In the unit or const pattern case, the parser can't determine
517 /// which it is. The resolver determines this, and
518 /// records this pattern's `NodeId` in an auxiliary
519 /// set (of "PatIdents that refer to unit patterns or constants").
520 Ident(BindingMode, Spanned<Ident>, Option<P<Pat>>),
522 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
523 /// The `bool` is `true` in the presence of a `..`.
524 Struct(Path, HirVec<Spanned<FieldPat>>, bool),
526 /// A tuple struct/variant pattern `Variant(x, y, z)`.
527 /// "None" means a `Variant(..)` pattern where we don't bind the fields to names.
528 TupleStruct(Path, Option<HirVec<P<Pat>>>),
531 /// Such pattern can be resolved to a unit struct/variant or a constant.
534 /// An associated const named using the qualified path `<T>::CONST` or
535 /// `<T as Trait>::CONST`. Associated consts from inherent impls can be
536 /// referred to as simply `T::CONST`, in which case they will end up as
537 /// PatKind::Path, and the resolver will have to sort that out.
540 /// A tuple pattern `(a, b)`
544 /// A reference pattern, e.g. `&mut (a, b)`
545 Ref(P<Pat>, Mutability),
548 /// A range pattern, e.g. `1...2`
549 Range(P<Expr>, P<Expr>),
550 /// `[a, b, ..i, y, z]` is represented as:
551 /// `PatKind::Vec(box [a, b], Some(i), box [y, z])`
552 Vec(HirVec<P<Pat>>, Option<P<Pat>>, HirVec<P<Pat>>),
555 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
556 pub enum Mutability {
561 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
563 /// The `+` operator (addition)
565 /// The `-` operator (subtraction)
567 /// The `*` operator (multiplication)
569 /// The `/` operator (division)
571 /// The `%` operator (modulus)
573 /// The `&&` operator (logical and)
575 /// The `||` operator (logical or)
577 /// The `^` operator (bitwise xor)
579 /// The `&` operator (bitwise and)
581 /// The `|` operator (bitwise or)
583 /// The `<<` operator (shift left)
585 /// The `>>` operator (shift right)
587 /// The `==` operator (equality)
589 /// The `<` operator (less than)
591 /// The `<=` operator (less than or equal to)
593 /// The `!=` operator (not equal to)
595 /// The `>=` operator (greater than or equal to)
597 /// The `>` operator (greater than)
601 pub type BinOp = Spanned<BinOp_>;
603 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
605 /// The `*` operator for dereferencing
607 /// The `!` operator for logical inversion
609 /// The `-` operator for negation
614 pub type Stmt = Spanned<Stmt_>;
616 impl fmt::Debug for Stmt_ {
617 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
619 let spanned = codemap::dummy_spanned(self.clone());
622 util::stmt_id(&spanned),
623 pprust::stmt_to_string(&spanned))
627 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
629 /// Could be an item or a local (let) binding:
630 StmtDecl(P<Decl>, NodeId),
632 /// Expr without trailing semi-colon (must have unit type):
633 StmtExpr(P<Expr>, NodeId),
635 /// Expr with trailing semi-colon (may have any type):
636 StmtSemi(P<Expr>, NodeId),
639 // FIXME (pending discussion of #1697, #2178...): local should really be
640 // a refinement on pat.
641 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
642 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
645 pub ty: Option<P<Ty>>,
646 /// Initializer expression to set the value, if any
647 pub init: Option<P<Expr>>,
650 pub attrs: ThinAttributes,
653 pub type Decl = Spanned<Decl_>;
655 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
657 /// A local (let) binding:
663 /// represents one arm of a 'match'
664 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
666 pub attrs: HirVec<Attribute>,
667 pub pats: HirVec<P<Pat>>,
668 pub guard: Option<P<Expr>>,
672 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
674 pub name: Spanned<Name>,
679 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
680 pub enum BlockCheckMode {
682 UnsafeBlock(UnsafeSource),
683 PushUnsafeBlock(UnsafeSource),
684 PopUnsafeBlock(UnsafeSource),
685 // Within this block (but outside a PopUnstableBlock), we suspend checking of stability.
690 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
691 pub enum UnsafeSource {
697 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
702 pub attrs: ThinAttributes,
705 impl fmt::Debug for Expr {
706 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
707 write!(f, "expr({}: {})", self.id, pprust::expr_to_string(self))
711 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
713 /// A `box x` expression.
715 /// An array (`[a, b, c, d]`)
716 ExprVec(HirVec<P<Expr>>),
719 /// The first field resolves to the function itself,
720 /// and the second field is the list of arguments
721 ExprCall(P<Expr>, HirVec<P<Expr>>),
722 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
724 /// The `Spanned<Name>` is the identifier for the method name.
725 /// The vector of `Ty`s are the ascripted type parameters for the method
726 /// (within the angle brackets).
728 /// The first element of the vector of `Expr`s is the expression that evaluates
729 /// to the object on which the method is being called on (the receiver),
730 /// and the remaining elements are the rest of the arguments.
732 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
733 /// `ExprMethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
734 ExprMethodCall(Spanned<Name>, HirVec<P<Ty>>, HirVec<P<Expr>>),
735 /// A tuple (`(a, b, c ,d)`)
736 ExprTup(HirVec<P<Expr>>),
737 /// A binary operation (For example: `a + b`, `a * b`)
738 ExprBinary(BinOp, P<Expr>, P<Expr>),
739 /// A unary operation (For example: `!x`, `*x`)
740 ExprUnary(UnOp, P<Expr>),
741 /// A literal (For example: `1u8`, `"foo"`)
743 /// A cast (`foo as f64`)
744 ExprCast(P<Expr>, P<Ty>),
745 ExprType(P<Expr>, P<Ty>),
746 /// An `if` block, with an optional else block
748 /// `if expr { block } else { expr }`
749 ExprIf(P<Expr>, P<Block>, Option<P<Expr>>),
750 /// A while loop, with an optional label
752 /// `'label: while expr { block }`
753 ExprWhile(P<Expr>, P<Block>, Option<Ident>),
754 /// Conditionless loop (can be exited with break, continue, or return)
756 /// `'label: loop { block }`
757 ExprLoop(P<Block>, Option<Ident>),
758 /// A `match` block, with a source that indicates whether or not it is
759 /// the result of a desugaring, and if so, which kind.
760 ExprMatch(P<Expr>, HirVec<Arm>, MatchSource),
761 /// A closure (for example, `move |a, b, c| {a + b + c}`)
762 ExprClosure(CaptureClause, P<FnDecl>, P<Block>),
763 /// A block (`{ ... }`)
766 /// An assignment (`a = foo()`)
767 ExprAssign(P<Expr>, P<Expr>),
768 /// An assignment with an operator
770 /// For example, `a += 1`.
771 ExprAssignOp(BinOp, P<Expr>, P<Expr>),
772 /// Access of a named struct field (`obj.foo`)
773 ExprField(P<Expr>, Spanned<Name>),
774 /// Access of an unnamed field of a struct or tuple-struct
776 /// For example, `foo.0`.
777 ExprTupField(P<Expr>, Spanned<usize>),
778 /// An indexing operation (`foo[2]`)
779 ExprIndex(P<Expr>, P<Expr>),
781 /// Variable reference, possibly containing `::` and/or type
782 /// parameters, e.g. foo::bar::<baz>.
784 /// Optionally "qualified",
785 /// e.g. `<HirVec<T> as SomeTrait>::SomeType`.
786 ExprPath(Option<QSelf>, Path),
788 /// A referencing operation (`&a` or `&mut a`)
789 ExprAddrOf(Mutability, P<Expr>),
790 /// A `break`, with an optional label to break
791 ExprBreak(Option<Spanned<Ident>>),
792 /// A `continue`, with an optional label
793 ExprAgain(Option<Spanned<Ident>>),
794 /// A `return`, with an optional value to be returned
795 ExprRet(Option<P<Expr>>),
797 /// Output of the `asm!()` macro
798 ExprInlineAsm(InlineAsm),
800 /// A struct literal expression.
802 /// For example, `Foo {x: 1, y: 2}`, or
803 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
804 ExprStruct(Path, HirVec<Field>, Option<P<Expr>>),
806 /// A vector literal constructed from one repeated element.
808 /// For example, `[1u8; 5]`. The first expression is the element
809 /// to be repeated; the second is the number of times to repeat it.
810 ExprRepeat(P<Expr>, P<Expr>),
813 /// The explicit Self type in a "qualified path". The actual
814 /// path, including the trait and the associated item, is stored
815 /// separately. `position` represents the index of the associated
816 /// item qualified with this Self type.
818 /// <HirVec<T> as a::b::Trait>::AssociatedItem
819 /// ^~~~~ ~~~~~~~~~~~~~~^
822 /// <HirVec<T>>::AssociatedItem
825 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
831 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
832 pub enum MatchSource {
835 contains_else_clause: bool,
841 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
842 pub enum CaptureClause {
847 // NB: If you change this, you'll probably want to change the corresponding
848 // type structure in middle/ty.rs as well.
849 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
852 pub mutbl: Mutability,
855 /// Represents a method's signature in a trait declaration,
856 /// or in an implementation.
857 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
858 pub struct MethodSig {
859 pub unsafety: Unsafety,
860 pub constness: Constness,
863 pub generics: Generics,
864 pub explicit_self: ExplicitSelf,
867 /// Represents a method declaration in a trait declaration, possibly including
868 /// a default implementation A trait method is either required (meaning it
869 /// doesn't have an implementation, just a signature) or provided (meaning it
870 /// has a default implementation).
871 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
872 pub struct TraitItem {
875 pub attrs: HirVec<Attribute>,
876 pub node: TraitItem_,
880 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
881 pub enum TraitItem_ {
882 ConstTraitItem(P<Ty>, Option<P<Expr>>),
883 MethodTraitItem(MethodSig, Option<P<Block>>),
884 TypeTraitItem(TyParamBounds, Option<P<Ty>>),
887 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
888 pub struct ImplItem {
892 pub attrs: HirVec<Attribute>,
893 pub node: ImplItemKind,
897 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
898 pub enum ImplItemKind {
899 Const(P<Ty>, P<Expr>),
900 Method(MethodSig, P<Block>),
904 // Bind a type to an associated type: `A=Foo`.
905 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
906 pub struct TypeBinding {
914 // NB PartialEq method appears below.
915 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
922 impl fmt::Debug for Ty {
923 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
924 write!(f, "type({})", pprust::ty_to_string(self))
928 /// Not represented directly in the AST, referred to by name through a ty_path.
929 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
939 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
940 pub struct BareFnTy {
941 pub unsafety: Unsafety,
943 pub lifetimes: HirVec<LifetimeDef>,
947 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
948 /// The different kinds of types recognized by the compiler
951 /// A fixed length array (`[T; n]`)
952 TyFixedLengthVec(P<Ty>, P<Expr>),
953 /// A raw pointer (`*const T` or `*mut T`)
955 /// A reference (`&'a T` or `&'a mut T`)
956 TyRptr(Option<Lifetime>, MutTy),
957 /// A bare function (e.g. `fn(usize) -> bool`)
958 TyBareFn(P<BareFnTy>),
959 /// A tuple (`(A, B, C, D,...)`)
960 TyTup(HirVec<P<Ty>>),
961 /// A path (`module::module::...::Type`), optionally
962 /// "qualified", e.g. `<HirVec<T> as SomeTrait>::SomeType`.
964 /// Type parameters are stored in the Path itself
965 TyPath(Option<QSelf>, Path),
966 /// Something like `A+B`. Note that `B` must always be a path.
967 TyObjectSum(P<Ty>, TyParamBounds),
968 /// A type like `for<'a> Foo<&'a Bar>`
969 TyPolyTraitRef(TyParamBounds),
972 /// TyInfer means the type should be inferred instead of it having been
973 /// specified. This can appear anywhere in a type.
977 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
978 pub struct InlineAsmOutput {
979 pub constraint: InternedString,
982 pub is_indirect: bool,
985 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
986 pub struct InlineAsm {
987 pub asm: InternedString,
988 pub asm_str_style: StrStyle,
989 pub outputs: HirVec<InlineAsmOutput>,
990 pub inputs: HirVec<(InternedString, P<Expr>)>,
991 pub clobbers: HirVec<InternedString>,
993 pub alignstack: bool,
994 pub dialect: AsmDialect,
998 /// represents an argument in a function header
999 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1007 pub fn new_self(span: Span, mutability: Mutability, self_ident: Ident) -> Arg {
1008 let path = Spanned {
1013 // HACK(eddyb) fake type for the self argument.
1021 node: PatKind::Ident(BindByValue(mutability), path, None),
1029 /// Represents the header (not the body) of a function declaration
1030 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1032 pub inputs: HirVec<Arg>,
1033 pub output: FunctionRetTy,
1037 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1043 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1044 pub enum Constness {
1049 impl fmt::Display for Unsafety {
1050 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1051 fmt::Display::fmt(match *self {
1052 Unsafety::Normal => "normal",
1053 Unsafety::Unsafe => "unsafe",
1059 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
1060 pub enum ImplPolarity {
1061 /// `impl Trait for Type`
1063 /// `impl !Trait for Type`
1067 impl fmt::Debug for ImplPolarity {
1068 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1070 ImplPolarity::Positive => "positive".fmt(f),
1071 ImplPolarity::Negative => "negative".fmt(f),
1077 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1078 pub enum FunctionRetTy {
1079 /// Functions with return type `!`that always
1080 /// raise an error or exit (i.e. never return to the caller)
1082 /// Return type is not specified.
1084 /// Functions default to `()` and
1085 /// closures default to inference. Span points to where return
1086 /// type would be inserted.
1087 DefaultReturn(Span),
1092 impl FunctionRetTy {
1093 pub fn span(&self) -> Span {
1095 NoReturn(span) => span,
1096 DefaultReturn(span) => span,
1097 Return(ref ty) => ty.span,
1102 /// Represents the kind of 'self' associated with a method
1103 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1104 pub enum ExplicitSelf_ {
1109 /// `&'lt self`, `&'lt mut self`
1110 SelfRegion(Option<Lifetime>, Mutability, Name),
1112 SelfExplicit(P<Ty>, Name),
1115 pub type ExplicitSelf = Spanned<ExplicitSelf_>;
1117 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1119 /// A span from the first token past `{` to the last token until `}`.
1120 /// For `mod foo;`, the inner span ranges from the first token
1121 /// to the last token in the external file.
1123 pub item_ids: HirVec<ItemId>,
1126 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1127 pub struct ForeignMod {
1129 pub items: HirVec<ForeignItem>,
1132 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1133 pub struct EnumDef {
1134 pub variants: HirVec<Variant>,
1137 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1138 pub struct Variant_ {
1140 pub attrs: HirVec<Attribute>,
1141 pub data: VariantData,
1142 /// Explicit discriminant, eg `Foo = 1`
1143 pub disr_expr: Option<P<Expr>>,
1146 pub type Variant = Spanned<Variant_>;
1148 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1149 pub enum PathListItem_ {
1152 /// renamed in list, eg `use foo::{bar as baz};`
1153 rename: Option<Name>,
1157 /// renamed in list, eg `use foo::{self as baz};`
1158 rename: Option<Name>,
1163 impl PathListItem_ {
1164 pub fn id(&self) -> NodeId {
1166 PathListIdent { id, .. } | PathListMod { id, .. } => id,
1170 pub fn name(&self) -> Option<Name> {
1172 PathListIdent { name, .. } => Some(name),
1173 PathListMod { .. } => None,
1177 pub fn rename(&self) -> Option<Name> {
1179 PathListIdent { rename, .. } | PathListMod { rename, .. } => rename,
1184 pub type PathListItem = Spanned<PathListItem_>;
1186 pub type ViewPath = Spanned<ViewPath_>;
1188 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1189 pub enum ViewPath_ {
1190 /// `foo::bar::baz as quux`
1194 /// `foo::bar::baz` (with `as baz` implicitly on the right)
1195 ViewPathSimple(Name, Path),
1200 /// `foo::bar::{a,b,c}`
1201 ViewPathList(Path, HirVec<PathListItem>),
1204 /// TraitRef's appear in impls.
1206 /// resolve maps each TraitRef's ref_id to its defining trait; that's all
1207 /// that the ref_id is for. The impl_id maps to the "self type" of this impl.
1208 /// If this impl is an ItemImpl, the impl_id is redundant (it could be the
1209 /// same as the impl's node id).
1210 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1211 pub struct TraitRef {
1216 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1217 pub struct PolyTraitRef {
1218 /// The `'a` in `<'a> Foo<&'a T>`
1219 pub bound_lifetimes: HirVec<LifetimeDef>,
1221 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
1222 pub trait_ref: TraitRef,
1227 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1228 pub enum Visibility {
1234 pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility {
1236 &Inherited => parent_visibility,
1242 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1243 pub struct StructField_ {
1244 pub kind: StructFieldKind,
1247 pub attrs: HirVec<Attribute>,
1251 pub fn name(&self) -> Option<Name> {
1253 NamedField(name, _) => Some(name),
1254 UnnamedField(_) => None,
1259 pub type StructField = Spanned<StructField_>;
1261 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1262 pub enum StructFieldKind {
1263 NamedField(Name, Visibility),
1264 /// Element of a tuple-like struct
1265 UnnamedField(Visibility),
1268 impl StructFieldKind {
1269 pub fn is_unnamed(&self) -> bool {
1271 UnnamedField(..) => true,
1272 NamedField(..) => false,
1276 pub fn visibility(&self) -> Visibility {
1278 NamedField(_, vis) | UnnamedField(vis) => vis,
1283 /// Fields and Ids of enum variants and structs
1285 /// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all
1286 /// variant kinds) and an Id of the variant's constructor (not relevant for `Struct`-variants).
1287 /// One shared Id can be successfully used for these two purposes.
1288 /// Id of the whole enum lives in `Item`.
1290 /// For structs: `NodeId` represents an Id of the structure's constructor, so it is not actually
1291 /// used for `Struct`-structs (but still presents). Structures don't have an analogue of "Id of
1292 /// the variant itself" from enum variants.
1293 /// Id of the whole struct lives in `Item`.
1294 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1295 pub enum VariantData {
1296 Struct(HirVec<StructField>, NodeId),
1297 Tuple(HirVec<StructField>, NodeId),
1302 pub fn fields(&self) -> &[StructField] {
1304 VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => fields,
1308 pub fn id(&self) -> NodeId {
1310 VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id,
1313 pub fn is_struct(&self) -> bool {
1314 if let VariantData::Struct(..) = *self {
1320 pub fn is_tuple(&self) -> bool {
1321 if let VariantData::Tuple(..) = *self {
1327 pub fn is_unit(&self) -> bool {
1328 if let VariantData::Unit(..) = *self {
1336 // The bodies for items are stored "out of line", in a separate
1337 // hashmap in the `Crate`. Here we just record the node-id of the item
1338 // so it can fetched later.
1339 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1344 // FIXME (#3300): Should allow items to be anonymous. Right now
1345 // we just use dummy names for anon items.
1348 /// The name might be a dummy name in case of anonymous items
1349 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1352 pub attrs: HirVec<Attribute>,
1355 pub vis: Visibility,
1359 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1361 /// An`extern crate` item, with optional original crate name,
1363 /// e.g. `extern crate foo` or `extern crate foo_bar as foo`
1364 ItemExternCrate(Option<Name>),
1365 /// A `use` or `pub use` item
1366 ItemUse(P<ViewPath>),
1369 ItemStatic(P<Ty>, Mutability, P<Expr>),
1371 ItemConst(P<Ty>, P<Expr>),
1372 /// A function declaration
1373 ItemFn(P<FnDecl>, Unsafety, Constness, Abi, Generics, P<Block>),
1376 /// An external module
1377 ItemForeignMod(ForeignMod),
1378 /// A type alias, e.g. `type Foo = Bar<u8>`
1379 ItemTy(P<Ty>, Generics),
1380 /// An enum definition, e.g. `enum Foo<A, B> {C<A>, D<B>}`
1381 ItemEnum(EnumDef, Generics),
1382 /// A struct definition, e.g. `struct Foo<A> {x: A}`
1383 ItemStruct(VariantData, Generics),
1384 /// Represents a Trait Declaration
1385 ItemTrait(Unsafety, Generics, TyParamBounds, HirVec<TraitItem>),
1387 // Default trait implementations
1389 /// `impl Trait for .. {}`
1390 ItemDefaultImpl(Unsafety, TraitRef),
1391 /// An implementation, eg `impl<A> Trait for Foo { .. }`
1395 Option<TraitRef>, // (optional) trait this impl implements
1401 pub fn descriptive_variant(&self) -> &str {
1403 ItemExternCrate(..) => "extern crate",
1404 ItemUse(..) => "use",
1405 ItemStatic(..) => "static item",
1406 ItemConst(..) => "constant item",
1407 ItemFn(..) => "function",
1408 ItemMod(..) => "module",
1409 ItemForeignMod(..) => "foreign module",
1410 ItemTy(..) => "type alias",
1411 ItemEnum(..) => "enum",
1412 ItemStruct(..) => "struct",
1413 ItemTrait(..) => "trait",
1415 ItemDefaultImpl(..) => "item",
1420 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1421 pub struct ForeignItem {
1423 pub attrs: HirVec<Attribute>,
1424 pub node: ForeignItem_,
1427 pub vis: Visibility,
1430 /// An item within an `extern` block
1431 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1432 pub enum ForeignItem_ {
1433 /// A foreign function
1434 ForeignItemFn(P<FnDecl>, Generics),
1435 /// A foreign static item (`static ext: u8`), with optional mutability
1436 /// (the boolean is true when mutable)
1437 ForeignItemStatic(P<Ty>, bool),
1441 pub fn descriptive_variant(&self) -> &str {
1443 ForeignItemFn(..) => "foreign function",
1444 ForeignItemStatic(..) => "foreign static item",