Parse function declarations.
This commit is contained in:
parent
302cafa81d
commit
57bb9d809b
7 changed files with 101 additions and 43 deletions
|
|
@ -10,6 +10,7 @@ import util.common.ty_mach;
|
|||
import util.common.append;
|
||||
|
||||
import front.ast;
|
||||
import front.ast.fn_decl;
|
||||
import front.ast.ident;
|
||||
import front.ast.path;
|
||||
import front.ast.mutability;
|
||||
|
|
@ -194,6 +195,11 @@ type ast_fold[ENV] =
|
|||
vec[ast.ty_param] ty_params,
|
||||
def_id id, ann a) -> @item) fold_item_fn,
|
||||
|
||||
(fn(&ENV e, &span sp, ident ident,
|
||||
&ast.fn_decl decl,
|
||||
vec[ast.ty_param] ty_params,
|
||||
def_id id) -> @native_item) fold_native_item_fn,
|
||||
|
||||
(fn(&ENV e, &span sp, ident ident,
|
||||
&ast._mod m, def_id id) -> @item) fold_item_mod,
|
||||
|
||||
|
|
@ -229,10 +235,13 @@ type ast_fold[ENV] =
|
|||
(fn(&ENV e, &span sp,
|
||||
&ast.block_) -> block) fold_block,
|
||||
|
||||
(fn(&ENV e, ast.effect effect,
|
||||
(fn(&ENV e, &fn_decl decl,
|
||||
bool is_iter,
|
||||
&block body) -> ast._fn) fold_fn,
|
||||
|
||||
(fn(&ENV e, ast.effect effect,
|
||||
vec[arg] inputs,
|
||||
@ty output, &block body) -> ast._fn) fold_fn,
|
||||
@ty output) -> ast.fn_decl) fold_fn_decl,
|
||||
|
||||
(fn(&ENV e, &ast._mod m) -> ast._mod) fold_mod,
|
||||
|
||||
|
|
@ -688,17 +697,22 @@ fn fold_arg[ENV](&ENV env, ast_fold[ENV] fld, &arg a) -> arg {
|
|||
ret rec(ty=ty with a);
|
||||
}
|
||||
|
||||
|
||||
fn fold_fn[ENV](&ENV env, ast_fold[ENV] fld, &ast._fn f) -> ast._fn {
|
||||
|
||||
fn fold_fn_decl[ENV](&ENV env, ast_fold[ENV] fld,
|
||||
&ast.fn_decl decl) -> ast.fn_decl {
|
||||
let vec[ast.arg] inputs = vec();
|
||||
for (ast.arg a in f.inputs) {
|
||||
for (ast.arg a in decl.inputs) {
|
||||
inputs += fold_arg(env, fld, a);
|
||||
}
|
||||
auto output = fold_ty[ENV](env, fld, f.output);
|
||||
auto output = fold_ty[ENV](env, fld, decl.output);
|
||||
ret fld.fold_fn_decl(env, decl.effect, inputs, output);
|
||||
}
|
||||
|
||||
fn fold_fn[ENV](&ENV env, ast_fold[ENV] fld, &ast._fn f) -> ast._fn {
|
||||
auto decl = fold_fn_decl(env, fld, f.decl);
|
||||
|
||||
auto body = fold_block[ENV](env, fld, f.body);
|
||||
|
||||
ret fld.fold_fn(env, f.effect, f.is_iter, inputs, output, body);
|
||||
ret fld.fold_fn(env, decl, f.is_iter, body);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -857,6 +871,10 @@ fn fold_native_item[ENV](&ENV env, ast_fold[ENV] fld,
|
|||
case (ast.native_item_ty(?ident, ?id)) {
|
||||
ret fld.fold_native_item_ty(env_, i.span, ident, id);
|
||||
}
|
||||
case (ast.native_item_fn(?ident, ?fn_decl, ?ty_params, ?id)) {
|
||||
ret fld.fold_native_item_fn(env_, i.span, ident, fn_decl,
|
||||
ty_params, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1144,6 +1162,13 @@ fn identity_fold_item_fn[ENV](&ENV e, &span sp, ident i,
|
|||
ret @respan(sp, ast.item_fn(i, f, ty_params, id, a));
|
||||
}
|
||||
|
||||
fn identity_fold_native_item_fn[ENV](&ENV e, &span sp, ident i,
|
||||
&ast.fn_decl decl,
|
||||
vec[ast.ty_param] ty_params,
|
||||
def_id id) -> @native_item {
|
||||
ret @respan(sp, ast.native_item_fn(i, decl, ty_params, id));
|
||||
}
|
||||
|
||||
fn identity_fold_item_mod[ENV](&ENV e, &span sp, ident i,
|
||||
&ast._mod m, def_id id) -> @item {
|
||||
ret @respan(sp, ast.item_mod(i, m, id));
|
||||
|
|
@ -1199,14 +1224,18 @@ fn identity_fold_block[ENV](&ENV e, &span sp, &ast.block_ blk) -> block {
|
|||
ret respan(sp, blk);
|
||||
}
|
||||
|
||||
fn identity_fold_fn_decl[ENV](&ENV e,
|
||||
ast.effect effect,
|
||||
vec[arg] inputs,
|
||||
@ty output) -> ast.fn_decl {
|
||||
ret rec(effect=effect, inputs=inputs, output=output);
|
||||
}
|
||||
|
||||
fn identity_fold_fn[ENV](&ENV e,
|
||||
ast.effect effect,
|
||||
&fn_decl decl,
|
||||
bool is_iter,
|
||||
vec[arg] inputs,
|
||||
@ast.ty output,
|
||||
&block body) -> ast._fn {
|
||||
ret rec(effect=effect, is_iter=is_iter, inputs=inputs,
|
||||
output=output, body=body);
|
||||
ret rec(decl=decl, is_iter=is_iter, body=body);
|
||||
}
|
||||
|
||||
fn identity_fold_mod[ENV](&ENV e, &ast._mod m) -> ast._mod {
|
||||
|
|
@ -1343,6 +1372,8 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
|
|||
|
||||
fold_item_const= bind identity_fold_item_const[ENV](_,_,_,_,_,_,_),
|
||||
fold_item_fn = bind identity_fold_item_fn[ENV](_,_,_,_,_,_,_),
|
||||
fold_native_item_fn =
|
||||
bind identity_fold_native_item_fn[ENV](_,_,_,_,_,_),
|
||||
fold_item_mod = bind identity_fold_item_mod[ENV](_,_,_,_,_),
|
||||
fold_item_native_mod =
|
||||
bind identity_fold_item_native_mod[ENV](_,_,_,_,_),
|
||||
|
|
@ -1358,7 +1389,8 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
|
|||
bind identity_fold_view_item_import[ENV](_,_,_,_,_,_),
|
||||
|
||||
fold_block = bind identity_fold_block[ENV](_,_,_),
|
||||
fold_fn = bind identity_fold_fn[ENV](_,_,_,_,_,_),
|
||||
fold_fn = bind identity_fold_fn[ENV](_,_,_,_),
|
||||
fold_fn_decl = bind identity_fold_fn_decl[ENV](_,_,_,_),
|
||||
fold_mod = bind identity_fold_mod[ENV](_,_),
|
||||
fold_native_mod = bind identity_fold_native_mod[ENV](_,_),
|
||||
fold_crate = bind identity_fold_crate[ENV](_,_,_),
|
||||
|
|
|
|||
|
|
@ -283,7 +283,7 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] {
|
|||
case (scope_item(?it)) {
|
||||
alt (it.node) {
|
||||
case (ast.item_fn(_, ?f, ?ty_params, _, _)) {
|
||||
for (ast.arg a in f.inputs) {
|
||||
for (ast.arg a in f.decl.inputs) {
|
||||
if (_str.eq(a.ident, i)) {
|
||||
auto t = ast.def_arg(a.id);
|
||||
ret some(def_wrap_other(t));
|
||||
|
|
|
|||
|
|
@ -3553,10 +3553,10 @@ fn trans_fn(@crate_ctxt cx, &ast._fn f, ast.def_id fid,
|
|||
|
||||
auto fcx = new_fn_ctxt(cx, cx.path, llfndecl);
|
||||
create_llargs_for_fn_args(fcx, ty_self, ret_ty_of_fn(ann),
|
||||
f.inputs, ty_params);
|
||||
f.decl.inputs, ty_params);
|
||||
auto bcx = new_top_block_ctxt(fcx);
|
||||
|
||||
copy_args_to_allocas(bcx, ty_self, f.inputs,
|
||||
copy_args_to_allocas(bcx, ty_self, f.decl.inputs,
|
||||
arg_tys_of_fn(ann));
|
||||
|
||||
alt (fcx.llself) {
|
||||
|
|
|
|||
|
|
@ -285,8 +285,8 @@ fn collect_item_types(session.session sess, @ast.crate crate)
|
|||
auto get = bind getter(id_to_ty_item, item_to_ty, _);
|
||||
auto convert = bind ast_ty_to_ty(get, _);
|
||||
auto f = bind ty_of_arg(id_to_ty_item, item_to_ty, _);
|
||||
auto inputs = _vec.map[ast.arg,arg](f, m.node.meth.inputs);
|
||||
auto output = convert(m.node.meth.output);
|
||||
auto inputs = _vec.map[ast.arg,arg](f, m.node.meth.decl.inputs);
|
||||
auto output = convert(m.node.meth.decl.output);
|
||||
ret rec(ident=m.node.ident, inputs=inputs, output=output);
|
||||
}
|
||||
|
||||
|
|
@ -339,8 +339,9 @@ fn collect_item_types(session.session sess, @ast.crate crate)
|
|||
// TODO: handle ty-params
|
||||
|
||||
auto f = bind ty_of_arg(id_to_ty_item, item_to_ty, _);
|
||||
auto input_tys = _vec.map[ast.arg,arg](f, fn_info.inputs);
|
||||
auto output_ty = convert(fn_info.output);
|
||||
auto input_tys = _vec.map[ast.arg,arg](f,
|
||||
fn_info.decl.inputs);
|
||||
auto output_ty = convert(fn_info.decl.output);
|
||||
|
||||
auto t_fn = plain_ty(ty.ty_fn(input_tys, output_ty));
|
||||
item_to_ty.insert(def_id, t_fn);
|
||||
|
|
@ -1773,9 +1774,8 @@ fn check_const(&@crate_ctxt ccx, &span sp, ast.ident ident, @ast.ty t,
|
|||
ret @fold.respan[ast.item_](sp, item);
|
||||
}
|
||||
|
||||
fn check_fn(&@crate_ctxt ccx, ast.effect effect,
|
||||
bool is_iter, vec[ast.arg] inputs,
|
||||
@ast.ty output, &ast.block body) -> ast._fn {
|
||||
fn check_fn(&@crate_ctxt ccx, &ast.fn_decl decl,
|
||||
bool is_iter, &ast.block body) -> ast._fn {
|
||||
auto local_ty_table = @common.new_def_hash[@ty.t]();
|
||||
|
||||
// FIXME: duplicate work: the item annotation already has the arg types
|
||||
|
|
@ -1789,12 +1789,12 @@ fn check_fn(&@crate_ctxt ccx, ast.effect effect,
|
|||
}
|
||||
|
||||
// Store the type of each argument in the table.
|
||||
for (ast.arg arg in inputs) {
|
||||
for (ast.arg arg in decl.inputs) {
|
||||
auto input_ty = ast_ty_to_ty_crate(ccx, arg.ty);
|
||||
local_ty_table.insert(arg.id, input_ty);
|
||||
}
|
||||
|
||||
let @fn_ctxt fcx = @rec(ret_ty = ast_ty_to_ty_crate(ccx, output),
|
||||
let @fn_ctxt fcx = @rec(ret_ty = ast_ty_to_ty_crate(ccx, decl.output),
|
||||
locals = local_ty_table,
|
||||
ccx = ccx);
|
||||
|
||||
|
|
@ -1802,8 +1802,8 @@ fn check_fn(&@crate_ctxt ccx, ast.effect effect,
|
|||
auto block_t = check_block(fcx, body);
|
||||
auto block_wb = writeback(fcx, block_t);
|
||||
|
||||
auto fn_t = rec(effect=effect, is_iter=is_iter,
|
||||
inputs=inputs, output=output, body=block_wb);
|
||||
auto fn_t = rec(decl=decl, is_iter=is_iter,
|
||||
body=block_wb);
|
||||
ret fn_t;
|
||||
}
|
||||
|
||||
|
|
@ -1816,12 +1816,12 @@ fn check_item_fn(&@crate_ctxt ccx, &span sp, ast.ident ident, &ast._fn f,
|
|||
// again here, we can extract them.
|
||||
|
||||
let vec[arg] inputs = vec();
|
||||
for (ast.arg arg in f.inputs) {
|
||||
for (ast.arg arg in f.decl.inputs) {
|
||||
auto input_ty = ast_ty_to_ty_crate(ccx, arg.ty);
|
||||
inputs += vec(rec(mode=arg.mode, ty=input_ty));
|
||||
}
|
||||
|
||||
auto output_ty = ast_ty_to_ty_crate(ccx, f.output);
|
||||
auto output_ty = ast_ty_to_ty_crate(ccx, f.decl.output);
|
||||
auto fn_sty = ty.ty_fn(inputs, output_ty);
|
||||
auto fn_ann = ast.ann_type(plain_ty(fn_sty));
|
||||
|
||||
|
|
@ -1854,7 +1854,7 @@ fn check_crate(session.session sess, @ast.crate crate) -> @ast.crate {
|
|||
auto fld = fold.new_identity_fold[@crate_ctxt]();
|
||||
|
||||
fld = @rec(update_env_for_item = bind update_obj_fields(_, _),
|
||||
fold_fn = bind check_fn(_,_,_,_,_,_),
|
||||
fold_fn = bind check_fn(_,_,_,_),
|
||||
fold_item_fn = bind check_item_fn(_,_,_,_,_,_,_)
|
||||
with *fld);
|
||||
ret fold.fold_crate[@crate_ctxt](ccx, fld, result._0);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue