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::ImplItem_
::*;
23 pub use self::Item_
::*;
24 pub use self::Mutability
::*;
25 pub use self::Pat_
::*;
26 pub use self::PathListItem_
::*;
27 pub use self::PatWildKind
::*;
28 pub use self::PrimTy
::*;
29 pub use self::Stmt_
::*;
30 pub use self::StructFieldKind
::*;
31 pub use self::TraitItem_
::*;
33 pub use self::TyParamBound
::*;
34 pub use self::UnOp
::*;
35 pub use self::UnsafeSource
::*;
36 pub use self::ViewPath_
::*;
37 pub use self::Visibility
::*;
38 pub use self::PathParameters
::*;
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, Hash, Debug)]
328 pub attrs
: Vec
<Attribute
>,
329 pub config
: CrateConfig
,
331 pub exported_macros
: Vec
<MacroDef
>,
334 /// A macro definition, in this crate or imported from another.
336 /// Not parsed directly, but created on macro import or `macro_rules!` expansion.
337 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
338 pub struct MacroDef
{
340 pub attrs
: Vec
<Attribute
>,
343 pub imported_from
: Option
<Name
>,
345 pub use_locally
: bool
,
346 pub allow_internal_unstable
: bool
,
347 pub body
: Vec
<TokenTree
>,
350 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
352 /// Statements in a block
353 pub stmts
: Vec
<P
<Stmt
>>,
354 /// An expression at the end of the block
355 /// without a semicolon, if any
356 pub expr
: Option
<P
<Expr
>>,
358 /// Distinguishes between `unsafe { ... }` and `{ ... }`
359 pub rules
: BlockCheckMode
,
363 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
370 impl fmt
::Debug
for Pat
{
371 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
372 write
!(f
, "pat({}: {})", self.id
, pprust
::pat_to_string(self))
376 /// A single field in a struct pattern
378 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
379 /// are treated the same as` x: x, y: ref y, z: ref mut z`,
380 /// except is_shorthand is true
381 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
382 pub struct FieldPat
{
383 /// The identifier for the field
385 /// The pattern the field is destructured to
387 pub is_shorthand
: bool
,
390 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
391 pub enum BindingMode
{
392 BindByRef(Mutability
),
393 BindByValue(Mutability
),
396 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
397 pub enum PatWildKind
{
398 /// Represents the wildcard pattern `_`
401 /// Represents the wildcard pattern `..`
405 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
407 /// Represents a wildcard pattern (either `_` or `..`)
408 PatWild(PatWildKind
),
410 /// A PatIdent may either be a new bound variable,
411 /// or a nullary enum (in which case the third field
414 /// In the nullary enum case, the parser can't determine
415 /// which it is. The resolver determines this, and
416 /// records this pattern's NodeId in an auxiliary
417 /// set (of "PatIdents that refer to nullary enums")
418 PatIdent(BindingMode
, Spanned
<Ident
>, Option
<P
<Pat
>>),
420 /// "None" means a * pattern where we don't bind the fields to names.
421 PatEnum(Path
, Option
<Vec
<P
<Pat
>>>),
423 /// An associated const named using the qualified path `<T>::CONST` or
424 /// `<T as Trait>::CONST`. Associated consts from inherent impls can be
425 /// referred to as simply `T::CONST`, in which case they will end up as
426 /// PatEnum, and the resolver will have to sort that out.
427 PatQPath(QSelf
, Path
),
429 /// Destructuring of a struct, e.g. `Foo {x, y, ..}`
430 /// The `bool` is `true` in the presence of a `..`
431 PatStruct(Path
, Vec
<Spanned
<FieldPat
>>, bool
),
432 /// A tuple pattern `(a, b)`
436 /// A reference pattern, e.g. `&mut (a, b)`
437 PatRegion(P
<Pat
>, Mutability
),
440 /// A range pattern, e.g. `1...2`
441 PatRange(P
<Expr
>, P
<Expr
>),
442 /// [a, b, ..i, y, z] is represented as:
443 /// PatVec(box [a, b], Some(i), box [y, z])
444 PatVec(Vec
<P
<Pat
>>, Option
<P
<Pat
>>, Vec
<P
<Pat
>>),
447 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
448 pub enum Mutability
{
453 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
455 /// The `+` operator (addition)
457 /// The `-` operator (subtraction)
459 /// The `*` operator (multiplication)
461 /// The `/` operator (division)
463 /// The `%` operator (modulus)
465 /// The `&&` operator (logical and)
467 /// The `||` operator (logical or)
469 /// The `^` operator (bitwise xor)
471 /// The `&` operator (bitwise and)
473 /// The `|` operator (bitwise or)
475 /// The `<<` operator (shift left)
477 /// The `>>` operator (shift right)
479 /// The `==` operator (equality)
481 /// The `<` operator (less than)
483 /// The `<=` operator (less than or equal to)
485 /// The `!=` operator (not equal to)
487 /// The `>=` operator (greater than or equal to)
489 /// The `>` operator (greater than)
493 pub type BinOp
= Spanned
<BinOp_
>;
495 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
497 /// The `*` operator for dereferencing
499 /// The `!` operator for logical inversion
501 /// The `-` operator for negation
506 pub type Stmt
= Spanned
<Stmt_
>;
508 impl fmt
::Debug
for Stmt_
{
509 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
511 let spanned
= codemap
::dummy_spanned(self.clone());
514 util
::stmt_id(&spanned
),
515 pprust
::stmt_to_string(&spanned
))
519 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
521 /// Could be an item or a local (let) binding:
522 StmtDecl(P
<Decl
>, NodeId
),
524 /// Expr without trailing semi-colon (must have unit type):
525 StmtExpr(P
<Expr
>, NodeId
),
527 /// Expr with trailing semi-colon (may have any type):
528 StmtSemi(P
<Expr
>, NodeId
),
531 // FIXME (pending discussion of #1697, #2178...): local should really be
532 // a refinement on pat.
533 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
534 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
537 pub ty
: Option
<P
<Ty
>>,
538 /// Initializer expression to set the value, if any
539 pub init
: Option
<P
<Expr
>>,
544 pub type Decl
= Spanned
<Decl_
>;
546 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
548 /// A local (let) binding:
554 /// represents one arm of a 'match'
555 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
557 pub attrs
: Vec
<Attribute
>,
558 pub pats
: Vec
<P
<Pat
>>,
559 pub guard
: Option
<P
<Expr
>>,
563 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
565 pub name
: Spanned
<Name
>,
570 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
571 pub enum BlockCheckMode
{
573 UnsafeBlock(UnsafeSource
),
574 PushUnsafeBlock(UnsafeSource
),
575 PopUnsafeBlock(UnsafeSource
),
576 // Within this block (but outside a PopUnstableBlock), we suspend checking of stability.
581 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
582 pub enum UnsafeSource
{
588 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
595 impl fmt
::Debug
for Expr
{
596 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
597 write
!(f
, "expr({}: {})", self.id
, pprust
::expr_to_string(self))
601 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
603 /// A `box x` expression.
605 /// An array (`[a, b, c, d]`)
606 ExprVec(Vec
<P
<Expr
>>),
609 /// The first field resolves to the function itself,
610 /// and the second field is the list of arguments
611 ExprCall(P
<Expr
>, Vec
<P
<Expr
>>),
612 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
614 /// The `Spanned<Name>` is the identifier for the method name.
615 /// The vector of `Ty`s are the ascripted type parameters for the method
616 /// (within the angle brackets).
618 /// The first element of the vector of `Expr`s is the expression that evaluates
619 /// to the object on which the method is being called on (the receiver),
620 /// and the remaining elements are the rest of the arguments.
622 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
623 /// `ExprMethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
624 ExprMethodCall(Spanned
<Name
>, Vec
<P
<Ty
>>, Vec
<P
<Expr
>>),
625 /// A tuple (`(a, b, c ,d)`)
626 ExprTup(Vec
<P
<Expr
>>),
627 /// A binary operation (For example: `a + b`, `a * b`)
628 ExprBinary(BinOp
, P
<Expr
>, P
<Expr
>),
629 /// A unary operation (For example: `!x`, `*x`)
630 ExprUnary(UnOp
, P
<Expr
>),
631 /// A literal (For example: `1u8`, `"foo"`)
633 /// A cast (`foo as f64`)
634 ExprCast(P
<Expr
>, P
<Ty
>),
635 /// An `if` block, with an optional else block
637 /// `if expr { block } else { expr }`
638 ExprIf(P
<Expr
>, P
<Block
>, Option
<P
<Expr
>>),
639 /// A while loop, with an optional label
641 /// `'label: while expr { block }`
642 ExprWhile(P
<Expr
>, P
<Block
>, Option
<Ident
>),
643 /// Conditionless loop (can be exited with break, continue, or return)
645 /// `'label: loop { block }`
646 ExprLoop(P
<Block
>, Option
<Ident
>),
647 /// A `match` block, with a source that indicates whether or not it is
648 /// the result of a desugaring, and if so, which kind.
649 ExprMatch(P
<Expr
>, Vec
<Arm
>, MatchSource
),
650 /// A closure (for example, `move |a, b, c| {a + b + c}`)
651 ExprClosure(CaptureClause
, P
<FnDecl
>, P
<Block
>),
652 /// A block (`{ ... }`)
655 /// An assignment (`a = foo()`)
656 ExprAssign(P
<Expr
>, P
<Expr
>),
657 /// An assignment with an operator
659 /// For example, `a += 1`.
660 ExprAssignOp(BinOp
, P
<Expr
>, P
<Expr
>),
661 /// Access of a named struct field (`obj.foo`)
662 ExprField(P
<Expr
>, Spanned
<Name
>),
663 /// Access of an unnamed field of a struct or tuple-struct
665 /// For example, `foo.0`.
666 ExprTupField(P
<Expr
>, Spanned
<usize>),
667 /// An indexing operation (`foo[2]`)
668 ExprIndex(P
<Expr
>, P
<Expr
>),
669 /// A range (`1..2`, `1..`, or `..2`)
670 ExprRange(Option
<P
<Expr
>>, Option
<P
<Expr
>>),
672 /// Variable reference, possibly containing `::` and/or type
673 /// parameters, e.g. foo::bar::<baz>.
675 /// Optionally "qualified",
676 /// e.g. `<Vec<T> as SomeTrait>::SomeType`.
677 ExprPath(Option
<QSelf
>, Path
),
679 /// A referencing operation (`&a` or `&mut a`)
680 ExprAddrOf(Mutability
, P
<Expr
>),
681 /// A `break`, with an optional label to break
682 ExprBreak(Option
<Spanned
<Ident
>>),
683 /// A `continue`, with an optional label
684 ExprAgain(Option
<Spanned
<Ident
>>),
685 /// A `return`, with an optional value to be returned
686 ExprRet(Option
<P
<Expr
>>),
688 /// Output of the `asm!()` macro
689 ExprInlineAsm(InlineAsm
),
691 /// A struct literal expression.
693 /// For example, `Foo {x: 1, y: 2}`, or
694 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
695 ExprStruct(Path
, Vec
<Field
>, Option
<P
<Expr
>>),
697 /// A vector literal constructed from one repeated element.
699 /// For example, `[1u8; 5]`. The first expression is the element
700 /// to be repeated; the second is the number of times to repeat it.
701 ExprRepeat(P
<Expr
>, P
<Expr
>),
704 /// The explicit Self type in a "qualified path". The actual
705 /// path, including the trait and the associated item, is stored
706 /// separately. `position` represents the index of the associated
707 /// item qualified with this Self type.
709 /// <Vec<T> as a::b::Trait>::AssociatedItem
710 /// ^~~~~ ~~~~~~~~~~~~~~^
713 /// <Vec<T>>::AssociatedItem
716 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
722 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
723 pub enum MatchSource
{
726 contains_else_clause
: bool
,
732 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
733 pub enum CaptureClause
{
738 // NB: If you change this, you'll probably want to change the corresponding
739 // type structure in middle/ty.rs as well.
740 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
743 pub mutbl
: Mutability
,
746 /// Represents a method's signature in a trait declaration,
747 /// or in an implementation.
748 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
749 pub struct MethodSig
{
750 pub unsafety
: Unsafety
,
751 pub constness
: Constness
,
754 pub generics
: Generics
,
755 pub explicit_self
: ExplicitSelf
,
758 /// Represents a method declaration in a trait declaration, possibly including
759 /// a default implementation A trait method is either required (meaning it
760 /// doesn't have an implementation, just a signature) or provided (meaning it
761 /// has a default implementation).
762 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
763 pub struct TraitItem
{
766 pub attrs
: Vec
<Attribute
>,
767 pub node
: TraitItem_
,
771 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
772 pub enum TraitItem_
{
773 ConstTraitItem(P
<Ty
>, Option
<P
<Expr
>>),
774 MethodTraitItem(MethodSig
, Option
<P
<Block
>>),
775 TypeTraitItem(TyParamBounds
, Option
<P
<Ty
>>),
778 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
779 pub struct ImplItem
{
783 pub attrs
: Vec
<Attribute
>,
788 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
790 ConstImplItem(P
<Ty
>, P
<Expr
>),
791 MethodImplItem(MethodSig
, P
<Block
>),
795 // Bind a type to an associated type: `A=Foo`.
796 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
797 pub struct TypeBinding
{
805 // NB PartialEq method appears below.
806 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
813 impl fmt
::Debug
for Ty
{
814 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
815 write
!(f
, "type({})", pprust
::ty_to_string(self))
819 /// Not represented directly in the AST, referred to by name through a ty_path.
820 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
830 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
831 pub struct BareFnTy
{
832 pub unsafety
: Unsafety
,
834 pub lifetimes
: Vec
<LifetimeDef
>,
838 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
839 /// The different kinds of types recognized by the compiler
842 /// A fixed length array (`[T; n]`)
843 TyFixedLengthVec(P
<Ty
>, P
<Expr
>),
844 /// A raw pointer (`*const T` or `*mut T`)
846 /// A reference (`&'a T` or `&'a mut T`)
847 TyRptr(Option
<Lifetime
>, MutTy
),
848 /// A bare function (e.g. `fn(usize) -> bool`)
849 TyBareFn(P
<BareFnTy
>),
850 /// A tuple (`(A, B, C, D,...)`)
852 /// A path (`module::module::...::Type`), optionally
853 /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
855 /// Type parameters are stored in the Path itself
856 TyPath(Option
<QSelf
>, Path
),
857 /// Something like `A+B`. Note that `B` must always be a path.
858 TyObjectSum(P
<Ty
>, TyParamBounds
),
859 /// A type like `for<'a> Foo<&'a Bar>`
860 TyPolyTraitRef(TyParamBounds
),
861 /// No-op; kept solely so that we can pretty-print faithfully
865 /// TyInfer means the type should be inferred instead of it having been
866 /// specified. This can appear anywhere in a type.
870 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
871 pub struct InlineAsm
{
872 pub asm
: InternedString
,
873 pub asm_str_style
: StrStyle
,
874 pub outputs
: Vec
<(InternedString
, P
<Expr
>, bool
)>,
875 pub inputs
: Vec
<(InternedString
, P
<Expr
>)>,
876 pub clobbers
: Vec
<InternedString
>,
878 pub alignstack
: bool
,
879 pub dialect
: AsmDialect
,
883 /// represents an argument in a function header
884 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
892 pub fn new_self(span
: Span
, mutability
: Mutability
, self_ident
: Ident
) -> Arg
{
898 // HACK(eddyb) fake type for the self argument.
906 node
: PatIdent(BindByValue(mutability
), path
, None
),
914 /// Represents the header (not the body) of a function declaration
915 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
917 pub inputs
: Vec
<Arg
>,
918 pub output
: FunctionRetTy
,
922 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
928 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
934 impl fmt
::Display
for Unsafety
{
935 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
936 fmt
::Display
::fmt(match *self {
937 Unsafety
::Normal
=> "normal",
938 Unsafety
::Unsafe
=> "unsafe",
944 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
945 pub enum ImplPolarity
{
946 /// `impl Trait for Type`
948 /// `impl !Trait for Type`
952 impl fmt
::Debug
for ImplPolarity
{
953 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
955 ImplPolarity
::Positive
=> "positive".fmt(f
),
956 ImplPolarity
::Negative
=> "negative".fmt(f
),
962 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
963 pub enum FunctionRetTy
{
964 /// Functions with return type `!`that always
965 /// raise an error or exit (i.e. never return to the caller)
967 /// Return type is not specified.
969 /// Functions default to `()` and
970 /// closures default to inference. Span points to where return
971 /// type would be inserted.
978 pub fn span(&self) -> Span
{
980 NoReturn(span
) => span
,
981 DefaultReturn(span
) => span
,
982 Return(ref ty
) => ty
.span
,
987 /// Represents the kind of 'self' associated with a method
988 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
989 pub enum ExplicitSelf_
{
994 /// `&'lt self`, `&'lt mut self`
995 SelfRegion(Option
<Lifetime
>, Mutability
, Name
),
997 SelfExplicit(P
<Ty
>, Name
),
1000 pub type ExplicitSelf
= Spanned
<ExplicitSelf_
>;
1002 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1004 /// A span from the first token past `{` to the last token until `}`.
1005 /// For `mod foo;`, the inner span ranges from the first token
1006 /// to the last token in the external file.
1008 pub items
: Vec
<P
<Item
>>,
1011 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1012 pub struct ForeignMod
{
1014 pub items
: Vec
<P
<ForeignItem
>>,
1017 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1018 pub struct EnumDef
{
1019 pub variants
: Vec
<P
<Variant
>>,
1022 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1023 pub struct Variant_
{
1025 pub attrs
: Vec
<Attribute
>,
1026 pub data
: VariantData
,
1027 /// Explicit discriminant, eg `Foo = 1`
1028 pub disr_expr
: Option
<P
<Expr
>>,
1031 pub type Variant
= Spanned
<Variant_
>;
1033 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1034 pub enum PathListItem_
{
1037 /// renamed in list, eg `use foo::{bar as baz};`
1038 rename
: Option
<Name
>,
1042 /// renamed in list, eg `use foo::{self as baz};`
1043 rename
: Option
<Name
>,
1048 impl PathListItem_
{
1049 pub fn id(&self) -> NodeId
{
1051 PathListIdent { id, .. }
| PathListMod { id, .. }
=> id
,
1055 pub fn name(&self) -> Option
<Name
> {
1057 PathListIdent { name, .. }
=> Some(name
),
1058 PathListMod { .. }
=> None
,
1062 pub fn rename(&self) -> Option
<Name
> {
1064 PathListIdent { rename, .. }
| PathListMod { rename, .. }
=> rename
,
1069 pub type PathListItem
= Spanned
<PathListItem_
>;
1071 pub type ViewPath
= Spanned
<ViewPath_
>;
1073 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1074 pub enum ViewPath_
{
1076 /// `foo::bar::baz as quux`
1080 /// `foo::bar::baz` (with `as baz` implicitly on the right)
1081 ViewPathSimple(Name
, Path
),
1086 /// `foo::bar::{a,b,c}`
1087 ViewPathList(Path
, Vec
<PathListItem
>),
1090 /// TraitRef's appear in impls.
1092 /// resolve maps each TraitRef's ref_id to its defining trait; that's all
1093 /// that the ref_id is for. The impl_id maps to the "self type" of this impl.
1094 /// If this impl is an ItemImpl, the impl_id is redundant (it could be the
1095 /// same as the impl's node id).
1096 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1097 pub struct TraitRef
{
1102 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1103 pub struct PolyTraitRef
{
1104 /// The `'a` in `<'a> Foo<&'a T>`
1105 pub bound_lifetimes
: Vec
<LifetimeDef
>,
1107 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
1108 pub trait_ref
: TraitRef
,
1113 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1114 pub enum Visibility
{
1120 pub fn inherit_from(&self, parent_visibility
: Visibility
) -> Visibility
{
1122 &Inherited
=> parent_visibility
,
1128 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1129 pub struct StructField_
{
1130 pub kind
: StructFieldKind
,
1133 pub attrs
: Vec
<Attribute
>,
1137 pub fn name(&self) -> Option
<Name
> {
1139 NamedField(name
, _
) => Some(name
),
1140 UnnamedField(_
) => None
,
1145 pub type StructField
= Spanned
<StructField_
>;
1147 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1148 pub enum StructFieldKind
{
1149 NamedField(Name
, Visibility
),
1150 /// Element of a tuple-like struct
1151 UnnamedField(Visibility
),
1154 impl StructFieldKind
{
1155 pub fn is_unnamed(&self) -> bool
{
1157 UnnamedField(..) => true,
1158 NamedField(..) => false,
1163 /// Fields and Ids of enum variants and structs
1165 /// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all
1166 /// variant kinds) and an Id of the variant's constructor (not relevant for `Struct`-variants).
1167 /// One shared Id can be successfully used for these two purposes.
1168 /// Id of the whole enum lives in `Item`.
1170 /// For structs: `NodeId` represents an Id of the structure's constructor, so it is not actually
1171 /// used for `Struct`-structs (but still presents). Structures don't have an analogue of "Id of
1172 /// the variant itself" from enum variants.
1173 /// Id of the whole struct lives in `Item`.
1174 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1175 pub enum VariantData
{
1176 Struct(Vec
<StructField
>, NodeId
),
1177 Tuple(Vec
<StructField
>, NodeId
),
1182 pub fn fields(&self) -> &[StructField
] {
1184 VariantData
::Struct(ref fields
, _
) | VariantData
::Tuple(ref fields
, _
) => fields
,
1188 pub fn id(&self) -> NodeId
{
1190 VariantData
::Struct(_
, id
) | VariantData
::Tuple(_
, id
) | VariantData
::Unit(id
) => id
1193 pub fn is_struct(&self) -> bool
{
1194 if let VariantData
::Struct(..) = *self { true }
else { false }
1196 pub fn is_tuple(&self) -> bool
{
1197 if let VariantData
::Tuple(..) = *self { true }
else { false }
1199 pub fn is_unit(&self) -> bool
{
1200 if let VariantData
::Unit(..) = *self { true }
else { false }
1205 FIXME (#3300): Should allow items to be anonymous. Right now
1206 we just use dummy names for anon items.
1210 /// The name might be a dummy name in case of anonymous items
1211 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1214 pub attrs
: Vec
<Attribute
>,
1217 pub vis
: Visibility
,
1221 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1223 /// An`extern crate` item, with optional original crate name,
1225 /// e.g. `extern crate foo` or `extern crate foo_bar as foo`
1226 ItemExternCrate(Option
<Name
>),
1227 /// A `use` or `pub use` item
1228 ItemUse(P
<ViewPath
>),
1231 ItemStatic(P
<Ty
>, Mutability
, P
<Expr
>),
1233 ItemConst(P
<Ty
>, P
<Expr
>),
1234 /// A function declaration
1235 ItemFn(P
<FnDecl
>, Unsafety
, Constness
, Abi
, Generics
, P
<Block
>),
1238 /// An external module
1239 ItemForeignMod(ForeignMod
),
1240 /// A type alias, e.g. `type Foo = Bar<u8>`
1241 ItemTy(P
<Ty
>, Generics
),
1242 /// An enum definition, e.g. `enum Foo<A, B> {C<A>, D<B>}`
1243 ItemEnum(EnumDef
, Generics
),
1244 /// A struct definition, e.g. `struct Foo<A> {x: A}`
1245 ItemStruct(VariantData
, Generics
),
1246 /// Represents a Trait Declaration
1247 ItemTrait(Unsafety
, Generics
, TyParamBounds
, Vec
<P
<TraitItem
>>),
1249 // Default trait implementations
1251 // `impl Trait for .. {}`
1252 ItemDefaultImpl(Unsafety
, TraitRef
),
1253 /// An implementation, eg `impl<A> Trait for Foo { .. }`
1257 Option
<TraitRef
>, // (optional) trait this impl implements
1263 pub fn descriptive_variant(&self) -> &str {
1265 ItemExternCrate(..) => "extern crate",
1266 ItemUse(..) => "use",
1267 ItemStatic(..) => "static item",
1268 ItemConst(..) => "constant item",
1269 ItemFn(..) => "function",
1270 ItemMod(..) => "module",
1271 ItemForeignMod(..) => "foreign module",
1272 ItemTy(..) => "type alias",
1273 ItemEnum(..) => "enum",
1274 ItemStruct(..) => "struct",
1275 ItemTrait(..) => "trait",
1277 ItemDefaultImpl(..) => "item",
1282 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1283 pub struct ForeignItem
{
1285 pub attrs
: Vec
<Attribute
>,
1286 pub node
: ForeignItem_
,
1289 pub vis
: Visibility
,
1292 /// An item within an `extern` block
1293 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1294 pub enum ForeignItem_
{
1295 /// A foreign function
1296 ForeignItemFn(P
<FnDecl
>, Generics
),
1297 /// A foreign static item (`static ext: u8`), with optional mutability
1298 /// (the boolean is true when mutable)
1299 ForeignItemStatic(P
<Ty
>, bool
),
1303 pub fn descriptive_variant(&self) -> &str {
1305 ForeignItemFn(..) => "foreign function",
1306 ForeignItemStatic(..) => "foreign static item",