[project @ 2001-02-05 11:49:20 by chak]
[ghc-hetmet.git] / ghc / lib / std / cbits / CTypes.h
1 /* -----------------------------------------------------------------------------
2  * $Id: CTypes.h,v 1.2 2001/02/05 11:49:20 chak Exp $
3  *
4  * Dirty CPP hackery for CTypes/CTypesISO
5  *
6  * (c) The FFI task force, 2000
7  * -------------------------------------------------------------------------- */
8
9 #include "MachDeps.h"
10
11 /* As long as there is no automatic derivation of classes for newtypes we resort
12    to extremely dirty cpp-hackery.   :-P   Some care has to be taken when the
13    macros below are modified, otherwise the layout rule will bite you. */
14
15 /* A hacked version for GHC follows the Haskell 98 version... */
16 #ifndef __GLASGOW_HASKELL__
17
18 #define NUMERIC_TYPE(T,C,S,B) \
19 newtype T = T B deriving (Eq, Ord) ; \
20 INSTANCE_NUM(T) ; \
21 INSTANCE_READ(T) ; \
22 INSTANCE_SHOW(T) ; \
23 INSTANCE_ENUM(T) ; \
24 INSTANCE_TYPEABLE(T,C,S) ;
25
26 #define INTEGRAL_TYPE(T,C,S,B) \
27 NUMERIC_TYPE(T,C,S,B) ; \
28 INSTANCE_BOUNDED(T) ; \
29 INSTANCE_REAL(T) ; \
30 INSTANCE_INTEGRAL(T) ; \
31 INSTANCE_BITS(T)
32
33 #define FLOATING_TYPE(T,C,S,B) \
34 NUMERIC_TYPE(T,C,S,B) ; \
35 INSTANCE_REAL(T) ; \
36 INSTANCE_FRACTIONAL(T) ; \
37 INSTANCE_FLOATING(T) ; \
38 INSTANCE_REALFRAC(T) ; \
39 INSTANCE_REALFLOAT(T)
40
41 #define INSTANCE_READ(T) \
42 instance Read T where { \
43    readsPrec p s = fakeMap (\(x, t) -> (T x, t)) (readsPrec p s) }
44
45 #define INSTANCE_SHOW(T) \
46 instance Show T where { \
47    showsPrec p (T x) = showsPrec p x }
48
49 #define INSTANCE_NUM(T) \
50 instance Num T where { \
51    (T i) + (T j) = T (i + j) ; \
52    (T i) - (T j) = T (i - j) ; \
53    (T i) * (T j) = T (i * j) ; \
54    negate  (T i) = T (negate i) ; \
55    abs     (T i) = T (abs    i) ; \
56    signum  (T i) = T (signum i) ; \
57    fromInteger x = T (fromInteger x) }
58
59 #define INSTANCE_TYPEABLE(T,C,S) \
60 C :: TyCon ; \
61 C = mkTyCon S ; \
62 instance Typeable T where { \
63   typeOf _ = mkAppTy C [] }
64
65 #define INSTANCE_BOUNDED(T) \
66 instance Bounded T where { \
67    minBound = T minBound ; \
68    maxBound = T maxBound }
69
70 #define INSTANCE_ENUM(T) \
71 instance Enum T where { \
72    succ           (T i)             = T (succ i) ; \
73    pred           (T i)             = T (pred i) ; \
74    toEnum               x           = T (toEnum x) ; \
75    fromEnum       (T i)             = fromEnum i ; \
76    enumFrom       (T i)             = fakeMap T (enumFrom i) ; \
77    enumFromThen   (T i) (T j)       = fakeMap T (enumFromThen i j) ; \
78    enumFromTo     (T i) (T j)       = fakeMap T (enumFromTo i j) ; \
79    enumFromThenTo (T i) (T j) (T k) = fakeMap T (enumFromThenTo i j k) }
80
81 #define INSTANCE_REAL(T) \
82 instance Real T where { \
83    toRational (T i) = toRational i }
84
85 #define INSTANCE_INTEGRAL(T) \
86 instance Integral T where { \
87    (T i) `quot`    (T j) = T (i `quot` j) ; \
88    (T i) `rem`     (T j) = T (i `rem`  j) ; \
89    (T i) `div`     (T j) = T (i `div`  j) ; \
90    (T i) `mod`     (T j) = T (i `mod`  j) ; \
91    (T i) `quotRem` (T j) = let (q,r) = i `quotRem` j in (T q, T r) ; \
92    (T i) `divMod`  (T j) = let (d,m) = i `divMod`  j in (T d, T m) ; \
93    toInteger (T i)       = toInteger i ; \
94    toInt     (T i)       = toInt     i }
95
96 #define INSTANCE_BITS(T) \
97 instance Bits T where { \
98   (T x) .&.     (T y)   = T (x .&.   y) ; \
99   (T x) .|.     (T y)   = T (x .|.   y) ; \
100   (T x) `xor`   (T y)   = T (x `xor` y) ; \
101   complement    (T x)   = T (complement x) ; \
102   shift         (T x) n = T (shift x n) ; \
103   rotate        (T x) n = T (rotate x n) ; \
104   bit                 n = T (bit n) ; \
105   setBit        (T x) n = T (setBit x n) ; \
106   clearBit      (T x) n = T (clearBit x n) ; \
107   complementBit (T x) n = T (complementBit x n) ; \
108   testBit       (T x) n = testBit x n ; \
109   bitSize       (T x)   = bitSize x ; \
110   isSigned      (T x)   = isSigned x }
111
112 #define INSTANCE_FRACTIONAL(T) \
113 instance Fractional T where { \
114    (T x) / (T y)  = T (x / y) ; \
115    recip   (T x)  = T (recip x) ; \
116    fromRational r = T (fromRational r) }
117
118 #define INSTANCE_FLOATING(T) \
119 instance Floating T where { \
120    pi                    = pi ; \
121    exp   (T x)           = T (exp   x) ; \
122    log   (T x)           = T (log   x) ; \
123    sqrt  (T x)           = T (sqrt  x) ; \
124    (T x) **        (T y) = T (x ** y) ; \
125    (T x) `logBase` (T y) = T (x `logBase` y) ; \
126    sin   (T x)           = T (sin   x) ; \
127    cos   (T x)           = T (cos   x) ; \
128    tan   (T x)           = T (tan   x) ; \
129    asin  (T x)           = T (asin  x) ; \
130    acos  (T x)           = T (acos  x) ; \
131    atan  (T x)           = T (atan  x) ; \
132    sinh  (T x)           = T (sinh  x) ; \
133    cosh  (T x)           = T (cosh  x) ; \
134    tanh  (T x)           = T (tanh  x) ; \
135    asinh (T x)           = T (asinh x) ; \
136    acosh (T x)           = T (acosh x) ; \
137    atanh (T x)           = T (atanh x) }
138
139 #define INSTANCE_REALFRAC(T) \
140 instance RealFrac T where { \
141    properFraction (T x) = let (m,y) = properFraction x in (m, T y) ; \
142    truncate (T x) = truncate x ; \
143    round    (T x) = round x ; \
144    ceiling  (T x) = ceiling x ; \
145    floor    (T x) = floor x }
146
147 #define INSTANCE_REALFLOAT(T) \
148 instance RealFloat T where { \
149    floatRadix     (T x) = floatRadix x ; \
150    floatDigits    (T x) = floatDigits x ; \
151    floatRange     (T x) = floatRange x ; \
152    decodeFloat    (T x) = decodeFloat x ; \
153    encodeFloat m n      = T (encodeFloat m n) ; \
154    exponent       (T x) = exponent x ; \
155    significand    (T x) = T (significand  x) ; \
156    scaleFloat n   (T x) = T (scaleFloat n x) ; \
157    isNaN          (T x) = isNaN x ; \
158    isInfinite     (T x) = isInfinite x ; \
159    isDenormalized (T x) = isDenormalized x ; \
160    isNegativeZero (T x) = isNegativeZero x ; \
161    isIEEE         (T x) = isIEEE x ; \
162    (T x) `atan2`  (T y) = T (x `atan2` y) }
163
164 #else /* __GLASGOW_HASKELL__ */
165
166 /* On GHC, we just cast the type of each method to the underlying
167  * type.  This means that GHC only needs to generate the dictionary
168  * for each instance, rather than a new function for each method (the
169  * simplifier currently isn't clever enough to reduce a method that
170  * simply deconstructs a newtype and calls the underlying method into
171  * an indirection to the underlying method, so that's what we're doing
172  * here). 
173  */
174
175 #define NUMERIC_TYPE(T,C,S,B) \
176 newtype T = T B ; \
177 INSTANCE_EQ(T,B) ; \
178 INSTANCE_ORD(T,B) ; \
179 INSTANCE_NUM(T,B) ; \
180 INSTANCE_READ(T,B) ; \
181 INSTANCE_SHOW(T,B) ; \
182 INSTANCE_ENUM(T,B) 
183
184 #define INTEGRAL_TYPE(T,C,S,B) \
185 NUMERIC_TYPE(T,C,S,B) ;  \
186 INSTANCE_BOUNDED(T,B) ; \
187 INSTANCE_REAL(T,B) ; \
188 INSTANCE_INTEGRAL(T,B) ; \
189 INSTANCE_BITS(T,B)
190
191 #define FLOATING_TYPE(T,C,S,B) \
192 NUMERIC_TYPE(T,C,S,B) ; \
193 INSTANCE_REAL(T,B) ; \
194 INSTANCE_FRACTIONAL(T,B) ; \
195 INSTANCE_FLOATING(T,B) ; \
196 INSTANCE_REALFRAC(T) ; \
197 INSTANCE_REALFLOAT(T,B)
198
199 #define INSTANCE_EQ(T,B) \
200 instance Eq T where { \
201    (==)         = unsafeCoerce# ((==) :: B -> B -> Bool); \
202    (/=)         = unsafeCoerce# ((/=) :: B -> B -> Bool); }
203
204 #define INSTANCE_ORD(T,B) \
205 instance Ord T where { \
206    compare              = unsafeCoerce# (compare :: B -> B -> Ordering); \
207    (<)                  = unsafeCoerce# ((<) :: B -> B -> Bool); \
208    (<=)                 = unsafeCoerce# ((<=) :: B -> B -> Bool); \
209    (>=)                 = unsafeCoerce# ((>=) :: B -> B -> Bool); \
210    (>)                  = unsafeCoerce# ((>) :: B -> B -> Bool); \
211    max                  = unsafeCoerce# (max :: B -> B -> B); \
212    min                  = unsafeCoerce# (min :: B -> B -> B); }
213
214 #define INSTANCE_READ(T,B) \
215 instance Read T where { \
216    readsPrec            = unsafeCoerce# (readsPrec :: Int -> ReadS B); \
217    readList             = unsafeCoerce# (readList  :: ReadS [B]); }
218
219 #define INSTANCE_SHOW(T,B) \
220 instance Show T where { \
221    showsPrec            = unsafeCoerce# (showsPrec :: Int -> B -> ShowS); \
222    show                 = unsafeCoerce# (show :: B -> String); \
223    showList             = unsafeCoerce# (showList :: [B] -> ShowS); }
224
225 #define INSTANCE_NUM(T,B) \
226 instance Num T where { \
227    (+)                  = unsafeCoerce# ((+) :: B -> B -> B); \
228    (-)                  = unsafeCoerce# ((-) :: B -> B -> B); \
229    (*)                  = unsafeCoerce# ((*) :: B -> B -> B); \
230    negate               = unsafeCoerce# (negate :: B -> B); \
231    abs                  = unsafeCoerce# (abs :: B -> B); \
232    signum               = unsafeCoerce# (signum :: B -> B); \
233    fromInteger          = unsafeCoerce# (fromInteger :: Integer -> B); \
234    fromInt              = unsafeCoerce# (fromInt :: Int -> B) }
235
236 #define INSTANCE_BOUNDED(T,B) \
237 instance Bounded T where { \
238    minBound = T minBound ; \
239    maxBound = T maxBound }
240
241 #define INSTANCE_ENUM(T,B) \
242 instance Enum T where { \
243     succ                = unsafeCoerce# (succ :: B -> B); \
244     pred                = unsafeCoerce# (pred :: B -> B); \
245     toEnum              = unsafeCoerce# (toEnum :: Int -> B); \
246     fromEnum            = unsafeCoerce# (fromEnum :: B -> Int); \
247     enumFrom            = unsafeCoerce# (enumFrom :: B -> [B]); \
248     enumFromThen        = unsafeCoerce# (enumFromThen :: B -> B -> [B]); \
249     enumFromTo          = unsafeCoerce# (enumFromTo :: B -> B -> [B]); \
250     enumFromThenTo      = unsafeCoerce# (enumFromThenTo :: B -> B -> B -> [B]);}
251
252 #define INSTANCE_REAL(T,B) \
253 instance Real T where { \
254    toRational = unsafeCoerce# (toRational :: B -> Rational) }
255
256 #define INSTANCE_INTEGRAL(T,B) \
257 instance Integral T where { \
258     quot                = unsafeCoerce# (quot:: B -> B -> B); \
259     rem                 = unsafeCoerce# (rem:: B -> B -> B); \
260     div                 = unsafeCoerce# (div:: B -> B -> B); \
261     mod                 = unsafeCoerce# (mod:: B -> B -> B); \
262     quotRem             = unsafeCoerce# (quotRem:: B -> B -> (B,B)); \
263     divMod              = unsafeCoerce# (divMod:: B -> B -> (B,B)); \
264     toInteger           = unsafeCoerce# (toInteger:: B -> Integer); \
265     toInt               = unsafeCoerce# (toInt:: B -> Int); }
266
267 #define INSTANCE_BITS(T,B) \
268 instance Bits T where { \
269   (.&.)                 = unsafeCoerce# ((.&.) :: B -> B -> B); \
270   (.|.)                 = unsafeCoerce# ((.|.) :: B -> B -> B); \
271   xor                   = unsafeCoerce# (xor:: B -> B -> B); \
272   complement            = unsafeCoerce# (complement:: B -> B); \
273   shift                 = unsafeCoerce# (shift:: B -> Int -> B); \
274   rotate                = unsafeCoerce# (rotate:: B -> Int -> B); \
275   bit                   = unsafeCoerce# (bit:: Int -> B); \
276   setBit                = unsafeCoerce# (setBit:: B -> Int -> B); \
277   clearBit              = unsafeCoerce# (clearBit:: B -> Int -> B); \
278   complementBit         = unsafeCoerce# (complementBit:: B -> Int -> B); \
279   testBit               = unsafeCoerce# (testBit:: B -> Int -> Bool); \
280   bitSize               = unsafeCoerce# (bitSize:: B -> Int); \
281   isSigned              = unsafeCoerce# (isSigned:: B -> Bool); }
282
283 #define INSTANCE_FRACTIONAL(T,B) \
284 instance Fractional T where { \
285     (/)                 = unsafeCoerce# ((/) :: B -> B -> B); \
286     recip               = unsafeCoerce# (recip :: B -> B); \
287     fromRational        = unsafeCoerce# (fromRational :: Rational -> B); }
288
289 #define INSTANCE_FLOATING(T,B) \
290 instance Floating T where { \
291     pi                  = unsafeCoerce# (pi :: B); \
292     exp                 = unsafeCoerce# (exp :: B -> B); \
293     log                 = unsafeCoerce# (log :: B -> B); \
294     sqrt                = unsafeCoerce# (sqrt :: B -> B); \
295     (**)                = unsafeCoerce# ((**) :: B -> B -> B); \
296     logBase             = unsafeCoerce# (logBase :: B -> B -> B); \
297     sin                 = unsafeCoerce# (sin :: B -> B); \
298     cos                 = unsafeCoerce# (cos :: B -> B); \
299     tan                 = unsafeCoerce# (tan :: B -> B); \
300     asin                = unsafeCoerce# (asin :: B -> B); \
301     acos                = unsafeCoerce# (acos :: B -> B); \
302     atan                = unsafeCoerce# (atan :: B -> B); \
303     sinh                = unsafeCoerce# (sinh :: B -> B); \
304     cosh                = unsafeCoerce# (cosh :: B -> B); \
305     tanh                = unsafeCoerce# (tanh :: B -> B); \
306     asinh               = unsafeCoerce# (asinh :: B -> B); \
307     acosh               = unsafeCoerce# (acosh :: B -> B); \
308     atanh               = unsafeCoerce# (atanh :: B -> B); }
309
310 /* The coerce trick doesn't work for RealFrac, these methods are
311  * polymorphic and overloaded.
312  */
313 #define INSTANCE_REALFRAC(T) \
314 instance RealFrac T where { \
315    properFraction (T x) = let (m,y) = properFraction x in (m, T y) ; \
316    truncate (T x) = truncate x ; \
317    round    (T x) = round x ; \
318    ceiling  (T x) = ceiling x ; \
319    floor    (T x) = floor x }
320
321 #define INSTANCE_REALFLOAT(T,B) \
322 instance RealFloat T where { \
323     floatRadix          = unsafeCoerce# (floatRadix :: B -> Integer); \
324     floatDigits         = unsafeCoerce# (floatDigits :: B -> Int); \
325     floatRange          = unsafeCoerce# (floatRange :: B -> (Int,Int)); \
326     decodeFloat         = unsafeCoerce# (decodeFloat :: B -> (Integer,Int)); \
327     encodeFloat         = unsafeCoerce# (encodeFloat :: Integer -> Int -> B); \
328     exponent            = unsafeCoerce# (exponent :: B -> Int); \
329     significand         = unsafeCoerce# (significand :: B -> B); \
330     scaleFloat          = unsafeCoerce# (scaleFloat :: Int -> B -> B); \
331     isNaN               = unsafeCoerce# (isNaN :: B -> Bool); \
332     isInfinite          = unsafeCoerce# (isInfinite :: B -> Bool); \
333     isDenormalized      = unsafeCoerce# (isDenormalized :: B -> Bool); \
334     isNegativeZero      = unsafeCoerce# (isNegativeZero :: B -> Bool); \
335     isIEEE              = unsafeCoerce# (isIEEE :: B -> Bool); \
336     atan2               = unsafeCoerce# (atan2 :: B -> B -> B); }
337
338 #endif /* __GLASGOW_HASKELL__ */