December 14, 2004
Russ Lewis wrote:
> I'm pretty sure that the code you wrote below exposes another bug in the compiler.  That cast would certainly be broken in any architecture where the C and D calling conventions were different.
> 

Ah yes, you are correct to question this one... Obviously I was getting excited about the possibilities without thinking the problem through clearly... just being hopeful.

Example:

# import std.stdio;
#
# extern (C) alias int function( int, int, int) functype;
#
# int func(int a, int b, int c)
# {
#    writefln("Test: %d, %d, %d", a, b, c);
#    return a+b+c;
# }
#
# void main()
# {	
#    functype f = cast(functype) &func;
#
#    writefln("Return value: %d", f(2,4,8));
# }

Output:

Test: 4, 2, 4202512
Return Value: 4202518
Error: Access Violation

In this case "func" is an extern(D) type and "f" is extern(C).

Now the question is: is this example equivalent to casting a function literal?  I'm not sure yet.

I was hoping that the function literal could be coerced in its definition by the cast to a different calling convention.

- John

December 14, 2004
Russ Lewis wrote:
> I'm pretty sure that the code you wrote below exposes another bug in the compiler.  That cast would certainly be broken in any architecture where the C and D calling conventions were different.
> 

And finally a proper example that demonstrates why what Russ said here is true:

# import std.stdio;
#
# extern (C) alias int function( int, int, int) functype;
#
# void main()
# {
#     functype f = cast(functype) function int(int a, int b, int c) {
#                writefln("Test function literal: %d, %d, %d", a, b, c);
#                return a+b+c;
#              };
#	
#     writefln("Return value: %d", f(2,4,8));
# }

Output:

Test Function literal: 4, 2, 4202564
Return value: 4202570
Error: Access Violation

"Can't declare extern(C) fp in function body" is a valid complaint. Fixing this in D should improve interfacing with external C code.

- John
December 15, 2004
This was mentioned in passing in this thread, but I thought I would mark it explicitly: at least in the context of a cast, I can't declare the type
	extern(C) int function()

The following module won't compile, but I'm pretty sure that it should.  (Can we add this to DStress?):

> extern(C) void *wglGetProcAddress(char*);
> extern(C) int function() glFunctionFoo;
>  static this() {
>   glFunctionFoo = cast(extern(C) int function())
>                       wglGetProcAddress("glFunctionFoo");
> }

Second, here's an example which (correctly) fails because I don't add the extern(C) modifier.  This would be good to add to DStress as expfail:

> extern(C) void *wglGetProcAddress(char*);
> extern(C) int function() glFunctionFoo;
>  static this() {
>   glFunctionFoo = cast(int function())
>                       wglGetProcAddress("glFunctionFoo");
> }

December 15, 2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Added to DStress as http://svn.kuehne.cn/dstress/nocompile/extern_06.d http://svn.kuehne.cn/dstress/compile/extern_07.d

Thomas

Russ Lewis schrieb am Tue, 14 Dec 2004 20:23:47 -0700:
> This was mentioned in passing in this thread, but I thought I would mark
> it explicitly: at least in the context of a cast, I can't declare the type
> 	extern(C) int function()
>
> The following module won't compile, but I'm pretty sure that it should.
>   (Can we add this to DStress?):
>
>> extern(C) void *wglGetProcAddress(char*);
>> extern(C) int function() glFunctionFoo;
>> 
>> static this() {
>>   glFunctionFoo = cast(extern(C) int function())
>>                       wglGetProcAddress("glFunctionFoo");
>> }
>
> Second, here's an example which (correctly) fails because I don't add the extern(C) modifier.  This would be good to add to DStress as expfail:
>
>> extern(C) void *wglGetProcAddress(char*);
>> extern(C) int function() glFunctionFoo;
>> 
>> static this() {
>>   glFunctionFoo = cast(int function())
>>                       wglGetProcAddress("glFunctionFoo");
>> }
>

-----BEGIN PGP SIGNATURE-----

iD8DBQFBwHe03w+/yD4P9tIRAk0OAJ0e+R9x8FJRZ5SJofA4yEXtdoDMYgCgvJQR
TaV+pFaIj1jAWhHStB1lQMA=
=6K9A
-----END PGP SIGNATURE-----
Next ›   Last »
1 2