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 use codemap::{Span, Spanned, DUMMY_SP};
16 use owned_slice::OwnedSlice;
17 use parse::token::{InternedString, special_idents, str_to_ident};
22 use std::option::Option;
24 use std::gc::{Gc, GC};
25 use serialize::{Encodable, Decodable, Encoder, Decoder};
27 /// A pointer abstraction. FIXME(eddyb) #10676 use Rc<T> in the future.
28 pub type P<T> = Gc<T>;
30 #[allow(non_snake_case_functions)]
31 /// Construct a P<T> from a T value.
32 pub fn P<T: 'static>(value: T) -> P<T> {
36 // FIXME #6993: in librustc, uses of "ident" should be replaced
39 // an identifier contains a Name (index into the interner
40 // table) and a SyntaxContext to track renaming and
41 // macro expansion per Flatt et al., "Macros
42 // That Work Together"
43 #[deriving(Clone, Hash, PartialOrd, Eq, Ord, Show)]
46 pub ctxt: SyntaxContext
50 /// Construct an identifier with the given name and an empty context:
51 pub fn new(name: Name) -> Ident { Ident {name: name, ctxt: EMPTY_CTXT}}
54 impl PartialEq for Ident {
55 fn eq(&self, other: &Ident) -> bool {
56 if self.ctxt == other.ctxt {
57 self.name == other.name
59 // IF YOU SEE ONE OF THESE FAILS: it means that you're comparing
60 // idents that have different contexts. You can't fix this without
61 // knowing whether the comparison should be hygienic or non-hygienic.
62 // if it should be non-hygienic (most things are), just compare the
63 // 'name' fields of the idents. Or, even better, replace the idents
66 // On the other hand, if the comparison does need to be hygienic,
67 // one example and its non-hygienic counterpart would be:
68 // syntax::parse::token::mtwt_token_eq
69 // syntax::ext::tt::macro_parser::token_name_eq
70 fail!("not allowed to compare these idents: {:?}, {:?}. \
71 Probably related to issue \\#6993", self, other);
74 fn ne(&self, other: &Ident) -> bool {
79 /// A SyntaxContext represents a chain of macro-expandings
80 /// and renamings. Each macro expansion corresponds to
83 // I'm representing this syntax context as an index into
84 // a table, in order to work around a compiler bug
85 // that's causing unreleased memory to cause core dumps
86 // and also perhaps to save some work in destructor checks.
87 // the special uint '0' will be used to indicate an empty
90 // this uint is a reference to a table stored in thread-local
92 pub type SyntaxContext = u32;
93 pub static EMPTY_CTXT : SyntaxContext = 0;
94 pub static ILLEGAL_CTXT : SyntaxContext = 1;
96 /// A name is a part of an identifier, representing a string or gensym. It's
97 /// the result of interning.
100 /// A mark represents a unique id associated with a macro expansion
103 impl<S: Encoder<E>, E> Encodable<S, E> for Ident {
104 fn encode(&self, s: &mut S) -> Result<(), E> {
105 s.emit_str(token::get_ident(*self).get())
109 impl<D:Decoder<E>, E> Decodable<D, E> for Ident {
110 fn decode(d: &mut D) -> Result<Ident, E> {
111 Ok(str_to_ident(try!(d.read_str()).as_slice()))
115 /// Function name (not all functions have names)
116 pub type FnIdent = Option<Ident>;
118 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
119 pub struct Lifetime {
125 // a "Path" is essentially Rust's notion of a name;
126 // for instance: std::cmp::PartialEq . It's represented
127 // as a sequence of identifiers, along with a bunch
128 // of supporting information.
129 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
132 /// A `::foo` path, is relative to the crate root rather than current
133 /// module (like paths in an import).
135 /// The segments in the path: the things separated by `::`.
136 pub segments: Vec<PathSegment> ,
139 /// A segment of a path: an identifier, an optional lifetime, and a set of
141 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
142 pub struct PathSegment {
143 /// The identifier portion of this path segment.
144 pub identifier: Ident,
145 /// The lifetime parameters for this path segment.
146 pub lifetimes: Vec<Lifetime>,
147 /// The type parameters for this path segment, if present.
148 pub types: OwnedSlice<P<Ty>>,
151 pub type CrateNum = u32;
153 pub type NodeId = u32;
155 #[deriving(Clone, Eq, Ord, PartialOrd, PartialEq, Encodable, Decodable, Hash, Show)]
161 /// Item definitions in the currently-compiled crate would have the CrateNum
162 /// LOCAL_CRATE in their DefId.
163 pub static LOCAL_CRATE: CrateNum = 0;
164 pub static CRATE_NODE_ID: NodeId = 0;
166 // When parsing and doing expansions, we initially give all AST nodes this AST
167 // node value. Then later, in the renumber pass, we renumber them to have
168 // small, positive ids.
169 pub static DUMMY_NODE_ID: NodeId = -1;
171 // The AST represents all type param bounds as types.
172 // typeck::collect::compute_bounds matches these against
173 // the "special" built-in traits (see middle::lang_items) and
174 // detects Copy, Send and Share.
175 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
176 pub enum TyParamBound {
177 TraitTyParamBound(TraitRef),
178 StaticRegionTyParamBound,
179 UnboxedFnTyParamBound(UnboxedFnTy),
180 OtherRegionTyParamBound(Span) // FIXME -- just here until work for #5723 lands
183 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
188 pub bounds: OwnedSlice<TyParamBound>,
189 pub default: Option<P<Ty>>,
193 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
194 pub struct Generics {
195 pub lifetimes: Vec<Lifetime>,
196 pub ty_params: OwnedSlice<TyParam>,
200 pub fn is_parameterized(&self) -> bool {
201 self.lifetimes.len() + self.ty_params.len() > 0
203 pub fn is_lt_parameterized(&self) -> bool {
204 self.lifetimes.len() > 0
206 pub fn is_type_parameterized(&self) -> bool {
207 self.ty_params.len() > 0
211 // The set of MetaItems that define the compilation environment of the crate,
212 // used to drive conditional compilation
213 pub type CrateConfig = Vec<Gc<MetaItem>>;
215 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
218 pub attrs: Vec<Attribute>,
219 pub config: CrateConfig,
223 pub type MetaItem = Spanned<MetaItem_>;
225 #[deriving(Clone, Encodable, Decodable, Eq, Hash)]
227 MetaWord(InternedString),
228 MetaList(InternedString, Vec<Gc<MetaItem>>),
229 MetaNameValue(InternedString, Lit),
232 // can't be derived because the MetaList requires an unordered comparison
233 impl PartialEq for MetaItem_ {
234 fn eq(&self, other: &MetaItem_) -> bool {
236 MetaWord(ref ns) => match *other {
237 MetaWord(ref no) => (*ns) == (*no),
240 MetaNameValue(ref ns, ref vs) => match *other {
241 MetaNameValue(ref no, ref vo) => {
242 (*ns) == (*no) && vs.node == vo.node
246 MetaList(ref ns, ref miss) => match *other {
247 MetaList(ref no, ref miso) => {
249 miss.iter().all(|mi| miso.iter().any(|x| x.node == mi.node))
257 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
259 pub view_items: Vec<ViewItem>,
260 pub stmts: Vec<Gc<Stmt>>,
261 pub expr: Option<Gc<Expr>>,
263 pub rules: BlockCheckMode,
267 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
274 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
275 pub struct FieldPat {
280 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
281 pub enum BindingMode {
282 BindByRef(Mutability),
283 BindByValue(Mutability),
286 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
290 // A PatIdent may either be a new bound variable,
291 // or a nullary enum (in which case the second field
293 // In the nullary enum case, the parser can't determine
294 // which it is. The resolver determines this, and
295 // records this pattern's NodeId in an auxiliary
296 // set (of "PatIdents that refer to nullary enums")
297 PatIdent(BindingMode, SpannedIdent, Option<Gc<Pat>>),
298 PatEnum(Path, Option<Vec<Gc<Pat>>>), /* "none" means a * pattern where
299 * we don't bind the fields to names */
300 PatStruct(Path, Vec<FieldPat>, bool),
301 PatTup(Vec<Gc<Pat>>),
303 PatRegion(Gc<Pat>), // reference pattern
305 PatRange(Gc<Expr>, Gc<Expr>),
306 // [a, b, ..i, y, z] is represented as
307 // PatVec(~[a, b], Some(i), ~[y, z])
308 PatVec(Vec<Gc<Pat>>, Option<Gc<Pat>>, Vec<Gc<Pat>>),
312 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
313 pub enum Mutability {
318 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
319 pub enum ExprVstore {
320 ExprVstoreUniq, // ~[1,2,3,4]
321 ExprVstoreSlice, // &[1,2,3,4]
322 ExprVstoreMutSlice, // &mut [1,2,3,4]
325 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
347 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
356 pub type Stmt = Spanned<Stmt_>;
358 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
360 // could be an item or a local (let) binding:
361 StmtDecl(Gc<Decl>, NodeId),
363 // expr without trailing semi-colon (must have unit type):
364 StmtExpr(Gc<Expr>, NodeId),
366 // expr with trailing semi-colon (may have any type):
367 StmtSemi(Gc<Expr>, NodeId),
369 // bool: is there a trailing sem-colon?
373 /// Where a local declaration came from: either a true `let ... =
374 /// ...;`, or one desugared from the pattern of a for loop.
375 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
376 pub enum LocalSource {
381 // FIXME (pending discussion of #1697, #2178...): local should really be
382 // a refinement on pat.
383 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
384 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash)]
388 pub init: Option<Gc<Expr>>,
391 pub source: LocalSource,
394 pub type Decl = Spanned<Decl_>;
396 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash)]
398 // a local (let) binding:
399 DeclLocal(Gc<Local>),
404 /// represents one arm of a 'match'
405 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
407 pub attrs: Vec<Attribute>,
408 pub pats: Vec<Gc<Pat>>,
409 pub guard: Option<Gc<Expr>>,
413 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
415 pub ident: SpannedIdent,
420 pub type SpannedIdent = Spanned<Ident>;
422 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
423 pub enum BlockCheckMode {
425 UnsafeBlock(UnsafeSource),
428 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
429 pub enum UnsafeSource {
434 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
441 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
443 ExprVstore(Gc<Expr>, ExprVstore),
444 // First expr is the place; second expr is the value.
445 ExprBox(Gc<Expr>, Gc<Expr>),
446 ExprVec(Vec<Gc<Expr>>),
447 ExprCall(Gc<Expr>, Vec<Gc<Expr>>),
448 ExprMethodCall(SpannedIdent, Vec<P<Ty>>, Vec<Gc<Expr>>),
449 ExprTup(Vec<Gc<Expr>>),
450 ExprBinary(BinOp, Gc<Expr>, Gc<Expr>),
451 ExprUnary(UnOp, Gc<Expr>),
453 ExprCast(Gc<Expr>, P<Ty>),
454 ExprIf(Gc<Expr>, P<Block>, Option<Gc<Expr>>),
455 ExprWhile(Gc<Expr>, P<Block>),
456 // FIXME #6993: change to Option<Name>
457 ExprForLoop(Gc<Pat>, Gc<Expr>, P<Block>, Option<Ident>),
458 // Conditionless loop (can be exited with break, cont, or ret)
459 // FIXME #6993: change to Option<Name>
460 ExprLoop(P<Block>, Option<Ident>),
461 ExprMatch(Gc<Expr>, Vec<Arm>),
462 ExprFnBlock(P<FnDecl>, P<Block>),
463 ExprProc(P<FnDecl>, P<Block>),
466 ExprAssign(Gc<Expr>, Gc<Expr>),
467 ExprAssignOp(BinOp, Gc<Expr>, Gc<Expr>),
468 ExprField(Gc<Expr>, SpannedIdent, Vec<P<Ty>>),
469 ExprIndex(Gc<Expr>, Gc<Expr>),
471 /// Expression that looks like a "name". For example,
472 /// `std::slice::from_elem::<uint>` is an ExprPath that's the "name" part
473 /// of a function call.
476 ExprAddrOf(Mutability, Gc<Expr>),
477 ExprBreak(Option<Ident>),
478 ExprAgain(Option<Ident>),
479 ExprRet(Option<Gc<Expr>>),
481 ExprInlineAsm(InlineAsm),
485 // A struct literal expression.
486 ExprStruct(Path, Vec<Field> , Option<Gc<Expr>> /* base */),
488 // A vector literal constructed from one repeated element.
489 ExprRepeat(Gc<Expr> /* element */, Gc<Expr> /* count */),
491 // No-op: used solely so we can pretty-print faithfully
495 // When the main rust parser encounters a syntax-extension invocation, it
496 // parses the arguments to the invocation as a token-tree. This is a very
497 // loose structure, such that all sorts of different AST-fragments can
498 // be passed to syntax extensions using a uniform type.
500 // If the syntax extension is an MBE macro, it will attempt to match its
501 // LHS "matchers" against the provided token tree, and if it finds a
502 // match, will transcribe the RHS token tree, splicing in any captured
503 // macro_parser::matched_nonterminals into the TTNonterminals it finds.
505 // The RHS of an MBE macro is the only place a TTNonterminal or TTSeq
506 // makes any real sense. You could write them elsewhere but nothing
507 // else knows what to do with them, so you'll probably get a syntax
510 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
511 #[doc="For macro invocations; parsing is delegated to the macro"]
514 TTTok(Span, ::parse::token::Token),
515 // a delimited sequence (the delimiters appear as the first
516 // and last elements of the vector)
517 // FIXME(eddyb) #6308 Use Rc<[TokenTree]> after DST.
518 TTDelim(Rc<Vec<TokenTree>>),
520 // These only make sense for right-hand-sides of MBE macros:
522 // a kleene-style repetition sequence with a span, a TTForest,
523 // an optional separator, and a boolean where true indicates
524 // zero or more (..), and false indicates one or more (+).
525 // FIXME(eddyb) #6308 Use Rc<[TokenTree]> after DST.
526 TTSeq(Span, Rc<Vec<TokenTree>>, Option<::parse::token::Token>, bool),
528 // a syntactic variable that will be filled in by macro expansion.
529 TTNonterminal(Span, Ident)
533 // Matchers are nodes defined-by and recognized-by the main rust parser and
534 // language, but they're only ever found inside syntax-extension invocations;
535 // indeed, the only thing that ever _activates_ the rules in the rust parser
536 // for parsing a matcher is a matcher looking for the 'matchers' nonterminal
537 // itself. Matchers represent a small sub-language for pattern-matching
538 // token-trees, and are thus primarily used by the macro-defining extension
544 // A matcher that matches a single token, denoted by the token itself. So
545 // long as there's no $ involved.
551 // A matcher that matches a sequence of sub-matchers, denoted various
554 // $(M)* zero or more Ms
555 // $(M)+ one or more Ms
556 // $(M),+ one or more comma-separated Ms
557 // $(A B C);* zero or more semi-separated 'A B C' seqs
563 // A matcher that matches one of a few interesting named rust
564 // nonterminals, such as types, expressions, items, or raw token-trees. A
565 // black-box matcher on expr, for example, binds an expr to a given ident,
566 // and that ident can re-occur as an interpolation in the RHS of a
567 // macro-by-example rule. For example:
569 // $foo:expr => 1 + $foo // interpolate an expr
570 // $foo:tt => $foo // interpolate a token-tree
571 // $foo:tt => bar! $foo // only other valid interpolation
572 // // is in arg position for another
575 // As a final, horrifying aside, note that macro-by-example's input is
576 // also matched by one of these matchers. Holy self-referential! It is matched
577 // by a MatchSeq, specifically this one:
579 // $( $lhs:matchers => $rhs:tt );+
581 // If you understand that, you have closed to loop and understand the whole
582 // macro system. Congratulations.
584 pub type Matcher = Spanned<Matcher_>;
586 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
589 MatchTok(::parse::token::Token),
590 // match repetitions of a sequence: body, separator, zero ok?,
591 // lo, hi position-in-match-array used:
592 MatchSeq(Vec<Matcher> , Option<::parse::token::Token>, bool, uint, uint),
593 // parse a Rust NT: name to bind, name of NT, position in match array:
594 MatchNonterminal(Ident, Ident, uint)
597 pub type Mac = Spanned<Mac_>;
599 // represents a macro invocation. The Path indicates which macro
600 // is being invoked, and the vector of token-trees contains the source
601 // of the macro invocation.
602 // There's only one flavor, now, so this could presumably be simplified.
603 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
605 MacInvocTT(Path, Vec<TokenTree> , SyntaxContext), // new macro-invocation
608 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
614 pub type Lit = Spanned<Lit_>;
616 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
618 LitStr(InternedString, StrStyle),
619 LitBinary(Rc<Vec<u8> >),
623 LitUint(u64, UintTy),
624 LitIntUnsuffixed(i64),
625 LitFloat(InternedString, FloatTy),
626 LitFloatUnsuffixed(InternedString),
631 // NB: If you change this, you'll probably want to change the corresponding
632 // type structure in middle/ty.rs as well.
633 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
636 pub mutbl: Mutability,
639 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash)]
640 pub struct TypeField {
646 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
647 pub struct TypeMethod {
649 pub attrs: Vec<Attribute>,
650 pub fn_style: FnStyle,
652 pub generics: Generics,
653 pub explicit_self: ExplicitSelf,
659 // A trait method is either required (meaning it doesn't have an
660 // implementation, just a signature) or provided (meaning it has a default
662 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
663 pub enum TraitMethod {
664 Required(TypeMethod),
665 Provided(Gc<Method>),
668 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
677 impl fmt::Show for IntTy {
678 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
679 write!(f, "{}", ast_util::int_ty_to_str(*self, None))
683 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
692 impl fmt::Show for UintTy {
693 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
694 write!(f, "{}", ast_util::uint_ty_to_str(*self, None))
698 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
704 impl fmt::Show for FloatTy {
705 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
706 write!(f, "{}", ast_util::float_ty_to_str(*self))
710 // NB PartialEq method appears below.
711 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
718 // Not represented directly in the AST, referred to by name through a ty_path.
719 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
729 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
735 impl fmt::Show for Onceness {
736 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
738 Once => "once".fmt(f),
739 Many => "many".fmt(f),
744 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash)]
745 pub struct ClosureTy {
746 pub lifetimes: Vec<Lifetime>,
747 pub fn_style: FnStyle,
748 pub onceness: Onceness,
750 // Optional optvec distinguishes between "fn()" and "fn:()" so we can
751 // implement issue #7264. None means "fn()", which means infer a default
752 // bound based on pointer sigil during typeck. Some(Empty) means "fn:()",
753 // which means use no bounds (e.g., not even Owned on a ~fn()).
754 pub bounds: Option<OwnedSlice<TyParamBound>>,
757 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash)]
758 pub struct BareFnTy {
759 pub fn_style: FnStyle,
761 pub lifetimes: Vec<Lifetime>,
765 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
766 pub struct UnboxedFnTy {
770 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
773 TyBot, /* bottom type */
777 TyFixedLengthVec(P<Ty>, Gc<Expr>),
779 TyRptr(Option<Lifetime>, MutTy),
780 TyClosure(Gc<ClosureTy>, Option<Lifetime>),
781 TyProc(Gc<ClosureTy>),
782 TyBareFn(Gc<BareFnTy>),
783 TyUnboxedFn(Gc<UnboxedFnTy>),
785 TyPath(Path, Option<OwnedSlice<TyParamBound>>, NodeId), // for #7264; see above
786 // No-op; kept solely so that we can pretty-print faithfully
789 // TyInfer means the type should be inferred instead of it having been
790 // specified. This can appear anywhere in a type.
794 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
795 pub enum AsmDialect {
800 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
801 pub struct InlineAsm {
802 pub asm: InternedString,
803 pub asm_str_style: StrStyle,
804 pub clobbers: InternedString,
805 pub inputs: Vec<(InternedString, Gc<Expr>)>,
806 pub outputs: Vec<(InternedString, Gc<Expr>)>,
808 pub alignstack: bool,
809 pub dialect: AsmDialect
812 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
820 pub fn new_self(span: Span, mutability: Mutability) -> Arg {
821 let path = Spanned{span:span,node:special_idents::self_};
823 // HACK(eddyb) fake type for the self argument.
831 node: PatIdent(BindByValue(mutability), path, None),
839 // represents the header (not the body) of a function declaration
840 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
842 pub inputs: Vec<Arg>,
848 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
850 UnsafeFn, // declared with "unsafe fn"
851 NormalFn, // declared with "fn"
854 impl fmt::Show for FnStyle {
855 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
857 NormalFn => "normal".fmt(f),
858 UnsafeFn => "unsafe".fmt(f),
863 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
865 NoReturn, // functions with return type _|_ that always
866 // raise an error or exit (i.e. never return to the caller)
867 Return, // everything else
870 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
871 pub enum ExplicitSelf_ {
872 SelfStatic, // no self
874 SelfRegion(Option<Lifetime>, Mutability), // `&'lt self`, `&'lt mut self`
878 pub type ExplicitSelf = Spanned<ExplicitSelf_>;
880 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash)]
883 pub attrs: Vec<Attribute>,
884 pub generics: Generics,
885 pub explicit_self: ExplicitSelf,
886 pub fn_style: FnStyle,
894 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
896 /// A span from the first token past `{` to the last token until `}`.
897 /// For `mod foo;`, the inner span ranges from the first token
898 /// to the last token in the external file.
900 pub view_items: Vec<ViewItem>,
901 pub items: Vec<Gc<Item>>,
904 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
905 pub struct ForeignMod {
907 pub view_items: Vec<ViewItem>,
908 pub items: Vec<Gc<ForeignItem>>,
911 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
912 pub struct VariantArg {
917 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
918 pub enum VariantKind {
919 TupleVariantKind(Vec<VariantArg>),
920 StructVariantKind(Gc<StructDef>),
923 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
925 pub variants: Vec<P<Variant>>,
928 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
929 pub struct Variant_ {
931 pub attrs: Vec<Attribute>,
932 pub kind: VariantKind,
934 pub disr_expr: Option<Gc<Expr>>,
938 pub type Variant = Spanned<Variant_>;
940 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
941 pub struct PathListIdent_ {
946 pub type PathListIdent = Spanned<PathListIdent_>;
948 pub type ViewPath = Spanned<ViewPath_>;
950 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash)]
953 // quux = foo::bar::baz
957 // foo::bar::baz (with 'baz =' implicitly on the left)
958 ViewPathSimple(Ident, Path, NodeId),
961 ViewPathGlob(Path, NodeId),
964 ViewPathList(Path, Vec<PathListIdent> , NodeId)
967 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
968 pub struct ViewItem {
970 pub attrs: Vec<Attribute>,
975 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
977 // ident: name used to refer to this crate in the code
978 // optional (InternedString,StrStyle): if present, this is a location
979 // (containing arbitrary characters) from which to fetch the crate sources
980 // For example, extern crate whatever = "github.com/rust-lang/rust"
981 ViewItemExternCrate(Ident, Option<(InternedString,StrStyle)>, NodeId),
982 ViewItemUse(Gc<ViewPath>),
985 // Meta-data associated with an item
986 pub type Attribute = Spanned<Attribute_>;
988 // Distinguishes between Attributes that decorate items and Attributes that
989 // are contained as statements within items. These two cases need to be
990 // distinguished for pretty-printing.
991 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
997 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
998 pub struct AttrId(pub uint);
1000 // doc-comments are promoted to attributes that have is_sugared_doc = true
1001 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
1002 pub struct Attribute_ {
1004 pub style: AttrStyle,
1005 pub value: Gc<MetaItem>,
1006 pub is_sugared_doc: bool,
1010 TraitRef's appear in impls.
1011 resolve maps each TraitRef's ref_id to its defining trait; that's all
1012 that the ref_id is for. The impl_id maps to the "self type" of this impl.
1013 If this impl is an ItemImpl, the impl_id is redundant (it could be the
1014 same as the impl's node id).
1016 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
1017 pub struct TraitRef {
1022 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
1023 pub enum Visibility {
1029 pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility {
1031 &Inherited => parent_visibility,
1037 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
1043 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
1044 pub struct StructField_ {
1045 pub kind: StructFieldKind,
1048 pub attrs: Vec<Attribute>,
1052 pub fn ident(&self) -> Option<Ident> {
1054 NamedField(ref ident, _) => Some(ident.clone()),
1055 UnnamedField(_) => None
1060 pub type StructField = Spanned<StructField_>;
1062 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
1063 pub enum StructFieldKind {
1064 NamedField(Ident, Visibility),
1065 UnnamedField(Visibility), // element of a tuple-like struct
1068 impl StructFieldKind {
1069 pub fn is_unnamed(&self) -> bool {
1071 UnnamedField(..) => true,
1072 NamedField(..) => false,
1077 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash)]
1078 pub struct StructDef {
1079 pub fields: Vec<StructField>, /* fields, not including ctor */
1080 /* ID of the constructor. This is only used for tuple- or enum-like
1082 pub ctor_id: Option<NodeId>,
1083 pub super_struct: Option<P<Ty>>, // Super struct, if specified.
1084 pub is_virtual: bool, // True iff the struct may be inherited from.
1088 FIXME (#3300): Should allow items to be anonymous. Right now
1089 we just use dummy names for anon items.
1091 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
1094 pub attrs: Vec<Attribute>,
1097 pub vis: Visibility,
1101 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
1103 ItemStatic(P<Ty>, Mutability, Gc<Expr>),
1104 ItemFn(P<FnDecl>, FnStyle, Abi, Generics, P<Block>),
1106 ItemForeignMod(ForeignMod),
1107 ItemTy(P<Ty>, Generics),
1108 ItemEnum(EnumDef, Generics),
1109 ItemStruct(Gc<StructDef>, Generics),
1110 ItemTrait(Generics, Sized, Vec<TraitRef> , Vec<TraitMethod> ),
1112 Option<TraitRef>, // (optional) trait this impl implements
1115 // a macro invocation (which includes macro definition)
1119 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash)]
1120 pub struct ForeignItem {
1122 pub attrs: Vec<Attribute>,
1123 pub node: ForeignItem_,
1126 pub vis: Visibility,
1129 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash)]
1130 pub enum ForeignItem_ {
1131 ForeignItemFn(P<FnDecl>, Generics),
1132 ForeignItemStatic(P<Ty>, /* is_mutbl */ bool),
1135 // The data we save and restore about an inlined item or method. This is not
1136 // part of the AST that we parse from a file, but it becomes part of the tree
1138 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash)]
1139 pub enum InlinedItem {
1141 IIMethod(DefId /* impl id */, bool /* is provided */, Gc<Method>),
1142 IIForeign(Gc<ForeignItem>),
1147 use serialize::json;
1152 // are ASTs encodable?
1154 fn check_asts_encodable() {
1163 view_items: Vec::new(),
1174 // doesn't matter which encoder we use....
1175 let _f = &e as &serialize::Encodable<json::Encoder, io::IoError>;