From 6bedd04d011eaadf9ec0dc865bb8f84127db7e75 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sat, 14 Mar 2020 15:37:03 -0400 Subject: fixed problems with foreign varargs --- cgen.c | 5 +++++ test.toc | 3 ++- types.c | 6 ++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cgen.c b/cgen.c index 75d3803..ab65f30 100644 --- a/cgen.c +++ b/cgen.c @@ -1326,6 +1326,11 @@ static void cgen_expr(CGenerator *g, Expression *e) { cgen_write(g, "%.16Lf", (long double)e->floatl); break; case EXPR_LITERAL_INT: + /* make sure it's the right type (for variadic foreign functions) */ + cgen_write(g, "("); + cgen_type_pre(g, &e->type); + cgen_type_post(g, &e->type); + cgen_write(g, ")"); cgen_write(g, U64_FMT, e->intl); break; case EXPR_LITERAL_STR: { diff --git a/test.toc b/test.toc index 56ba2e0..ba33c5d 100644 --- a/test.toc +++ b/test.toc @@ -2,7 +2,8 @@ printf ::= #foreign("printf","libc.so.6") fn(#C &"const char", #C ..) #C int; main ::= fn() { - printf("hey\n\0"); + x := "hey %ld %ld %ld %ld %ld\n\0"; + printf(&x[0], 5, 3, 8, 9, 4); }; main(); \ No newline at end of file diff --git a/types.c b/types.c index b584528..9f3d2c6 100644 --- a/types.c +++ b/types.c @@ -2103,8 +2103,10 @@ static Status types_expr(Typer *tr, Expression *e) { } else { if (nargs != nparams) { - err_print(e->where, "Expected %lu arguments to function call, but got %lu.", (unsigned long)nparams, (unsigned long)nargs); - return false; + if (!has_varargs || nargs < nparams-1) { + err_print(e->where, "Expected %lu arguments to function call, but got %lu.", (unsigned long)nparams, (unsigned long)nargs); + return false; + } } for (size_t p = 0; p < nargs; ++p) { if (args[p].name) { -- cgit v1.2.3