From 2a9c631e88effc41d3e6432ab31767d311afad44 Mon Sep 17 00:00:00 2001 From: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Date: Sun, 29 Dec 2024 00:25:47 +0800 Subject: [PATCH] Handle pointer arithmetics with function operands --- parse.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/parse.c b/parse.c index 16cdffb..3da1900 100644 --- a/parse.c +++ b/parse.c @@ -3383,6 +3383,11 @@ static Node *new_add(Node *lhs, Node *rhs, Token *tok) { if (is_numeric(lhs->ty) && is_numeric(rhs->ty)) return new_binary(ND_ADD, lhs, rhs, tok); + if (lhs->ty->kind == TY_FUNC) + lhs = new_cast(lhs, pointer_to(lhs->ty)); + if (rhs->ty->kind == TY_FUNC) + rhs = new_cast(rhs, pointer_to(rhs->ty)); + Node **ofs = is_integer(lhs->ty) ? &lhs : is_integer(rhs->ty) ? &rhs : NULL; Node *ptr = lhs->ty->base ? lhs : rhs->ty->base ? rhs : NULL; @@ -3407,6 +3412,11 @@ static Node *new_sub(Node *lhs, Node *rhs, Token *tok) { if (is_numeric(lhs->ty) && is_numeric(rhs->ty)) return new_binary(ND_SUB, lhs, rhs, tok); + if (lhs->ty->kind == TY_FUNC) + lhs = new_cast(lhs, pointer_to(lhs->ty)); + if (rhs->ty->kind == TY_FUNC) + rhs = new_cast(rhs, pointer_to(rhs->ty)); + // ptr - num if (lhs->ty->base && is_integer(rhs->ty)) { if (lhs->ty->base->kind == TY_VLA)