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, FileName, spanned};
17 use core::option::{None, Option, Some};
21 use core::to_str::ToStr;
22 use std::serialize::{Encodable, Decodable, Encoder, Decoder};
24 /* can't import macros yet, so this is copied from token.rs. See its comment
26 macro_rules! interner_key (
27 () => (cast::transmute::<(uint, uint),
28 &fn(+v: @@::parse::token::ident_interner)>(
33 pub struct ident { repr: uint }
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!())
40 None => fail!(~"encode: TLS interner not set up"),
44 s.emit_owned_str(*(*intr).get(*self));
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!())
53 None => fail!(~"decode: TLS interner not set up"),
57 (*intr).intern(@d.read_owned_str())
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)
67 // Functions may or may not have names.
68 pub type fn_ident = Option<ident>;
87 pub type crate_num = int;
89 pub type node_id = int;
99 pub const local_crate: crate_num = 0;
100 pub const crate_node_id: node_id = 0;
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),
117 pub struct ty_param {
120 bounds: @~[ty_param_bound]
127 def_fn(def_id, purity),
128 def_static_method(/* method */ def_id,
129 /* trait */ Option<def_id>,
131 def_self(node_id, bool /* is_implicit */),
132 def_self_ty(node_id),
134 def_foreign_mod(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 */),
140 def_prim_ty(prim_ty),
141 def_ty_param(def_id, uint),
142 def_binding(node_id, binding_mode),
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
149 def_typaram_binder(node_id), /* struct, impl or trait with ty params */
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];
159 pub type crate = spanned<crate_>;
170 pub type meta_item = spanned<meta_item_>;
175 pub enum meta_item_ {
177 meta_list(~str, ~[@meta_item]),
178 meta_name_value(~str, lit),
181 pub type blk = spanned<blk_>;
187 view_items: ~[@view_item],
191 rules: blk_check_mode,
206 pub struct field_pat {
214 pub enum binding_mode {
216 bind_by_ref(mutability),
220 pub impl to_bytes::IterBytes for binding_mode {
221 pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
223 bind_by_copy => 0u8.iter_bytes(lsb0, f),
225 bind_by_ref(ref m) =>
226 to_bytes::iter_bytes_2(&1u8, m, lsb0, f),
229 2u8.iter_bytes(lsb0, f),
239 // A pat_ident may either be a new bound variable,
240 // or a nullary enum (in which case the second field
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),
254 pat_region(@pat), // borrowed pointer pattern
256 pat_range(@expr, @expr),
257 pat_vec(~[@pat], Option<@pat>)
263 pub enum mutability { m_mutbl, m_imm, m_const, }
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)
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)
284 pub impl ToStr for Abi {
285 pure fn to_str(&self) -> ~str {
287 RustAbi => ~"\"rust\""
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)
307 pub impl ToStr for Sigil {
308 pure fn to_str(&self) -> ~str {
310 BorrowedSigil => ~"&",
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)?
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]
376 // Generally, after typeck you can get the inferred value
377 // using ty::resolved_T(...).
381 pub enum inferable<T> {
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) {
390 to_bytes::iter_bytes_2(&0u8, t, lsb0, f),
393 to_bytes::iter_bytes_2(&1u8, n, lsb0, f),
398 // "resolved" mode: the real modes.
402 pub enum rmode { by_ref, by_val, by_copy }
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)
411 pub type mode = inferable<rmode>;
413 pub type stmt = spanned<stmt_>;
419 stmt_decl(@decl, node_id),
421 // expr without trailing semi-colon (must have unit type):
422 stmt_expr(@expr, node_id),
424 // expr with trailing semi-colon (may have any type):
425 stmt_semi(@expr, node_id),
427 // bool: is there a trailing sem-colon?
431 // FIXME (pending discussion of #1697, #2178...): local should really be
432 // a refinement on pat.
444 pub type local = spanned<local_>;
446 pub type decl = spanned<decl_>;
451 pub enum decl_ { decl_local(~[@local]), decl_item(@item), }
458 guard: Option<@expr>,
471 pub type field = spanned<field_>;
476 pub enum blk_check_mode { default_blk, unsafe_blk, }
483 // Extra node ID is only used for index, assign_op, unary, binary, method
493 pub enum log_level { error, debug, log_other }
494 // 0 = error, 1 = debug, 2 = log_other
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),
515 expr_binary(binop, @expr, @expr),
516 expr_unary(unop, @expr),
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]),
527 // FIXME(#4717) the @() is req'd on windows or else LLVM croaks
528 expr_fn(Sigil, fn_decl, blk, @()),
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
534 expr_loop_body(@expr),
535 // Like expr_loop_body but for 'do' blocks
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),
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),
557 // A struct literal expression.
558 expr_struct(@path, ~[field], Option<@expr>),
560 // A vector literal constructed from one repeated element.
561 expr_repeat(@expr /* element */, @expr /* count */, mutability),
563 // No-op: used solely so we can pretty-print faithfully
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.
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.
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
585 #[doc="For macro invocations; parsing is delegated to the macro"]
586 pub enum token_tree {
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:
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),
599 // a syntactic variable that will be filled in by macro expansion.
600 tt_nonterminal(span, ident)
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
615 // A matcher that matches a single token, denoted by the token itself. So
616 // long as there's no $ involved.
622 // A matcher that matches a sequence of sub-matchers, denoted various
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
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:
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
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:
650 // $( $lhs:matchers => $rhs:tt );+
652 // If you understand that, you have closed to loop and understand the whole
653 // macro system. Congratulations.
655 pub type matcher = spanned<matcher_>;
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)
670 pub type mac = spanned<mac_>;
676 mac_invoc_tt(@path,~[token_tree]), // new macro-invocation
679 pub type lit = spanned<lit_>;
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),
695 // NB: If you change this, you'll probably want to change the corresponding
696 // type structure in middle/ty.rs as well.
708 pub struct ty_field_ {
713 pub type ty_field = spanned<ty_field_>;
718 pub struct ty_method {
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 pub enum trait_method {
743 pub enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, }
745 pub impl ToStr for int_ty {
746 pure fn to_str(&self) -> ~str {
747 ::ast_util::int_ty_to_str(*self)
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)
760 pub enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, }
762 pub impl ToStr for uint_ty {
763 pure fn to_str(&self) -> ~str {
764 ::ast_util::uint_ty_to_str(*self)
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)
777 pub enum float_ty { ty_f, ty_f32, ty_f64, }
779 pub impl ToStr for float_ty {
780 pure fn to_str(&self) -> ~str {
781 ::ast_util::float_ty_to_str(*self)
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)
791 // NB Eq method appears below.
801 // Not represented directly in the AST, referred to by name through a ty_path.
839 pub impl ToStr for Onceness {
840 pure fn to_str(&self) -> ~str {
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);
857 pub struct TyClosure {
859 region: Option<@region>,
868 pub struct TyBareFn {
879 ty_bot, /* bottom type */
883 ty_fixed_length_vec(mt, uint),
885 ty_rptr(@region, mt),
887 ty_closure(@TyClosure),
888 ty_bare_fn(@TyBareFn),
890 ty_path(@path, node_id),
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
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);
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"
934 pub impl ToStr for purity {
935 pure fn to_str(&self) -> ~str {
937 impure_fn => ~"impure",
938 unsafe_fn => ~"unsafe",
940 extern_fn => ~"extern"
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)
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
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)
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`
978 pub type self_ty = spanned<self_ty_>;
1001 view_items: ~[@view_item],
1008 pub enum foreign_abi {
1009 foreign_abi_rust_intrinsic,
1011 foreign_abi_stdcall,
1014 // Foreign mods can be named or anonymous
1018 pub enum foreign_mod_sort { named, anonymous }
1023 pub struct foreign_mod {
1024 sort: foreign_mod_sort,
1026 view_items: ~[@view_item],
1027 items: ~[@foreign_item],
1033 pub struct variant_arg {
1041 pub enum variant_kind {
1042 tuple_variant_kind(~[variant_arg]),
1043 struct_variant_kind(@struct_def),
1044 enum_variant_kind(enum_def)
1050 pub struct enum_def_ {
1051 variants: ~[variant],
1052 common: Option<@struct_def>,
1058 pub enum enum_def = enum_def_;
1063 pub struct variant_ {
1065 attrs: ~[attribute],
1068 disr_expr: Option<@expr>,
1072 pub type variant = spanned<variant_>;
1077 pub struct path_list_ident_ {
1082 pub type path_list_ident = spanned<path_list_ident_>;
1087 pub enum namespace { module_ns, type_value_ns }
1089 pub type view_path = spanned<view_path_>;
1094 pub enum view_path_ {
1096 // quux = foo::bar::baz
1100 // foo::bar::baz (with 'baz =' implicitly on the left)
1101 view_path_simple(ident, @path, namespace, node_id),
1104 view_path_glob(@path, node_id),
1106 // foo::bar::{a,b,c}
1107 view_path_list(@path, ~[path_list_ident], node_id)
1113 pub struct view_item {
1115 attrs: ~[attribute],
1123 pub enum view_item_ {
1124 view_item_extern_mod(ident, ~[@meta_item], node_id),
1125 view_item_use(~[@view_path]),
1128 // Meta-data associated with an item
1129 pub type attribute = spanned<attribute_>;
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.
1137 pub enum attr_style { attr_outer, attr_inner, }
1139 // doc-comments are promoted to attributes that have is_sugared_doc = true
1143 pub struct attribute_ {
1146 is_sugared_doc: bool,
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).
1159 pub struct trait_ref {
1167 pub enum visibility { public, private, inherited }
1172 pub struct struct_field_ {
1173 kind: struct_field_kind,
1178 pub type struct_field = spanned<struct_field_>;
1183 pub enum struct_field_kind {
1184 named_field(ident, struct_mutability, visibility),
1185 unnamed_field // element of a tuple-like struct
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
1198 ctor_id: Option<node_id>
1202 FIXME (#3300): Should allow items to be anonymous. Right now
1203 we just use dummy names for anon items.
1210 attrs: ~[attribute],
1221 item_const(@Ty, @expr),
1222 item_fn(fn_decl, purity, ~[ty_param], blk),
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
1239 pub enum struct_mutability { struct_mutable, struct_immutable }
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)
1247 pub type struct_dtor = spanned<struct_dtor_>;
1252 pub struct struct_dtor_ {
1254 attrs: ~[attribute],
1262 pub struct foreign_item {
1264 attrs: ~[attribute],
1265 node: foreign_item_,
1274 pub enum foreign_item_ {
1275 foreign_item_fn(fn_decl, purity, ~[ty_param]),
1276 foreign_item_const(@Ty)
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
1285 pub enum inlined_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 */)
1298 //are asts encodable?
1300 // it looks like this *will* be a compiler bug, after
1301 // I get deriving_eq for crates into incoming :)
1303 #[test] fn check_asts_encodable() {
1304 let bogus_span = span {lo:BytePos(10),
1310 module: _mod {view_items: ~[], items: ~[]},
1315 // doesn't matter which encoder we use....
1316 let _f = (_e as std::serialize::Encodable::<std::json::Encoder>);
1324 // indent-tabs-mode: nil
1325 // c-basic-offset: 4
1326 // buffer-file-coding-system: utf-8-unix