aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGraydon Hoare <[email protected]>2010-12-09 14:37:50 -0800
committerGraydon Hoare <[email protected]>2010-12-09 14:37:50 -0800
commit876282791e36c482676aeeaee722f5038126e175 (patch)
tree6455132ac599209d9263440a5fcb7b8fe30b252b /src
parentActually un-XFAIL drop-on-ret.rs. (diff)
downloadrust-876282791e36c482676aeeaee722f5038126e175.tar.xz
rust-876282791e36c482676aeeaee722f5038126e175.zip
First sketch of support for const items, not including most of trans.
Diffstat (limited to 'src')
-rw-r--r--src/comp/front/ast.rs1
-rw-r--r--src/comp/front/parser.rs22
-rw-r--r--src/comp/middle/fold.rs17
-rw-r--r--src/comp/middle/resolve.rs3
-rw-r--r--src/comp/middle/trans.rs12
-rw-r--r--src/comp/middle/typeck.rs36
6 files changed, 88 insertions, 3 deletions
diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs
index a37fd191..553a49f6 100644
--- a/src/comp/front/ast.rs
+++ b/src/comp/front/ast.rs
@@ -204,6 +204,7 @@ type variant = rec(str name, vec[variant_arg] args, def_id id, ann ann);
type item = spanned[item_];
tag item_ {
+ item_const(ident, @ty, @expr, def_id, ann);
item_fn(ident, _fn, vec[ty_param], def_id, ann);
item_mod(ident, _mod, def_id);
item_ty(ident, @ty, vec[ty_param], def_id, ann);
diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs
index 093d4b7c..8cf088e4 100644
--- a/src/comp/front/parser.rs
+++ b/src/comp/front/parser.rs
@@ -1229,6 +1229,9 @@ impure fn parse_mod_items(parser p, token.token term) -> ast._mod {
// Index the item.
alt (item.node) {
+ case (ast.item_const(?id, _, _, _, _)) {
+ index.insert(id, ast.mie_item(u));
+ }
case (ast.item_fn(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(u));
}
@@ -1254,6 +1257,19 @@ impure fn parse_mod_items(parser p, token.token term) -> ast._mod {
ret rec(items=items, index=index);
}
+impure fn parse_item_const(parser p) -> @ast.item {
+ auto lo = p.get_span();
+ expect(p, token.CONST);
+ auto ty = parse_ty(p);
+ auto id = parse_ident(p);
+ expect(p, token.EQ);
+ auto e = parse_expr(p);
+ auto hi = p.get_span();
+ expect(p, token.SEMI);
+ auto item = ast.item_const(id, ty, e, p.next_def_id(), ast.ann_none);
+ ret @spanned(lo, hi, item);
+}
+
impure fn parse_item_mod(parser p) -> @ast.item {
auto lo = p.get_span();
expect(p, token.MOD);
@@ -1369,6 +1385,12 @@ impure fn parse_item(parser p) -> @ast.item {
let ast.layer lyr = parse_layer(p);
alt (p.peek()) {
+ case (token.CONST) {
+ check (eff == ast.eff_pure);
+ check (lyr == ast.layer_value);
+ ret parse_item_const(p);
+ }
+
case (token.FN) {
check (lyr == ast.layer_value);
ret parse_item_fn(p, eff);
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) {