diff options
| author | Lindsey Kuper <[email protected]> | 2011-04-07 13:49:27 -0700 |
|---|---|---|
| committer | Graydon Hoare <[email protected]> | 2011-04-07 14:26:34 -0700 |
| commit | 1092bbfff0cf932ef14e5b92cd54133571ca4727 (patch) | |
| tree | a771667972fb8f75d393372032d5f05c03b3b68d /src/comp/middle | |
| parent | Add a very basic crate-dump utility (diff) | |
| download | rust-1092bbfff0cf932ef14e5b92cd54133571ca4727.tar.xz rust-1092bbfff0cf932ef14e5b92cd54133571ca4727.zip | |
Support for self-calls that take arguments.
Nicer parsing of self-calls (expr_self_method nodes inside expr_call
nodes, rather than a separate expr_call_self) makes typechecking
tractable. We can now write self-calls that take arguments and return
values (see: test/run-pass/obj-self-*.rs).
Diffstat (limited to 'src/comp/middle')
| -rw-r--r-- | src/comp/middle/fold.rs | 18 | ||||
| -rw-r--r-- | src/comp/middle/trans.rs | 10 | ||||
| -rw-r--r-- | src/comp/middle/ty.rs | 2 | ||||
| -rw-r--r-- | src/comp/middle/typeck.rs | 61 |
4 files changed, 58 insertions, 33 deletions
diff --git a/src/comp/middle/fold.rs b/src/comp/middle/fold.rs index fa85f791..d87c4d08 100644 --- a/src/comp/middle/fold.rs +++ b/src/comp/middle/fold.rs @@ -89,8 +89,7 @@ type ast_fold[ENV] = ann a) -> @expr) fold_expr_call, (fn(&ENV e, &span sp, - ident id, vec[@expr] args, - ann a) -> @expr) fold_expr_call_self, + ident id, ann a) -> @expr) fold_expr_self_method, (fn(&ENV e, &span sp, @expr f, vec[option.t[@expr]] args, @@ -569,9 +568,8 @@ fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr { ret fld.fold_expr_call(env_, e.span, ff, aargs, t); } - case (ast.expr_call_self(?ident, ?args, ?t)) { - auto aargs = fold_exprs(env_, fld, args); - ret fld.fold_expr_call_self(env_, e.span, ident, aargs, t); + case (ast.expr_self_method(?ident, ?t)) { + ret fld.fold_expr_self_method(env_, e.span, ident, t); } case (ast.expr_bind(?f, ?args_opt, ?t)) { @@ -1187,9 +1185,9 @@ fn identity_fold_expr_call[ENV](&ENV env, &span sp, @expr f, ret @respan(sp, ast.expr_call(f, args, a)); } -fn identity_fold_expr_call_self[ENV](&ENV env, &span sp, ident id, - vec[@expr] args, ann a) -> @expr { - ret @respan(sp, ast.expr_call_self(id, args, a)); +fn identity_fold_expr_self_method[ENV](&ENV env, &span sp, ident id, + ann a) -> @expr { + ret @respan(sp, ast.expr_self_method(id, a)); } fn identity_fold_expr_bind[ENV](&ENV env, &span sp, @expr f, @@ -1601,8 +1599,8 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] { fold_expr_tup = bind identity_fold_expr_tup[ENV](_,_,_,_), fold_expr_rec = bind identity_fold_expr_rec[ENV](_,_,_,_,_), fold_expr_call = bind identity_fold_expr_call[ENV](_,_,_,_,_), - fold_expr_call_self - = bind identity_fold_expr_call_self[ENV](_,_,_,_,_), + fold_expr_self_method + = bind identity_fold_expr_self_method[ENV](_,_,_,_), fold_expr_bind = bind identity_fold_expr_bind[ENV](_,_,_,_,_), fold_expr_spawn = bind identity_fold_expr_spawn[ENV](_,_,_,_,_,_,_), fold_expr_binary = bind identity_fold_expr_binary[ENV](_,_,_,_,_,_), diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index e1660b3f..5a8ce98b 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -3911,10 +3911,7 @@ fn trans_lval(@block_ctxt cx, @ast.expr e) -> lval_result { C_int(abi.box_rc_field_body))); ret lval_mem(sub.bcx, val); } - - // Kind of bizarre to pass an *entire* self-call here...but let's try - // it - case (ast.expr_call_self(?ident, _, ?ann)) { + case (ast.expr_self_method(?ident, ?ann)) { alt (cx.fcx.llself) { case (some[self_vt](?s_vt)) { auto r = s_vt.v; @@ -4765,11 +4762,6 @@ fn trans_expr(@block_ctxt cx, @ast.expr e) -> result { ret trans_call(cx, f, none[ValueRef], args, ann); } - case (ast.expr_call_self(?ident, ?args, ?ann)) { - // A weird hack to make self-calls work. - ret trans_call(cx, e, none[ValueRef], args, ann); - } - case (ast.expr_cast(?e, _, ?ann)) { ret trans_cast(cx, e, ann); } diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs index 09c89fdf..3a2fc917 100644 --- a/src/comp/middle/ty.rs +++ b/src/comp/middle/ty.rs @@ -764,7 +764,7 @@ fn expr_ty(@ast.expr expr) -> @t { case (ast.expr_rec(_, _, ?ann)) { ret ann_to_type(ann); } case (ast.expr_bind(_, _, ?ann)) { ret ann_to_type(ann); } case (ast.expr_call(_, _, ?ann)) { ret ann_to_type(ann); } - case (ast.expr_call_self(_, _, ?ann)) { ret ann_to_type(ann); } + case (ast.expr_self_method(_, ?ann)) { ret ann_to_type(ann); } case (ast.expr_spawn(_, _, _, _, ?ann)) { ret ann_to_type(ann); } case (ast.expr_binary(_, _, _, ?ann)) { ret ann_to_type(ann); } diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index d969df5a..b60c23e9 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -34,6 +34,8 @@ import std.option; import std.option.none; import std.option.some; +import pretty.pprust; + import util.typestate_ann.ts_ann; type ty_table = hashmap[ast.def_id, @ty.t]; @@ -49,6 +51,7 @@ type crate_ctxt = rec(session.session sess, ty.type_cache type_cache, @ty_item_table item_items, vec[ast.obj_field] obj_fields, + option.t[ast.def_id] this_obj, mutable int next_var_id); type fn_ctxt = rec(@ty.t ret_ty, @@ -1242,10 +1245,9 @@ fn demand_expr_full(&@fn_ctxt fcx, @ty.t expected, @ast.expr e, ann_to_type(ann), adk); e_1 = ast.expr_call(sube, es, triv_ann(t)); } - case (ast.expr_call_self(?sube, ?es, ?ann)) { - auto t = demand_full(fcx, e.span, expected, - ann_to_type(ann), adk); - e_1 = ast.expr_call_self(sube, es, triv_ann(t)); + case (ast.expr_self_method(?id, ?ann)) { + auto t = demand(fcx, e.span, expected, ann_to_type(ann)); + e_1 = ast.expr_self_method(id, triv_ann(t)); } case (ast.expr_binary(?bop, ?lhs, ?rhs, ?ann)) { auto t = demand(fcx, e.span, expected, ann_to_type(ann)); @@ -1572,6 +1574,8 @@ fn check_pat(&@fn_ctxt fcx, @ast.pat pat) -> @ast.pat { } fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr { + // log "typechecking expr " + pretty.pprust.expr_to_str(expr); + // A generic function to factor out common logic from call and bind // expressions. fn check_call_or_bind(&@fn_ctxt fcx, &@ast.expr f, @@ -2125,16 +2129,44 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr { ast.expr_call(f_1, args_1, ann)); } - case (ast.expr_call_self(?ident, ?args, _)) { - // FIXME: What's to check here? + case (ast.expr_self_method(?id, _)) { + auto t = plain_ty(ty.ty_nil); + let @ty.t this_obj_ty; + + // Grab the type of the current object + auto this_obj_id = fcx.ccx.this_obj; + alt (this_obj_id) { + case (some[ast.def_id](?def_id)) { + auto this_obj_tpt = fcx.ccx.type_cache.find(def_id); + alt (this_obj_tpt) { + case (some[ty_params_opt_and_ty](?tpt)) { + this_obj_ty = tpt._1; + } + case (_) { fail; } + } + } + case (_) { fail; } + } + - // FIXME: These two lines are ripped off from the expr_call case; - // what should they be really? - auto rt_1 = plain_ty(ty.ty_nil); - auto ann = triv_ann(rt_1); + // Grab this method's type out of the current object type + + // this_obj_ty is an @ty.t + alt (this_obj_ty.struct) { + case (ty.ty_obj(?methods)) { + for (ty.method method in methods) { + if (method.ident == id) { + t = ty.method_ty_to_fn_ty(method); + } + } + } + case (_) { fail; } + } + + auto ann = triv_ann(t); ret @fold.respan[ast.expr_](expr.span, - ast.expr_call_self(ident, args, ann)); + ast.expr_self_method(id, ann)); } case (ast.expr_spawn(?dom, ?name, ?f, ?args, _)) { @@ -2596,8 +2628,10 @@ fn check_item_fn(&@crate_ctxt ccx, &span sp, ast.ident ident, &ast._fn f, fn update_obj_fields(&@crate_ctxt ccx, @ast.item i) -> @crate_ctxt { alt (i.node) { - case (ast.item_obj(_, ?ob, _, _, _)) { - ret @rec(obj_fields = ob.fields with *ccx); + case (ast.item_obj(_, ?ob, _, ?obj_def_ids, _)) { + let ast.def_id di = obj_def_ids.ty; + ret @rec(obj_fields = ob.fields, + this_obj = some[ast.def_id](di) with *ccx); } case (_) { } @@ -2617,6 +2651,7 @@ fn check_crate(session.session sess, @ast.crate crate) -> typecheck_result { type_cache=result._1, item_items=result._2, obj_fields=fields, + this_obj=none[ast.def_id], mutable next_var_id=0); auto fld = fold.new_identity_fold[@crate_ctxt](); |