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};
17 use parse::token::{InternedString, special_idents, str_to_ident};
22 use std::cell::RefCell;
23 use collections::HashMap;
24 use std::option::Option;
26 use serialize::{Encodable, Decodable, Encoder, Decoder};
28 /// A pointer abstraction. FIXME(eddyb) #10676 use Rc<T> in the future.
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, TotalEq, TotalOrd, Show)]
44 pub struct Ident { name: Name, ctxt: SyntaxContext }
47 /// Construct an identifier with the given name and an empty context:
48 pub fn new(name: Name) -> Ident { Ident {name: name, ctxt: EMPTY_CTXT}}
52 fn eq(&self, other: &Ident) -> bool {
53 if self.ctxt == other.ctxt {
54 self.name == other.name
56 // IF YOU SEE ONE OF THESE FAILS: it means that you're comparing
57 // idents that have different contexts. You can't fix this without
58 // knowing whether the comparison should be hygienic or non-hygienic.
59 // if it should be non-hygienic (most things are), just compare the
60 // 'name' fields of the idents. Or, even better, replace the idents
63 // On the other hand, if the comparison does need to be hygienic,
64 // one example and its non-hygienic counterpart would be:
65 // syntax::parse::token::mtwt_token_eq
66 // syntax::ext::tt::macro_parser::token_name_eq
67 fail!("not allowed to compare these idents: {:?}, {:?}. \
68 Probably related to issue \\#6993", self, other);
71 fn ne(&self, other: &Ident) -> bool {
76 /// A SyntaxContext represents a chain of macro-expandings
77 /// and renamings. Each macro expansion corresponds to
80 // I'm representing this syntax context as an index into
81 // a table, in order to work around a compiler bug
82 // that's causing unreleased memory to cause core dumps
83 // and also perhaps to save some work in destructor checks.
84 // the special uint '0' will be used to indicate an empty
87 // this uint is a reference to a table stored in thread-local
89 pub type SyntaxContext = u32;
91 // the SCTable contains a table of SyntaxContext_'s. It
92 // represents a flattened tree structure, to avoid having
93 // managed pointers everywhere (that caused an ICE).
94 // the mark_memo and rename_memo fields are side-tables
95 // that ensure that adding the same mark to the same context
96 // gives you back the same context as before. This shouldn't
97 // change the semantics--everything here is immutable--but
98 // it should cut down on memory use *a lot*; applying a mark
99 // to a tree containing 50 identifiers would otherwise generate
101 table: RefCell<Vec<SyntaxContext_> >,
102 mark_memo: RefCell<HashMap<(SyntaxContext,Mrk),SyntaxContext>>,
103 rename_memo: RefCell<HashMap<(SyntaxContext,Ident,Name),SyntaxContext>>,
106 // NB: these must be placed in any SCTable...
107 pub static EMPTY_CTXT : SyntaxContext = 0;
108 pub static ILLEGAL_CTXT : SyntaxContext = 1;
110 #[deriving(Eq, Encodable, Decodable, Hash)]
111 pub enum SyntaxContext_ {
113 Mark (Mrk,SyntaxContext),
114 // flattening the name and syntaxcontext into the rename...
115 // HIDDEN INVARIANTS:
116 // 1) the first name in a Rename node
117 // can only be a programmer-supplied name.
118 // 2) Every Rename node with a given Name in the
119 // "to" slot must have the same name and context
120 // in the "from" slot. In essence, they're all
121 // pointers to a single "rename" event node.
122 Rename (Ident,Name,SyntaxContext),
123 // actually, IllegalCtxt may not be necessary.
127 /// A name is a part of an identifier, representing a string or gensym. It's
128 /// the result of interning.
131 /// A mark represents a unique id associated with a macro expansion
134 impl<S: Encoder> Encodable<S> for Ident {
135 fn encode(&self, s: &mut S) {
136 s.emit_str(token::get_ident(*self).get());
140 impl<D:Decoder> Decodable<D> for Ident {
141 fn decode(d: &mut D) -> Ident {
142 str_to_ident(d.read_str())
146 /// Function name (not all functions have names)
147 pub type FnIdent = Option<Ident>;
149 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
150 pub struct Lifetime {
156 // a "Path" is essentially Rust's notion of a name;
157 // for instance: std::cmp::Eq . It's represented
158 // as a sequence of identifiers, along with a bunch
159 // of supporting information.
160 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
163 /// A `::foo` path, is relative to the crate root rather than current
164 /// module (like paths in an import).
166 /// The segments in the path: the things separated by `::`.
167 segments: Vec<PathSegment> ,
170 /// A segment of a path: an identifier, an optional lifetime, and a set of
172 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
173 pub struct PathSegment {
174 /// The identifier portion of this path segment.
176 /// The lifetime parameters for this path segment.
177 lifetimes: OptVec<Lifetime>,
178 /// The type parameters for this path segment, if present.
179 types: OptVec<P<Ty>>,
182 pub type CrateNum = u32;
184 pub type NodeId = u32;
186 #[deriving(Clone, TotalEq, TotalOrd, Eq, Encodable, Decodable, Hash, Show)]
192 /// Item definitions in the currently-compiled crate would have the CrateNum
193 /// LOCAL_CRATE in their DefId.
194 pub static LOCAL_CRATE: CrateNum = 0;
195 pub static CRATE_NODE_ID: NodeId = 0;
197 // When parsing and doing expansions, we initially give all AST nodes this AST
198 // node value. Then later, in the renumber pass, we renumber them to have
199 // small, positive ids.
200 pub static DUMMY_NODE_ID: NodeId = -1;
202 // The AST represents all type param bounds as types.
203 // typeck::collect::compute_bounds matches these against
204 // the "special" built-in traits (see middle::lang_items) and
205 // detects Copy, Send, Send, and Freeze.
206 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
207 pub enum TyParamBound {
208 TraitTyParamBound(TraitRef),
212 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
216 bounds: OptVec<TyParamBound>,
217 default: Option<P<Ty>>
220 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
221 pub struct Generics {
222 lifetimes: OptVec<Lifetime>,
223 ty_params: OptVec<TyParam>,
227 pub fn is_parameterized(&self) -> bool {
228 self.lifetimes.len() + self.ty_params.len() > 0
230 pub fn is_lt_parameterized(&self) -> bool {
231 self.lifetimes.len() > 0
233 pub fn is_type_parameterized(&self) -> bool {
234 self.ty_params.len() > 0
238 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
239 pub enum MethodProvenance {
244 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
246 DefFn(DefId, Purity),
247 DefStaticMethod(/* method */ DefId, MethodProvenance, Purity),
248 DefSelfTy(/* trait id */ NodeId),
250 DefForeignMod(DefId),
251 DefStatic(DefId, bool /* is_mutbl */),
252 DefArg(NodeId, BindingMode),
253 DefLocal(NodeId, BindingMode),
254 DefVariant(DefId /* enum */, DefId /* variant */, bool /* is_structure */),
258 DefTyParam(DefId, uint),
259 DefBinding(NodeId, BindingMode),
261 DefUpvar(NodeId, // id of closed over var
262 @Def, // closed over def
263 NodeId, // expr node that creates the closure
264 NodeId), // id for the block/body of the closure expr
266 /// Note that if it's a tuple struct's definition, the node id of the DefId
267 /// may either refer to the item definition's id or the StructDef.ctor_id.
269 /// The cases that I have encountered so far are (this is not exhaustive):
270 /// - If it's a ty_path referring to some tuple struct, then DefMap maps
271 /// it to a def whose id is the item definition's id.
272 /// - If it's an ExprPath referring to some tuple struct, then DefMap maps
273 /// it to a def whose id is the StructDef.ctor_id.
275 DefTyParamBinder(NodeId), /* struct, impl or trait with ty params */
278 DefMethod(DefId /* method */, Option<DefId> /* trait */),
281 #[deriving(Clone, Eq, Hash, Encodable, Decodable, Show)]
284 DefEarlyBoundRegion(/* index */ uint, /* lifetime decl */ NodeId),
285 DefLateBoundRegion(/* binder_id */ NodeId, /* depth */ uint, /* lifetime decl */ NodeId),
286 DefFreeRegion(/* block scope */ NodeId, /* lifetime decl */ NodeId),
289 // The set of MetaItems that define the compilation environment of the crate,
290 // used to drive conditional compilation
291 pub type CrateConfig = Vec<@MetaItem> ;
293 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
296 attrs: Vec<Attribute> ,
301 pub type MetaItem = Spanned<MetaItem_>;
303 #[deriving(Clone, Encodable, Decodable, Hash)]
305 MetaWord(InternedString),
306 MetaList(InternedString, Vec<@MetaItem> ),
307 MetaNameValue(InternedString, Lit),
310 // can't be derived because the MetaList requires an unordered comparison
311 impl Eq for MetaItem_ {
312 fn eq(&self, other: &MetaItem_) -> bool {
314 MetaWord(ref ns) => match *other {
315 MetaWord(ref no) => (*ns) == (*no),
318 MetaNameValue(ref ns, ref vs) => match *other {
319 MetaNameValue(ref no, ref vo) => {
320 (*ns) == (*no) && vs.node == vo.node
324 MetaList(ref ns, ref miss) => match *other {
325 MetaList(ref no, ref miso) => {
327 miss.iter().all(|mi| miso.iter().any(|x| x.node == mi.node))
335 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
337 view_items: Vec<ViewItem> ,
341 rules: BlockCheckMode,
345 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
352 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
353 pub struct FieldPat {
358 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
359 pub enum BindingMode {
360 BindByRef(Mutability),
361 BindByValue(Mutability),
364 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
368 // A PatIdent may either be a new bound variable,
369 // or a nullary enum (in which case the second field
371 // In the nullary enum case, the parser can't determine
372 // which it is. The resolver determines this, and
373 // records this pattern's NodeId in an auxiliary
374 // set (of "pat_idents that refer to nullary enums")
375 PatIdent(BindingMode, Path, Option<@Pat>),
376 PatEnum(Path, Option<Vec<@Pat> >), /* "none" means a * pattern where
377 * we don't bind the fields to names */
378 PatStruct(Path, Vec<FieldPat> , bool),
381 PatRegion(@Pat), // reference pattern
383 PatRange(@Expr, @Expr),
384 // [a, b, ..i, y, z] is represented as
385 // PatVec(~[a, b], Some(i), ~[y, z])
386 PatVec(Vec<@Pat> , Option<@Pat>, Vec<@Pat> )
389 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
390 pub enum Mutability {
395 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
402 impl fmt::Show for Sigil {
403 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
405 BorrowedSigil => "&".fmt(f),
406 OwnedSigil => "~".fmt(f),
407 ManagedSigil => "@".fmt(f),
412 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
413 pub enum ExprVstore {
414 ExprVstoreUniq, // ~[1,2,3,4]
415 ExprVstoreSlice, // &[1,2,3,4]
416 ExprVstoreMutSlice, // &mut [1,2,3,4]
419 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
441 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
450 pub type Stmt = Spanned<Stmt_>;
452 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
454 // could be an item or a local (let) binding:
455 StmtDecl(@Decl, NodeId),
457 // expr without trailing semi-colon (must have unit type):
458 StmtExpr(@Expr, NodeId),
460 // expr with trailing semi-colon (may have any type):
461 StmtSemi(@Expr, NodeId),
463 // bool: is there a trailing sem-colon?
467 // FIXME (pending discussion of #1697, #2178...): local should really be
468 // a refinement on pat.
469 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
470 #[deriving(Eq, Encodable, Decodable, Hash)]
479 pub type Decl = Spanned<Decl_>;
481 #[deriving(Eq, Encodable, Decodable, Hash)]
483 // a local (let) binding:
489 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
492 guard: Option<@Expr>,
496 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
503 pub type SpannedIdent = Spanned<Ident>;
505 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
506 pub enum BlockCheckMode {
508 UnsafeBlock(UnsafeSource),
511 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
512 pub enum UnsafeSource {
517 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
524 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
526 ExprVstore(@Expr, ExprVstore),
527 // First expr is the place; second expr is the value.
528 ExprBox(@Expr, @Expr),
529 ExprVec(Vec<@Expr> , Mutability),
530 ExprCall(@Expr, Vec<@Expr> ),
531 ExprMethodCall(Ident, Vec<P<Ty>> , Vec<@Expr> ),
532 ExprTup(Vec<@Expr> ),
533 ExprBinary(BinOp, @Expr, @Expr),
534 ExprUnary(UnOp, @Expr),
536 ExprCast(@Expr, P<Ty>),
537 ExprIf(@Expr, P<Block>, Option<@Expr>),
538 ExprWhile(@Expr, P<Block>),
539 // FIXME #6993: change to Option<Name>
540 ExprForLoop(@Pat, @Expr, P<Block>, Option<Ident>),
541 // Conditionless loop (can be exited with break, cont, or ret)
542 // FIXME #6993: change to Option<Name>
543 ExprLoop(P<Block>, Option<Ident>),
544 ExprMatch(@Expr, Vec<Arm> ),
545 ExprFnBlock(P<FnDecl>, P<Block>),
546 ExprProc(P<FnDecl>, P<Block>),
549 ExprAssign(@Expr, @Expr),
550 ExprAssignOp(BinOp, @Expr, @Expr),
551 ExprField(@Expr, Ident, Vec<P<Ty>> ),
552 ExprIndex(@Expr, @Expr),
554 /// Expression that looks like a "name". For example,
555 /// `std::vec::from_elem::<uint>` is an ExprPath that's the "name" part
556 /// of a function call.
559 ExprAddrOf(Mutability, @Expr),
560 ExprBreak(Option<Ident>),
561 ExprAgain(Option<Ident>),
562 ExprRet(Option<@Expr>),
564 /// Gets the log level for the enclosing module
567 ExprInlineAsm(InlineAsm),
571 // A struct literal expression.
572 ExprStruct(Path, Vec<Field> , Option<@Expr> /* base */),
574 // A vector literal constructed from one repeated element.
575 ExprRepeat(@Expr /* element */, @Expr /* count */, Mutability),
577 // No-op: used solely so we can pretty-print faithfully
581 // When the main rust parser encounters a syntax-extension invocation, it
582 // parses the arguments to the invocation as a token-tree. This is a very
583 // loose structure, such that all sorts of different AST-fragments can
584 // be passed to syntax extensions using a uniform type.
586 // If the syntax extension is an MBE macro, it will attempt to match its
587 // LHS "matchers" against the provided token tree, and if it finds a
588 // match, will transcribe the RHS token tree, splicing in any captured
589 // macro_parser::matched_nonterminals into the TTNonterminals it finds.
591 // The RHS of an MBE macro is the only place a TTNonterminal or TTSeq
592 // makes any real sense. You could write them elsewhere but nothing
593 // else knows what to do with them, so you'll probably get a syntax
596 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
597 #[doc="For macro invocations; parsing is delegated to the macro"]
600 TTTok(Span, ::parse::token::Token),
601 // a delimited sequence (the delimiters appear as the first
602 // and last elements of the vector)
603 TTDelim(@Vec<TokenTree> ),
605 // These only make sense for right-hand-sides of MBE macros:
607 // a kleene-style repetition sequence with a span, a TTForest,
608 // an optional separator, and a boolean where true indicates
609 // zero or more (..), and false indicates one or more (+).
610 TTSeq(Span, @Vec<TokenTree> , Option<::parse::token::Token>, bool),
612 // a syntactic variable that will be filled in by macro expansion.
613 TTNonterminal(Span, Ident)
617 // Matchers are nodes defined-by and recognized-by the main rust parser and
618 // language, but they're only ever found inside syntax-extension invocations;
619 // indeed, the only thing that ever _activates_ the rules in the rust parser
620 // for parsing a matcher is a matcher looking for the 'matchers' nonterminal
621 // itself. Matchers represent a small sub-language for pattern-matching
622 // token-trees, and are thus primarily used by the macro-defining extension
628 // A matcher that matches a single token, denoted by the token itself. So
629 // long as there's no $ involved.
635 // A matcher that matches a sequence of sub-matchers, denoted various
638 // $(M)* zero or more Ms
639 // $(M)+ one or more Ms
640 // $(M),+ one or more comma-separated Ms
641 // $(A B C);* zero or more semi-separated 'A B C' seqs
647 // A matcher that matches one of a few interesting named rust
648 // nonterminals, such as types, expressions, items, or raw token-trees. A
649 // black-box matcher on expr, for example, binds an expr to a given ident,
650 // and that ident can re-occur as an interpolation in the RHS of a
651 // macro-by-example rule. For example:
653 // $foo:expr => 1 + $foo // interpolate an expr
654 // $foo:tt => $foo // interpolate a token-tree
655 // $foo:tt => bar! $foo // only other valid interpolation
656 // // is in arg position for another
659 // As a final, horrifying aside, note that macro-by-example's input is
660 // also matched by one of these matchers. Holy self-referential! It is matched
661 // by a MatchSeq, specifically this one:
663 // $( $lhs:matchers => $rhs:tt );+
665 // If you understand that, you have closed to loop and understand the whole
666 // macro system. Congratulations.
668 pub type Matcher = Spanned<Matcher_>;
670 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
673 MatchTok(::parse::token::Token),
674 // match repetitions of a sequence: body, separator, zero ok?,
675 // lo, hi position-in-match-array used:
676 MatchSeq(Vec<Matcher> , Option<::parse::token::Token>, bool, uint, uint),
677 // parse a Rust NT: name to bind, name of NT, position in match array:
678 MatchNonterminal(Ident, Ident, uint)
681 pub type Mac = Spanned<Mac_>;
683 // represents a macro invocation. The Path indicates which macro
684 // is being invoked, and the vector of token-trees contains the source
685 // of the macro invocation.
686 // There's only one flavor, now, so this could presumably be simplified.
687 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
689 MacInvocTT(Path, Vec<TokenTree> , SyntaxContext), // new macro-invocation
692 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
698 pub type Lit = Spanned<Lit_>;
700 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
702 LitStr(InternedString, StrStyle),
703 LitBinary(Rc<Vec<u8> >),
706 LitUint(u64, UintTy),
707 LitIntUnsuffixed(i64),
708 LitFloat(InternedString, FloatTy),
709 LitFloatUnsuffixed(InternedString),
714 // NB: If you change this, you'll probably want to change the corresponding
715 // type structure in middle/ty.rs as well.
716 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
722 #[deriving(Eq, Encodable, Decodable, Hash)]
723 pub struct TypeField {
729 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
730 pub struct TypeMethod {
732 attrs: Vec<Attribute> ,
736 explicit_self: ExplicitSelf,
741 // A trait method is either required (meaning it doesn't have an
742 // implementation, just a signature) or provided (meaning it has a default
744 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
745 pub enum TraitMethod {
746 Required(TypeMethod),
750 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
759 impl fmt::Show for IntTy {
760 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
761 write!(f.buf, "{}", ast_util::int_ty_to_str(*self))
765 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
774 impl fmt::Show for UintTy {
775 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
776 write!(f.buf, "{}", ast_util::uint_ty_to_str(*self))
780 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
786 impl fmt::Show for FloatTy {
787 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
788 write!(f.buf, "{}", ast_util::float_ty_to_str(*self))
792 // NB Eq method appears below.
793 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
800 // Not represented directly in the AST, referred to by name through a ty_path.
801 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
811 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
817 impl fmt::Show for Onceness {
818 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
820 Once => "once".fmt(f),
821 Many => "many".fmt(f),
826 #[deriving(Eq, Encodable, Decodable, Hash)]
827 pub struct ClosureTy {
829 region: Option<Lifetime>,
830 lifetimes: OptVec<Lifetime>,
834 // Optional optvec distinguishes between "fn()" and "fn:()" so we can
835 // implement issue #7264. None means "fn()", which means infer a default
836 // bound based on pointer sigil during typeck. Some(Empty) means "fn:()",
837 // which means use no bounds (e.g., not even Owned on a ~fn()).
838 bounds: Option<OptVec<TyParamBound>>,
841 #[deriving(Eq, Encodable, Decodable, Hash)]
842 pub struct BareFnTy {
845 lifetimes: OptVec<Lifetime>,
849 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
852 TyBot, /* bottom type */
856 TyFixedLengthVec(P<Ty>, @Expr),
858 TyRptr(Option<Lifetime>, MutTy),
859 TyClosure(@ClosureTy),
862 TyPath(Path, Option<OptVec<TyParamBound>>, NodeId), // for #7264; see above
864 // TyInfer means the type should be inferred instead of it having been
865 // specified. This should only appear at the "top level" of a type and not
870 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
871 pub enum AsmDialect {
876 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
877 pub struct InlineAsm {
879 asm_str_style: StrStyle,
880 clobbers: InternedString,
881 inputs: Vec<(InternedString, @Expr)> ,
882 outputs: Vec<(InternedString, @Expr)> ,
888 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
896 pub fn new_self(span: Span, mutability: Mutability) -> Arg {
897 let path = ast_util::ident_to_path(span, special_idents::self_);
899 // HACK(eddyb) fake type for the self argument.
907 node: PatIdent(BindByValue(mutability), path, None),
915 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
923 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
925 UnsafeFn, // declared with "unsafe fn"
926 ImpureFn, // declared with "fn"
927 ExternFn, // declared with "extern fn"
930 impl fmt::Show for Purity {
931 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
933 ImpureFn => "impure".fmt(f),
934 UnsafeFn => "unsafe".fmt(f),
935 ExternFn => "extern".fmt(f),
940 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
942 NoReturn, // functions with return type _|_ that always
943 // raise an error or exit (i.e. never return to the caller)
944 Return, // everything else
947 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
948 pub enum ExplicitSelf_ {
949 SelfStatic, // no self
951 SelfRegion(Option<Lifetime>, Mutability), // `&'lt self`, `&'lt mut self`
955 pub type ExplicitSelf = Spanned<ExplicitSelf_>;
957 #[deriving(Eq, Encodable, Decodable, Hash)]
960 attrs: Vec<Attribute> ,
962 explicit_self: ExplicitSelf,
971 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
973 view_items: Vec<ViewItem> ,
977 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
978 pub struct ForeignMod {
980 view_items: Vec<ViewItem> ,
981 items: Vec<@ForeignItem> ,
984 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
985 pub struct VariantArg {
990 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
991 pub enum VariantKind {
992 TupleVariantKind(Vec<VariantArg> ),
993 StructVariantKind(@StructDef),
996 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
998 variants: Vec<P<Variant>> ,
1001 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1002 pub struct Variant_ {
1004 attrs: Vec<Attribute> ,
1007 disr_expr: Option<@Expr>,
1011 pub type Variant = Spanned<Variant_>;
1013 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1014 pub struct PathListIdent_ {
1019 pub type PathListIdent = Spanned<PathListIdent_>;
1021 pub type ViewPath = Spanned<ViewPath_>;
1023 #[deriving(Eq, Encodable, Decodable, Hash)]
1024 pub enum ViewPath_ {
1026 // quux = foo::bar::baz
1030 // foo::bar::baz (with 'baz =' implicitly on the left)
1031 ViewPathSimple(Ident, Path, NodeId),
1034 ViewPathGlob(Path, NodeId),
1036 // foo::bar::{a,b,c}
1037 ViewPathList(Path, Vec<PathListIdent> , NodeId)
1040 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1041 pub struct ViewItem {
1043 attrs: Vec<Attribute> ,
1048 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1049 pub enum ViewItem_ {
1050 // ident: name used to refer to this crate in the code
1051 // optional (InternedString,StrStyle): if present, this is a location
1052 // (containing arbitrary characters) from which to fetch the crate sources
1053 // For example, extern crate whatever = "github.com/mozilla/rust"
1054 ViewItemExternMod(Ident, Option<(InternedString,StrStyle)>, NodeId),
1055 ViewItemUse(Vec<@ViewPath> ),
1058 // Meta-data associated with an item
1059 pub type Attribute = Spanned<Attribute_>;
1061 // Distinguishes between Attributes that decorate items and Attributes that
1062 // are contained as statements within items. These two cases need to be
1063 // distinguished for pretty-printing.
1064 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1065 pub enum AttrStyle {
1070 // doc-comments are promoted to attributes that have is_sugared_doc = true
1071 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1072 pub struct Attribute_ {
1075 is_sugared_doc: bool,
1079 TraitRef's appear in impls.
1080 resolve maps each TraitRef's ref_id to its defining trait; that's all
1081 that the ref_id is for. The impl_id maps to the "self type" of this impl.
1082 If this impl is an ItemImpl, the impl_id is redundant (it could be the
1083 same as the impl's node id).
1085 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1086 pub struct TraitRef {
1091 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1092 pub enum Visibility {
1099 pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility {
1101 &Inherited => parent_visibility,
1102 &Public | &Private => *self
1107 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1108 pub struct StructField_ {
1109 kind: StructFieldKind,
1112 attrs: Vec<Attribute> ,
1115 pub type StructField = Spanned<StructField_>;
1117 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1118 pub enum StructFieldKind {
1119 NamedField(Ident, Visibility),
1120 UnnamedField // element of a tuple-like struct
1123 #[deriving(Eq, Encodable, Decodable, Hash)]
1124 pub struct StructDef {
1125 fields: Vec<StructField> , /* fields, not including ctor */
1126 /* ID of the constructor. This is only used for tuple- or enum-like
1128 ctor_id: Option<NodeId>
1132 FIXME (#3300): Should allow items to be anonymous. Right now
1133 we just use dummy names for anon items.
1135 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1138 attrs: Vec<Attribute> ,
1145 #[deriving(Clone, Eq, Encodable, Decodable, Hash)]
1147 ItemStatic(P<Ty>, Mutability, @Expr),
1148 ItemFn(P<FnDecl>, Purity, AbiSet, Generics, P<Block>),
1150 ItemForeignMod(ForeignMod),
1151 ItemTy(P<Ty>, Generics),
1152 ItemEnum(EnumDef, Generics),
1153 ItemStruct(@StructDef, Generics),
1154 ItemTrait(Generics, Vec<TraitRef> , Vec<TraitMethod> ),
1156 Option<TraitRef>, // (optional) trait this impl implements
1159 // a macro invocation (which includes macro definition)
1163 #[deriving(Eq, Encodable, Decodable, Hash)]
1164 pub struct ForeignItem {
1166 attrs: Vec<Attribute> ,
1173 #[deriving(Eq, Encodable, Decodable, Hash)]
1174 pub enum ForeignItem_ {
1175 ForeignItemFn(P<FnDecl>, Generics),
1176 ForeignItemStatic(P<Ty>, /* is_mutbl */ bool),
1179 // The data we save and restore about an inlined item or method. This is not
1180 // part of the AST that we parse from a file, but it becomes part of the tree
1182 #[deriving(Eq, Encodable, Decodable, Hash)]
1183 pub enum InlinedItem {
1185 IIMethod(DefId /* impl id */, bool /* is provided */, @Method),
1186 IIForeign(@ForeignItem),
1191 use serialize::json;
1196 fn is_freeze<T: Freeze>() {}
1198 // Assert that the AST remains Freeze (#10693).
1200 fn ast_is_freeze() {
1201 is_freeze::<Item>();
1204 // are ASTs encodable?
1206 fn check_asts_encodable() {
1208 module: Mod {view_items: Vec::new(), items: Vec::new()},
1217 // doesn't matter which encoder we use....
1218 let _f = &e as &serialize::Encodable<json::Encoder>;