Opened 3 years ago

Last modified 3 years ago

#229 new defect

Incorrect implementation of casting to monomorphic

Reported by: f37yu Owned by:
Priority: major Component: cfa-cc
Version: 1.0 Keywords:
Cc:

Description

This program attempts to assign a polymorphic function to a compatible monomorphic function pointer. It is allowed by cfa-cpp but produces incorrect C code:

forall (dtype T | {int asst (T *);}) void foo (T * x) {}

int asst (void *) { return 1; }

void f() {
    void (*bar) (void*);
    bar = foo;
}

cfa-cpp output:

void _X3fooQ1_0_0_1__X4asstFi_PBD0__Fv_PBD0__1(__attribute__ ((unused)) signed int (*_X4asstFi_PY1T__1)(void *__param_0), void *_X1xPY1T_1){
}
signed int _X4asstFi_Pv__1(__attribute__ ((unused)) void *__anonymous_object0){
    __attribute__ ((unused)) signed int _X12_retval_assti_1;
    {
        ((void)(_X12_retval_assti_1=1) /* ?{} */);
    }

    return _X12_retval_assti_1;
}
void _X1fFv___1(){
    void (*_X3barFv_Pv__2)(void *__param_0);
    {
        ((void)(_X3barFv_Pv__2=_X3fooQ1_0_0_1__X4asstFi_PBD0__Fv_PBD0__1));
    }

}

The incompatible type problem is caught by gcc afterwards.

test.c: In function '_X1fFv___1':
test.c:7:31: warning: assignment to 'void (*)(void *)' from incompatible pointer type 'void (*)(int (*)(void *), void *)' [-Wincompatible-pointer-types]
    7 |     bar = foo;

Change History (1)

comment:1 Changed 3 years ago by f37yu

I propose to correctly support this feature by generating a local adapter (the same thing created to satisfy an assertion with a polymorphic function) like this:

void f() {
    void (*bar) (void *);
    void __adapter_foo(void *arg) {
        foo(asst, arg);
    }
    bar = __adapter_foo;
}
Last edited 3 years ago by f37yu (previous) (diff)
Note: See TracTickets for help on using tickets.