Skip to content

Commit 5adee36

Browse files
committed
Fix type of indirect function calls
Fixes #587 Signed-off-by: Roberto Raggi <[email protected]>
1 parent 71afe0f commit 5adee36

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/parser/cxx/type_checker.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,32 @@ void TypeChecker::Visitor::operator()(CallExpressionAST* ast) {
460460
}
461461

462462
auto functionType = type_cast<FunctionType>(ast->baseExpression->type);
463+
464+
if (!functionType) {
465+
if (control()->is_pointer(ast->baseExpression->type)) {
466+
// ressolve pointer to function type
467+
functionType = type_cast<FunctionType>(
468+
control()->get_element_type(ast->baseExpression->type));
469+
}
470+
471+
if (functionType && is_parsing_c()) {
472+
(void)ensure_prvalue(ast->baseExpression);
473+
}
474+
}
475+
463476
if (!functionType) {
477+
// todo: enable when support for the __builtin_<op> functions is added
478+
479+
#if false
480+
error(ast->firstSourceLocation(),
481+
std::format("invalid call of type '{}'",
482+
to_string(ast->baseExpression->type)));
483+
#endif
464484
return;
465485
}
466486

487+
// TODO: check the arguments
488+
467489
ast->type = functionType->returnType();
468490

469491
if (control()->is_lvalue_reference(ast->type)) {

tests/unit_tests/sema/call_c_01.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %cxx -verify -fcheck %s
2+
3+
int f() { return 0; }
4+
5+
int main() {
6+
int (*fptr)() = f;
7+
(void)_Generic(fptr(), int: 1);
8+
}

0 commit comments

Comments
 (0)