After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 558106 - support typeof to emit C type, for use of API based C macros
support typeof to emit C type, for use of API based C macros
Status: RESOLVED WONTFIX
Product: vala
Classification: Core
Component: Code Generator
0.4.x
Other All
: Normal enhancement
: ---
Assigned To: Jürg Billeter
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2008-10-27 14:29 UTC by sam
Modified: 2009-06-04 08:49 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Allow TypeSymbols in emitted C code for some C library macros (4.14 KB, patch)
2008-10-27 14:32 UTC, sam
reviewed Details | Review

Description sam 2008-10-27 14:29:31 UTC
Some API's are part-implemented by C macros, and want the C type to be passed as an argument. 

The typeof argument is perhaps stringified for debugging or error checking, or maybe has sizeof applied or whatever.

talloc, a memory allocator I use is one example of such an API.

I'm submitting a patch whereby if typeof(vala_class) is passed as a varargs parameter, that it ought to emit the underlying C type.

Maybe varags is a bit of a hack, but vala doesn't support typesymbol as a first class type (neither does C) so clearly this will only be useful for macros!
Comment 1 sam 2008-10-27 14:32:14 UTC
Created attachment 121432 [details] [review]
Allow TypeSymbols in emitted C code for some C library macros

The attached patch to vala 0.3.5 allows vala to emit naked type symbols
as arguments to ellipses functions.
(The ellipses is really a red-herring, but it is currently the favoured
way to pass non-type-checked arguments to library macros which vala is
wrapping as functions - maybe some work could be done on "void" here, or
"any" [like python] for un-type-checked arguments)

This function is useful when a macro wants the C type passing, generally
for stringifying or casting; I particularly need it to make use of this
Samba4 talloc library macro, which is a pretty darn useful macro, wrapped
with this vapi:
      [CCode (sentinel="")]
      public void* zero(void* mem_ctx, ...);

NOTE: only one of the fragment for
  gobject/valaccodeinvocationexpressionbinding.vala
and
  gobject/valaccodegenerator.vala
is needed.

valaccodeinvocationexpressionbinding has the most specific patch only
covering the ellipses case in method invocation generation,
valaccodegenerator has a more general case and triggers quite often and
so for all I know has some broader side effects.

Perhaps the valaccodegenerator.vala patch should check that the parent
symbol is an argument
Comment 2 Jürg Billeter 2008-11-21 09:02:21 UTC
We shortly already talked about this on IRC some time ago. I still don't like the idea to abuse typeof for that. We're in the process of making varargs typesafe, we don't want to introduce even more issues.

Is there no other solution for the issue you're facing? Can you describe how exactly you're using these talloc functions? Using generics might be a lot better for this use case.
Comment 3 sam 2009-02-12 16:35:39 UTC
I don't think this conflicts with the idea of varargs being typesafe at all.

I'm just asking that vala be able to emit the type symbol.

talloc uses the type symbol in 2 ways:
1. it calls sizeof() so it knows how much to allocate
2. it stringifies it for reporting, debug and run-time type checking

It is perfectly reasonable behaviour for a C api to use sizeof() in macros, we ought to support the emitting of type symbols

Sam
Comment 4 Jürg Billeter 2009-04-14 17:34:22 UTC
The proposed solution does not make sense from a language point of view. If you can find a solution that fits better to the language semantics (maybe using generics and/or CCode attributes), please reopen the bug.
Comment 5 sam 2009-06-04 08:49:36 UTC
As varargs is [to be] typesafe, perhaps we can use a notation in vapi definitions for arguments which expect the type symbol.

I'll contrast the method I was proposing with what is perhaps a more acceptable alternative. Please don't get distracted by the talloc api and whether or not that could be implemented in a better way, it's just an example of a C macro API that expects the type symbol as an argument.

I had been using vapi:
      [CCode (sentinel="")]
      public void* zero(void* mem_ctx, ...);

and vala:
      a_compact_class cc = talloc.zero(parent, typeof(a_compact_class));

which emitted C something like:
      struct a_compact_class cc = talloc_zero(parent, struct a_compact_class);

however, as this abuses typeof from a language point of view (but IMHO not from a semantic point of view), how about vapi:

      [CCode (sentinel="")]
      public void* zero(void* mem_ctx, typesymbol type);

vala:
      a_compact_class cc = talloc.zero(parent, a_compact_class);

I don't prefer this method because in fact for the C macro, this is a typeless argument, and I can't see how this method will work for ellipses arguments - but it moves the magic to the declaration rather than the use, and this may be preferable from a language point of view.