extended foreign decls

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Wed Jan 3 14:36:20 EST 2001

Wed, 3 Jan 2001 21:44:03 +1100, Fergus Henderson <fjh at cs.mu.oz.au> pisze:

> Personally I'd rather have a single well-designed convenient foreign
> language interface as a standard part of the language, rather than
> having a minimalistic foreign language interface in the language
> standard and having convenience provided by a separate tool (or by
> several competing separate tools).  But I understand why you might
> differ on that point.

I would prefer that too if it was possible, but it does not look well.
Interfacing to C is complex enough, it's yet worse for other languages.

A problem: HsFFI includes some C headers (stdint.h, which includes
among others features.h on glibc). There are headers like stdio.h (if
I remember correctly) which provide certain prototypes or macros only
when certain macros are defined, like _XOPEN_SOURCE and _GNU_SOURCE.

If I #define these constants after inclusion HsFFI.h, it's too late:
features.h already determined what should be visible and it will not
be processed again. But hsc2hs does not distinguish between #defines
which should go before HsFFI.h and those which should go after. So
I had to define them via {-# OPTIONS -optc-D_XOPEN_SOURCE=500 #-}
instead of #define, or rather hsc2hs's #option (because it generates
its own {-# OPTIONS #-} and only one such pragma used to be accepted by
ghc), to be visible early enough. But hsc2hs needs them defined during
its job of preprocessing (for constants), so #define is needed too,
protected by #undef because symbols will be already defined when
#defines are processed in the generated C header (they are put in
multiple places):

#option -optc-D_GNU_SOURCE -optc-D_XOPEN_SOURCE=500
#undef _GNU_SOURCE
#define _GNU_SOURCE 1
#define _XOPEN_SOURCE 500

Ugly, isn't it?

Better ugly than impossible, assuming that the compiler provided only
a single elegant way of handling #defines.

Would a builtin support predict all possible uses of C features?

 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
  ^^                      SYGNATURA ZASTÊPCZA

More information about the FFI mailing list