First sketch of support for const items, not including most of trans.

This commit is contained in:
Graydon Hoare 2010-12-09 14:37:50 -08:00
parent 9da8d11a52
commit 876282791e
6 changed files with 88 additions and 3 deletions

View file

@ -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);

View file

@ -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);

View file

@ -169,6 +169,10 @@ type ast_fold[ENV] =
@expr e) -> @stmt) fold_stmt_expr,
// 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,
@ -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](_,_,_,_,_,_,_),

View file

@ -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));
}

View file

@ -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
}

View file

@ -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) {