aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPatrick Walton <[email protected]>2010-11-24 16:52:49 -0800
committerPatrick Walton <[email protected]>2010-11-24 16:52:49 -0800
commitc1916adc7e16bd7ecd3ca8dbbe985ec75d0c825a (patch)
treed69d2e25a0a016c8c798d9a416b628d9f907a2d1 /src
parentrustc: Don't require a semicolon after an "alt" statement (diff)
downloadrust-c1916adc7e16bd7ecd3ca8dbbe985ec75d0c825a.tar.xz
rust-c1916adc7e16bd7ecd3ca8dbbe985ec75d0c825a.zip
rustc: Parse type-parametric functions
Diffstat (limited to 'src')
-rw-r--r--src/comp/front/ast.rs3
-rw-r--r--src/comp/front/parser.rs12
-rw-r--r--src/comp/middle/fold.rs15
-rw-r--r--src/comp/middle/resolve.rs4
-rw-r--r--src/comp/middle/trans.rs5
-rw-r--r--src/comp/middle/typeck.rs17
6 files changed, 38 insertions, 18 deletions
diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs
index cc0f2961..3bc2fe16 100644
--- a/src/comp/front/ast.rs
+++ b/src/comp/front/ast.rs
@@ -7,6 +7,7 @@ import util.common.spanned;
import util.common.ty_mach;
type ident = str;
+type ty_param = ident;
type name_ = rec(ident ident, vec[@ty] types);
type name = spanned[name_];
@@ -167,7 +168,7 @@ type variant = rec(str name, vec[@ty] args);
type item = spanned[item_];
tag item_ {
- item_fn(ident, _fn, def_id, ann);
+ item_fn(ident, _fn, vec[ty_param], def_id, ann);
item_mod(ident, _mod, def_id);
item_ty(ident, @ty, def_id, ann);
item_tag(ident, vec[variant], def_id);
diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs
index 598b04af..66529c69 100644
--- a/src/comp/front/parser.rs
+++ b/src/comp/front/parser.rs
@@ -1020,7 +1020,7 @@ impure fn parse_block(parser p) -> ast.block {
}
case (ast.decl_item(?it)) {
alt (it.node) {
- case (ast.item_fn(?i, _, _, _)) {
+ case (ast.item_fn(?i, _, _, _, _)) {
index.insert(i, u-1u);
}
case (ast.item_mod(?i, _, _)) {
@@ -1043,6 +1043,14 @@ impure fn parse_item_fn(parser p) -> tup(ast.ident, @ast.item) {
auto lo = p.get_span();
expect(p, token.FN);
auto id = parse_ident(p);
+
+ let vec[ast.ty_param] ty_params = vec();
+ if (p.peek() == token.LBRACKET) {
+ auto pg = parse_ident; // FIXME: pass as lval directly
+ ty_params = parse_seq[ast.ty_param](token.LBRACKET, token.RBRACKET,
+ some(token.COMMA), pg, p).node;
+ }
+
auto pf = parse_arg;
let util.common.spanned[vec[ast.arg]] inputs =
// FIXME: passing parse_arg as an lval doesn't work at the
@@ -1067,7 +1075,7 @@ impure fn parse_item_fn(parser p) -> tup(ast.ident, @ast.item) {
output = output,
body = body);
- auto item = ast.item_fn(id, f, p.next_def_id(), ast.ann_none);
+ auto item = ast.item_fn(id, f, ty_params, p.next_def_id(), ast.ann_none);
ret tup(id, @spanned(lo, body.span, item));
}
diff --git a/src/comp/middle/fold.rs b/src/comp/middle/fold.rs
index eea114ac..2afbc8b9 100644
--- a/src/comp/middle/fold.rs
+++ b/src/comp/middle/fold.rs
@@ -158,7 +158,9 @@ type ast_fold[ENV] =
// Item folds.
(fn(&ENV e, &span sp, ident ident,
- &ast._fn f, def_id id, ann a) -> @item) fold_item_fn,
+ &ast._fn f,
+ vec[ast.ty_param] ty_params,
+ def_id id, ann a) -> @item) fold_item_fn,
(fn(&ENV e, &span sp, ident ident,
&ast._mod m, def_id id) -> @item) fold_item_mod,
@@ -537,9 +539,9 @@ fn fold_item[ENV](&ENV env, ast_fold[ENV] fld, @item i) -> @item {
alt (i.node) {
- case (ast.item_fn(?ident, ?ff, ?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_, id, ann);
+ ret fld.fold_item_fn(env_, i.span, ident, ff_, tps, id, ann);
}
case (ast.item_mod(?ident, ?mm, ?id)) {
@@ -798,8 +800,9 @@ fn identity_fold_stmt_expr[ENV](&ENV e, &span sp, @expr x) -> @stmt {
// Item identities.
fn identity_fold_item_fn[ENV](&ENV e, &span sp, ident i,
- &ast._fn f, def_id id, ann a) -> @item {
- ret @respan(sp, ast.item_fn(i, f, id, a));
+ &ast._fn f, vec[ast.ty_param] ty_params,
+ def_id id, ann a) -> @item {
+ ret @respan(sp, ast.item_fn(i, f, ty_params, id, a));
}
fn identity_fold_item_mod[ENV](&ENV e, &span sp, ident i,
@@ -933,7 +936,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_fn = bind identity_fold_item_fn[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](_,_,_,_,_,_),
fold_item_tag = bind identity_fold_item_tag[ENV](_,_,_,_,_),
diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs
index 3ccc6c8b..f3957b3b 100644
--- a/src/comp/middle/resolve.rs
+++ b/src/comp/middle/resolve.rs
@@ -29,7 +29,7 @@ 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_fn(_, _, ?id, _)) {
+ case (ast.item_fn(_, _, _, ?id, _)) {
ret some[def](ast.def_fn(id));
}
case (ast.item_mod(_, _, ?id)) {
@@ -76,7 +76,7 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def] {
case (scope_item(?it)) {
alt (it.node) {
- case (ast.item_fn(_, ?f, _, _)) {
+ case (ast.item_fn(_, ?f, _, _, _)) {
for (ast.arg a in f.inputs) {
if (_str.eq(a.ident, i)) {
ret some[def](ast.def_arg(a.id));
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs
index f7ee82f6..1972a434 100644
--- a/src/comp/middle/trans.rs
+++ b/src/comp/middle/trans.rs
@@ -1295,7 +1295,7 @@ impure fn trans_fn(@crate_ctxt cx, &ast._fn f, ast.def_id fid) {
impure fn trans_item(@crate_ctxt cx, &ast.item item) {
alt (item.node) {
- case (ast.item_fn(?name, ?f, ?fid, _)) {
+ case (ast.item_fn(?name, ?f, _, ?fid, _)) {
auto sub_cx = @rec(path=cx.path + "." + name with *cx);
trans_fn(sub_cx, f, fid);
}
@@ -1315,7 +1315,8 @@ impure fn trans_mod(@crate_ctxt cx, &ast._mod m) {
fn collect_item(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt {
alt (i.node) {
- case (ast.item_fn(?name, ?f, ?fid, ?ann)) {
+ case (ast.item_fn(?name, ?f, _, ?fid, ?ann)) {
+ // TODO: type-params
cx.items.insert(fid, i);
auto llty = node_type(cx, ann);
let str s = cx.names.next("_rust_fn") + "." + name;
diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs
index aefb92bc..c7bd36b7 100644
--- a/src/comp/middle/typeck.rs
+++ b/src/comp/middle/typeck.rs
@@ -310,7 +310,9 @@ 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_fn(?ident, ?fn_info, ?def_id, _)) {
+ case (ast.item_fn(?ident, ?fn_info, _, ?def_id, _)) {
+ // TODO: handle ty-params
+
auto f = bind trans_fn_arg_to_ty(id_to_ty_item, item_to_ty,
_);
auto input_tys = _vec.map[ast.arg,arg](f, fn_info.inputs);
@@ -362,9 +364,12 @@ 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_fn(?ident, ?fn_info, ?def_id, _)) {
+ case (ast.item_fn(?ident, ?fn_info, ?tps, ?def_id, _)) {
+ // TODO: type-params
+
auto t = trans_ty_item_to_ty(id_to_ty_item, item_to_ty, it);
- result = ast.item_fn(ident, fn_info, def_id, ast.ann_type(t));
+ result = ast.item_fn(ident, fn_info, tps, def_id,
+ ast.ann_type(t));
}
case (ast.item_ty(?ident, ?referent_ty, ?def_id, _)) {
auto t = trans_ty_item_to_ty(id_to_ty_item, item_to_ty, it);
@@ -1274,7 +1279,8 @@ fn check_block(&fn_ctxt fcx, &ast.block block) -> ast.block {
}
fn check_fn(&@crate_ctxt ccx, &span sp, ast.ident ident, &ast._fn f,
- ast.def_id id, ast.ann ann) -> @ast.item {
+ vec[ast.ty_param] ty_params, ast.def_id id,
+ ast.ann ann) -> @ast.item {
auto local_ty_table = @common.new_def_hash[@ty]();
// Store the type of each argument in the table.
@@ -1296,7 +1302,8 @@ fn check_fn(&@crate_ctxt ccx, &span sp, ast.ident ident, &ast._fn f,
auto block_t = check_block(fcx, f.body);
auto block_wb = writeback(fcx, block_t);
auto fn_t = rec(inputs=f.inputs, output=f.output, body=block_wb);
- ret @fold.respan[ast.item_](sp, ast.item_fn(ident, fn_t, id, fn_ann));
+ auto item = ast.item_fn(ident, fn_t, ty_params, id, fn_ann);
+ ret @fold.respan[ast.item_](sp, item);
}
fn check_crate(session.session sess, @ast.crate crate) -> @ast.crate {