Start refactoring the flag parsing
[hsc2hs.git] / 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 /* For the single-argument macros we make the macros variadic (the
13 argument is x... rather than simply x) so that arguments containing
14 commas work. See trac #590. */
15
16 #ifndef offsetof
17 #define offsetof(t, f) ((size_t) &((t *)0)->f)
18 #endif
19
20 #if __NHC__
21 #define hsc_line(line, file) \
22 printf ("# %d \"%s\"\n", line, file);
23 #else
24 #define hsc_line(line, file) \
25 printf ("{-# LINE %d \"%s\" #-}\n", line, file);
26 #endif
27
28 #define hsc_const(x...) \
29 if ((x) < 0) \
30 printf ("%ld", (long)(x)); \
31 else \
32 printf ("%lu", (unsigned long)(x));
33
34 #define hsc_const_str(x...) \
35 { \
36 const char *s = (x); \
37 printf ("\""); \
38 while (*s != '\0') \
39 { \
40 if (*s == '"' || *s == '\\') \
41 printf ("\\%c", *s); \
42 else if (*s >= 0x20 && *s <= 0x7E) \
43 printf ("%c", *s); \
44 else \
45 printf ("\\%d%s", \
46 (unsigned char) *s, \
47 s[1] >= '0' && s[1] <= '9' ? "\\&" : ""); \
48 ++s; \
49 } \
50 printf ("\""); \
51 }
52
53 #define hsc_type(t...) \
54 if ((t)(int)(t)1.4 == (t)1.4) \
55 printf ("%s%lu", \
56 (t)(-1) < (t)0 ? "Int" : "Word", \
57 (unsigned long)sizeof (t) * 8); \
58 else \
59 printf ("%s", \
60 sizeof (t) > sizeof (double) ? "LDouble" : \
61 sizeof (t) == sizeof (double) ? "Double" : \
62 "Float");
63
64 #define hsc_peek(t, f) \
65 printf ("(\\hsc_ptr -> peekByteOff hsc_ptr %ld)", (long) offsetof (t, f));
66
67 #define hsc_poke(t, f) \
68 printf ("(\\hsc_ptr -> pokeByteOff hsc_ptr %ld)", (long) offsetof (t, f));
69
70 #define hsc_ptr(t, f) \
71 printf ("(\\hsc_ptr -> hsc_ptr `plusPtr` %ld)", (long) offsetof (t, f));
72
73 #define hsc_offset(t, f) \
74 printf("(%ld)", (long) offsetof (t, f));
75
76 #define hsc_size(t...) \
77 printf("(%ld)", (long) sizeof(t));
78
79 #define hsc_enum(t, f, print_name, x) \
80 print_name; \
81 printf (" :: %s\n", #t); \
82 print_name; \
83 printf (" = %s ", #f); \
84 if ((x) < 0) \
85 printf ("(%ld)\n", (long)(x)); \
86 else \
87 printf ("%lu\n", (unsigned long)(x));
88
89 #define hsc_haskellize(x...) \
90 { \
91 const char *s = (x); \
92 int upper = 0; \
93 if (*s != '\0') \
94 { \
95 putchar (tolower (*s)); \
96 ++s; \
97 while (*s != '\0') \
98 { \
99 if (*s == '_') \
100 upper = 1; \
101 else \
102 { \
103 putchar (upper ? toupper (*s) : tolower (*s)); \
104 upper = 0; \
105 } \
106 ++s; \
107 } \
108 } \
109 }