1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 pub use self::BindingMode::*;
14 pub use self::BinOp_::*;
15 pub use self::BlockCheckMode::*;
16 pub use self::CaptureClause::*;
17 pub use self::Decl_::*;
18 pub use self::Expr_::*;
19 pub use self::FunctionRetTy::*;
20 pub use self::ForeignItem_::*;
21 pub use self::Item_::*;
22 pub use self::Mutability::*;
23 pub use self::PathListItem_::*;
24 pub use self::PrimTy::*;
25 pub use self::Stmt_::*;
26 pub use self::TraitItem_::*;
28 pub use self::TyParamBound::*;
29 pub use self::UnOp::*;
30 pub use self::UnsafeSource::*;
31 pub use self::ViewPath_::*;
32 pub use self::Visibility::{Public, Inherited};
33 pub use self::PathParameters::*;
36 use hir::def_id::DefId;
37 use util::nodemap::{NodeMap, FnvHashSet};
39 use syntax_pos::{BytePos, mk_sp, Span, ExpnId};
40 use syntax::codemap::{self, respan, Spanned};
42 use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, AsmDialect};
43 use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
44 use syntax::parse::token::{keywords, InternedString};
46 use syntax::tokenstream::TokenTree;
47 use syntax::util::ThinVec;
49 use std::collections::BTreeMap;
52 /// HIR doesn't commit to a concrete storage type and have its own alias for a vector.
53 /// It can be `Vec`, `P<[T]>` or potentially `Box<[T]>`, or some other container with similar
54 /// behavior. Unlike AST, HIR is mostly a static structure, so we can use an owned slice instead
55 /// of `Vec` to avoid keeping extra capacity.
56 pub type HirVec<T> = P<[T]>;
58 macro_rules! hir_vec {
59 ($elem:expr; $n:expr) => (
60 $crate::hir::HirVec::from(vec![$elem; $n])
63 $crate::hir::HirVec::from(vec![$($x),*])
65 ($($x:expr,)*) => (hir_vec![$($x),*])
79 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
86 impl fmt::Debug for Lifetime {
87 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91 print::lifetime_to_string(self))
95 /// A lifetime definition, eg `'a: 'b+'c+'d`
96 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
97 pub struct LifetimeDef {
98 pub lifetime: Lifetime,
99 pub bounds: HirVec<Lifetime>,
102 /// A "Path" is essentially Rust's notion of a name; for instance:
103 /// std::cmp::PartialEq . It's represented as a sequence of identifiers,
104 /// along with a bunch of supporting information.
105 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
108 /// A `::foo` path, is relative to the crate root rather than current
109 /// module (like paths in an import).
111 /// The segments in the path: the things separated by `::`.
112 pub segments: HirVec<PathSegment>,
115 impl fmt::Debug for Path {
116 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117 write!(f, "path({})", print::path_to_string(self))
121 impl fmt::Display for Path {
122 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123 write!(f, "{}", print::path_to_string(self))
128 /// Convert a span and an identifier to the corresponding
130 pub fn from_name(s: Span, name: Name) -> Path {
134 segments: hir_vec![PathSegment {
136 parameters: PathParameters::none()
142 /// A segment of a path: an identifier, an optional lifetime, and a set of
144 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
145 pub struct PathSegment {
146 /// The identifier portion of this path segment.
149 /// Type/lifetime parameters attached to this path. They come in
150 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
151 /// this is more than just simple syntactic sugar; the use of
152 /// parens affects the region binding rules, so we preserve the
154 pub parameters: PathParameters,
157 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
158 pub enum PathParameters {
159 /// The `<'a, A,B,C>` in `foo::bar::baz::<'a, A,B,C>`
160 AngleBracketedParameters(AngleBracketedParameterData),
161 /// The `(A,B)` and `C` in `Foo(A,B) -> C`
162 ParenthesizedParameters(ParenthesizedParameterData),
165 impl PathParameters {
166 pub fn none() -> PathParameters {
167 AngleBracketedParameters(AngleBracketedParameterData {
168 lifetimes: HirVec::new(),
169 types: HirVec::new(),
170 bindings: HirVec::new(),
174 pub fn is_empty(&self) -> bool {
176 AngleBracketedParameters(ref data) => data.is_empty(),
178 // Even if the user supplied no types, something like
179 // `X()` is equivalent to `X<(),()>`.
180 ParenthesizedParameters(..) => false,
184 pub fn has_lifetimes(&self) -> bool {
186 AngleBracketedParameters(ref data) => !data.lifetimes.is_empty(),
187 ParenthesizedParameters(_) => false,
191 pub fn has_types(&self) -> bool {
193 AngleBracketedParameters(ref data) => !data.types.is_empty(),
194 ParenthesizedParameters(..) => true,
198 /// Returns the types that the user wrote. Note that these do not necessarily map to the type
199 /// parameters in the parenthesized case.
200 pub fn types(&self) -> HirVec<&P<Ty>> {
202 AngleBracketedParameters(ref data) => {
203 data.types.iter().collect()
205 ParenthesizedParameters(ref data) => {
208 .chain(data.output.iter())
214 pub fn lifetimes(&self) -> HirVec<&Lifetime> {
216 AngleBracketedParameters(ref data) => {
217 data.lifetimes.iter().collect()
219 ParenthesizedParameters(_) => {
225 pub fn bindings(&self) -> HirVec<&TypeBinding> {
227 AngleBracketedParameters(ref data) => {
228 data.bindings.iter().collect()
230 ParenthesizedParameters(_) => {
237 /// A path like `Foo<'a, T>`
238 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
239 pub struct AngleBracketedParameterData {
240 /// The lifetime parameters for this path segment.
241 pub lifetimes: HirVec<Lifetime>,
242 /// The type parameters for this path segment, if present.
243 pub types: HirVec<P<Ty>>,
244 /// Bindings (equality constraints) on associated types, if present.
245 /// E.g., `Foo<A=Bar>`.
246 pub bindings: HirVec<TypeBinding>,
249 impl AngleBracketedParameterData {
250 fn is_empty(&self) -> bool {
251 self.lifetimes.is_empty() && self.types.is_empty() && self.bindings.is_empty()
255 /// A path like `Foo(A,B) -> C`
256 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
257 pub struct ParenthesizedParameterData {
262 pub inputs: HirVec<P<Ty>>,
265 pub output: Option<P<Ty>>,
268 /// The AST represents all type param bounds as types.
269 /// typeck::collect::compute_bounds matches these against
270 /// the "special" built-in traits (see middle::lang_items) and
271 /// detects Copy, Send and Sync.
272 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
273 pub enum TyParamBound {
274 TraitTyParamBound(PolyTraitRef, TraitBoundModifier),
275 RegionTyParamBound(Lifetime),
278 /// A modifier on a bound, currently this is only used for `?Sized`, where the
279 /// modifier is `Maybe`. Negative bounds should also be handled here.
280 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
281 pub enum TraitBoundModifier {
286 pub type TyParamBounds = HirVec<TyParamBound>;
288 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
292 pub bounds: TyParamBounds,
293 pub default: Option<P<Ty>>,
297 /// Represents lifetimes and type parameters attached to a declaration
298 /// of a function, enum, trait, etc.
299 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
300 pub struct Generics {
301 pub lifetimes: HirVec<LifetimeDef>,
302 pub ty_params: HirVec<TyParam>,
303 pub where_clause: WhereClause,
307 pub fn empty() -> Generics {
309 lifetimes: HirVec::new(),
310 ty_params: HirVec::new(),
311 where_clause: WhereClause {
313 predicates: HirVec::new(),
318 pub fn is_lt_parameterized(&self) -> bool {
319 !self.lifetimes.is_empty()
322 pub fn is_type_parameterized(&self) -> bool {
323 !self.ty_params.is_empty()
326 pub fn is_parameterized(&self) -> bool {
327 self.is_lt_parameterized() || self.is_type_parameterized()
330 // Does return a span which includes lifetimes and type parameters,
332 pub fn span(&self) -> Option<Span> {
333 if !self.is_parameterized() {
336 let mut span: Option<Span> = None;
337 for lifetime in self.lifetimes.iter() {
338 if let Some(ref mut span) = span {
339 let life_span = lifetime.lifetime.span;
340 span.hi = if span.hi > life_span.hi { span.hi } else { life_span.hi };
341 span.lo = if span.lo < life_span.lo { span.lo } else { life_span.lo };
343 span = Some(lifetime.lifetime.span.clone());
346 for ty_param in self.ty_params.iter() {
347 if let Some(ref mut span) = span {
348 span.lo = if span.lo < ty_param.span.lo { span.lo } else { ty_param.span.lo };
349 span.hi = if span.hi > ty_param.span.hi { span.hi } else { ty_param.span.hi };
351 span = Some(ty_param.span.clone());
354 if let Some(ref mut span) = span {
355 span.lo = span.lo - BytePos(1);
356 span.hi = span.hi + BytePos(1);
363 /// A `where` clause in a definition
364 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
365 pub struct WhereClause {
367 pub predicates: HirVec<WherePredicate>,
370 /// A single predicate in a `where` clause
371 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
372 pub enum WherePredicate {
373 /// A type binding, eg `for<'c> Foo: Send+Clone+'c`
374 BoundPredicate(WhereBoundPredicate),
375 /// A lifetime predicate, e.g. `'a: 'b+'c`
376 RegionPredicate(WhereRegionPredicate),
377 /// An equality predicate (unsupported)
378 EqPredicate(WhereEqPredicate),
381 /// A type bound, eg `for<'c> Foo: Send+Clone+'c`
382 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
383 pub struct WhereBoundPredicate {
385 /// Any lifetimes from a `for` binding
386 pub bound_lifetimes: HirVec<LifetimeDef>,
387 /// The type being bounded
388 pub bounded_ty: P<Ty>,
389 /// Trait and lifetime bounds (`Clone+Send+'static`)
390 pub bounds: TyParamBounds,
393 /// A lifetime predicate, e.g. `'a: 'b+'c`
394 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
395 pub struct WhereRegionPredicate {
397 pub lifetime: Lifetime,
398 pub bounds: HirVec<Lifetime>,
401 /// An equality predicate (unsupported), e.g. `T=int`
402 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
403 pub struct WhereEqPredicate {
410 pub type CrateConfig = HirVec<P<MetaItem>>;
412 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
415 pub attrs: HirVec<Attribute>,
416 pub config: CrateConfig,
418 pub exported_macros: HirVec<MacroDef>,
420 // NB: We use a BTreeMap here so that `visit_all_items` iterates
421 // over the ids in increasing order. In principle it should not
422 // matter what order we visit things in, but in *practice* it
423 // does, because it can affect the order in which errors are
424 // detected, which in turn can make compile-fail tests yield
425 // slightly different results.
426 pub items: BTreeMap<NodeId, Item>,
430 pub fn item(&self, id: NodeId) -> &Item {
434 /// Visits all items in the crate in some determinstic (but
435 /// unspecified) order. If you just need to process every item,
436 /// but don't care about nesting, this method is the best choice.
438 /// If you do care about nesting -- usually because your algorithm
439 /// follows lexical scoping rules -- then you want a different
440 /// approach. You should override `visit_nested_item` in your
441 /// visitor and then call `intravisit::walk_crate` instead.
442 pub fn visit_all_items<'hir, V>(&'hir self, visitor: &mut V)
443 where V: intravisit::Visitor<'hir>
445 for (_, item) in &self.items {
446 visitor.visit_item(item);
451 /// A macro definition, in this crate or imported from another.
453 /// Not parsed directly, but created on macro import or `macro_rules!` expansion.
454 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
455 pub struct MacroDef {
457 pub attrs: HirVec<Attribute>,
460 pub imported_from: Option<Name>,
462 pub use_locally: bool,
463 pub allow_internal_unstable: bool,
464 pub body: HirVec<TokenTree>,
467 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
469 /// Statements in a block
470 pub stmts: HirVec<Stmt>,
471 /// An expression at the end of the block
472 /// without a semicolon, if any
473 pub expr: Option<P<Expr>>,
475 /// Distinguishes between `unsafe { ... }` and `{ ... }`
476 pub rules: BlockCheckMode,
480 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
487 impl fmt::Debug for Pat {
488 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
489 write!(f, "pat({}: {})", self.id, print::pat_to_string(self))
494 // FIXME(#19596) this is a workaround, but there should be a better way
495 fn walk_<G>(&self, it: &mut G) -> bool
496 where G: FnMut(&Pat) -> bool
503 PatKind::Binding(_, _, Some(ref p)) => p.walk_(it),
504 PatKind::Struct(_, ref fields, _) => {
505 fields.iter().all(|field| field.node.pat.walk_(it))
507 PatKind::TupleStruct(_, ref s, _) | PatKind::Tuple(ref s, _) => {
508 s.iter().all(|p| p.walk_(it))
510 PatKind::Box(ref s) | PatKind::Ref(ref s, _) => {
513 PatKind::Vec(ref before, ref slice, ref after) => {
514 before.iter().all(|p| p.walk_(it)) &&
515 slice.iter().all(|p| p.walk_(it)) &&
516 after.iter().all(|p| p.walk_(it))
520 PatKind::Range(_, _) |
521 PatKind::Binding(..) |
522 PatKind::Path(..) => {
528 pub fn walk<F>(&self, mut it: F) -> bool
529 where F: FnMut(&Pat) -> bool
535 /// A single field in a struct pattern
537 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
538 /// are treated the same as` x: x, y: ref y, z: ref mut z`,
539 /// except is_shorthand is true
540 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
541 pub struct FieldPat {
542 /// The identifier for the field
544 /// The pattern the field is destructured to
546 pub is_shorthand: bool,
549 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
550 pub enum BindingMode {
551 BindByRef(Mutability),
552 BindByValue(Mutability),
555 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
557 /// Represents a wildcard pattern (`_`)
560 /// A fresh binding `ref mut binding @ OPT_SUBPATTERN`.
561 Binding(BindingMode, Spanned<Name>, Option<P<Pat>>),
563 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
564 /// The `bool` is `true` in the presence of a `..`.
565 Struct(Path, HirVec<Spanned<FieldPat>>, bool),
567 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
568 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
569 /// 0 <= position <= subpats.len()
570 TupleStruct(Path, HirVec<P<Pat>>, Option<usize>),
572 /// A possibly qualified path pattern.
573 /// Such pattern can be resolved to a unit struct/variant or a constant.
574 Path(Option<QSelf>, Path),
576 /// A tuple pattern `(a, b)`.
577 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
578 /// 0 <= position <= subpats.len()
579 Tuple(HirVec<P<Pat>>, Option<usize>),
582 /// A reference pattern, e.g. `&mut (a, b)`
583 Ref(P<Pat>, Mutability),
586 /// A range pattern, e.g. `1...2`
587 Range(P<Expr>, P<Expr>),
588 /// `[a, b, ..i, y, z]` is represented as:
589 /// `PatKind::Vec(box [a, b], Some(i), box [y, z])`
590 Vec(HirVec<P<Pat>>, Option<P<Pat>>, HirVec<P<Pat>>),
593 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
594 pub enum Mutability {
599 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
601 /// The `+` operator (addition)
603 /// The `-` operator (subtraction)
605 /// The `*` operator (multiplication)
607 /// The `/` operator (division)
609 /// The `%` operator (modulus)
611 /// The `&&` operator (logical and)
613 /// The `||` operator (logical or)
615 /// The `^` operator (bitwise xor)
617 /// The `&` operator (bitwise and)
619 /// The `|` operator (bitwise or)
621 /// The `<<` operator (shift left)
623 /// The `>>` operator (shift right)
625 /// The `==` operator (equality)
627 /// The `<` operator (less than)
629 /// The `<=` operator (less than or equal to)
631 /// The `!=` operator (not equal to)
633 /// The `>=` operator (greater than or equal to)
635 /// The `>` operator (greater than)
640 pub fn as_str(self) -> &'static str {
663 pub fn is_lazy(self) -> bool {
665 BiAnd | BiOr => true,
670 pub fn is_shift(self) -> bool {
672 BiShl | BiShr => true,
677 pub fn is_comparison(self) -> bool {
679 BiEq | BiLt | BiLe | BiNe | BiGt | BiGe => true,
695 /// Returns `true` if the binary operator takes its arguments by value
696 pub fn is_by_value(self) -> bool {
697 !self.is_comparison()
701 pub type BinOp = Spanned<BinOp_>;
703 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
705 /// The `*` operator for dereferencing
707 /// The `!` operator for logical inversion
709 /// The `-` operator for negation
714 pub fn as_str(self) -> &'static str {
722 /// Returns `true` if the unary operator takes its argument by value
723 pub fn is_by_value(self) -> bool {
725 UnNeg | UnNot => true,
732 pub type Stmt = Spanned<Stmt_>;
734 impl fmt::Debug for Stmt_ {
735 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
737 let spanned = codemap::dummy_spanned(self.clone());
741 print::stmt_to_string(&spanned))
745 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
747 /// Could be an item or a local (let) binding:
748 StmtDecl(P<Decl>, NodeId),
750 /// Expr without trailing semi-colon (must have unit type):
751 StmtExpr(P<Expr>, NodeId),
753 /// Expr with trailing semi-colon (may have any type):
754 StmtSemi(P<Expr>, NodeId),
758 pub fn attrs(&self) -> &[Attribute] {
760 StmtDecl(ref d, _) => d.node.attrs(),
762 StmtSemi(ref e, _) => &e.attrs,
766 pub fn id(&self) -> NodeId {
768 StmtDecl(_, id) => id,
769 StmtExpr(_, id) => id,
770 StmtSemi(_, id) => id,
775 // FIXME (pending discussion of #1697, #2178...): local should really be
776 // a refinement on pat.
777 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
778 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
781 pub ty: Option<P<Ty>>,
782 /// Initializer expression to set the value, if any
783 pub init: Option<P<Expr>>,
786 pub attrs: ThinVec<Attribute>,
789 pub type Decl = Spanned<Decl_>;
791 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
793 /// A local (let) binding:
800 pub fn attrs(&self) -> &[Attribute] {
802 DeclLocal(ref l) => &l.attrs,
808 /// represents one arm of a 'match'
809 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
811 pub attrs: HirVec<Attribute>,
812 pub pats: HirVec<P<Pat>>,
813 pub guard: Option<P<Expr>>,
817 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
819 pub name: Spanned<Name>,
824 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
825 pub enum BlockCheckMode {
827 UnsafeBlock(UnsafeSource),
828 PushUnsafeBlock(UnsafeSource),
829 PopUnsafeBlock(UnsafeSource),
830 // Within this block (but outside a PopUnstableBlock), we suspend checking of stability.
835 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
836 pub enum UnsafeSource {
842 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
847 pub attrs: ThinVec<Attribute>,
850 impl fmt::Debug for Expr {
851 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
852 write!(f, "expr({}: {})", self.id, print::expr_to_string(self))
856 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
858 /// A `box x` expression.
860 /// An array (`[a, b, c, d]`)
861 ExprVec(HirVec<P<Expr>>),
864 /// The first field resolves to the function itself (usually an `ExprPath`),
865 /// and the second field is the list of arguments
866 ExprCall(P<Expr>, HirVec<P<Expr>>),
867 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
869 /// The `Spanned<Name>` is the identifier for the method name.
870 /// The vector of `Ty`s are the ascripted type parameters for the method
871 /// (within the angle brackets).
873 /// The first element of the vector of `Expr`s is the expression that
874 /// evaluates to the object on which the method is being called on (the
875 /// receiver), and the remaining elements are the rest of the arguments.
877 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
878 /// `ExprMethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
879 ExprMethodCall(Spanned<Name>, HirVec<P<Ty>>, HirVec<P<Expr>>),
880 /// A tuple (`(a, b, c ,d)`)
881 ExprTup(HirVec<P<Expr>>),
882 /// A binary operation (For example: `a + b`, `a * b`)
883 ExprBinary(BinOp, P<Expr>, P<Expr>),
884 /// A unary operation (For example: `!x`, `*x`)
885 ExprUnary(UnOp, P<Expr>),
886 /// A literal (For example: `1`, `"foo"`)
888 /// A cast (`foo as f64`)
889 ExprCast(P<Expr>, P<Ty>),
890 ExprType(P<Expr>, P<Ty>),
891 /// An `if` block, with an optional else block
893 /// `if expr { block } else { expr }`
894 ExprIf(P<Expr>, P<Block>, Option<P<Expr>>),
895 /// A while loop, with an optional label
897 /// `'label: while expr { block }`
898 ExprWhile(P<Expr>, P<Block>, Option<Spanned<Name>>),
899 /// Conditionless loop (can be exited with break, continue, or return)
901 /// `'label: loop { block }`
902 ExprLoop(P<Block>, Option<Spanned<Name>>),
903 /// A `match` block, with a source that indicates whether or not it is
904 /// the result of a desugaring, and if so, which kind.
905 ExprMatch(P<Expr>, HirVec<Arm>, MatchSource),
906 /// A closure (for example, `move |a, b, c| {a + b + c}`).
908 /// The final span is the span of the argument block `|...|`
909 ExprClosure(CaptureClause, P<FnDecl>, P<Block>, Span),
910 /// A block (`{ ... }`)
913 /// An assignment (`a = foo()`)
914 ExprAssign(P<Expr>, P<Expr>),
915 /// An assignment with an operator
917 /// For example, `a += 1`.
918 ExprAssignOp(BinOp, P<Expr>, P<Expr>),
919 /// Access of a named struct field (`obj.foo`)
920 ExprField(P<Expr>, Spanned<Name>),
921 /// Access of an unnamed field of a struct or tuple-struct
923 /// For example, `foo.0`.
924 ExprTupField(P<Expr>, Spanned<usize>),
925 /// An indexing operation (`foo[2]`)
926 ExprIndex(P<Expr>, P<Expr>),
928 /// Variable reference, possibly containing `::` and/or type
929 /// parameters, e.g. foo::bar::<baz>.
931 /// Optionally "qualified",
932 /// e.g. `<HirVec<T> as SomeTrait>::SomeType`.
933 ExprPath(Option<QSelf>, Path),
935 /// A referencing operation (`&a` or `&mut a`)
936 ExprAddrOf(Mutability, P<Expr>),
937 /// A `break`, with an optional label to break
938 ExprBreak(Option<Spanned<Name>>),
939 /// A `continue`, with an optional label
940 ExprAgain(Option<Spanned<Name>>),
941 /// A `return`, with an optional value to be returned
942 ExprRet(Option<P<Expr>>),
944 /// Inline assembly (from `asm!`), with its outputs and inputs.
945 ExprInlineAsm(InlineAsm, Vec<P<Expr>>, Vec<P<Expr>>),
947 /// A struct or struct-like variant literal expression.
949 /// For example, `Foo {x: 1, y: 2}`, or
950 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
951 ExprStruct(Path, HirVec<Field>, Option<P<Expr>>),
953 /// An array literal constructed from one repeated element.
955 /// For example, `[1; 5]`. The first expression is the element
956 /// to be repeated; the second is the number of times to repeat it.
957 ExprRepeat(P<Expr>, P<Expr>),
960 /// The explicit Self type in a "qualified path". The actual
961 /// path, including the trait and the associated item, is stored
962 /// separately. `position` represents the index of the associated
963 /// item qualified with this Self type.
965 /// <HirVec<T> as a::b::Trait>::AssociatedItem
966 /// ^~~~~ ~~~~~~~~~~~~~~^
969 /// <HirVec<T>>::AssociatedItem
972 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
978 /// Hints at the original code for a `match _ { .. }`
979 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
980 pub enum MatchSource {
981 /// A `match _ { .. }`
983 /// An `if let _ = _ { .. }` (optionally with `else { .. }`)
985 contains_else_clause: bool,
987 /// A `while let _ = _ { .. }` (which was desugared to a
988 /// `loop { match _ { .. } }`)
990 /// A desugared `for _ in _ { .. }` loop
992 /// A desugared `?` operator
996 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
997 pub enum CaptureClause {
1002 // NB: If you change this, you'll probably want to change the corresponding
1003 // type structure in middle/ty.rs as well.
1004 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1007 pub mutbl: Mutability,
1010 /// Represents a method's signature in a trait declaration or implementation.
1011 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1012 pub struct MethodSig {
1013 pub unsafety: Unsafety,
1014 pub constness: Constness,
1016 pub decl: P<FnDecl>,
1017 pub generics: Generics,
1020 /// Represents an item declaration within a trait declaration,
1021 /// possibly including a default implementation. A trait item is
1022 /// either required (meaning it doesn't have an implementation, just a
1023 /// signature) or provided (meaning it has a default implementation).
1024 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1025 pub struct TraitItem {
1028 pub attrs: HirVec<Attribute>,
1029 pub node: TraitItem_,
1033 /// Represents a trait method or associated constant or type
1034 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1035 pub enum TraitItem_ {
1036 /// An associated constant with an optional value (otherwise `impl`s
1037 /// must contain a value)
1038 ConstTraitItem(P<Ty>, Option<P<Expr>>),
1039 /// A method with an optional body
1040 MethodTraitItem(MethodSig, Option<P<Block>>),
1041 /// An associated type with (possibly empty) bounds and optional concrete
1043 TypeTraitItem(TyParamBounds, Option<P<Ty>>),
1046 /// Represents anything within an `impl` block
1047 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1048 pub struct ImplItem {
1051 pub vis: Visibility,
1052 pub defaultness: Defaultness,
1053 pub attrs: HirVec<Attribute>,
1054 pub node: ImplItemKind,
1058 /// Represents different contents within `impl`s
1059 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1060 pub enum ImplItemKind {
1061 /// An associated constant of the given type, set to the constant result
1062 /// of the expression
1063 Const(P<Ty>, P<Expr>),
1064 /// A method implementation with the given signature and body
1065 Method(MethodSig, P<Block>),
1066 /// An associated type
1070 // Bind a type to an associated type: `A=Foo`.
1071 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1072 pub struct TypeBinding {
1080 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
1087 impl fmt::Debug for Ty {
1088 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1089 write!(f, "type({})", print::ty_to_string(self))
1093 /// Not represented directly in the AST, referred to by name through a ty_path.
1094 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1104 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1105 pub struct BareFnTy {
1106 pub unsafety: Unsafety,
1108 pub lifetimes: HirVec<LifetimeDef>,
1109 pub decl: P<FnDecl>,
1112 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1113 /// The different kinds of types recognized by the compiler
1116 /// A fixed length array (`[T; n]`)
1117 TyFixedLengthVec(P<Ty>, P<Expr>),
1118 /// A raw pointer (`*const T` or `*mut T`)
1120 /// A reference (`&'a T` or `&'a mut T`)
1121 TyRptr(Option<Lifetime>, MutTy),
1122 /// A bare function (e.g. `fn(usize) -> bool`)
1123 TyBareFn(P<BareFnTy>),
1124 /// A tuple (`(A, B, C, D,...)`)
1125 TyTup(HirVec<P<Ty>>),
1126 /// A path (`module::module::...::Type`), optionally
1127 /// "qualified", e.g. `<HirVec<T> as SomeTrait>::SomeType`.
1129 /// Type parameters are stored in the Path itself
1130 TyPath(Option<QSelf>, Path),
1131 /// Something like `A+B`. Note that `B` must always be a path.
1132 TyObjectSum(P<Ty>, TyParamBounds),
1133 /// A type like `for<'a> Foo<&'a Bar>`
1134 TyPolyTraitRef(TyParamBounds),
1137 /// TyInfer means the type should be inferred instead of it having been
1138 /// specified. This can appear anywhere in a type.
1142 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1143 pub struct InlineAsmOutput {
1144 pub constraint: InternedString,
1146 pub is_indirect: bool,
1149 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1150 pub struct InlineAsm {
1151 pub asm: InternedString,
1152 pub asm_str_style: StrStyle,
1153 pub outputs: HirVec<InlineAsmOutput>,
1154 pub inputs: HirVec<InternedString>,
1155 pub clobbers: HirVec<InternedString>,
1157 pub alignstack: bool,
1158 pub dialect: AsmDialect,
1159 pub expn_id: ExpnId,
1162 /// represents an argument in a function header
1163 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1170 /// Alternative representation for `Arg`s describing `self` parameter of methods.
1171 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1173 /// `self`, `mut self`
1175 /// `&'lt self`, `&'lt mut self`
1176 Region(Option<Lifetime>, Mutability),
1177 /// `self: TYPE`, `mut self: TYPE`
1178 Explicit(P<Ty>, Mutability),
1181 pub type ExplicitSelf = Spanned<SelfKind>;
1184 pub fn to_self(&self) -> Option<ExplicitSelf> {
1185 if let PatKind::Binding(BindByValue(mutbl), name, _) = self.pat.node {
1186 if name.node == keywords::SelfValue.name() {
1187 return match self.ty.node {
1188 TyInfer => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
1189 TyRptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyInfer => {
1190 Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
1192 _ => Some(respan(mk_sp(self.pat.span.lo, self.ty.span.hi),
1193 SelfKind::Explicit(self.ty.clone(), mutbl)))
1200 pub fn is_self(&self) -> bool {
1201 if let PatKind::Binding(_, name, _) = self.pat.node {
1202 name.node == keywords::SelfValue.name()
1209 /// Represents the header (not the body) of a function declaration
1210 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1212 pub inputs: HirVec<Arg>,
1213 pub output: FunctionRetTy,
1218 pub fn get_self(&self) -> Option<ExplicitSelf> {
1219 self.inputs.get(0).and_then(Arg::to_self)
1221 pub fn has_self(&self) -> bool {
1222 self.inputs.get(0).map(Arg::is_self).unwrap_or(false)
1226 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1232 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1233 pub enum Constness {
1238 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1239 pub enum Defaultness {
1245 pub fn is_final(&self) -> bool {
1246 *self == Defaultness::Final
1249 pub fn is_default(&self) -> bool {
1250 *self == Defaultness::Default
1254 impl fmt::Display for Unsafety {
1255 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1256 fmt::Display::fmt(match *self {
1257 Unsafety::Normal => "normal",
1258 Unsafety::Unsafe => "unsafe",
1264 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
1265 pub enum ImplPolarity {
1266 /// `impl Trait for Type`
1268 /// `impl !Trait for Type`
1272 impl fmt::Debug for ImplPolarity {
1273 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1275 ImplPolarity::Positive => "positive".fmt(f),
1276 ImplPolarity::Negative => "negative".fmt(f),
1282 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1283 pub enum FunctionRetTy {
1284 /// Functions with return type `!`that always
1285 /// raise an error or exit (i.e. never return to the caller)
1287 /// Return type is not specified.
1289 /// Functions default to `()` and
1290 /// closures default to inference. Span points to where return
1291 /// type would be inserted.
1292 DefaultReturn(Span),
1297 impl FunctionRetTy {
1298 pub fn span(&self) -> Span {
1300 NoReturn(span) => span,
1301 DefaultReturn(span) => span,
1302 Return(ref ty) => ty.span,
1307 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1309 /// A span from the first token past `{` to the last token until `}`.
1310 /// For `mod foo;`, the inner span ranges from the first token
1311 /// to the last token in the external file.
1313 pub item_ids: HirVec<ItemId>,
1316 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1317 pub struct ForeignMod {
1319 pub items: HirVec<ForeignItem>,
1322 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1323 pub struct EnumDef {
1324 pub variants: HirVec<Variant>,
1327 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1328 pub struct Variant_ {
1330 pub attrs: HirVec<Attribute>,
1331 pub data: VariantData,
1332 /// Explicit discriminant, eg `Foo = 1`
1333 pub disr_expr: Option<P<Expr>>,
1336 pub type Variant = Spanned<Variant_>;
1338 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1339 pub enum PathListItem_ {
1342 /// renamed in list, eg `use foo::{bar as baz};`
1343 rename: Option<Name>,
1347 /// renamed in list, eg `use foo::{self as baz};`
1348 rename: Option<Name>,
1353 impl PathListItem_ {
1354 pub fn id(&self) -> NodeId {
1356 PathListIdent { id, .. } | PathListMod { id, .. } => id,
1360 pub fn name(&self) -> Option<Name> {
1362 PathListIdent { name, .. } => Some(name),
1363 PathListMod { .. } => None,
1367 pub fn rename(&self) -> Option<Name> {
1369 PathListIdent { rename, .. } | PathListMod { rename, .. } => rename,
1374 pub type PathListItem = Spanned<PathListItem_>;
1376 pub type ViewPath = Spanned<ViewPath_>;
1378 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1379 pub enum ViewPath_ {
1380 /// `foo::bar::baz as quux`
1384 /// `foo::bar::baz` (with `as baz` implicitly on the right)
1385 ViewPathSimple(Name, Path),
1390 /// `foo::bar::{a,b,c}`
1391 ViewPathList(Path, HirVec<PathListItem>),
1394 /// TraitRef's appear in impls.
1396 /// resolve maps each TraitRef's ref_id to its defining trait; that's all
1397 /// that the ref_id is for. Note that ref_id's value is not the NodeId of the
1398 /// trait being referred to but just a unique NodeId that serves as a key
1399 /// within the DefMap.
1400 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1401 pub struct TraitRef {
1406 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1407 pub struct PolyTraitRef {
1408 /// The `'a` in `<'a> Foo<&'a T>`
1409 pub bound_lifetimes: HirVec<LifetimeDef>,
1411 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
1412 pub trait_ref: TraitRef,
1417 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1418 pub enum Visibility {
1421 Restricted { path: P<Path>, id: NodeId },
1425 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1426 pub struct StructField {
1429 pub vis: Visibility,
1432 pub attrs: HirVec<Attribute>,
1436 // Still necessary in couple of places
1437 pub fn is_positional(&self) -> bool {
1438 let first = self.name.as_str().as_bytes()[0];
1439 first >= b'0' && first <= b'9'
1443 /// Fields and Ids of enum variants and structs
1445 /// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all
1446 /// variant kinds) and an Id of the variant's constructor (not relevant for `Struct`-variants).
1447 /// One shared Id can be successfully used for these two purposes.
1448 /// Id of the whole enum lives in `Item`.
1450 /// For structs: `NodeId` represents an Id of the structure's constructor, so it is not actually
1451 /// used for `Struct`-structs (but still presents). Structures don't have an analogue of "Id of
1452 /// the variant itself" from enum variants.
1453 /// Id of the whole struct lives in `Item`.
1454 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1455 pub enum VariantData {
1456 Struct(HirVec<StructField>, NodeId),
1457 Tuple(HirVec<StructField>, NodeId),
1462 pub fn fields(&self) -> &[StructField] {
1464 VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => fields,
1468 pub fn id(&self) -> NodeId {
1470 VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id,
1473 pub fn is_struct(&self) -> bool {
1474 if let VariantData::Struct(..) = *self {
1480 pub fn is_tuple(&self) -> bool {
1481 if let VariantData::Tuple(..) = *self {
1487 pub fn is_unit(&self) -> bool {
1488 if let VariantData::Unit(..) = *self {
1496 // The bodies for items are stored "out of line", in a separate
1497 // hashmap in the `Crate`. Here we just record the node-id of the item
1498 // so it can fetched later.
1499 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1504 // FIXME (#3300): Should allow items to be anonymous. Right now
1505 // we just use dummy names for anon items.
1508 /// The name might be a dummy name in case of anonymous items
1509 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1512 pub attrs: HirVec<Attribute>,
1515 pub vis: Visibility,
1519 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1521 /// An`extern crate` item, with optional original crate name,
1523 /// e.g. `extern crate foo` or `extern crate foo_bar as foo`
1524 ItemExternCrate(Option<Name>),
1525 /// A `use` or `pub use` item
1526 ItemUse(P<ViewPath>),
1529 ItemStatic(P<Ty>, Mutability, P<Expr>),
1531 ItemConst(P<Ty>, P<Expr>),
1532 /// A function declaration
1533 ItemFn(P<FnDecl>, Unsafety, Constness, Abi, Generics, P<Block>),
1536 /// An external module
1537 ItemForeignMod(ForeignMod),
1538 /// A type alias, e.g. `type Foo = Bar<u8>`
1539 ItemTy(P<Ty>, Generics),
1540 /// An enum definition, e.g. `enum Foo<A, B> {C<A>, D<B>}`
1541 ItemEnum(EnumDef, Generics),
1542 /// A struct definition, e.g. `struct Foo<A> {x: A}`
1543 ItemStruct(VariantData, Generics),
1544 /// Represents a Trait Declaration
1545 ItemTrait(Unsafety, Generics, TyParamBounds, HirVec<TraitItem>),
1547 // Default trait implementations
1549 /// `impl Trait for .. {}`
1550 ItemDefaultImpl(Unsafety, TraitRef),
1551 /// An implementation, eg `impl<A> Trait for Foo { .. }`
1555 Option<TraitRef>, // (optional) trait this impl implements
1561 pub fn descriptive_variant(&self) -> &str {
1563 ItemExternCrate(..) => "extern crate",
1564 ItemUse(..) => "use",
1565 ItemStatic(..) => "static item",
1566 ItemConst(..) => "constant item",
1567 ItemFn(..) => "function",
1568 ItemMod(..) => "module",
1569 ItemForeignMod(..) => "foreign module",
1570 ItemTy(..) => "type alias",
1571 ItemEnum(..) => "enum",
1572 ItemStruct(..) => "struct",
1573 ItemTrait(..) => "trait",
1575 ItemDefaultImpl(..) => "item",
1580 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1581 pub struct ForeignItem {
1583 pub attrs: HirVec<Attribute>,
1584 pub node: ForeignItem_,
1587 pub vis: Visibility,
1590 /// An item within an `extern` block
1591 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1592 pub enum ForeignItem_ {
1593 /// A foreign function
1594 ForeignItemFn(P<FnDecl>, Generics),
1595 /// A foreign static item (`static ext: u8`), with optional mutability
1596 /// (the boolean is true when mutable)
1597 ForeignItemStatic(P<Ty>, bool),
1601 pub fn descriptive_variant(&self) -> &str {
1603 ForeignItemFn(..) => "foreign function",
1604 ForeignItemStatic(..) => "foreign static item",
1609 /// A free variable referred to in a function.
1610 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
1611 pub struct Freevar {
1612 /// The variable being accessed free.
1615 // First span where it is accessed (there can be multiple).
1619 pub type FreevarMap = NodeMap<Vec<Freevar>>;
1621 pub type CaptureModeMap = NodeMap<CaptureClause>;
1624 pub struct TraitCandidate {
1626 pub import_id: Option<NodeId>,
1629 // Trait method resolution
1630 pub type TraitMap = NodeMap<Vec<TraitCandidate>>;
1632 // Map from the NodeId of a glob import to a list of items which are actually
1634 pub type GlobMap = NodeMap<FnvHashSet<Name>>;