diff options
Diffstat (limited to 'src/comp/middle')
| -rw-r--r-- | src/comp/middle/fold.rs | 17 | ||||
| -rw-r--r-- | src/comp/middle/resolve.rs | 3 | ||||
| -rw-r--r-- | src/comp/middle/trans.rs | 12 | ||||
| -rw-r--r-- | src/comp/middle/typeck.rs | 36 |
4 files changed, 65 insertions, 3 deletions
diff --git a/src/comp/middle/fold.rs b/src/comp/middle/fold.rs index 54492305..c81d2785 100644 --- a/src/comp/middle/fold.rs +++ b/src/comp/middle/fold.rs @@ -170,6 +170,10 @@ type ast_fold[ENV] = // Item folds. (fn(&ENV e, &span sp, ident ident, + @ty t, @expr e, + def_id id, ann a) -> @item) fold_item_const, + + (fn(&ENV e, &span sp, ident ident, &ast._fn f, vec[ast.ty_param] ty_params, def_id id, ann a) -> @item) fold_item_fn, @@ -595,6 +599,12 @@ fn fold_item[ENV](&ENV env, ast_fold[ENV] fld, @item i) -> @item { alt (i.node) { + case (ast.item_const(?ident, ?t, ?e, ?id, ?ann)) { + let @ast.ty t_ = fold_ty[ENV](env_, fld, t); + let @ast.expr e_ = fold_expr(env_, fld, e); + ret fld.fold_item_const(env_, i.span, ident, t_, e_, id, ann); + } + case (ast.item_fn(?ident, ?ff, ?tps, ?id, ?ann)) { let ast._fn ff_ = fold_fn[ENV](env_, fld, ff); ret fld.fold_item_fn(env_, i.span, ident, ff_, tps, id, ann); @@ -878,6 +888,12 @@ fn identity_fold_stmt_expr[ENV](&ENV e, &span sp, @expr x) -> @stmt { // Item identities. +fn identity_fold_item_const[ENV](&ENV e, &span sp, ident i, + @ty t, @expr ex, + def_id id, ann a) -> @item { + ret @respan(sp, ast.item_const(i, t, ex, id, a)); +} + fn identity_fold_item_fn[ENV](&ENV e, &span sp, ident i, &ast._fn f, vec[ast.ty_param] ty_params, def_id id, ann a) -> @item { @@ -1023,6 +1039,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] { = bind identity_fold_stmt_check_expr[ENV](_,_,_), fold_stmt_expr = bind identity_fold_stmt_expr[ENV](_,_,_), + fold_item_const= bind identity_fold_item_const[ENV](_,_,_,_,_,_,_), fold_item_fn = bind identity_fold_item_fn[ENV](_,_,_,_,_,_,_), fold_item_mod = bind identity_fold_item_mod[ENV](_,_,_,_,_), fold_item_ty = bind identity_fold_item_ty[ENV](_,_,_,_,_,_,_), diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index dca7479c..491d9e4a 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -29,6 +29,9 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def] { fn found_def_item(@ast.item i) -> option.t[def] { alt (i.node) { + case (ast.item_const(_, _, _, ?id, _)) { + ret some[def](ast.def_const(id)); + } case (ast.item_fn(_, _, _, ?id, _)) { ret some[def](ast.def_fn(id)); } diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 0a74b7a0..8249d31d 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -2001,6 +2001,10 @@ fn collect_item(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt { cx.item_ids.insert(fid, llfn); } + case (ast.item_const(?name, _, _, ?cid, _)) { + cx.items.insert(cid, i); + } + case (ast.item_mod(?name, ?m, ?mid)) { cx.items.insert(mid, i); } @@ -2128,6 +2132,14 @@ fn trans_constant(&@crate_ctxt cx, @ast.item it) -> @crate_ctxt { i += 1u; } } + + case (ast.item_const(?name, _, ?expr, ?cid, ?ann)) { + // FIXME: The whole expr-translation system needs cloning to deal + // with consts. + auto v = C_int(1); + cx.item_ids.insert(cid, v); + } + case (_) { // empty } diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index fc09bd92..160dce2c 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -372,6 +372,13 @@ fn collect_item_types(@ast.crate crate) -> tup(@ast.crate, @ty_table) { @ty_table item_to_ty, @ast.item it) -> @ty { alt (it.node) { + + case (ast.item_const(?ident, ?t, _, ?def_id, _)) { + auto f = bind trans_ty_item_id_to_ty(id_to_ty_item, + item_to_ty, _); + item_to_ty.insert(def_id, ast_ty_to_ty(f, t)); + } + case (ast.item_fn(?ident, ?fn_info, _, ?def_id, _)) { // TODO: handle ty-params @@ -391,7 +398,6 @@ fn collect_item_types(@ast.crate crate) -> tup(@ast.crate, @ty_table) { case (ast.item_ty(?ident, ?referent_ty, _, ?def_id, _)) { if (item_to_ty.contains_key(def_id)) { // Avoid repeating work. - check (item_to_ty.contains_key(def_id)); ret item_to_ty.get(def_id); } @@ -431,11 +437,11 @@ fn collect_item_types(@ast.crate crate) -> tup(@ast.crate, @ty_table) { auto arg_ty = ast_ty_to_ty(f, va.ty); args += vec(rec(mode=ast.alias, ty=arg_ty)); } - result_ty = plain_ty(ty_fn(args, plain_ty(ty_tag(tag_id)))); + result_ty = plain_ty(ty_fn(args, plain_ty(ty_tag(tag_id)))); } item_to_ty.insert(variant.id, result_ty); - + auto variant_t = rec(ann=ast.ann_type(result_ty) with variant); result += vec(variant_t); } @@ -461,6 +467,11 @@ fn collect_item_types(@ast.crate crate) -> tup(@ast.crate, @ty_table) { for (@ast.item it in module.items) { let ast.item_ result; alt (it.node) { + case (ast.item_const(?ident, ?t, ?e, ?def_id, _)) { + auto ty = trans_ty_item_to_ty(id_to_ty_item, item_to_ty, it); + result = ast.item_const(ident, t, e, def_id, + ast.ann_type(ty)); + } case (ast.item_fn(?ident, ?fn_info, ?tps, ?def_id, _)) { // TODO: type-params @@ -1659,11 +1670,30 @@ fn check_block(&fn_ctxt fcx, &ast.block block) -> ast.block { index=block.node.index)); } +fn check_const(&@crate_ctxt ccx, &span sp, ast.ident ident, @ast.ty t, + @ast.expr e, ast.def_id id, ast.ann ann) -> @ast.item { + // FIXME: this is kinda a kludge; we manufacture a fake "function context" + // for checking the initializer expression. + auto rty = ann_to_type(ann); + let fn_ctxt fcx = rec(ret_ty = rty, + locals = @common.new_def_hash[@ty](), + ccx = ccx); + auto e_ = check_expr(fcx, e); + // FIXME: necessary? Correct sequence? + demand_expr(fcx, rty, e_); + auto item = ast.item_const(ident, t, e_, id, ann); + ret @fold.respan[ast.item_](sp, item); +} + fn check_fn(&@crate_ctxt ccx, &span sp, ast.ident ident, &ast._fn f, vec[ast.ty_param] ty_params, ast.def_id id, ast.ann ann) -> @ast.item { auto local_ty_table = @common.new_def_hash[@ty](); + // FIXME: duplicate work: the item annotation already has the arg types + // and return type translated to typeck.ty values. We don't need do to it + // again here, we can extract them. + // Store the type of each argument in the table. let vec[arg] inputs = vec(); for (ast.arg arg in f.inputs) { |