1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // The Rust abstract syntax tree.
13 pub use self::BindingMode::*;
14 pub use self::BinOp_::*;
15 pub use self::BlockCheckMode::*;
16 pub use self::CaptureClause::*;
17 pub use self::Decl_::*;
18 pub use self::ExplicitSelf_::*;
19 pub use self::Expr_::*;
20 pub use self::FloatTy::*;
21 pub use self::FunctionRetTy::*;
22 pub use self::ForeignItem_::*;
23 pub use self::IntTy::*;
24 pub use self::Item_::*;
25 pub use self::KleeneOp::*;
26 pub use self::Lit_::*;
27 pub use self::LitIntType::*;
28 pub use self::MacStmtStyle::*;
29 pub use self::MetaItem_::*;
30 pub use self::Mutability::*;
31 pub use self::Pat_::*;
32 pub use self::PathListItem_::*;
33 pub use self::PrimTy::*;
34 pub use self::Sign::*;
35 pub use self::Stmt_::*;
36 pub use self::StrStyle::*;
37 pub use self::StructFieldKind::*;
38 pub use self::TraitItem_::*;
40 pub use self::TyParamBound::*;
41 pub use self::UintTy::*;
42 pub use self::UnOp::*;
43 pub use self::UnsafeSource::*;
44 pub use self::ViewPath_::*;
45 pub use self::Visibility::*;
46 pub use self::PathParameters::*;
48 use attr::ThinAttributes;
49 use codemap::{Span, Spanned, DUMMY_SP, ExpnId};
52 use ext::tt::macro_parser;
53 use owned_slice::OwnedSlice;
54 use parse::token::{InternedString, str_to_ident};
57 use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
64 use std::hash::{Hash, Hasher};
65 use serialize::{Encodable, Decodable, Encoder, Decoder};
67 /// A name is a part of an identifier, representing a string or gensym. It's
68 /// the result of interning.
69 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
70 pub struct Name(pub u32);
72 /// A SyntaxContext represents a chain of macro-expandings
73 /// and renamings. Each macro expansion corresponds to
74 /// a fresh u32. This u32 is a reference to a table stored
75 // in thread-local storage.
76 // The special value EMPTY_CTXT is used to indicate an empty
78 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
79 pub struct SyntaxContext(pub u32);
81 /// An identifier contains a Name (index into the interner
82 /// table) and a SyntaxContext to track renaming and
83 /// macro expansion per Flatt et al., "Macros That Work Together"
84 #[derive(Clone, Copy, Eq)]
87 pub ctxt: SyntaxContext
91 pub fn as_str(self) -> token::InternedString {
92 token::InternedString::new_from_name(self)
96 impl fmt::Debug for Name {
97 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98 write!(f, "{}({})", self, self.0)
102 impl fmt::Display for Name {
103 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
104 fmt::Display::fmt(&self.as_str(), f)
108 impl Encodable for Name {
109 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
110 s.emit_str(&self.as_str())
114 impl Decodable for Name {
115 fn decode<D: Decoder>(d: &mut D) -> Result<Name, D::Error> {
116 Ok(token::intern(&try!(d.read_str())[..]))
120 pub const EMPTY_CTXT : SyntaxContext = SyntaxContext(0);
123 pub fn new(name: Name, ctxt: SyntaxContext) -> Ident {
124 Ident {name: name, ctxt: ctxt}
126 pub fn with_empty_ctxt(name: Name) -> Ident {
127 Ident {name: name, ctxt: EMPTY_CTXT}
131 impl PartialEq for Ident {
132 fn eq(&self, other: &Ident) -> bool {
133 if self.ctxt != other.ctxt {
134 // There's no one true way to compare Idents. They can be compared
135 // non-hygienically `id1.name == id2.name`, hygienically
136 // `mtwt::resolve(id1) == mtwt::resolve(id2)`, or even member-wise
137 // `(id1.name, id1.ctxt) == (id2.name, id2.ctxt)` depending on the situation.
138 // Ideally, PartialEq should not be implemented for Ident at all, but that
139 // would be too impractical, because many larger structures (Token, in particular)
140 // including Idents as their parts derive PartialEq and use it for non-hygienic
141 // comparisons. That's why PartialEq is implemented and defaults to non-hygienic
142 // comparison. Hash is implemented too and is consistent with PartialEq, i.e. only
143 // the name of Ident is hashed. Still try to avoid comparing idents in your code
144 // (especially as keys in hash maps), use one of the three methods listed above
147 // If you see this panic, then some idents from different contexts were compared
148 // non-hygienically. It's likely a bug. Use one of the three comparison methods
149 // listed above explicitly.
151 panic!("idents with different contexts are compared with operator `==`: \
152 {:?}, {:?}.", self, other);
155 self.name == other.name
159 impl Hash for Ident {
160 fn hash<H: Hasher>(&self, state: &mut H) {
161 self.name.hash(state)
165 impl fmt::Debug for Ident {
166 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
167 write!(f, "{}#{}", self.name, self.ctxt.0)
171 impl fmt::Display for Ident {
172 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
173 fmt::Display::fmt(&self.name, f)
177 impl Encodable for Ident {
178 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
183 impl Decodable for Ident {
184 fn decode<D: Decoder>(d: &mut D) -> Result<Ident, D::Error> {
185 Ok(Ident::with_empty_ctxt(try!(Name::decode(d))))
189 /// A mark represents a unique id associated with a macro expansion
192 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
193 pub struct Lifetime {
199 impl fmt::Debug for Lifetime {
200 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
201 write!(f, "lifetime({}: {})", self.id, pprust::lifetime_to_string(self))
205 /// A lifetime definition, eg `'a: 'b+'c+'d`
206 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
207 pub struct LifetimeDef {
208 pub lifetime: Lifetime,
209 pub bounds: Vec<Lifetime>
212 /// A "Path" is essentially Rust's notion of a name; for instance:
213 /// std::cmp::PartialEq . It's represented as a sequence of identifiers,
214 /// along with a bunch of supporting information.
215 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
218 /// A `::foo` path, is relative to the crate root rather than current
219 /// module (like paths in an import).
221 /// The segments in the path: the things separated by `::`.
222 pub segments: Vec<PathSegment>,
225 impl fmt::Debug for Path {
226 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
227 write!(f, "path({})", pprust::path_to_string(self))
231 impl fmt::Display for Path {
232 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
233 write!(f, "{}", pprust::path_to_string(self))
237 /// A segment of a path: an identifier, an optional lifetime, and a set of
239 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
240 pub struct PathSegment {
241 /// The identifier portion of this path segment.
242 pub identifier: Ident,
244 /// Type/lifetime parameters attached to this path. They come in
245 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
246 /// this is more than just simple syntactic sugar; the use of
247 /// parens affects the region binding rules, so we preserve the
249 pub parameters: PathParameters,
252 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
253 pub enum PathParameters {
254 /// The `<'a, A,B,C>` in `foo::bar::baz::<'a, A,B,C>`
255 AngleBracketedParameters(AngleBracketedParameterData),
256 /// The `(A,B)` and `C` in `Foo(A,B) -> C`
257 ParenthesizedParameters(ParenthesizedParameterData),
260 impl PathParameters {
261 pub fn none() -> PathParameters {
262 AngleBracketedParameters(AngleBracketedParameterData {
263 lifetimes: Vec::new(),
264 types: OwnedSlice::empty(),
265 bindings: OwnedSlice::empty(),
269 pub fn is_empty(&self) -> bool {
271 AngleBracketedParameters(ref data) => data.is_empty(),
273 // Even if the user supplied no types, something like
274 // `X()` is equivalent to `X<(),()>`.
275 ParenthesizedParameters(..) => false,
279 pub fn has_lifetimes(&self) -> bool {
281 AngleBracketedParameters(ref data) => !data.lifetimes.is_empty(),
282 ParenthesizedParameters(_) => false,
286 pub fn has_types(&self) -> bool {
288 AngleBracketedParameters(ref data) => !data.types.is_empty(),
289 ParenthesizedParameters(..) => true,
293 /// Returns the types that the user wrote. Note that these do not necessarily map to the type
294 /// parameters in the parenthesized case.
295 pub fn types(&self) -> Vec<&P<Ty>> {
297 AngleBracketedParameters(ref data) => {
298 data.types.iter().collect()
300 ParenthesizedParameters(ref data) => {
302 .chain(data.output.iter())
308 pub fn lifetimes(&self) -> Vec<&Lifetime> {
310 AngleBracketedParameters(ref data) => {
311 data.lifetimes.iter().collect()
313 ParenthesizedParameters(_) => {
319 pub fn bindings(&self) -> Vec<&P<TypeBinding>> {
321 AngleBracketedParameters(ref data) => {
322 data.bindings.iter().collect()
324 ParenthesizedParameters(_) => {
331 /// A path like `Foo<'a, T>`
332 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
333 pub struct AngleBracketedParameterData {
334 /// The lifetime parameters for this path segment.
335 pub lifetimes: Vec<Lifetime>,
336 /// The type parameters for this path segment, if present.
337 pub types: OwnedSlice<P<Ty>>,
338 /// Bindings (equality constraints) on associated types, if present.
339 /// E.g., `Foo<A=Bar>`.
340 pub bindings: OwnedSlice<P<TypeBinding>>,
343 impl AngleBracketedParameterData {
344 fn is_empty(&self) -> bool {
345 self.lifetimes.is_empty() && self.types.is_empty() && self.bindings.is_empty()
349 /// A path like `Foo(A,B) -> C`
350 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
351 pub struct ParenthesizedParameterData {
356 pub inputs: Vec<P<Ty>>,
359 pub output: Option<P<Ty>>,
362 pub type CrateNum = u32;
364 pub type NodeId = u32;
366 /// Node id used to represent the root of the crate.
367 pub const CRATE_NODE_ID: NodeId = 0;
369 /// When parsing and doing expansions, we initially give all AST nodes this AST
370 /// node value. Then later, in the renumber pass, we renumber them to have
371 /// small, positive ids.
372 pub const DUMMY_NODE_ID: NodeId = !0;
374 pub trait NodeIdAssigner {
375 fn next_node_id(&self) -> NodeId;
376 fn peek_node_id(&self) -> NodeId;
379 /// The AST represents all type param bounds as types.
380 /// typeck::collect::compute_bounds matches these against
381 /// the "special" built-in traits (see middle::lang_items) and
382 /// detects Copy, Send and Sync.
383 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
384 pub enum TyParamBound {
385 TraitTyParamBound(PolyTraitRef, TraitBoundModifier),
386 RegionTyParamBound(Lifetime)
389 /// A modifier on a bound, currently this is only used for `?Sized`, where the
390 /// modifier is `Maybe`. Negative bounds should also be handled here.
391 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
392 pub enum TraitBoundModifier {
397 pub type TyParamBounds = OwnedSlice<TyParamBound>;
399 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
403 pub bounds: TyParamBounds,
404 pub default: Option<P<Ty>>,
408 /// Represents lifetimes and type parameters attached to a declaration
409 /// of a function, enum, trait, etc.
410 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
411 pub struct Generics {
412 pub lifetimes: Vec<LifetimeDef>,
413 pub ty_params: OwnedSlice<TyParam>,
414 pub where_clause: WhereClause,
418 pub fn is_lt_parameterized(&self) -> bool {
419 !self.lifetimes.is_empty()
421 pub fn is_type_parameterized(&self) -> bool {
422 !self.ty_params.is_empty()
424 pub fn is_parameterized(&self) -> bool {
425 self.is_lt_parameterized() || self.is_type_parameterized()
429 impl Default for Generics {
430 fn default() -> Generics {
432 lifetimes: Vec::new(),
433 ty_params: OwnedSlice::empty(),
434 where_clause: WhereClause {
436 predicates: Vec::new(),
442 /// A `where` clause in a definition
443 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
444 pub struct WhereClause {
446 pub predicates: Vec<WherePredicate>,
449 /// A single predicate in a `where` clause
450 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
451 pub enum WherePredicate {
452 /// A type binding, eg `for<'c> Foo: Send+Clone+'c`
453 BoundPredicate(WhereBoundPredicate),
454 /// A lifetime predicate, e.g. `'a: 'b+'c`
455 RegionPredicate(WhereRegionPredicate),
456 /// An equality predicate (unsupported)
457 EqPredicate(WhereEqPredicate),
460 /// A type bound, eg `for<'c> Foo: Send+Clone+'c`
461 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
462 pub struct WhereBoundPredicate {
464 /// Any lifetimes from a `for` binding
465 pub bound_lifetimes: Vec<LifetimeDef>,
466 /// The type being bounded
467 pub bounded_ty: P<Ty>,
468 /// Trait and lifetime bounds (`Clone+Send+'static`)
469 pub bounds: OwnedSlice<TyParamBound>,
472 /// A lifetime predicate, e.g. `'a: 'b+'c`
473 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
474 pub struct WhereRegionPredicate {
476 pub lifetime: Lifetime,
477 pub bounds: Vec<Lifetime>,
480 /// An equality predicate (unsupported), e.g. `T=int`
481 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
482 pub struct WhereEqPredicate {
489 /// The set of MetaItems that define the compilation environment of the crate,
490 /// used to drive conditional compilation
491 pub type CrateConfig = Vec<P<MetaItem>> ;
493 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
496 pub attrs: Vec<Attribute>,
497 pub config: CrateConfig,
499 pub exported_macros: Vec<MacroDef>,
502 pub type MetaItem = Spanned<MetaItem_>;
504 #[derive(Clone, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
506 MetaWord(InternedString),
507 MetaList(InternedString, Vec<P<MetaItem>>),
508 MetaNameValue(InternedString, Lit),
511 // can't be derived because the MetaList requires an unordered comparison
512 impl PartialEq for MetaItem_ {
513 fn eq(&self, other: &MetaItem_) -> bool {
515 MetaWord(ref ns) => match *other {
516 MetaWord(ref no) => (*ns) == (*no),
519 MetaNameValue(ref ns, ref vs) => match *other {
520 MetaNameValue(ref no, ref vo) => {
521 (*ns) == (*no) && vs.node == vo.node
525 MetaList(ref ns, ref miss) => match *other {
526 MetaList(ref no, ref miso) => {
528 miss.iter().all(|mi| miso.iter().any(|x| x.node == mi.node))
536 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
538 /// Statements in a block
539 pub stmts: Vec<P<Stmt>>,
540 /// An expression at the end of the block
541 /// without a semicolon, if any
542 pub expr: Option<P<Expr>>,
544 /// Distinguishes between `unsafe { ... }` and `{ ... }`
545 pub rules: BlockCheckMode,
549 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
556 impl fmt::Debug for Pat {
557 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
558 write!(f, "pat({}: {})", self.id, pprust::pat_to_string(self))
562 /// A single field in a struct pattern
564 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
565 /// are treated the same as` x: x, y: ref y, z: ref mut z`,
566 /// except is_shorthand is true
567 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
568 pub struct FieldPat {
569 /// The identifier for the field
571 /// The pattern the field is destructured to
573 pub is_shorthand: bool,
576 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
577 pub enum BindingMode {
578 BindByRef(Mutability),
579 BindByValue(Mutability),
582 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
584 /// Represents a wildcard pattern (`_`)
587 /// A PatIdent may either be a new bound variable,
588 /// or a nullary enum (in which case the third field
591 /// In the nullary enum case, the parser can't determine
592 /// which it is. The resolver determines this, and
593 /// records this pattern's NodeId in an auxiliary
594 /// set (of "PatIdents that refer to nullary enums")
595 PatIdent(BindingMode, SpannedIdent, Option<P<Pat>>),
597 /// "None" means a `Variant(..)` pattern where we don't bind the fields to names.
598 PatEnum(Path, Option<Vec<P<Pat>>>),
600 /// An associated const named using the qualified path `<T>::CONST` or
601 /// `<T as Trait>::CONST`. Associated consts from inherent impls can be
602 /// referred to as simply `T::CONST`, in which case they will end up as
603 /// PatEnum, and the resolver will have to sort that out.
604 PatQPath(QSelf, Path),
606 /// Destructuring of a struct, e.g. `Foo {x, y, ..}`
607 /// The `bool` is `true` in the presence of a `..`
608 PatStruct(Path, Vec<Spanned<FieldPat>>, bool),
609 /// A tuple pattern `(a, b)`
613 /// A reference pattern, e.g. `&mut (a, b)`
614 PatRegion(P<Pat>, Mutability),
617 /// A range pattern, e.g. `1...2`
618 PatRange(P<Expr>, P<Expr>),
619 /// `[a, b, ..i, y, z]` is represented as:
620 /// `PatVec(box [a, b], Some(i), box [y, z])`
621 PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
622 /// A macro pattern; pre-expansion
626 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
627 pub enum Mutability {
632 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
634 /// The `+` operator (addition)
636 /// The `-` operator (subtraction)
638 /// The `*` operator (multiplication)
640 /// The `/` operator (division)
642 /// The `%` operator (modulus)
644 /// The `&&` operator (logical and)
646 /// The `||` operator (logical or)
648 /// The `^` operator (bitwise xor)
650 /// The `&` operator (bitwise and)
652 /// The `|` operator (bitwise or)
654 /// The `<<` operator (shift left)
656 /// The `>>` operator (shift right)
658 /// The `==` operator (equality)
660 /// The `<` operator (less than)
662 /// The `<=` operator (less than or equal to)
664 /// The `!=` operator (not equal to)
666 /// The `>=` operator (greater than or equal to)
668 /// The `>` operator (greater than)
673 pub fn to_string(&self) -> &'static str {
695 pub fn lazy(&self) -> bool {
697 BiAnd | BiOr => true,
702 pub fn is_shift(&self) -> bool {
704 BiShl | BiShr => true,
708 pub fn is_comparison(&self) -> bool {
710 BiEq | BiLt | BiLe | BiNe | BiGt | BiGe =>
712 BiAnd | BiOr | BiAdd | BiSub | BiMul | BiDiv | BiRem |
713 BiBitXor | BiBitAnd | BiBitOr | BiShl | BiShr =>
717 /// Returns `true` if the binary operator takes its arguments by value
718 pub fn is_by_value(&self) -> bool {
719 !BinOp_::is_comparison(self)
723 pub type BinOp = Spanned<BinOp_>;
725 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
727 /// The `*` operator for dereferencing
729 /// The `!` operator for logical inversion
731 /// The `-` operator for negation
736 /// Returns `true` if the unary operator takes its argument by value
737 pub fn is_by_value(u: UnOp) -> bool {
739 UnNeg | UnNot => true,
744 pub fn to_string(op: UnOp) -> &'static str {
754 pub type Stmt = Spanned<Stmt_>;
756 impl fmt::Debug for Stmt {
757 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
758 write!(f, "stmt({}: {})",
760 .map_or(Cow::Borrowed("<macro>"),|id|Cow::Owned(id.to_string())),
761 pprust::stmt_to_string(self))
766 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
768 /// Could be an item or a local (let) binding:
769 StmtDecl(P<Decl>, NodeId),
771 /// Expr without trailing semi-colon (must have unit type):
772 StmtExpr(P<Expr>, NodeId),
774 /// Expr with trailing semi-colon (may have any type):
775 StmtSemi(P<Expr>, NodeId),
777 StmtMac(P<Mac>, MacStmtStyle, ThinAttributes),
781 pub fn id(&self) -> Option<NodeId> {
783 StmtDecl(_, id) => Some(id),
784 StmtExpr(_, id) => Some(id),
785 StmtSemi(_, id) => Some(id),
790 pub fn attrs(&self) -> &[Attribute] {
792 StmtDecl(ref d, _) => d.attrs(),
794 StmtSemi(ref e, _) => e.attrs(),
795 StmtMac(_, _, Some(ref b)) => b,
796 StmtMac(_, _, None) => &[],
801 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
802 pub enum MacStmtStyle {
803 /// The macro statement had a trailing semicolon, e.g. `foo! { ... };`
804 /// `foo!(...);`, `foo![...];`
805 MacStmtWithSemicolon,
806 /// The macro statement had braces; e.g. foo! { ... }
808 /// The macro statement had parentheses or brackets and no semicolon; e.g.
809 /// `foo!(...)`. All of these will end up being converted into macro
811 MacStmtWithoutBraces,
814 // FIXME (pending discussion of #1697, #2178...): local should really be
815 // a refinement on pat.
816 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
817 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
820 pub ty: Option<P<Ty>>,
821 /// Initializer expression to set the value, if any
822 pub init: Option<P<Expr>>,
825 pub attrs: ThinAttributes,
829 pub fn attrs(&self) -> &[Attribute] {
837 pub type Decl = Spanned<Decl_>;
839 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
841 /// A local (let) binding:
848 pub fn attrs(&self) -> &[Attribute] {
850 DeclLocal(ref l) => l.attrs(),
851 DeclItem(ref i) => i.attrs(),
856 /// represents one arm of a 'match'
857 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
859 pub attrs: Vec<Attribute>,
860 pub pats: Vec<P<Pat>>,
861 pub guard: Option<P<Expr>>,
865 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
867 pub ident: SpannedIdent,
872 pub type SpannedIdent = Spanned<Ident>;
874 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
875 pub enum BlockCheckMode {
877 UnsafeBlock(UnsafeSource),
880 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
881 pub enum UnsafeSource {
887 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash,)]
892 pub attrs: ThinAttributes
896 pub fn attrs(&self) -> &[Attribute] {
904 impl fmt::Debug for Expr {
905 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
906 write!(f, "expr({}: {})", self.id, pprust::expr_to_string(self))
910 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
912 /// A `box x` expression.
914 /// First expr is the place; second expr is the value.
915 ExprInPlace(P<Expr>, P<Expr>),
916 /// An array (`[a, b, c, d]`)
917 ExprVec(Vec<P<Expr>>),
920 /// The first field resolves to the function itself,
921 /// and the second field is the list of arguments
922 ExprCall(P<Expr>, Vec<P<Expr>>),
923 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
925 /// The `SpannedIdent` is the identifier for the method name.
926 /// The vector of `Ty`s are the ascripted type parameters for the method
927 /// (within the angle brackets).
929 /// The first element of the vector of `Expr`s is the expression that evaluates
930 /// to the object on which the method is being called on (the receiver),
931 /// and the remaining elements are the rest of the arguments.
933 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
934 /// `ExprMethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
935 ExprMethodCall(SpannedIdent, Vec<P<Ty>>, Vec<P<Expr>>),
936 /// A tuple (`(a, b, c ,d)`)
937 ExprTup(Vec<P<Expr>>),
938 /// A binary operation (For example: `a + b`, `a * b`)
939 ExprBinary(BinOp, P<Expr>, P<Expr>),
940 /// A unary operation (For example: `!x`, `*x`)
941 ExprUnary(UnOp, P<Expr>),
942 /// A literal (For example: `1u8`, `"foo"`)
944 /// A cast (`foo as f64`)
945 ExprCast(P<Expr>, P<Ty>),
946 /// An `if` block, with an optional else block
948 /// `if expr { block } else { expr }`
949 ExprIf(P<Expr>, P<Block>, Option<P<Expr>>),
950 /// An `if let` expression with an optional else block
952 /// `if let pat = expr { block } else { expr }`
954 /// This is desugared to a `match` expression.
955 ExprIfLet(P<Pat>, P<Expr>, P<Block>, Option<P<Expr>>),
956 /// A while loop, with an optional label
958 /// `'label: while expr { block }`
959 ExprWhile(P<Expr>, P<Block>, Option<Ident>),
960 /// A while-let loop, with an optional label
962 /// `'label: while let pat = expr { block }`
964 /// This is desugared to a combination of `loop` and `match` expressions.
965 ExprWhileLet(P<Pat>, P<Expr>, P<Block>, Option<Ident>),
966 /// A for loop, with an optional label
968 /// `'label: for pat in expr { block }`
970 /// This is desugared to a combination of `loop` and `match` expressions.
971 ExprForLoop(P<Pat>, P<Expr>, P<Block>, Option<Ident>),
972 /// Conditionless loop (can be exited with break, continue, or return)
974 /// `'label: loop { block }`
975 ExprLoop(P<Block>, Option<Ident>),
977 ExprMatch(P<Expr>, Vec<Arm>),
978 /// A closure (for example, `move |a, b, c| {a + b + c}`)
979 ExprClosure(CaptureClause, P<FnDecl>, P<Block>),
980 /// A block (`{ ... }`)
983 /// An assignment (`a = foo()`)
984 ExprAssign(P<Expr>, P<Expr>),
985 /// An assignment with an operator
987 /// For example, `a += 1`.
988 ExprAssignOp(BinOp, P<Expr>, P<Expr>),
989 /// Access of a named struct field (`obj.foo`)
990 ExprField(P<Expr>, SpannedIdent),
991 /// Access of an unnamed field of a struct or tuple-struct
993 /// For example, `foo.0`.
994 ExprTupField(P<Expr>, Spanned<usize>),
995 /// An indexing operation (`foo[2]`)
996 ExprIndex(P<Expr>, P<Expr>),
997 /// A range (`1..2`, `1..`, or `..2`)
998 ExprRange(Option<P<Expr>>, Option<P<Expr>>),
1000 /// Variable reference, possibly containing `::` and/or type
1001 /// parameters, e.g. foo::bar::<baz>.
1003 /// Optionally "qualified",
1004 /// e.g. `<Vec<T> as SomeTrait>::SomeType`.
1005 ExprPath(Option<QSelf>, Path),
1007 /// A referencing operation (`&a` or `&mut a`)
1008 ExprAddrOf(Mutability, P<Expr>),
1009 /// A `break`, with an optional label to break
1010 ExprBreak(Option<SpannedIdent>),
1011 /// A `continue`, with an optional label
1012 ExprAgain(Option<SpannedIdent>),
1013 /// A `return`, with an optional value to be returned
1014 ExprRet(Option<P<Expr>>),
1016 /// Output of the `asm!()` macro
1017 ExprInlineAsm(InlineAsm),
1019 /// A macro invocation; pre-expansion
1022 /// A struct literal expression.
1024 /// For example, `Foo {x: 1, y: 2}`, or
1025 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
1026 ExprStruct(Path, Vec<Field>, Option<P<Expr>>),
1028 /// An array literal constructed from one repeated element.
1030 /// For example, `[1u8; 5]`. The first expression is the element
1031 /// to be repeated; the second is the number of times to repeat it.
1032 ExprRepeat(P<Expr>, P<Expr>),
1034 /// No-op: used solely so we can pretty-print faithfully
1038 /// The explicit Self type in a "qualified path". The actual
1039 /// path, including the trait and the associated item, is stored
1040 /// separately. `position` represents the index of the associated
1041 /// item qualified with this Self type.
1044 /// <Vec<T> as a::b::Trait>::AssociatedItem
1045 /// ^~~~~ ~~~~~~~~~~~~~~^
1048 /// <Vec<T>>::AssociatedItem
1052 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1058 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1059 pub enum CaptureClause {
1064 /// A delimited sequence of token trees
1065 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1066 pub struct Delimited {
1067 /// The type of delimiter
1068 pub delim: token::DelimToken,
1069 /// The span covering the opening delimiter
1070 pub open_span: Span,
1071 /// The delimited sequence of token trees
1072 pub tts: Vec<TokenTree>,
1073 /// The span covering the closing delimiter
1074 pub close_span: Span,
1078 /// Returns the opening delimiter as a token.
1079 pub fn open_token(&self) -> token::Token {
1080 token::OpenDelim(self.delim)
1083 /// Returns the closing delimiter as a token.
1084 pub fn close_token(&self) -> token::Token {
1085 token::CloseDelim(self.delim)
1088 /// Returns the opening delimiter as a token tree.
1089 pub fn open_tt(&self) -> TokenTree {
1090 TokenTree::Token(self.open_span, self.open_token())
1093 /// Returns the closing delimiter as a token tree.
1094 pub fn close_tt(&self) -> TokenTree {
1095 TokenTree::Token(self.close_span, self.close_token())
1099 /// A sequence of token treesee
1100 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1101 pub struct SequenceRepetition {
1102 /// The sequence of token trees
1103 pub tts: Vec<TokenTree>,
1104 /// The optional separator
1105 pub separator: Option<token::Token>,
1106 /// Whether the sequence can be repeated zero (*), or one or more times (+)
1108 /// The number of `MatchNt`s that appear in the sequence (and subsequences)
1109 pub num_captures: usize,
1112 /// A Kleene-style [repetition operator](http://en.wikipedia.org/wiki/Kleene_star)
1113 /// for token sequences.
1114 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1120 /// When the main rust parser encounters a syntax-extension invocation, it
1121 /// parses the arguments to the invocation as a token-tree. This is a very
1122 /// loose structure, such that all sorts of different AST-fragments can
1123 /// be passed to syntax extensions using a uniform type.
1125 /// If the syntax extension is an MBE macro, it will attempt to match its
1126 /// LHS token tree against the provided token tree, and if it finds a
1127 /// match, will transcribe the RHS token tree, splicing in any captured
1128 /// macro_parser::matched_nonterminals into the `SubstNt`s it finds.
1130 /// The RHS of an MBE macro is the only place `SubstNt`s are substituted.
1131 /// Nothing special happens to misnamed or misplaced `SubstNt`s.
1132 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1133 pub enum TokenTree {
1135 Token(Span, token::Token),
1136 /// A delimited sequence of token trees
1137 Delimited(Span, Rc<Delimited>),
1139 // This only makes sense in MBE macros.
1141 /// A kleene-style repetition sequence with a span
1142 // FIXME(eddyb) #12938 Use DST.
1143 Sequence(Span, Rc<SequenceRepetition>),
1147 pub fn len(&self) -> usize {
1149 TokenTree::Token(_, token::DocComment(name)) => {
1150 match doc_comment_style(&name.as_str()) {
1151 AttrStyle::Outer => 2,
1152 AttrStyle::Inner => 3
1155 TokenTree::Token(_, token::SpecialVarNt(..)) => 2,
1156 TokenTree::Token(_, token::MatchNt(..)) => 3,
1157 TokenTree::Delimited(_, ref delimed) => {
1158 delimed.tts.len() + 2
1160 TokenTree::Sequence(_, ref seq) => {
1163 TokenTree::Token(..) => 0
1167 pub fn get_tt(&self, index: usize) -> TokenTree {
1168 match (self, index) {
1169 (&TokenTree::Token(sp, token::DocComment(_)), 0) => {
1170 TokenTree::Token(sp, token::Pound)
1172 (&TokenTree::Token(sp, token::DocComment(name)), 1)
1173 if doc_comment_style(&name.as_str()) == AttrStyle::Inner => {
1174 TokenTree::Token(sp, token::Not)
1176 (&TokenTree::Token(sp, token::DocComment(name)), _) => {
1177 let stripped = strip_doc_comment_decoration(&name.as_str());
1178 TokenTree::Delimited(sp, Rc::new(Delimited {
1179 delim: token::Bracket,
1181 tts: vec![TokenTree::Token(sp, token::Ident(token::str_to_ident("doc"),
1183 TokenTree::Token(sp, token::Eq),
1184 TokenTree::Token(sp, token::Literal(
1185 token::StrRaw(token::intern(&stripped), 0), None))],
1189 (&TokenTree::Delimited(_, ref delimed), _) => {
1191 return delimed.open_tt();
1193 if index == delimed.tts.len() + 1 {
1194 return delimed.close_tt();
1196 delimed.tts[index - 1].clone()
1198 (&TokenTree::Token(sp, token::SpecialVarNt(var)), _) => {
1199 let v = [TokenTree::Token(sp, token::Dollar),
1200 TokenTree::Token(sp, token::Ident(token::str_to_ident(var.as_str()),
1204 (&TokenTree::Token(sp, token::MatchNt(name, kind, name_st, kind_st)), _) => {
1205 let v = [TokenTree::Token(sp, token::SubstNt(name, name_st)),
1206 TokenTree::Token(sp, token::Colon),
1207 TokenTree::Token(sp, token::Ident(kind, kind_st))];
1210 (&TokenTree::Sequence(_, ref seq), _) => {
1211 seq.tts[index].clone()
1213 _ => panic!("Cannot expand a token tree")
1217 /// Returns the `Span` corresponding to this token tree.
1218 pub fn get_span(&self) -> Span {
1220 TokenTree::Token(span, _) => span,
1221 TokenTree::Delimited(span, _) => span,
1222 TokenTree::Sequence(span, _) => span,
1226 /// Use this token tree as a matcher to parse given tts.
1227 pub fn parse(cx: &base::ExtCtxt, mtch: &[TokenTree], tts: &[TokenTree])
1228 -> macro_parser::NamedParseResult {
1229 // `None` is because we're not interpolating
1230 let arg_rdr = lexer::new_tt_reader_with_doc_flag(&cx.parse_sess().span_diagnostic,
1233 tts.iter().cloned().collect(),
1235 macro_parser::parse(cx.parse_sess(), cx.cfg(), arg_rdr, mtch)
1239 pub type Mac = Spanned<Mac_>;
1241 /// Represents a macro invocation. The Path indicates which macro
1242 /// is being invoked, and the vector of token-trees contains the source
1243 /// of the macro invocation.
1245 /// NB: the additional ident for a macro_rules-style macro is actually
1246 /// stored in the enclosing item. Oog.
1247 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1250 pub tts: Vec<TokenTree>,
1251 pub ctxt: SyntaxContext,
1254 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1256 /// A regular string, like `"foo"`
1258 /// A raw string, like `r##"foo"##`
1260 /// The uint is the number of `#` symbols used
1265 pub type Lit = Spanned<Lit_>;
1267 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1274 pub fn new<T: IntSign>(n: T) -> Sign {
1280 fn sign(&self) -> Sign;
1283 ($($t:ident)*) => ($(impl IntSign for $t {
1284 #[allow(unused_comparisons)]
1285 fn sign(&self) -> Sign {
1286 if *self < 0 {Minus} else {Plus}
1290 doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
1292 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1293 pub enum LitIntType {
1294 SignedIntLit(IntTy, Sign),
1295 UnsignedIntLit(UintTy),
1296 UnsuffixedIntLit(Sign)
1299 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1301 /// A string literal (`"foo"`)
1302 LitStr(InternedString, StrStyle),
1303 /// A byte string (`b"foo"`)
1304 LitByteStr(Rc<Vec<u8>>),
1305 /// A byte char (`b'f'`)
1307 /// A character literal (`'a'`)
1309 /// An integer literal (`1u8`)
1310 LitInt(u64, LitIntType),
1311 /// A float literal (`1f64` or `1E10f64`)
1312 LitFloat(InternedString, FloatTy),
1313 /// A float literal without a suffix (`1.0 or 1.0E10`)
1314 LitFloatUnsuffixed(InternedString),
1315 /// A boolean literal
1320 /// Returns true if this literal is a string and false otherwise.
1321 pub fn is_str(&self) -> bool {
1329 // NB: If you change this, you'll probably want to change the corresponding
1330 // type structure in middle/ty.rs as well.
1331 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1334 pub mutbl: Mutability,
1337 /// Represents a method's signature in a trait declaration,
1338 /// or in an implementation.
1339 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1340 pub struct MethodSig {
1341 pub unsafety: Unsafety,
1342 pub constness: Constness,
1344 pub decl: P<FnDecl>,
1345 pub generics: Generics,
1346 pub explicit_self: ExplicitSelf,
1349 /// Represents a method declaration in a trait declaration, possibly including
1350 /// a default implementation A trait method is either required (meaning it
1351 /// doesn't have an implementation, just a signature) or provided (meaning it
1352 /// has a default implementation).
1353 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1354 pub struct TraitItem {
1357 pub attrs: Vec<Attribute>,
1358 pub node: TraitItem_,
1362 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1363 pub enum TraitItem_ {
1364 ConstTraitItem(P<Ty>, Option<P<Expr>>),
1365 MethodTraitItem(MethodSig, Option<P<Block>>),
1366 TypeTraitItem(TyParamBounds, Option<P<Ty>>),
1369 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1370 pub struct ImplItem {
1373 pub vis: Visibility,
1374 pub attrs: Vec<Attribute>,
1375 pub node: ImplItemKind,
1379 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1380 pub enum ImplItemKind {
1381 Const(P<Ty>, P<Expr>),
1382 Method(MethodSig, P<Block>),
1387 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
1396 impl fmt::Debug for IntTy {
1397 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1398 fmt::Display::fmt(self, f)
1402 impl fmt::Display for IntTy {
1403 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1404 write!(f, "{}", self.ty_to_string())
1409 pub fn ty_to_string(&self) -> &'static str {
1419 pub fn val_to_string(&self, val: i64) -> String {
1420 // cast to a u64 so we can correctly print INT64_MIN. All integral types
1421 // are parsed as u64, so we wouldn't want to print an extra negative
1423 format!("{}{}", val as u64, self.ty_to_string())
1426 pub fn ty_max(&self) -> u64 {
1430 TyIs | TyI32 => 0x80000000, // actually ni about TyIs
1431 TyI64 => 0x8000000000000000
1435 pub fn bit_width(&self) -> Option<usize> {
1437 TyIs => return None,
1446 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
1456 pub fn ty_to_string(&self) -> &'static str {
1466 pub fn val_to_string(&self, val: u64) -> String {
1467 format!("{}{}", val, self.ty_to_string())
1470 pub fn ty_max(&self) -> u64 {
1474 TyUs | TyU32 => 0xffffffff, // actually ni about TyUs
1475 TyU64 => 0xffffffffffffffff
1479 pub fn bit_width(&self) -> Option<usize> {
1481 TyUs => return None,
1490 impl fmt::Debug for UintTy {
1491 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1492 fmt::Display::fmt(self, f)
1496 impl fmt::Display for UintTy {
1497 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1498 write!(f, "{}", self.ty_to_string())
1502 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
1508 impl fmt::Debug for FloatTy {
1509 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1510 fmt::Display::fmt(self, f)
1514 impl fmt::Display for FloatTy {
1515 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1516 write!(f, "{}", self.ty_to_string())
1521 pub fn ty_to_string(&self) -> &'static str {
1528 pub fn bit_width(&self) -> usize {
1536 // Bind a type to an associated type: `A=Foo`.
1537 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1538 pub struct TypeBinding {
1545 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
1552 impl fmt::Debug for Ty {
1553 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1554 write!(f, "type({})", pprust::ty_to_string(self))
1558 /// Not represented directly in the AST, referred to by name through a ty_path.
1559 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1569 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1570 pub struct BareFnTy {
1571 pub unsafety: Unsafety,
1573 pub lifetimes: Vec<LifetimeDef>,
1577 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1578 /// The different kinds of types recognized by the compiler
1581 /// A fixed length array (`[T; n]`)
1582 TyFixedLengthVec(P<Ty>, P<Expr>),
1583 /// A raw pointer (`*const T` or `*mut T`)
1585 /// A reference (`&'a T` or `&'a mut T`)
1586 TyRptr(Option<Lifetime>, MutTy),
1587 /// A bare function (e.g. `fn(usize) -> bool`)
1588 TyBareFn(P<BareFnTy>),
1589 /// A tuple (`(A, B, C, D,...)`)
1591 /// A path (`module::module::...::Type`), optionally
1592 /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
1594 /// Type parameters are stored in the Path itself
1595 TyPath(Option<QSelf>, Path),
1596 /// Something like `A+B`. Note that `B` must always be a path.
1597 TyObjectSum(P<Ty>, TyParamBounds),
1598 /// A type like `for<'a> Foo<&'a Bar>`
1599 TyPolyTraitRef(TyParamBounds),
1600 /// No-op; kept solely so that we can pretty-print faithfully
1604 /// TyInfer means the type should be inferred instead of it having been
1605 /// specified. This can appear anywhere in a type.
1607 // A macro in the type position.
1611 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1612 pub enum AsmDialect {
1617 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1618 pub struct InlineAsmOutput {
1619 pub constraint: InternedString,
1622 pub is_indirect: bool,
1625 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1626 pub struct InlineAsm {
1627 pub asm: InternedString,
1628 pub asm_str_style: StrStyle,
1629 pub outputs: Vec<InlineAsmOutput>,
1630 pub inputs: Vec<(InternedString, P<Expr>)>,
1631 pub clobbers: Vec<InternedString>,
1633 pub alignstack: bool,
1634 pub dialect: AsmDialect,
1635 pub expn_id: ExpnId,
1638 /// represents an argument in a function header
1639 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1647 pub fn new_self(span: Span, mutability: Mutability, self_ident: Ident) -> Arg {
1648 let path = Spanned{span:span,node:self_ident};
1650 // HACK(eddyb) fake type for the self argument.
1658 node: PatIdent(BindByValue(mutability), path, None),
1666 /// Represents the header (not the body) of a function declaration
1667 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1669 pub inputs: Vec<Arg>,
1670 pub output: FunctionRetTy,
1674 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1680 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1681 pub enum Constness {
1686 impl fmt::Display for Unsafety {
1687 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1688 fmt::Display::fmt(match *self {
1689 Unsafety::Normal => "normal",
1690 Unsafety::Unsafe => "unsafe",
1695 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
1696 pub enum ImplPolarity {
1697 /// `impl Trait for Type`
1699 /// `impl !Trait for Type`
1703 impl fmt::Debug for ImplPolarity {
1704 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1706 ImplPolarity::Positive => "positive".fmt(f),
1707 ImplPolarity::Negative => "negative".fmt(f),
1713 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1714 pub enum FunctionRetTy {
1715 /// Functions with return type `!`that always
1716 /// raise an error or exit (i.e. never return to the caller)
1718 /// Return type is not specified.
1720 /// Functions default to `()` and
1721 /// closures default to inference. Span points to where return
1722 /// type would be inserted.
1723 DefaultReturn(Span),
1728 impl FunctionRetTy {
1729 pub fn span(&self) -> Span {
1731 NoReturn(span) => span,
1732 DefaultReturn(span) => span,
1733 Return(ref ty) => ty.span
1738 /// Represents the kind of 'self' associated with a method
1739 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1740 pub enum ExplicitSelf_ {
1745 /// `&'lt self`, `&'lt mut self`
1746 SelfRegion(Option<Lifetime>, Mutability, Ident),
1748 SelfExplicit(P<Ty>, Ident),
1751 pub type ExplicitSelf = Spanned<ExplicitSelf_>;
1753 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1755 /// A span from the first token past `{` to the last token until `}`.
1756 /// For `mod foo;`, the inner span ranges from the first token
1757 /// to the last token in the external file.
1759 pub items: Vec<P<Item>>,
1762 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1763 pub struct ForeignMod {
1765 pub items: Vec<P<ForeignItem>>,
1768 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1769 pub struct EnumDef {
1770 pub variants: Vec<P<Variant>>,
1773 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1774 pub struct Variant_ {
1776 pub attrs: Vec<Attribute>,
1777 pub data: VariantData,
1778 /// Explicit discriminant, eg `Foo = 1`
1779 pub disr_expr: Option<P<Expr>>,
1782 pub type Variant = Spanned<Variant_>;
1784 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1785 pub enum PathListItem_ {
1788 /// renamed in list, eg `use foo::{bar as baz};`
1789 rename: Option<Ident>,
1793 /// renamed in list, eg `use foo::{self as baz};`
1794 rename: Option<Ident>,
1799 impl PathListItem_ {
1800 pub fn id(&self) -> NodeId {
1802 PathListIdent { id, .. } | PathListMod { id, .. } => id
1806 pub fn name(&self) -> Option<Ident> {
1808 PathListIdent { name, .. } => Some(name),
1809 PathListMod { .. } => None,
1813 pub fn rename(&self) -> Option<Ident> {
1815 PathListIdent { rename, .. } | PathListMod { rename, .. } => rename
1820 pub type PathListItem = Spanned<PathListItem_>;
1822 pub type ViewPath = Spanned<ViewPath_>;
1824 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1825 pub enum ViewPath_ {
1827 /// `foo::bar::baz as quux`
1831 /// `foo::bar::baz` (with `as baz` implicitly on the right)
1832 ViewPathSimple(Ident, Path),
1837 /// `foo::bar::{a,b,c}`
1838 ViewPathList(Path, Vec<PathListItem>)
1841 /// Meta-data associated with an item
1842 pub type Attribute = Spanned<Attribute_>;
1844 /// Distinguishes between Attributes that decorate items and Attributes that
1845 /// are contained as statements within items. These two cases need to be
1846 /// distinguished for pretty-printing.
1847 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1848 pub enum AttrStyle {
1853 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1854 pub struct AttrId(pub usize);
1856 /// Doc-comments are promoted to attributes that have is_sugared_doc = true
1857 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1858 pub struct Attribute_ {
1860 pub style: AttrStyle,
1861 pub value: P<MetaItem>,
1862 pub is_sugared_doc: bool,
1865 /// TraitRef's appear in impls.
1867 /// resolve maps each TraitRef's ref_id to its defining trait; that's all
1868 /// that the ref_id is for. The impl_id maps to the "self type" of this impl.
1869 /// If this impl is an ItemImpl, the impl_id is redundant (it could be the
1870 /// same as the impl's node id).
1871 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1872 pub struct TraitRef {
1877 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1878 pub struct PolyTraitRef {
1879 /// The `'a` in `<'a> Foo<&'a T>`
1880 pub bound_lifetimes: Vec<LifetimeDef>,
1882 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
1883 pub trait_ref: TraitRef,
1888 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1889 pub enum Visibility {
1895 pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility {
1897 Inherited => parent_visibility,
1903 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1904 pub struct StructField_ {
1905 pub kind: StructFieldKind,
1908 pub attrs: Vec<Attribute>,
1912 pub fn ident(&self) -> Option<Ident> {
1914 NamedField(ref ident, _) => Some(ident.clone()),
1915 UnnamedField(_) => None
1920 pub type StructField = Spanned<StructField_>;
1922 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1923 pub enum StructFieldKind {
1924 NamedField(Ident, Visibility),
1925 /// Element of a tuple-like struct
1926 UnnamedField(Visibility),
1929 impl StructFieldKind {
1930 pub fn is_unnamed(&self) -> bool {
1932 UnnamedField(..) => true,
1933 NamedField(..) => false,
1937 pub fn visibility(&self) -> Visibility {
1939 NamedField(_, vis) | UnnamedField(vis) => vis
1944 /// Fields and Ids of enum variants and structs
1946 /// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all
1947 /// variant kinds) and an Id of the variant's constructor (not relevant for `Struct`-variants).
1948 /// One shared Id can be successfully used for these two purposes.
1949 /// Id of the whole enum lives in `Item`.
1951 /// For structs: `NodeId` represents an Id of the structure's constructor, so it is not actually
1952 /// used for `Struct`-structs (but still presents). Structures don't have an analogue of "Id of
1953 /// the variant itself" from enum variants.
1954 /// Id of the whole struct lives in `Item`.
1955 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1956 pub enum VariantData {
1957 Struct(Vec<StructField>, NodeId),
1958 Tuple(Vec<StructField>, NodeId),
1963 pub fn fields(&self) -> &[StructField] {
1965 VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => fields,
1969 pub fn id(&self) -> NodeId {
1971 VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id
1974 pub fn is_struct(&self) -> bool {
1975 if let VariantData::Struct(..) = *self { true } else { false }
1977 pub fn is_tuple(&self) -> bool {
1978 if let VariantData::Tuple(..) = *self { true } else { false }
1980 pub fn is_unit(&self) -> bool {
1981 if let VariantData::Unit(..) = *self { true } else { false }
1986 FIXME (#3300): Should allow items to be anonymous. Right now
1987 we just use dummy names for anon items.
1991 /// The name might be a dummy name in case of anonymous items
1992 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1995 pub attrs: Vec<Attribute>,
1998 pub vis: Visibility,
2003 pub fn attrs(&self) -> &[Attribute] {
2008 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
2010 /// An`extern crate` item, with optional original crate name,
2012 /// e.g. `extern crate foo` or `extern crate foo_bar as foo`
2013 ItemExternCrate(Option<Name>),
2014 /// A `use` or `pub use` item
2015 ItemUse(P<ViewPath>),
2018 ItemStatic(P<Ty>, Mutability, P<Expr>),
2020 ItemConst(P<Ty>, P<Expr>),
2021 /// A function declaration
2022 ItemFn(P<FnDecl>, Unsafety, Constness, Abi, Generics, P<Block>),
2025 /// An external module
2026 ItemForeignMod(ForeignMod),
2027 /// A type alias, e.g. `type Foo = Bar<u8>`
2028 ItemTy(P<Ty>, Generics),
2029 /// An enum definition, e.g. `enum Foo<A, B> {C<A>, D<B>}`
2030 ItemEnum(EnumDef, Generics),
2031 /// A struct definition, e.g. `struct Foo<A> {x: A}`
2032 ItemStruct(VariantData, Generics),
2033 /// Represents a Trait Declaration
2039 // Default trait implementations
2041 // `impl Trait for .. {}`
2042 ItemDefaultImpl(Unsafety, TraitRef),
2043 /// An implementation, eg `impl<A> Trait for Foo { .. }`
2047 Option<TraitRef>, // (optional) trait this impl implements
2050 /// A macro invocation (which includes macro definition)
2055 pub fn descriptive_variant(&self) -> &str {
2057 ItemExternCrate(..) => "extern crate",
2058 ItemUse(..) => "use",
2059 ItemStatic(..) => "static item",
2060 ItemConst(..) => "constant item",
2061 ItemFn(..) => "function",
2062 ItemMod(..) => "module",
2063 ItemForeignMod(..) => "foreign module",
2064 ItemTy(..) => "type alias",
2065 ItemEnum(..) => "enum",
2066 ItemStruct(..) => "struct",
2067 ItemTrait(..) => "trait",
2070 ItemDefaultImpl(..) => "item"
2075 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
2076 pub struct ForeignItem {
2078 pub attrs: Vec<Attribute>,
2079 pub node: ForeignItem_,
2082 pub vis: Visibility,
2085 /// An item within an `extern` block
2086 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
2087 pub enum ForeignItem_ {
2088 /// A foreign function
2089 ForeignItemFn(P<FnDecl>, Generics),
2090 /// A foreign static item (`static ext: u8`), with optional mutability
2091 /// (the boolean is true when mutable)
2092 ForeignItemStatic(P<Ty>, bool),
2096 pub fn descriptive_variant(&self) -> &str {
2098 ForeignItemFn(..) => "foreign function",
2099 ForeignItemStatic(..) => "foreign static item"
2104 /// A macro definition, in this crate or imported from another.
2106 /// Not parsed directly, but created on macro import or `macro_rules!` expansion.
2107 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
2108 pub struct MacroDef {
2110 pub attrs: Vec<Attribute>,
2113 pub imported_from: Option<Ident>,
2115 pub use_locally: bool,
2116 pub allow_internal_unstable: bool,
2117 pub body: Vec<TokenTree>,
2125 // are ASTs encodable?
2127 fn check_asts_encodable() {
2128 fn assert_encodable<T: serialize::Encodable>() {}
2129 assert_encodable::<Crate>();