From 4ebbafbb19455d71cd185504cdef992e1a91db7e Mon Sep 17 00:00:00 2001 From: lemon Date: Fri, 5 Aug 2022 20:01:39 +0200 Subject: call functin pointers directly --- bootstrap/parse.c | 21 +++++++++++++-------- bootstrap/test.cff | 4 ++++ 2 files changed, 17 insertions(+), 8 deletions(-) (limited to 'bootstrap') diff --git a/bootstrap/parse.c b/bootstrap/parse.c index 9c6f5a8..d55339d 100644 --- a/bootstrap/parse.c +++ b/bootstrap/parse.c @@ -940,22 +940,27 @@ pexpostfix(struct parser *P) { for (;;) if (lexmatch(P, &tok, '(')) { vec_t(struct expr) args = {0}; - int i = 0, n = ex.ty->fn.params.n; + const struct type *ty = ex.ty; + int i = 0; + + if (ty->t == TYptr) + ty = ty->child; - if (ex.ty->t != TYfn) - fatal(P, ex.span, "callee is not a function (is %t)", ex.ty); + if (ty->t != TYfn) + fatal(P, ex.span, "callee is not a function (is %t)", ty); while (!lexmatch(P, NULL, ')')) { + int n = ty->fn.params.n; struct expr arg; - if (i == n && ! ex.ty->fn.variadic) + if (i == n && ! ty->fn.variadic) fatal(P, arg.span, "too many args for call: (expected %d)", n); if (i < n) - P->targty = ex.ty->fn.params.d[i]; + P->targty = ty->fn.params.d[i]; arg = parseexpr(P); - if (i < n && !typeof2(arg.ty, ex.ty->fn.params.d[i++])) + if (i < n && !typeof2(arg.ty, ty->fn.params.d[i++])) fatal(P, arg.span, "call argument #%d type mismatch (%t, expected %t)", - i, arg.ty, ex.ty->fn.params.d[i - 1]); + i, arg.ty, ty->fn.params.d[i - 1]); vec_push(&args, arg); if (!lexmatch(P, NULL, ',')) { @@ -966,7 +971,7 @@ pexpostfix(struct parser *P) { ex.call.callee = exprdup(ex); ex.t = Ecall; ex.span = tok.span; - ex.ty = ex.call.callee->ty->fn.retty; + ex.ty = ty->fn.retty; vec_slice_cpy(&ex.call.args, &args); } else if (lexmatch(P, &tok, '[')) { struct expr lhs = ex, diff --git a/bootstrap/test.cff b/bootstrap/test.cff index d7f86bf..fb1741d 100644 --- a/bootstrap/test.cff +++ b/bootstrap/test.cff @@ -27,6 +27,10 @@ fn isort(xs *int, n usize) void { return lhs - rhs; } qsort(xs, n, 4, &icmp); + + fn foo() void {} + let x= &foo; + x(); } extern fn main (argc int, argv **u8) int { -- cgit v1.2.3