]> git.lizzy.rs Git - rust.git/blob - src/librustc_ast/ast.rs
Rollup merge of #69830 - RalfJung:miri-invalid-terminator, r=oli-obk
[rust.git] / src / librustc_ast / ast.rs
1 //! The Rust abstract syntax tree module.
2 //!
3 //! This module contains common structures forming the language AST.
4 //! Two main entities in the module are [`Item`] (which represents an AST element with
5 //! additional metadata), and [`ItemKind`] (which represents a concrete type and contains
6 //! information specific to the type of the item).
7 //!
8 //! Other module items that worth mentioning:
9 //! - [`Ty`] and [`TyKind`]: A parsed Rust type.
10 //! - [`Expr`] and [`ExprKind`]: A parsed Rust expression.
11 //! - [`Pat`] and [`PatKind`]: A parsed Rust pattern. Patterns are often dual to expressions.
12 //! - [`Stmt`] and [`StmtKind`]: An executable action that does not return a value.
13 //! - [`FnDecl`], [`FnHeader`] and [`Param`]: Metadata associated with a function declaration.
14 //! - [`Generics`], [`GenericParam`], [`WhereClause`]: Metadata associated with generic parameters.
15 //! - [`EnumDef`] and [`Variant`]: Enum declaration.
16 //! - [`Lit`] and [`LitKind`]: Literal expressions.
17 //! - [`MacroDef`], [`MacStmtStyle`], [`Mac`], [`MacDelimeter`]: Macro definition and invocation.
18 //! - [`Attribute`]: Metadata associated with item.
19 //! - [`UnOp`], [`UnOpKind`], [`BinOp`], [`BinOpKind`]: Unary and binary operators.
20
21 pub use crate::util::parser::ExprPrecedence;
22 pub use GenericArgs::*;
23 pub use UnsafeSource::*;
24
25 pub use rustc_span::symbol::{Ident, Symbol as Name};
26
27 use crate::ptr::P;
28 use crate::token::{self, DelimToken};
29 use crate::tokenstream::{DelimSpan, TokenStream, TokenTree};
30
31 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
32 use rustc_data_structures::sync::Lrc;
33 use rustc_data_structures::thin_vec::ThinVec;
34 use rustc_index::vec::Idx;
35 use rustc_macros::HashStable_Generic;
36 use rustc_serialize::{self, Decoder, Encoder};
37 use rustc_span::source_map::{respan, Spanned};
38 use rustc_span::symbol::{kw, sym, Symbol};
39 use rustc_span::{Span, DUMMY_SP};
40
41 use std::convert::TryFrom;
42 use std::fmt;
43 use std::iter;
44
45 #[cfg(test)]
46 mod tests;
47
48 /// A "Label" is an identifier of some point in sources,
49 /// e.g. in the following code:
50 ///
51 /// ```rust
52 /// 'outer: loop {
53 ///     break 'outer;
54 /// }
55 /// ```
56 ///
57 /// `'outer` is a label.
58 #[derive(Clone, RustcEncodable, RustcDecodable, Copy, HashStable_Generic)]
59 pub struct Label {
60     pub ident: Ident,
61 }
62
63 impl fmt::Debug for Label {
64     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65         write!(f, "label({:?})", self.ident)
66     }
67 }
68
69 /// A "Lifetime" is an annotation of the scope in which variable
70 /// can be used, e.g. `'a` in `&'a i32`.
71 #[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
72 pub struct Lifetime {
73     pub id: NodeId,
74     pub ident: Ident,
75 }
76
77 impl fmt::Debug for Lifetime {
78     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79         write!(f, "lifetime({}: {})", self.id, self)
80     }
81 }
82
83 impl fmt::Display for Lifetime {
84     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85         write!(f, "{}", self.ident.name)
86     }
87 }
88
89 /// A "Path" is essentially Rust's notion of a name.
90 ///
91 /// It's represented as a sequence of identifiers,
92 /// along with a bunch of supporting information.
93 ///
94 /// E.g., `std::cmp::PartialEq`.
95 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
96 pub struct Path {
97     pub span: Span,
98     /// The segments in the path: the things separated by `::`.
99     /// Global paths begin with `kw::PathRoot`.
100     pub segments: Vec<PathSegment>,
101 }
102
103 impl PartialEq<Symbol> for Path {
104     fn eq(&self, symbol: &Symbol) -> bool {
105         self.segments.len() == 1 && { self.segments[0].ident.name == *symbol }
106     }
107 }
108
109 impl<CTX> HashStable<CTX> for Path {
110     fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
111         self.segments.len().hash_stable(hcx, hasher);
112         for segment in &self.segments {
113             segment.ident.name.hash_stable(hcx, hasher);
114         }
115     }
116 }
117
118 impl Path {
119     // Convert a span and an identifier to the corresponding
120     // one-segment path.
121     pub fn from_ident(ident: Ident) -> Path {
122         Path { segments: vec![PathSegment::from_ident(ident)], span: ident.span }
123     }
124
125     pub fn is_global(&self) -> bool {
126         !self.segments.is_empty() && self.segments[0].ident.name == kw::PathRoot
127     }
128 }
129
130 /// A segment of a path: an identifier, an optional lifetime, and a set of types.
131 ///
132 /// E.g., `std`, `String` or `Box<T>`.
133 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
134 pub struct PathSegment {
135     /// The identifier portion of this path segment.
136     pub ident: Ident,
137
138     pub id: NodeId,
139
140     /// Type/lifetime parameters attached to this path. They come in
141     /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`.
142     /// `None` means that no parameter list is supplied (`Path`),
143     /// `Some` means that parameter list is supplied (`Path<X, Y>`)
144     /// but it can be empty (`Path<>`).
145     /// `P` is used as a size optimization for the common case with no parameters.
146     pub args: Option<P<GenericArgs>>,
147 }
148
149 impl PathSegment {
150     pub fn from_ident(ident: Ident) -> Self {
151         PathSegment { ident, id: DUMMY_NODE_ID, args: None }
152     }
153     pub fn path_root(span: Span) -> Self {
154         PathSegment::from_ident(Ident::new(kw::PathRoot, span))
155     }
156 }
157
158 /// The arguments of a path segment.
159 ///
160 /// E.g., `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`.
161 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
162 pub enum GenericArgs {
163     /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`.
164     AngleBracketed(AngleBracketedArgs),
165     /// The `(A, B)` and `C` in `Foo(A, B) -> C`.
166     Parenthesized(ParenthesizedArgs),
167 }
168
169 impl GenericArgs {
170     pub fn is_parenthesized(&self) -> bool {
171         match *self {
172             Parenthesized(..) => true,
173             _ => false,
174         }
175     }
176
177     pub fn is_angle_bracketed(&self) -> bool {
178         match *self {
179             AngleBracketed(..) => true,
180             _ => false,
181         }
182     }
183
184     pub fn span(&self) -> Span {
185         match *self {
186             AngleBracketed(ref data) => data.span,
187             Parenthesized(ref data) => data.span,
188         }
189     }
190 }
191
192 /// Concrete argument in the sequence of generic args.
193 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
194 pub enum GenericArg {
195     /// `'a` in `Foo<'a>`
196     Lifetime(Lifetime),
197     /// `Bar` in `Foo<Bar>`
198     Type(P<Ty>),
199     /// `1` in `Foo<1>`
200     Const(AnonConst),
201 }
202
203 impl GenericArg {
204     pub fn span(&self) -> Span {
205         match self {
206             GenericArg::Lifetime(lt) => lt.ident.span,
207             GenericArg::Type(ty) => ty.span,
208             GenericArg::Const(ct) => ct.value.span,
209         }
210     }
211 }
212
213 /// A path like `Foo<'a, T>`.
214 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Default)]
215 pub struct AngleBracketedArgs {
216     /// The overall span.
217     pub span: Span,
218     /// The arguments for this path segment.
219     pub args: Vec<GenericArg>,
220     /// Constraints on associated types, if any.
221     /// E.g., `Foo<A = Bar, B: Baz>`.
222     pub constraints: Vec<AssocTyConstraint>,
223 }
224
225 impl Into<Option<P<GenericArgs>>> for AngleBracketedArgs {
226     fn into(self) -> Option<P<GenericArgs>> {
227         Some(P(GenericArgs::AngleBracketed(self)))
228     }
229 }
230
231 impl Into<Option<P<GenericArgs>>> for ParenthesizedArgs {
232     fn into(self) -> Option<P<GenericArgs>> {
233         Some(P(GenericArgs::Parenthesized(self)))
234     }
235 }
236
237 /// A path like `Foo(A, B) -> C`.
238 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
239 pub struct ParenthesizedArgs {
240     /// Overall span
241     pub span: Span,
242
243     /// `(A, B)`
244     pub inputs: Vec<P<Ty>>,
245
246     /// `C`
247     pub output: FnRetTy,
248 }
249
250 impl ParenthesizedArgs {
251     pub fn as_angle_bracketed_args(&self) -> AngleBracketedArgs {
252         AngleBracketedArgs {
253             span: self.span,
254             args: self.inputs.iter().cloned().map(|input| GenericArg::Type(input)).collect(),
255             constraints: vec![],
256         }
257     }
258 }
259
260 pub use crate::node_id::{NodeId, CRATE_NODE_ID, DUMMY_NODE_ID};
261
262 /// A modifier on a bound, e.g., `?Sized` or `?const Trait`.
263 ///
264 /// Negative bounds should also be handled here.
265 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
266 pub enum TraitBoundModifier {
267     /// No modifiers
268     None,
269
270     /// `?Trait`
271     Maybe,
272
273     /// `?const Trait`
274     MaybeConst,
275
276     /// `?const ?Trait`
277     //
278     // This parses but will be rejected during AST validation.
279     MaybeConstMaybe,
280 }
281
282 /// The AST represents all type param bounds as types.
283 /// `typeck::collect::compute_bounds` matches these against
284 /// the "special" built-in traits (see `middle::lang_items`) and
285 /// detects `Copy`, `Send` and `Sync`.
286 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
287 pub enum GenericBound {
288     Trait(PolyTraitRef, TraitBoundModifier),
289     Outlives(Lifetime),
290 }
291
292 impl GenericBound {
293     pub fn span(&self) -> Span {
294         match self {
295             &GenericBound::Trait(ref t, ..) => t.span,
296             &GenericBound::Outlives(ref l) => l.ident.span,
297         }
298     }
299 }
300
301 pub type GenericBounds = Vec<GenericBound>;
302
303 /// Specifies the enforced ordering for generic parameters. In the future,
304 /// if we wanted to relax this order, we could override `PartialEq` and
305 /// `PartialOrd`, to allow the kinds to be unordered.
306 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
307 pub enum ParamKindOrd {
308     Lifetime,
309     Type,
310     Const,
311 }
312
313 impl fmt::Display for ParamKindOrd {
314     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
315         match self {
316             ParamKindOrd::Lifetime => "lifetime".fmt(f),
317             ParamKindOrd::Type => "type".fmt(f),
318             ParamKindOrd::Const => "const".fmt(f),
319         }
320     }
321 }
322
323 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
324 pub enum GenericParamKind {
325     /// A lifetime definition (e.g., `'a: 'b + 'c + 'd`).
326     Lifetime,
327     Type {
328         default: Option<P<Ty>>,
329     },
330     Const {
331         ty: P<Ty>,
332     },
333 }
334
335 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
336 pub struct GenericParam {
337     pub id: NodeId,
338     pub ident: Ident,
339     pub attrs: AttrVec,
340     pub bounds: GenericBounds,
341     pub is_placeholder: bool,
342     pub kind: GenericParamKind,
343 }
344
345 /// Represents lifetime, type and const parameters attached to a declaration of
346 /// a function, enum, trait, etc.
347 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
348 pub struct Generics {
349     pub params: Vec<GenericParam>,
350     pub where_clause: WhereClause,
351     pub span: Span,
352 }
353
354 impl Default for Generics {
355     /// Creates an instance of `Generics`.
356     fn default() -> Generics {
357         Generics {
358             params: Vec::new(),
359             where_clause: WhereClause { predicates: Vec::new(), span: DUMMY_SP },
360             span: DUMMY_SP,
361         }
362     }
363 }
364
365 /// A where-clause in a definition.
366 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
367 pub struct WhereClause {
368     pub predicates: Vec<WherePredicate>,
369     pub span: Span,
370 }
371
372 /// A single predicate in a where-clause.
373 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
374 pub enum WherePredicate {
375     /// A type binding (e.g., `for<'c> Foo: Send + Clone + 'c`).
376     BoundPredicate(WhereBoundPredicate),
377     /// A lifetime predicate (e.g., `'a: 'b + 'c`).
378     RegionPredicate(WhereRegionPredicate),
379     /// An equality predicate (unsupported).
380     EqPredicate(WhereEqPredicate),
381 }
382
383 impl WherePredicate {
384     pub fn span(&self) -> Span {
385         match self {
386             &WherePredicate::BoundPredicate(ref p) => p.span,
387             &WherePredicate::RegionPredicate(ref p) => p.span,
388             &WherePredicate::EqPredicate(ref p) => p.span,
389         }
390     }
391 }
392
393 /// A type bound.
394 ///
395 /// E.g., `for<'c> Foo: Send + Clone + 'c`.
396 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
397 pub struct WhereBoundPredicate {
398     pub span: Span,
399     /// Any generics from a `for` binding.
400     pub bound_generic_params: Vec<GenericParam>,
401     /// The type being bounded.
402     pub bounded_ty: P<Ty>,
403     /// Trait and lifetime bounds (`Clone + Send + 'static`).
404     pub bounds: GenericBounds,
405 }
406
407 /// A lifetime predicate.
408 ///
409 /// E.g., `'a: 'b + 'c`.
410 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
411 pub struct WhereRegionPredicate {
412     pub span: Span,
413     pub lifetime: Lifetime,
414     pub bounds: GenericBounds,
415 }
416
417 /// An equality predicate (unsupported).
418 ///
419 /// E.g., `T = int`.
420 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
421 pub struct WhereEqPredicate {
422     pub id: NodeId,
423     pub span: Span,
424     pub lhs_ty: P<Ty>,
425     pub rhs_ty: P<Ty>,
426 }
427
428 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
429 pub struct Crate {
430     pub module: Mod,
431     pub attrs: Vec<Attribute>,
432     pub span: Span,
433     /// The order of items in the HIR is unrelated to the order of
434     /// items in the AST. However, we generate proc macro harnesses
435     /// based on the AST order, and later refer to these harnesses
436     /// from the HIR. This field keeps track of the order in which
437     /// we generated proc macros harnesses, so that we can map
438     /// HIR proc macros items back to their harness items.
439     pub proc_macros: Vec<NodeId>,
440 }
441
442 /// Possible values inside of compile-time attribute lists.
443 ///
444 /// E.g., the '..' in `#[name(..)]`.
445 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
446 pub enum NestedMetaItem {
447     /// A full MetaItem, for recursive meta items.
448     MetaItem(MetaItem),
449     /// A literal.
450     ///
451     /// E.g., `"foo"`, `64`, `true`.
452     Literal(Lit),
453 }
454
455 /// A spanned compile-time attribute item.
456 ///
457 /// E.g., `#[test]`, `#[derive(..)]`, `#[rustfmt::skip]` or `#[feature = "foo"]`.
458 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
459 pub struct MetaItem {
460     pub path: Path,
461     pub kind: MetaItemKind,
462     pub span: Span,
463 }
464
465 /// A compile-time attribute item.
466 ///
467 /// E.g., `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`.
468 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
469 pub enum MetaItemKind {
470     /// Word meta item.
471     ///
472     /// E.g., `test` as in `#[test]`.
473     Word,
474     /// List meta item.
475     ///
476     /// E.g., `derive(..)` as in `#[derive(..)]`.
477     List(Vec<NestedMetaItem>),
478     /// Name value meta item.
479     ///
480     /// E.g., `feature = "foo"` as in `#[feature = "foo"]`.
481     NameValue(Lit),
482 }
483
484 /// A block (`{ .. }`).
485 ///
486 /// E.g., `{ .. }` as in `fn foo() { .. }`.
487 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
488 pub struct Block {
489     /// The statements in the block.
490     pub stmts: Vec<Stmt>,
491     pub id: NodeId,
492     /// Distinguishes between `unsafe { ... }` and `{ ... }`.
493     pub rules: BlockCheckMode,
494     pub span: Span,
495 }
496
497 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
498 pub struct Pat {
499     pub id: NodeId,
500     pub kind: PatKind,
501     pub span: Span,
502 }
503
504 impl Pat {
505     /// Attempt reparsing the pattern as a type.
506     /// This is intended for use by diagnostics.
507     pub fn to_ty(&self) -> Option<P<Ty>> {
508         let kind = match &self.kind {
509             // In a type expression `_` is an inference variable.
510             PatKind::Wild => TyKind::Infer,
511             // An IDENT pattern with no binding mode would be valid as path to a type. E.g. `u32`.
512             PatKind::Ident(BindingMode::ByValue(Mutability::Not), ident, None) => {
513                 TyKind::Path(None, Path::from_ident(*ident))
514             }
515             PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
516             PatKind::Mac(mac) => TyKind::Mac(mac.clone()),
517             // `&mut? P` can be reinterpreted as `&mut? T` where `T` is `P` reparsed as a type.
518             PatKind::Ref(pat, mutbl) => {
519                 pat.to_ty().map(|ty| TyKind::Rptr(None, MutTy { ty, mutbl: *mutbl }))?
520             }
521             // A slice/array pattern `[P]` can be reparsed as `[T]`, an unsized array,
522             // when `P` can be reparsed as a type `T`.
523             PatKind::Slice(pats) if pats.len() == 1 => pats[0].to_ty().map(TyKind::Slice)?,
524             // A tuple pattern `(P0, .., Pn)` can be reparsed as `(T0, .., Tn)`
525             // assuming `T0` to `Tn` are all syntactically valid as types.
526             PatKind::Tuple(pats) => {
527                 let mut tys = Vec::with_capacity(pats.len());
528                 // FIXME(#48994) - could just be collected into an Option<Vec>
529                 for pat in pats {
530                     tys.push(pat.to_ty()?);
531                 }
532                 TyKind::Tup(tys)
533             }
534             _ => return None,
535         };
536
537         Some(P(Ty { kind, id: self.id, span: self.span }))
538     }
539
540     /// Walk top-down and call `it` in each place where a pattern occurs
541     /// starting with the root pattern `walk` is called on. If `it` returns
542     /// false then we will descend no further but siblings will be processed.
543     pub fn walk(&self, it: &mut impl FnMut(&Pat) -> bool) {
544         if !it(self) {
545             return;
546         }
547
548         match &self.kind {
549             // Walk into the pattern associated with `Ident` (if any).
550             PatKind::Ident(_, _, Some(p)) => p.walk(it),
551
552             // Walk into each field of struct.
553             PatKind::Struct(_, fields, _) => fields.iter().for_each(|field| field.pat.walk(it)),
554
555             // Sequence of patterns.
556             PatKind::TupleStruct(_, s) | PatKind::Tuple(s) | PatKind::Slice(s) | PatKind::Or(s) => {
557                 s.iter().for_each(|p| p.walk(it))
558             }
559
560             // Trivial wrappers over inner patterns.
561             PatKind::Box(s) | PatKind::Ref(s, _) | PatKind::Paren(s) => s.walk(it),
562
563             // These patterns do not contain subpatterns, skip.
564             PatKind::Wild
565             | PatKind::Rest
566             | PatKind::Lit(_)
567             | PatKind::Range(..)
568             | PatKind::Ident(..)
569             | PatKind::Path(..)
570             | PatKind::Mac(_) => {}
571         }
572     }
573
574     /// Is this a `..` pattern?
575     pub fn is_rest(&self) -> bool {
576         match self.kind {
577             PatKind::Rest => true,
578             _ => false,
579         }
580     }
581 }
582
583 /// A single field in a struct pattern
584 ///
585 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
586 /// are treated the same as` x: x, y: ref y, z: ref mut z`,
587 /// except is_shorthand is true
588 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
589 pub struct FieldPat {
590     /// The identifier for the field
591     pub ident: Ident,
592     /// The pattern the field is destructured to
593     pub pat: P<Pat>,
594     pub is_shorthand: bool,
595     pub attrs: AttrVec,
596     pub id: NodeId,
597     pub span: Span,
598     pub is_placeholder: bool,
599 }
600
601 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
602 pub enum BindingMode {
603     ByRef(Mutability),
604     ByValue(Mutability),
605 }
606
607 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
608 pub enum RangeEnd {
609     Included(RangeSyntax),
610     Excluded,
611 }
612
613 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
614 pub enum RangeSyntax {
615     /// `...`
616     DotDotDot,
617     /// `..=`
618     DotDotEq,
619 }
620
621 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
622 pub enum PatKind {
623     /// Represents a wildcard pattern (`_`).
624     Wild,
625
626     /// A `PatKind::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
627     /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
628     /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
629     /// during name resolution.
630     Ident(BindingMode, Ident, Option<P<Pat>>),
631
632     /// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).
633     /// The `bool` is `true` in the presence of a `..`.
634     Struct(Path, Vec<FieldPat>, /* recovered */ bool),
635
636     /// A tuple struct/variant pattern (`Variant(x, y, .., z)`).
637     TupleStruct(Path, Vec<P<Pat>>),
638
639     /// An or-pattern `A | B | C`.
640     /// Invariant: `pats.len() >= 2`.
641     Or(Vec<P<Pat>>),
642
643     /// A possibly qualified path pattern.
644     /// Unqualified path patterns `A::B::C` can legally refer to variants, structs, constants
645     /// or associated constants. Qualified path patterns `<A>::B::C`/`<A as Trait>::B::C` can
646     /// only legally refer to associated constants.
647     Path(Option<QSelf>, Path),
648
649     /// A tuple pattern (`(a, b)`).
650     Tuple(Vec<P<Pat>>),
651
652     /// A `box` pattern.
653     Box(P<Pat>),
654
655     /// A reference pattern (e.g., `&mut (a, b)`).
656     Ref(P<Pat>, Mutability),
657
658     /// A literal.
659     Lit(P<Expr>),
660
661     /// A range pattern (e.g., `1...2`, `1..=2` or `1..2`).
662     Range(Option<P<Expr>>, Option<P<Expr>>, Spanned<RangeEnd>),
663
664     /// A slice pattern `[a, b, c]`.
665     Slice(Vec<P<Pat>>),
666
667     /// A rest pattern `..`.
668     ///
669     /// Syntactically it is valid anywhere.
670     ///
671     /// Semantically however, it only has meaning immediately inside:
672     /// - a slice pattern: `[a, .., b]`,
673     /// - a binding pattern immediately inside a slice pattern: `[a, r @ ..]`,
674     /// - a tuple pattern: `(a, .., b)`,
675     /// - a tuple struct/variant pattern: `$path(a, .., b)`.
676     ///
677     /// In all of these cases, an additional restriction applies,
678     /// only one rest pattern may occur in the pattern sequences.
679     Rest,
680
681     /// Parentheses in patterns used for grouping (i.e., `(PAT)`).
682     Paren(P<Pat>),
683
684     /// A macro pattern; pre-expansion.
685     Mac(Mac),
686 }
687
688 #[derive(
689     Clone,
690     PartialEq,
691     Eq,
692     PartialOrd,
693     Ord,
694     Hash,
695     RustcEncodable,
696     RustcDecodable,
697     Debug,
698     Copy,
699     HashStable_Generic
700 )]
701 pub enum Mutability {
702     Mut,
703     Not,
704 }
705
706 impl Mutability {
707     /// Returns `MutMutable` only if both `self` and `other` are mutable.
708     pub fn and(self, other: Self) -> Self {
709         match self {
710             Mutability::Mut => other,
711             Mutability::Not => Mutability::Not,
712         }
713     }
714
715     pub fn invert(self) -> Self {
716         match self {
717             Mutability::Mut => Mutability::Not,
718             Mutability::Not => Mutability::Mut,
719         }
720     }
721
722     pub fn prefix_str(&self) -> &'static str {
723         match self {
724             Mutability::Mut => "mut ",
725             Mutability::Not => "",
726         }
727     }
728 }
729
730 /// The kind of borrow in an `AddrOf` expression,
731 /// e.g., `&place` or `&raw const place`.
732 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
733 #[derive(RustcEncodable, RustcDecodable, HashStable_Generic)]
734 pub enum BorrowKind {
735     /// A normal borrow, `&$expr` or `&mut $expr`.
736     /// The resulting type is either `&'a T` or `&'a mut T`
737     /// where `T = typeof($expr)` and `'a` is some lifetime.
738     Ref,
739     /// A raw borrow, `&raw const $expr` or `&raw mut $expr`.
740     /// The resulting type is either `*const T` or `*mut T`
741     /// where `T = typeof($expr)`.
742     Raw,
743 }
744
745 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
746 pub enum BinOpKind {
747     /// The `+` operator (addition)
748     Add,
749     /// The `-` operator (subtraction)
750     Sub,
751     /// The `*` operator (multiplication)
752     Mul,
753     /// The `/` operator (division)
754     Div,
755     /// The `%` operator (modulus)
756     Rem,
757     /// The `&&` operator (logical and)
758     And,
759     /// The `||` operator (logical or)
760     Or,
761     /// The `^` operator (bitwise xor)
762     BitXor,
763     /// The `&` operator (bitwise and)
764     BitAnd,
765     /// The `|` operator (bitwise or)
766     BitOr,
767     /// The `<<` operator (shift left)
768     Shl,
769     /// The `>>` operator (shift right)
770     Shr,
771     /// The `==` operator (equality)
772     Eq,
773     /// The `<` operator (less than)
774     Lt,
775     /// The `<=` operator (less than or equal to)
776     Le,
777     /// The `!=` operator (not equal to)
778     Ne,
779     /// The `>=` operator (greater than or equal to)
780     Ge,
781     /// The `>` operator (greater than)
782     Gt,
783 }
784
785 impl BinOpKind {
786     pub fn to_string(&self) -> &'static str {
787         use BinOpKind::*;
788         match *self {
789             Add => "+",
790             Sub => "-",
791             Mul => "*",
792             Div => "/",
793             Rem => "%",
794             And => "&&",
795             Or => "||",
796             BitXor => "^",
797             BitAnd => "&",
798             BitOr => "|",
799             Shl => "<<",
800             Shr => ">>",
801             Eq => "==",
802             Lt => "<",
803             Le => "<=",
804             Ne => "!=",
805             Ge => ">=",
806             Gt => ">",
807         }
808     }
809     pub fn lazy(&self) -> bool {
810         match *self {
811             BinOpKind::And | BinOpKind::Or => true,
812             _ => false,
813         }
814     }
815
816     pub fn is_shift(&self) -> bool {
817         match *self {
818             BinOpKind::Shl | BinOpKind::Shr => true,
819             _ => false,
820         }
821     }
822
823     pub fn is_comparison(&self) -> bool {
824         use BinOpKind::*;
825         // Note for developers: please keep this as is;
826         // we want compilation to fail if another variant is added.
827         match *self {
828             Eq | Lt | Le | Ne | Gt | Ge => true,
829             And | Or | Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Shl | Shr => false,
830         }
831     }
832
833     /// Returns `true` if the binary operator takes its arguments by value
834     pub fn is_by_value(&self) -> bool {
835         !self.is_comparison()
836     }
837 }
838
839 pub type BinOp = Spanned<BinOpKind>;
840
841 /// Unary operator.
842 ///
843 /// Note that `&data` is not an operator, it's an `AddrOf` expression.
844 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy)]
845 pub enum UnOp {
846     /// The `*` operator for dereferencing
847     Deref,
848     /// The `!` operator for logical inversion
849     Not,
850     /// The `-` operator for negation
851     Neg,
852 }
853
854 impl UnOp {
855     /// Returns `true` if the unary operator takes its argument by value
856     pub fn is_by_value(u: UnOp) -> bool {
857         match u {
858             UnOp::Neg | UnOp::Not => true,
859             _ => false,
860         }
861     }
862
863     pub fn to_string(op: UnOp) -> &'static str {
864         match op {
865             UnOp::Deref => "*",
866             UnOp::Not => "!",
867             UnOp::Neg => "-",
868         }
869     }
870 }
871
872 /// A statement
873 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
874 pub struct Stmt {
875     pub id: NodeId,
876     pub kind: StmtKind,
877     pub span: Span,
878 }
879
880 impl Stmt {
881     pub fn add_trailing_semicolon(mut self) -> Self {
882         self.kind = match self.kind {
883             StmtKind::Expr(expr) => StmtKind::Semi(expr),
884             StmtKind::Mac(mac) => {
885                 StmtKind::Mac(mac.map(|(mac, _style, attrs)| (mac, MacStmtStyle::Semicolon, attrs)))
886             }
887             kind => kind,
888         };
889         self
890     }
891
892     pub fn is_item(&self) -> bool {
893         match self.kind {
894             StmtKind::Item(_) => true,
895             _ => false,
896         }
897     }
898
899     pub fn is_expr(&self) -> bool {
900         match self.kind {
901             StmtKind::Expr(_) => true,
902             _ => false,
903         }
904     }
905 }
906
907 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
908 pub enum StmtKind {
909     /// A local (let) binding.
910     Local(P<Local>),
911     /// An item definition.
912     Item(P<Item>),
913     /// Expr without trailing semi-colon.
914     Expr(P<Expr>),
915     /// Expr with a trailing semi-colon.
916     Semi(P<Expr>),
917     /// Just a trailing semi-colon.
918     Empty,
919     /// Macro.
920     Mac(P<(Mac, MacStmtStyle, AttrVec)>),
921 }
922
923 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
924 pub enum MacStmtStyle {
925     /// The macro statement had a trailing semicolon (e.g., `foo! { ... };`
926     /// `foo!(...);`, `foo![...];`).
927     Semicolon,
928     /// The macro statement had braces (e.g., `foo! { ... }`).
929     Braces,
930     /// The macro statement had parentheses or brackets and no semicolon (e.g.,
931     /// `foo!(...)`). All of these will end up being converted into macro
932     /// expressions.
933     NoBraces,
934 }
935
936 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`.
937 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
938 pub struct Local {
939     pub id: NodeId,
940     pub pat: P<Pat>,
941     pub ty: Option<P<Ty>>,
942     /// Initializer expression to set the value, if any.
943     pub init: Option<P<Expr>>,
944     pub span: Span,
945     pub attrs: AttrVec,
946 }
947
948 /// An arm of a 'match'.
949 ///
950 /// E.g., `0..=10 => { println!("match!") }` as in
951 ///
952 /// ```
953 /// match 123 {
954 ///     0..=10 => { println!("match!") },
955 ///     _ => { println!("no match!") },
956 /// }
957 /// ```
958 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
959 pub struct Arm {
960     pub attrs: Vec<Attribute>,
961     /// Match arm pattern, e.g. `10` in `match foo { 10 => {}, _ => {} }`
962     pub pat: P<Pat>,
963     /// Match arm guard, e.g. `n > 10` in `match foo { n if n > 10 => {}, _ => {} }`
964     pub guard: Option<P<Expr>>,
965     /// Match arm body.
966     pub body: P<Expr>,
967     pub span: Span,
968     pub id: NodeId,
969     pub is_placeholder: bool,
970 }
971
972 /// Access of a named (e.g., `obj.foo`) or unnamed (e.g., `obj.0`) struct field.
973 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
974 pub struct Field {
975     pub attrs: AttrVec,
976     pub id: NodeId,
977     pub span: Span,
978     pub ident: Ident,
979     pub expr: P<Expr>,
980     pub is_shorthand: bool,
981     pub is_placeholder: bool,
982 }
983
984 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
985 pub enum BlockCheckMode {
986     Default,
987     Unsafe(UnsafeSource),
988 }
989
990 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
991 pub enum UnsafeSource {
992     CompilerGenerated,
993     UserProvided,
994 }
995
996 /// A constant (expression) that's not an item or associated item,
997 /// but needs its own `DefId` for type-checking, const-eval, etc.
998 /// These are usually found nested inside types (e.g., array lengths)
999 /// or expressions (e.g., repeat counts), and also used to define
1000 /// explicit discriminant values for enum variants.
1001 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1002 pub struct AnonConst {
1003     pub id: NodeId,
1004     pub value: P<Expr>,
1005 }
1006
1007 /// An expression.
1008 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1009 pub struct Expr {
1010     pub id: NodeId,
1011     pub kind: ExprKind,
1012     pub span: Span,
1013     pub attrs: AttrVec,
1014 }
1015
1016 // `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
1017 #[cfg(target_arch = "x86_64")]
1018 rustc_data_structures::static_assert_size!(Expr, 96);
1019
1020 impl Expr {
1021     /// Returns `true` if this expression would be valid somewhere that expects a value;
1022     /// for example, an `if` condition.
1023     pub fn returns(&self) -> bool {
1024         if let ExprKind::Block(ref block, _) = self.kind {
1025             match block.stmts.last().map(|last_stmt| &last_stmt.kind) {
1026                 // Implicit return
1027                 Some(&StmtKind::Expr(_)) => true,
1028                 Some(&StmtKind::Semi(ref expr)) => {
1029                     if let ExprKind::Ret(_) = expr.kind {
1030                         // Last statement is explicit return.
1031                         true
1032                     } else {
1033                         false
1034                     }
1035                 }
1036                 // This is a block that doesn't end in either an implicit or explicit return.
1037                 _ => false,
1038             }
1039         } else {
1040             // This is not a block, it is a value.
1041             true
1042         }
1043     }
1044
1045     pub fn to_bound(&self) -> Option<GenericBound> {
1046         match &self.kind {
1047             ExprKind::Path(None, path) => Some(GenericBound::Trait(
1048                 PolyTraitRef::new(Vec::new(), path.clone(), self.span),
1049                 TraitBoundModifier::None,
1050             )),
1051             _ => None,
1052         }
1053     }
1054
1055     /// Attempts to reparse as `Ty` (for diagnostic purposes).
1056     pub fn to_ty(&self) -> Option<P<Ty>> {
1057         let kind = match &self.kind {
1058             // Trivial conversions.
1059             ExprKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
1060             ExprKind::Mac(mac) => TyKind::Mac(mac.clone()),
1061
1062             ExprKind::Paren(expr) => expr.to_ty().map(TyKind::Paren)?,
1063
1064             ExprKind::AddrOf(BorrowKind::Ref, mutbl, expr) => {
1065                 expr.to_ty().map(|ty| TyKind::Rptr(None, MutTy { ty, mutbl: *mutbl }))?
1066             }
1067
1068             ExprKind::Repeat(expr, expr_len) => {
1069                 expr.to_ty().map(|ty| TyKind::Array(ty, expr_len.clone()))?
1070             }
1071
1072             ExprKind::Array(exprs) if exprs.len() == 1 => exprs[0].to_ty().map(TyKind::Slice)?,
1073
1074             ExprKind::Tup(exprs) => {
1075                 let tys = exprs.iter().map(|expr| expr.to_ty()).collect::<Option<Vec<_>>>()?;
1076                 TyKind::Tup(tys)
1077             }
1078
1079             // If binary operator is `Add` and both `lhs` and `rhs` are trait bounds,
1080             // then type of result is trait object.
1081             // Otherwise we don't assume the result type.
1082             ExprKind::Binary(binop, lhs, rhs) if binop.node == BinOpKind::Add => {
1083                 if let (Some(lhs), Some(rhs)) = (lhs.to_bound(), rhs.to_bound()) {
1084                     TyKind::TraitObject(vec![lhs, rhs], TraitObjectSyntax::None)
1085                 } else {
1086                     return None;
1087                 }
1088             }
1089
1090             // This expression doesn't look like a type syntactically.
1091             _ => return None,
1092         };
1093
1094         Some(P(Ty { kind, id: self.id, span: self.span }))
1095     }
1096
1097     pub fn precedence(&self) -> ExprPrecedence {
1098         match self.kind {
1099             ExprKind::Box(_) => ExprPrecedence::Box,
1100             ExprKind::Array(_) => ExprPrecedence::Array,
1101             ExprKind::Call(..) => ExprPrecedence::Call,
1102             ExprKind::MethodCall(..) => ExprPrecedence::MethodCall,
1103             ExprKind::Tup(_) => ExprPrecedence::Tup,
1104             ExprKind::Binary(op, ..) => ExprPrecedence::Binary(op.node),
1105             ExprKind::Unary(..) => ExprPrecedence::Unary,
1106             ExprKind::Lit(_) => ExprPrecedence::Lit,
1107             ExprKind::Type(..) | ExprKind::Cast(..) => ExprPrecedence::Cast,
1108             ExprKind::Let(..) => ExprPrecedence::Let,
1109             ExprKind::If(..) => ExprPrecedence::If,
1110             ExprKind::While(..) => ExprPrecedence::While,
1111             ExprKind::ForLoop(..) => ExprPrecedence::ForLoop,
1112             ExprKind::Loop(..) => ExprPrecedence::Loop,
1113             ExprKind::Match(..) => ExprPrecedence::Match,
1114             ExprKind::Closure(..) => ExprPrecedence::Closure,
1115             ExprKind::Block(..) => ExprPrecedence::Block,
1116             ExprKind::TryBlock(..) => ExprPrecedence::TryBlock,
1117             ExprKind::Async(..) => ExprPrecedence::Async,
1118             ExprKind::Await(..) => ExprPrecedence::Await,
1119             ExprKind::Assign(..) => ExprPrecedence::Assign,
1120             ExprKind::AssignOp(..) => ExprPrecedence::AssignOp,
1121             ExprKind::Field(..) => ExprPrecedence::Field,
1122             ExprKind::Index(..) => ExprPrecedence::Index,
1123             ExprKind::Range(..) => ExprPrecedence::Range,
1124             ExprKind::Path(..) => ExprPrecedence::Path,
1125             ExprKind::AddrOf(..) => ExprPrecedence::AddrOf,
1126             ExprKind::Break(..) => ExprPrecedence::Break,
1127             ExprKind::Continue(..) => ExprPrecedence::Continue,
1128             ExprKind::Ret(..) => ExprPrecedence::Ret,
1129             ExprKind::InlineAsm(..) => ExprPrecedence::InlineAsm,
1130             ExprKind::Mac(..) => ExprPrecedence::Mac,
1131             ExprKind::Struct(..) => ExprPrecedence::Struct,
1132             ExprKind::Repeat(..) => ExprPrecedence::Repeat,
1133             ExprKind::Paren(..) => ExprPrecedence::Paren,
1134             ExprKind::Try(..) => ExprPrecedence::Try,
1135             ExprKind::Yield(..) => ExprPrecedence::Yield,
1136             ExprKind::Err => ExprPrecedence::Err,
1137         }
1138     }
1139 }
1140
1141 /// Limit types of a range (inclusive or exclusive)
1142 #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
1143 pub enum RangeLimits {
1144     /// Inclusive at the beginning, exclusive at the end
1145     HalfOpen,
1146     /// Inclusive at the beginning and end
1147     Closed,
1148 }
1149
1150 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1151 pub enum ExprKind {
1152     /// A `box x` expression.
1153     Box(P<Expr>),
1154     /// An array (`[a, b, c, d]`)
1155     Array(Vec<P<Expr>>),
1156     /// A function call
1157     ///
1158     /// The first field resolves to the function itself,
1159     /// and the second field is the list of arguments.
1160     /// This also represents calling the constructor of
1161     /// tuple-like ADTs such as tuple structs and enum variants.
1162     Call(P<Expr>, Vec<P<Expr>>),
1163     /// A method call (`x.foo::<'static, Bar, Baz>(a, b, c, d)`)
1164     ///
1165     /// The `PathSegment` represents the method name and its generic arguments
1166     /// (within the angle brackets).
1167     /// The first element of the vector of an `Expr` is the expression that evaluates
1168     /// to the object on which the method is being called on (the receiver),
1169     /// and the remaining elements are the rest of the arguments.
1170     /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
1171     /// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, [x, a, b, c, d])`.
1172     MethodCall(PathSegment, Vec<P<Expr>>),
1173     /// A tuple (e.g., `(a, b, c, d)`).
1174     Tup(Vec<P<Expr>>),
1175     /// A binary operation (e.g., `a + b`, `a * b`).
1176     Binary(BinOp, P<Expr>, P<Expr>),
1177     /// A unary operation (e.g., `!x`, `*x`).
1178     Unary(UnOp, P<Expr>),
1179     /// A literal (e.g., `1`, `"foo"`).
1180     Lit(Lit),
1181     /// A cast (e.g., `foo as f64`).
1182     Cast(P<Expr>, P<Ty>),
1183     /// A type ascription (e.g., `42: usize`).
1184     Type(P<Expr>, P<Ty>),
1185     /// A `let pat = expr` expression that is only semantically allowed in the condition
1186     /// of `if` / `while` expressions. (e.g., `if let 0 = x { .. }`).
1187     Let(P<Pat>, P<Expr>),
1188     /// An `if` block, with an optional `else` block.
1189     ///
1190     /// `if expr { block } else { expr }`
1191     If(P<Expr>, P<Block>, Option<P<Expr>>),
1192     /// A while loop, with an optional label.
1193     ///
1194     /// `'label: while expr { block }`
1195     While(P<Expr>, P<Block>, Option<Label>),
1196     /// A `for` loop, with an optional label.
1197     ///
1198     /// `'label: for pat in expr { block }`
1199     ///
1200     /// This is desugared to a combination of `loop` and `match` expressions.
1201     ForLoop(P<Pat>, P<Expr>, P<Block>, Option<Label>),
1202     /// Conditionless loop (can be exited with `break`, `continue`, or `return`).
1203     ///
1204     /// `'label: loop { block }`
1205     Loop(P<Block>, Option<Label>),
1206     /// A `match` block.
1207     Match(P<Expr>, Vec<Arm>),
1208     /// A closure (e.g., `move |a, b, c| a + b + c`).
1209     ///
1210     /// The final span is the span of the argument block `|...|`.
1211     Closure(CaptureBy, Async, Movability, P<FnDecl>, P<Expr>, Span),
1212     /// A block (`'label: { ... }`).
1213     Block(P<Block>, Option<Label>),
1214     /// An async block (`async move { ... }`).
1215     ///
1216     /// The `NodeId` is the `NodeId` for the closure that results from
1217     /// desugaring an async block, just like the NodeId field in the
1218     /// `Async::Yes` variant. This is necessary in order to create a def for the
1219     /// closure which can be used as a parent of any child defs. Defs
1220     /// created during lowering cannot be made the parent of any other
1221     /// preexisting defs.
1222     Async(CaptureBy, NodeId, P<Block>),
1223     /// An await expression (`my_future.await`).
1224     Await(P<Expr>),
1225
1226     /// A try block (`try { ... }`).
1227     TryBlock(P<Block>),
1228
1229     /// An assignment (`a = foo()`).
1230     /// The `Span` argument is the span of the `=` token.
1231     Assign(P<Expr>, P<Expr>, Span),
1232     /// An assignment with an operator.
1233     ///
1234     /// E.g., `a += 1`.
1235     AssignOp(BinOp, P<Expr>, P<Expr>),
1236     /// Access of a named (e.g., `obj.foo`) or unnamed (e.g., `obj.0`) struct field.
1237     Field(P<Expr>, Ident),
1238     /// An indexing operation (e.g., `foo[2]`).
1239     Index(P<Expr>, P<Expr>),
1240     /// A range (e.g., `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
1241     Range(Option<P<Expr>>, Option<P<Expr>>, RangeLimits),
1242
1243     /// Variable reference, possibly containing `::` and/or type
1244     /// parameters (e.g., `foo::bar::<baz>`).
1245     ///
1246     /// Optionally "qualified" (e.g., `<Vec<T> as SomeTrait>::SomeType`).
1247     Path(Option<QSelf>, Path),
1248
1249     /// A referencing operation (`&a`, `&mut a`, `&raw const a` or `&raw mut a`).
1250     AddrOf(BorrowKind, Mutability, P<Expr>),
1251     /// A `break`, with an optional label to break, and an optional expression.
1252     Break(Option<Label>, Option<P<Expr>>),
1253     /// A `continue`, with an optional label.
1254     Continue(Option<Label>),
1255     /// A `return`, with an optional value to be returned.
1256     Ret(Option<P<Expr>>),
1257
1258     /// Output of the `asm!()` macro.
1259     InlineAsm(P<InlineAsm>),
1260
1261     /// A macro invocation; pre-expansion.
1262     Mac(Mac),
1263
1264     /// A struct literal expression.
1265     ///
1266     /// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. base}`,
1267     /// where `base` is the `Option<Expr>`.
1268     Struct(Path, Vec<Field>, Option<P<Expr>>),
1269
1270     /// An array literal constructed from one repeated element.
1271     ///
1272     /// E.g., `[1; 5]`. The expression is the element to be
1273     /// repeated; the constant is the number of times to repeat it.
1274     Repeat(P<Expr>, AnonConst),
1275
1276     /// No-op: used solely so we can pretty-print faithfully.
1277     Paren(P<Expr>),
1278
1279     /// A try expression (`expr?`).
1280     Try(P<Expr>),
1281
1282     /// A `yield`, with an optional value to be yielded.
1283     Yield(Option<P<Expr>>),
1284
1285     /// Placeholder for an expression that wasn't syntactically well formed in some way.
1286     Err,
1287 }
1288
1289 /// The explicit `Self` type in a "qualified path". The actual
1290 /// path, including the trait and the associated item, is stored
1291 /// separately. `position` represents the index of the associated
1292 /// item qualified with this `Self` type.
1293 ///
1294 /// ```ignore (only-for-syntax-highlight)
1295 /// <Vec<T> as a::b::Trait>::AssociatedItem
1296 ///  ^~~~~     ~~~~~~~~~~~~~~^
1297 ///  ty        position = 3
1298 ///
1299 /// <Vec<T>>::AssociatedItem
1300 ///  ^~~~~    ^
1301 ///  ty       position = 0
1302 /// ```
1303 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1304 pub struct QSelf {
1305     pub ty: P<Ty>,
1306
1307     /// The span of `a::b::Trait` in a path like `<Vec<T> as
1308     /// a::b::Trait>::AssociatedItem`; in the case where `position ==
1309     /// 0`, this is an empty span.
1310     pub path_span: Span,
1311     pub position: usize,
1312 }
1313
1314 /// A capture clause used in closures and `async` blocks.
1315 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
1316 pub enum CaptureBy {
1317     /// `move |x| y + x`.
1318     Value,
1319     /// `move` keyword was not specified.
1320     Ref,
1321 }
1322
1323 /// The movability of a generator / closure literal:
1324 /// whether a generator contains self-references, causing it to be `!Unpin`.
1325 #[derive(
1326     Clone,
1327     PartialEq,
1328     Eq,
1329     PartialOrd,
1330     Ord,
1331     Hash,
1332     RustcEncodable,
1333     RustcDecodable,
1334     Debug,
1335     Copy,
1336     HashStable_Generic
1337 )]
1338 pub enum Movability {
1339     /// May contain self-references, `!Unpin`.
1340     Static,
1341     /// Must not contain self-references, `Unpin`.
1342     Movable,
1343 }
1344
1345 /// Represents a macro invocation. The `path` indicates which macro
1346 /// is being invoked, and the `args` are arguments passed to it.
1347 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1348 pub struct Mac {
1349     pub path: Path,
1350     pub args: P<MacArgs>,
1351     pub prior_type_ascription: Option<(Span, bool)>,
1352 }
1353
1354 impl Mac {
1355     pub fn span(&self) -> Span {
1356         self.path.span.to(self.args.span().unwrap_or(self.path.span))
1357     }
1358 }
1359
1360 /// Arguments passed to an attribute or a function-like macro.
1361 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
1362 pub enum MacArgs {
1363     /// No arguments - `#[attr]`.
1364     Empty,
1365     /// Delimited arguments - `#[attr()/[]/{}]` or `mac!()/[]/{}`.
1366     Delimited(DelimSpan, MacDelimiter, TokenStream),
1367     /// Arguments of a key-value attribute - `#[attr = "value"]`.
1368     Eq(
1369         /// Span of the `=` token.
1370         Span,
1371         /// Token stream of the "value".
1372         TokenStream,
1373     ),
1374 }
1375
1376 impl MacArgs {
1377     pub fn delim(&self) -> DelimToken {
1378         match self {
1379             MacArgs::Delimited(_, delim, _) => delim.to_token(),
1380             MacArgs::Empty | MacArgs::Eq(..) => token::NoDelim,
1381         }
1382     }
1383
1384     pub fn span(&self) -> Option<Span> {
1385         match *self {
1386             MacArgs::Empty => None,
1387             MacArgs::Delimited(dspan, ..) => Some(dspan.entire()),
1388             MacArgs::Eq(eq_span, ref tokens) => Some(eq_span.to(tokens.span().unwrap_or(eq_span))),
1389         }
1390     }
1391
1392     /// Tokens inside the delimiters or after `=`.
1393     /// Proc macros see these tokens, for example.
1394     pub fn inner_tokens(&self) -> TokenStream {
1395         match self {
1396             MacArgs::Empty => TokenStream::default(),
1397             MacArgs::Delimited(.., tokens) | MacArgs::Eq(.., tokens) => tokens.clone(),
1398         }
1399     }
1400
1401     /// Tokens together with the delimiters or `=`.
1402     /// Use of this method generally means that something suboptimal or hacky is happening.
1403     pub fn outer_tokens(&self) -> TokenStream {
1404         match *self {
1405             MacArgs::Empty => TokenStream::default(),
1406             MacArgs::Delimited(dspan, delim, ref tokens) => {
1407                 TokenTree::Delimited(dspan, delim.to_token(), tokens.clone()).into()
1408             }
1409             MacArgs::Eq(eq_span, ref tokens) => {
1410                 iter::once(TokenTree::token(token::Eq, eq_span)).chain(tokens.trees()).collect()
1411             }
1412         }
1413     }
1414
1415     /// Whether a macro with these arguments needs a semicolon
1416     /// when used as a standalone item or statement.
1417     pub fn need_semicolon(&self) -> bool {
1418         !matches!(self, MacArgs::Delimited(_, MacDelimiter::Brace, _))
1419     }
1420 }
1421
1422 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
1423 pub enum MacDelimiter {
1424     Parenthesis,
1425     Bracket,
1426     Brace,
1427 }
1428
1429 impl MacDelimiter {
1430     pub fn to_token(self) -> DelimToken {
1431         match self {
1432             MacDelimiter::Parenthesis => DelimToken::Paren,
1433             MacDelimiter::Bracket => DelimToken::Bracket,
1434             MacDelimiter::Brace => DelimToken::Brace,
1435         }
1436     }
1437
1438     pub fn from_token(delim: DelimToken) -> Option<MacDelimiter> {
1439         match delim {
1440             token::Paren => Some(MacDelimiter::Parenthesis),
1441             token::Bracket => Some(MacDelimiter::Bracket),
1442             token::Brace => Some(MacDelimiter::Brace),
1443             token::NoDelim => None,
1444         }
1445     }
1446 }
1447
1448 /// Represents a macro definition.
1449 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1450 pub struct MacroDef {
1451     pub body: P<MacArgs>,
1452     /// `true` if macro was defined with `macro_rules`.
1453     pub legacy: bool,
1454 }
1455
1456 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy, Hash, Eq, PartialEq)]
1457 #[derive(HashStable_Generic)]
1458 pub enum StrStyle {
1459     /// A regular string, like `"foo"`.
1460     Cooked,
1461     /// A raw string, like `r##"foo"##`.
1462     ///
1463     /// The value is the number of `#` symbols used.
1464     Raw(u16),
1465 }
1466
1467 /// An AST literal.
1468 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
1469 pub struct Lit {
1470     /// The original literal token as written in source code.
1471     pub token: token::Lit,
1472     /// The "semantic" representation of the literal lowered from the original tokens.
1473     /// Strings are unescaped, hexadecimal forms are eliminated, etc.
1474     /// FIXME: Remove this and only create the semantic representation during lowering to HIR.
1475     pub kind: LitKind,
1476     pub span: Span,
1477 }
1478
1479 /// Same as `Lit`, but restricted to string literals.
1480 #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)]
1481 pub struct StrLit {
1482     /// The original literal token as written in source code.
1483     pub style: StrStyle,
1484     pub symbol: Symbol,
1485     pub suffix: Option<Symbol>,
1486     pub span: Span,
1487     /// The unescaped "semantic" representation of the literal lowered from the original token.
1488     /// FIXME: Remove this and only create the semantic representation during lowering to HIR.
1489     pub symbol_unescaped: Symbol,
1490 }
1491
1492 impl StrLit {
1493     pub fn as_lit(&self) -> Lit {
1494         let token_kind = match self.style {
1495             StrStyle::Cooked => token::Str,
1496             StrStyle::Raw(n) => token::StrRaw(n),
1497         };
1498         Lit {
1499             token: token::Lit::new(token_kind, self.symbol, self.suffix),
1500             span: self.span,
1501             kind: LitKind::Str(self.symbol_unescaped, self.style),
1502         }
1503     }
1504 }
1505
1506 /// Type of the integer literal based on provided suffix.
1507 #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug, Hash, Eq, PartialEq)]
1508 #[derive(HashStable_Generic)]
1509 pub enum LitIntType {
1510     /// e.g. `42_i32`.
1511     Signed(IntTy),
1512     /// e.g. `42_u32`.
1513     Unsigned(UintTy),
1514     /// e.g. `42`.
1515     Unsuffixed,
1516 }
1517
1518 /// Type of the float literal based on provided suffix.
1519 #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug, Hash, Eq, PartialEq)]
1520 #[derive(HashStable_Generic)]
1521 pub enum LitFloatType {
1522     /// A float literal with a suffix (`1f32` or `1E10f32`).
1523     Suffixed(FloatTy),
1524     /// A float literal without a suffix (`1.0 or 1.0E10`).
1525     Unsuffixed,
1526 }
1527
1528 /// Literal kind.
1529 ///
1530 /// E.g., `"foo"`, `42`, `12.34`, or `bool`.
1531 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash, Eq, PartialEq, HashStable_Generic)]
1532 pub enum LitKind {
1533     /// A string literal (`"foo"`).
1534     Str(Symbol, StrStyle),
1535     /// A byte string (`b"foo"`).
1536     ByteStr(Lrc<Vec<u8>>),
1537     /// A byte char (`b'f'`).
1538     Byte(u8),
1539     /// A character literal (`'a'`).
1540     Char(char),
1541     /// An integer literal (`1`).
1542     Int(u128, LitIntType),
1543     /// A float literal (`1f64` or `1E10f64`).
1544     Float(Symbol, LitFloatType),
1545     /// A boolean literal.
1546     Bool(bool),
1547     /// Placeholder for a literal that wasn't well-formed in some way.
1548     Err(Symbol),
1549 }
1550
1551 impl LitKind {
1552     /// Returns `true` if this literal is a string.
1553     pub fn is_str(&self) -> bool {
1554         match *self {
1555             LitKind::Str(..) => true,
1556             _ => false,
1557         }
1558     }
1559
1560     /// Returns `true` if this literal is byte literal string.
1561     pub fn is_bytestr(&self) -> bool {
1562         match self {
1563             LitKind::ByteStr(_) => true,
1564             _ => false,
1565         }
1566     }
1567
1568     /// Returns `true` if this is a numeric literal.
1569     pub fn is_numeric(&self) -> bool {
1570         match *self {
1571             LitKind::Int(..) | LitKind::Float(..) => true,
1572             _ => false,
1573         }
1574     }
1575
1576     /// Returns `true` if this literal has no suffix.
1577     /// Note: this will return true for literals with prefixes such as raw strings and byte strings.
1578     pub fn is_unsuffixed(&self) -> bool {
1579         !self.is_suffixed()
1580     }
1581
1582     /// Returns `true` if this literal has a suffix.
1583     pub fn is_suffixed(&self) -> bool {
1584         match *self {
1585             // suffixed variants
1586             LitKind::Int(_, LitIntType::Signed(..))
1587             | LitKind::Int(_, LitIntType::Unsigned(..))
1588             | LitKind::Float(_, LitFloatType::Suffixed(..)) => true,
1589             // unsuffixed variants
1590             LitKind::Str(..)
1591             | LitKind::ByteStr(..)
1592             | LitKind::Byte(..)
1593             | LitKind::Char(..)
1594             | LitKind::Int(_, LitIntType::Unsuffixed)
1595             | LitKind::Float(_, LitFloatType::Unsuffixed)
1596             | LitKind::Bool(..)
1597             | LitKind::Err(..) => false,
1598         }
1599     }
1600 }
1601
1602 // N.B., If you change this, you'll probably want to change the corresponding
1603 // type structure in `middle/ty.rs` as well.
1604 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1605 pub struct MutTy {
1606     pub ty: P<Ty>,
1607     pub mutbl: Mutability,
1608 }
1609
1610 /// Represents a function's signature in a trait declaration,
1611 /// trait implementation, or free function.
1612 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1613 pub struct FnSig {
1614     pub header: FnHeader,
1615     pub decl: P<FnDecl>,
1616 }
1617
1618 #[derive(
1619     Clone,
1620     Copy,
1621     PartialEq,
1622     Eq,
1623     PartialOrd,
1624     Ord,
1625     Hash,
1626     HashStable_Generic,
1627     RustcEncodable,
1628     RustcDecodable,
1629     Debug
1630 )]
1631 pub enum FloatTy {
1632     F32,
1633     F64,
1634 }
1635
1636 impl FloatTy {
1637     pub fn name_str(self) -> &'static str {
1638         match self {
1639             FloatTy::F32 => "f32",
1640             FloatTy::F64 => "f64",
1641         }
1642     }
1643
1644     pub fn name(self) -> Symbol {
1645         match self {
1646             FloatTy::F32 => sym::f32,
1647             FloatTy::F64 => sym::f64,
1648         }
1649     }
1650
1651     pub fn bit_width(self) -> usize {
1652         match self {
1653             FloatTy::F32 => 32,
1654             FloatTy::F64 => 64,
1655         }
1656     }
1657 }
1658
1659 #[derive(
1660     Clone,
1661     Copy,
1662     PartialEq,
1663     Eq,
1664     PartialOrd,
1665     Ord,
1666     Hash,
1667     HashStable_Generic,
1668     RustcEncodable,
1669     RustcDecodable,
1670     Debug
1671 )]
1672 pub enum IntTy {
1673     Isize,
1674     I8,
1675     I16,
1676     I32,
1677     I64,
1678     I128,
1679 }
1680
1681 impl IntTy {
1682     pub fn name_str(&self) -> &'static str {
1683         match *self {
1684             IntTy::Isize => "isize",
1685             IntTy::I8 => "i8",
1686             IntTy::I16 => "i16",
1687             IntTy::I32 => "i32",
1688             IntTy::I64 => "i64",
1689             IntTy::I128 => "i128",
1690         }
1691     }
1692
1693     pub fn name(&self) -> Symbol {
1694         match *self {
1695             IntTy::Isize => sym::isize,
1696             IntTy::I8 => sym::i8,
1697             IntTy::I16 => sym::i16,
1698             IntTy::I32 => sym::i32,
1699             IntTy::I64 => sym::i64,
1700             IntTy::I128 => sym::i128,
1701         }
1702     }
1703
1704     pub fn val_to_string(&self, val: i128) -> String {
1705         // Cast to a `u128` so we can correctly print `INT128_MIN`. All integral types
1706         // are parsed as `u128`, so we wouldn't want to print an extra negative
1707         // sign.
1708         format!("{}{}", val as u128, self.name_str())
1709     }
1710
1711     pub fn bit_width(&self) -> Option<usize> {
1712         Some(match *self {
1713             IntTy::Isize => return None,
1714             IntTy::I8 => 8,
1715             IntTy::I16 => 16,
1716             IntTy::I32 => 32,
1717             IntTy::I64 => 64,
1718             IntTy::I128 => 128,
1719         })
1720     }
1721
1722     pub fn normalize(&self, target_width: u32) -> Self {
1723         match self {
1724             IntTy::Isize => match target_width {
1725                 16 => IntTy::I16,
1726                 32 => IntTy::I32,
1727                 64 => IntTy::I64,
1728                 _ => unreachable!(),
1729             },
1730             _ => *self,
1731         }
1732     }
1733 }
1734
1735 #[derive(
1736     Clone,
1737     PartialEq,
1738     Eq,
1739     PartialOrd,
1740     Ord,
1741     Hash,
1742     HashStable_Generic,
1743     RustcEncodable,
1744     RustcDecodable,
1745     Copy,
1746     Debug
1747 )]
1748 pub enum UintTy {
1749     Usize,
1750     U8,
1751     U16,
1752     U32,
1753     U64,
1754     U128,
1755 }
1756
1757 impl UintTy {
1758     pub fn name_str(&self) -> &'static str {
1759         match *self {
1760             UintTy::Usize => "usize",
1761             UintTy::U8 => "u8",
1762             UintTy::U16 => "u16",
1763             UintTy::U32 => "u32",
1764             UintTy::U64 => "u64",
1765             UintTy::U128 => "u128",
1766         }
1767     }
1768
1769     pub fn name(&self) -> Symbol {
1770         match *self {
1771             UintTy::Usize => sym::usize,
1772             UintTy::U8 => sym::u8,
1773             UintTy::U16 => sym::u16,
1774             UintTy::U32 => sym::u32,
1775             UintTy::U64 => sym::u64,
1776             UintTy::U128 => sym::u128,
1777         }
1778     }
1779
1780     pub fn val_to_string(&self, val: u128) -> String {
1781         format!("{}{}", val, self.name_str())
1782     }
1783
1784     pub fn bit_width(&self) -> Option<usize> {
1785         Some(match *self {
1786             UintTy::Usize => return None,
1787             UintTy::U8 => 8,
1788             UintTy::U16 => 16,
1789             UintTy::U32 => 32,
1790             UintTy::U64 => 64,
1791             UintTy::U128 => 128,
1792         })
1793     }
1794
1795     pub fn normalize(&self, target_width: u32) -> Self {
1796         match self {
1797             UintTy::Usize => match target_width {
1798                 16 => UintTy::U16,
1799                 32 => UintTy::U32,
1800                 64 => UintTy::U64,
1801                 _ => unreachable!(),
1802             },
1803             _ => *self,
1804         }
1805     }
1806 }
1807
1808 /// A constraint on an associated type (e.g., `A = Bar` in `Foo<A = Bar>` or
1809 /// `A: TraitA + TraitB` in `Foo<A: TraitA + TraitB>`).
1810 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1811 pub struct AssocTyConstraint {
1812     pub id: NodeId,
1813     pub ident: Ident,
1814     pub kind: AssocTyConstraintKind,
1815     pub span: Span,
1816 }
1817
1818 /// The kinds of an `AssocTyConstraint`.
1819 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1820 pub enum AssocTyConstraintKind {
1821     /// E.g., `A = Bar` in `Foo<A = Bar>`.
1822     Equality { ty: P<Ty> },
1823     /// E.g. `A: TraitA + TraitB` in `Foo<A: TraitA + TraitB>`.
1824     Bound { bounds: GenericBounds },
1825 }
1826
1827 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1828 pub struct Ty {
1829     pub id: NodeId,
1830     pub kind: TyKind,
1831     pub span: Span,
1832 }
1833
1834 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1835 pub struct BareFnTy {
1836     pub unsafety: Unsafe,
1837     pub ext: Extern,
1838     pub generic_params: Vec<GenericParam>,
1839     pub decl: P<FnDecl>,
1840 }
1841
1842 /// The various kinds of type recognized by the compiler.
1843 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1844 pub enum TyKind {
1845     /// A variable-length slice (`[T]`).
1846     Slice(P<Ty>),
1847     /// A fixed length array (`[T; n]`).
1848     Array(P<Ty>, AnonConst),
1849     /// A raw pointer (`*const T` or `*mut T`).
1850     Ptr(MutTy),
1851     /// A reference (`&'a T` or `&'a mut T`).
1852     Rptr(Option<Lifetime>, MutTy),
1853     /// A bare function (e.g., `fn(usize) -> bool`).
1854     BareFn(P<BareFnTy>),
1855     /// The never type (`!`).
1856     Never,
1857     /// A tuple (`(A, B, C, D,...)`).
1858     Tup(Vec<P<Ty>>),
1859     /// A path (`module::module::...::Type`), optionally
1860     /// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
1861     ///
1862     /// Type parameters are stored in the `Path` itself.
1863     Path(Option<QSelf>, Path),
1864     /// A trait object type `Bound1 + Bound2 + Bound3`
1865     /// where `Bound` is a trait or a lifetime.
1866     TraitObject(GenericBounds, TraitObjectSyntax),
1867     /// An `impl Bound1 + Bound2 + Bound3` type
1868     /// where `Bound` is a trait or a lifetime.
1869     ///
1870     /// The `NodeId` exists to prevent lowering from having to
1871     /// generate `NodeId`s on the fly, which would complicate
1872     /// the generation of opaque `type Foo = impl Trait` items significantly.
1873     ImplTrait(NodeId, GenericBounds),
1874     /// No-op; kept solely so that we can pretty-print faithfully.
1875     Paren(P<Ty>),
1876     /// Unused for now.
1877     Typeof(AnonConst),
1878     /// This means the type should be inferred instead of it having been
1879     /// specified. This can appear anywhere in a type.
1880     Infer,
1881     /// Inferred type of a `self` or `&self` argument in a method.
1882     ImplicitSelf,
1883     /// A macro in the type position.
1884     Mac(Mac),
1885     /// Placeholder for a kind that has failed to be defined.
1886     Err,
1887     /// Placeholder for a `va_list`.
1888     CVarArgs,
1889 }
1890
1891 impl TyKind {
1892     pub fn is_implicit_self(&self) -> bool {
1893         if let TyKind::ImplicitSelf = *self { true } else { false }
1894     }
1895
1896     pub fn is_unit(&self) -> bool {
1897         if let TyKind::Tup(ref tys) = *self { tys.is_empty() } else { false }
1898     }
1899
1900     /// HACK(type_alias_impl_trait, Centril): A temporary crutch used
1901     /// in lowering to avoid making larger changes there and beyond.
1902     pub fn opaque_top_hack(&self) -> Option<&GenericBounds> {
1903         match self {
1904             Self::ImplTrait(_, bounds) => Some(bounds),
1905             _ => None,
1906         }
1907     }
1908 }
1909
1910 /// Syntax used to declare a trait object.
1911 #[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
1912 pub enum TraitObjectSyntax {
1913     Dyn,
1914     None,
1915 }
1916
1917 /// Inline assembly dialect.
1918 ///
1919 /// E.g., `"intel"` as in `asm!("mov eax, 2" : "={eax}"(result) : : : "intel")`.
1920 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy, HashStable_Generic)]
1921 pub enum AsmDialect {
1922     Att,
1923     Intel,
1924 }
1925
1926 /// Inline assembly.
1927 ///
1928 /// E.g., `"={eax}"(result)` as in `asm!("mov eax, 2" : "={eax}"(result) : : : "intel")`.
1929 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1930 pub struct InlineAsmOutput {
1931     pub constraint: Symbol,
1932     pub expr: P<Expr>,
1933     pub is_rw: bool,
1934     pub is_indirect: bool,
1935 }
1936
1937 /// Inline assembly.
1938 ///
1939 /// E.g., `asm!("NOP");`.
1940 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1941 pub struct InlineAsm {
1942     pub asm: Symbol,
1943     pub asm_str_style: StrStyle,
1944     pub outputs: Vec<InlineAsmOutput>,
1945     pub inputs: Vec<(Symbol, P<Expr>)>,
1946     pub clobbers: Vec<Symbol>,
1947     pub volatile: bool,
1948     pub alignstack: bool,
1949     pub dialect: AsmDialect,
1950 }
1951
1952 /// A parameter in a function header.
1953 ///
1954 /// E.g., `bar: usize` as in `fn foo(bar: usize)`.
1955 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1956 pub struct Param {
1957     pub attrs: AttrVec,
1958     pub ty: P<Ty>,
1959     pub pat: P<Pat>,
1960     pub id: NodeId,
1961     pub span: Span,
1962     pub is_placeholder: bool,
1963 }
1964
1965 /// Alternative representation for `Arg`s describing `self` parameter of methods.
1966 ///
1967 /// E.g., `&mut self` as in `fn foo(&mut self)`.
1968 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1969 pub enum SelfKind {
1970     /// `self`, `mut self`
1971     Value(Mutability),
1972     /// `&'lt self`, `&'lt mut self`
1973     Region(Option<Lifetime>, Mutability),
1974     /// `self: TYPE`, `mut self: TYPE`
1975     Explicit(P<Ty>, Mutability),
1976 }
1977
1978 pub type ExplicitSelf = Spanned<SelfKind>;
1979
1980 impl Param {
1981     /// Attempts to cast parameter to `ExplicitSelf`.
1982     pub fn to_self(&self) -> Option<ExplicitSelf> {
1983         if let PatKind::Ident(BindingMode::ByValue(mutbl), ident, _) = self.pat.kind {
1984             if ident.name == kw::SelfLower {
1985                 return match self.ty.kind {
1986                     TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
1987                     TyKind::Rptr(lt, MutTy { ref ty, mutbl }) if ty.kind.is_implicit_self() => {
1988                         Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
1989                     }
1990                     _ => Some(respan(
1991                         self.pat.span.to(self.ty.span),
1992                         SelfKind::Explicit(self.ty.clone(), mutbl),
1993                     )),
1994                 };
1995             }
1996         }
1997         None
1998     }
1999
2000     /// Returns `true` if parameter is `self`.
2001     pub fn is_self(&self) -> bool {
2002         if let PatKind::Ident(_, ident, _) = self.pat.kind {
2003             ident.name == kw::SelfLower
2004         } else {
2005             false
2006         }
2007     }
2008
2009     /// Builds a `Param` object from `ExplicitSelf`.
2010     pub fn from_self(attrs: AttrVec, eself: ExplicitSelf, eself_ident: Ident) -> Param {
2011         let span = eself.span.to(eself_ident.span);
2012         let infer_ty = P(Ty { id: DUMMY_NODE_ID, kind: TyKind::ImplicitSelf, span });
2013         let param = |mutbl, ty| Param {
2014             attrs,
2015             pat: P(Pat {
2016                 id: DUMMY_NODE_ID,
2017                 kind: PatKind::Ident(BindingMode::ByValue(mutbl), eself_ident, None),
2018                 span,
2019             }),
2020             span,
2021             ty,
2022             id: DUMMY_NODE_ID,
2023             is_placeholder: false,
2024         };
2025         match eself.node {
2026             SelfKind::Explicit(ty, mutbl) => param(mutbl, ty),
2027             SelfKind::Value(mutbl) => param(mutbl, infer_ty),
2028             SelfKind::Region(lt, mutbl) => param(
2029                 Mutability::Not,
2030                 P(Ty {
2031                     id: DUMMY_NODE_ID,
2032                     kind: TyKind::Rptr(lt, MutTy { ty: infer_ty, mutbl }),
2033                     span,
2034                 }),
2035             ),
2036         }
2037     }
2038 }
2039
2040 /// A signature (not the body) of a function declaration.
2041 ///
2042 /// E.g., `fn foo(bar: baz)`.
2043 ///
2044 /// Please note that it's different from `FnHeader` structure
2045 /// which contains metadata about function safety, asyncness, constness and ABI.
2046 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2047 pub struct FnDecl {
2048     pub inputs: Vec<Param>,
2049     pub output: FnRetTy,
2050 }
2051
2052 impl FnDecl {
2053     pub fn get_self(&self) -> Option<ExplicitSelf> {
2054         self.inputs.get(0).and_then(Param::to_self)
2055     }
2056     pub fn has_self(&self) -> bool {
2057         self.inputs.get(0).map_or(false, Param::is_self)
2058     }
2059     pub fn c_variadic(&self) -> bool {
2060         self.inputs.last().map_or(false, |arg| match arg.ty.kind {
2061             TyKind::CVarArgs => true,
2062             _ => false,
2063         })
2064     }
2065 }
2066
2067 /// Is the trait definition an auto trait?
2068 #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
2069 pub enum IsAuto {
2070     Yes,
2071     No,
2072 }
2073
2074 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable, Debug)]
2075 #[derive(HashStable_Generic)]
2076 pub enum Unsafe {
2077     Yes(Span),
2078     No,
2079 }
2080
2081 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
2082 pub enum Async {
2083     Yes { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
2084     No,
2085 }
2086
2087 impl Async {
2088     pub fn is_async(self) -> bool {
2089         if let Async::Yes { .. } = self { true } else { false }
2090     }
2091
2092     /// In this case this is an `async` return, the `NodeId` for the generated `impl Trait` item.
2093     pub fn opt_return_id(self) -> Option<NodeId> {
2094         match self {
2095             Async::Yes { return_impl_trait_id, .. } => Some(return_impl_trait_id),
2096             Async::No => None,
2097         }
2098     }
2099 }
2100
2101 #[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
2102 #[derive(HashStable_Generic)]
2103 pub enum Const {
2104     Yes(Span),
2105     No,
2106 }
2107
2108 /// Item defaultness.
2109 /// For details see the [RFC #2532](https://github.com/rust-lang/rfcs/pull/2532).
2110 #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
2111 pub enum Defaultness {
2112     Default(Span),
2113     Final,
2114 }
2115
2116 #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable_Generic)]
2117 pub enum ImplPolarity {
2118     /// `impl Trait for Type`
2119     Positive,
2120     /// `impl !Trait for Type`
2121     Negative(Span),
2122 }
2123
2124 impl fmt::Debug for ImplPolarity {
2125     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2126         match *self {
2127             ImplPolarity::Positive => "positive".fmt(f),
2128             ImplPolarity::Negative(_) => "negative".fmt(f),
2129         }
2130     }
2131 }
2132
2133 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2134 pub enum FnRetTy {
2135     /// Returns type is not specified.
2136     ///
2137     /// Functions default to `()` and closures default to inference.
2138     /// Span points to where return type would be inserted.
2139     Default(Span),
2140     /// Everything else.
2141     Ty(P<Ty>),
2142 }
2143
2144 impl FnRetTy {
2145     pub fn span(&self) -> Span {
2146         match *self {
2147             FnRetTy::Default(span) => span,
2148             FnRetTy::Ty(ref ty) => ty.span,
2149         }
2150     }
2151 }
2152
2153 /// Module declaration.
2154 ///
2155 /// E.g., `mod foo;` or `mod foo { .. }`.
2156 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2157 pub struct Mod {
2158     /// A span from the first token past `{` to the last token until `}`.
2159     /// For `mod foo;`, the inner span ranges from the first token
2160     /// to the last token in the external file.
2161     pub inner: Span,
2162     pub items: Vec<P<Item>>,
2163     /// `true` for `mod foo { .. }`; `false` for `mod foo;`.
2164     pub inline: bool,
2165 }
2166
2167 /// Foreign module declaration.
2168 ///
2169 /// E.g., `extern { .. }` or `extern C { .. }`.
2170 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2171 pub struct ForeignMod {
2172     pub abi: Option<StrLit>,
2173     pub items: Vec<P<ForeignItem>>,
2174 }
2175
2176 /// Global inline assembly.
2177 ///
2178 /// Also known as "module-level assembly" or "file-scoped assembly".
2179 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy)]
2180 pub struct GlobalAsm {
2181     pub asm: Symbol,
2182 }
2183
2184 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2185 pub struct EnumDef {
2186     pub variants: Vec<Variant>,
2187 }
2188 /// Enum variant.
2189 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2190 pub struct Variant {
2191     /// Attributes of the variant.
2192     pub attrs: Vec<Attribute>,
2193     /// Id of the variant (not the constructor, see `VariantData::ctor_id()`).
2194     pub id: NodeId,
2195     /// Span
2196     pub span: Span,
2197     /// The visibility of the variant. Syntactically accepted but not semantically.
2198     pub vis: Visibility,
2199     /// Name of the variant.
2200     pub ident: Ident,
2201
2202     /// Fields and constructor id of the variant.
2203     pub data: VariantData,
2204     /// Explicit discriminant, e.g., `Foo = 1`.
2205     pub disr_expr: Option<AnonConst>,
2206     /// Is a macro placeholder
2207     pub is_placeholder: bool,
2208 }
2209
2210 /// Part of `use` item to the right of its prefix.
2211 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2212 pub enum UseTreeKind {
2213     /// `use prefix` or `use prefix as rename`
2214     ///
2215     /// The extra `NodeId`s are for HIR lowering, when additional statements are created for each
2216     /// namespace.
2217     Simple(Option<Ident>, NodeId, NodeId),
2218     /// `use prefix::{...}`
2219     Nested(Vec<(UseTree, NodeId)>),
2220     /// `use prefix::*`
2221     Glob,
2222 }
2223
2224 /// A tree of paths sharing common prefixes.
2225 /// Used in `use` items both at top-level and inside of braces in import groups.
2226 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2227 pub struct UseTree {
2228     pub prefix: Path,
2229     pub kind: UseTreeKind,
2230     pub span: Span,
2231 }
2232
2233 impl UseTree {
2234     pub fn ident(&self) -> Ident {
2235         match self.kind {
2236             UseTreeKind::Simple(Some(rename), ..) => rename,
2237             UseTreeKind::Simple(None, ..) => {
2238                 self.prefix.segments.last().expect("empty prefix in a simple import").ident
2239             }
2240             _ => panic!("`UseTree::ident` can only be used on a simple import"),
2241         }
2242     }
2243 }
2244
2245 /// Distinguishes between `Attribute`s that decorate items and Attributes that
2246 /// are contained as statements within items. These two cases need to be
2247 /// distinguished for pretty-printing.
2248 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy, HashStable_Generic)]
2249 pub enum AttrStyle {
2250     Outer,
2251     Inner,
2252 }
2253
2254 #[derive(Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, Copy)]
2255 pub struct AttrId(pub usize);
2256
2257 impl Idx for AttrId {
2258     fn new(idx: usize) -> Self {
2259         AttrId(idx)
2260     }
2261     fn index(self) -> usize {
2262         self.0
2263     }
2264 }
2265
2266 impl rustc_serialize::Encodable for AttrId {
2267     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
2268         s.emit_unit()
2269     }
2270 }
2271
2272 impl rustc_serialize::Decodable for AttrId {
2273     fn decode<D: Decoder>(d: &mut D) -> Result<AttrId, D::Error> {
2274         d.read_nil().map(|_| crate::attr::mk_attr_id())
2275     }
2276 }
2277
2278 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
2279 pub struct AttrItem {
2280     pub path: Path,
2281     pub args: MacArgs,
2282 }
2283
2284 /// A list of attributes.
2285 pub type AttrVec = ThinVec<Attribute>;
2286
2287 /// Metadata associated with an item.
2288 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2289 pub struct Attribute {
2290     pub kind: AttrKind,
2291     pub id: AttrId,
2292     /// Denotes if the attribute decorates the following construct (outer)
2293     /// or the construct this attribute is contained within (inner).
2294     pub style: AttrStyle,
2295     pub span: Span,
2296 }
2297
2298 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2299 pub enum AttrKind {
2300     /// A normal attribute.
2301     Normal(AttrItem),
2302
2303     /// A doc comment (e.g. `/// ...`, `//! ...`, `/** ... */`, `/*! ... */`).
2304     /// Doc attributes (e.g. `#[doc="..."]`) are represented with the `Normal`
2305     /// variant (which is much less compact and thus more expensive).
2306     DocComment(Symbol),
2307 }
2308
2309 /// `TraitRef`s appear in impls.
2310 ///
2311 /// Resolution maps each `TraitRef`'s `ref_id` to its defining trait; that's all
2312 /// that the `ref_id` is for. The `impl_id` maps to the "self type" of this impl.
2313 /// If this impl is an `ItemKind::Impl`, the `impl_id` is redundant (it could be the
2314 /// same as the impl's `NodeId`).
2315 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2316 pub struct TraitRef {
2317     pub path: Path,
2318     pub ref_id: NodeId,
2319 }
2320
2321 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2322 pub struct PolyTraitRef {
2323     /// The `'a` in `<'a> Foo<&'a T>`.
2324     pub bound_generic_params: Vec<GenericParam>,
2325
2326     /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`.
2327     pub trait_ref: TraitRef,
2328
2329     pub span: Span,
2330 }
2331
2332 impl PolyTraitRef {
2333     pub fn new(generic_params: Vec<GenericParam>, path: Path, span: Span) -> Self {
2334         PolyTraitRef {
2335             bound_generic_params: generic_params,
2336             trait_ref: TraitRef { path, ref_id: DUMMY_NODE_ID },
2337             span,
2338         }
2339     }
2340 }
2341
2342 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
2343 pub enum CrateSugar {
2344     /// Source is `pub(crate)`.
2345     PubCrate,
2346
2347     /// Source is (just) `crate`.
2348     JustCrate,
2349 }
2350
2351 pub type Visibility = Spanned<VisibilityKind>;
2352
2353 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2354 pub enum VisibilityKind {
2355     Public,
2356     Crate(CrateSugar),
2357     Restricted { path: P<Path>, id: NodeId },
2358     Inherited,
2359 }
2360
2361 impl VisibilityKind {
2362     pub fn is_pub(&self) -> bool {
2363         if let VisibilityKind::Public = *self { true } else { false }
2364     }
2365 }
2366
2367 /// Field of a struct.
2368 ///
2369 /// E.g., `bar: usize` as in `struct Foo { bar: usize }`.
2370 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2371 pub struct StructField {
2372     pub attrs: Vec<Attribute>,
2373     pub id: NodeId,
2374     pub span: Span,
2375     pub vis: Visibility,
2376     pub ident: Option<Ident>,
2377
2378     pub ty: P<Ty>,
2379     pub is_placeholder: bool,
2380 }
2381
2382 /// Fields and constructor ids of enum variants and structs.
2383 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2384 pub enum VariantData {
2385     /// Struct variant.
2386     ///
2387     /// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
2388     Struct(Vec<StructField>, bool),
2389     /// Tuple variant.
2390     ///
2391     /// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
2392     Tuple(Vec<StructField>, NodeId),
2393     /// Unit variant.
2394     ///
2395     /// E.g., `Bar = ..` as in `enum Foo { Bar = .. }`.
2396     Unit(NodeId),
2397 }
2398
2399 impl VariantData {
2400     /// Return the fields of this variant.
2401     pub fn fields(&self) -> &[StructField] {
2402         match *self {
2403             VariantData::Struct(ref fields, ..) | VariantData::Tuple(ref fields, _) => fields,
2404             _ => &[],
2405         }
2406     }
2407
2408     /// Return the `NodeId` of this variant's constructor, if it has one.
2409     pub fn ctor_id(&self) -> Option<NodeId> {
2410         match *self {
2411             VariantData::Struct(..) => None,
2412             VariantData::Tuple(_, id) | VariantData::Unit(id) => Some(id),
2413         }
2414     }
2415 }
2416
2417 /// An item definition.
2418 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2419 pub struct Item<K = ItemKind> {
2420     pub attrs: Vec<Attribute>,
2421     pub id: NodeId,
2422     pub span: Span,
2423     pub vis: Visibility,
2424     /// The name of the item.
2425     /// It might be a dummy name in case of anonymous items.
2426     pub ident: Ident,
2427
2428     pub kind: K,
2429
2430     /// Original tokens this item was parsed from. This isn't necessarily
2431     /// available for all items, although over time more and more items should
2432     /// have this be `Some`. Right now this is primarily used for procedural
2433     /// macros, notably custom attributes.
2434     ///
2435     /// Note that the tokens here do not include the outer attributes, but will
2436     /// include inner attributes.
2437     pub tokens: Option<TokenStream>,
2438 }
2439
2440 impl Item {
2441     /// Return the span that encompasses the attributes.
2442     pub fn span_with_attributes(&self) -> Span {
2443         self.attrs.iter().fold(self.span, |acc, attr| acc.to(attr.span))
2444     }
2445 }
2446
2447 impl<K: Into<ItemKind>> Item<K> {
2448     pub fn into_item(self) -> Item {
2449         let Item { attrs, id, span, vis, ident, kind, tokens } = self;
2450         Item { attrs, id, span, vis, ident, kind: kind.into(), tokens }
2451     }
2452 }
2453
2454 /// `extern` qualifier on a function item or function type.
2455 #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)]
2456 pub enum Extern {
2457     None,
2458     Implicit,
2459     Explicit(StrLit),
2460 }
2461
2462 impl Extern {
2463     pub fn from_abi(abi: Option<StrLit>) -> Extern {
2464         abi.map_or(Extern::Implicit, Extern::Explicit)
2465     }
2466 }
2467
2468 /// A function header.
2469 ///
2470 /// All the information between the visibility and the name of the function is
2471 /// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
2472 #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)]
2473 pub struct FnHeader {
2474     pub unsafety: Unsafe,
2475     pub asyncness: Async,
2476     pub constness: Const,
2477     pub ext: Extern,
2478 }
2479
2480 impl FnHeader {
2481     /// Does this function header have any qualifiers or is it empty?
2482     pub fn has_qualifiers(&self) -> bool {
2483         let Self { unsafety, asyncness, constness, ext } = self;
2484         matches!(unsafety, Unsafe::Yes(_))
2485             || asyncness.is_async()
2486             || matches!(constness, Const::Yes(_))
2487             || !matches!(ext, Extern::None)
2488     }
2489 }
2490
2491 impl Default for FnHeader {
2492     fn default() -> FnHeader {
2493         FnHeader {
2494             unsafety: Unsafe::No,
2495             asyncness: Async::No,
2496             constness: Const::No,
2497             ext: Extern::None,
2498         }
2499     }
2500 }
2501
2502 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2503 pub enum ItemKind {
2504     /// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
2505     ///
2506     /// E.g., `extern crate foo` or `extern crate foo_bar as foo`.
2507     ExternCrate(Option<Name>),
2508     /// A use declaration item (`use`).
2509     ///
2510     /// E.g., `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`.
2511     Use(P<UseTree>),
2512     /// A static item (`static`).
2513     ///
2514     /// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`.
2515     Static(P<Ty>, Mutability, Option<P<Expr>>),
2516     /// A constant item (`const`).
2517     ///
2518     /// E.g., `const FOO: i32 = 42;`.
2519     Const(Defaultness, P<Ty>, Option<P<Expr>>),
2520     /// A function declaration (`fn`).
2521     ///
2522     /// E.g., `fn foo(bar: usize) -> usize { .. }`.
2523     Fn(Defaultness, FnSig, Generics, Option<P<Block>>),
2524     /// A module declaration (`mod`).
2525     ///
2526     /// E.g., `mod foo;` or `mod foo { .. }`.
2527     Mod(Mod),
2528     /// An external module (`extern`).
2529     ///
2530     /// E.g., `extern {}` or `extern "C" {}`.
2531     ForeignMod(ForeignMod),
2532     /// Module-level inline assembly (from `global_asm!()`).
2533     GlobalAsm(P<GlobalAsm>),
2534     /// A type alias (`type`).
2535     ///
2536     /// E.g., `type Foo = Bar<u8>;`.
2537     TyAlias(Defaultness, Generics, GenericBounds, Option<P<Ty>>),
2538     /// An enum definition (`enum`).
2539     ///
2540     /// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
2541     Enum(EnumDef, Generics),
2542     /// A struct definition (`struct`).
2543     ///
2544     /// E.g., `struct Foo<A> { x: A }`.
2545     Struct(VariantData, Generics),
2546     /// A union definition (`union`).
2547     ///
2548     /// E.g., `union Foo<A, B> { x: A, y: B }`.
2549     Union(VariantData, Generics),
2550     /// A trait declaration (`trait`).
2551     ///
2552     /// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
2553     Trait(IsAuto, Unsafe, Generics, GenericBounds, Vec<P<AssocItem>>),
2554     /// Trait alias
2555     ///
2556     /// E.g., `trait Foo = Bar + Quux;`.
2557     TraitAlias(Generics, GenericBounds),
2558     /// An implementation.
2559     ///
2560     /// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
2561     Impl {
2562         unsafety: Unsafe,
2563         polarity: ImplPolarity,
2564         defaultness: Defaultness,
2565         constness: Const,
2566         generics: Generics,
2567
2568         /// The trait being implemented, if any.
2569         of_trait: Option<TraitRef>,
2570
2571         self_ty: P<Ty>,
2572         items: Vec<P<AssocItem>>,
2573     },
2574     /// A macro invocation.
2575     ///
2576     /// E.g., `foo!(..)`.
2577     Mac(Mac),
2578
2579     /// A macro definition.
2580     MacroDef(MacroDef),
2581 }
2582
2583 impl ItemKind {
2584     pub fn article(&self) -> &str {
2585         use ItemKind::*;
2586         match self {
2587             Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
2588             | Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..) => "a",
2589             ExternCrate(..) | ForeignMod(..) | Mac(..) | Enum(..) | Impl { .. } => "an",
2590         }
2591     }
2592
2593     pub fn descr(&self) -> &str {
2594         match self {
2595             ItemKind::ExternCrate(..) => "extern crate",
2596             ItemKind::Use(..) => "`use` import",
2597             ItemKind::Static(..) => "static item",
2598             ItemKind::Const(..) => "constant item",
2599             ItemKind::Fn(..) => "function",
2600             ItemKind::Mod(..) => "module",
2601             ItemKind::ForeignMod(..) => "extern block",
2602             ItemKind::GlobalAsm(..) => "global asm item",
2603             ItemKind::TyAlias(..) => "type alias",
2604             ItemKind::Enum(..) => "enum",
2605             ItemKind::Struct(..) => "struct",
2606             ItemKind::Union(..) => "union",
2607             ItemKind::Trait(..) => "trait",
2608             ItemKind::TraitAlias(..) => "trait alias",
2609             ItemKind::Mac(..) => "item macro invocation",
2610             ItemKind::MacroDef(..) => "macro definition",
2611             ItemKind::Impl { .. } => "implementation",
2612         }
2613     }
2614
2615     pub fn generics(&self) -> Option<&Generics> {
2616         match self {
2617             Self::Fn(_, _, generics, _)
2618             | Self::TyAlias(_, generics, ..)
2619             | Self::Enum(_, generics)
2620             | Self::Struct(_, generics)
2621             | Self::Union(_, generics)
2622             | Self::Trait(_, _, generics, ..)
2623             | Self::TraitAlias(generics, _)
2624             | Self::Impl { generics, .. } => Some(generics),
2625             _ => None,
2626         }
2627     }
2628 }
2629
2630 /// Represents associated items.
2631 /// These include items in `impl` and `trait` definitions.
2632 pub type AssocItem = Item<AssocItemKind>;
2633
2634 /// Represents associated item kinds.
2635 ///
2636 /// The term "provided" in the variants below refers to the item having a default
2637 /// definition / body. Meanwhile, a "required" item lacks a definition / body.
2638 /// In an implementation, all items must be provided.
2639 /// The `Option`s below denote the bodies, where `Some(_)`
2640 /// means "provided" and conversely `None` means "required".
2641 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2642 pub enum AssocItemKind {
2643     /// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
2644     /// If `def` is parsed, then the constant is provided, and otherwise required.
2645     Const(Defaultness, P<Ty>, Option<P<Expr>>),
2646     /// An associated function.
2647     Fn(Defaultness, FnSig, Generics, Option<P<Block>>),
2648     /// An associated type.
2649     TyAlias(Defaultness, Generics, GenericBounds, Option<P<Ty>>),
2650     /// A macro expanding to associated items.
2651     Macro(Mac),
2652 }
2653
2654 impl AssocItemKind {
2655     pub fn defaultness(&self) -> Defaultness {
2656         match *self {
2657             Self::Const(def, ..) | Self::Fn(def, ..) | Self::TyAlias(def, ..) => def,
2658             Self::Macro(..) => Defaultness::Final,
2659         }
2660     }
2661 }
2662
2663 impl From<AssocItemKind> for ItemKind {
2664     fn from(assoc_item_kind: AssocItemKind) -> ItemKind {
2665         match assoc_item_kind {
2666             AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c),
2667             AssocItemKind::Fn(a, b, c, d) => ItemKind::Fn(a, b, c, d),
2668             AssocItemKind::TyAlias(a, b, c, d) => ItemKind::TyAlias(a, b, c, d),
2669             AssocItemKind::Macro(a) => ItemKind::Mac(a),
2670         }
2671     }
2672 }
2673
2674 impl TryFrom<ItemKind> for AssocItemKind {
2675     type Error = ItemKind;
2676
2677     fn try_from(item_kind: ItemKind) -> Result<AssocItemKind, ItemKind> {
2678         Ok(match item_kind {
2679             ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c),
2680             ItemKind::Fn(a, b, c, d) => AssocItemKind::Fn(a, b, c, d),
2681             ItemKind::TyAlias(a, b, c, d) => AssocItemKind::TyAlias(a, b, c, d),
2682             ItemKind::Mac(a) => AssocItemKind::Macro(a),
2683             _ => return Err(item_kind),
2684         })
2685     }
2686 }
2687
2688 /// An item in `extern` block.
2689 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2690 pub enum ForeignItemKind {
2691     /// A foreign static item (`static FOO: u8`).
2692     Static(P<Ty>, Mutability, Option<P<Expr>>),
2693     /// A foreign function.
2694     Fn(Defaultness, FnSig, Generics, Option<P<Block>>),
2695     /// A foreign type.
2696     TyAlias(Defaultness, Generics, GenericBounds, Option<P<Ty>>),
2697     /// A macro expanding to foreign items.
2698     Macro(Mac),
2699 }
2700
2701 impl From<ForeignItemKind> for ItemKind {
2702     fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
2703         match foreign_item_kind {
2704             ForeignItemKind::Static(a, b, c) => ItemKind::Static(a, b, c),
2705             ForeignItemKind::Fn(a, b, c, d) => ItemKind::Fn(a, b, c, d),
2706             ForeignItemKind::TyAlias(a, b, c, d) => ItemKind::TyAlias(a, b, c, d),
2707             ForeignItemKind::Macro(a) => ItemKind::Mac(a),
2708         }
2709     }
2710 }
2711
2712 impl TryFrom<ItemKind> for ForeignItemKind {
2713     type Error = ItemKind;
2714
2715     fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
2716         Ok(match item_kind {
2717             ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c),
2718             ItemKind::Fn(a, b, c, d) => ForeignItemKind::Fn(a, b, c, d),
2719             ItemKind::TyAlias(a, b, c, d) => ForeignItemKind::TyAlias(a, b, c, d),
2720             ItemKind::Mac(a) => ForeignItemKind::Macro(a),
2721             _ => return Err(item_kind),
2722         })
2723     }
2724 }
2725
2726 pub type ForeignItem = Item<ForeignItemKind>;