1 // The Rust abstract syntax tree.
4 import codemap::{span, filename};
6 type spanned<T> = {node: T, span: span};
10 // Functions may or may not have names.
11 type fn_ident = option::t<ident>;
13 // FIXME: with typestate constraint, could say
14 // idents and types are the same length, and are
16 type path_ = {global: bool, idents: [ident], types: [@ty]};
18 type path = spanned<path_>;
22 type def_id = {crate: crate_num, node: node_id};
24 const local_crate: crate_num = 0;
25 const crate_node_id: node_id = 0;
33 type ty_param = {ident: ident, id: node_id, bounds: @[ty_param_bound]};
36 def_fn(def_id, purity);
37 def_obj_field(def_id, mutability);
40 def_native_mod(def_id);
42 def_arg(def_id, mode);
43 def_local(def_id, let_style);
44 def_variant(def_id /* tag */, def_id /* variant */);
46 def_ty_param(def_id, uint);
49 def_native_ty(def_id);
50 def_native_fn(def_id, purity);
51 def_upvar(def_id, @def, node_id); // node_id == expr_fn or expr_fn_block
54 // The set of meta_items that define the compilation environment of the crate,
55 // used to drive conditional compilation
56 type crate_cfg = [@meta_item];
58 type crate = spanned<crate_>;
61 {directives: [@crate_directive],
66 tag crate_directive_ {
67 cdir_src_mod(ident, [attribute]);
68 cdir_dir_mod(ident, [@crate_directive], [attribute]);
70 // NB: cdir_view_item is *not* processed by the rest of the compiler; the
71 // attached view_items are sunk into the crate's module during parsing,
72 // and processed (resolved, imported, etc.) there. This tag-variant exists
73 // only to preserve the view items in order in case we decide to
74 // pretty-print crates in the future.
75 cdir_view_item(@view_item);
80 type crate_directive = spanned<crate_directive_>;
82 type meta_item = spanned<meta_item_>;
86 meta_list(ident, [@meta_item]);
87 meta_name_value(ident, lit);
90 type blk = spanned<blk_>;
92 type blk_ = {view_items: [@view_item], stmts: [@stmt], expr: option::t<@expr>,
93 id: node_id, rules: blk_check_mode};
95 type pat = {id: node_id, node: pat_, span: span};
97 type field_pat = {ident: ident, pat: @pat};
101 pat_bind(ident, option::t<@pat>);
102 pat_tag(@path, [@pat]);
103 pat_rec([field_pat], bool);
108 pat_range(@expr, @expr);
111 tag mutability { mut; imm; maybe_mut; }
148 tag mode { by_ref; by_val; by_mut_ref; by_move; by_copy; mode_infer; }
150 type stmt = spanned<stmt_>;
153 stmt_decl(@decl, node_id);
155 // expr without trailing semi-colon (must have unit type):
156 stmt_expr(@expr, node_id);
158 // expr with trailing semi-colon (may have any type):
159 stmt_semi(@expr, node_id);
162 tag init_op { init_assign; init_move; }
164 type initializer = {op: init_op, expr: @expr};
166 type local_ = // FIXME: should really be a refinement on pat
167 {ty: @ty, pat: @pat, init: option::t<initializer>, id: node_id};
169 type local = spanned<local_>;
171 type decl = spanned<decl_>;
173 tag let_style { let_copy; let_ref; }
175 tag decl_ { decl_local([(let_style, @local)]); decl_item(@item); }
177 type arm = {pats: [@pat], guard: option::t<@expr>, body: blk};
179 type field_ = {mut: mutability, ident: ident, expr: @expr};
181 type field = spanned<field_>;
183 tag blk_check_mode { default_blk; unchecked_blk; unsafe_blk; }
185 tag expr_check_mode { claimed_expr; checked_expr; }
187 type expr = {id: node_id, node: expr_, span: span};
190 expr_vec([@expr], mutability);
191 expr_rec([field], option::t<@expr>);
192 expr_call(@expr, [@expr], bool);
194 expr_bind(@expr, [option::t<@expr>]);
195 expr_binary(binop, @expr, @expr);
196 expr_unary(unop, @expr);
198 expr_cast(@expr, @ty);
199 expr_if(@expr, blk, option::t<@expr>);
200 expr_ternary(@expr, @expr, @expr);
201 expr_while(@expr, blk);
202 expr_for(@local, @expr, blk);
203 expr_do_while(blk, @expr);
204 expr_alt(@expr, [arm]);
205 expr_fn(proto, fn_decl, blk, @capture_clause);
206 expr_fn_block(fn_decl, blk);
210 * FIXME: many of these @exprs should be constrained with
211 * is_lval once we have constrained types working.
214 expr_move(@expr, @expr);
215 expr_assign(@expr, @expr);
216 expr_swap(@expr, @expr);
217 expr_assign_op(binop, @expr, @expr);
218 expr_field(@expr, ident, [@ty]);
219 expr_index(@expr, @expr);
221 expr_fail(option::t<@expr>);
224 expr_ret(option::t<@expr>);
226 expr_log(int, @expr, @expr);
228 /* just an assert, no significance to typestate */
231 /* preds that typestate is aware of */
232 expr_check(expr_check_mode, @expr);
234 /* FIXME Would be nice if expr_check desugared
236 expr_if_check(@expr, blk, option::t<@expr>);
237 expr_anon_obj(anon_obj);
241 type capture_item = {
243 name: ident, // Currently, can only capture a local var.
246 type capture_clause = {
247 copies: [@capture_item],
248 moves: [@capture_item]
252 // Says whether this is a block the user marked as
255 blk_unchecked; // declared as "exception to effect-checking rules"
256 blk_checked; // all typing rules apply
260 type mac = spanned<mac_>;
263 mac_invoc(@path, @expr, option::t<str>);
265 mac_embed_block(blk);
269 type lit = spanned<lit_>;
273 lit_int(i64, int_ty);
274 lit_uint(u64, uint_ty);
275 lit_float(str, float_ty);
280 // NB: If you change this, you'll probably want to change the corresponding
281 // type structure in middle/ty.rs as well.
282 type mt = {ty: @ty, mut: mutability};
284 type ty_field_ = {ident: ident, mt: mt};
286 type ty_field = spanned<ty_field_>;
288 type ty_method = {ident: ident, decl: fn_decl, tps: [ty_param], span: span};
290 tag int_ty { ty_i; ty_char; ty_i8; ty_i16; ty_i32; ty_i64; }
292 tag uint_ty { ty_u; ty_u8; ty_u16; ty_u32; ty_u64; }
294 tag float_ty { ty_f; ty_f32; ty_f64; }
296 type ty = spanned<ty_>;
300 ty_bot; /* return type of ! functions and type of
301 ret/fail/break/cont. there is no syntax
304 /* bot represents the value of functions that don't return a value
305 locally to their context. in contrast, things like log that do
306 return, but don't return a meaningful value, have result type nil. */
320 ty_fn(proto, fn_decl);
323 ty_path(@path, node_id);
325 ty_constr(@ty, [@ty_constr]);
327 // ty_infer means the type should be inferred instead of it having been
328 // specified. This should only appear at the "top level" of a type and not
335 A constraint arg that's a function argument is referred to by its position
336 rather than name. This is so we could have higher-order functions that have
337 constraints (potentially -- right now there's no way to write that), and also
338 so that the typestate pass doesn't have to map a function name onto its decl.
339 So, the constr_arg type is parameterized: it's instantiated with uint for
340 declarations, and ident for uses.
342 tag constr_arg_general_<T> { carg_base; carg_ident(T); carg_lit(@lit); }
344 type fn_constr_arg = constr_arg_general_<uint>;
345 type sp_constr_arg<T> = spanned<constr_arg_general_<T>>;
346 type ty_constr_arg = sp_constr_arg<@path>;
347 type constr_arg = spanned<fn_constr_arg>;
349 // Constrained types' args are parameterized by paths, since
350 // we refer to paths directly and not by indices.
351 // The implicit root of such path, in the constraint-list for a
352 // constrained type, is * (referring to the base record)
354 type constr_general_<ARG, ID> =
355 {path: @path, args: [@spanned<constr_arg_general_<ARG>>], id: ID};
357 // In the front end, constraints have a node ID attached.
358 // Typeck turns this to a def_id, using the output of resolve.
359 type constr_general<ARG> = spanned<constr_general_<ARG, node_id>>;
360 type constr_ = constr_general_<uint, node_id>;
361 type constr = spanned<constr_general_<uint, node_id>>;
362 type ty_constr_ = constr_general_<@path, node_id>;
363 type ty_constr = spanned<ty_constr_>;
365 /* The parser generates ast::constrs; resolve generates
366 a mapping from each function to a list of ty::constr_defs,
367 corresponding to these. */
368 type arg = {mode: mode, ty: @ty, ident: ident, id: node_id};
375 constraints: [@constr]};
378 pure_fn; // declared with "pure fn"
379 unsafe_fn; // declared with "unsafe fn"
380 impure_fn; // declared with "fn"
384 noreturn; // functions with return type _|_ that always
385 // raise an error or exit (i.e. never return to the caller)
386 return_val; // everything else
389 type method = {ident: ident, tps: [ty_param], decl: fn_decl, body: blk,
390 id: node_id, span: span};
392 type obj_field = {mut: mutability, ty: @ty, ident: ident, id: node_id};
393 type anon_obj_field =
394 {mut: mutability, ty: @ty, expr: @expr, ident: ident, id: node_id};
396 type _obj = {fields: [obj_field], methods: [@method]};
399 // New fields and methods, if they exist.
400 // inner_obj: the original object being extended, if it exists.
401 {fields: option::t<[anon_obj_field]>,
403 inner_obj: option::t<@expr>};
405 type _mod = {view_items: [@view_item], items: [@item]};
408 native_abi_rust_intrinsic;
414 {view_items: [@view_item],
415 items: [@native_item]};
417 type variant_arg = {ty: @ty, id: node_id};
419 type variant_ = {name: ident, args: [variant_arg], id: node_id};
421 type variant = spanned<variant_>;
423 type view_item = spanned<view_item_>;
425 // FIXME: May want to just use path here, which would allow things like
427 type simple_path = [ident];
429 type import_ident_ = {name: ident, id: node_id};
431 type import_ident = spanned<import_ident_>;
434 view_item_use(ident, [@meta_item], node_id);
435 view_item_import(ident, @simple_path, node_id);
436 view_item_import_glob(@simple_path, node_id);
437 view_item_import_from(@simple_path, [import_ident], node_id);
438 view_item_export([ident], node_id);
441 type obj_def_ids = {ty: node_id, ctor: node_id};
444 // Meta-data associated with an item
445 type attribute = spanned<attribute_>;
448 // Distinguishes between attributes that decorate items and attributes that
449 // are contained as statements within items. These two cases need to be
450 // distinguished for pretty-printing.
451 tag attr_style { attr_outer; attr_inner; }
453 type attribute_ = {style: attr_style, value: meta_item};
455 type item = // For objs and resources, this is the type def_id
456 {ident: ident, attrs: [attribute], id: node_id, node: item_, span: span};
459 item_const(@ty, @expr);
460 item_fn(fn_decl, [ty_param], blk);
462 item_native_mod(native_mod);
463 item_ty(@ty, [ty_param]);
464 item_tag([variant], [ty_param]);
465 item_obj(_obj, [ty_param], /* constructor id */node_id);
466 item_res(fn_decl /* dtor */, [ty_param], blk,
467 node_id /* dtor id */, node_id /* ctor id */);
468 item_iface([ty_param], [ty_method]);
469 item_impl([ty_param], option::t<@ty> /* iface */,
470 @ty /* self */, [@method]);
482 native_item_fn(fn_decl, [ty_param]);
489 // indent-tabs-mode: nil
491 // buffer-file-coding-system: utf-8-unix