www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - overloaded extern(C) called without error

reply =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
I know that extern(C) functions cannot be legally overloaded, but 
not issuing an error in the following situation seems wrong / a 
bug:

     extern(C) void foo(int);
     extern(C) void foo() {}

     void main()
     {
         foo(42);
     }
Sep 10 2013
next sibling parent reply =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
I just realized I wasn't clear -- it calls the (wrong) overloaded 
function:

     extern(C) void foo(int);
     extern(C) void foo() { writeln("yes, this is called"); }

     void main()
     {
         foo(42);
     }

outputs:

     yes, this is called
Sep 10 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-09-10 16:38, Dicebot wrote:

 This is why mixing ABI and mangling in one entity is bad. And why
 overloading extern(C) functions is compile-time error in C++.

There's a GCC (Clang) extension that enables support function overloading in C. I guess it's a bit overkill to support in D. -- /Jacob Carlborg
Sep 11 2013
prev sibling next sibling parent "Dicebot" <public dicebot.lv> writes:
On Tuesday, 10 September 2013 at 14:07:18 UTC, Luís Marques wrote:
 I just realized I wasn't clear -- it calls the (wrong) 
 overloaded function:

     extern(C) void foo(int);
     extern(C) void foo() { writeln("yes, this is called"); }

     void main()
     {
         foo(42);
     }

 outputs:

     yes, this is called

This is why mixing ABI and mangling in one entity is bad. And why overloading extern(C) functions is compile-time error in C++.
Sep 10 2013
prev sibling next sibling parent "David Nadlinger" <code klickverbot.at> writes:
On Tuesday, 10 September 2013 at 14:04:58 UTC, Luís Marques wrote:
     extern(C) void foo(int);
     extern(C) void foo() {}

I agree that this should not be legal. LDC detects this in the glue layer (we would have to add a workaround for the corresponding LLVM restriction otherwise): --- $ ldc2 test.d test.d(4): Error: Function type does not match previously declared function with the same mangled name: foo --- David
Sep 10 2013
prev sibling next sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Tuesday, 10 September 2013 at 14:38:56 UTC, Dicebot wrote:
 On Tuesday, 10 September 2013 at 14:07:18 UTC, Luís Marques 
 wrote:
 I just realized I wasn't clear -- it calls the (wrong) 
 overloaded function:

    extern(C) void foo(int);
    extern(C) void foo() { writeln("yes, this is called"); }

    void main()
    {
        foo(42);
    }

 outputs:

    yes, this is called

This is why mixing ABI and mangling in one entity is bad. And why overloading extern(C) functions is compile-time error in C++.

I think this is another form of the unclear qualifier binding problem. Qualifier bind to symbol : mangling. Qualifier bind to type : calling convention.
Sep 11 2013
prev sibling parent "Dicebot" <public dicebot.lv> writes:
On Wednesday, 11 September 2013 at 08:26:02 UTC, deadalnix wrote:
 I think this is another form of the unclear qualifier binding 
 problem.

 Qualifier bind to symbol : mangling.
 Qualifier bind to type : calling convention.

Yeah, giving tool to differ those can be an elegant way out of this inconvenience.
Sep 11 2013