]> git.proxmox.com Git - rustc.git/blob - src/librustc_front/hir.rs
Imported Upstream version 1.5.0+dfsg1
[rustc.git] / src / librustc_front / hir.rs
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.
4 //
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.
10
11 // The Rust HIR.
12
13 pub use self::BindingMode::*;
14 pub use self::BinOp_::*;
15 pub use self::BlockCheckMode::*;
16 pub use self::CaptureClause::*;
17 pub use self::Decl_::*;
18 pub use self::ExplicitSelf_::*;
19 pub use self::Expr_::*;
20 pub use self::FunctionRetTy::*;
21 pub use self::ForeignItem_::*;
22 pub use self::ImplItem_::*;
23 pub use self::Item_::*;
24 pub use self::Mutability::*;
25 pub use self::Pat_::*;
26 pub use self::PathListItem_::*;
27 pub use self::PatWildKind::*;
28 pub use self::PrimTy::*;
29 pub use self::Stmt_::*;
30 pub use self::StructFieldKind::*;
31 pub use self::TraitItem_::*;
32 pub use self::Ty_::*;
33 pub use self::TyParamBound::*;
34 pub use self::UnOp::*;
35 pub use self::UnsafeSource::*;
36 pub use self::ViewPath_::*;
37 pub use self::Visibility::*;
38 pub use self::PathParameters::*;
39
40 use syntax::codemap::{self, Span, Spanned, DUMMY_SP, ExpnId};
41 use syntax::abi::Abi;
42 use syntax::ast::{Name, Ident, NodeId, DUMMY_NODE_ID, TokenTree, AsmDialect};
43 use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, CrateConfig};
44 use syntax::owned_slice::OwnedSlice;
45 use syntax::parse::token::InternedString;
46 use syntax::ptr::P;
47
48 use print::pprust;
49 use util;
50
51 use std::fmt;
52 use serialize::{Encodable, Encoder, Decoder};
53
54 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
55 pub struct Lifetime {
56 pub id: NodeId,
57 pub span: Span,
58 pub name: Name,
59 }
60
61 impl fmt::Debug for Lifetime {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63 write!(f,
64 "lifetime({}: {})",
65 self.id,
66 pprust::lifetime_to_string(self))
67 }
68 }
69
70 /// A lifetime definition, eg `'a: 'b+'c+'d`
71 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
72 pub struct LifetimeDef {
73 pub lifetime: Lifetime,
74 pub bounds: Vec<Lifetime>,
75 }
76
77 /// A "Path" is essentially Rust's notion of a name; for instance:
78 /// std::cmp::PartialEq . It's represented as a sequence of identifiers,
79 /// along with a bunch of supporting information.
80 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
81 pub struct Path {
82 pub span: Span,
83 /// A `::foo` path, is relative to the crate root rather than current
84 /// module (like paths in an import).
85 pub global: bool,
86 /// The segments in the path: the things separated by `::`.
87 pub segments: Vec<PathSegment>,
88 }
89
90 impl fmt::Debug for Path {
91 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92 write!(f, "path({})", pprust::path_to_string(self))
93 }
94 }
95
96 impl fmt::Display for Path {
97 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98 write!(f, "{}", pprust::path_to_string(self))
99 }
100 }
101
102 /// A segment of a path: an identifier, an optional lifetime, and a set of
103 /// types.
104 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
105 pub struct PathSegment {
106 /// The identifier portion of this path segment.
107 pub identifier: Ident,
108
109 /// Type/lifetime parameters attached to this path. They come in
110 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
111 /// this is more than just simple syntactic sugar; the use of
112 /// parens affects the region binding rules, so we preserve the
113 /// distinction.
114 pub parameters: PathParameters,
115 }
116
117 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
118 pub enum PathParameters {
119 /// The `<'a, A,B,C>` in `foo::bar::baz::<'a, A,B,C>`
120 AngleBracketedParameters(AngleBracketedParameterData),
121 /// The `(A,B)` and `C` in `Foo(A,B) -> C`
122 ParenthesizedParameters(ParenthesizedParameterData),
123 }
124
125 impl PathParameters {
126 pub fn none() -> PathParameters {
127 AngleBracketedParameters(AngleBracketedParameterData {
128 lifetimes: Vec::new(),
129 types: OwnedSlice::empty(),
130 bindings: OwnedSlice::empty(),
131 })
132 }
133
134 pub fn is_empty(&self) -> bool {
135 match *self {
136 AngleBracketedParameters(ref data) => data.is_empty(),
137
138 // Even if the user supplied no types, something like
139 // `X()` is equivalent to `X<(),()>`.
140 ParenthesizedParameters(..) => false,
141 }
142 }
143
144 pub fn has_lifetimes(&self) -> bool {
145 match *self {
146 AngleBracketedParameters(ref data) => !data.lifetimes.is_empty(),
147 ParenthesizedParameters(_) => false,
148 }
149 }
150
151 pub fn has_types(&self) -> bool {
152 match *self {
153 AngleBracketedParameters(ref data) => !data.types.is_empty(),
154 ParenthesizedParameters(..) => true,
155 }
156 }
157
158 /// Returns the types that the user wrote. Note that these do not necessarily map to the type
159 /// parameters in the parenthesized case.
160 pub fn types(&self) -> Vec<&P<Ty>> {
161 match *self {
162 AngleBracketedParameters(ref data) => {
163 data.types.iter().collect()
164 }
165 ParenthesizedParameters(ref data) => {
166 data.inputs
167 .iter()
168 .chain(data.output.iter())
169 .collect()
170 }
171 }
172 }
173
174 pub fn lifetimes(&self) -> Vec<&Lifetime> {
175 match *self {
176 AngleBracketedParameters(ref data) => {
177 data.lifetimes.iter().collect()
178 }
179 ParenthesizedParameters(_) => {
180 Vec::new()
181 }
182 }
183 }
184
185 pub fn bindings(&self) -> Vec<&P<TypeBinding>> {
186 match *self {
187 AngleBracketedParameters(ref data) => {
188 data.bindings.iter().collect()
189 }
190 ParenthesizedParameters(_) => {
191 Vec::new()
192 }
193 }
194 }
195 }
196
197 /// A path like `Foo<'a, T>`
198 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
199 pub struct AngleBracketedParameterData {
200 /// The lifetime parameters for this path segment.
201 pub lifetimes: Vec<Lifetime>,
202 /// The type parameters for this path segment, if present.
203 pub types: OwnedSlice<P<Ty>>,
204 /// Bindings (equality constraints) on associated types, if present.
205 /// E.g., `Foo<A=Bar>`.
206 pub bindings: OwnedSlice<P<TypeBinding>>,
207 }
208
209 impl AngleBracketedParameterData {
210 fn is_empty(&self) -> bool {
211 self.lifetimes.is_empty() && self.types.is_empty() && self.bindings.is_empty()
212 }
213 }
214
215 /// A path like `Foo(A,B) -> C`
216 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
217 pub struct ParenthesizedParameterData {
218 /// Overall span
219 pub span: Span,
220
221 /// `(A,B)`
222 pub inputs: Vec<P<Ty>>,
223
224 /// `C`
225 pub output: Option<P<Ty>>,
226 }
227
228 /// The AST represents all type param bounds as types.
229 /// typeck::collect::compute_bounds matches these against
230 /// the "special" built-in traits (see middle::lang_items) and
231 /// detects Copy, Send and Sync.
232 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
233 pub enum TyParamBound {
234 TraitTyParamBound(PolyTraitRef, TraitBoundModifier),
235 RegionTyParamBound(Lifetime),
236 }
237
238 /// A modifier on a bound, currently this is only used for `?Sized`, where the
239 /// modifier is `Maybe`. Negative bounds should also be handled here.
240 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
241 pub enum TraitBoundModifier {
242 None,
243 Maybe,
244 }
245
246 pub type TyParamBounds = OwnedSlice<TyParamBound>;
247
248 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
249 pub struct TyParam {
250 pub name: Name,
251 pub id: NodeId,
252 pub bounds: TyParamBounds,
253 pub default: Option<P<Ty>>,
254 pub span: Span,
255 }
256
257 /// Represents lifetimes and type parameters attached to a declaration
258 /// of a function, enum, trait, etc.
259 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
260 pub struct Generics {
261 pub lifetimes: Vec<LifetimeDef>,
262 pub ty_params: OwnedSlice<TyParam>,
263 pub where_clause: WhereClause,
264 }
265
266 impl Generics {
267 pub fn is_lt_parameterized(&self) -> bool {
268 !self.lifetimes.is_empty()
269 }
270 pub fn is_type_parameterized(&self) -> bool {
271 !self.ty_params.is_empty()
272 }
273 pub fn is_parameterized(&self) -> bool {
274 self.is_lt_parameterized() || self.is_type_parameterized()
275 }
276 }
277
278 /// A `where` clause in a definition
279 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
280 pub struct WhereClause {
281 pub id: NodeId,
282 pub predicates: Vec<WherePredicate>,
283 }
284
285 /// A single predicate in a `where` clause
286 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
287 pub enum WherePredicate {
288 /// A type binding, eg `for<'c> Foo: Send+Clone+'c`
289 BoundPredicate(WhereBoundPredicate),
290 /// A lifetime predicate, e.g. `'a: 'b+'c`
291 RegionPredicate(WhereRegionPredicate),
292 /// An equality predicate (unsupported)
293 EqPredicate(WhereEqPredicate),
294 }
295
296 /// A type bound, eg `for<'c> Foo: Send+Clone+'c`
297 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
298 pub struct WhereBoundPredicate {
299 pub span: Span,
300 /// Any lifetimes from a `for` binding
301 pub bound_lifetimes: Vec<LifetimeDef>,
302 /// The type being bounded
303 pub bounded_ty: P<Ty>,
304 /// Trait and lifetime bounds (`Clone+Send+'static`)
305 pub bounds: OwnedSlice<TyParamBound>,
306 }
307
308 /// A lifetime predicate, e.g. `'a: 'b+'c`
309 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
310 pub struct WhereRegionPredicate {
311 pub span: Span,
312 pub lifetime: Lifetime,
313 pub bounds: Vec<Lifetime>,
314 }
315
316 /// An equality predicate (unsupported), e.g. `T=int`
317 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
318 pub struct WhereEqPredicate {
319 pub id: NodeId,
320 pub span: Span,
321 pub path: Path,
322 pub ty: P<Ty>,
323 }
324
325 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
326 pub struct Crate {
327 pub module: Mod,
328 pub attrs: Vec<Attribute>,
329 pub config: CrateConfig,
330 pub span: Span,
331 pub exported_macros: Vec<MacroDef>,
332 }
333
334 /// A macro definition, in this crate or imported from another.
335 ///
336 /// Not parsed directly, but created on macro import or `macro_rules!` expansion.
337 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
338 pub struct MacroDef {
339 pub name: Name,
340 pub attrs: Vec<Attribute>,
341 pub id: NodeId,
342 pub span: Span,
343 pub imported_from: Option<Name>,
344 pub export: bool,
345 pub use_locally: bool,
346 pub allow_internal_unstable: bool,
347 pub body: Vec<TokenTree>,
348 }
349
350 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
351 pub struct Block {
352 /// Statements in a block
353 pub stmts: Vec<P<Stmt>>,
354 /// An expression at the end of the block
355 /// without a semicolon, if any
356 pub expr: Option<P<Expr>>,
357 pub id: NodeId,
358 /// Distinguishes between `unsafe { ... }` and `{ ... }`
359 pub rules: BlockCheckMode,
360 pub span: Span,
361 }
362
363 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
364 pub struct Pat {
365 pub id: NodeId,
366 pub node: Pat_,
367 pub span: Span,
368 }
369
370 impl fmt::Debug for Pat {
371 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
372 write!(f, "pat({}: {})", self.id, pprust::pat_to_string(self))
373 }
374 }
375
376 /// A single field in a struct pattern
377 ///
378 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
379 /// are treated the same as` x: x, y: ref y, z: ref mut z`,
380 /// except is_shorthand is true
381 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
382 pub struct FieldPat {
383 /// The identifier for the field
384 pub name: Name,
385 /// The pattern the field is destructured to
386 pub pat: P<Pat>,
387 pub is_shorthand: bool,
388 }
389
390 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
391 pub enum BindingMode {
392 BindByRef(Mutability),
393 BindByValue(Mutability),
394 }
395
396 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
397 pub enum PatWildKind {
398 /// Represents the wildcard pattern `_`
399 PatWildSingle,
400
401 /// Represents the wildcard pattern `..`
402 PatWildMulti,
403 }
404
405 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
406 pub enum Pat_ {
407 /// Represents a wildcard pattern (either `_` or `..`)
408 PatWild(PatWildKind),
409
410 /// A PatIdent may either be a new bound variable,
411 /// or a nullary enum (in which case the third field
412 /// is None).
413 ///
414 /// In the nullary enum case, the parser can't determine
415 /// which it is. The resolver determines this, and
416 /// records this pattern's NodeId in an auxiliary
417 /// set (of "PatIdents that refer to nullary enums")
418 PatIdent(BindingMode, Spanned<Ident>, Option<P<Pat>>),
419
420 /// "None" means a * pattern where we don't bind the fields to names.
421 PatEnum(Path, Option<Vec<P<Pat>>>),
422
423 /// An associated const named using the qualified path `<T>::CONST` or
424 /// `<T as Trait>::CONST`. Associated consts from inherent impls can be
425 /// referred to as simply `T::CONST`, in which case they will end up as
426 /// PatEnum, and the resolver will have to sort that out.
427 PatQPath(QSelf, Path),
428
429 /// Destructuring of a struct, e.g. `Foo {x, y, ..}`
430 /// The `bool` is `true` in the presence of a `..`
431 PatStruct(Path, Vec<Spanned<FieldPat>>, bool),
432 /// A tuple pattern `(a, b)`
433 PatTup(Vec<P<Pat>>),
434 /// A `box` pattern
435 PatBox(P<Pat>),
436 /// A reference pattern, e.g. `&mut (a, b)`
437 PatRegion(P<Pat>, Mutability),
438 /// A literal
439 PatLit(P<Expr>),
440 /// A range pattern, e.g. `1...2`
441 PatRange(P<Expr>, P<Expr>),
442 /// [a, b, ..i, y, z] is represented as:
443 /// PatVec(box [a, b], Some(i), box [y, z])
444 PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
445 }
446
447 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
448 pub enum Mutability {
449 MutMutable,
450 MutImmutable,
451 }
452
453 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
454 pub enum BinOp_ {
455 /// The `+` operator (addition)
456 BiAdd,
457 /// The `-` operator (subtraction)
458 BiSub,
459 /// The `*` operator (multiplication)
460 BiMul,
461 /// The `/` operator (division)
462 BiDiv,
463 /// The `%` operator (modulus)
464 BiRem,
465 /// The `&&` operator (logical and)
466 BiAnd,
467 /// The `||` operator (logical or)
468 BiOr,
469 /// The `^` operator (bitwise xor)
470 BiBitXor,
471 /// The `&` operator (bitwise and)
472 BiBitAnd,
473 /// The `|` operator (bitwise or)
474 BiBitOr,
475 /// The `<<` operator (shift left)
476 BiShl,
477 /// The `>>` operator (shift right)
478 BiShr,
479 /// The `==` operator (equality)
480 BiEq,
481 /// The `<` operator (less than)
482 BiLt,
483 /// The `<=` operator (less than or equal to)
484 BiLe,
485 /// The `!=` operator (not equal to)
486 BiNe,
487 /// The `>=` operator (greater than or equal to)
488 BiGe,
489 /// The `>` operator (greater than)
490 BiGt,
491 }
492
493 pub type BinOp = Spanned<BinOp_>;
494
495 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
496 pub enum UnOp {
497 /// The `*` operator for dereferencing
498 UnDeref,
499 /// The `!` operator for logical inversion
500 UnNot,
501 /// The `-` operator for negation
502 UnNeg,
503 }
504
505 /// A statement
506 pub type Stmt = Spanned<Stmt_>;
507
508 impl fmt::Debug for Stmt_ {
509 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
510 // Sadness.
511 let spanned = codemap::dummy_spanned(self.clone());
512 write!(f,
513 "stmt({}: {})",
514 util::stmt_id(&spanned),
515 pprust::stmt_to_string(&spanned))
516 }
517 }
518
519 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
520 pub enum Stmt_ {
521 /// Could be an item or a local (let) binding:
522 StmtDecl(P<Decl>, NodeId),
523
524 /// Expr without trailing semi-colon (must have unit type):
525 StmtExpr(P<Expr>, NodeId),
526
527 /// Expr with trailing semi-colon (may have any type):
528 StmtSemi(P<Expr>, NodeId),
529 }
530
531 // FIXME (pending discussion of #1697, #2178...): local should really be
532 // a refinement on pat.
533 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
534 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
535 pub struct Local {
536 pub pat: P<Pat>,
537 pub ty: Option<P<Ty>>,
538 /// Initializer expression to set the value, if any
539 pub init: Option<P<Expr>>,
540 pub id: NodeId,
541 pub span: Span,
542 }
543
544 pub type Decl = Spanned<Decl_>;
545
546 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
547 pub enum Decl_ {
548 /// A local (let) binding:
549 DeclLocal(P<Local>),
550 /// An item binding:
551 DeclItem(P<Item>),
552 }
553
554 /// represents one arm of a 'match'
555 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
556 pub struct Arm {
557 pub attrs: Vec<Attribute>,
558 pub pats: Vec<P<Pat>>,
559 pub guard: Option<P<Expr>>,
560 pub body: P<Expr>,
561 }
562
563 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
564 pub struct Field {
565 pub name: Spanned<Name>,
566 pub expr: P<Expr>,
567 pub span: Span,
568 }
569
570 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
571 pub enum BlockCheckMode {
572 DefaultBlock,
573 UnsafeBlock(UnsafeSource),
574 PushUnsafeBlock(UnsafeSource),
575 PopUnsafeBlock(UnsafeSource),
576 // Within this block (but outside a PopUnstableBlock), we suspend checking of stability.
577 PushUnstableBlock,
578 PopUnstableBlock,
579 }
580
581 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
582 pub enum UnsafeSource {
583 CompilerGenerated,
584 UserProvided,
585 }
586
587 /// An expression
588 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
589 pub struct Expr {
590 pub id: NodeId,
591 pub node: Expr_,
592 pub span: Span,
593 }
594
595 impl fmt::Debug for Expr {
596 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
597 write!(f, "expr({}: {})", self.id, pprust::expr_to_string(self))
598 }
599 }
600
601 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
602 pub enum Expr_ {
603 /// A `box x` expression.
604 ExprBox(P<Expr>),
605 /// An array (`[a, b, c, d]`)
606 ExprVec(Vec<P<Expr>>),
607 /// A function call
608 ///
609 /// The first field resolves to the function itself,
610 /// and the second field is the list of arguments
611 ExprCall(P<Expr>, Vec<P<Expr>>),
612 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
613 ///
614 /// The `Spanned<Name>` is the identifier for the method name.
615 /// The vector of `Ty`s are the ascripted type parameters for the method
616 /// (within the angle brackets).
617 ///
618 /// The first element of the vector of `Expr`s is the expression that evaluates
619 /// to the object on which the method is being called on (the receiver),
620 /// and the remaining elements are the rest of the arguments.
621 ///
622 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
623 /// `ExprMethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
624 ExprMethodCall(Spanned<Name>, Vec<P<Ty>>, Vec<P<Expr>>),
625 /// A tuple (`(a, b, c ,d)`)
626 ExprTup(Vec<P<Expr>>),
627 /// A binary operation (For example: `a + b`, `a * b`)
628 ExprBinary(BinOp, P<Expr>, P<Expr>),
629 /// A unary operation (For example: `!x`, `*x`)
630 ExprUnary(UnOp, P<Expr>),
631 /// A literal (For example: `1u8`, `"foo"`)
632 ExprLit(P<Lit>),
633 /// A cast (`foo as f64`)
634 ExprCast(P<Expr>, P<Ty>),
635 /// An `if` block, with an optional else block
636 ///
637 /// `if expr { block } else { expr }`
638 ExprIf(P<Expr>, P<Block>, Option<P<Expr>>),
639 /// A while loop, with an optional label
640 ///
641 /// `'label: while expr { block }`
642 ExprWhile(P<Expr>, P<Block>, Option<Ident>),
643 /// Conditionless loop (can be exited with break, continue, or return)
644 ///
645 /// `'label: loop { block }`
646 ExprLoop(P<Block>, Option<Ident>),
647 /// A `match` block, with a source that indicates whether or not it is
648 /// the result of a desugaring, and if so, which kind.
649 ExprMatch(P<Expr>, Vec<Arm>, MatchSource),
650 /// A closure (for example, `move |a, b, c| {a + b + c}`)
651 ExprClosure(CaptureClause, P<FnDecl>, P<Block>),
652 /// A block (`{ ... }`)
653 ExprBlock(P<Block>),
654
655 /// An assignment (`a = foo()`)
656 ExprAssign(P<Expr>, P<Expr>),
657 /// An assignment with an operator
658 ///
659 /// For example, `a += 1`.
660 ExprAssignOp(BinOp, P<Expr>, P<Expr>),
661 /// Access of a named struct field (`obj.foo`)
662 ExprField(P<Expr>, Spanned<Name>),
663 /// Access of an unnamed field of a struct or tuple-struct
664 ///
665 /// For example, `foo.0`.
666 ExprTupField(P<Expr>, Spanned<usize>),
667 /// An indexing operation (`foo[2]`)
668 ExprIndex(P<Expr>, P<Expr>),
669 /// A range (`1..2`, `1..`, or `..2`)
670 ExprRange(Option<P<Expr>>, Option<P<Expr>>),
671
672 /// Variable reference, possibly containing `::` and/or type
673 /// parameters, e.g. foo::bar::<baz>.
674 ///
675 /// Optionally "qualified",
676 /// e.g. `<Vec<T> as SomeTrait>::SomeType`.
677 ExprPath(Option<QSelf>, Path),
678
679 /// A referencing operation (`&a` or `&mut a`)
680 ExprAddrOf(Mutability, P<Expr>),
681 /// A `break`, with an optional label to break
682 ExprBreak(Option<Spanned<Ident>>),
683 /// A `continue`, with an optional label
684 ExprAgain(Option<Spanned<Ident>>),
685 /// A `return`, with an optional value to be returned
686 ExprRet(Option<P<Expr>>),
687
688 /// Output of the `asm!()` macro
689 ExprInlineAsm(InlineAsm),
690
691 /// A struct literal expression.
692 ///
693 /// For example, `Foo {x: 1, y: 2}`, or
694 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
695 ExprStruct(Path, Vec<Field>, Option<P<Expr>>),
696
697 /// A vector literal constructed from one repeated element.
698 ///
699 /// For example, `[1u8; 5]`. The first expression is the element
700 /// to be repeated; the second is the number of times to repeat it.
701 ExprRepeat(P<Expr>, P<Expr>),
702 }
703
704 /// The explicit Self type in a "qualified path". The actual
705 /// path, including the trait and the associated item, is stored
706 /// separately. `position` represents the index of the associated
707 /// item qualified with this Self type.
708 ///
709 /// <Vec<T> as a::b::Trait>::AssociatedItem
710 /// ^~~~~ ~~~~~~~~~~~~~~^
711 /// ty position = 3
712 ///
713 /// <Vec<T>>::AssociatedItem
714 /// ^~~~~ ^
715 /// ty position = 0
716 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
717 pub struct QSelf {
718 pub ty: P<Ty>,
719 pub position: usize,
720 }
721
722 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
723 pub enum MatchSource {
724 Normal,
725 IfLetDesugar {
726 contains_else_clause: bool,
727 },
728 WhileLetDesugar,
729 ForLoopDesugar,
730 }
731
732 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
733 pub enum CaptureClause {
734 CaptureByValue,
735 CaptureByRef,
736 }
737
738 // NB: If you change this, you'll probably want to change the corresponding
739 // type structure in middle/ty.rs as well.
740 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
741 pub struct MutTy {
742 pub ty: P<Ty>,
743 pub mutbl: Mutability,
744 }
745
746 /// Represents a method's signature in a trait declaration,
747 /// or in an implementation.
748 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
749 pub struct MethodSig {
750 pub unsafety: Unsafety,
751 pub constness: Constness,
752 pub abi: Abi,
753 pub decl: P<FnDecl>,
754 pub generics: Generics,
755 pub explicit_self: ExplicitSelf,
756 }
757
758 /// Represents a method declaration in a trait declaration, possibly including
759 /// a default implementation A trait method is either required (meaning it
760 /// doesn't have an implementation, just a signature) or provided (meaning it
761 /// has a default implementation).
762 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
763 pub struct TraitItem {
764 pub id: NodeId,
765 pub name: Name,
766 pub attrs: Vec<Attribute>,
767 pub node: TraitItem_,
768 pub span: Span,
769 }
770
771 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
772 pub enum TraitItem_ {
773 ConstTraitItem(P<Ty>, Option<P<Expr>>),
774 MethodTraitItem(MethodSig, Option<P<Block>>),
775 TypeTraitItem(TyParamBounds, Option<P<Ty>>),
776 }
777
778 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
779 pub struct ImplItem {
780 pub id: NodeId,
781 pub name: Name,
782 pub vis: Visibility,
783 pub attrs: Vec<Attribute>,
784 pub node: ImplItem_,
785 pub span: Span,
786 }
787
788 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
789 pub enum ImplItem_ {
790 ConstImplItem(P<Ty>, P<Expr>),
791 MethodImplItem(MethodSig, P<Block>),
792 TypeImplItem(P<Ty>),
793 }
794
795 // Bind a type to an associated type: `A=Foo`.
796 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
797 pub struct TypeBinding {
798 pub id: NodeId,
799 pub name: Name,
800 pub ty: P<Ty>,
801 pub span: Span,
802 }
803
804
805 // NB PartialEq method appears below.
806 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
807 pub struct Ty {
808 pub id: NodeId,
809 pub node: Ty_,
810 pub span: Span,
811 }
812
813 impl fmt::Debug for Ty {
814 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
815 write!(f, "type({})", pprust::ty_to_string(self))
816 }
817 }
818
819 /// Not represented directly in the AST, referred to by name through a ty_path.
820 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
821 pub enum PrimTy {
822 TyInt(IntTy),
823 TyUint(UintTy),
824 TyFloat(FloatTy),
825 TyStr,
826 TyBool,
827 TyChar,
828 }
829
830 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
831 pub struct BareFnTy {
832 pub unsafety: Unsafety,
833 pub abi: Abi,
834 pub lifetimes: Vec<LifetimeDef>,
835 pub decl: P<FnDecl>,
836 }
837
838 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
839 /// The different kinds of types recognized by the compiler
840 pub enum Ty_ {
841 TyVec(P<Ty>),
842 /// A fixed length array (`[T; n]`)
843 TyFixedLengthVec(P<Ty>, P<Expr>),
844 /// A raw pointer (`*const T` or `*mut T`)
845 TyPtr(MutTy),
846 /// A reference (`&'a T` or `&'a mut T`)
847 TyRptr(Option<Lifetime>, MutTy),
848 /// A bare function (e.g. `fn(usize) -> bool`)
849 TyBareFn(P<BareFnTy>),
850 /// A tuple (`(A, B, C, D,...)`)
851 TyTup(Vec<P<Ty>>),
852 /// A path (`module::module::...::Type`), optionally
853 /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
854 ///
855 /// Type parameters are stored in the Path itself
856 TyPath(Option<QSelf>, Path),
857 /// Something like `A+B`. Note that `B` must always be a path.
858 TyObjectSum(P<Ty>, TyParamBounds),
859 /// A type like `for<'a> Foo<&'a Bar>`
860 TyPolyTraitRef(TyParamBounds),
861 /// No-op; kept solely so that we can pretty-print faithfully
862 TyParen(P<Ty>),
863 /// Unused for now
864 TyTypeof(P<Expr>),
865 /// TyInfer means the type should be inferred instead of it having been
866 /// specified. This can appear anywhere in a type.
867 TyInfer,
868 }
869
870 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
871 pub struct InlineAsm {
872 pub asm: InternedString,
873 pub asm_str_style: StrStyle,
874 pub outputs: Vec<(InternedString, P<Expr>, bool)>,
875 pub inputs: Vec<(InternedString, P<Expr>)>,
876 pub clobbers: Vec<InternedString>,
877 pub volatile: bool,
878 pub alignstack: bool,
879 pub dialect: AsmDialect,
880 pub expn_id: ExpnId,
881 }
882
883 /// represents an argument in a function header
884 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
885 pub struct Arg {
886 pub ty: P<Ty>,
887 pub pat: P<Pat>,
888 pub id: NodeId,
889 }
890
891 impl Arg {
892 pub fn new_self(span: Span, mutability: Mutability, self_ident: Ident) -> Arg {
893 let path = Spanned {
894 span: span,
895 node: self_ident,
896 };
897 Arg {
898 // HACK(eddyb) fake type for the self argument.
899 ty: P(Ty {
900 id: DUMMY_NODE_ID,
901 node: TyInfer,
902 span: DUMMY_SP,
903 }),
904 pat: P(Pat {
905 id: DUMMY_NODE_ID,
906 node: PatIdent(BindByValue(mutability), path, None),
907 span: span,
908 }),
909 id: DUMMY_NODE_ID,
910 }
911 }
912 }
913
914 /// Represents the header (not the body) of a function declaration
915 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
916 pub struct FnDecl {
917 pub inputs: Vec<Arg>,
918 pub output: FunctionRetTy,
919 pub variadic: bool,
920 }
921
922 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
923 pub enum Unsafety {
924 Unsafe,
925 Normal,
926 }
927
928 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
929 pub enum Constness {
930 Const,
931 NotConst,
932 }
933
934 impl fmt::Display for Unsafety {
935 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
936 fmt::Display::fmt(match *self {
937 Unsafety::Normal => "normal",
938 Unsafety::Unsafe => "unsafe",
939 },
940 f)
941 }
942 }
943
944 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
945 pub enum ImplPolarity {
946 /// `impl Trait for Type`
947 Positive,
948 /// `impl !Trait for Type`
949 Negative,
950 }
951
952 impl fmt::Debug for ImplPolarity {
953 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
954 match *self {
955 ImplPolarity::Positive => "positive".fmt(f),
956 ImplPolarity::Negative => "negative".fmt(f),
957 }
958 }
959 }
960
961
962 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
963 pub enum FunctionRetTy {
964 /// Functions with return type `!`that always
965 /// raise an error or exit (i.e. never return to the caller)
966 NoReturn(Span),
967 /// Return type is not specified.
968 ///
969 /// Functions default to `()` and
970 /// closures default to inference. Span points to where return
971 /// type would be inserted.
972 DefaultReturn(Span),
973 /// Everything else
974 Return(P<Ty>),
975 }
976
977 impl FunctionRetTy {
978 pub fn span(&self) -> Span {
979 match *self {
980 NoReturn(span) => span,
981 DefaultReturn(span) => span,
982 Return(ref ty) => ty.span,
983 }
984 }
985 }
986
987 /// Represents the kind of 'self' associated with a method
988 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
989 pub enum ExplicitSelf_ {
990 /// No self
991 SelfStatic,
992 /// `self`
993 SelfValue(Name),
994 /// `&'lt self`, `&'lt mut self`
995 SelfRegion(Option<Lifetime>, Mutability, Name),
996 /// `self: TYPE`
997 SelfExplicit(P<Ty>, Name),
998 }
999
1000 pub type ExplicitSelf = Spanned<ExplicitSelf_>;
1001
1002 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1003 pub struct Mod {
1004 /// A span from the first token past `{` to the last token until `}`.
1005 /// For `mod foo;`, the inner span ranges from the first token
1006 /// to the last token in the external file.
1007 pub inner: Span,
1008 pub items: Vec<P<Item>>,
1009 }
1010
1011 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1012 pub struct ForeignMod {
1013 pub abi: Abi,
1014 pub items: Vec<P<ForeignItem>>,
1015 }
1016
1017 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1018 pub struct EnumDef {
1019 pub variants: Vec<P<Variant>>,
1020 }
1021
1022 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1023 pub struct Variant_ {
1024 pub name: Name,
1025 pub attrs: Vec<Attribute>,
1026 pub data: VariantData,
1027 /// Explicit discriminant, eg `Foo = 1`
1028 pub disr_expr: Option<P<Expr>>,
1029 }
1030
1031 pub type Variant = Spanned<Variant_>;
1032
1033 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1034 pub enum PathListItem_ {
1035 PathListIdent {
1036 name: Name,
1037 /// renamed in list, eg `use foo::{bar as baz};`
1038 rename: Option<Name>,
1039 id: NodeId,
1040 },
1041 PathListMod {
1042 /// renamed in list, eg `use foo::{self as baz};`
1043 rename: Option<Name>,
1044 id: NodeId,
1045 },
1046 }
1047
1048 impl PathListItem_ {
1049 pub fn id(&self) -> NodeId {
1050 match *self {
1051 PathListIdent { id, .. } | PathListMod { id, .. } => id,
1052 }
1053 }
1054
1055 pub fn name(&self) -> Option<Name> {
1056 match *self {
1057 PathListIdent { name, .. } => Some(name),
1058 PathListMod { .. } => None,
1059 }
1060 }
1061
1062 pub fn rename(&self) -> Option<Name> {
1063 match *self {
1064 PathListIdent { rename, .. } | PathListMod { rename, .. } => rename,
1065 }
1066 }
1067 }
1068
1069 pub type PathListItem = Spanned<PathListItem_>;
1070
1071 pub type ViewPath = Spanned<ViewPath_>;
1072
1073 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1074 pub enum ViewPath_ {
1075
1076 /// `foo::bar::baz as quux`
1077 ///
1078 /// or just
1079 ///
1080 /// `foo::bar::baz` (with `as baz` implicitly on the right)
1081 ViewPathSimple(Name, Path),
1082
1083 /// `foo::bar::*`
1084 ViewPathGlob(Path),
1085
1086 /// `foo::bar::{a,b,c}`
1087 ViewPathList(Path, Vec<PathListItem>),
1088 }
1089
1090 /// TraitRef's appear in impls.
1091 ///
1092 /// resolve maps each TraitRef's ref_id to its defining trait; that's all
1093 /// that the ref_id is for. The impl_id maps to the "self type" of this impl.
1094 /// If this impl is an ItemImpl, the impl_id is redundant (it could be the
1095 /// same as the impl's node id).
1096 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1097 pub struct TraitRef {
1098 pub path: Path,
1099 pub ref_id: NodeId,
1100 }
1101
1102 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1103 pub struct PolyTraitRef {
1104 /// The `'a` in `<'a> Foo<&'a T>`
1105 pub bound_lifetimes: Vec<LifetimeDef>,
1106
1107 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
1108 pub trait_ref: TraitRef,
1109
1110 pub span: Span,
1111 }
1112
1113 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1114 pub enum Visibility {
1115 Public,
1116 Inherited,
1117 }
1118
1119 impl Visibility {
1120 pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility {
1121 match self {
1122 &Inherited => parent_visibility,
1123 &Public => *self,
1124 }
1125 }
1126 }
1127
1128 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1129 pub struct StructField_ {
1130 pub kind: StructFieldKind,
1131 pub id: NodeId,
1132 pub ty: P<Ty>,
1133 pub attrs: Vec<Attribute>,
1134 }
1135
1136 impl StructField_ {
1137 pub fn name(&self) -> Option<Name> {
1138 match self.kind {
1139 NamedField(name, _) => Some(name),
1140 UnnamedField(_) => None,
1141 }
1142 }
1143 }
1144
1145 pub type StructField = Spanned<StructField_>;
1146
1147 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1148 pub enum StructFieldKind {
1149 NamedField(Name, Visibility),
1150 /// Element of a tuple-like struct
1151 UnnamedField(Visibility),
1152 }
1153
1154 impl StructFieldKind {
1155 pub fn is_unnamed(&self) -> bool {
1156 match *self {
1157 UnnamedField(..) => true,
1158 NamedField(..) => false,
1159 }
1160 }
1161 }
1162
1163 /// Fields and Ids of enum variants and structs
1164 ///
1165 /// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all
1166 /// variant kinds) and an Id of the variant's constructor (not relevant for `Struct`-variants).
1167 /// One shared Id can be successfully used for these two purposes.
1168 /// Id of the whole enum lives in `Item`.
1169 ///
1170 /// For structs: `NodeId` represents an Id of the structure's constructor, so it is not actually
1171 /// used for `Struct`-structs (but still presents). Structures don't have an analogue of "Id of
1172 /// the variant itself" from enum variants.
1173 /// Id of the whole struct lives in `Item`.
1174 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1175 pub enum VariantData {
1176 Struct(Vec<StructField>, NodeId),
1177 Tuple(Vec<StructField>, NodeId),
1178 Unit(NodeId),
1179 }
1180
1181 impl VariantData {
1182 pub fn fields(&self) -> &[StructField] {
1183 match *self {
1184 VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => fields,
1185 _ => &[],
1186 }
1187 }
1188 pub fn id(&self) -> NodeId {
1189 match *self {
1190 VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id
1191 }
1192 }
1193 pub fn is_struct(&self) -> bool {
1194 if let VariantData::Struct(..) = *self { true } else { false }
1195 }
1196 pub fn is_tuple(&self) -> bool {
1197 if let VariantData::Tuple(..) = *self { true } else { false }
1198 }
1199 pub fn is_unit(&self) -> bool {
1200 if let VariantData::Unit(..) = *self { true } else { false }
1201 }
1202 }
1203
1204 /*
1205 FIXME (#3300): Should allow items to be anonymous. Right now
1206 we just use dummy names for anon items.
1207 */
1208 /// An item
1209 ///
1210 /// The name might be a dummy name in case of anonymous items
1211 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1212 pub struct Item {
1213 pub name: Name,
1214 pub attrs: Vec<Attribute>,
1215 pub id: NodeId,
1216 pub node: Item_,
1217 pub vis: Visibility,
1218 pub span: Span,
1219 }
1220
1221 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1222 pub enum Item_ {
1223 /// An`extern crate` item, with optional original crate name,
1224 ///
1225 /// e.g. `extern crate foo` or `extern crate foo_bar as foo`
1226 ItemExternCrate(Option<Name>),
1227 /// A `use` or `pub use` item
1228 ItemUse(P<ViewPath>),
1229
1230 /// A `static` item
1231 ItemStatic(P<Ty>, Mutability, P<Expr>),
1232 /// A `const` item
1233 ItemConst(P<Ty>, P<Expr>),
1234 /// A function declaration
1235 ItemFn(P<FnDecl>, Unsafety, Constness, Abi, Generics, P<Block>),
1236 /// A module
1237 ItemMod(Mod),
1238 /// An external module
1239 ItemForeignMod(ForeignMod),
1240 /// A type alias, e.g. `type Foo = Bar<u8>`
1241 ItemTy(P<Ty>, Generics),
1242 /// An enum definition, e.g. `enum Foo<A, B> {C<A>, D<B>}`
1243 ItemEnum(EnumDef, Generics),
1244 /// A struct definition, e.g. `struct Foo<A> {x: A}`
1245 ItemStruct(VariantData, Generics),
1246 /// Represents a Trait Declaration
1247 ItemTrait(Unsafety, Generics, TyParamBounds, Vec<P<TraitItem>>),
1248
1249 // Default trait implementations
1250 ///
1251 // `impl Trait for .. {}`
1252 ItemDefaultImpl(Unsafety, TraitRef),
1253 /// An implementation, eg `impl<A> Trait for Foo { .. }`
1254 ItemImpl(Unsafety,
1255 ImplPolarity,
1256 Generics,
1257 Option<TraitRef>, // (optional) trait this impl implements
1258 P<Ty>, // self
1259 Vec<P<ImplItem>>),
1260 }
1261
1262 impl Item_ {
1263 pub fn descriptive_variant(&self) -> &str {
1264 match *self {
1265 ItemExternCrate(..) => "extern crate",
1266 ItemUse(..) => "use",
1267 ItemStatic(..) => "static item",
1268 ItemConst(..) => "constant item",
1269 ItemFn(..) => "function",
1270 ItemMod(..) => "module",
1271 ItemForeignMod(..) => "foreign module",
1272 ItemTy(..) => "type alias",
1273 ItemEnum(..) => "enum",
1274 ItemStruct(..) => "struct",
1275 ItemTrait(..) => "trait",
1276 ItemImpl(..) |
1277 ItemDefaultImpl(..) => "item",
1278 }
1279 }
1280 }
1281
1282 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1283 pub struct ForeignItem {
1284 pub name: Name,
1285 pub attrs: Vec<Attribute>,
1286 pub node: ForeignItem_,
1287 pub id: NodeId,
1288 pub span: Span,
1289 pub vis: Visibility,
1290 }
1291
1292 /// An item within an `extern` block
1293 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1294 pub enum ForeignItem_ {
1295 /// A foreign function
1296 ForeignItemFn(P<FnDecl>, Generics),
1297 /// A foreign static item (`static ext: u8`), with optional mutability
1298 /// (the boolean is true when mutable)
1299 ForeignItemStatic(P<Ty>, bool),
1300 }
1301
1302 impl ForeignItem_ {
1303 pub fn descriptive_variant(&self) -> &str {
1304 match *self {
1305 ForeignItemFn(..) => "foreign function",
1306 ForeignItemStatic(..) => "foreign static item",
1307 }
1308 }
1309 }