Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typedef function prototypes don't retain arguments #262

Open
jblachly opened this issue Nov 24, 2020 · 5 comments
Open

typedef function prototypes don't retain arguments #262

jblachly opened this issue Nov 24, 2020 · 5 comments
Labels

Comments

@jblachly
Copy link

typedef char *kgets_func(char *, int, void *);

int f(char *s, kgets_func *fgets, void *fp);

int g(char *s, char* (*fun)(char *, int, void *), void *fp);

Results in:

extern (C):

alias kgets_func = char* function (char*, int, void*);

int f (char* s, char* function () fgets, void* fp);

int g (char* s, char* function (char*, int, void*) fun, void* fp);

Note f was not translated correctly; its second parameter takes type char* function() but should take char* function(char* int, void *) or more simply kgets_func via the alias.

@jblachly
Copy link
Author

Actually, thinking more carefully about the title, I haven't proved that the typedef does not retain the function arguments, but I THINK that is the case given that g translates correctly

@jacob-carlborg
Copy link
Owner

jacob-carlborg commented Nov 24, 2020

The AST looks different for f and g. I guess that's why it fails to do a proper translation. Are you trying to create bindings for an existing library? Or is there a reason to declare f like that?

Based on the title of this issue, I think you're misunderstanding the code. kgets_func is not a function pointer, it's a function prototype. Then it becomes a function pointer when you declare parameter fgets. You might be looking for this code:

typedef char *(*kgets_func)(char *, int, void *);
int f(char *s, kgets_func fgets, void *fp);

Which is properly translated to:

extern (C):

alias kgets_func = char* function (char*, int, void*);
int f (char* s, kgets_func fgets, void* fp);

@jacob-carlborg
Copy link
Owner

The example you have provided is still a bug. But it might not be the code you want to write.

@jblachly
Copy link
Author

f comes directly from a library to which I maintain bindings:

    typedef char *kgets_func(char *, int, void *);
    
    int kgetline(kstring_t *s, kgets_func *fgets_fn, void *fp);

https://github.com/samtools/htslib/blob/51275bcbda6d1e0849d0e50d0edd13814d38ebd1/htslib/kstring.h#L116-L118

@jblachly
Copy link
Author

I see now it is a function prototype. C programmer habit of attaching pointer operator to the RHS drives me crazy.

typedef char* kgets_func(char *, int, void*) is more clear

Either way, appreciate insight into the bug as it requires manual intervention after dstep

@jacob-carlborg jacob-carlborg changed the title typedef function pointers don't retain arguments typedef function prototypes don't retain arguments Nov 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants