Follow changes in GHC build system
[hsc2hs.git] / template-hsc.h
1
2 /* We need stddef to be able to use size_t. Hopefully this won't cause
3 any problems along the lines of ghc trac #2897. */
4 #include <stddef.h>
5
6 /* hsc_* are defined in the generated utils.c */
7 int hsc_printf(const char *format, ...);
8 int hsc_toupper(int c);
9 int hsc_tolower(int c);
10 int hsc_putchar(int c);
11 /* "void" should really be "FILE", but we aren't able to refer to "FILE"
12 as we don't want to include <stdio.h> here */
13 int hsc_fputs(const char *s, void *stream);
14 /* "void" should really be "FILE", but we aren't able to refer to "FILE"
15 as we don't want to include <stdio.h> here */
16 void *hsc_stdout(void);
17
18 /* For the single-argument macros we make the macros variadic (the
19 argument is x... rather than simply x) so that arguments containing
20 commas work. See trac #590. */
21
22 #ifndef offsetof
23 #define offsetof(t, f) ((size_t) &((t *)0)->f)
24 #endif
25
26 #if __NHC__
27 #define hsc_line(line, file) \
28 hsc_printf ("# %d \"%s\"\n", line, file);
29 #else
30 #define hsc_line(line, file) \
31 hsc_printf ("{-# LINE %d \"%s\" #-}\n", line, file);
32 #endif
33
34 #define hsc_const(x...) \
35 if ((x) < 0) \
36 hsc_printf ("%lld", (long long)(x)); \
37 else \
38 hsc_printf ("%llu", (unsigned long long)(x));
39
40 #define hsc_const_str(x...) \
41 { \
42 const char *s = (x); \
43 hsc_printf ("\""); \
44 while (*s != '\0') \
45 { \
46 if (*s == '"' || *s == '\\') \
47 hsc_printf ("\\%c", *s); \
48 else if (*s >= 0x20 && *s <= 0x7E) \
49 hsc_printf ("%c", *s); \
50 else \
51 hsc_printf ("\\%d%s", \
52 (unsigned char) *s, \
53 s[1] >= '0' && s[1] <= '9' ? "\\&" : ""); \
54 ++s; \
55 } \
56 hsc_printf ("\""); \
57 }
58
59 #define hsc_type(t...) \
60 if ((t)(int)(t)1.4 == (t)1.4) \
61 hsc_printf ("%s%lu", \
62 (t)(-1) < (t)0 ? "Int" : "Word", \
63 (unsigned long)sizeof (t) * 8); \
64 else \
65 hsc_printf ("%s", \
66 sizeof (t) > sizeof (double) ? "LDouble" : \
67 sizeof (t) == sizeof (double) ? "Double" : \
68 "Float");
69
70 #define hsc_peek(t, f) \
71 hsc_printf ("(\\hsc_ptr -> peekByteOff hsc_ptr %ld)", \
72 (long) offsetof (t, f));
73
74 #define hsc_poke(t, f) \
75 hsc_printf ("(\\hsc_ptr -> pokeByteOff hsc_ptr %ld)", \
76 (long) offsetof (t, f));
77
78 #define hsc_ptr(t, f) \
79 hsc_printf ("(\\hsc_ptr -> hsc_ptr `plusPtr` %ld)", \
80 (long) offsetof (t, f));
81
82 #define hsc_offset(t, f) \
83 hsc_printf("(%ld)", (long) offsetof (t, f));
84
85 #define hsc_size(t...) \
86 hsc_printf("(%ld)", (long) sizeof(t));
87
88 #define hsc_enum(t, f, print_name, x) \
89 print_name; \
90 hsc_printf (" :: %s\n", #t); \
91 print_name; \
92 hsc_printf (" = %s ", #f); \
93 if ((x) < 0) \
94 hsc_printf ("(%lld)\n", (long long)(x)); \
95 else \
96 hsc_printf ("%llu\n", (unsigned long long)(x));
97
98 #define hsc_haskellize(x...) \
99 { \
100 const char *s = (x); \
101 int upper = 0; \
102 if (*s != '\0') \
103 { \
104 hsc_putchar (hsc_tolower (*s)); \
105 ++s; \
106 while (*s != '\0') \
107 { \
108 if (*s == '_') \
109 upper = 1; \
110 else \
111 { \
112 hsc_putchar (upper ? hsc_toupper (*s) \
113 : hsc_tolower (*s)); \
114 upper = 0; \
115 } \
116 ++s; \
117 } \
118 } \
119 }