[project @ 2002-01-22 15:55:59 by simonmar]
[ghc-hetmet.git] / ghc / utils / hsc2hs / template-hsc.h
1 #if __GLASGOW_HASKELL__ && __GLASGOW_HASKELL__ < 409
2 #include <Rts.h>
3 #endif
4 #include <HsFFI.h>
5
6 #include <stddef.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <ctype.h>
11
12 #ifndef offsetof
13 #define offsetof(t, f) ((size_t) &((t *)0)->f)
14 #endif
15
16 #if __NHC__
17 #define hsc_line(line, file) \
18     printf ("# %d \"%s\"\n", line, file);
19 #else
20 #define hsc_line(line, file) \
21     printf ("{-# LINE %d \"%s\" #-}\n", line, file);
22 #endif
23
24 #define hsc_const(x)                        \
25     if ((x) < 0)                            \
26         printf ("%ld", (long)(x));          \
27     else                                    \
28         printf ("%lu", (unsigned long)(x));
29
30 #define hsc_const_str(x)                                          \
31     {                                                             \
32         const char *s = (x);                                      \
33         printf ("\"");                                            \
34         while (*s != '\0')                                        \
35         {                                                         \
36             if (*s == '"' || *s == '\\')                          \
37                 printf ("\\%c", *s);                              \
38             else if (*s >= 0x20 && *s <= 0x7E)                    \
39                 printf ("%c", *s);                                \
40             else                                                  \
41                 printf ("\\%d%s",                                 \
42                         (unsigned char) *s,                       \
43                         s[1] >= '0' && s[1] <= '9' ? "\\&" : ""); \
44             ++s;                                                  \
45         }                                                         \
46         printf ("\"");                                            \
47     }
48
49 #define hsc_type(t)                                         \
50     if ((t)(int)(t)1.4 == (t)1.4)                           \
51         printf ("%s%d",                                     \
52                 (t)(-1) < (t)0 ? "Int" : "Word",            \
53                 sizeof (t) * 8);                            \
54     else                                                    \
55         printf ("%s",                                       \
56                 sizeof (t) >  sizeof (double) ? "LDouble" : \
57                 sizeof (t) == sizeof (double) ? "Double"  : \
58                 "Float");
59
60 #define hsc_peek(t, f) \
61     printf ("(\\hsc_ptr -> peekByteOff hsc_ptr %ld)", (long) offsetof (t, f));
62
63 #define hsc_poke(t, f) \
64     printf ("(\\hsc_ptr -> pokeByteOff hsc_ptr %ld)", (long) offsetof (t, f));
65
66 #define hsc_ptr(t, f) \
67     printf ("(\\hsc_ptr -> hsc_ptr `plusPtr` %ld)", (long) offsetof (t, f));
68
69 #define hsc_enum(t, f, print_name, x)         \
70     print_name;                               \
71     printf (" :: %s\n", #t);                  \
72     print_name;                               \
73     printf (" = %s ", #f);                    \
74     if ((x) < 0)                              \
75         printf ("(%ld)\n", (long)(x));        \
76     else                                      \
77         printf ("%lu\n", (unsigned long)(x));
78
79 #define hsc_haskellize(x)                                          \
80     {                                                              \
81         const char *s = (x);                                       \
82         int upper = 0;                                             \
83         if (*s != '\0')                                            \
84         {                                                          \
85             putchar (tolower (*s));                                \
86             ++s;                                                   \
87             while (*s != '\0')                                     \
88             {                                                      \
89                 if (*s == '_')                                     \
90                     upper = 1;                                     \
91                 else                                               \
92                 {                                                  \
93                     putchar (upper ? toupper (*s) : tolower (*s)); \
94                     upper = 0;                                     \
95                 }                                                  \
96                 ++s;                                               \
97             }                                                      \
98         }                                                          \
99     }