1 // Copyright 2012 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};
16 use parse::token::{interner_get, str_to_ident};
18 use std::hashmap::HashMap;
19 use std::option::Option;
20 use std::to_str::ToStr;
21 use extra::serialize::{Encodable, Decodable, Encoder, Decoder};
24 // FIXME #6993: in librustc, uses of "ident" should be replaced
27 // an identifier contains a Name (index into the interner
28 // table) and a SyntaxContext to track renaming and
29 // macro expansion per Flatt et al., "Macros
30 // That Work Together"
31 #[deriving(Clone, IterBytes, ToStr)]
32 pub struct Ident { name: Name, ctxt: SyntaxContext }
35 /// Construct an identifier with the given name and an empty context:
36 pub fn new(name: Name) -> Ident { Ident {name: name, ctxt: EMPTY_CTXT}}
40 fn eq(&self, other: &Ident) -> bool {
41 if (self.ctxt == other.ctxt) {
42 self.name == other.name
44 // IF YOU SEE ONE OF THESE FAILS: it means that you're comparing
45 // idents that have different contexts. You can't fix this without
46 // knowing whether the comparison should be hygienic or non-hygienic.
47 // if it should be non-hygienic (most things are), just compare the
48 // 'name' fields of the idents. Or, even better, replace the idents
50 fail!("not allowed to compare these idents: {:?}, {:?}.
51 Probably related to issue \\#6993", self, other);
54 fn ne(&self, other: &Ident) -> bool {
59 /// A SyntaxContext represents a chain of macro-expandings
60 /// and renamings. Each macro expansion corresponds to
63 // I'm representing this syntax context as an index into
64 // a table, in order to work around a compiler bug
65 // that's causing unreleased memory to cause core dumps
66 // and also perhaps to save some work in destructor checks.
67 // the special uint '0' will be used to indicate an empty
70 // this uint is a reference to a table stored in thread-local
72 pub type SyntaxContext = uint;
74 // the SCTable contains a table of SyntaxContext_'s. It
75 // represents a flattened tree structure, to avoid having
76 // managed pointers everywhere (that caused an ICE).
77 // the mark_memo and rename_memo fields are side-tables
78 // that ensure that adding the same mark to the same context
79 // gives you back the same context as before. This shouldn't
80 // change the semantics--everything here is immutable--but
81 // it should cut down on memory use *a lot*; applying a mark
82 // to a tree containing 50 identifiers would otherwise generate
84 table : ~[SyntaxContext_],
85 mark_memo : HashMap<(SyntaxContext,Mrk),SyntaxContext>,
86 rename_memo : HashMap<(SyntaxContext,Ident,Name),SyntaxContext>
89 // NB: these must be placed in any SCTable...
90 pub static EMPTY_CTXT : uint = 0;
91 pub static ILLEGAL_CTXT : uint = 1;
93 #[deriving(Eq, Encodable, Decodable,IterBytes)]
94 pub enum SyntaxContext_ {
96 Mark (Mrk,SyntaxContext),
97 // flattening the name and syntaxcontext into the rename...
99 // 1) the first name in a Rename node
100 // can only be a programmer-supplied name.
101 // 2) Every Rename node with a given Name in the
102 // "to" slot must have the same name and context
103 // in the "from" slot. In essence, they're all
104 // pointers to a single "rename" event node.
105 Rename (Ident,Name,SyntaxContext),
106 // actually, IllegalCtxt may not be necessary.
110 /// A name is a part of an identifier, representing a string or gensym. It's
111 /// the result of interning.
112 pub type Name = uint;
113 /// A mark represents a unique id associated with a macro expansion
116 impl<S:Encoder> Encodable<S> for Ident {
117 fn encode(&self, s: &mut S) {
118 s.emit_str(interner_get(self.name));
122 #[deriving(IterBytes)]
123 impl<D:Decoder> Decodable<D> for Ident {
124 fn decode(d: &mut D) -> Ident {
125 str_to_ident(d.read_str())
129 /// Function name (not all functions have names)
130 pub type FnIdent = Option<Ident>;
132 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
133 pub struct Lifetime {
136 // FIXME #7743 : change this to Name!
140 // a "Path" is essentially Rust's notion of a name;
141 // for instance: std::cmp::Eq . It's represented
142 // as a sequence of identifiers, along with a bunch
143 // of supporting information.
144 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
147 /// A `::foo` path, is relative to the crate root rather than current
148 /// module (like paths in an import).
150 /// The segments in the path: the things separated by `::`.
151 segments: ~[PathSegment],
154 /// A segment of a path: an identifier, an optional lifetime, and a set of
156 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
157 pub struct PathSegment {
158 /// The identifier portion of this path segment.
160 /// The lifetime parameter for this path segment. Currently only one
161 /// lifetime parameter is allowed.
162 lifetime: Option<Lifetime>,
163 /// The type parameters for this path segment, if present.
167 pub type CrateNum = int;
169 pub type NodeId = int;
171 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes, ToStr)]
177 pub static LOCAL_CRATE: CrateNum = 0;
178 pub static CRATE_NODE_ID: NodeId = 0;
180 // When parsing and doing expansions, we initially give all AST nodes this AST
181 // node value. Then later, in the renumber pass, we renumber them to have
182 // small, positive ids.
183 pub static DUMMY_NODE_ID: NodeId = -1;
185 // The AST represents all type param bounds as types.
186 // typeck::collect::compute_bounds matches these against
187 // the "special" built-in traits (see middle::lang_items) and
188 // detects Copy, Send, Send, and Freeze.
189 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
190 pub enum TyParamBound {
191 TraitTyParamBound(trait_ref),
195 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
199 bounds: OptVec<TyParamBound>
202 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
203 pub struct Generics {
204 lifetimes: OptVec<Lifetime>,
205 ty_params: OptVec<TyParam>,
209 pub fn is_parameterized(&self) -> bool {
210 self.lifetimes.len() + self.ty_params.len() > 0
212 pub fn is_lt_parameterized(&self) -> bool {
213 self.lifetimes.len() > 0
215 pub fn is_type_parameterized(&self) -> bool {
216 self.ty_params.len() > 0
220 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
221 pub enum MethodProvenance {
226 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
228 DefFn(DefId, purity),
229 DefStaticMethod(/* method */ DefId, MethodProvenance, purity),
230 DefSelf(NodeId, bool /* is_mutbl */),
231 DefSelfTy(/* trait id */ NodeId),
233 DefForeignMod(DefId),
234 DefStatic(DefId, bool /* is_mutbl */),
235 DefArg(NodeId, bool /* is_mutbl */),
236 DefLocal(NodeId, bool /* is_mutbl */),
237 DefVariant(DefId /* enum */, DefId /* variant */, bool /* is_structure */),
241 DefTyParam(DefId, uint),
242 DefBinding(NodeId, BindingMode),
244 DefUpvar(NodeId, // id of closed over var
245 @Def, // closed over def
246 NodeId, // expr node that creates the closure
247 NodeId), // id for the block/body of the closure expr
249 DefTyParamBinder(NodeId), /* struct, impl or trait with ty params */
252 DefMethod(DefId /* method */, Option<DefId> /* trait */),
255 // The set of MetaItems that define the compilation environment of the crate,
256 // used to drive conditional compilation
257 pub type CrateConfig = ~[@MetaItem];
259 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
267 pub type MetaItem = Spanned<MetaItem_>;
269 #[deriving(Clone, Encodable, Decodable, IterBytes)]
272 MetaList(@str, ~[@MetaItem]),
273 MetaNameValue(@str, lit),
276 // can't be derived because the MetaList requires an unordered comparison
277 impl Eq for MetaItem_ {
278 fn eq(&self, other: &MetaItem_) -> bool {
280 MetaWord(ref ns) => match *other {
281 MetaWord(ref no) => (*ns) == (*no),
284 MetaNameValue(ref ns, ref vs) => match *other {
285 MetaNameValue(ref no, ref vo) => {
286 (*ns) == (*no) && vs.node == vo.node
290 MetaList(ref ns, ref miss) => match *other {
291 MetaList(ref no, ref miso) => {
293 miss.iter().all(|mi| miso.iter().any(|x| x.node == mi.node))
301 #[deriving(Clone, Eq, Encodable, Decodable,IterBytes)]
303 view_items: ~[view_item],
307 rules: BlockCheckMode,
311 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
318 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
319 pub struct FieldPat {
324 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
325 pub enum BindingMode {
326 BindByRef(Mutability),
330 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
333 // A pat_ident may either be a new bound variable,
334 // or a nullary enum (in which case the second field
336 // In the nullary enum case, the parser can't determine
337 // which it is. The resolver determines this, and
338 // records this pattern's NodeId in an auxiliary
339 // set (of "pat_idents that refer to nullary enums")
340 PatIdent(BindingMode, Path, Option<@Pat>),
341 PatEnum(Path, Option<~[@Pat]>), /* "none" means a * pattern where
342 * we don't bind the fields to names */
343 PatStruct(Path, ~[FieldPat], bool),
347 PatRegion(@Pat), // borrowed pointer pattern
349 PatRange(@Expr, @Expr),
350 // [a, b, ..i, y, z] is represented as
351 // pat_vec(~[a, b], Some(i), ~[y, z])
352 PatVec(~[@Pat], Option<@Pat>, ~[@Pat])
355 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
356 pub enum Mutability {
361 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
368 impl ToStr for Sigil {
369 fn to_str(&self) -> ~str {
371 BorrowedSigil => ~"&",
378 #[deriving(Eq, Encodable, Decodable, IterBytes)]
380 // FIXME (#3469): Change uint to @expr (actually only constant exprs)
381 VstoreFixed(Option<uint>), // [1,2,3,4]
382 VstoreUniq, // ~[1,2,3,4]
383 VstoreBox, // @[1,2,3,4]
384 VstoreSlice(Option<Lifetime>) // &'foo? [1,2,3,4]
387 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
388 pub enum ExprVstore {
389 ExprVstoreUniq, // ~[1,2,3,4]
390 ExprVstoreBox, // @[1,2,3,4]
391 ExprVstoreMutBox, // @mut [1,2,3,4]
392 ExprVstoreSlice, // &[1,2,3,4]
393 ExprVstoreMutSlice, // &mut [1,2,3,4]
396 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
418 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
427 pub type Stmt = Spanned<Stmt_>;
429 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
431 // could be an item or a local (let) binding:
432 StmtDecl(@Decl, NodeId),
434 // expr without trailing semi-colon (must have unit type):
435 StmtExpr(@Expr, NodeId),
437 // expr with trailing semi-colon (may have any type):
438 StmtSemi(@Expr, NodeId),
440 // bool: is there a trailing sem-colon?
444 // FIXME (pending discussion of #1697, #2178...): local should really be
445 // a refinement on pat.
446 #[deriving(Eq, Encodable, Decodable,IterBytes)]
456 pub type Decl = Spanned<Decl_>;
458 #[deriving(Eq, Encodable, Decodable,IterBytes)]
460 // a local (let) binding:
466 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
469 guard: Option<@Expr>,
473 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
480 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
481 pub enum BlockCheckMode {
483 UnsafeBlock(UnsafeSource),
486 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
487 pub enum UnsafeSource {
492 #[deriving(Clone, Eq, Encodable, Decodable,IterBytes)]
500 pub fn get_callee_id(&self) -> Option<NodeId> {
502 ExprMethodCall(callee_id, _, _, _, _, _) |
503 ExprIndex(callee_id, _, _) |
504 ExprBinary(callee_id, _, _, _) |
505 ExprAssignOp(callee_id, _, _, _) |
506 ExprUnary(callee_id, _, _) => Some(callee_id),
512 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
519 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
521 ExprVstore(@Expr, ExprVstore),
522 ExprVec(~[@Expr], Mutability),
523 ExprCall(@Expr, ~[@Expr], CallSugar),
524 ExprMethodCall(NodeId, @Expr, Ident, ~[Ty], ~[@Expr], CallSugar),
526 ExprBinary(NodeId, BinOp, @Expr, @Expr),
527 ExprUnary(NodeId, UnOp, @Expr),
530 ExprIf(@Expr, Block, Option<@Expr>),
531 ExprWhile(@Expr, Block),
532 // FIXME #6993: change to Option<Name>
533 ExprForLoop(@Pat, @Expr, Block, Option<Ident>),
534 // Conditionless loop (can be exited with break, cont, or ret)
535 // FIXME #6993: change to Option<Name>
536 ExprLoop(Block, Option<Ident>),
537 ExprMatch(@Expr, ~[Arm]),
538 ExprFnBlock(fn_decl, Block),
542 ExprAssign(@Expr, @Expr),
543 ExprAssignOp(NodeId, BinOp, @Expr, @Expr),
544 ExprField(@Expr, Ident, ~[Ty]),
545 ExprIndex(NodeId, @Expr, @Expr),
548 /// The special identifier `self`.
550 ExprAddrOf(Mutability, @Expr),
551 ExprBreak(Option<Name>),
552 ExprAgain(Option<Name>),
553 ExprRet(Option<@Expr>),
555 /// Gets the log level for the enclosing module
558 ExprInlineAsm(inline_asm),
562 // A struct literal expression.
563 ExprStruct(Path, ~[Field], Option<@Expr> /* base */),
565 // A vector literal constructed from one repeated element.
566 ExprRepeat(@Expr /* element */, @Expr /* count */, Mutability),
568 // No-op: used solely so we can pretty-print faithfully
572 // When the main rust parser encounters a syntax-extension invocation, it
573 // parses the arguments to the invocation as a token-tree. This is a very
574 // loose structure, such that all sorts of different AST-fragments can
575 // be passed to syntax extensions using a uniform type.
577 // If the syntax extension is an MBE macro, it will attempt to match its
578 // LHS "matchers" against the provided token tree, and if it finds a
579 // match, will transcribe the RHS token tree, splicing in any captured
580 // macro_parser::matched_nonterminals into the tt_nonterminals it finds.
582 // The RHS of an MBE macro is the only place a tt_nonterminal or tt_seq
583 // makes any real sense. You could write them elsewhere but nothing
584 // else knows what to do with them, so you'll probably get a syntax
587 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
588 #[doc="For macro invocations; parsing is delegated to the macro"]
589 pub enum token_tree {
591 tt_tok(Span, ::parse::token::Token),
592 // a delimited sequence (the delimiters appear as the first
593 // and last elements of the vector)
594 tt_delim(@mut ~[token_tree]),
596 // These only make sense for right-hand-sides of MBE macros:
598 // a kleene-style repetition sequence with a span, a tt_forest,
599 // an optional separator, and a boolean where true indicates
600 // zero or more (*), and false indicates one or more (+).
601 tt_seq(Span, @mut ~[token_tree], Option<::parse::token::Token>, bool),
603 // a syntactic variable that will be filled in by macro expansion.
604 tt_nonterminal(Span, Ident)
608 // Matchers are nodes defined-by and recognized-by the main rust parser and
609 // language, but they're only ever found inside syntax-extension invocations;
610 // indeed, the only thing that ever _activates_ the rules in the rust parser
611 // for parsing a matcher is a matcher looking for the 'matchers' nonterminal
612 // itself. Matchers represent a small sub-language for pattern-matching
613 // token-trees, and are thus primarily used by the macro-defining extension
619 // A matcher that matches a single token, denoted by the token itself. So
620 // long as there's no $ involved.
626 // A matcher that matches a sequence of sub-matchers, denoted various
629 // $(M)* zero or more Ms
630 // $(M)+ one or more Ms
631 // $(M),+ one or more comma-separated Ms
632 // $(A B C);* zero or more semi-separated 'A B C' seqs
638 // A matcher that matches one of a few interesting named rust
639 // nonterminals, such as types, expressions, items, or raw token-trees. A
640 // black-box matcher on expr, for example, binds an expr to a given ident,
641 // and that ident can re-occur as an interpolation in the RHS of a
642 // macro-by-example rule. For example:
644 // $foo:expr => 1 + $foo // interpolate an expr
645 // $foo:tt => $foo // interpolate a token-tree
646 // $foo:tt => bar! $foo // only other valid interpolation
647 // // is in arg position for another
650 // As a final, horrifying aside, note that macro-by-example's input is
651 // also matched by one of these matchers. Holy self-referential! It is matched
652 // by an match_seq, specifically this one:
654 // $( $lhs:matchers => $rhs:tt );+
656 // If you understand that, you have closed to loop and understand the whole
657 // macro system. Congratulations.
659 pub type matcher = Spanned<matcher_>;
661 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
664 match_tok(::parse::token::Token),
665 // match repetitions of a sequence: body, separator, zero ok?,
666 // lo, hi position-in-match-array used:
667 match_seq(~[matcher], Option<::parse::token::Token>, bool, uint, uint),
668 // parse a Rust NT: name to bind, name of NT, position in match array:
669 match_nonterminal(Ident, Ident, uint)
672 pub type mac = Spanned<mac_>;
674 // represents a macro invocation. The Path indicates which macro
675 // is being invoked, and the vector of token-trees contains the source
676 // of the macro invocation.
677 // There's only one flavor, now, so this could presumably be simplified.
678 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
680 mac_invoc_tt(Path,~[token_tree],SyntaxContext), // new macro-invocation
683 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
689 pub type lit = Spanned<lit_>;
691 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
693 lit_str(@str, StrStyle),
696 lit_int(i64, int_ty),
697 lit_uint(u64, uint_ty),
698 lit_int_unsuffixed(i64),
699 lit_float(@str, float_ty),
700 lit_float_unsuffixed(@str),
705 // NB: If you change this, you'll probably want to change the corresponding
706 // type structure in middle/ty.rs as well.
707 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
713 #[deriving(Eq, Encodable, Decodable,IterBytes)]
714 pub struct TypeField {
720 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
721 pub struct TypeMethod {
727 explicit_self: explicit_self,
732 // A trait method is either required (meaning it doesn't have an
733 // implementation, just a signature) or provided (meaning it has a default
735 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
736 pub enum trait_method {
737 required(TypeMethod),
741 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
750 impl ToStr for int_ty {
751 fn to_str(&self) -> ~str {
752 ::ast_util::int_ty_to_str(*self)
756 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
765 impl ToStr for uint_ty {
766 fn to_str(&self) -> ~str {
767 ::ast_util::uint_ty_to_str(*self)
771 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
777 impl ToStr for float_ty {
778 fn to_str(&self) -> ~str {
779 ::ast_util::float_ty_to_str(*self)
783 // NB Eq method appears below.
784 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
791 // Not represented directly in the AST, referred to by name through a ty_path.
792 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
802 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
808 #[deriving(IterBytes)]
809 impl ToStr for Onceness {
810 fn to_str(&self) -> ~str {
818 #[deriving(Eq, Encodable, Decodable,IterBytes)]
819 pub struct TyClosure {
821 region: Option<Lifetime>,
822 lifetimes: OptVec<Lifetime>,
826 // Optional optvec distinguishes between "fn()" and "fn:()" so we can
827 // implement issue #7264. None means "fn()", which means infer a default
828 // bound based on pointer sigil during typeck. Some(Empty) means "fn:()",
829 // which means use no bounds (e.g., not even Owned on a ~fn()).
830 bounds: Option<OptVec<TyParamBound>>,
833 #[deriving(Eq, Encodable, Decodable,IterBytes)]
834 pub struct TyBareFn {
837 lifetimes: OptVec<Lifetime>,
841 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
844 ty_bot, /* bottom type */
848 ty_fixed_length_vec(mt, @Expr),
850 ty_rptr(Option<Lifetime>, mt),
851 ty_closure(@TyClosure),
852 ty_bare_fn(@TyBareFn),
854 ty_path(Path, Option<OptVec<TyParamBound>>, NodeId), // for #7264; see above
857 // ty_infer means the type should be inferred instead of it having been
858 // specified. This should only appear at the "top level" of a type and not
863 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
864 pub enum asm_dialect {
869 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
870 pub struct inline_asm {
872 asm_str_style: StrStyle,
874 inputs: ~[(@str, @Expr)],
875 outputs: ~[(@str, @Expr)],
881 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
889 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
896 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
898 unsafe_fn, // declared with "unsafe fn"
899 impure_fn, // declared with "fn"
900 extern_fn, // declared with "extern fn"
903 #[deriving(IterBytes)]
904 impl ToStr for purity {
905 fn to_str(&self) -> ~str {
907 impure_fn => ~"impure",
908 unsafe_fn => ~"unsafe",
909 extern_fn => ~"extern"
914 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
916 noreturn, // functions with return type _|_ that always
917 // raise an error or exit (i.e. never return to the caller)
918 return_val, // everything else
921 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
922 pub enum explicit_self_ {
923 sty_static, // no self
924 sty_value(Mutability), // `self`
925 sty_region(Option<Lifetime>, Mutability), // `&'lt self`
926 sty_box(Mutability), // `@self`
927 sty_uniq(Mutability) // `~self`
930 pub type explicit_self = Spanned<explicit_self_>;
932 #[deriving(Eq, Encodable, Decodable,IterBytes)]
937 explicit_self: explicit_self,
947 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
949 view_items: ~[view_item],
953 #[deriving(Clone, Eq, Encodable, Decodable,IterBytes)]
954 pub struct foreign_mod {
956 view_items: ~[view_item],
957 items: ~[@foreign_item],
960 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
961 pub struct variant_arg {
966 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
967 pub enum variant_kind {
968 tuple_variant_kind(~[variant_arg]),
969 struct_variant_kind(@struct_def),
972 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
973 pub struct enum_def {
974 variants: ~[variant],
977 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
978 pub struct variant_ {
983 disr_expr: Option<@Expr>,
987 pub type variant = Spanned<variant_>;
989 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
990 pub struct path_list_ident_ {
995 pub type path_list_ident = Spanned<path_list_ident_>;
997 pub type view_path = Spanned<view_path_>;
999 #[deriving(Eq, Encodable, Decodable, IterBytes)]
1000 pub enum view_path_ {
1002 // quux = foo::bar::baz
1006 // foo::bar::baz (with 'baz =' implicitly on the left)
1007 view_path_simple(Ident, Path, NodeId),
1010 view_path_glob(Path, NodeId),
1012 // foo::bar::{a,b,c}
1013 view_path_list(Path, ~[path_list_ident], NodeId)
1016 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
1017 pub struct view_item {
1019 attrs: ~[Attribute],
1024 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
1025 pub enum view_item_ {
1026 // ident: name used to refer to this crate in the code
1027 // optional @str: if present, this is a location (containing
1028 // arbitrary characters) from which to fetch the crate sources
1029 // For example, extern mod whatever = "github.com/mozilla/rust"
1030 view_item_extern_mod(Ident, Option<(@str, StrStyle)>, ~[@MetaItem], NodeId),
1031 view_item_use(~[@view_path]),
1034 // Meta-data associated with an item
1035 pub type Attribute = Spanned<Attribute_>;
1037 // Distinguishes between Attributes that decorate items and Attributes that
1038 // are contained as statements within items. These two cases need to be
1039 // distinguished for pretty-printing.
1040 #[deriving(Clone, Eq, Encodable, Decodable,IterBytes)]
1041 pub enum AttrStyle {
1046 // doc-comments are promoted to attributes that have is_sugared_doc = true
1047 #[deriving(Clone, Eq, Encodable, Decodable,IterBytes)]
1048 pub struct Attribute_ {
1051 is_sugared_doc: bool,
1055 trait_refs appear in impls.
1056 resolve maps each trait_ref's ref_id to its defining trait; that's all
1057 that the ref_id is for. The impl_id maps to the "self type" of this impl.
1058 If this impl is an item_impl, the impl_id is redundant (it could be the
1059 same as the impl's node id).
1061 #[deriving(Clone, Eq, Encodable, Decodable,IterBytes)]
1062 pub struct trait_ref {
1067 #[deriving(Clone, Eq, Encodable, Decodable,IterBytes)]
1068 pub enum visibility {
1075 pub fn inherit_from(&self, parent_visibility: visibility) -> visibility {
1077 &inherited => parent_visibility,
1078 &public | &private => *self
1083 #[deriving(Eq, Encodable, Decodable,IterBytes)]
1084 pub struct struct_field_ {
1085 kind: struct_field_kind,
1088 attrs: ~[Attribute],
1091 pub type struct_field = Spanned<struct_field_>;
1093 #[deriving(Eq, Encodable, Decodable,IterBytes)]
1094 pub enum struct_field_kind {
1095 named_field(Ident, visibility),
1096 unnamed_field // element of a tuple-like struct
1099 #[deriving(Eq, Encodable, Decodable,IterBytes)]
1100 pub struct struct_def {
1101 fields: ~[@struct_field], /* fields, not including ctor */
1102 /* ID of the constructor. This is only used for tuple- or enum-like
1104 ctor_id: Option<NodeId>
1108 FIXME (#3300): Should allow items to be anonymous. Right now
1109 we just use dummy names for anon items.
1111 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
1114 attrs: ~[Attribute],
1121 #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
1123 item_static(Ty, Mutability, @Expr),
1124 item_fn(fn_decl, purity, AbiSet, Generics, Block),
1126 item_foreign_mod(foreign_mod),
1127 item_ty(Ty, Generics),
1128 item_enum(enum_def, Generics),
1129 item_struct(@struct_def, Generics),
1130 item_trait(Generics, ~[trait_ref], ~[trait_method]),
1132 Option<trait_ref>, // (optional) trait this impl implements
1135 // a macro invocation (which includes macro definition)
1139 #[deriving(Eq, Encodable, Decodable,IterBytes)]
1140 pub struct foreign_item {
1142 attrs: ~[Attribute],
1143 node: foreign_item_,
1149 #[deriving(Eq, Encodable, Decodable,IterBytes)]
1150 pub enum foreign_item_ {
1151 foreign_item_fn(fn_decl, Generics),
1152 foreign_item_static(Ty, /* is_mutbl */ bool),
1155 // The data we save and restore about an inlined item or method. This is not
1156 // part of the AST that we parse from a file, but it becomes part of the tree
1158 #[deriving(Eq, Encodable, Decodable,IterBytes)]
1159 pub enum inlined_item {
1161 ii_method(DefId /* impl id */, bool /* is provided */, @method),
1162 ii_foreign(@foreign_item),
1165 /* hold off on tests ... they appear in a later merge.
1168 use std::option::{None, Option, Some};
1175 #[test] fn xorpush_test () {
1178 assert_eq!(s,~[14]);
1182 assert_eq!(s,~[14]);
1184 assert_eq!(s,~[14,15]);
1185 xorPush (&mut s,16);
1186 assert_eq! (s,~[14,15,16]);
1187 xorPush (&mut s,16);
1188 assert_eq! (s,~[14,15]);
1189 xorPush (&mut s,15);
1190 assert_eq! (s,~[14]);
1193 #[test] fn test_marksof () {
1194 let stopname = uints_to_name(&~[12,14,78]);
1197 assert_eq!(s,~[14]);
1199 assert_eq!(s,~[14,15]);
1200 xorPush (&mut s,16);
1201 assert_eq! (s,~[14,15,16]);
1202 xorPush (&mut s,16);
1203 assert_eq! (s,~[14,15]);
1204 xorPush (&mut s,15);
1205 assert_eq! (s,~[14]);
1208 #[test] fn test_marksof () {
1209 let stopname = uints_to_name(&~[12,14,78]);
1210 let name1 = uints_to_name(&~[4,9,7]);
1211 assert_eq!(marksof (MT,stopname),~[]);
1212 assert_eq! (marksof (Mark (4,@Mark(98,@MT)),stopname),~[4,98]);
1213 // does xoring work?
1214 assert_eq! (marksof (Mark (5, @Mark (5, @Mark (16,@MT))),stopname),
1216 // does nested xoring work?
1217 assert_eq! (marksof (Mark (5,
1224 // stop has no effect on marks
1225 assert_eq! (marksof (Mark (9, @Mark (14, @Mark (12, @MT))),stopname),
1227 // rename where stop doesn't match:
1228 assert_eq! (marksof (Mark (9, @Rename
1231 uints_to_name(&~[100,101,102]),
1235 // rename where stop does match
1237 assert_eq! (marksof (Mark(9, @Rename (name1,
1245 // are ASTs encodable?
1246 #[test] fn check_asts_encodable() {
1247 let bogus_span = span {lo:BytePos(10),
1253 module: _mod {view_items: ~[], items: ~[]},
1258 // doesn't matter which encoder we use....
1259 let _f = (@e as @extra::serialize::Encodable<extra::json::Encoder>);
1270 // indent-tabs-mode: nil
1271 // c-basic-offset: 4
1272 // buffer-file-coding-system: utf-8-unix