[project @ 2004-11-10 03:20:31 by wolfgang]
[ghc-hetmet.git] / ghc / utils / hsc2hs / template-hsc.h
index c235f24..bdc34ed 100644 (file)
@@ -6,47 +6,20 @@
 #include <stddef.h>
 #include <string.h>
 #include <stdio.h>
+#include <stdarg.h>
+#include <ctype.h>
 
 #ifndef offsetof
 #define offsetof(t, f) ((size_t) &((t *)0)->f)
 #endif
 
-#if __GLASGOW_HASKELL__
-
-static int hsc_options_started;
-
-static void hsc_begin_options (void)
-{
-#if __GLASGOW_HASKELL__ < 409
-    printf ("{-# OPTIONS -optc-D__GLASGOW_HASKELL__=%d", __GLASGOW_HASKELL__);
-    hsc_options_started = 1;
+#if __NHC__
+#define hsc_line(line, file) \
+    printf ("# %d \"%s\"\n", line, file);
 #else
-    hsc_options_started = 0;
+#define hsc_line(line, file) \
+    printf ("{-# LINE %d \"%s\" #-}\n", line, file);
 #endif
-}
-
-static void hsc_option (const char *s)
-{
-    if (!hsc_options_started)
-    {
-        printf ("{-# OPTIONS");
-        hsc_options_started = 1;
-    }
-    printf (" %s", s);
-}
-
-static void hsc_end_options (void)
-{
-    if (hsc_options_started) printf (" #-}\n");
-}
-
-#else /* !__GLASGOW_HASKELL__ */
-
-#define hsc_begin_options()
-#define hsc_option(s)
-#define hsc_end_options()
-
-#endif /* !__GLASGOW_HASKELL__ */
 
 #define hsc_const(x)                        \
     if ((x) < 0)                            \
@@ -68,7 +41,7 @@ static void hsc_end_options (void)
                 printf ("\\%d%s",                                 \
                         (unsigned char) *s,                       \
                         s[1] >= '0' && s[1] <= '9' ? "\\&" : ""); \
-            s++;                                                  \
+            ++s;                                                  \
         }                                                         \
         printf ("\"");                                            \
     }
@@ -93,3 +66,40 @@ static void hsc_end_options (void)
 #define hsc_ptr(t, f) \
     printf ("(\\hsc_ptr -> hsc_ptr `plusPtr` %ld)", (long) offsetof (t, f));
 
+#define hsc_offset(t, f) \
+    printf("(%ld)", (long) offsetof (t, f));
+
+#define hsc_size(t) \
+    printf("(%ld)", (long) sizeof(t));
+
+#define hsc_enum(t, f, print_name, x)         \
+    print_name;                               \
+    printf (" :: %s\n", #t);                  \
+    print_name;                               \
+    printf (" = %s ", #f);                    \
+    if ((x) < 0)                              \
+        printf ("(%ld)\n", (long)(x));        \
+    else                                      \
+        printf ("%lu\n", (unsigned long)(x));
+
+#define hsc_haskellize(x)                                          \
+    {                                                              \
+        const char *s = (x);                                       \
+        int upper = 0;                                             \
+        if (*s != '\0')                                            \
+        {                                                          \
+            putchar (tolower (*s));                                \
+            ++s;                                                   \
+            while (*s != '\0')                                     \
+            {                                                      \
+                if (*s == '_')                                     \
+                    upper = 1;                                     \
+                else                                               \
+                {                                                  \
+                    putchar (upper ? toupper (*s) : tolower (*s)); \
+                    upper = 0;                                     \
+                }                                                  \
+                ++s;                                               \
+            }                                                      \
+        }                                                          \
+    }