ghc.mk: Use the same conditional install logic from unlit
[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 #define hsc_column(column)
30 #else
31 #define hsc_line(line, file) \
32 hsc_printf ("{-# LINE %d \"%s\" #-}\n", line, file);
33 #define hsc_column(column) \
34 hsc_printf ("{-# COLUMN %d #-}", column);
35 #endif
36
37 #define hsc_const(x...) \
38 if ((x) < 0) \
39 hsc_printf ("%lld", (long long)(x)); \
40 else \
41 hsc_printf ("%llu", (unsigned long long)(x));
42
43 #define hsc_const_str(x...) \
44 { \
45 const char *s = (x); \
46 hsc_printf ("\""); \
47 while (*s != '\0') \
48 { \
49 if (*s == '"' || *s == '\\') \
50 hsc_printf ("\\%c", *s); \
51 else if (*s >= 0x20 && *s <= 0x7E) \
52 hsc_printf ("%c", *s); \
53 else \
54 hsc_printf ("\\%d%s", \
55 (unsigned char) *s, \
56 s[1] >= '0' && s[1] <= '9' ? "\\&" : ""); \
57 ++s; \
58 } \
59 hsc_printf ("\""); \
60 }
61
62 #define hsc_type(t...) \
63 if ((t)(int)(t)1.4 == (t)1.4) \
64 hsc_printf ("%s%lu", \
65 (t)(-1) < (t)0 ? "Int" : "Word", \
66 (unsigned long)sizeof (t) * 8); \
67 else \
68 hsc_printf ("%s", \
69 sizeof (t) > sizeof (double) ? "LDouble" : \
70 sizeof (t) == sizeof (double) ? "Double" : \
71 "Float");
72
73 #define hsc_peek(t, f) \
74 hsc_printf ("(\\hsc_ptr -> peekByteOff hsc_ptr %ld)", \
75 (long) offsetof (t, f));
76
77 #define hsc_poke(t, f) \
78 hsc_printf ("(\\hsc_ptr -> pokeByteOff hsc_ptr %ld)", \
79 (long) offsetof (t, f));
80
81 #define hsc_ptr(t, f) \
82 hsc_printf ("(\\hsc_ptr -> hsc_ptr `plusPtr` %ld)", \
83 (long) offsetof (t, f));
84
85 #define hsc_offset(t, f) \
86 hsc_printf("(%ld)", (long) offsetof (t, f));
87
88 #define hsc_size(t...) \
89 hsc_printf("(%ld)", (long) sizeof(t));
90
91 #define hsc_alignment(x...) \
92 do { \
93 struct __anon_x__ { \
94 char a; \
95 x b; \
96 }; \
97 hsc_printf("%lu", (unsigned long)offsetof(struct __anon_x__, b)); \
98 } while (0)
99
100 #define hsc_enum(t, f, print_name, x) \
101 print_name; \
102 hsc_printf (" :: %s\n", #t); \
103 print_name; \
104 hsc_printf (" = %s ", #f); \
105 if ((x) < 0) \
106 hsc_printf ("(%lld)\n", (long long)(x)); \
107 else \
108 hsc_printf ("%llu\n", (unsigned long long)(x));
109
110 #define hsc_haskellize(x...) \
111 { \
112 const char *s = (x); \
113 int upper = 0; \
114 if (*s != '\0') \
115 { \
116 hsc_putchar (hsc_tolower (*s)); \
117 ++s; \
118 while (*s != '\0') \
119 { \
120 if (*s == '_') \
121 upper = 1; \
122 else \
123 { \
124 hsc_putchar (upper ? hsc_toupper (*s) \
125 : hsc_tolower (*s)); \
126 upper = 0; \
127 } \
128 ++s; \
129 } \
130 } \
131 }