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 serialize::{Encodable, Decodable, Encoder, Decoder};
26 /// A pointer abstraction. FIXME(eddyb) #10676 use Rc<T> in the future.
29 /// Construct a P<T> from a T value.
30 pub fn P<T: 'static>(value: T) -> P<T> {
34 // FIXME #6993: in librustc, uses of "ident" should be replaced
37 // an identifier contains a Name (index into the interner
38 // table) and a SyntaxContext to track renaming and
39 // macro expansion per Flatt et al., "Macros
40 // That Work Together"
41 #[deriving(Clone, Hash, Ord, TotalEq, TotalOrd, Show)]
48 /// Construct an identifier with the given name and an empty context:
49 pub fn new(name: Name) -> Ident { Ident {name: name, ctxt: EMPTY_CTXT}}
53 fn eq(&self, other: &Ident) -> bool {
54 if self.ctxt == other.ctxt {
55 self.name == other.name
57 // IF YOU SEE ONE OF THESE FAILS: it means that you're comparing
58 // idents that have different contexts. You can't fix this without
59 // knowing whether the comparison should be hygienic or non-hygienic.
60 // if it should be non-hygienic (most things are), just compare the
61 // 'name' fields of the idents. Or, even better, replace the idents
64 // On the other hand, if the comparison does need to be hygienic,
65 // one example and its non-hygienic counterpart would be:
66 // syntax::parse::token::mtwt_token_eq
67 // syntax::ext::tt::macro_parser::token_name_eq
68 fail!("not allowed to compare these idents: {:?}, {:?}. \
69 Probably related to issue \\#6993", self, other);
72 fn ne(&self, other: &Ident) -> bool {
77 /// A SyntaxContext represents a chain of macro-expandings
78 /// and renamings. Each macro expansion corresponds to
81 // I'm representing this syntax context as an index into
82 // a table, in order to work around a compiler bug
83 // that's causing unreleased memory to cause core dumps
84 // and also perhaps to save some work in destructor checks.
85 // the special uint '0' will be used to indicate an empty
88 // this uint is a reference to a table stored in thread-local
90 pub type SyntaxContext = u32;
91 pub static EMPTY_CTXT : SyntaxContext = 0;
92 pub static ILLEGAL_CTXT : SyntaxContext = 1;
94 /// A name is a part of an identifier, representing a string or gensym. It's
95 /// the result of interning.
98 /// A mark represents a unique id associated with a macro expansion
101 impl<S: Encoder> Encodable<S> for Ident {
102 fn encode(&self, s: &mut S) {
103 s.emit_str(token::get_ident(*self).get());
107 impl<D:Decoder> Decodable<D> for Ident {
108 fn decode(d: &mut D) -> Ident {
109 str_to_ident(d.read_str())
113 /// Function name (not all functions have names)
114 pub type FnIdent = Option<Ident>;
116 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
117 pub struct Lifetime {
123 // a "Path" is essentially Rust's notion of a name;
124 // for instance: std::cmp::Eq . It's represented
125 // as a sequence of identifiers, along with a bunch
126 // of supporting information.
127 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
130 /// A `::foo` path, is relative to the crate root rather than current
131 /// module (like paths in an import).
133 /// The segments in the path: the things separated by `::`.
134 segments: Vec<PathSegment> ,
137 /// A segment of a path: an identifier, an optional lifetime, and a set of
139 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
140 pub struct PathSegment {
141 /// The identifier portion of this path segment.
143 /// The lifetime parameters for this path segment.
144 lifetimes: Vec<Lifetime>,
145 /// The type parameters for this path segment, if present.
146 types: OwnedSlice<P<Ty>>,
149 pub type CrateNum = u32;
151 pub type NodeId = u32;
153 #[deriving(Clone, TotalEq, TotalOrd, Ord, Eq, Encodable, Decodable, Hash, Show)]
159 /// Item definitions in the currently-compiled crate would have the CrateNum
160 /// LOCAL_CRATE in their DefId.
161 pub static LOCAL_CRATE: CrateNum = 0;
162 pub static CRATE_NODE_ID: NodeId = 0;
164 // When parsing and doing expansions, we initially give all AST nodes this AST
165 // node value. Then later, in the renumber pass, we renumber them to have
166 // small, positive ids.
167 pub static DUMMY_NODE_ID: NodeId = -1;
169 // The AST represents all type param bounds as types.
170 // typeck::collect::compute_bounds matches these against
171 // the "special" built-in traits (see middle::lang_items) and
172 // detects Copy, Send and Share.
173 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
174 pub enum TyParamBound {
175 TraitTyParamBound(TraitRef),
179 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
183 bounds: OwnedSlice<TyParamBound>,
184 default: Option<P<Ty>>
187 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
188 pub struct Generics {
189 lifetimes: Vec<Lifetime>,
190 ty_params: OwnedSlice<TyParam>,
194 pub fn is_parameterized(&self) -> bool {
195 self.lifetimes.len() + self.ty_params.len() > 0
197 pub fn is_lt_parameterized(&self) -> bool {
198 self.lifetimes.len() > 0
200 pub fn is_type_parameterized(&self) -> bool {
201 self.ty_params.len() > 0
205 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
206 pub enum MethodProvenance {
211 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
213 DefFn(DefId, Purity),
214 DefStaticMethod(/* method */ DefId, MethodProvenance, Purity),
215 DefSelfTy(/* trait id */ NodeId),
217 DefForeignMod(DefId),
218 DefStatic(DefId, bool /* is_mutbl */),
219 DefArg(NodeId, BindingMode),
220 DefLocal(NodeId, BindingMode),
221 DefVariant(DefId /* enum */, DefId /* variant */, bool /* is_structure */),
225 DefTyParam(DefId, uint),
226 DefBinding(NodeId, BindingMode),
228 DefUpvar(NodeId, // id of closed over var
229 @Def, // closed over def
230 NodeId, // expr node that creates the closure
231 NodeId), // id for the block/body of the closure expr
233 /// Note that if it's a tuple struct's definition, the node id of the DefId
234 /// may either refer to the item definition's id or the StructDef.ctor_id.
236 /// The cases that I have encountered so far are (this is not exhaustive):
237 /// - If it's a ty_path referring to some tuple struct, then DefMap maps
238 /// it to a def whose id is the item definition's id.
239 /// - If it's an ExprPath referring to some tuple struct, then DefMap maps
240 /// it to a def whose id is the StructDef.ctor_id.
242 DefTyParamBinder(NodeId), /* struct, impl or trait with ty params */
245 DefMethod(DefId /* method */, Option<DefId> /* trait */),
248 #[deriving(Clone, Eq, TotalEq, Hash, Encodable, Decodable, Show)]
251 DefEarlyBoundRegion(/* index */ uint, /* lifetime decl */ NodeId),
252 DefLateBoundRegion(/* binder_id */ NodeId, /* depth */ uint, /* lifetime decl */ NodeId),
253 DefFreeRegion(/* block scope */ NodeId, /* lifetime decl */ NodeId),
256 // The set of MetaItems that define the compilation environment of the crate,
257 // used to drive conditional compilation
258 pub type CrateConfig = Vec<@MetaItem> ;
260 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
263 attrs: Vec<Attribute> ,
268 pub type MetaItem = Spanned<MetaItem_>;
270 #[deriving(Clone, Encodable, Decodable, TotalEq, Hash)]
272 MetaWord(InternedString),
273 MetaList(InternedString, Vec<@MetaItem> ),
274 MetaNameValue(InternedString, Lit),
277 // can't be derived because the MetaList requires an unordered comparison
278 impl Eq for MetaItem_ {
279 fn eq(&self, other: &MetaItem_) -> bool {
281 MetaWord(ref ns) => match *other {
282 MetaWord(ref no) => (*ns) == (*no),
285 MetaNameValue(ref ns, ref vs) => match *other {
286 MetaNameValue(ref no, ref vo) => {
287 (*ns) == (*no) && vs.node == vo.node
291 MetaList(ref ns, ref miss) => match *other {
292 MetaList(ref no, ref miso) => {
294 miss.iter().all(|mi| miso.iter().any(|x| x.node == mi.node))
302 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
304 view_items: Vec<ViewItem> ,
308 rules: BlockCheckMode,
312 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
319 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
320 pub struct FieldPat {
325 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
326 pub enum BindingMode {
327 BindByRef(Mutability),
328 BindByValue(Mutability),
331 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
335 // A PatIdent may either be a new bound variable,
336 // or a nullary enum (in which case the second field
338 // In the nullary enum case, the parser can't determine
339 // which it is. The resolver determines this, and
340 // records this pattern's NodeId in an auxiliary
341 // set (of "pat_idents that refer to nullary enums")
342 PatIdent(BindingMode, Path, Option<@Pat>),
343 PatEnum(Path, Option<Vec<@Pat> >), /* "none" means a * pattern where
344 * we don't bind the fields to names */
345 PatStruct(Path, Vec<FieldPat> , bool),
348 PatRegion(@Pat), // reference pattern
350 PatRange(@Expr, @Expr),
351 // [a, b, ..i, y, z] is represented as
352 // PatVec(~[a, b], Some(i), ~[y, z])
353 PatVec(Vec<@Pat> , Option<@Pat>, Vec<@Pat> )
356 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash, Show)]
357 pub enum Mutability {
362 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
369 impl fmt::Show for Sigil {
370 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
372 BorrowedSigil => "&".fmt(f),
373 OwnedSigil => "~".fmt(f),
374 ManagedSigil => "@".fmt(f),
379 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
380 pub enum ExprVstore {
381 ExprVstoreUniq, // ~[1,2,3,4]
382 ExprVstoreSlice, // &[1,2,3,4]
383 ExprVstoreMutSlice, // &mut [1,2,3,4]
386 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
408 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
417 pub type Stmt = Spanned<Stmt_>;
419 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
421 // could be an item or a local (let) binding:
422 StmtDecl(@Decl, NodeId),
424 // expr without trailing semi-colon (must have unit type):
425 StmtExpr(@Expr, NodeId),
427 // expr with trailing semi-colon (may have any type):
428 StmtSemi(@Expr, NodeId),
430 // bool: is there a trailing sem-colon?
434 // FIXME (pending discussion of #1697, #2178...): local should really be
435 // a refinement on pat.
436 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
437 #[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
446 pub type Decl = Spanned<Decl_>;
448 #[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
450 // a local (let) binding:
456 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
459 guard: Option<@Expr>,
463 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
470 pub type SpannedIdent = Spanned<Ident>;
472 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
473 pub enum BlockCheckMode {
475 UnsafeBlock(UnsafeSource),
478 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
479 pub enum UnsafeSource {
484 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
491 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
493 ExprVstore(@Expr, ExprVstore),
494 // First expr is the place; second expr is the value.
495 ExprBox(@Expr, @Expr),
496 ExprVec(Vec<@Expr> , Mutability),
497 ExprCall(@Expr, Vec<@Expr> ),
498 ExprMethodCall(Ident, Vec<P<Ty>> , Vec<@Expr> ),
499 ExprTup(Vec<@Expr> ),
500 ExprBinary(BinOp, @Expr, @Expr),
501 ExprUnary(UnOp, @Expr),
503 ExprCast(@Expr, P<Ty>),
504 ExprIf(@Expr, P<Block>, Option<@Expr>),
505 ExprWhile(@Expr, P<Block>),
506 // FIXME #6993: change to Option<Name>
507 ExprForLoop(@Pat, @Expr, P<Block>, Option<Ident>),
508 // Conditionless loop (can be exited with break, cont, or ret)
509 // FIXME #6993: change to Option<Name>
510 ExprLoop(P<Block>, Option<Ident>),
511 ExprMatch(@Expr, Vec<Arm> ),
512 ExprFnBlock(P<FnDecl>, P<Block>),
513 ExprProc(P<FnDecl>, P<Block>),
516 ExprAssign(@Expr, @Expr),
517 ExprAssignOp(BinOp, @Expr, @Expr),
518 ExprField(@Expr, Ident, Vec<P<Ty>> ),
519 ExprIndex(@Expr, @Expr),
521 /// Expression that looks like a "name". For example,
522 /// `std::slice::from_elem::<uint>` is an ExprPath that's the "name" part
523 /// of a function call.
526 ExprAddrOf(Mutability, @Expr),
527 ExprBreak(Option<Ident>),
528 ExprAgain(Option<Ident>),
529 ExprRet(Option<@Expr>),
531 ExprInlineAsm(InlineAsm),
535 // A struct literal expression.
536 ExprStruct(Path, Vec<Field> , Option<@Expr> /* base */),
538 // A vector literal constructed from one repeated element.
539 ExprRepeat(@Expr /* element */, @Expr /* count */, Mutability),
541 // No-op: used solely so we can pretty-print faithfully
545 // When the main rust parser encounters a syntax-extension invocation, it
546 // parses the arguments to the invocation as a token-tree. This is a very
547 // loose structure, such that all sorts of different AST-fragments can
548 // be passed to syntax extensions using a uniform type.
550 // If the syntax extension is an MBE macro, it will attempt to match its
551 // LHS "matchers" against the provided token tree, and if it finds a
552 // match, will transcribe the RHS token tree, splicing in any captured
553 // macro_parser::matched_nonterminals into the TTNonterminals it finds.
555 // The RHS of an MBE macro is the only place a TTNonterminal or TTSeq
556 // makes any real sense. You could write them elsewhere but nothing
557 // else knows what to do with them, so you'll probably get a syntax
560 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
561 #[doc="For macro invocations; parsing is delegated to the macro"]
564 TTTok(Span, ::parse::token::Token),
565 // a delimited sequence (the delimiters appear as the first
566 // and last elements of the vector)
567 TTDelim(@Vec<TokenTree> ),
569 // These only make sense for right-hand-sides of MBE macros:
571 // a kleene-style repetition sequence with a span, a TTForest,
572 // an optional separator, and a boolean where true indicates
573 // zero or more (..), and false indicates one or more (+).
574 TTSeq(Span, @Vec<TokenTree> , Option<::parse::token::Token>, bool),
576 // a syntactic variable that will be filled in by macro expansion.
577 TTNonterminal(Span, Ident)
581 // Matchers are nodes defined-by and recognized-by the main rust parser and
582 // language, but they're only ever found inside syntax-extension invocations;
583 // indeed, the only thing that ever _activates_ the rules in the rust parser
584 // for parsing a matcher is a matcher looking for the 'matchers' nonterminal
585 // itself. Matchers represent a small sub-language for pattern-matching
586 // token-trees, and are thus primarily used by the macro-defining extension
592 // A matcher that matches a single token, denoted by the token itself. So
593 // long as there's no $ involved.
599 // A matcher that matches a sequence of sub-matchers, denoted various
602 // $(M)* zero or more Ms
603 // $(M)+ one or more Ms
604 // $(M),+ one or more comma-separated Ms
605 // $(A B C);* zero or more semi-separated 'A B C' seqs
611 // A matcher that matches one of a few interesting named rust
612 // nonterminals, such as types, expressions, items, or raw token-trees. A
613 // black-box matcher on expr, for example, binds an expr to a given ident,
614 // and that ident can re-occur as an interpolation in the RHS of a
615 // macro-by-example rule. For example:
617 // $foo:expr => 1 + $foo // interpolate an expr
618 // $foo:tt => $foo // interpolate a token-tree
619 // $foo:tt => bar! $foo // only other valid interpolation
620 // // is in arg position for another
623 // As a final, horrifying aside, note that macro-by-example's input is
624 // also matched by one of these matchers. Holy self-referential! It is matched
625 // by a MatchSeq, specifically this one:
627 // $( $lhs:matchers => $rhs:tt );+
629 // If you understand that, you have closed to loop and understand the whole
630 // macro system. Congratulations.
632 pub type Matcher = Spanned<Matcher_>;
634 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
637 MatchTok(::parse::token::Token),
638 // match repetitions of a sequence: body, separator, zero ok?,
639 // lo, hi position-in-match-array used:
640 MatchSeq(Vec<Matcher> , Option<::parse::token::Token>, bool, uint, uint),
641 // parse a Rust NT: name to bind, name of NT, position in match array:
642 MatchNonterminal(Ident, Ident, uint)
645 pub type Mac = Spanned<Mac_>;
647 // represents a macro invocation. The Path indicates which macro
648 // is being invoked, and the vector of token-trees contains the source
649 // of the macro invocation.
650 // There's only one flavor, now, so this could presumably be simplified.
651 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
653 MacInvocTT(Path, Vec<TokenTree> , SyntaxContext), // new macro-invocation
656 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
662 pub type Lit = Spanned<Lit_>;
664 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
666 LitStr(InternedString, StrStyle),
667 LitBinary(Rc<Vec<u8> >),
670 LitUint(u64, UintTy),
671 LitIntUnsuffixed(i64),
672 LitFloat(InternedString, FloatTy),
673 LitFloatUnsuffixed(InternedString),
678 // NB: If you change this, you'll probably want to change the corresponding
679 // type structure in middle/ty.rs as well.
680 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
686 #[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
687 pub struct TypeField {
693 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
694 pub struct TypeMethod {
696 attrs: Vec<Attribute> ,
700 explicit_self: ExplicitSelf,
705 // A trait method is either required (meaning it doesn't have an
706 // implementation, just a signature) or provided (meaning it has a default
708 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
709 pub enum TraitMethod {
710 Required(TypeMethod),
714 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
723 impl fmt::Show for IntTy {
724 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
725 write!(f.buf, "{}", ast_util::int_ty_to_str(*self))
729 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
738 impl fmt::Show for UintTy {
739 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
740 write!(f.buf, "{}", ast_util::uint_ty_to_str(*self))
744 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
750 impl fmt::Show for FloatTy {
751 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
752 write!(f.buf, "{}", ast_util::float_ty_to_str(*self))
756 // NB Eq method appears below.
757 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
764 // Not represented directly in the AST, referred to by name through a ty_path.
765 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
775 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
781 impl fmt::Show for Onceness {
782 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
784 Once => "once".fmt(f),
785 Many => "many".fmt(f),
790 #[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
791 pub struct ClosureTy {
793 region: Option<Lifetime>,
794 lifetimes: Vec<Lifetime>,
798 // Optional optvec distinguishes between "fn()" and "fn:()" so we can
799 // implement issue #7264. None means "fn()", which means infer a default
800 // bound based on pointer sigil during typeck. Some(Empty) means "fn:()",
801 // which means use no bounds (e.g., not even Owned on a ~fn()).
802 bounds: Option<OwnedSlice<TyParamBound>>,
805 #[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
806 pub struct BareFnTy {
809 lifetimes: Vec<Lifetime>,
813 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
816 TyBot, /* bottom type */
820 TyFixedLengthVec(P<Ty>, @Expr),
822 TyRptr(Option<Lifetime>, MutTy),
823 TyClosure(@ClosureTy),
826 TyPath(Path, Option<OwnedSlice<TyParamBound>>, NodeId), // for #7264; see above
828 // TyInfer means the type should be inferred instead of it having been
829 // specified. This can appear anywhere in a type.
833 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
834 pub enum AsmDialect {
839 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
840 pub struct InlineAsm {
842 asm_str_style: StrStyle,
843 clobbers: InternedString,
844 inputs: Vec<(InternedString, @Expr)> ,
845 outputs: Vec<(InternedString, @Expr)> ,
851 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
859 pub fn new_self(span: Span, mutability: Mutability) -> Arg {
860 let path = ast_util::ident_to_path(span, special_idents::self_);
862 // HACK(eddyb) fake type for the self argument.
870 node: PatIdent(BindByValue(mutability), path, None),
878 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
886 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
888 UnsafeFn, // declared with "unsafe fn"
889 ImpureFn, // declared with "fn"
890 ExternFn, // declared with "extern fn"
893 impl fmt::Show for Purity {
894 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
896 ImpureFn => "impure".fmt(f),
897 UnsafeFn => "unsafe".fmt(f),
898 ExternFn => "extern".fmt(f),
903 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
905 NoReturn, // functions with return type _|_ that always
906 // raise an error or exit (i.e. never return to the caller)
907 Return, // everything else
910 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
911 pub enum ExplicitSelf_ {
912 SelfStatic, // no self
914 SelfRegion(Option<Lifetime>, Mutability), // `&'lt self`, `&'lt mut self`
918 pub type ExplicitSelf = Spanned<ExplicitSelf_>;
920 #[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
923 attrs: Vec<Attribute> ,
925 explicit_self: ExplicitSelf,
934 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
936 view_items: Vec<ViewItem> ,
940 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
941 pub struct ForeignMod {
943 view_items: Vec<ViewItem> ,
944 items: Vec<@ForeignItem> ,
947 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
948 pub struct VariantArg {
953 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
954 pub enum VariantKind {
955 TupleVariantKind(Vec<VariantArg> ),
956 StructVariantKind(@StructDef),
959 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
961 variants: Vec<P<Variant>> ,
964 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
965 pub struct Variant_ {
967 attrs: Vec<Attribute> ,
970 disr_expr: Option<@Expr>,
974 pub type Variant = Spanned<Variant_>;
976 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
977 pub struct PathListIdent_ {
982 pub type PathListIdent = Spanned<PathListIdent_>;
984 pub type ViewPath = Spanned<ViewPath_>;
986 #[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
989 // quux = foo::bar::baz
993 // foo::bar::baz (with 'baz =' implicitly on the left)
994 ViewPathSimple(Ident, Path, NodeId),
997 ViewPathGlob(Path, NodeId),
1000 ViewPathList(Path, Vec<PathListIdent> , NodeId)
1003 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
1004 pub struct ViewItem {
1006 attrs: Vec<Attribute> ,
1011 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
1012 pub enum ViewItem_ {
1013 // ident: name used to refer to this crate in the code
1014 // optional (InternedString,StrStyle): if present, this is a location
1015 // (containing arbitrary characters) from which to fetch the crate sources
1016 // For example, extern crate whatever = "github.com/mozilla/rust"
1017 ViewItemExternCrate(Ident, Option<(InternedString,StrStyle)>, NodeId),
1018 ViewItemUse(Vec<@ViewPath> ),
1021 // Meta-data associated with an item
1022 pub type Attribute = Spanned<Attribute_>;
1024 // Distinguishes between Attributes that decorate items and Attributes that
1025 // are contained as statements within items. These two cases need to be
1026 // distinguished for pretty-printing.
1027 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
1028 pub enum AttrStyle {
1033 // doc-comments are promoted to attributes that have is_sugared_doc = true
1034 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
1035 pub struct Attribute_ {
1038 is_sugared_doc: bool,
1042 TraitRef's appear in impls.
1043 resolve maps each TraitRef's ref_id to its defining trait; that's all
1044 that the ref_id is for. The impl_id maps to the "self type" of this impl.
1045 If this impl is an ItemImpl, the impl_id is redundant (it could be the
1046 same as the impl's node id).
1048 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
1049 pub struct TraitRef {
1054 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
1055 pub enum Visibility {
1062 pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility {
1064 &Inherited => parent_visibility,
1065 &Public | &Private => *self
1070 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
1071 pub struct StructField_ {
1072 kind: StructFieldKind,
1075 attrs: Vec<Attribute> ,
1078 pub type StructField = Spanned<StructField_>;
1080 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
1081 pub enum StructFieldKind {
1082 NamedField(Ident, Visibility),
1083 UnnamedField // element of a tuple-like struct
1086 #[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
1087 pub struct StructDef {
1088 fields: Vec<StructField> , /* fields, not including ctor */
1089 /* ID of the constructor. This is only used for tuple- or enum-like
1091 ctor_id: Option<NodeId>
1095 FIXME (#3300): Should allow items to be anonymous. Right now
1096 we just use dummy names for anon items.
1098 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
1101 attrs: Vec<Attribute> ,
1108 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
1110 ItemStatic(P<Ty>, Mutability, @Expr),
1111 ItemFn(P<FnDecl>, Purity, AbiSet, Generics, P<Block>),
1113 ItemForeignMod(ForeignMod),
1114 ItemTy(P<Ty>, Generics),
1115 ItemEnum(EnumDef, Generics),
1116 ItemStruct(@StructDef, Generics),
1117 ItemTrait(Generics, Vec<TraitRef> , Vec<TraitMethod> ),
1119 Option<TraitRef>, // (optional) trait this impl implements
1122 // a macro invocation (which includes macro definition)
1126 #[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
1127 pub struct ForeignItem {
1129 attrs: Vec<Attribute> ,
1136 #[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
1137 pub enum ForeignItem_ {
1138 ForeignItemFn(P<FnDecl>, Generics),
1139 ForeignItemStatic(P<Ty>, /* is_mutbl */ bool),
1142 // The data we save and restore about an inlined item or method. This is not
1143 // part of the AST that we parse from a file, but it becomes part of the tree
1145 #[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
1146 pub enum InlinedItem {
1148 IIMethod(DefId /* impl id */, bool /* is provided */, @Method),
1149 IIForeign(@ForeignItem),
1154 use serialize::json;
1159 // are ASTs encodable?
1161 fn check_asts_encodable() {
1163 module: Mod {view_items: Vec::new(), items: Vec::new()},
1172 // doesn't matter which encoder we use....
1173 let _f = &e as &serialize::Encodable<json::Encoder>;