Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More machinery for the new expr_call_self AST node #303

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/comp/middle/fold.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import std.map.hashmap;
import std.option;
import std.option;
import std.option.some;
import std.option.none;

Expand Down Expand Up @@ -87,6 +87,10 @@ type ast_fold[ENV] =
@expr f, vec[@expr] args,
ann a) -> @expr) fold_expr_call,

(fn(&ENV e, &span sp,
@expr f, vec[@expr] args,
ann a) -> @expr) fold_expr_call_self,

(fn(&ENV e, &span sp,
@expr f, vec[option.t[@expr]] args,
ann a) -> @expr) fold_expr_bind,
Expand Down Expand Up @@ -562,6 +566,12 @@ 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(?f, ?args, ?t)) {
auto ff = fold_expr(env_, fld, f);
auto aargs = fold_exprs(env_, fld, args);
ret fld.fold_expr_call_self(env_, e.span, ff, aargs, t);
}

case (ast.expr_bind(?f, ?args_opt, ?t)) {
auto ff = fold_expr(env_, fld, f);
let vec[option.t[@ast.expr]] aargs_opt = vec();
Expand Down Expand Up @@ -1175,6 +1185,11 @@ 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, @expr f,
vec[@expr] args, ann a) -> @expr {
ret @respan(sp, ast.expr_call_self(f, args, a));
}

fn identity_fold_expr_bind[ENV](&ENV env, &span sp, @expr f,
vec[option.t[@expr]] args_opt, ann a)
-> @expr {
Expand Down Expand Up @@ -1580,6 +1595,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_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](_,_,_,_,_,_),
Expand Down
68 changes: 68 additions & 0 deletions src/comp/middle/trans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4444,6 +4444,70 @@ fn trans_call(@block_ctxt cx, @ast.expr f,
ret res(bcx, retval);
}

fn trans_call_self(@block_ctxt cx, @ast.expr f,
option.t[ValueRef] lliterbody,
vec[@ast.expr] args,
&ast.ann ann) -> result {
log "translating a self-call";

auto f_res = trans_lval(cx, f);
auto faddr = f_res.res.val;
auto llenv = C_null(T_opaque_closure_ptr(cx.fcx.ccx.tn));

alt (f_res.llobj) {
case (some[ValueRef](_)) {
// It's a vtbl entry.
faddr = f_res.res.bcx.build.Load(faddr);
}
case (none[ValueRef]) {
// It's a closure.
auto bcx = f_res.res.bcx;
auto pair = faddr;
faddr = bcx.build.GEP(pair, vec(C_int(0),
C_int(abi.fn_field_code)));
faddr = bcx.build.Load(faddr);

auto llclosure = bcx.build.GEP(pair,
vec(C_int(0),
C_int(abi.fn_field_box)));
llenv = bcx.build.Load(llclosure);
}
}
auto fn_ty = ty.expr_ty(f);
auto ret_ty = ty.ann_to_type(ann);
auto args_res = trans_args(f_res.res.bcx,
llenv, f_res.llobj,
f_res.generic,
lliterbody,
args, fn_ty);

auto bcx = args_res._0;
auto llargs = args_res._1;
auto llretslot = args_res._2;

/*
log "calling: " + val_str(cx.fcx.ccx.tn, faddr);

for (ValueRef arg in llargs) {
log "arg: " + val_str(cx.fcx.ccx.tn, arg);
}
*/

bcx.build.FastCall(faddr, llargs);
auto retval = C_nil();

if (!ty.type_is_nil(ret_ty)) {
retval = load_scalar_or_boxed(bcx, llretslot, ret_ty);
// Retval doesn't correspond to anything really tangible in the frame,
// but it's a ref all the same, so we put a note here to drop it when
// we're done in this scope.
find_scope_cx(cx).cleanups +=
vec(clean(bind drop_ty(_, retval, ret_ty)));
}

ret res(bcx, retval);
}

fn trans_tup(@block_ctxt cx, vec[ast.elt] elts,
&ast.ann ann) -> result {
auto bcx = cx;
Expand Down Expand Up @@ -4680,6 +4744,10 @@ fn trans_expr(@block_ctxt cx, @ast.expr e) -> result {
ret trans_call(cx, f, none[ValueRef], args, ann);
}

case (ast.expr_call_self(?f, ?args, ?ann)) {
ret trans_call_self(cx, f, none[ValueRef], args, ann);
}

case (ast.expr_cast(?e, _, ?ann)) {
ret trans_cast(cx, e, ann);
}
Expand Down
7 changes: 7 additions & 0 deletions src/comp/pretty/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@ impure fn print_expr(ps s, &@ast.expr expr) {
commasep_exprs(s, args);
pclose(s);
}
case (ast.expr_call_self(?func,?args,_)) {
wrd(s.s, "self.");
print_expr(s, func);
popen(s);
commasep_exprs(s, args);
pclose(s);
}
case (ast.expr_bind(?func,?args,_)) {
impure fn print_opt(ps s, &option.t[@ast.expr] expr) {
alt (expr) {
Expand Down