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::Pat_::*;
25 pub use self::PathListItem_::*;
26 pub use self::PrimTy::*;
27 pub use self::Stmt_::*;
28 pub use self::StructFieldKind::*;
29 pub use self::TraitItem_::*;
31 pub use self::TyParamBound::*;
32 pub use self::UnOp::*;
33 pub use self::UnsafeSource::*;
34 pub use self::ViewPath_::*;
35 pub use self::Visibility::*;
36 pub use self::PathParameters::*;
38 use intravisit::Visitor;
39 use rustc_data_structures::fnv::FnvHashMap;
40 use syntax::codemap::{self, Span, Spanned, DUMMY_SP, ExpnId};
42 use syntax::ast::{Name, Ident, NodeId, DUMMY_NODE_ID, TokenTree, AsmDialect};
43 use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, CrateConfig};
44 use syntax::owned_slice::OwnedSlice;
45 use syntax::parse::token::InternedString;
52 use serialize::{Encodable, Encoder, Decoder};
54 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
61 impl fmt::Debug for Lifetime {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 pprust::lifetime_to_string(self))
70 /// A lifetime definition, eg `'a: 'b+'c+'d`
71 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
72 pub struct LifetimeDef {
73 pub lifetime: Lifetime,
74 pub bounds: Vec<Lifetime>,
77 /// A "Path" is essentially Rust's notion of a name; for instance:
78 /// std::cmp::PartialEq . It's represented as a sequence of identifiers,
79 /// along with a bunch of supporting information.
80 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
83 /// A `::foo` path, is relative to the crate root rather than current
84 /// module (like paths in an import).
86 /// The segments in the path: the things separated by `::`.
87 pub segments: Vec<PathSegment>,
90 impl fmt::Debug for Path {
91 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92 write!(f, "path({})", pprust::path_to_string(self))
96 impl fmt::Display for Path {
97 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98 write!(f, "{}", pprust::path_to_string(self))
102 /// A segment of a path: an identifier, an optional lifetime, and a set of
104 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
105 pub struct PathSegment {
106 /// The identifier portion of this path segment.
107 pub identifier: Ident,
109 /// Type/lifetime parameters attached to this path. They come in
110 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
111 /// this is more than just simple syntactic sugar; the use of
112 /// parens affects the region binding rules, so we preserve the
114 pub parameters: PathParameters,
117 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
118 pub enum PathParameters {
119 /// The `<'a, A,B,C>` in `foo::bar::baz::<'a, A,B,C>`
120 AngleBracketedParameters(AngleBracketedParameterData),
121 /// The `(A,B)` and `C` in `Foo(A,B) -> C`
122 ParenthesizedParameters(ParenthesizedParameterData),
125 impl PathParameters {
126 pub fn none() -> PathParameters {
127 AngleBracketedParameters(AngleBracketedParameterData {
128 lifetimes: Vec::new(),
129 types: OwnedSlice::empty(),
130 bindings: OwnedSlice::empty(),
134 pub fn is_empty(&self) -> bool {
136 AngleBracketedParameters(ref data) => data.is_empty(),
138 // Even if the user supplied no types, something like
139 // `X()` is equivalent to `X<(),()>`.
140 ParenthesizedParameters(..) => false,
144 pub fn has_lifetimes(&self) -> bool {
146 AngleBracketedParameters(ref data) => !data.lifetimes.is_empty(),
147 ParenthesizedParameters(_) => false,
151 pub fn has_types(&self) -> bool {
153 AngleBracketedParameters(ref data) => !data.types.is_empty(),
154 ParenthesizedParameters(..) => true,
158 /// Returns the types that the user wrote. Note that these do not necessarily map to the type
159 /// parameters in the parenthesized case.
160 pub fn types(&self) -> Vec<&P<Ty>> {
162 AngleBracketedParameters(ref data) => {
163 data.types.iter().collect()
165 ParenthesizedParameters(ref data) => {
168 .chain(data.output.iter())
174 pub fn lifetimes(&self) -> Vec<&Lifetime> {
176 AngleBracketedParameters(ref data) => {
177 data.lifetimes.iter().collect()
179 ParenthesizedParameters(_) => {
185 pub fn bindings(&self) -> Vec<&P<TypeBinding>> {
187 AngleBracketedParameters(ref data) => {
188 data.bindings.iter().collect()
190 ParenthesizedParameters(_) => {
197 /// A path like `Foo<'a, T>`
198 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
199 pub struct AngleBracketedParameterData {
200 /// The lifetime parameters for this path segment.
201 pub lifetimes: Vec<Lifetime>,
202 /// The type parameters for this path segment, if present.
203 pub types: OwnedSlice<P<Ty>>,
204 /// Bindings (equality constraints) on associated types, if present.
205 /// E.g., `Foo<A=Bar>`.
206 pub bindings: OwnedSlice<P<TypeBinding>>,
209 impl AngleBracketedParameterData {
210 fn is_empty(&self) -> bool {
211 self.lifetimes.is_empty() && self.types.is_empty() && self.bindings.is_empty()
215 /// A path like `Foo(A,B) -> C`
216 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
217 pub struct ParenthesizedParameterData {
222 pub inputs: Vec<P<Ty>>,
225 pub output: Option<P<Ty>>,
228 /// The AST represents all type param bounds as types.
229 /// typeck::collect::compute_bounds matches these against
230 /// the "special" built-in traits (see middle::lang_items) and
231 /// detects Copy, Send and Sync.
232 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
233 pub enum TyParamBound {
234 TraitTyParamBound(PolyTraitRef, TraitBoundModifier),
235 RegionTyParamBound(Lifetime),
238 /// A modifier on a bound, currently this is only used for `?Sized`, where the
239 /// modifier is `Maybe`. Negative bounds should also be handled here.
240 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
241 pub enum TraitBoundModifier {
246 pub type TyParamBounds = OwnedSlice<TyParamBound>;
248 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
252 pub bounds: TyParamBounds,
253 pub default: Option<P<Ty>>,
257 /// Represents lifetimes and type parameters attached to a declaration
258 /// of a function, enum, trait, etc.
259 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
260 pub struct Generics {
261 pub lifetimes: Vec<LifetimeDef>,
262 pub ty_params: OwnedSlice<TyParam>,
263 pub where_clause: WhereClause,
267 pub fn is_lt_parameterized(&self) -> bool {
268 !self.lifetimes.is_empty()
270 pub fn is_type_parameterized(&self) -> bool {
271 !self.ty_params.is_empty()
273 pub fn is_parameterized(&self) -> bool {
274 self.is_lt_parameterized() || self.is_type_parameterized()
278 /// A `where` clause in a definition
279 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
280 pub struct WhereClause {
282 pub predicates: Vec<WherePredicate>,
285 /// A single predicate in a `where` clause
286 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
287 pub enum WherePredicate {
288 /// A type binding, eg `for<'c> Foo: Send+Clone+'c`
289 BoundPredicate(WhereBoundPredicate),
290 /// A lifetime predicate, e.g. `'a: 'b+'c`
291 RegionPredicate(WhereRegionPredicate),
292 /// An equality predicate (unsupported)
293 EqPredicate(WhereEqPredicate),
296 /// A type bound, eg `for<'c> Foo: Send+Clone+'c`
297 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
298 pub struct WhereBoundPredicate {
300 /// Any lifetimes from a `for` binding
301 pub bound_lifetimes: Vec<LifetimeDef>,
302 /// The type being bounded
303 pub bounded_ty: P<Ty>,
304 /// Trait and lifetime bounds (`Clone+Send+'static`)
305 pub bounds: OwnedSlice<TyParamBound>,
308 /// A lifetime predicate, e.g. `'a: 'b+'c`
309 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
310 pub struct WhereRegionPredicate {
312 pub lifetime: Lifetime,
313 pub bounds: Vec<Lifetime>,
316 /// An equality predicate (unsupported), e.g. `T=int`
317 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
318 pub struct WhereEqPredicate {
325 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
328 pub attrs: Vec<Attribute>,
329 pub config: CrateConfig,
331 pub exported_macros: Vec<MacroDef>,
332 pub items: FnvHashMap<NodeId, Item>,
336 pub fn item(&self, id: NodeId) -> &Item {
340 /// Visits all items in the crate in some determinstic (but
341 /// unspecified) order. If you just need to process every item,
342 /// but don't care about nesting, this method is the best choice.
344 /// If you do care about nesting -- usually because your algorithm
345 /// follows lexical scoping rules -- then you want a different
346 /// approach. You should override `visit_nested_item` in your
347 /// visitor and then call `intravisit::walk_crate` instead.
348 pub fn visit_all_items<'hir, V:Visitor<'hir>>(&'hir self, visitor: &mut V) {
349 // In principle, we could just iterate over the hashmap, but
350 // in practice that makes the order of error reporting vary
351 // with small changes in the input etc etc, which makes the
352 // test base hard to maintain. So instead we sort by node-id
353 // so as to get reproducible results.
354 let mut pairs: Vec<_> = self.items.iter().collect();
355 pairs.sort_by(|&(id1, _), &(id2, _)| id1.cmp(id2));
357 for (_, item) in pairs {
358 visitor.visit_item(item);
363 /// A macro definition, in this crate or imported from another.
365 /// Not parsed directly, but created on macro import or `macro_rules!` expansion.
366 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
367 pub struct MacroDef {
369 pub attrs: Vec<Attribute>,
372 pub imported_from: Option<Name>,
374 pub use_locally: bool,
375 pub allow_internal_unstable: bool,
376 pub body: Vec<TokenTree>,
379 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
381 /// Statements in a block
382 pub stmts: Vec<P<Stmt>>,
383 /// An expression at the end of the block
384 /// without a semicolon, if any
385 pub expr: Option<P<Expr>>,
387 /// Distinguishes between `unsafe { ... }` and `{ ... }`
388 pub rules: BlockCheckMode,
392 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
399 impl fmt::Debug for Pat {
400 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
401 write!(f, "pat({}: {})", self.id, pprust::pat_to_string(self))
405 /// A single field in a struct pattern
407 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
408 /// are treated the same as` x: x, y: ref y, z: ref mut z`,
409 /// except is_shorthand is true
410 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
411 pub struct FieldPat {
412 /// The identifier for the field
414 /// The pattern the field is destructured to
416 pub is_shorthand: bool,
419 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
420 pub enum BindingMode {
421 BindByRef(Mutability),
422 BindByValue(Mutability),
425 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
427 /// Represents a wildcard pattern (`_`)
430 /// A PatIdent may either be a new bound variable,
431 /// or a nullary enum (in which case the third field
434 /// In the nullary enum case, the parser can't determine
435 /// which it is. The resolver determines this, and
436 /// records this pattern's NodeId in an auxiliary
437 /// set (of "PatIdents that refer to nullary enums")
438 PatIdent(BindingMode, Spanned<Ident>, Option<P<Pat>>),
440 /// "None" means a `Variant(..)` pattern where we don't bind the fields to names.
441 PatEnum(Path, Option<Vec<P<Pat>>>),
443 /// An associated const named using the qualified path `<T>::CONST` or
444 /// `<T as Trait>::CONST`. Associated consts from inherent impls can be
445 /// referred to as simply `T::CONST`, in which case they will end up as
446 /// PatEnum, and the resolver will have to sort that out.
447 PatQPath(QSelf, Path),
449 /// Destructuring of a struct, e.g. `Foo {x, y, ..}`
450 /// The `bool` is `true` in the presence of a `..`
451 PatStruct(Path, Vec<Spanned<FieldPat>>, bool),
452 /// A tuple pattern `(a, b)`
456 /// A reference pattern, e.g. `&mut (a, b)`
457 PatRegion(P<Pat>, Mutability),
460 /// A range pattern, e.g. `1...2`
461 PatRange(P<Expr>, P<Expr>),
462 /// `[a, b, ..i, y, z]` is represented as:
463 /// `PatVec(box [a, b], Some(i), box [y, z])`
464 PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
467 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
468 pub enum Mutability {
473 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
475 /// The `+` operator (addition)
477 /// The `-` operator (subtraction)
479 /// The `*` operator (multiplication)
481 /// The `/` operator (division)
483 /// The `%` operator (modulus)
485 /// The `&&` operator (logical and)
487 /// The `||` operator (logical or)
489 /// The `^` operator (bitwise xor)
491 /// The `&` operator (bitwise and)
493 /// The `|` operator (bitwise or)
495 /// The `<<` operator (shift left)
497 /// The `>>` operator (shift right)
499 /// The `==` operator (equality)
501 /// The `<` operator (less than)
503 /// The `<=` operator (less than or equal to)
505 /// The `!=` operator (not equal to)
507 /// The `>=` operator (greater than or equal to)
509 /// The `>` operator (greater than)
513 pub type BinOp = Spanned<BinOp_>;
515 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
517 /// The `*` operator for dereferencing
519 /// The `!` operator for logical inversion
521 /// The `-` operator for negation
526 pub type Stmt = Spanned<Stmt_>;
528 impl fmt::Debug for Stmt_ {
529 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
531 let spanned = codemap::dummy_spanned(self.clone());
534 util::stmt_id(&spanned),
535 pprust::stmt_to_string(&spanned))
539 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
541 /// Could be an item or a local (let) binding:
542 StmtDecl(P<Decl>, NodeId),
544 /// Expr without trailing semi-colon (must have unit type):
545 StmtExpr(P<Expr>, NodeId),
547 /// Expr with trailing semi-colon (may have any type):
548 StmtSemi(P<Expr>, NodeId),
551 // FIXME (pending discussion of #1697, #2178...): local should really be
552 // a refinement on pat.
553 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
554 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
557 pub ty: Option<P<Ty>>,
558 /// Initializer expression to set the value, if any
559 pub init: Option<P<Expr>>,
564 pub type Decl = Spanned<Decl_>;
566 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
568 /// A local (let) binding:
574 /// represents one arm of a 'match'
575 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
577 pub attrs: Vec<Attribute>,
578 pub pats: Vec<P<Pat>>,
579 pub guard: Option<P<Expr>>,
583 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
585 pub name: Spanned<Name>,
590 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
591 pub enum BlockCheckMode {
593 UnsafeBlock(UnsafeSource),
594 PushUnsafeBlock(UnsafeSource),
595 PopUnsafeBlock(UnsafeSource),
596 // Within this block (but outside a PopUnstableBlock), we suspend checking of stability.
601 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
602 pub enum UnsafeSource {
608 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
615 impl fmt::Debug for Expr {
616 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
617 write!(f, "expr({}: {})", self.id, pprust::expr_to_string(self))
621 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
623 /// A `box x` expression.
625 /// An array (`[a, b, c, d]`)
626 ExprVec(Vec<P<Expr>>),
629 /// The first field resolves to the function itself,
630 /// and the second field is the list of arguments
631 ExprCall(P<Expr>, Vec<P<Expr>>),
632 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
634 /// The `Spanned<Name>` is the identifier for the method name.
635 /// The vector of `Ty`s are the ascripted type parameters for the method
636 /// (within the angle brackets).
638 /// The first element of the vector of `Expr`s is the expression that evaluates
639 /// to the object on which the method is being called on (the receiver),
640 /// and the remaining elements are the rest of the arguments.
642 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
643 /// `ExprMethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
644 ExprMethodCall(Spanned<Name>, Vec<P<Ty>>, Vec<P<Expr>>),
645 /// A tuple (`(a, b, c ,d)`)
646 ExprTup(Vec<P<Expr>>),
647 /// A binary operation (For example: `a + b`, `a * b`)
648 ExprBinary(BinOp, P<Expr>, P<Expr>),
649 /// A unary operation (For example: `!x`, `*x`)
650 ExprUnary(UnOp, P<Expr>),
651 /// A literal (For example: `1u8`, `"foo"`)
653 /// A cast (`foo as f64`)
654 ExprCast(P<Expr>, P<Ty>),
655 /// An `if` block, with an optional else block
657 /// `if expr { block } else { expr }`
658 ExprIf(P<Expr>, P<Block>, Option<P<Expr>>),
659 /// A while loop, with an optional label
661 /// `'label: while expr { block }`
662 ExprWhile(P<Expr>, P<Block>, Option<Ident>),
663 /// Conditionless loop (can be exited with break, continue, or return)
665 /// `'label: loop { block }`
666 ExprLoop(P<Block>, Option<Ident>),
667 /// A `match` block, with a source that indicates whether or not it is
668 /// the result of a desugaring, and if so, which kind.
669 ExprMatch(P<Expr>, Vec<Arm>, MatchSource),
670 /// A closure (for example, `move |a, b, c| {a + b + c}`)
671 ExprClosure(CaptureClause, P<FnDecl>, P<Block>),
672 /// A block (`{ ... }`)
675 /// An assignment (`a = foo()`)
676 ExprAssign(P<Expr>, P<Expr>),
677 /// An assignment with an operator
679 /// For example, `a += 1`.
680 ExprAssignOp(BinOp, P<Expr>, P<Expr>),
681 /// Access of a named struct field (`obj.foo`)
682 ExprField(P<Expr>, Spanned<Name>),
683 /// Access of an unnamed field of a struct or tuple-struct
685 /// For example, `foo.0`.
686 ExprTupField(P<Expr>, Spanned<usize>),
687 /// An indexing operation (`foo[2]`)
688 ExprIndex(P<Expr>, P<Expr>),
689 /// A range (`1..2`, `1..`, or `..2`)
690 ExprRange(Option<P<Expr>>, Option<P<Expr>>),
692 /// Variable reference, possibly containing `::` and/or type
693 /// parameters, e.g. foo::bar::<baz>.
695 /// Optionally "qualified",
696 /// e.g. `<Vec<T> as SomeTrait>::SomeType`.
697 ExprPath(Option<QSelf>, Path),
699 /// A referencing operation (`&a` or `&mut a`)
700 ExprAddrOf(Mutability, P<Expr>),
701 /// A `break`, with an optional label to break
702 ExprBreak(Option<Spanned<Ident>>),
703 /// A `continue`, with an optional label
704 ExprAgain(Option<Spanned<Ident>>),
705 /// A `return`, with an optional value to be returned
706 ExprRet(Option<P<Expr>>),
708 /// Output of the `asm!()` macro
709 ExprInlineAsm(InlineAsm),
711 /// A struct literal expression.
713 /// For example, `Foo {x: 1, y: 2}`, or
714 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
715 ExprStruct(Path, Vec<Field>, Option<P<Expr>>),
717 /// A vector literal constructed from one repeated element.
719 /// For example, `[1u8; 5]`. The first expression is the element
720 /// to be repeated; the second is the number of times to repeat it.
721 ExprRepeat(P<Expr>, P<Expr>),
724 /// The explicit Self type in a "qualified path". The actual
725 /// path, including the trait and the associated item, is stored
726 /// separately. `position` represents the index of the associated
727 /// item qualified with this Self type.
729 /// <Vec<T> as a::b::Trait>::AssociatedItem
730 /// ^~~~~ ~~~~~~~~~~~~~~^
733 /// <Vec<T>>::AssociatedItem
736 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
742 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
743 pub enum MatchSource {
746 contains_else_clause: bool,
752 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
753 pub enum CaptureClause {
758 // NB: If you change this, you'll probably want to change the corresponding
759 // type structure in middle/ty.rs as well.
760 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
763 pub mutbl: Mutability,
766 /// Represents a method's signature in a trait declaration,
767 /// or in an implementation.
768 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
769 pub struct MethodSig {
770 pub unsafety: Unsafety,
771 pub constness: Constness,
774 pub generics: Generics,
775 pub explicit_self: ExplicitSelf,
778 /// Represents a method declaration in a trait declaration, possibly including
779 /// a default implementation A trait method is either required (meaning it
780 /// doesn't have an implementation, just a signature) or provided (meaning it
781 /// has a default implementation).
782 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
783 pub struct TraitItem {
786 pub attrs: Vec<Attribute>,
787 pub node: TraitItem_,
791 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
792 pub enum TraitItem_ {
793 ConstTraitItem(P<Ty>, Option<P<Expr>>),
794 MethodTraitItem(MethodSig, Option<P<Block>>),
795 TypeTraitItem(TyParamBounds, Option<P<Ty>>),
798 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
799 pub struct ImplItem {
803 pub attrs: Vec<Attribute>,
804 pub node: ImplItemKind,
808 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
809 pub enum ImplItemKind {
810 Const(P<Ty>, P<Expr>),
811 Method(MethodSig, P<Block>),
815 // Bind a type to an associated type: `A=Foo`.
816 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
817 pub struct TypeBinding {
825 // NB PartialEq method appears below.
826 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
833 impl fmt::Debug for Ty {
834 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
835 write!(f, "type({})", pprust::ty_to_string(self))
839 /// Not represented directly in the AST, referred to by name through a ty_path.
840 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
850 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
851 pub struct BareFnTy {
852 pub unsafety: Unsafety,
854 pub lifetimes: Vec<LifetimeDef>,
858 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
859 /// The different kinds of types recognized by the compiler
862 /// A fixed length array (`[T; n]`)
863 TyFixedLengthVec(P<Ty>, P<Expr>),
864 /// A raw pointer (`*const T` or `*mut T`)
866 /// A reference (`&'a T` or `&'a mut T`)
867 TyRptr(Option<Lifetime>, MutTy),
868 /// A bare function (e.g. `fn(usize) -> bool`)
869 TyBareFn(P<BareFnTy>),
870 /// A tuple (`(A, B, C, D,...)`)
872 /// A path (`module::module::...::Type`), optionally
873 /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
875 /// Type parameters are stored in the Path itself
876 TyPath(Option<QSelf>, Path),
877 /// Something like `A+B`. Note that `B` must always be a path.
878 TyObjectSum(P<Ty>, TyParamBounds),
879 /// A type like `for<'a> Foo<&'a Bar>`
880 TyPolyTraitRef(TyParamBounds),
883 /// TyInfer means the type should be inferred instead of it having been
884 /// specified. This can appear anywhere in a type.
888 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
889 pub struct InlineAsm {
890 pub asm: InternedString,
891 pub asm_str_style: StrStyle,
892 pub outputs: Vec<(InternedString, P<Expr>, bool)>,
893 pub inputs: Vec<(InternedString, P<Expr>)>,
894 pub clobbers: Vec<InternedString>,
896 pub alignstack: bool,
897 pub dialect: AsmDialect,
901 /// represents an argument in a function header
902 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
910 pub fn new_self(span: Span, mutability: Mutability, self_ident: Ident) -> Arg {
916 // HACK(eddyb) fake type for the self argument.
924 node: PatIdent(BindByValue(mutability), path, None),
932 /// Represents the header (not the body) of a function declaration
933 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
935 pub inputs: Vec<Arg>,
936 pub output: FunctionRetTy,
940 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
946 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
952 impl fmt::Display for Unsafety {
953 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
954 fmt::Display::fmt(match *self {
955 Unsafety::Normal => "normal",
956 Unsafety::Unsafe => "unsafe",
962 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
963 pub enum ImplPolarity {
964 /// `impl Trait for Type`
966 /// `impl !Trait for Type`
970 impl fmt::Debug for ImplPolarity {
971 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
973 ImplPolarity::Positive => "positive".fmt(f),
974 ImplPolarity::Negative => "negative".fmt(f),
980 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
981 pub enum FunctionRetTy {
982 /// Functions with return type `!`that always
983 /// raise an error or exit (i.e. never return to the caller)
985 /// Return type is not specified.
987 /// Functions default to `()` and
988 /// closures default to inference. Span points to where return
989 /// type would be inserted.
996 pub fn span(&self) -> Span {
998 NoReturn(span) => span,
999 DefaultReturn(span) => span,
1000 Return(ref ty) => ty.span,
1005 /// Represents the kind of 'self' associated with a method
1006 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1007 pub enum ExplicitSelf_ {
1012 /// `&'lt self`, `&'lt mut self`
1013 SelfRegion(Option<Lifetime>, Mutability, Name),
1015 SelfExplicit(P<Ty>, Name),
1018 pub type ExplicitSelf = Spanned<ExplicitSelf_>;
1020 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1022 /// A span from the first token past `{` to the last token until `}`.
1023 /// For `mod foo;`, the inner span ranges from the first token
1024 /// to the last token in the external file.
1026 pub item_ids: Vec<ItemId>,
1029 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1030 pub struct ForeignMod {
1032 pub items: Vec<P<ForeignItem>>,
1035 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1036 pub struct EnumDef {
1037 pub variants: Vec<P<Variant>>,
1040 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1041 pub struct Variant_ {
1043 pub attrs: Vec<Attribute>,
1044 pub data: VariantData,
1045 /// Explicit discriminant, eg `Foo = 1`
1046 pub disr_expr: Option<P<Expr>>,
1049 pub type Variant = Spanned<Variant_>;
1051 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1052 pub enum PathListItem_ {
1055 /// renamed in list, eg `use foo::{bar as baz};`
1056 rename: Option<Name>,
1060 /// renamed in list, eg `use foo::{self as baz};`
1061 rename: Option<Name>,
1066 impl PathListItem_ {
1067 pub fn id(&self) -> NodeId {
1069 PathListIdent { id, .. } | PathListMod { id, .. } => id,
1073 pub fn name(&self) -> Option<Name> {
1075 PathListIdent { name, .. } => Some(name),
1076 PathListMod { .. } => None,
1080 pub fn rename(&self) -> Option<Name> {
1082 PathListIdent { rename, .. } | PathListMod { rename, .. } => rename,
1087 pub type PathListItem = Spanned<PathListItem_>;
1089 pub type ViewPath = Spanned<ViewPath_>;
1091 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1092 pub enum ViewPath_ {
1093 /// `foo::bar::baz as quux`
1097 /// `foo::bar::baz` (with `as baz` implicitly on the right)
1098 ViewPathSimple(Name, Path),
1103 /// `foo::bar::{a,b,c}`
1104 ViewPathList(Path, Vec<PathListItem>),
1107 /// TraitRef's appear in impls.
1109 /// resolve maps each TraitRef's ref_id to its defining trait; that's all
1110 /// that the ref_id is for. The impl_id maps to the "self type" of this impl.
1111 /// If this impl is an ItemImpl, the impl_id is redundant (it could be the
1112 /// same as the impl's node id).
1113 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1114 pub struct TraitRef {
1119 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1120 pub struct PolyTraitRef {
1121 /// The `'a` in `<'a> Foo<&'a T>`
1122 pub bound_lifetimes: Vec<LifetimeDef>,
1124 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
1125 pub trait_ref: TraitRef,
1130 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1131 pub enum Visibility {
1137 pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility {
1139 &Inherited => parent_visibility,
1145 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1146 pub struct StructField_ {
1147 pub kind: StructFieldKind,
1150 pub attrs: Vec<Attribute>,
1154 pub fn name(&self) -> Option<Name> {
1156 NamedField(name, _) => Some(name),
1157 UnnamedField(_) => None,
1162 pub type StructField = Spanned<StructField_>;
1164 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1165 pub enum StructFieldKind {
1166 NamedField(Name, Visibility),
1167 /// Element of a tuple-like struct
1168 UnnamedField(Visibility),
1171 impl StructFieldKind {
1172 pub fn is_unnamed(&self) -> bool {
1174 UnnamedField(..) => true,
1175 NamedField(..) => false,
1179 pub fn visibility(&self) -> Visibility {
1181 NamedField(_, vis) | UnnamedField(vis) => vis,
1186 /// Fields and Ids of enum variants and structs
1188 /// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all
1189 /// variant kinds) and an Id of the variant's constructor (not relevant for `Struct`-variants).
1190 /// One shared Id can be successfully used for these two purposes.
1191 /// Id of the whole enum lives in `Item`.
1193 /// For structs: `NodeId` represents an Id of the structure's constructor, so it is not actually
1194 /// used for `Struct`-structs (but still presents). Structures don't have an analogue of "Id of
1195 /// the variant itself" from enum variants.
1196 /// Id of the whole struct lives in `Item`.
1197 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1198 pub enum VariantData {
1199 Struct(Vec<StructField>, NodeId),
1200 Tuple(Vec<StructField>, NodeId),
1205 pub fn fields(&self) -> &[StructField] {
1207 VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => fields,
1211 pub fn id(&self) -> NodeId {
1213 VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id,
1216 pub fn is_struct(&self) -> bool {
1217 if let VariantData::Struct(..) = *self {
1223 pub fn is_tuple(&self) -> bool {
1224 if let VariantData::Tuple(..) = *self {
1230 pub fn is_unit(&self) -> bool {
1231 if let VariantData::Unit(..) = *self {
1239 // The bodies for items are stored "out of line", in a separate
1240 // hashmap in the `Crate`. Here we just record the node-id of the item
1241 // so it can fetched later.
1242 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1247 // FIXME (#3300): Should allow items to be anonymous. Right now
1248 // we just use dummy names for anon items.
1251 /// The name might be a dummy name in case of anonymous items
1252 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1255 pub attrs: Vec<Attribute>,
1258 pub vis: Visibility,
1262 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1264 /// An`extern crate` item, with optional original crate name,
1266 /// e.g. `extern crate foo` or `extern crate foo_bar as foo`
1267 ItemExternCrate(Option<Name>),
1268 /// A `use` or `pub use` item
1269 ItemUse(P<ViewPath>),
1272 ItemStatic(P<Ty>, Mutability, P<Expr>),
1274 ItemConst(P<Ty>, P<Expr>),
1275 /// A function declaration
1276 ItemFn(P<FnDecl>, Unsafety, Constness, Abi, Generics, P<Block>),
1279 /// An external module
1280 ItemForeignMod(ForeignMod),
1281 /// A type alias, e.g. `type Foo = Bar<u8>`
1282 ItemTy(P<Ty>, Generics),
1283 /// An enum definition, e.g. `enum Foo<A, B> {C<A>, D<B>}`
1284 ItemEnum(EnumDef, Generics),
1285 /// A struct definition, e.g. `struct Foo<A> {x: A}`
1286 ItemStruct(VariantData, Generics),
1287 /// Represents a Trait Declaration
1288 ItemTrait(Unsafety, Generics, TyParamBounds, Vec<P<TraitItem>>),
1290 // Default trait implementations
1292 /// `impl Trait for .. {}`
1293 ItemDefaultImpl(Unsafety, TraitRef),
1294 /// An implementation, eg `impl<A> Trait for Foo { .. }`
1298 Option<TraitRef>, // (optional) trait this impl implements
1304 pub fn descriptive_variant(&self) -> &str {
1306 ItemExternCrate(..) => "extern crate",
1307 ItemUse(..) => "use",
1308 ItemStatic(..) => "static item",
1309 ItemConst(..) => "constant item",
1310 ItemFn(..) => "function",
1311 ItemMod(..) => "module",
1312 ItemForeignMod(..) => "foreign module",
1313 ItemTy(..) => "type alias",
1314 ItemEnum(..) => "enum",
1315 ItemStruct(..) => "struct",
1316 ItemTrait(..) => "trait",
1318 ItemDefaultImpl(..) => "item",
1323 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1324 pub struct ForeignItem {
1326 pub attrs: Vec<Attribute>,
1327 pub node: ForeignItem_,
1330 pub vis: Visibility,
1333 /// An item within an `extern` block
1334 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1335 pub enum ForeignItem_ {
1336 /// A foreign function
1337 ForeignItemFn(P<FnDecl>, Generics),
1338 /// A foreign static item (`static ext: u8`), with optional mutability
1339 /// (the boolean is true when mutable)
1340 ForeignItemStatic(P<Ty>, bool),
1344 pub fn descriptive_variant(&self) -> &str {
1346 ForeignItemFn(..) => "foreign function",
1347 ForeignItemStatic(..) => "foreign static item",