]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ast.rs
libsyntax: Update view_item_use/import to reflect actual usage
[rust.git] / src / libsyntax / ast.rs
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.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // The Rust abstract syntax tree.
12
13 use codemap::{span, FileName, spanned};
14
15 use core::cast;
16 use core::cmp;
17 use core::option::{None, Option, Some};
18 use core::ptr;
19 use core::task;
20 use core::to_bytes;
21 use core::to_str::ToStr;
22 use std::serialize::{Encodable, Decodable, Encoder, Decoder};
23
24 /* can't import macros yet, so this is copied from token.rs. See its comment
25  * there. */
26 macro_rules! interner_key (
27     () => (cast::transmute::<(uint, uint),
28             &fn(+v: @@::parse::token::ident_interner)>(
29         (-3 as uint, 0u)))
30 )
31
32 #[deriving_eq]
33 pub struct ident { repr: uint }
34
35 pub impl<S: Encoder> Encodable<S> for ident {
36     fn encode(&self, s: &S) {
37         let intr = match unsafe {
38             task::local_data::local_data_get(interner_key!())
39         } {
40             None => fail!(~"encode: TLS interner not set up"),
41             Some(intr) => intr
42         };
43
44         s.emit_owned_str(*(*intr).get(*self));
45     }
46 }
47
48 pub impl<D: Decoder> Decodable<D> for ident {
49     static fn decode(d: &D) -> ident {
50         let intr = match unsafe {
51             task::local_data::local_data_get(interner_key!())
52         } {
53             None => fail!(~"decode: TLS interner not set up"),
54             Some(intr) => intr
55         };
56
57         (*intr).intern(@d.read_owned_str())
58     }
59 }
60
61 pub impl to_bytes::IterBytes for ident {
62     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
63         self.repr.iter_bytes(lsb0, f)
64     }
65 }
66
67 // Functions may or may not have names.
68 pub type fn_ident = Option<ident>;
69
70 pub struct Lifetime {
71     id: node_id,
72     span: span,
73     ident: ident
74 }
75
76 #[auto_encode]
77 #[auto_decode]
78 #[deriving_eq]
79 pub struct path {
80     span: span,
81     global: bool,
82     idents: ~[ident],
83     rp: Option<@region>,
84     types: ~[@Ty],
85 }
86
87 pub type crate_num = int;
88
89 pub type node_id = int;
90
91 #[auto_encode]
92 #[auto_decode]
93 #[deriving_eq]
94 pub struct def_id {
95     crate: crate_num,
96     node: node_id,
97 }
98
99 pub const local_crate: crate_num = 0;
100 pub const crate_node_id: node_id = 0;
101
102 #[auto_encode]
103 #[auto_decode]
104 #[deriving_eq]
105 // The AST represents all type param bounds as types.
106 // typeck::collect::compute_bounds matches these against
107 // the "special" built-in traits (see middle::lang_items) and
108 // detects Copy, Send, Owned, and Const.
109 pub enum ty_param_bound {
110     TraitTyParamBound(@Ty),
111     RegionTyParamBound
112 }
113
114 #[auto_encode]
115 #[auto_decode]
116 #[deriving_eq]
117 pub struct ty_param {
118     ident: ident,
119     id: node_id,
120     bounds: @~[ty_param_bound]
121 }
122
123 #[auto_encode]
124 #[auto_decode]
125 #[deriving_eq]
126 pub enum def {
127     def_fn(def_id, purity),
128     def_static_method(/* method */ def_id,
129                       /* trait */  Option<def_id>,
130                       purity),
131     def_self(node_id, bool /* is_implicit */),
132     def_self_ty(node_id),
133     def_mod(def_id),
134     def_foreign_mod(def_id),
135     def_const(def_id),
136     def_arg(node_id, mode, bool /* is_mutbl */),
137     def_local(node_id, bool /* is_mutbl */),
138     def_variant(def_id /* enum */, def_id /* variant */),
139     def_ty(def_id),
140     def_prim_ty(prim_ty),
141     def_ty_param(def_id, uint),
142     def_binding(node_id, binding_mode),
143     def_use(def_id),
144     def_upvar(node_id,  // id of closed over var
145               @def,     // closed over def
146               node_id,  // expr node that creates the closure
147               node_id), // id for the block/body of the closure expr
148     def_struct(def_id),
149     def_typaram_binder(node_id), /* struct, impl or trait with ty params */
150     def_region(node_id),
151     def_label(node_id)
152 }
153
154
155 // The set of meta_items that define the compilation environment of the crate,
156 // used to drive conditional compilation
157 pub type crate_cfg = ~[@meta_item];
158
159 pub type crate = spanned<crate_>;
160
161 #[auto_encode]
162 #[auto_decode]
163 #[deriving_eq]
164 pub struct crate_ {
165     module: _mod,
166     attrs: ~[attribute],
167     config: crate_cfg,
168 }
169
170 pub type meta_item = spanned<meta_item_>;
171
172 #[auto_encode]
173 #[auto_decode]
174 #[deriving_eq]
175 pub enum meta_item_ {
176     meta_word(~str),
177     meta_list(~str, ~[@meta_item]),
178     meta_name_value(~str, lit),
179 }
180
181 pub type blk = spanned<blk_>;
182
183 #[auto_encode]
184 #[auto_decode]
185 #[deriving_eq]
186 pub struct blk_ {
187     view_items: ~[@view_item],
188     stmts: ~[@stmt],
189     expr: Option<@expr>,
190     id: node_id,
191     rules: blk_check_mode,
192 }
193
194 #[auto_encode]
195 #[auto_decode]
196 #[deriving_eq]
197 pub struct pat {
198     id: node_id,
199     node: pat_,
200     span: span,
201 }
202
203 #[auto_encode]
204 #[auto_decode]
205 #[deriving_eq]
206 pub struct field_pat {
207     ident: ident,
208     pat: @pat,
209 }
210
211 #[auto_encode]
212 #[auto_decode]
213 #[deriving_eq]
214 pub enum binding_mode {
215     bind_by_copy,
216     bind_by_ref(mutability),
217     bind_infer
218 }
219
220 pub impl to_bytes::IterBytes for binding_mode {
221     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
222         match *self {
223           bind_by_copy => 0u8.iter_bytes(lsb0, f),
224
225           bind_by_ref(ref m) =>
226           to_bytes::iter_bytes_2(&1u8, m, lsb0, f),
227
228           bind_infer =>
229           2u8.iter_bytes(lsb0, f),
230         }
231     }
232 }
233
234 #[auto_encode]
235 #[auto_decode]
236 #[deriving_eq]
237 pub enum pat_ {
238     pat_wild,
239     // A pat_ident may either be a new bound variable,
240     // or a nullary enum (in which case the second field
241     // is None).
242     // In the nullary enum case, the parser can't determine
243     // which it is. The resolver determines this, and
244     // records this pattern's node_id in an auxiliary
245     // set (of "pat_idents that refer to nullary enums")
246     pat_ident(binding_mode, @path, Option<@pat>),
247     pat_enum(@path, Option<~[@pat]>), // "none" means a * pattern where
248                                   // we don't bind the fields to names
249     pat_rec(~[field_pat], bool),
250     pat_struct(@path, ~[field_pat], bool),
251     pat_tup(~[@pat]),
252     pat_box(@pat),
253     pat_uniq(@pat),
254     pat_region(@pat), // borrowed pointer pattern
255     pat_lit(@expr),
256     pat_range(@expr, @expr),
257     pat_vec(~[@pat], Option<@pat>)
258 }
259
260 #[auto_encode]
261 #[auto_decode]
262 #[deriving_eq]
263 pub enum mutability { m_mutbl, m_imm, m_const, }
264
265 pub impl to_bytes::IterBytes for mutability {
266     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
267         (*self as u8).iter_bytes(lsb0, f)
268     }
269 }
270
271 #[auto_encode]
272 #[auto_decode]
273 #[deriving_eq]
274 pub enum Abi {
275     RustAbi
276 }
277
278 pub impl to_bytes::IterBytes for Abi {
279     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
280         (*self as uint).iter_bytes(lsb0, f)
281     }
282 }
283
284 pub impl ToStr for Abi {
285     pure fn to_str(&self) -> ~str {
286         match *self {
287             RustAbi => ~"\"rust\""
288         }
289     }
290 }
291
292 #[auto_encode]
293 #[auto_decode]
294 #[deriving_eq]
295 pub enum Sigil {
296     BorrowedSigil,
297     OwnedSigil,
298     ManagedSigil
299 }
300
301 pub impl to_bytes::IterBytes for Sigil {
302     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
303         (*self as uint).iter_bytes(lsb0, f)
304     }
305 }
306
307 pub impl ToStr for Sigil {
308     pure fn to_str(&self) -> ~str {
309         match *self {
310             BorrowedSigil => ~"&",
311             OwnedSigil => ~"~",
312             ManagedSigil => ~"@"
313          }
314     }
315 }
316
317 #[auto_encode]
318 #[auto_decode]
319 #[deriving_eq]
320 pub enum vstore {
321     // FIXME (#3469): Change uint to @expr (actually only constant exprs)
322     vstore_fixed(Option<uint>),   // [1,2,3,4]
323     vstore_uniq,                  // ~[1,2,3,4]
324     vstore_box,                   // @[1,2,3,4]
325     vstore_slice(@region)         // &[1,2,3,4](foo)?
326 }
327
328 #[auto_encode]
329 #[auto_decode]
330 #[deriving_eq]
331 pub enum expr_vstore {
332     // FIXME (#3469): Change uint to @expr (actually only constant exprs)
333     expr_vstore_fixed(Option<uint>),   // [1,2,3,4]
334     expr_vstore_uniq,                  // ~[1,2,3,4]
335     expr_vstore_box,                   // @[1,2,3,4]
336     expr_vstore_mut_box,               // @mut [1,2,3,4]
337     expr_vstore_slice,                 // &[1,2,3,4]
338     expr_vstore_mut_slice,             // &mut [1,2,3,4]
339 }
340
341 #[auto_encode]
342 #[auto_decode]
343 #[deriving_eq]
344 pub enum binop {
345     add,
346     subtract,
347     mul,
348     div,
349     rem,
350     and,
351     or,
352     bitxor,
353     bitand,
354     bitor,
355     shl,
356     shr,
357     eq,
358     lt,
359     le,
360     ne,
361     ge,
362     gt,
363 }
364
365 #[auto_encode]
366 #[auto_decode]
367 #[deriving_eq]
368 pub enum unop {
369     box(mutability),
370     uniq(mutability),
371     deref,
372     not,
373     neg
374 }
375
376 // Generally, after typeck you can get the inferred value
377 // using ty::resolved_T(...).
378 #[auto_encode]
379 #[auto_decode]
380 #[deriving_eq]
381 pub enum inferable<T> {
382     expl(T),
383     infer(node_id)
384 }
385
386 pub impl<T: to_bytes::IterBytes> to_bytes::IterBytes for inferable<T> {
387     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
388         match *self {
389           expl(ref t) =>
390           to_bytes::iter_bytes_2(&0u8, t, lsb0, f),
391
392           infer(ref n) =>
393           to_bytes::iter_bytes_2(&1u8, n, lsb0, f),
394         }
395     }
396 }
397
398 // "resolved" mode: the real modes.
399 #[auto_encode]
400 #[auto_decode]
401 #[deriving_eq]
402 pub enum rmode { by_ref, by_val, by_copy }
403
404 pub impl to_bytes::IterBytes for rmode {
405     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
406         (*self as u8).iter_bytes(lsb0, f)
407     }
408 }
409
410 // inferable mode.
411 pub type mode = inferable<rmode>;
412
413 pub type stmt = spanned<stmt_>;
414
415 #[auto_encode]
416 #[auto_decode]
417 #[deriving_eq]
418 pub enum stmt_ {
419     stmt_decl(@decl, node_id),
420
421     // expr without trailing semi-colon (must have unit type):
422     stmt_expr(@expr, node_id),
423
424     // expr with trailing semi-colon (may have any type):
425     stmt_semi(@expr, node_id),
426
427     // bool: is there a trailing sem-colon?
428     stmt_mac(mac, bool),
429 }
430
431 // FIXME (pending discussion of #1697, #2178...): local should really be
432 // a refinement on pat.
433 #[auto_encode]
434 #[auto_decode]
435 #[deriving_eq]
436 pub struct local_ {
437     is_mutbl: bool,
438     ty: @Ty,
439     pat: @pat,
440     init: Option<@expr>,
441     id: node_id,
442 }
443
444 pub type local = spanned<local_>;
445
446 pub type decl = spanned<decl_>;
447
448 #[auto_encode]
449 #[auto_decode]
450 #[deriving_eq]
451 pub enum decl_ { decl_local(~[@local]), decl_item(@item), }
452
453 #[auto_encode]
454 #[auto_decode]
455 #[deriving_eq]
456 pub struct arm {
457     pats: ~[@pat],
458     guard: Option<@expr>,
459     body: blk,
460 }
461
462 #[auto_encode]
463 #[auto_decode]
464 #[deriving_eq]
465 pub struct field_ {
466     mutbl: mutability,
467     ident: ident,
468     expr: @expr,
469 }
470
471 pub type field = spanned<field_>;
472
473 #[auto_encode]
474 #[auto_decode]
475 #[deriving_eq]
476 pub enum blk_check_mode { default_blk, unsafe_blk, }
477
478 #[auto_encode]
479 #[auto_decode]
480 #[deriving_eq]
481 pub struct expr {
482     id: node_id,
483     // Extra node ID is only used for index, assign_op, unary, binary, method
484     // call
485     callee_id: node_id,
486     node: expr_,
487     span: span,
488 }
489
490 #[auto_encode]
491 #[auto_decode]
492 #[deriving_eq]
493 pub enum log_level { error, debug, log_other }
494 // 0 = error, 1 = debug, 2 = log_other
495
496 #[auto_encode]
497 #[auto_decode]
498 #[deriving_eq]
499 pub enum CallSugar {
500     NoSugar,
501     DoSugar,
502     ForSugar
503 }
504
505 #[auto_encode]
506 #[auto_decode]
507 #[deriving_eq]
508 pub enum expr_ {
509     expr_vstore(@expr, expr_vstore),
510     expr_vec(~[@expr], mutability),
511     expr_rec(~[field], Option<@expr>),
512     expr_call(@expr, ~[@expr], CallSugar),
513     expr_method_call(@expr, ident, ~[@Ty], ~[@expr], CallSugar),
514     expr_tup(~[@expr]),
515     expr_binary(binop, @expr, @expr),
516     expr_unary(unop, @expr),
517     expr_lit(@lit),
518     expr_cast(@expr, @Ty),
519     expr_if(@expr, blk, Option<@expr>),
520     expr_while(@expr, blk),
521     /* Conditionless loop (can be exited with break, cont, or ret)
522        Same semantics as while(true) { body }, but typestate knows that the
523        (implicit) condition is always true. */
524     expr_loop(blk, Option<ident>),
525     expr_match(@expr, ~[arm]),
526
527     // FIXME(#4717) the @() is req'd on windows or else LLVM croaks
528     expr_fn(Sigil, fn_decl, blk, @()),
529
530     expr_fn_block(fn_decl, blk),
531     // Inner expr is always an expr_fn_block. We need the wrapping node to
532     // easily type this (a function returning nil on the inside but bool on
533     // the outside).
534     expr_loop_body(@expr),
535     // Like expr_loop_body but for 'do' blocks
536     expr_do_body(@expr),
537     expr_block(blk),
538
539     expr_copy(@expr),
540     expr_assign(@expr, @expr),
541     expr_swap(@expr, @expr),
542     expr_assign_op(binop, @expr, @expr),
543     expr_field(@expr, ident, ~[@Ty]),
544     expr_index(@expr, @expr),
545     expr_path(@path),
546     expr_addr_of(mutability, @expr),
547     expr_break(Option<ident>),
548     expr_again(Option<ident>),
549     expr_ret(Option<@expr>),
550     expr_log(log_level, @expr, @expr),
551
552     /* just an assert */
553     expr_assert(@expr),
554
555     expr_mac(mac),
556
557     // A struct literal expression.
558     expr_struct(@path, ~[field], Option<@expr>),
559
560     // A vector literal constructed from one repeated element.
561     expr_repeat(@expr /* element */, @expr /* count */, mutability),
562
563     // No-op: used solely so we can pretty-print faithfully
564     expr_paren(@expr)
565 }
566
567 // When the main rust parser encounters a syntax-extension invocation, it
568 // parses the arguments to the invocation as a token-tree. This is a very
569 // loose structure, such that all sorts of different AST-fragments can
570 // be passed to syntax extensions using a uniform type.
571 //
572 // If the syntax extension is an MBE macro, it will attempt to match its
573 // LHS "matchers" against the provided token tree, and if it finds a
574 // match, will transcribe the RHS token tree, splicing in any captured
575 // macro_parser::matched_nonterminals into the tt_nonterminals it finds.
576 //
577 // The RHS of an MBE macro is the only place a tt_nonterminal or tt_seq
578 // makes any real sense. You could write them elsewhere but nothing
579 // else knows what to do with them, so you'll probably get a syntax
580 // error.
581 //
582 #[auto_encode]
583 #[auto_decode]
584 #[deriving_eq]
585 #[doc="For macro invocations; parsing is delegated to the macro"]
586 pub enum token_tree {
587     // a single token
588     tt_tok(span, ::parse::token::Token),
589     // a delimited sequence (the delimiters appear as the first
590     // and last elements of the vector)
591     tt_delim(~[token_tree]),
592     // These only make sense for right-hand-sides of MBE macros:
593
594     // a kleene-style repetition sequence with a span, a tt_forest,
595     // an optional separator (?), and a boolean where true indicates
596     // zero or more (*), and false indicates one or more (+).
597     tt_seq(span, ~[token_tree], Option<::parse::token::Token>, bool),
598
599     // a syntactic variable that will be filled in by macro expansion.
600     tt_nonterminal(span, ident)
601 }
602
603 //
604 // Matchers are nodes defined-by and recognized-by the main rust parser and
605 // language, but they're only ever found inside syntax-extension invocations;
606 // indeed, the only thing that ever _activates_ the rules in the rust parser
607 // for parsing a matcher is a matcher looking for the 'matchers' nonterminal
608 // itself. Matchers represent a small sub-language for pattern-matching
609 // token-trees, and are thus primarily used by the macro-defining extension
610 // itself.
611 //
612 // match_tok
613 // ---------
614 //
615 //     A matcher that matches a single token, denoted by the token itself. So
616 //     long as there's no $ involved.
617 //
618 //
619 // match_seq
620 // ---------
621 //
622 //     A matcher that matches a sequence of sub-matchers, denoted various
623 //     possible ways:
624 //
625 //             $(M)*       zero or more Ms
626 //             $(M)+       one or more Ms
627 //             $(M),+      one or more comma-separated Ms
628 //             $(A B C);*  zero or more semi-separated 'A B C' seqs
629 //
630 //
631 // match_nonterminal
632 // -----------------
633 //
634 //     A matcher that matches one of a few interesting named rust
635 //     nonterminals, such as types, expressions, items, or raw token-trees. A
636 //     black-box matcher on expr, for example, binds an expr to a given ident,
637 //     and that ident can re-occur as an interpolation in the RHS of a
638 //     macro-by-example rule. For example:
639 //
640 //        $foo:expr   =>     1 + $foo    // interpolate an expr
641 //        $foo:tt     =>     $foo        // interpolate a token-tree
642 //        $foo:tt     =>     bar! $foo   // only other valid interpolation
643 //                                       // is in arg position for another
644 //                                       // macro
645 //
646 // As a final, horrifying aside, note that macro-by-example's input is
647 // also matched by one of these matchers. Holy self-referential! It is matched
648 // by an match_seq, specifically this one:
649 //
650 //                   $( $lhs:matchers => $rhs:tt );+
651 //
652 // If you understand that, you have closed to loop and understand the whole
653 // macro system. Congratulations.
654 //
655 pub type matcher = spanned<matcher_>;
656
657 #[auto_encode]
658 #[auto_decode]
659 #[deriving_eq]
660 pub enum matcher_ {
661     // match one token
662     match_tok(::parse::token::Token),
663     // match repetitions of a sequence: body, separator, zero ok?,
664     // lo, hi position-in-match-array used:
665     match_seq(~[matcher], Option<::parse::token::Token>, bool, uint, uint),
666     // parse a Rust NT: name to bind, name of NT, position in match array:
667     match_nonterminal(ident, ident, uint)
668 }
669
670 pub type mac = spanned<mac_>;
671
672 #[auto_encode]
673 #[auto_decode]
674 #[deriving_eq]
675 pub enum mac_ {
676     mac_invoc_tt(@path,~[token_tree]),   // new macro-invocation
677 }
678
679 pub type lit = spanned<lit_>;
680
681 #[auto_encode]
682 #[auto_decode]
683 #[deriving_eq]
684 pub enum lit_ {
685     lit_str(@~str),
686     lit_int(i64, int_ty),
687     lit_uint(u64, uint_ty),
688     lit_int_unsuffixed(i64),
689     lit_float(@~str, float_ty),
690     lit_float_unsuffixed(@~str),
691     lit_nil,
692     lit_bool(bool),
693 }
694
695 // NB: If you change this, you'll probably want to change the corresponding
696 // type structure in middle/ty.rs as well.
697 #[auto_encode]
698 #[auto_decode]
699 #[deriving_eq]
700 pub struct mt {
701     ty: @Ty,
702     mutbl: mutability,
703 }
704
705 #[auto_encode]
706 #[auto_decode]
707 #[deriving_eq]
708 pub struct ty_field_ {
709     ident: ident,
710     mt: mt,
711 }
712
713 pub type ty_field = spanned<ty_field_>;
714
715 #[auto_encode]
716 #[auto_decode]
717 #[deriving_eq]
718 pub struct ty_method {
719     ident: ident,
720     attrs: ~[attribute],
721     purity: purity,
722     decl: fn_decl,
723     tps: ~[ty_param],
724     self_ty: self_ty,
725     id: node_id,
726     span: span,
727 }
728
729 #[auto_encode]
730 #[auto_decode]
731 #[deriving_eq]
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
734 // implementation).
735 pub enum trait_method {
736     required(ty_method),
737     provided(@method),
738 }
739
740 #[auto_encode]
741 #[auto_decode]
742 #[deriving_eq]
743 pub enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, }
744
745 pub impl ToStr for int_ty {
746     pure fn to_str(&self) -> ~str {
747         ::ast_util::int_ty_to_str(*self)
748     }
749 }
750
751 pub impl to_bytes::IterBytes for int_ty {
752     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
753         (*self as u8).iter_bytes(lsb0, f)
754     }
755 }
756
757 #[auto_encode]
758 #[auto_decode]
759 #[deriving_eq]
760 pub enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, }
761
762 pub impl ToStr for uint_ty {
763     pure fn to_str(&self) -> ~str {
764         ::ast_util::uint_ty_to_str(*self)
765     }
766 }
767
768 pub impl to_bytes::IterBytes for uint_ty {
769     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
770         (*self as u8).iter_bytes(lsb0, f)
771     }
772 }
773
774 #[auto_encode]
775 #[auto_decode]
776 #[deriving_eq]
777 pub enum float_ty { ty_f, ty_f32, ty_f64, }
778
779 pub impl ToStr for float_ty {
780     pure fn to_str(&self) -> ~str {
781         ::ast_util::float_ty_to_str(*self)
782     }
783 }
784
785 pub impl to_bytes::IterBytes for float_ty {
786     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
787         (*self as u8).iter_bytes(lsb0, f)
788     }
789 }
790
791 // NB Eq method appears below.
792 #[auto_encode]
793 #[auto_decode]
794 #[deriving_eq]
795 pub struct Ty {
796     id: node_id,
797     node: ty_,
798     span: span,
799 }
800
801 // Not represented directly in the AST, referred to by name through a ty_path.
802 #[auto_encode]
803 #[auto_decode]
804 #[deriving_eq]
805 pub enum prim_ty {
806     ty_int(int_ty),
807     ty_uint(uint_ty),
808     ty_float(float_ty),
809     ty_str,
810     ty_bool,
811 }
812
813 #[auto_encode]
814 #[auto_decode]
815 #[deriving_eq]
816 pub struct region {
817     id: node_id,
818     node: region_,
819 }
820
821 #[auto_encode]
822 #[auto_decode]
823 #[deriving_eq]
824 pub enum region_ {
825     re_anon,
826     re_static,
827     re_self,
828     re_named(ident)
829 }
830
831 #[auto_encode]
832 #[auto_decode]
833 #[deriving_eq]
834 pub enum Onceness {
835     Once,
836     Many
837 }
838
839 pub impl ToStr for Onceness {
840     pure fn to_str(&self) -> ~str {
841         match *self {
842             Once => ~"once",
843             Many => ~"many"
844         }
845     }
846 }
847
848 pub impl to_bytes::IterBytes for Onceness {
849     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
850         (*self as uint).iter_bytes(lsb0, f);
851     }
852 }
853
854 #[auto_encode]
855 #[auto_decode]
856 #[deriving_eq]
857 pub struct TyClosure {
858     sigil: Sigil,
859     region: Option<@region>,
860     purity: purity,
861     onceness: Onceness,
862     decl: fn_decl
863 }
864
865 #[auto_encode]
866 #[auto_decode]
867 #[deriving_eq]
868 pub struct TyBareFn {
869     purity: purity,
870     abi: Abi,
871     decl: fn_decl
872 }
873
874 #[auto_encode]
875 #[auto_decode]
876 #[deriving_eq]
877 pub enum ty_ {
878     ty_nil,
879     ty_bot, /* bottom type */
880     ty_box(mt),
881     ty_uniq(mt),
882     ty_vec(mt),
883     ty_fixed_length_vec(mt, uint),
884     ty_ptr(mt),
885     ty_rptr(@region, mt),
886     ty_rec(~[ty_field]),
887     ty_closure(@TyClosure),
888     ty_bare_fn(@TyBareFn),
889     ty_tup(~[@Ty]),
890     ty_path(@path, node_id),
891     ty_mac(mac),
892     // ty_infer means the type should be inferred instead of it having been
893     // specified. This should only appear at the "top level" of a type and not
894     // nested in one.
895     ty_infer,
896 }
897
898 pub impl to_bytes::IterBytes for Ty {
899     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
900         to_bytes::iter_bytes_2(&self.span.lo, &self.span.hi, lsb0, f);
901     }
902 }
903
904 #[auto_encode]
905 #[auto_decode]
906 #[deriving_eq]
907 pub struct arg {
908     mode: mode,
909     is_mutbl: bool,
910     ty: @Ty,
911     pat: @pat,
912     id: node_id,
913 }
914
915 #[auto_encode]
916 #[auto_decode]
917 #[deriving_eq]
918 pub struct fn_decl {
919     inputs: ~[arg],
920     output: @Ty,
921     cf: ret_style,
922 }
923
924 #[auto_encode]
925 #[auto_decode]
926 #[deriving_eq]
927 pub enum purity {
928     pure_fn, // declared with "pure fn"
929     unsafe_fn, // declared with "unsafe fn"
930     impure_fn, // declared with "fn"
931     extern_fn, // declared with "extern fn"
932 }
933
934 pub impl ToStr for purity {
935     pure fn to_str(&self) -> ~str {
936         match *self {
937             impure_fn => ~"impure",
938             unsafe_fn => ~"unsafe",
939             pure_fn => ~"pure",
940             extern_fn => ~"extern"
941         }
942     }
943 }
944
945 pub impl to_bytes::IterBytes for purity {
946     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
947         (*self as u8).iter_bytes(lsb0, f)
948     }
949 }
950
951 #[auto_encode]
952 #[auto_decode]
953 #[deriving_eq]
954 pub enum ret_style {
955     noreturn, // functions with return type _|_ that always
956               // raise an error or exit (i.e. never return to the caller)
957     return_val, // everything else
958 }
959
960 pub impl to_bytes::IterBytes for ret_style {
961     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
962         (*self as u8).iter_bytes(lsb0, f)
963     }
964 }
965
966 #[auto_encode]
967 #[auto_decode]
968 #[deriving_eq]
969 pub enum self_ty_ {
970     sty_static,                         // no self: static method
971     sty_by_ref,                         // old by-reference self: ``
972     sty_value,                          // by-value self: `self`
973     sty_region(mutability),             // by-region self: `&self`
974     sty_box(mutability),                // by-managed-pointer self: `@self`
975     sty_uniq(mutability)                // by-unique-pointer self: `~self`
976 }
977
978 pub type self_ty = spanned<self_ty_>;
979
980 #[auto_encode]
981 #[auto_decode]
982 #[deriving_eq]
983 pub struct method {
984     ident: ident,
985     attrs: ~[attribute],
986     tps: ~[ty_param],
987     self_ty: self_ty,
988     purity: purity,
989     decl: fn_decl,
990     body: blk,
991     id: node_id,
992     span: span,
993     self_id: node_id,
994     vis: visibility,
995 }
996
997 #[auto_encode]
998 #[auto_decode]
999 #[deriving_eq]
1000 pub struct _mod {
1001     view_items: ~[@view_item],
1002     items: ~[@item],
1003 }
1004
1005 #[auto_encode]
1006 #[auto_decode]
1007 #[deriving_eq]
1008 pub enum foreign_abi {
1009     foreign_abi_rust_intrinsic,
1010     foreign_abi_cdecl,
1011     foreign_abi_stdcall,
1012 }
1013
1014 // Foreign mods can be named or anonymous
1015 #[auto_encode]
1016 #[auto_decode]
1017 #[deriving_eq]
1018 pub enum foreign_mod_sort { named, anonymous }
1019
1020 #[auto_encode]
1021 #[auto_decode]
1022 #[deriving_eq]
1023 pub struct foreign_mod {
1024     sort: foreign_mod_sort,
1025     abi: ident,
1026     view_items: ~[@view_item],
1027     items: ~[@foreign_item],
1028 }
1029
1030 #[auto_encode]
1031 #[auto_decode]
1032 #[deriving_eq]
1033 pub struct variant_arg {
1034     ty: @Ty,
1035     id: node_id,
1036 }
1037
1038 #[auto_encode]
1039 #[auto_decode]
1040 #[deriving_eq]
1041 pub enum variant_kind {
1042     tuple_variant_kind(~[variant_arg]),
1043     struct_variant_kind(@struct_def),
1044     enum_variant_kind(enum_def)
1045 }
1046
1047 #[auto_encode]
1048 #[auto_decode]
1049 #[deriving_eq]
1050 pub struct enum_def_ {
1051     variants: ~[variant],
1052     common: Option<@struct_def>,
1053 }
1054
1055 #[auto_encode]
1056 #[auto_decode]
1057 #[deriving_eq]
1058 pub enum enum_def = enum_def_;
1059
1060 #[auto_encode]
1061 #[auto_decode]
1062 #[deriving_eq]
1063 pub struct variant_ {
1064     name: ident,
1065     attrs: ~[attribute],
1066     kind: variant_kind,
1067     id: node_id,
1068     disr_expr: Option<@expr>,
1069     vis: visibility,
1070 }
1071
1072 pub type variant = spanned<variant_>;
1073
1074 #[auto_encode]
1075 #[auto_decode]
1076 #[deriving_eq]
1077 pub struct path_list_ident_ {
1078     name: ident,
1079     id: node_id,
1080 }
1081
1082 pub type path_list_ident = spanned<path_list_ident_>;
1083
1084 #[auto_encode]
1085 #[auto_decode]
1086 #[deriving_eq]
1087 pub enum namespace { module_ns, type_value_ns }
1088
1089 pub type view_path = spanned<view_path_>;
1090
1091 #[auto_encode]
1092 #[auto_decode]
1093 #[deriving_eq]
1094 pub enum view_path_ {
1095
1096     // quux = foo::bar::baz
1097     //
1098     // or just
1099     //
1100     // foo::bar::baz  (with 'baz =' implicitly on the left)
1101     view_path_simple(ident, @path, namespace, node_id),
1102
1103     // foo::bar::*
1104     view_path_glob(@path, node_id),
1105
1106     // foo::bar::{a,b,c}
1107     view_path_list(@path, ~[path_list_ident], node_id)
1108 }
1109
1110 #[auto_encode]
1111 #[auto_decode]
1112 #[deriving_eq]
1113 pub struct view_item {
1114     node: view_item_,
1115     attrs: ~[attribute],
1116     vis: visibility,
1117     span: span,
1118 }
1119
1120 #[auto_encode]
1121 #[auto_decode]
1122 #[deriving_eq]
1123 pub enum view_item_ {
1124     view_item_extern_mod(ident, ~[@meta_item], node_id),
1125     view_item_use(~[@view_path]),
1126 }
1127
1128 // Meta-data associated with an item
1129 pub type attribute = spanned<attribute_>;
1130
1131 // Distinguishes between attributes that decorate items and attributes that
1132 // are contained as statements within items. These two cases need to be
1133 // distinguished for pretty-printing.
1134 #[auto_encode]
1135 #[auto_decode]
1136 #[deriving_eq]
1137 pub enum attr_style { attr_outer, attr_inner, }
1138
1139 // doc-comments are promoted to attributes that have is_sugared_doc = true
1140 #[auto_encode]
1141 #[auto_decode]
1142 #[deriving_eq]
1143 pub struct attribute_ {
1144     style: attr_style,
1145     value: meta_item,
1146     is_sugared_doc: bool,
1147 }
1148
1149 /*
1150   trait_refs appear in impls.
1151   resolve maps each trait_ref's ref_id to its defining trait; that's all
1152   that the ref_id is for. The impl_id maps to the "self type" of this impl.
1153   If this impl is an item_impl, the impl_id is redundant (it could be the
1154   same as the impl's node id).
1155  */
1156 #[auto_encode]
1157 #[auto_decode]
1158 #[deriving_eq]
1159 pub struct trait_ref {
1160     path: @path,
1161     ref_id: node_id,
1162 }
1163
1164 #[auto_encode]
1165 #[auto_decode]
1166 #[deriving_eq]
1167 pub enum visibility { public, private, inherited }
1168
1169 #[auto_encode]
1170 #[auto_decode]
1171 #[deriving_eq]
1172 pub struct struct_field_ {
1173     kind: struct_field_kind,
1174     id: node_id,
1175     ty: @Ty,
1176 }
1177
1178 pub type struct_field = spanned<struct_field_>;
1179
1180 #[auto_encode]
1181 #[auto_decode]
1182 #[deriving_eq]
1183 pub enum struct_field_kind {
1184     named_field(ident, struct_mutability, visibility),
1185     unnamed_field   // element of a tuple-like struct
1186 }
1187
1188 #[auto_encode]
1189 #[auto_decode]
1190 #[deriving_eq]
1191 pub struct struct_def {
1192     fields: ~[@struct_field], /* fields */
1193     /* (not including ctor or dtor) */
1194     /* dtor is optional */
1195     dtor: Option<struct_dtor>,
1196     /* ID of the constructor. This is only used for tuple- or enum-like
1197      * structs. */
1198     ctor_id: Option<node_id>
1199 }
1200
1201 /*
1202   FIXME (#3300): Should allow items to be anonymous. Right now
1203   we just use dummy names for anon items.
1204  */
1205 #[auto_encode]
1206 #[auto_decode]
1207 #[deriving_eq]
1208 pub struct item {
1209     ident: ident,
1210     attrs: ~[attribute],
1211     id: node_id,
1212     node: item_,
1213     vis: visibility,
1214     span: span,
1215 }
1216
1217 #[auto_encode]
1218 #[auto_decode]
1219 #[deriving_eq]
1220 pub enum item_ {
1221     item_const(@Ty, @expr),
1222     item_fn(fn_decl, purity, ~[ty_param], blk),
1223     item_mod(_mod),
1224     item_foreign_mod(foreign_mod),
1225     item_ty(@Ty, ~[ty_param]),
1226     item_enum(enum_def, ~[ty_param]),
1227     item_struct(@struct_def, ~[ty_param]),
1228     item_trait(~[ty_param], ~[@trait_ref], ~[trait_method]),
1229     item_impl(~[ty_param],
1230               Option<@trait_ref>, // (optional) trait this impl implements
1231               @Ty, // self
1232               ~[@method]),
1233     item_mac(mac),
1234 }
1235
1236 #[auto_encode]
1237 #[auto_decode]
1238 #[deriving_eq]
1239 pub enum struct_mutability { struct_mutable, struct_immutable }
1240
1241 pub impl to_bytes::IterBytes for struct_mutability {
1242     pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
1243         (*self as u8).iter_bytes(lsb0, f)
1244     }
1245 }
1246
1247 pub type struct_dtor = spanned<struct_dtor_>;
1248
1249 #[auto_encode]
1250 #[auto_decode]
1251 #[deriving_eq]
1252 pub struct struct_dtor_ {
1253     id: node_id,
1254     attrs: ~[attribute],
1255     self_id: node_id,
1256     body: blk,
1257 }
1258
1259 #[auto_encode]
1260 #[auto_decode]
1261 #[deriving_eq]
1262 pub struct foreign_item {
1263     ident: ident,
1264     attrs: ~[attribute],
1265     node: foreign_item_,
1266     id: node_id,
1267     span: span,
1268     vis: visibility,
1269 }
1270
1271 #[auto_encode]
1272 #[auto_decode]
1273 #[deriving_eq]
1274 pub enum foreign_item_ {
1275     foreign_item_fn(fn_decl, purity, ~[ty_param]),
1276     foreign_item_const(@Ty)
1277 }
1278
1279 // The data we save and restore about an inlined item or method.  This is not
1280 // part of the AST that we parse from a file, but it becomes part of the tree
1281 // that we trans.
1282 #[auto_encode]
1283 #[auto_decode]
1284 #[deriving_eq]
1285 pub enum inlined_item {
1286     ii_item(@item),
1287     ii_method(def_id /* impl id */, @method),
1288     ii_foreign(@foreign_item),
1289     ii_dtor(struct_dtor, ident, ~[ty_param], def_id /* parent id */)
1290 }
1291
1292 #[cfg(test)]
1293 mod test {
1294     use std;
1295     use codemap::*;
1296     use super::*;
1297
1298     //are asts encodable?
1299
1300     // it looks like this *will* be a compiler bug, after
1301     // I get deriving_eq for crates into incoming :)
1302     /*
1303     #[test] fn check_asts_encodable() {
1304         let bogus_span = span {lo:BytePos(10),
1305                                hi:BytePos(20),
1306                                expn_info:None};
1307         let _e : crate =
1308             spanned{
1309             node: crate_{
1310                 module: _mod {view_items: ~[], items: ~[]},
1311                 attrs: ~[],
1312                 config: ~[]
1313             },
1314             span: bogus_span};
1315         // doesn't matter which encoder we use....
1316         let _f = (_e as std::serialize::Encodable::<std::json::Encoder>);
1317     }
1318     */
1319 }
1320 //
1321 // Local Variables:
1322 // mode: rust
1323 // fill-column: 78;
1324 // indent-tabs-mode: nil
1325 // c-basic-offset: 4
1326 // buffer-file-coding-system: utf-8-unix
1327 // End:
1328 //