mirror of
https://github.com/rui314/chibicc
synced 2025-02-19 23:23:57 +03:00
Dereferencing a function shouldn't do anything
This is an oddity in the C spec, but you can apply the unary `*` operator to a function as many times as you want. `*x` and `x` means exactly the same, given that `x` is a function.
This commit is contained in:
parent
603de502fd
commit
e0b5da3b39
13
parse.c
13
parse.c
@ -1947,8 +1947,17 @@ static Node *unary(Token **rest, Token *tok) {
|
||||
if (equal(tok, "&"))
|
||||
return new_unary(ND_ADDR, cast(rest, tok->next), tok);
|
||||
|
||||
if (equal(tok, "*"))
|
||||
return new_unary(ND_DEREF, cast(rest, tok->next), tok);
|
||||
if (equal(tok, "*")) {
|
||||
// [https://www.sigbus.info/n1570#6.5.3.2p4] This is an oddity
|
||||
// in the C spec, but dereferencing a function shouldn't do
|
||||
// anything. If foo is a function, `*foo`, `**foo` or `*****foo`
|
||||
// are all equivalent to just `foo`.
|
||||
Node *node = cast(rest, tok->next);
|
||||
add_type(node);
|
||||
if (node->ty->kind == TY_FUNC)
|
||||
return node;
|
||||
return new_unary(ND_DEREF, node, tok);
|
||||
}
|
||||
|
||||
if (equal(tok, "!"))
|
||||
return new_unary(ND_NOT, cast(rest, tok->next), tok);
|
||||
|
@ -363,6 +363,8 @@ int main() {
|
||||
ASSERT(15, struct_test38().a[14]);
|
||||
ASSERT(20, struct_test38().a[19]);
|
||||
|
||||
ASSERT(5, (***add2)(2,3));
|
||||
|
||||
printf("OK\n");
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user