Bug 8656 - [3.3/3.4 Regression] Unable to assign function with __attribute__ and pointer return type to an appropriate variable
Summary: [3.3/3.4 Regression] Unable to assign function with __attribute__ and pointer...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2
: P3 normal
Target Milestone: 3.3.2
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on: 12344
Blocks:
  Show dependency treegraph
 
Reported: 2002-11-20 03:06 UTC by rmathew
Modified: 2004-01-17 04:22 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description rmathew 2002-11-20 03:06:03 UTC
Consider the following source code saved in file "bar.c":
--------------------------- 8< ---------------------------
extern int * (__attribute__((stdcall)) *fooPtr)( void);

int * __attribute__((stdcall)) myFn01( void) { return 0; }

void snafu( void)
{
    fooPtr = myFn01;
}
--------------------------- 8< ---------------------------

Running "g++ -c bar.c" gives:
--------------------------- 8< ---------------------------
bar.c: In function `void snafu()':
bar.c:7: invalid conversion from `int*(*)()' to `int*(*)()'
--------------------------- 8< ---------------------------

However, running "gcc -c bar.c" creates no such problems.

With reference to the source code given above, here're
a few more observations:

1. This problem is not shown by the 2.95.3 g++.

2. If the definition of myFn01 above is changed to
   a declaration or if a declaration for the same is
   added before it, the problem disappears.

3. The problem appears only if the return type is
   of the kind "xyz *" - if there's a "typedef int *intptr;"
   and we use "intptr" instead of "int *" for myFn01, the
   problem disappears.

4. The problem can also be circumvented by putting the
   attribute and the function name together in parentheses
   like "int * (__attribute__((stdcall)) myFn01)( void)".

5. The problem does not come if all the "stdcall" instances
   are replaced by "cdecl", but it reappears if they are
   replaced by "fastcall".

6. Both Borland C/C++ 5.5 and MSVC++ 6.2 compile this code
   perfectly (after using the MS equivalent "__stdcall").

Lastly, this looks very similar to PR 6626, but IMHO is
different - the syntax and semantics are perfectly valid
in this case as far as I can see.

Release:
GCC 3.2

Environment:
MinGW, Cygwin and Linux(x86) (and possibly other x86-32
targets)

How-To-Repeat:
Just save the sample code given above as file "bar.c" (or
whatever) and run "g++ -c" on it.
Comment 1 Christian Ehrhardt 2002-12-05 05:54:11 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: I'm not sure if this is a bug or nor. The problem is that the
    types actually differ because the attribute applies to different
    parts of the declaration. In
    
    int * __attribute__((stdcall)) F (void) {};
    
    the attribute is applied to the return type not to the function type.
    In the extern declaration
    
    extern int * (__attribute__((stdcall)) * f) (void);
    
    the attribute is applied to the function type. I'm not 100% sure if
    this is intended or a parser error but I suspect it is intended.
    
    As a result of this the error itself is justified because calling
    conventions for the function and the function pointer differ. The
    text of the error however isn't helful at all.
    
    Workaround: Put explicit parenthesis around the attribute and
    the function name:
    
    int * (__attribute__(stdcall)) F) (void) {};
    
       regards  Christian
Comment 2 Mark Mitchell 2003-10-07 07:10:45 UTC
Fixed in GCC 3.3.2 and GCC 3.4.