rust/src/comp/front/ast.rs

548 lines
14 KiB
Rust
Raw Normal View History

import std.map.hashmap;
2010-11-03 17:10:37 -07:00
import std.option;
import std._vec;
import util.common.span;
import util.common.spanned;
import util.common.ty_mach;
import util.common.filename;
type ident = str;
type path_ = rec(vec[ident] idents, vec[@ty] types);
type path = spanned[path_];
type crate_num = int;
type def_num = int;
type def_id = tup(crate_num, def_num);
type ty_param = rec(ident ident, def_id id);
// Annotations added during successive passes.
tag ann {
ann_none;
ann_type(@middle.ty.t, option.t[vec[@middle.ty.t]] /* ty param substs */);
}
tag def {
def_fn(def_id);
2010-12-14 15:32:13 -08:00
def_obj(def_id);
2010-12-30 15:27:19 -08:00
def_obj_field(def_id);
def_mod(def_id);
2011-03-10 21:33:53 -05:00
def_native_mod(def_id);
def_const(def_id);
def_arg(def_id);
def_local(def_id);
def_upvar(def_id);
2010-12-01 10:19:20 -08:00
def_variant(def_id /* tag */, def_id /* variant */);
def_ty(def_id);
def_ty_arg(def_id);
2010-12-10 18:08:32 -08:00
def_binding(def_id);
def_use(def_id);
def_native_ty(def_id);
def_native_fn(def_id);
}
fn def_id_of_def(def d) -> def_id {
alt (d) {
case (def_fn(?id)) { ret id; }
case (def_obj(?id)) { ret id; }
case (def_obj_field(?id)) { ret id; }
case (def_mod(?id)) { ret id; }
case (def_native_mod(?id)) { ret id; }
case (def_const(?id)) { ret id; }
case (def_arg(?id)) { ret id; }
case (def_local(?id)) { ret id; }
case (def_upvar(?id)) { ret id; }
case (def_variant(_, ?id)) { ret id; }
case (def_ty(?id)) { ret id; }
case (def_ty_arg(?id)) { ret id; }
case (def_binding(?id)) { ret id; }
case (def_use(?id)) { ret id; }
case (def_native_ty(?id)) { ret id; }
case (def_native_fn(?id)) { ret id; }
}
fail;
}
type crate = spanned[crate_];
type crate_ = rec(vec[@crate_directive] directives,
_mod module);
tag crate_directive_ {
cdir_expr(@expr);
// FIXME: cdir_let should be eliminated
// and redirected to the use of const stmt_decls inside
// crate directive blocks.
cdir_let(ident, @expr, vec[@crate_directive]);
cdir_src_mod(ident, option.t[filename]);
cdir_dir_mod(ident, option.t[filename], vec[@crate_directive]);
cdir_view_item(@view_item);
2011-03-04 11:28:40 -08:00
cdir_meta(vec[@meta_item]);
cdir_syntax(path);
cdir_auth(path, effect);
}
type crate_directive = spanned[crate_directive_];
2010-12-30 11:21:37 -05:00
type meta_item = spanned[meta_item_];
type meta_item_ = rec(ident name, str value);
type block = spanned[block_];
type block_index = hashmap[ident, block_index_entry];
tag block_index_entry {
bie_item(@item);
bie_local(@local);
bie_tag_variant(@item /* tag item */, uint /* variant index */);
}
type block_ = rec(vec[@stmt] stmts,
option.t[@expr] expr,
hashmap[ident,block_index_entry] index);
type variant_def = tup(def_id /* tag */, def_id /* variant */);
2010-11-24 14:42:01 -08:00
type pat = spanned[pat_];
tag pat_ {
2010-11-24 15:45:59 -08:00
pat_wild(ann);
2010-12-10 17:24:53 -08:00
pat_bind(ident, def_id, ann);
pat_lit(@lit, ann);
pat_tag(path, vec[@pat], option.t[variant_def], ann);
2010-11-24 14:42:01 -08:00
}
tag mutability {
mut;
imm;
maybe_mut;
}
tag opacity {
op_abstract;
op_transparent;
}
2010-12-03 18:03:28 -08:00
tag layer {
layer_value;
layer_state;
layer_gc;
}
tag effect {
eff_pure;
eff_impure;
eff_unsafe;
}
tag proto {
proto_iter;
proto_fn;
}
tag binop {
2010-09-28 10:30:34 -07:00
add;
sub;
mul;
div;
rem;
and;
or;
bitxor;
bitand;
bitor;
lsl;
lsr;
asr;
eq;
lt;
le;
ne;
ge;
gt;
}
fn binop_to_str(binop op) -> str {
alt (op) {
case (add) {ret "+";}
case (sub) {ret "-";}
case (mul) {ret "*";}
case (div) {ret "/";}
case (rem) {ret "%";}
case (and) {ret "&&";}
case (or) {ret "||";}
case (bitxor) {ret "^";}
case (bitand) {ret "&";}
case (bitor) {ret "|";}
case (lsl) {ret "<<";}
case (lsr) {ret ">>";}
case (asr) {ret ">>>";}
case (eq) {ret "==";}
case (lt) {ret "<";}
case (le) {ret "<=";}
case (ne) {ret "!=";}
case (ge) {ret ">=";}
case (gt) {ret ">";}
}
}
tag unop {
2010-09-28 10:30:34 -07:00
box;
deref;
bitnot;
not;
neg;
}
fn unop_to_str(unop op) -> str {
alt (op) {
case (box) {ret "@";}
case (deref) {ret "*";}
case (bitnot) {ret "~";}
case (not) {ret "!";}
case (neg) {ret "-";}
}
}
2010-12-03 18:03:28 -08:00
tag mode {
val;
alias;
}
type stmt = spanned[stmt_];
tag stmt_ {
stmt_decl(@decl);
stmt_expr(@expr);
// These only exist in crate-level blocks.
stmt_crate_directive(@crate_directive);
}
tag init_op {
init_assign;
init_recv;
}
type initializer = rec(init_op op,
@expr expr);
2010-11-03 17:10:37 -07:00
type local = rec(option.t[@ty] ty,
bool infer,
ident ident,
option.t[initializer] init,
def_id id,
ann ann);
type decl = spanned[decl_];
tag decl_ {
decl_local(@local);
decl_item(@item);
}
2010-12-10 18:08:32 -08:00
type arm = rec(@pat pat, block block, hashmap[ident,def_id] index);
2010-11-24 15:45:59 -08:00
type elt = rec(mutability mut, @expr expr);
type field = rec(mutability mut, ident ident, @expr expr);
tag spawn_dom {
dom_implicit;
dom_thread;
}
type expr = spanned[expr_];
tag expr_ {
expr_vec(vec[@expr], mutability, ann);
expr_tup(vec[elt], ann);
expr_rec(vec[field], option.t[@expr], ann);
expr_call(@expr, vec[@expr], ann);
expr_call_self(@expr, vec[@expr], ann);
2010-12-20 18:58:18 -08:00
expr_bind(@expr, vec[option.t[@expr]], ann);
expr_spawn(spawn_dom, option.t[str], @expr, vec[@expr], ann);
expr_binary(binop, @expr, @expr, ann);
expr_unary(unop, @expr, ann);
expr_lit(@lit, ann);
expr_cast(@expr, @ty, ann);
expr_if(@expr, block, option.t[@expr], ann);
expr_while(@expr, block, ann);
expr_for(@decl, @expr, block, ann);
expr_for_each(@decl, @expr, block, ann);
expr_do_while(block, @expr, ann);
2010-11-24 14:42:01 -08:00
expr_alt(@expr, vec[arm], ann);
expr_block(block, ann);
expr_assign(@expr /* TODO: @expr|is_lval */, @expr, ann);
expr_assign_op(binop, @expr /* TODO: @expr|is_lval */, @expr, ann);
expr_send(@expr /* TODO: @expr|is_lval */, @expr, ann);
expr_recv(@expr /* TODO: @expr|is_lval */, @expr, ann);
expr_field(@expr, ident, ann);
expr_index(@expr, @expr, ann);
expr_path(path, option.t[def], ann);
expr_ext(path, vec[@expr], option.t[@expr], @expr, ann);
expr_fail;
expr_break;
expr_cont;
expr_ret(option.t[@expr]);
expr_put(option.t[@expr]);
expr_be(@expr);
expr_log(@expr);
expr_check_expr(@expr);
expr_port(ann);
expr_chan(@expr, ann);
}
type lit = spanned[lit_];
tag lit_ {
lit_str(str);
lit_char(char);
lit_int(int);
lit_uint(uint);
lit_mach_int(ty_mach, int);
lit_float(str);
lit_mach_float(ty_mach, str);
lit_nil;
lit_bool(bool);
}
2010-11-03 16:43:12 -07:00
// NB: If you change this, you'll probably want to change the corresponding
// type structure in middle/ty.rs as well.
2010-12-03 18:03:28 -08:00
type mt = rec(@ty ty, mutability mut);
type ty_field = rec(ident ident, mt mt);
2010-12-03 18:03:28 -08:00
type ty_arg = rec(mode mode, @ty ty);
type ty_method = rec(effect effect, proto proto, ident ident,
vec[ty_arg] inputs, @ty output);
type ty = spanned[ty_];
tag ty_ {
ty_nil;
ty_bool;
ty_int;
ty_uint;
ty_float;
ty_machine(util.common.ty_mach);
ty_char;
ty_str;
ty_box(mt);
ty_vec(mt);
ty_port(@ty);
ty_chan(@ty);
ty_tup(vec[mt]);
ty_rec(vec[ty_field]);
ty_fn(effect, proto, vec[ty_arg], @ty);
2010-12-14 17:42:12 -08:00
ty_obj(vec[ty_method]);
2010-11-03 17:10:37 -07:00
ty_path(path, option.t[def]);
2011-02-01 14:56:21 -08:00
ty_type;
ty_constr(@ty, vec[@constr]);
}
tag constr_arg_ {
carg_base;
carg_ident(ident);
}
type constr_arg = spanned[constr_arg_];
type constr_ = rec(path path, vec[@constr_arg] args);
type constr = spanned[constr_];
type arg = rec(mode mode, @ty ty, ident ident, def_id id);
2011-02-04 11:10:04 -05:00
type fn_decl = rec(effect effect,
vec[arg] inputs,
@ty output);
type _fn = rec(fn_decl decl,
proto proto,
block body);
2010-12-14 15:32:13 -08:00
2010-12-16 18:34:04 -08:00
type method_ = rec(ident ident, _fn meth, def_id id, ann ann);
2010-12-14 15:32:13 -08:00
type method = spanned[method_];
2010-12-16 18:34:04 -08:00
type obj_field = rec(@ty ty, ident ident, def_id id, ann ann);
2010-12-14 15:32:13 -08:00
type _obj = rec(vec[obj_field] fields,
2011-03-01 17:32:16 -08:00
vec[@method] methods,
option.t[block] dtor);
2010-12-14 15:32:13 -08:00
2010-12-01 10:19:20 -08:00
tag mod_index_entry {
mie_view_item(@view_item);
mie_item(@item);
mie_tag_variant(@item /* tag item */, uint /* variant index */);
2010-12-01 10:19:20 -08:00
}
2011-03-07 11:48:43 -08:00
tag native_mod_index_entry {
nmie_view_item(@view_item);
nmie_item(@native_item);
}
type mod_index = hashmap[ident,mod_index_entry];
type _mod = rec(vec[@view_item] view_items,
vec[@item] items,
mod_index index);
2011-02-23 14:06:37 -05:00
tag native_abi {
native_abi_rust;
native_abi_cdecl;
native_abi_llvm;
2011-02-23 14:06:37 -05:00
}
type native_mod = rec(str native_name,
2011-02-23 14:06:37 -05:00
native_abi abi,
2011-03-07 11:48:43 -08:00
vec[@view_item] view_items,
vec[@native_item] items,
native_mod_index index);
2011-03-07 11:48:43 -08:00
type native_mod_index = hashmap[ident,native_mod_index_entry];
type variant_arg = rec(@ty ty, def_id id);
type variant = rec(str name, vec[variant_arg] args, def_id id, ann ann);
type view_item = spanned[view_item_];
tag view_item_ {
view_item_use(ident, vec[@meta_item], def_id, option.t[int]);
view_item_import(ident, vec[ident], def_id, option.t[def]);
view_item_export(ident);
}
type obj_def_ids = rec(def_id ty, def_id ctor);
type item = spanned[item_];
tag item_ {
item_const(ident, @ty, @expr, def_id, ann);
2010-11-24 16:52:49 -08:00
item_fn(ident, _fn, vec[ty_param], def_id, ann);
item_mod(ident, _mod, def_id);
item_native_mod(ident, native_mod, def_id);
2010-11-24 17:36:22 -08:00
item_ty(ident, @ty, vec[ty_param], def_id, ann);
2010-11-24 17:15:54 -08:00
item_tag(ident, vec[variant], vec[ty_param], def_id);
item_obj(ident, _obj, vec[ty_param], obj_def_ids, ann);
}
type native_item = spanned[native_item_];
tag native_item_ {
native_item_ty(ident, def_id);
native_item_fn(ident, option.t[str],
fn_decl, vec[ty_param], def_id, ann);
}
fn index_view_item(mod_index index, @view_item it) {
alt (it.node) {
case(ast.view_item_use(?id, _, _, _)) {
index.insert(id, ast.mie_view_item(it));
}
case(ast.view_item_import(?def_ident,_,_,_)) {
index.insert(def_ident, ast.mie_view_item(it));
}
case(ast.view_item_export(_)) {
// NB: don't index these, they might collide with
// the import or use that they're exporting. Have
// to do linear search for exports.
}
}
}
fn index_item(mod_index index, @item it) {
alt (it.node) {
case (ast.item_const(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(it));
}
case (ast.item_fn(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(it));
}
case (ast.item_mod(?id, _, _)) {
index.insert(id, ast.mie_item(it));
}
case (ast.item_native_mod(?id, _, _)) {
index.insert(id, ast.mie_item(it));
}
case (ast.item_ty(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(it));
}
case (ast.item_tag(?id, ?variants, _, _)) {
index.insert(id, ast.mie_item(it));
let uint variant_idx = 0u;
for (ast.variant v in variants) {
index.insert(v.name,
ast.mie_tag_variant(it, variant_idx));
variant_idx += 1u;
}
}
case (ast.item_obj(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(it));
}
}
}
2010-08-12 10:27:50 -07:00
fn index_native_item(native_mod_index index, @native_item it) {
alt (it.node) {
case (ast.native_item_ty(?id, _)) {
2011-03-07 11:48:43 -08:00
index.insert(id, ast.nmie_item(it));
}
case (ast.native_item_fn(?id, _, _, _, _, _)) {
2011-03-07 11:48:43 -08:00
index.insert(id, ast.nmie_item(it));
}
}
}
fn index_native_view_item(native_mod_index index, @view_item it) {
alt (it.node) {
case(ast.view_item_import(?def_ident,_,_,_)) {
index.insert(def_ident, ast.nmie_view_item(it));
}
case(ast.view_item_export(_)) {
// NB: don't index these, they might collide with
// the import or use that they're exporting. Have
// to do linear search for exports.
2011-02-04 11:10:04 -05:00
}
}
}
2011-03-11 17:29:53 -05:00
fn index_stmt(block_index index, @stmt s) {
alt (s.node) {
case (ast.stmt_decl(?d)) {
alt (d.node) {
case (ast.decl_local(?loc)) {
index.insert(loc.ident, ast.bie_local(loc));
}
case (ast.decl_item(?it)) {
alt (it.node) {
case (ast.item_fn(?i, _, _, _, _)) {
index.insert(i, ast.bie_item(it));
}
case (ast.item_mod(?i, _, _)) {
index.insert(i, ast.bie_item(it));
}
case (ast.item_ty(?i, _, _, _, _)) {
index.insert(i, ast.bie_item(it));
}
case (ast.item_tag(?i, ?variants, _, _)) {
index.insert(i, ast.bie_item(it));
let uint vid = 0u;
for (ast.variant v in variants) {
auto t = ast.bie_tag_variant(it, vid);
index.insert(v.name, t);
vid += 1u;
}
}
case (ast.item_obj(?i, _, _, _, _)) {
index.insert(i, ast.bie_item(it));
}
}
}
}
}
case (_) { /* fall through */ }
}
}
fn is_call_expr(@expr e) -> bool {
alt (e.node) {
case (expr_call(_, _, _)) {
ret true;
}
case (_) {
ret false;
}
}
}
2010-08-12 10:27:50 -07:00
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
2010-08-12 10:27:50 -07:00
// End:
//