1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
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.
11 // The Rust abstract syntax tree.
13 pub use self::GenericArgs::*;
14 pub use self::UnsafeSource::*;
15 pub use symbol::{Ident, Symbol as Name};
16 pub use util::parser::ExprPrecedence;
18 use ext::hygiene::{Mark, SyntaxContext};
21 use rustc_data_structures::indexed_vec;
22 use rustc_data_structures::indexed_vec::Idx;
23 use rustc_target::spec::abi::Abi;
24 use source_map::{dummy_spanned, respan, Spanned};
25 use symbol::{keywords, Symbol};
26 use syntax_pos::{Span, DUMMY_SP};
27 use tokenstream::{ThinTokenStream, TokenStream};
30 use rustc_data_structures::fx::FxHashSet;
31 use rustc_data_structures::sync::Lrc;
32 use serialize::{self, Decoder, Encoder};
36 pub use rustc_target::abi::FloatTy;
38 #[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
43 impl fmt::Debug for Label {
44 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45 write!(f, "label({:?})", self.ident)
49 #[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
55 impl fmt::Debug for Lifetime {
56 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61 pprust::lifetime_to_string(self)
66 /// A "Path" is essentially Rust's notion of a name.
68 /// It's represented as a sequence of identifiers,
69 /// along with a bunch of supporting information.
71 /// E.g. `std::cmp::PartialEq`
72 #[derive(Clone, RustcEncodable, RustcDecodable)]
75 /// The segments in the path: the things separated by `::`.
76 /// Global paths begin with `keywords::CrateRoot`.
77 pub segments: Vec<PathSegment>,
80 impl<'a> PartialEq<&'a str> for Path {
81 fn eq(&self, string: &&'a str) -> bool {
82 self.segments.len() == 1 && self.segments[0].ident.name == *string
86 impl fmt::Debug for Path {
87 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
88 write!(f, "path({})", pprust::path_to_string(self))
92 impl fmt::Display for Path {
93 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94 write!(f, "{}", pprust::path_to_string(self))
99 // convert a span and an identifier to the corresponding
101 pub fn from_ident(ident: Ident) -> Path {
103 segments: vec![PathSegment::from_ident(ident)],
108 // Make a "crate root" segment for this path unless it already has it
109 // or starts with something like `self`/`super`/`$crate`/etc.
110 pub fn make_root(&self) -> Option<PathSegment> {
111 if let Some(ident) = self.segments.get(0).map(|seg| seg.ident) {
112 if ident.is_path_segment_keyword() {
116 Some(PathSegment::crate_root(self.span.shrink_to_lo()))
119 pub fn is_global(&self) -> bool {
120 !self.segments.is_empty() && self.segments[0].ident.name == keywords::CrateRoot.name()
124 /// A segment of a path: an identifier, an optional lifetime, and a set of types.
126 /// E.g. `std`, `String` or `Box<T>`
127 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
128 pub struct PathSegment {
129 /// The identifier portion of this path segment.
134 /// Type/lifetime parameters attached to this path. They come in
135 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`.
136 /// `None` means that no parameter list is supplied (`Path`),
137 /// `Some` means that parameter list is supplied (`Path<X, Y>`)
138 /// but it can be empty (`Path<>`).
139 /// `P` is used as a size optimization for the common case with no parameters.
140 pub args: Option<P<GenericArgs>>,
144 pub fn from_ident(ident: Ident) -> Self {
145 PathSegment { ident, id: DUMMY_NODE_ID, args: None }
147 pub fn crate_root(span: Span) -> Self {
148 PathSegment::from_ident(Ident::new(keywords::CrateRoot.name(), span))
152 /// Arguments of a path segment.
154 /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
155 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
156 pub enum GenericArgs {
157 /// The `<'a, A,B,C>` in `foo::bar::baz::<'a, A,B,C>`
158 AngleBracketed(AngleBracketedArgs),
159 /// The `(A,B)` and `C` in `Foo(A,B) -> C`
160 Parenthesized(ParenthesisedArgs),
164 pub fn span(&self) -> Span {
166 AngleBracketed(ref data) => data.span,
167 Parenthesized(ref data) => data.span,
172 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
173 pub enum GenericArg {
178 /// A path like `Foo<'a, T>`
179 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Default)]
180 pub struct AngleBracketedArgs {
183 /// The arguments for this path segment.
184 pub args: Vec<GenericArg>,
185 /// Bindings (equality constraints) on associated types, if present.
187 /// E.g., `Foo<A=Bar>`.
188 pub bindings: Vec<TypeBinding>,
191 impl Into<Option<P<GenericArgs>>> for AngleBracketedArgs {
192 fn into(self) -> Option<P<GenericArgs>> {
193 Some(P(GenericArgs::AngleBracketed(self)))
197 impl Into<Option<P<GenericArgs>>> for ParenthesisedArgs {
198 fn into(self) -> Option<P<GenericArgs>> {
199 Some(P(GenericArgs::Parenthesized(self)))
203 /// A path like `Foo(A,B) -> C`
204 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
205 pub struct ParenthesisedArgs {
210 pub inputs: Vec<P<Ty>>,
213 pub output: Option<P<Ty>>,
216 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
217 pub struct NodeId(u32);
220 pub fn new(x: usize) -> NodeId {
221 assert!(x < (u32::MAX as usize));
225 pub fn from_u32(x: u32) -> NodeId {
229 pub fn as_usize(&self) -> usize {
233 pub fn as_u32(&self) -> u32 {
237 pub fn placeholder_from_mark(mark: Mark) -> Self {
238 NodeId(mark.as_u32())
241 pub fn placeholder_to_mark(self) -> Mark {
242 Mark::from_u32(self.0)
246 impl fmt::Display for NodeId {
247 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
248 fmt::Display::fmt(&self.0, f)
252 impl serialize::UseSpecializedEncodable for NodeId {
253 fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
258 impl serialize::UseSpecializedDecodable for NodeId {
259 fn default_decode<D: Decoder>(d: &mut D) -> Result<NodeId, D::Error> {
260 d.read_u32().map(NodeId)
264 impl indexed_vec::Idx for NodeId {
265 fn new(idx: usize) -> Self {
269 fn index(self) -> usize {
274 /// Node id used to represent the root of the crate.
275 pub const CRATE_NODE_ID: NodeId = NodeId(0);
277 /// When parsing and doing expansions, we initially give all AST nodes this AST
278 /// node value. Then later, in the renumber pass, we renumber them to have
279 /// small, positive ids.
280 pub const DUMMY_NODE_ID: NodeId = NodeId(!0);
282 /// A modifier on a bound, currently this is only used for `?Sized`, where the
283 /// modifier is `Maybe`. Negative bounds should also be handled here.
284 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
285 pub enum TraitBoundModifier {
290 /// The AST represents all type param bounds as types.
291 /// `typeck::collect::compute_bounds` matches these against
292 /// the "special" built-in traits (see `middle::lang_items`) and
293 /// detects `Copy`, `Send` and `Sync`.
294 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
295 pub enum GenericBound {
296 Trait(PolyTraitRef, TraitBoundModifier),
301 pub fn span(&self) -> Span {
303 &GenericBound::Trait(ref t, ..) => t.span,
304 &GenericBound::Outlives(ref l) => l.ident.span,
309 pub type GenericBounds = Vec<GenericBound>;
311 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
312 pub enum GenericParamKind {
313 /// A lifetime definition, e.g. `'a: 'b+'c+'d`.
316 default: Option<P<Ty>>,
320 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
321 pub struct GenericParam {
324 pub attrs: ThinVec<Attribute>,
325 pub bounds: GenericBounds,
327 pub kind: GenericParamKind,
330 /// Represents lifetime, type and const parameters attached to a declaration of
331 /// a function, enum, trait, etc.
332 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
333 pub struct Generics {
334 pub params: Vec<GenericParam>,
335 pub where_clause: WhereClause,
339 impl Default for Generics {
340 /// Creates an instance of `Generics`.
341 fn default() -> Generics {
344 where_clause: WhereClause {
346 predicates: Vec::new(),
354 /// A `where` clause in a definition
355 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
356 pub struct WhereClause {
358 pub predicates: Vec<WherePredicate>,
362 /// A single predicate in a `where` clause
363 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
364 pub enum WherePredicate {
365 /// A type binding, e.g. `for<'c> Foo: Send+Clone+'c`
366 BoundPredicate(WhereBoundPredicate),
367 /// A lifetime predicate, e.g. `'a: 'b+'c`
368 RegionPredicate(WhereRegionPredicate),
369 /// An equality predicate (unsupported)
370 EqPredicate(WhereEqPredicate),
373 impl WherePredicate {
374 pub fn span(&self) -> Span {
376 &WherePredicate::BoundPredicate(ref p) => p.span,
377 &WherePredicate::RegionPredicate(ref p) => p.span,
378 &WherePredicate::EqPredicate(ref p) => p.span,
385 /// E.g. `for<'c> Foo: Send+Clone+'c`
386 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
387 pub struct WhereBoundPredicate {
389 /// Any generics from a `for` binding
390 pub bound_generic_params: Vec<GenericParam>,
391 /// The type being bounded
392 pub bounded_ty: P<Ty>,
393 /// Trait and lifetime bounds (`Clone+Send+'static`)
394 pub bounds: GenericBounds,
397 /// A lifetime predicate.
400 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
401 pub struct WhereRegionPredicate {
403 pub lifetime: Lifetime,
404 pub bounds: GenericBounds,
407 /// An equality predicate (unsupported).
410 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
411 pub struct WhereEqPredicate {
418 /// The set of MetaItems that define the compilation environment of the crate,
419 /// used to drive conditional compilation
420 pub type CrateConfig = FxHashSet<(Name, Option<Symbol>)>;
422 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
425 pub attrs: Vec<Attribute>,
429 /// A spanned compile-time attribute list item.
430 pub type NestedMetaItem = Spanned<NestedMetaItemKind>;
432 /// Possible values inside of compile-time attribute lists.
434 /// E.g. the '..' in `#[name(..)]`.
435 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
436 pub enum NestedMetaItemKind {
437 /// A full MetaItem, for recursive meta items.
441 /// E.g. "foo", 64, true
445 /// A spanned compile-time attribute item.
447 /// E.g. `#[test]`, `#[derive(..)]`, `#[rustfmt::skip]` or `#[feature = "foo"]`
448 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
449 pub struct MetaItem {
451 pub node: MetaItemKind,
455 /// A compile-time attribute item.
457 /// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
458 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
459 pub enum MetaItemKind {
462 /// E.g. `test` as in `#[test]`
466 /// E.g. `derive(..)` as in `#[derive(..)]`
467 List(Vec<NestedMetaItem>),
468 /// Name value meta item.
470 /// E.g. `feature = "foo"` as in `#[feature = "foo"]`
474 /// A Block (`{ .. }`).
476 /// E.g. `{ .. }` as in `fn foo() { .. }`
477 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
479 /// Statements in a block
480 pub stmts: Vec<Stmt>,
482 /// Distinguishes between `unsafe { ... }` and `{ ... }`
483 pub rules: BlockCheckMode,
488 #[derive(Clone, RustcEncodable, RustcDecodable)]
495 impl fmt::Debug for Pat {
496 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
497 write!(f, "pat({}: {})", self.id, pprust::pat_to_string(self))
502 pub(super) fn to_ty(&self) -> Option<P<Ty>> {
503 let node = match &self.node {
504 PatKind::Wild => TyKind::Infer,
505 PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), ident, None) => {
506 TyKind::Path(None, Path::from_ident(*ident))
508 PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
509 PatKind::Mac(mac) => TyKind::Mac(mac.clone()),
510 PatKind::Ref(pat, mutbl) => pat
512 .map(|ty| TyKind::Rptr(None, MutTy { ty, mutbl: *mutbl }))?,
513 PatKind::Slice(pats, None, _) if pats.len() == 1 => {
514 pats[0].to_ty().map(TyKind::Slice)?
516 PatKind::Tuple(pats, None) => {
517 let mut tys = Vec::with_capacity(pats.len());
518 // FIXME(#48994) - could just be collected into an Option<Vec>
520 tys.push(pat.to_ty()?);
534 pub fn walk<F>(&self, it: &mut F) -> bool
536 F: FnMut(&Pat) -> bool,
543 PatKind::Ident(_, _, Some(ref p)) => p.walk(it),
544 PatKind::Struct(_, ref fields, _) => fields.iter().all(|field| field.node.pat.walk(it)),
545 PatKind::TupleStruct(_, ref s, _) | PatKind::Tuple(ref s, _) => {
546 s.iter().all(|p| p.walk(it))
548 PatKind::Box(ref s) | PatKind::Ref(ref s, _) | PatKind::Paren(ref s) => s.walk(it),
549 PatKind::Slice(ref before, ref slice, ref after) => {
550 before.iter().all(|p| p.walk(it))
551 && slice.iter().all(|p| p.walk(it))
552 && after.iter().all(|p| p.walk(it))
559 | PatKind::Mac(_) => true,
564 /// A single field in a struct pattern
566 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
567 /// are treated the same as` x: x, y: ref y, z: ref mut z`,
568 /// except is_shorthand is true
569 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
570 pub struct FieldPat {
571 /// The identifier for the field
573 /// The pattern the field is destructured to
575 pub is_shorthand: bool,
576 pub attrs: ThinVec<Attribute>,
579 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
580 pub enum BindingMode {
585 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
587 Included(RangeSyntax),
591 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
592 pub enum RangeSyntax {
597 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
599 /// Represents a wildcard pattern (`_`)
602 /// A `PatKind::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
603 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
604 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
605 /// during name resolution.
606 Ident(BindingMode, Ident, Option<P<Pat>>),
608 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
609 /// The `bool` is `true` in the presence of a `..`.
610 Struct(Path, Vec<Spanned<FieldPat>>, bool),
612 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
613 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
614 /// 0 <= position <= subpats.len()
615 TupleStruct(Path, Vec<P<Pat>>, Option<usize>),
617 /// A possibly qualified path pattern.
618 /// Unqualified path patterns `A::B::C` can legally refer to variants, structs, constants
619 /// or associated constants. Qualified path patterns `<A>::B::C`/`<A as Trait>::B::C` can
620 /// only legally refer to associated constants.
621 Path(Option<QSelf>, Path),
623 /// A tuple pattern `(a, b)`.
624 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
625 /// 0 <= position <= subpats.len()
626 Tuple(Vec<P<Pat>>, Option<usize>),
629 /// A reference pattern, e.g. `&mut (a, b)`
630 Ref(P<Pat>, Mutability),
633 /// A range pattern, e.g. `1...2`, `1..=2` or `1..2`
634 Range(P<Expr>, P<Expr>, Spanned<RangeEnd>),
635 /// `[a, b, ..i, y, z]` is represented as:
636 /// `PatKind::Slice(box [a, b], Some(i), box [y, z])`
637 Slice(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
638 /// Parentheses in patterns used for grouping, i.e. `(PAT)`.
640 /// A macro pattern; pre-expansion
645 Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable, Debug, Copy,
647 pub enum Mutability {
652 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
654 /// The `+` operator (addition)
656 /// The `-` operator (subtraction)
658 /// The `*` operator (multiplication)
660 /// The `/` operator (division)
662 /// The `%` operator (modulus)
664 /// The `&&` operator (logical and)
666 /// The `||` operator (logical or)
668 /// The `^` operator (bitwise xor)
670 /// The `&` operator (bitwise and)
672 /// The `|` operator (bitwise or)
674 /// The `<<` operator (shift left)
676 /// The `>>` operator (shift right)
678 /// The `==` operator (equality)
680 /// The `<` operator (less than)
682 /// The `<=` operator (less than or equal to)
684 /// The `!=` operator (not equal to)
686 /// The `>=` operator (greater than or equal to)
688 /// The `>` operator (greater than)
693 pub fn to_string(&self) -> &'static str {
694 use self::BinOpKind::*;
716 pub fn lazy(&self) -> bool {
718 BinOpKind::And | BinOpKind::Or => true,
723 pub fn is_shift(&self) -> bool {
725 BinOpKind::Shl | BinOpKind::Shr => true,
730 pub fn is_comparison(&self) -> bool {
731 use self::BinOpKind::*;
733 Eq | Lt | Le | Ne | Gt | Ge => true,
734 And | Or | Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Shl | Shr => false,
738 /// Returns `true` if the binary operator takes its arguments by value
739 pub fn is_by_value(&self) -> bool {
740 !self.is_comparison()
744 pub type BinOp = Spanned<BinOpKind>;
746 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy)]
748 /// The `*` operator for dereferencing
750 /// The `!` operator for logical inversion
752 /// The `-` operator for negation
757 /// Returns `true` if the unary operator takes its argument by value
758 pub fn is_by_value(u: UnOp) -> bool {
760 UnOp::Neg | UnOp::Not => true,
765 pub fn to_string(op: UnOp) -> &'static str {
775 #[derive(Clone, RustcEncodable, RustcDecodable)]
783 pub fn add_trailing_semicolon(mut self) -> Self {
784 self.node = match self.node {
785 StmtKind::Expr(expr) => StmtKind::Semi(expr),
786 StmtKind::Mac(mac) => {
787 StmtKind::Mac(mac.map(|(mac, _style, attrs)| (mac, MacStmtStyle::Semicolon, attrs)))
794 pub fn is_item(&self) -> bool {
796 StmtKind::Item(_) => true,
801 pub fn is_expr(&self) -> bool {
803 StmtKind::Expr(_) => true,
809 impl fmt::Debug for Stmt {
810 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
815 pprust::stmt_to_string(self)
820 #[derive(Clone, RustcEncodable, RustcDecodable)]
822 /// A local (let) binding.
825 /// An item definition.
828 /// Expr without trailing semi-colon.
830 /// Expr with a trailing semi-colon.
833 Mac(P<(Mac, MacStmtStyle, ThinVec<Attribute>)>),
836 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
837 pub enum MacStmtStyle {
838 /// The macro statement had a trailing semicolon, e.g. `foo! { ... };`
839 /// `foo!(...);`, `foo![...];`
841 /// The macro statement had braces; e.g. foo! { ... }
843 /// The macro statement had parentheses or brackets and no semicolon; e.g.
844 /// `foo!(...)`. All of these will end up being converted into macro
849 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
850 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
853 pub ty: Option<P<Ty>>,
854 /// Initializer expression to set the value, if any
855 pub init: Option<P<Expr>>,
858 pub attrs: ThinVec<Attribute>,
861 /// An arm of a 'match'.
863 /// E.g. `0..=10 => { println!("match!") }` as in
867 /// 0..=10 => { println!("match!") },
868 /// _ => { println!("no match!") },
871 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
873 pub attrs: Vec<Attribute>,
874 pub pats: Vec<P<Pat>>,
875 pub guard: Option<Guard>,
879 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
884 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
889 pub is_shorthand: bool,
890 pub attrs: ThinVec<Attribute>,
893 pub type SpannedIdent = Spanned<Ident>;
895 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy)]
896 pub enum BlockCheckMode {
898 Unsafe(UnsafeSource),
901 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy)]
902 pub enum UnsafeSource {
907 /// A constant (expression) that's not an item or associated item,
908 /// but needs its own `DefId` for type-checking, const-eval, etc.
909 /// These are usually found nested inside types (e.g. array lengths)
910 /// or expressions (e.g. repeat counts), and also used to define
911 /// explicit discriminant values for enum variants.
912 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
913 pub struct AnonConst {
919 #[derive(Clone, RustcEncodable, RustcDecodable)]
924 pub attrs: ThinVec<Attribute>,
928 /// Whether this expression would be valid somewhere that expects a value, for example, an `if`
930 pub fn returns(&self) -> bool {
931 if let ExprKind::Block(ref block, _) = self.node {
932 match block.stmts.last().map(|last_stmt| &last_stmt.node) {
934 Some(&StmtKind::Expr(_)) => true,
935 Some(&StmtKind::Semi(ref expr)) => {
936 if let ExprKind::Ret(_) = expr.node {
937 // last statement is explicit return
943 // This is a block that doesn't end in either an implicit or explicit return
947 // This is not a block, it is a value
952 fn to_bound(&self) -> Option<GenericBound> {
954 ExprKind::Path(None, path) => Some(GenericBound::Trait(
955 PolyTraitRef::new(Vec::new(), path.clone(), self.span),
956 TraitBoundModifier::None,
962 pub(super) fn to_ty(&self) -> Option<P<Ty>> {
963 let node = match &self.node {
964 ExprKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
965 ExprKind::Mac(mac) => TyKind::Mac(mac.clone()),
966 ExprKind::Paren(expr) => expr.to_ty().map(TyKind::Paren)?,
967 ExprKind::AddrOf(mutbl, expr) => expr
969 .map(|ty| TyKind::Rptr(None, MutTy { ty, mutbl: *mutbl }))?,
970 ExprKind::Repeat(expr, expr_len) => {
971 expr.to_ty().map(|ty| TyKind::Array(ty, expr_len.clone()))?
973 ExprKind::Array(exprs) if exprs.len() == 1 => exprs[0].to_ty().map(TyKind::Slice)?,
974 ExprKind::Tup(exprs) => {
977 .map(|expr| expr.to_ty())
978 .collect::<Option<Vec<_>>>()?;
981 ExprKind::Binary(binop, lhs, rhs) if binop.node == BinOpKind::Add => {
982 if let (Some(lhs), Some(rhs)) = (lhs.to_bound(), rhs.to_bound()) {
983 TyKind::TraitObject(vec![lhs, rhs], TraitObjectSyntax::None)
998 pub fn precedence(&self) -> ExprPrecedence {
1000 ExprKind::Box(_) => ExprPrecedence::Box,
1001 ExprKind::ObsoleteInPlace(..) => ExprPrecedence::ObsoleteInPlace,
1002 ExprKind::Array(_) => ExprPrecedence::Array,
1003 ExprKind::Call(..) => ExprPrecedence::Call,
1004 ExprKind::MethodCall(..) => ExprPrecedence::MethodCall,
1005 ExprKind::Tup(_) => ExprPrecedence::Tup,
1006 ExprKind::Binary(op, ..) => ExprPrecedence::Binary(op.node),
1007 ExprKind::Unary(..) => ExprPrecedence::Unary,
1008 ExprKind::Lit(_) => ExprPrecedence::Lit,
1009 ExprKind::Type(..) | ExprKind::Cast(..) => ExprPrecedence::Cast,
1010 ExprKind::If(..) => ExprPrecedence::If,
1011 ExprKind::IfLet(..) => ExprPrecedence::IfLet,
1012 ExprKind::While(..) => ExprPrecedence::While,
1013 ExprKind::WhileLet(..) => ExprPrecedence::WhileLet,
1014 ExprKind::ForLoop(..) => ExprPrecedence::ForLoop,
1015 ExprKind::Loop(..) => ExprPrecedence::Loop,
1016 ExprKind::Match(..) => ExprPrecedence::Match,
1017 ExprKind::Closure(..) => ExprPrecedence::Closure,
1018 ExprKind::Block(..) => ExprPrecedence::Block,
1019 ExprKind::TryBlock(..) => ExprPrecedence::TryBlock,
1020 ExprKind::Async(..) => ExprPrecedence::Async,
1021 ExprKind::Assign(..) => ExprPrecedence::Assign,
1022 ExprKind::AssignOp(..) => ExprPrecedence::AssignOp,
1023 ExprKind::Field(..) => ExprPrecedence::Field,
1024 ExprKind::Index(..) => ExprPrecedence::Index,
1025 ExprKind::Range(..) => ExprPrecedence::Range,
1026 ExprKind::Path(..) => ExprPrecedence::Path,
1027 ExprKind::AddrOf(..) => ExprPrecedence::AddrOf,
1028 ExprKind::Break(..) => ExprPrecedence::Break,
1029 ExprKind::Continue(..) => ExprPrecedence::Continue,
1030 ExprKind::Ret(..) => ExprPrecedence::Ret,
1031 ExprKind::InlineAsm(..) => ExprPrecedence::InlineAsm,
1032 ExprKind::Mac(..) => ExprPrecedence::Mac,
1033 ExprKind::Struct(..) => ExprPrecedence::Struct,
1034 ExprKind::Repeat(..) => ExprPrecedence::Repeat,
1035 ExprKind::Paren(..) => ExprPrecedence::Paren,
1036 ExprKind::Try(..) => ExprPrecedence::Try,
1037 ExprKind::Yield(..) => ExprPrecedence::Yield,
1042 impl fmt::Debug for Expr {
1043 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1044 write!(f, "expr({}: {})", self.id, pprust::expr_to_string(self))
1048 /// Limit types of a range (inclusive or exclusive)
1049 #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
1050 pub enum RangeLimits {
1051 /// Inclusive at the beginning, exclusive at the end
1053 /// Inclusive at the beginning and end
1057 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1059 /// A `box x` expression.
1061 /// First expr is the place; second expr is the value.
1062 ObsoleteInPlace(P<Expr>, P<Expr>),
1063 /// An array (`[a, b, c, d]`)
1064 Array(Vec<P<Expr>>),
1067 /// The first field resolves to the function itself,
1068 /// and the second field is the list of arguments.
1069 /// This also represents calling the constructor of
1070 /// tuple-like ADTs such as tuple structs and enum variants.
1071 Call(P<Expr>, Vec<P<Expr>>),
1072 /// A method call (`x.foo::<'static, Bar, Baz>(a, b, c, d)`)
1074 /// The `PathSegment` represents the method name and its generic arguments
1075 /// (within the angle brackets).
1076 /// The first element of the vector of `Expr`s is the expression that evaluates
1077 /// to the object on which the method is being called on (the receiver),
1078 /// and the remaining elements are the rest of the arguments.
1079 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
1080 /// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, [x, a, b, c, d])`.
1081 MethodCall(PathSegment, Vec<P<Expr>>),
1082 /// A tuple (`(a, b, c ,d)`)
1084 /// A binary operation (For example: `a + b`, `a * b`)
1085 Binary(BinOp, P<Expr>, P<Expr>),
1086 /// A unary operation (For example: `!x`, `*x`)
1087 Unary(UnOp, P<Expr>),
1088 /// A literal (For example: `1`, `"foo"`)
1090 /// A cast (`foo as f64`)
1091 Cast(P<Expr>, P<Ty>),
1092 Type(P<Expr>, P<Ty>),
1093 /// An `if` block, with an optional else block
1095 /// `if expr { block } else { expr }`
1096 If(P<Expr>, P<Block>, Option<P<Expr>>),
1097 /// An `if let` expression with an optional else block
1099 /// `if let pat = expr { block } else { expr }`
1101 /// This is desugared to a `match` expression.
1102 IfLet(Vec<P<Pat>>, P<Expr>, P<Block>, Option<P<Expr>>),
1103 /// A while loop, with an optional label
1105 /// `'label: while expr { block }`
1106 While(P<Expr>, P<Block>, Option<Label>),
1107 /// A while-let loop, with an optional label
1109 /// `'label: while let pat = expr { block }`
1111 /// This is desugared to a combination of `loop` and `match` expressions.
1112 WhileLet(Vec<P<Pat>>, P<Expr>, P<Block>, Option<Label>),
1113 /// A for loop, with an optional label
1115 /// `'label: for pat in expr { block }`
1117 /// This is desugared to a combination of `loop` and `match` expressions.
1118 ForLoop(P<Pat>, P<Expr>, P<Block>, Option<Label>),
1119 /// Conditionless loop (can be exited with break, continue, or return)
1121 /// `'label: loop { block }`
1122 Loop(P<Block>, Option<Label>),
1123 /// A `match` block.
1124 Match(P<Expr>, Vec<Arm>),
1125 /// A closure (for example, `move |a, b, c| a + b + c`)
1127 /// The final span is the span of the argument block `|...|`
1128 Closure(CaptureBy, IsAsync, Movability, P<FnDecl>, P<Expr>, Span),
1129 /// A block (`'label: { ... }`)
1130 Block(P<Block>, Option<Label>),
1131 /// An async block (`async move { ... }`)
1133 /// The `NodeId` is the `NodeId` for the closure that results from
1134 /// desugaring an async block, just like the NodeId field in the
1135 /// `IsAsync` enum. This is necessary in order to create a def for the
1136 /// closure which can be used as a parent of any child defs. Defs
1137 /// created during lowering cannot be made the parent of any other
1138 /// preexisting defs.
1139 Async(CaptureBy, NodeId, P<Block>),
1140 /// A try block (`try { ... }`)
1143 /// An assignment (`a = foo()`)
1144 Assign(P<Expr>, P<Expr>),
1145 /// An assignment with an operator
1147 /// For example, `a += 1`.
1148 AssignOp(BinOp, P<Expr>, P<Expr>),
1149 /// Access of a named (`obj.foo`) or unnamed (`obj.0`) struct field
1150 Field(P<Expr>, Ident),
1151 /// An indexing operation (`foo[2]`)
1152 Index(P<Expr>, P<Expr>),
1153 /// A range (`1..2`, `1..`, `..2`, `1...2`, `1...`, `...2`)
1154 Range(Option<P<Expr>>, Option<P<Expr>>, RangeLimits),
1156 /// Variable reference, possibly containing `::` and/or type
1157 /// parameters, e.g. foo::bar::<baz>.
1159 /// Optionally "qualified",
1160 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
1161 Path(Option<QSelf>, Path),
1163 /// A referencing operation (`&a` or `&mut a`)
1164 AddrOf(Mutability, P<Expr>),
1165 /// A `break`, with an optional label to break, and an optional expression
1166 Break(Option<Label>, Option<P<Expr>>),
1167 /// A `continue`, with an optional label
1168 Continue(Option<Label>),
1169 /// A `return`, with an optional value to be returned
1170 Ret(Option<P<Expr>>),
1172 /// Output of the `asm!()` macro
1173 InlineAsm(P<InlineAsm>),
1175 /// A macro invocation; pre-expansion
1178 /// A struct literal expression.
1180 /// For example, `Foo {x: 1, y: 2}`, or
1181 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
1182 Struct(Path, Vec<Field>, Option<P<Expr>>),
1184 /// An array literal constructed from one repeated element.
1186 /// For example, `[1; 5]`. The expression is the element to be
1187 /// repeated; the constant is the number of times to repeat it.
1188 Repeat(P<Expr>, AnonConst),
1190 /// No-op: used solely so we can pretty-print faithfully
1196 /// A `yield`, with an optional value to be yielded
1197 Yield(Option<P<Expr>>),
1200 /// The explicit Self type in a "qualified path". The actual
1201 /// path, including the trait and the associated item, is stored
1202 /// separately. `position` represents the index of the associated
1203 /// item qualified with this Self type.
1205 /// ```ignore (only-for-syntax-highlight)
1206 /// <Vec<T> as a::b::Trait>::AssociatedItem
1207 /// ^~~~~ ~~~~~~~~~~~~~~^
1210 /// <Vec<T>>::AssociatedItem
1214 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1218 /// The span of `a::b::Trait` in a path like `<Vec<T> as
1219 /// a::b::Trait>::AssociatedItem`; in the case where `position ==
1220 /// 0`, this is an empty span.
1221 pub path_span: Span,
1222 pub position: usize,
1225 /// A capture clause
1226 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
1227 pub enum CaptureBy {
1232 /// The movability of a generator / closure literal
1233 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
1234 pub enum Movability {
1239 pub type Mac = Spanned<Mac_>;
1241 /// Represents a macro invocation. The Path indicates which macro
1242 /// is being invoked, and the vector of token-trees contains the source
1243 /// of the macro invocation.
1245 /// NB: the additional ident for a macro_rules-style macro is actually
1246 /// stored in the enclosing item. Oog.
1247 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1250 pub delim: MacDelimiter,
1251 pub tts: ThinTokenStream,
1254 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
1255 pub enum MacDelimiter {
1262 pub fn stream(&self) -> TokenStream {
1263 self.tts.clone().into()
1267 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1268 pub struct MacroDef {
1269 pub tokens: ThinTokenStream,
1274 pub fn stream(&self) -> TokenStream {
1275 self.tokens.clone().into()
1279 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy, Hash, PartialEq)]
1281 /// A regular string, like `"foo"`
1283 /// A raw string, like `r##"foo"##`
1285 /// The value is the number of `#` symbols used.
1290 pub type Lit = Spanned<LitKind>;
1292 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy, Hash, PartialEq)]
1293 pub enum LitIntType {
1301 /// E.g. `"foo"`, `42`, `12.34` or `bool`
1302 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash, PartialEq)]
1304 /// A string literal (`"foo"`)
1305 Str(Symbol, StrStyle),
1306 /// A byte string (`b"foo"`)
1307 ByteStr(Lrc<Vec<u8>>),
1308 /// A byte char (`b'f'`)
1310 /// A character literal (`'a'`)
1312 /// An integer literal (`1`)
1313 Int(u128, LitIntType),
1314 /// A float literal (`1f64` or `1E10f64`)
1315 Float(Symbol, FloatTy),
1316 /// A float literal without a suffix (`1.0 or 1.0E10`)
1317 FloatUnsuffixed(Symbol),
1318 /// A boolean literal
1323 /// Returns true if this literal is a string and false otherwise.
1324 pub fn is_str(&self) -> bool {
1326 LitKind::Str(..) => true,
1331 /// Returns true if this literal is byte literal string false otherwise.
1332 pub fn is_bytestr(&self) -> bool {
1334 LitKind::ByteStr(_) => true,
1339 /// Returns true if this is a numeric literal.
1340 pub fn is_numeric(&self) -> bool {
1342 LitKind::Int(..) | LitKind::Float(..) | LitKind::FloatUnsuffixed(..) => true,
1347 /// Returns true if this literal has no suffix. Note: this will return true
1348 /// for literals with prefixes such as raw strings and byte strings.
1349 pub fn is_unsuffixed(&self) -> bool {
1351 // unsuffixed variants
1353 | LitKind::ByteStr(..)
1356 | LitKind::Int(_, LitIntType::Unsuffixed)
1357 | LitKind::FloatUnsuffixed(..)
1358 | LitKind::Bool(..) => true,
1359 // suffixed variants
1360 LitKind::Int(_, LitIntType::Signed(..))
1361 | LitKind::Int(_, LitIntType::Unsigned(..))
1362 | LitKind::Float(..) => false,
1366 /// Returns true if this literal has a suffix.
1367 pub fn is_suffixed(&self) -> bool {
1368 !self.is_unsuffixed()
1372 // NB: If you change this, you'll probably want to change the corresponding
1373 // type structure in middle/ty.rs as well.
1374 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1377 pub mutbl: Mutability,
1380 /// Represents a method's signature in a trait declaration,
1381 /// or in an implementation.
1382 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1383 pub struct MethodSig {
1384 pub header: FnHeader,
1385 pub decl: P<FnDecl>,
1388 /// Represents an item declaration within a trait declaration,
1389 /// possibly including a default implementation. A trait item is
1390 /// either required (meaning it doesn't have an implementation, just a
1391 /// signature) or provided (meaning it has a default implementation).
1392 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1393 pub struct TraitItem {
1396 pub attrs: Vec<Attribute>,
1397 pub generics: Generics,
1398 pub node: TraitItemKind,
1400 /// See `Item::tokens` for what this is
1401 pub tokens: Option<TokenStream>,
1404 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1405 pub enum TraitItemKind {
1406 Const(P<Ty>, Option<P<Expr>>),
1407 Method(MethodSig, Option<P<Block>>),
1408 Type(GenericBounds, Option<P<Ty>>),
1412 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1413 pub struct ImplItem {
1416 pub vis: Visibility,
1417 pub defaultness: Defaultness,
1418 pub attrs: Vec<Attribute>,
1419 pub generics: Generics,
1420 pub node: ImplItemKind,
1422 /// See `Item::tokens` for what this is
1423 pub tokens: Option<TokenStream>,
1426 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1427 pub enum ImplItemKind {
1428 Const(P<Ty>, P<Expr>),
1429 Method(MethodSig, P<Block>),
1431 Existential(GenericBounds),
1435 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable, Copy)]
1445 impl fmt::Debug for IntTy {
1446 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1447 fmt::Display::fmt(self, f)
1451 impl fmt::Display for IntTy {
1452 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1453 write!(f, "{}", self.ty_to_string())
1458 pub fn ty_to_string(&self) -> &'static str {
1460 IntTy::Isize => "isize",
1462 IntTy::I16 => "i16",
1463 IntTy::I32 => "i32",
1464 IntTy::I64 => "i64",
1465 IntTy::I128 => "i128",
1469 pub fn val_to_string(&self, val: i128) -> String {
1470 // cast to a u128 so we can correctly print INT128_MIN. All integral types
1471 // are parsed as u128, so we wouldn't want to print an extra negative
1473 format!("{}{}", val as u128, self.ty_to_string())
1476 pub fn bit_width(&self) -> Option<usize> {
1478 IntTy::Isize => return None,
1488 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable, Copy)]
1499 pub fn ty_to_string(&self) -> &'static str {
1501 UintTy::Usize => "usize",
1503 UintTy::U16 => "u16",
1504 UintTy::U32 => "u32",
1505 UintTy::U64 => "u64",
1506 UintTy::U128 => "u128",
1510 pub fn val_to_string(&self, val: u128) -> String {
1511 format!("{}{}", val, self.ty_to_string())
1514 pub fn bit_width(&self) -> Option<usize> {
1516 UintTy::Usize => return None,
1521 UintTy::U128 => 128,
1526 impl fmt::Debug for UintTy {
1527 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1528 fmt::Display::fmt(self, f)
1532 impl fmt::Display for UintTy {
1533 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1534 write!(f, "{}", self.ty_to_string())
1538 // Bind a type to an associated type: `A=Foo`.
1539 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1540 pub struct TypeBinding {
1547 #[derive(Clone, RustcEncodable, RustcDecodable)]
1554 impl fmt::Debug for Ty {
1555 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1556 write!(f, "type({})", pprust::ty_to_string(self))
1560 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1561 pub struct BareFnTy {
1562 pub unsafety: Unsafety,
1564 pub generic_params: Vec<GenericParam>,
1565 pub decl: P<FnDecl>,
1568 /// The different kinds of types recognized by the compiler
1569 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1571 /// A variable-length slice (`[T]`)
1573 /// A fixed length array (`[T; n]`)
1574 Array(P<Ty>, AnonConst),
1575 /// A raw pointer (`*const T` or `*mut T`)
1577 /// A reference (`&'a T` or `&'a mut T`)
1578 Rptr(Option<Lifetime>, MutTy),
1579 /// A bare function (e.g. `fn(usize) -> bool`)
1580 BareFn(P<BareFnTy>),
1581 /// The never type (`!`)
1583 /// A tuple (`(A, B, C, D,...)`)
1585 /// A path (`module::module::...::Type`), optionally
1586 /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
1588 /// Type parameters are stored in the Path itself
1589 Path(Option<QSelf>, Path),
1590 /// A trait object type `Bound1 + Bound2 + Bound3`
1591 /// where `Bound` is a trait or a lifetime.
1592 TraitObject(GenericBounds, TraitObjectSyntax),
1593 /// An `impl Bound1 + Bound2 + Bound3` type
1594 /// where `Bound` is a trait or a lifetime.
1596 /// The `NodeId` exists to prevent lowering from having to
1597 /// generate `NodeId`s on the fly, which would complicate
1598 /// the generation of `existential type` items significantly
1599 ImplTrait(NodeId, GenericBounds),
1600 /// No-op; kept solely so that we can pretty-print faithfully
1604 /// TyKind::Infer means the type should be inferred instead of it having been
1605 /// specified. This can appear anywhere in a type.
1607 /// Inferred type of a `self` or `&self` argument in a method.
1609 // A macro in the type position.
1611 /// Placeholder for a kind that has failed to be defined.
1616 pub fn is_implicit_self(&self) -> bool {
1617 if let TyKind::ImplicitSelf = *self {
1624 pub fn is_unit(&self) -> bool {
1625 if let TyKind::Tup(ref tys) = *self {
1633 /// Syntax used to declare a trait object.
1634 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
1635 pub enum TraitObjectSyntax {
1640 /// Inline assembly dialect.
1642 /// E.g. `"intel"` as in `asm!("mov eax, 2" : "={eax}"(result) : : : "intel")`
1643 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
1644 pub enum AsmDialect {
1649 /// Inline assembly.
1651 /// E.g. `"={eax}"(result)` as in `asm!("mov eax, 2" : "={eax}"(result) : : : "intel")`
1652 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1653 pub struct InlineAsmOutput {
1654 pub constraint: Symbol,
1657 pub is_indirect: bool,
1660 /// Inline assembly.
1662 /// E.g. `asm!("NOP");`
1663 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1664 pub struct InlineAsm {
1666 pub asm_str_style: StrStyle,
1667 pub outputs: Vec<InlineAsmOutput>,
1668 pub inputs: Vec<(Symbol, P<Expr>)>,
1669 pub clobbers: Vec<Symbol>,
1671 pub alignstack: bool,
1672 pub dialect: AsmDialect,
1673 pub ctxt: SyntaxContext,
1676 /// An argument in a function header.
1678 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
1679 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1686 /// Alternative representation for `Arg`s describing `self` parameter of methods.
1688 /// E.g. `&mut self` as in `fn foo(&mut self)`
1689 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1691 /// `self`, `mut self`
1693 /// `&'lt self`, `&'lt mut self`
1694 Region(Option<Lifetime>, Mutability),
1695 /// `self: TYPE`, `mut self: TYPE`
1696 Explicit(P<Ty>, Mutability),
1699 pub type ExplicitSelf = Spanned<SelfKind>;
1702 pub fn to_self(&self) -> Option<ExplicitSelf> {
1703 if let PatKind::Ident(BindingMode::ByValue(mutbl), ident, _) = self.pat.node {
1704 if ident.name == keywords::SelfValue.name() {
1705 return match self.ty.node {
1706 TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
1707 TyKind::Rptr(lt, MutTy { ref ty, mutbl }) if ty.node.is_implicit_self() => {
1708 Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
1711 self.pat.span.to(self.ty.span),
1712 SelfKind::Explicit(self.ty.clone(), mutbl),
1720 pub fn is_self(&self) -> bool {
1721 if let PatKind::Ident(_, ident, _) = self.pat.node {
1722 ident.name == keywords::SelfValue.name()
1728 pub fn from_self(eself: ExplicitSelf, eself_ident: Ident) -> Arg {
1729 let span = eself.span.to(eself_ident.span);
1730 let infer_ty = P(Ty {
1732 node: TyKind::ImplicitSelf,
1735 let arg = |mutbl, ty| Arg {
1738 node: PatKind::Ident(BindingMode::ByValue(mutbl), eself_ident, None),
1745 SelfKind::Explicit(ty, mutbl) => arg(mutbl, ty),
1746 SelfKind::Value(mutbl) => arg(mutbl, infer_ty),
1747 SelfKind::Region(lt, mutbl) => arg(
1748 Mutability::Immutable,
1765 /// Header (not the body) of a function declaration.
1767 /// E.g. `fn foo(bar: baz)`
1768 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1770 pub inputs: Vec<Arg>,
1771 pub output: FunctionRetTy,
1776 pub fn get_self(&self) -> Option<ExplicitSelf> {
1777 self.inputs.get(0).and_then(Arg::to_self)
1779 pub fn has_self(&self) -> bool {
1780 self.inputs.get(0).map(Arg::is_self).unwrap_or(false)
1784 /// Is the trait definition an auto trait?
1785 #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
1791 #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
1797 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
1801 return_impl_trait_id: NodeId,
1807 pub fn is_async(self) -> bool {
1808 if let IsAsync::Async { .. } = self {
1814 /// In case this is an `Async` return the `NodeId` for the generated impl Trait item
1815 pub fn opt_return_id(self) -> Option<NodeId> {
1818 return_impl_trait_id,
1820 } => Some(return_impl_trait_id),
1821 IsAsync::NotAsync => None,
1826 #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
1827 pub enum Constness {
1832 #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
1833 pub enum Defaultness {
1838 impl fmt::Display for Unsafety {
1839 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1842 Unsafety::Normal => "normal",
1843 Unsafety::Unsafe => "unsafe",
1850 #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
1851 pub enum ImplPolarity {
1852 /// `impl Trait for Type`
1854 /// `impl !Trait for Type`
1858 impl fmt::Debug for ImplPolarity {
1859 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1861 ImplPolarity::Positive => "positive".fmt(f),
1862 ImplPolarity::Negative => "negative".fmt(f),
1867 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1868 pub enum FunctionRetTy {
1869 /// Return type is not specified.
1871 /// Functions default to `()` and
1872 /// closures default to inference. Span points to where return
1873 /// type would be inserted.
1879 impl FunctionRetTy {
1880 pub fn span(&self) -> Span {
1882 FunctionRetTy::Default(span) => span,
1883 FunctionRetTy::Ty(ref ty) => ty.span,
1888 /// Module declaration.
1890 /// E.g. `mod foo;` or `mod foo { .. }`
1891 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1893 /// A span from the first token past `{` to the last token until `}`.
1894 /// For `mod foo;`, the inner span ranges from the first token
1895 /// to the last token in the external file.
1897 pub items: Vec<P<Item>>,
1898 /// For `mod foo;` inline is false, for `mod foo { .. }` it is true.
1902 /// Foreign module declaration.
1904 /// E.g. `extern { .. }` or `extern C { .. }`
1905 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1906 pub struct ForeignMod {
1908 pub items: Vec<ForeignItem>,
1911 /// Global inline assembly
1913 /// aka module-level assembly or file-scoped assembly
1914 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy)]
1915 pub struct GlobalAsm {
1917 pub ctxt: SyntaxContext,
1920 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1921 pub struct EnumDef {
1922 pub variants: Vec<Variant>,
1925 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1926 pub struct Variant_ {
1928 pub attrs: Vec<Attribute>,
1929 pub data: VariantData,
1930 /// Explicit discriminant, e.g. `Foo = 1`
1931 pub disr_expr: Option<AnonConst>,
1934 pub type Variant = Spanned<Variant_>;
1936 /// Part of `use` item to the right of its prefix.
1937 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1938 pub enum UseTreeKind {
1939 /// `use prefix` or `use prefix as rename`
1941 /// The extra `NodeId`s are for HIR lowering, when additional statements are created for each
1943 Simple(Option<Ident>, NodeId, NodeId),
1944 /// `use prefix::{...}`
1945 Nested(Vec<(UseTree, NodeId)>),
1950 /// A tree of paths sharing common prefixes.
1951 /// Used in `use` items both at top-level and inside of braces in import groups.
1952 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1953 pub struct UseTree {
1955 pub kind: UseTreeKind,
1960 pub fn ident(&self) -> Ident {
1962 UseTreeKind::Simple(Some(rename), ..) => rename,
1963 UseTreeKind::Simple(None, ..) => {
1967 .expect("empty prefix in a simple import")
1970 _ => panic!("`UseTree::ident` can only be used on a simple import"),
1975 /// Distinguishes between Attributes that decorate items and Attributes that
1976 /// are contained as statements within items. These two cases need to be
1977 /// distinguished for pretty-printing.
1978 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
1979 pub enum AttrStyle {
1985 Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, PartialOrd, Ord, Copy,
1987 pub struct AttrId(pub usize);
1989 impl Idx for AttrId {
1990 fn new(idx: usize) -> Self {
1993 fn index(self) -> usize {
1998 /// Meta-data associated with an item
1999 /// Doc-comments are promoted to attributes that have is_sugared_doc = true
2000 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2001 pub struct Attribute {
2003 pub style: AttrStyle,
2005 pub tokens: TokenStream,
2006 pub is_sugared_doc: bool,
2010 /// TraitRef's appear in impls.
2012 /// resolve maps each TraitRef's ref_id to its defining trait; that's all
2013 /// that the ref_id is for. The impl_id maps to the "self type" of this impl.
2014 /// If this impl is an ItemKind::Impl, the impl_id is redundant (it could be the
2015 /// same as the impl's node id).
2016 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2017 pub struct TraitRef {
2022 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2023 pub struct PolyTraitRef {
2024 /// The `'a` in `<'a> Foo<&'a T>`
2025 pub bound_generic_params: Vec<GenericParam>,
2027 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
2028 pub trait_ref: TraitRef,
2034 pub fn new(generic_params: Vec<GenericParam>, path: Path, span: Span) -> Self {
2036 bound_generic_params: generic_params,
2037 trait_ref: TraitRef {
2039 ref_id: DUMMY_NODE_ID,
2046 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
2047 pub enum CrateSugar {
2048 /// Source is `pub(crate)`
2051 /// Source is (just) `crate`
2055 pub type Visibility = Spanned<VisibilityKind>;
2057 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2058 pub enum VisibilityKind {
2061 Restricted { path: P<Path>, id: NodeId },
2065 impl VisibilityKind {
2066 pub fn is_pub(&self) -> bool {
2067 if let VisibilityKind::Public = *self {
2075 /// Field of a struct.
2077 /// E.g. `bar: usize` as in `struct Foo { bar: usize }`
2078 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2079 pub struct StructField {
2081 pub ident: Option<Ident>,
2082 pub vis: Visibility,
2085 pub attrs: Vec<Attribute>,
2088 /// Fields and Ids of enum variants and structs
2090 /// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all
2091 /// variant kinds) and an Id of the variant's constructor (not relevant for `Struct`-variants).
2092 /// One shared Id can be successfully used for these two purposes.
2093 /// Id of the whole enum lives in `Item`.
2095 /// For structs: `NodeId` represents an Id of the structure's constructor, so it is not actually
2096 /// used for `Struct`-structs (but still presents). Structures don't have an analogue of "Id of
2097 /// the variant itself" from enum variants.
2098 /// Id of the whole struct lives in `Item`.
2099 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2100 pub enum VariantData {
2103 /// E.g. `Bar { .. }` as in `enum Foo { Bar { .. } }`
2104 Struct(Vec<StructField>, NodeId),
2107 /// E.g. `Bar(..)` as in `enum Foo { Bar(..) }`
2108 Tuple(Vec<StructField>, NodeId),
2111 /// E.g. `Bar = ..` as in `enum Foo { Bar = .. }`
2116 pub fn fields(&self) -> &[StructField] {
2118 VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => fields,
2122 pub fn id(&self) -> NodeId {
2124 VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id,
2127 pub fn is_struct(&self) -> bool {
2128 if let VariantData::Struct(..) = *self {
2134 pub fn is_tuple(&self) -> bool {
2135 if let VariantData::Tuple(..) = *self {
2141 pub fn is_unit(&self) -> bool {
2142 if let VariantData::Unit(..) = *self {
2152 /// The name might be a dummy name in case of anonymous items
2153 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2156 pub attrs: Vec<Attribute>,
2159 pub vis: Visibility,
2162 /// Original tokens this item was parsed from. This isn't necessarily
2163 /// available for all items, although over time more and more items should
2164 /// have this be `Some`. Right now this is primarily used for procedural
2165 /// macros, notably custom attributes.
2167 /// Note that the tokens here do not include the outer attributes, but will
2168 /// include inner attributes.
2169 pub tokens: Option<TokenStream>,
2172 /// A function header
2174 /// All the information between the visibility & the name of the function is
2175 /// included in this struct (e.g. `async unsafe fn` or `const extern "C" fn`)
2176 #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)]
2177 pub struct FnHeader {
2178 pub unsafety: Unsafety,
2179 pub asyncness: IsAsync,
2180 pub constness: Spanned<Constness>,
2184 impl Default for FnHeader {
2185 fn default() -> FnHeader {
2187 unsafety: Unsafety::Normal,
2188 asyncness: IsAsync::NotAsync,
2189 constness: dummy_spanned(Constness::NotConst),
2195 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2197 /// An `extern crate` item, with optional *original* crate name if the crate was renamed.
2199 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
2200 ExternCrate(Option<Name>),
2201 /// A use declaration (`use` or `pub use`) item.
2203 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
2205 /// A static item (`static` or `pub static`).
2207 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
2208 Static(P<Ty>, Mutability, P<Expr>),
2209 /// A constant item (`const` or `pub const`).
2211 /// E.g. `const FOO: i32 = 42;`
2212 Const(P<Ty>, P<Expr>),
2213 /// A function declaration (`fn` or `pub fn`).
2215 /// E.g. `fn foo(bar: usize) -> usize { .. }`
2216 Fn(P<FnDecl>, FnHeader, Generics, P<Block>),
2217 /// A module declaration (`mod` or `pub mod`).
2219 /// E.g. `mod foo;` or `mod foo { .. }`
2221 /// An external module (`extern` or `pub extern`).
2223 /// E.g. `extern {}` or `extern "C" {}`
2224 ForeignMod(ForeignMod),
2225 /// Module-level inline assembly (from `global_asm!()`)
2226 GlobalAsm(P<GlobalAsm>),
2227 /// A type alias (`type` or `pub type`).
2229 /// E.g. `type Foo = Bar<u8>;`
2230 Ty(P<Ty>, Generics),
2231 /// An existential type declaration (`existential type`).
2233 /// E.g. `existential type Foo: Bar + Boo;`
2234 Existential(GenericBounds, Generics),
2235 /// An enum definition (`enum` or `pub enum`).
2237 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
2238 Enum(EnumDef, Generics),
2239 /// A struct definition (`struct` or `pub struct`).
2241 /// E.g. `struct Foo<A> { x: A }`
2242 Struct(VariantData, Generics),
2243 /// A union definition (`union` or `pub union`).
2245 /// E.g. `union Foo<A, B> { x: A, y: B }`
2246 Union(VariantData, Generics),
2247 /// A Trait declaration (`trait` or `pub trait`).
2249 /// E.g. `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`
2250 Trait(IsAuto, Unsafety, Generics, GenericBounds, Vec<TraitItem>),
2253 /// E.g. `trait Foo = Bar + Quux;`
2254 TraitAlias(Generics, GenericBounds),
2255 /// An implementation.
2257 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
2263 Option<TraitRef>, // (optional) trait this impl implements
2267 /// A macro invocation.
2269 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
2272 /// A macro definition.
2277 pub fn descriptive_variant(&self) -> &str {
2279 ItemKind::ExternCrate(..) => "extern crate",
2280 ItemKind::Use(..) => "use",
2281 ItemKind::Static(..) => "static item",
2282 ItemKind::Const(..) => "constant item",
2283 ItemKind::Fn(..) => "function",
2284 ItemKind::Mod(..) => "module",
2285 ItemKind::ForeignMod(..) => "foreign module",
2286 ItemKind::GlobalAsm(..) => "global asm",
2287 ItemKind::Ty(..) => "type alias",
2288 ItemKind::Existential(..) => "existential type",
2289 ItemKind::Enum(..) => "enum",
2290 ItemKind::Struct(..) => "struct",
2291 ItemKind::Union(..) => "union",
2292 ItemKind::Trait(..) => "trait",
2293 ItemKind::TraitAlias(..) => "trait alias",
2294 ItemKind::Mac(..) | ItemKind::MacroDef(..) | ItemKind::Impl(..) => "item",
2299 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2300 pub struct ForeignItem {
2302 pub attrs: Vec<Attribute>,
2303 pub node: ForeignItemKind,
2306 pub vis: Visibility,
2309 /// An item within an `extern` block
2310 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2311 pub enum ForeignItemKind {
2312 /// A foreign function
2313 Fn(P<FnDecl>, Generics),
2314 /// A foreign static item (`static ext: u8`), with optional mutability
2315 /// (the boolean is true when mutable)
2316 Static(P<Ty>, bool),
2319 /// A macro invocation
2323 impl ForeignItemKind {
2324 pub fn descriptive_variant(&self) -> &str {
2326 ForeignItemKind::Fn(..) => "foreign function",
2327 ForeignItemKind::Static(..) => "foreign static item",
2328 ForeignItemKind::Ty => "foreign type",
2329 ForeignItemKind::Macro(..) => "macro in foreign module",
2339 // are ASTs encodable?
2341 fn check_asts_encodable() {
2342 fn assert_encodable<T: serialize::Encodable>() {}
2343 assert_encodable::<Crate>();