Suggestion for hsc2hs (structures with commas)

Dimitry Golubovsky golubovsky at
Tue Jun 14 10:19:05 EDT 2005

I came across the following issue in hsc2hs:

when requesting a size of a C structure with #size (or similarly this
may apply to #peek, #poke, #offset macros), and the structure is
declared "anonymously", and contains multiple field declaration of the
same type such as:

struct {int ab,cd;}

then #size(struct {int ab,cd;}) will result in

hsc_size(struct {int ab,cd;})

which will fail because of the comma inside the structure makes the
macro recognize 2 arguments instead of 1.

GCC's preprocessor allows variadic macros defined like

#define __quote__(x...) x

If hsc_size is called:

hsc_size(__quote__(struct {int ab,cd;}))

then error does not occur.


hsc_peek(__quote__(struct {int ab,cd;}),cd)

because __quote__ is scanned first, and its result is treated as a
single argument.

I looked through, but could not find any other way to quote
macro arguments.

Is it possible to update hsc2hs (Main.hs I believe) to emit macro
calls involving C types in the way described above?

i. e. #size(t) becomes hsc_size(__quote__(t)),
#peek(t,f) becomes hsc_peek(__quote__(t),f) etc.

when hsc2hs generates the C file from a hsc source.

PS I am working with hsc2hs from the GHC 6.2.2 distribution. I
couldn't find such fixes in the changelog since then.

I believe, the person maintaining hsc2hs might be able to implement
these fixes faster than if I tried.

PPS This will work with GCC, but I am not sure abouth other compilers
(like Microsoft C)

Dimitry Golubovsky

Anywhere on the Web

More information about the Glasgow-haskell-users mailing list