]> git.lizzy.rs Git - rust.git/blob - src/comp/syntax/ast.rs
Remove proto_sugar and 'lambda' as keyword, commit to fn@.
[rust.git] / src / comp / syntax / ast.rs
1 // The Rust abstract syntax tree.
2
3 import option;
4 import codemap::{span, filename};
5
6 type spanned<T> = {node: T, span: span};
7
8 type ident = str;
9
10 // Functions may or may not have names.
11 type fn_ident = option::t<ident>;
12
13 // FIXME: with typestate constraint, could say
14 // idents and types are the same length, and are
15 // non-empty
16 type path_ = {global: bool, idents: [ident], types: [@ty]};
17
18 type path = spanned<path_>;
19
20 type crate_num = int;
21 type node_id = int;
22 type def_id = {crate: crate_num, node: node_id};
23
24 const local_crate: crate_num = 0;
25 const crate_node_id: node_id = 0;
26
27 tag ty_param_bound {
28     bound_copy;
29     bound_send;
30     bound_iface(@ty);
31 }
32
33 type ty_param = {ident: ident, id: node_id, bounds: @[ty_param_bound]};
34
35 tag def {
36     def_fn(def_id, purity);
37     def_obj_field(def_id, mutability);
38     def_self(def_id);
39     def_mod(def_id);
40     def_native_mod(def_id);
41     def_const(def_id);
42     def_arg(def_id, mode);
43     def_local(def_id, let_style);
44     def_variant(def_id /* tag */, def_id /* variant */);
45     def_ty(def_id);
46     def_ty_param(def_id, uint);
47     def_binding(def_id);
48     def_use(def_id);
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
52 }
53
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];
57
58 type crate = spanned<crate_>;
59
60 type crate_ =
61     {directives: [@crate_directive],
62      module: _mod,
63      attrs: [attribute],
64      config: crate_cfg};
65
66 tag crate_directive_ {
67     cdir_src_mod(ident, [attribute]);
68     cdir_dir_mod(ident, [@crate_directive], [attribute]);
69
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);
76
77     cdir_syntax(@path);
78 }
79
80 type crate_directive = spanned<crate_directive_>;
81
82 type meta_item = spanned<meta_item_>;
83
84 tag meta_item_ {
85     meta_word(ident);
86     meta_list(ident, [@meta_item]);
87     meta_name_value(ident, lit);
88 }
89
90 type blk = spanned<blk_>;
91
92 type blk_ = {view_items: [@view_item], stmts: [@stmt], expr: option::t<@expr>,
93              id: node_id, rules: blk_check_mode};
94
95 type pat = {id: node_id, node: pat_, span: span};
96
97 type field_pat = {ident: ident, pat: @pat};
98
99 tag pat_ {
100     pat_wild;
101     pat_bind(ident, option::t<@pat>);
102     pat_tag(@path, [@pat]);
103     pat_rec([field_pat], bool);
104     pat_tup([@pat]);
105     pat_box(@pat);
106     pat_uniq(@pat);
107     pat_lit(@expr);
108     pat_range(@expr, @expr);
109 }
110
111 tag mutability { mut; imm; maybe_mut; }
112
113 tag proto {
114     proto_bare;
115     proto_send;
116     proto_shared;
117     proto_block;
118 }
119
120 tag binop {
121     add;
122     sub;
123     mul;
124     div;
125     rem;
126     and;
127     or;
128     bitxor;
129     bitand;
130     bitor;
131     lsl;
132     lsr;
133     asr;
134     eq;
135     lt;
136     le;
137     ne;
138     ge;
139     gt;
140 }
141
142 tag unop {
143     box(mutability);
144     uniq(mutability);
145     deref; not; neg;
146 }
147
148 tag mode { by_ref; by_val; by_mut_ref; by_move; by_copy; mode_infer; }
149
150 type stmt = spanned<stmt_>;
151
152 tag stmt_ {
153     stmt_decl(@decl, node_id);
154
155     // expr without trailing semi-colon (must have unit type):
156     stmt_expr(@expr, node_id);
157
158     // expr with trailing semi-colon (may have any type):
159     stmt_semi(@expr, node_id);
160 }
161
162 tag init_op { init_assign; init_move; }
163
164 type initializer = {op: init_op, expr: @expr};
165
166 type local_ =  // FIXME: should really be a refinement on pat
167     {ty: @ty, pat: @pat, init: option::t<initializer>, id: node_id};
168
169 type local = spanned<local_>;
170
171 type decl = spanned<decl_>;
172
173 tag let_style { let_copy; let_ref; }
174
175 tag decl_ { decl_local([(let_style, @local)]); decl_item(@item); }
176
177 type arm = {pats: [@pat], guard: option::t<@expr>, body: blk};
178
179 type field_ = {mut: mutability, ident: ident, expr: @expr};
180
181 type field = spanned<field_>;
182
183 tag blk_check_mode { default_blk; unchecked_blk; unsafe_blk; }
184
185 tag expr_check_mode { claimed_expr; checked_expr; }
186
187 type expr = {id: node_id, node: expr_, span: span};
188
189 tag expr_ {
190     expr_vec([@expr], mutability);
191     expr_rec([field], option::t<@expr>);
192     expr_call(@expr, [@expr], bool);
193     expr_tup([@expr]);
194     expr_bind(@expr, [option::t<@expr>]);
195     expr_binary(binop, @expr, @expr);
196     expr_unary(unop, @expr);
197     expr_lit(@lit);
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);
207     expr_block(blk);
208
209     /*
210      * FIXME: many of these @exprs should be constrained with
211      * is_lval once we have constrained types working.
212      */
213     expr_copy(@expr);
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);
220     expr_path(@path);
221     expr_fail(option::t<@expr>);
222     expr_break;
223     expr_cont;
224     expr_ret(option::t<@expr>);
225     expr_be(@expr);
226     expr_log(int, @expr, @expr);
227
228     /* just an assert, no significance to typestate */
229     expr_assert(@expr);
230
231     /* preds that typestate is aware of */
232     expr_check(expr_check_mode, @expr);
233
234     /* FIXME Would be nice if expr_check desugared
235        to expr_if_check. */
236     expr_if_check(@expr, blk, option::t<@expr>);
237     expr_anon_obj(anon_obj);
238     expr_mac(mac);
239 }
240
241 type capture_item = {
242     id: int,
243     name: ident, // Currently, can only capture a local var.
244     span: span
245 };
246 type capture_clause = {
247     copies: [@capture_item],
248     moves: [@capture_item]
249 };
250
251 /*
252 // Says whether this is a block the user marked as
253 // "unchecked"
254 tag blk_sort {
255     blk_unchecked; // declared as "exception to effect-checking rules"
256     blk_checked; // all typing rules apply
257 }
258 */
259
260 type mac = spanned<mac_>;
261
262 tag mac_ {
263     mac_invoc(@path, @expr, option::t<str>);
264     mac_embed_type(@ty);
265     mac_embed_block(blk);
266     mac_ellipsis;
267 }
268
269 type lit = spanned<lit_>;
270
271 tag lit_ {
272     lit_str(str);
273     lit_int(i64, int_ty);
274     lit_uint(u64, uint_ty);
275     lit_float(str, float_ty);
276     lit_nil;
277     lit_bool(bool);
278 }
279
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};
283
284 type ty_field_ = {ident: ident, mt: mt};
285
286 type ty_field = spanned<ty_field_>;
287
288 type ty_method = {ident: ident, decl: fn_decl, tps: [ty_param], span: span};
289
290 tag int_ty { ty_i; ty_char; ty_i8; ty_i16; ty_i32; ty_i64; }
291
292 tag uint_ty { ty_u; ty_u8; ty_u16; ty_u32; ty_u64; }
293
294 tag float_ty { ty_f; ty_f32; ty_f64; }
295
296 type ty = spanned<ty_>;
297
298 tag ty_ {
299     ty_nil;
300     ty_bot; /* return type of ! functions and type of
301              ret/fail/break/cont. there is no syntax
302              for this type. */
303
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. */
307     ty_bool;
308     ty_int(int_ty);
309     ty_uint(uint_ty);
310     ty_float(float_ty);
311     ty_str;
312     ty_box(mt);
313     ty_uniq(mt);
314     ty_vec(mt);
315     ty_ptr(mt);
316     ty_task;
317     ty_port(@ty);
318     ty_chan(@ty);
319     ty_rec([ty_field]);
320     ty_fn(proto, fn_decl);
321     ty_obj([ty_method]);
322     ty_tup([@ty]);
323     ty_path(@path, node_id);
324     ty_type;
325     ty_constr(@ty, [@ty_constr]);
326     ty_mac(mac);
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
329     // nested in one.
330     ty_infer;
331 }
332
333
334 /*
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.
341 */
342 tag constr_arg_general_<T> { carg_base; carg_ident(T); carg_lit(@lit); }
343
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>;
348
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)
353
354 type constr_general_<ARG, ID> =
355     {path: @path, args: [@spanned<constr_arg_general_<ARG>>], id: ID};
356
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_>;
364
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};
369
370 type fn_decl =
371     {inputs: [arg],
372      output: @ty,
373      purity: purity,
374      cf: ret_style,
375      constraints: [@constr]};
376
377 tag purity {
378     pure_fn; // declared with "pure fn"
379     unsafe_fn; // declared with "unsafe fn"
380     impure_fn; // declared with "fn"
381 }
382
383 tag ret_style {
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
387 }
388
389 type method = {ident: ident, tps: [ty_param], decl: fn_decl, body: blk,
390                id: node_id, span: span};
391
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};
395
396 type _obj = {fields: [obj_field], methods: [@method]};
397
398 type anon_obj =
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]>,
402      methods: [@method],
403      inner_obj: option::t<@expr>};
404
405 type _mod = {view_items: [@view_item], items: [@item]};
406
407 tag native_abi {
408     native_abi_rust_intrinsic;
409     native_abi_cdecl;
410     native_abi_stdcall;
411 }
412
413 type native_mod =
414     {view_items: [@view_item],
415      items: [@native_item]};
416
417 type variant_arg = {ty: @ty, id: node_id};
418
419 type variant_ = {name: ident, args: [variant_arg], id: node_id};
420
421 type variant = spanned<variant_>;
422
423 type view_item = spanned<view_item_>;
424
425 // FIXME: May want to just use path here, which would allow things like
426 // 'import ::foo'
427 type simple_path = [ident];
428
429 type import_ident_ = {name: ident, id: node_id};
430
431 type import_ident = spanned<import_ident_>;
432
433 tag view_item_ {
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);
439 }
440
441 type obj_def_ids = {ty: node_id, ctor: node_id};
442
443
444 // Meta-data associated with an item
445 type attribute = spanned<attribute_>;
446
447
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; }
452
453 type attribute_ = {style: attr_style, value: meta_item};
454
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};
457
458 tag item_ {
459     item_const(@ty, @expr);
460     item_fn(fn_decl, [ty_param], blk);
461     item_mod(_mod);
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]);
471 }
472
473 type native_item =
474     {ident: ident,
475      attrs: [attribute],
476      node: native_item_,
477      id: node_id,
478      span: span};
479
480 tag native_item_ {
481     native_item_ty;
482     native_item_fn(fn_decl, [ty_param]);
483 }
484
485 //
486 // Local Variables:
487 // mode: rust
488 // fill-column: 78;
489 // indent-tabs-mode: nil
490 // c-basic-offset: 4
491 // buffer-file-coding-system: utf-8-unix
492 // End:
493 //