www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cryptic C function pointer for conversion

reply data pulverizer <data.pulverizer gmail.com> writes:
I have come across a function pointer in C that I am attempting 
to convert, and am not sure what the current interpretation is:

```
\\ The C Code:
void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
```

The best I can tell is that this is a function pointer that 
returns a function that returns void and the correct translation 
to D is:

```
alias void function(sqlite3_vfs*,void*, const char *zSymbol) ptr;
ptr* function() xDlSym;
```

I've never seen a construction like this before so my 
interpretation might be wrong!
Dec 17 2016
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Saturday, 17 December 2016 at 13:39:27 UTC, data pulverizer 
wrote:

that is what it means, in D:

//void (*(*xDlSym)(sqlite3_vfs*,void*, const char 
*zSymbol))(void);

struct sqlite3_vfs {}

extern(C) {
alias RetRes = void function ();
alias DeclType = RetRes function (sqlite3_vfs *a,void *b, const 
char *zSymbol);

DeclType xDlSym;

void zoo (void) {}
auto goo (sqlite3_vfs *a,void *b, const char *zSymbol) { return 
&zoo; }
}

void main () {
   xDlSym = &goo;
}


at least that is what i managed to decode, fed to C(++) compiler 
and translate to D.
Dec 17 2016
next sibling parent reply data pulverizer <data.pulverizer gmail.com> writes:
On Saturday, 17 December 2016 at 14:06:07 UTC, ketmar wrote:
 that is what it means, in D:

 //void (*(*xDlSym)(sqlite3_vfs*,void*, const char 
 *zSymbol))(void);

 struct sqlite3_vfs {}

 extern(C) {
 alias RetRes = void function ();
 alias DeclType = RetRes function (sqlite3_vfs *a,void *b, const 
 char *zSymbol);
 ...
 }
Thanks ketmar, I guess that this means I got it the other way round the function pointer that is returned is the function that takes in and returns void.
 at least that is what i managed to decode, fed to C(++) 
 compiler and translate to D.
Does this mean that you can translate C code to D natively? I am currently only aware of the dstep package.
Dec 17 2016
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Saturday, 17 December 2016 at 15:15:26 UTC, data pulverizer 
wrote:

 Does this mean that you can translate C code to D natively? I 
 am currently only aware of the dstep package.
with my head and bare hands. well, armed with some regular expressions. did you seen some of my "port" announcements? they all done manually. it's not that hard, mostly search-and-replace. also, i did used c++ 'cause it has `auto` feature, so i pasted your declaration, and then played with c++ and -Wall until it silenced. actually, void zoo (void) {} auto goo (sqlite3_vfs *a,void *b, const char *zSymbol) { return &zoo; } was taken verbatim from c++. as you can see, it even has `(void)`, which is forbidden in D (but allowed in my dmd fork ;-).
Dec 17 2016
parent ketmar <ketmar ketmar.no-ip.org> writes:
p.s.: that means that i didn't really *decoded* that declaration, 
just brute-forced someting that c++ compiler happily accepts. so 
take it with a grain of salt. ;-)
Dec 17 2016
prev sibling parent bachmeier <no spam.net> writes:
On Saturday, 17 December 2016 at 15:15:26 UTC, data pulverizer 
wrote:

 Does this mean that you can translate C code to D natively? I 
 am currently only aware of the dstep package.
It may not help you, but something I've done in the past is use Swig to create a Common Lisp interface. It translates the C to Common Lisp. That is generally much easier for me to understand.
Dec 17 2016
prev sibling parent data pulverizer <data.pulverizer gmail.com> writes:
On Saturday, 17 December 2016 at 14:06:07 UTC, ketmar wrote:
 On Saturday, 17 December 2016 at 13:39:27 UTC, data pulverizer 
 wrote:

 that is what it means, in D:

 //void (*(*xDlSym)(sqlite3_vfs*,void*, const char 
 *zSymbol))(void);

 struct sqlite3_vfs {}

 extern(C) {
 alias RetRes = void function ();
 alias DeclType = RetRes function (sqlite3_vfs *a,void *b, const 
 char *zSymbol);

 DeclType xDlSym;

 void zoo (void) {}
 auto goo (sqlite3_vfs *a,void *b, const char *zSymbol) { return 
 &zoo; }
 }

 void main () {
   xDlSym = &goo;
 }


 at least that is what i managed to decode, fed to C(++) 
 compiler and translate to D.
p.s. I confirmed your interpretation on stackoverflow: http://stackoverflow.com/questions/8722817/syntax-for-a-pointer-to-a-function-returning-a-function-pointer-in-c
Dec 17 2016