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

Function typedefs drop type information #50

Open
stacksmith opened this issue Aug 4, 2017 · 6 comments
Open

Function typedefs drop type information #50

stacksmith opened this issue Aug 4, 2017 · 6 comments

Comments

@stacksmith
Copy link

stacksmith commented Aug 4, 2017

Hello again. I am generating a spec from some raspbian headers. This header line:
typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
is translated to:
{ "tag": "typedef", "ns": 0, "name": "VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T", "location": "raspbian/usr/include/interface/vchi/message_drivers/message.h:156:14", "type": { "tag": ":function" } }
losing the void return type as well as the (void* state) parameter entirely.
I am using up-to-date 4.0 LLVM/CLANG this time:)

Any thoughts?

@stacksmith stacksmith changed the title C function not translating correctly... C typedef not translating correctly... Aug 4, 2017
@rpav
Copy link
Owner

rpav commented Aug 4, 2017

Yeah, at least in CL this isn't really necessary—it's just a typedef, and basically all you care about is it being a function-pointer type—so I never really bothered to add this information. If you're doing something fancy like code completion or similar, it could probably be added without a lot of trouble or breaking compatibility. I don't know if/when I'd have time to do this myself though.

@stacksmith
Copy link
Author

stacksmith commented Aug 4, 2017

Interestingly, I think it is not a function pointer, but a function stand in, describing a function. This particular one is a problem for my FFI, as the function itself is never declared anywhere else - it is a callback function description. So yes, the actual return type and parameters are absolutely necessary in this context.

https://stackoverflow.com/questions/3674200/what-does-a-typedef-with-parenthesis-like-typedef-int-fvoid-mean-is-it-a)

@rpav
Copy link
Owner

rpav commented Aug 5, 2017

Well, you can't actually use them for anything. It doesn't describe an actual function, just the "type" of a function. In C/C++, this is used more or less exclusively for validating a function pointer assignment, i.e., as per the post, if you were to do

typedef int (functype)(void);

void bar(void) { .. }

functype *fn = bar;

...you'd probably get at least a warning. But in the context of c2ffi, this is irrelevant: bar would be the function whose declaration you care about, and if any structs etc had this as an element, it only matters (to autowrap, and CFFI last I checked) that this is a function pointer you assign... no actual type checking is done.

Additionally for the declaration case in that post, you still get full declarations for the actual functions.

In this case, it looks like a callback declaration type, which is probably used for passing "correct" function pointers around in C, but in CL it doesn't matter. You need to explicitly declare your defcallback, and you don't need to cast the callback pointer to pass or assign it. Structs members will just be :pointer. So as above, even if the info was there, there's just no place to really apply it to anything.

@stacksmith
Copy link
Author

stacksmith commented Aug 5, 2017

Mostly agree. In my case I've written (well, not completely :)a cl library to inspect and reason about ffis generated by c2ffi (and possibly generate various kinds of bindings). With it I expect to be able to generate callback function types based on information collected, and oddly enough, the typedef above is the only description of the function. In any event, it seems that c2ffi's mission is to preserve any useful information for the next level to handle, and my argument here, is that the function type information is indeed useful.

My goal is to gather a complete database of all types as seen by a particular incantation of c2ffi, be able to inspect it, and interactively select a useful subset (a tree-shake of sorts). It would be nice to have a complete record of types, and the typedef may be the only such record (proven by at least this one case).

It is not a really important issue, but I'd love to see it fixed when you can.

@rpav
Copy link
Owner

rpav commented Aug 5, 2017

Yeah if you're doing something more fancy with the data, it could be useful. Will tag as feature.

@stacksmith
Copy link
Author

Thanks.

@stacksmith stacksmith changed the title C typedef not translating correctly... Function typedefs drop type information Aug 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants