Types.hs 13.1 KB
Newer Older
1
{-# LANGUAGE Trustworthy #-}
2
3
4
5
6
{-# LANGUAGE CPP
           , NoImplicitPrelude
           , MagicHash
           , GeneralizedNewtypeDeriving
  #-}
Ian Lynagh's avatar
Ian Lynagh committed
7
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
8
9
10
#ifdef __GLASGOW_HASKELL__
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
#endif
Ian Lynagh's avatar
Ian Lynagh committed
11
12
-- XXX -fno-warn-unused-binds stops us warning about unused constructors,
-- but really we should just remove them if we don't want them
13

14
-----------------------------------------------------------------------------
15
-- |
16
17
-- Module      :  Foreign.C.Types
-- Copyright   :  (c) The FFI task force 2001
18
-- License     :  BSD-style (see the file libraries/base/LICENSE)
19
20
-- 
-- Maintainer  :  ffi@haskell.org
21
22
-- Stability   :  provisional
-- Portability :  portable
23
--
24
-- Mapping of C types to corresponding Haskell types.
25
26
27
28
--
-----------------------------------------------------------------------------

module Foreign.C.Types
Don Stewart's avatar
Don Stewart committed
29
30
31
32
33
34
35
36
37
38
        ( -- * Representations of C types
          -- $ctypes

          -- ** Integral types
          -- | These types are are represented as @newtype@s of
          -- types in "Data.Int" and "Data.Word", and are instances of
          -- 'Prelude.Eq', 'Prelude.Ord', 'Prelude.Num', 'Prelude.Read',
          -- 'Prelude.Show', 'Prelude.Enum', 'Typeable', 'Storable',
          -- 'Prelude.Bounded', 'Prelude.Real', 'Prelude.Integral' and
          -- 'Bits'.
39
40
41
42
43
44
          CChar(..),    CSChar(..),   CUChar(..)
        , CShort(..),   CUShort(..),  CInt(..),      CUInt(..)
        , CLong(..),    CULong(..)
        , CPtrdiff(..), CSize(..),    CWchar(..),    CSigAtomic(..)
        , CLLong(..),   CULLong(..)
        , CIntPtr(..),  CUIntPtr(..), CIntMax(..),   CUIntMax(..)
Don Stewart's avatar
Don Stewart committed
45
46

          -- ** Numeric types
ian@well-typed.com's avatar
ian@well-typed.com committed
47
          -- | These types are represented as @newtype@s of basic
Don Stewart's avatar
Don Stewart committed
48
49
50
          -- foreign types, and are instances of
          -- 'Prelude.Eq', 'Prelude.Ord', 'Prelude.Num', 'Prelude.Read',
          -- 'Prelude.Show', 'Prelude.Enum', 'Typeable' and 'Storable'.
51
        , CClock(..),   CTime(..),    CUSeconds(..), CSUSeconds(..)
Don Stewart's avatar
Don Stewart committed
52

53
        -- extracted from CTime, because we don't want this comment in
54
        -- the Haskell language reports:
55
56
57
58
59
60

        -- | To convert 'CTime' to 'Data.Time.UTCTime', use the following formula:
        --
        -- >  posixSecondsToUTCTime (realToFrac :: POSIXTime)
        --

Don Stewart's avatar
Don Stewart committed
61
62
63
64
65
66
67
          -- ** Floating types
          -- | These types are are represented as @newtype@s of
          -- 'Prelude.Float' and 'Prelude.Double', and are instances of
          -- 'Prelude.Eq', 'Prelude.Ord', 'Prelude.Num', 'Prelude.Read',
          -- 'Prelude.Show', 'Prelude.Enum', 'Typeable', 'Storable',
          -- 'Prelude.Real', 'Prelude.Fractional', 'Prelude.Floating',
          -- 'Prelude.RealFrac' and 'Prelude.RealFloat'.
68
        , CFloat(..),   CDouble(..)
69
70
-- GHC doesn't support CLDouble yet
#ifndef __GLASGOW_HASKELL__
71
        , CLDouble(..)
72
#endif
Don Stewart's avatar
Don Stewart committed
73
          -- ** Other types
74
75

          -- Instances of: Eq and Storable
Don Stewart's avatar
Don Stewart committed
76
77
        , CFile,        CFpos,     CJmpBuf
        ) where
78

79
#ifndef __NHC__
80

81
import Foreign.Storable
Don Stewart's avatar
Don Stewart committed
82
83
84
import Data.Bits        ( Bits(..) )
import Data.Int         ( Int8,  Int16,  Int32,  Int64  )
import Data.Word        ( Word8, Word16, Word32, Word64 )
85
import {-# SOURCE #-} Data.Typeable
Simon Marlow's avatar
Simon Marlow committed
86
87
  -- loop: Data.Typeable -> Data.List -> Data.Char -> GHC.Unicode
  --            -> Foreign.C.Type
88
89
90
91
92
93
94
95
96

#ifdef __GLASGOW_HASKELL__
import GHC.Base
import GHC.Float
import GHC.Enum
import GHC.Real
import GHC.Show
import GHC.Read
import GHC.Num
ross's avatar
ross committed
97
#else
Don Stewart's avatar
Don Stewart committed
98
import Control.Monad    ( liftM )
99
100
101
#endif

#ifdef __HUGS__
Don Stewart's avatar
Don Stewart committed
102
import Hugs.Ptr         ( castPtr )
103
104
#endif

105
#include "HsBaseConfig.h"
106
107
#include "CTypes.h"

ross's avatar
ross committed
108
-- | Haskell type representing the C @char@ type.
109
INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
ross's avatar
ross committed
110
-- | Haskell type representing the C @signed char@ type.
111
INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
ross's avatar
ross committed
112
-- | Haskell type representing the C @unsigned char@ type.
113
114
INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)

ross's avatar
ross committed
115
-- | Haskell type representing the C @short@ type.
116
INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
ross's avatar
ross committed
117
-- | Haskell type representing the C @unsigned short@ type.
118
119
INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)

ross's avatar
ross committed
120
-- | Haskell type representing the C @int@ type.
121
INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
ross's avatar
ross committed
122
-- | Haskell type representing the C @unsigned int@ type.
123
124
INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)

ross's avatar
ross committed
125
-- | Haskell type representing the C @long@ type.
126
INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
ross's avatar
ross committed
127
-- | Haskell type representing the C @unsigned long@ type.
128
129
INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)

ross's avatar
ross committed
130
-- | Haskell type representing the C @long long@ type.
131
INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
ross's avatar
ross committed
132
-- | Haskell type representing the C @unsigned long long@ type.
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)

{-# RULES
"fromIntegral/a->CChar"   fromIntegral = \x -> CChar   (fromIntegral x)
"fromIntegral/a->CSChar"  fromIntegral = \x -> CSChar  (fromIntegral x)
"fromIntegral/a->CUChar"  fromIntegral = \x -> CUChar  (fromIntegral x)
"fromIntegral/a->CShort"  fromIntegral = \x -> CShort  (fromIntegral x)
"fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
"fromIntegral/a->CInt"    fromIntegral = \x -> CInt    (fromIntegral x)
"fromIntegral/a->CUInt"   fromIntegral = \x -> CUInt   (fromIntegral x)
"fromIntegral/a->CLong"   fromIntegral = \x -> CLong   (fromIntegral x)
"fromIntegral/a->CULong"  fromIntegral = \x -> CULong  (fromIntegral x)
"fromIntegral/a->CLLong"  fromIntegral = \x -> CLLong  (fromIntegral x)
"fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)

"fromIntegral/CChar->a"   fromIntegral = \(CChar   x) -> fromIntegral x
"fromIntegral/CSChar->a"  fromIntegral = \(CSChar  x) -> fromIntegral x
"fromIntegral/CUChar->a"  fromIntegral = \(CUChar  x) -> fromIntegral x
"fromIntegral/CShort->a"  fromIntegral = \(CShort  x) -> fromIntegral x
"fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
"fromIntegral/CInt->a"    fromIntegral = \(CInt    x) -> fromIntegral x
"fromIntegral/CUInt->a"   fromIntegral = \(CUInt   x) -> fromIntegral x
"fromIntegral/CLong->a"   fromIntegral = \(CLong   x) -> fromIntegral x
"fromIntegral/CULong->a"  fromIntegral = \(CULong  x) -> fromIntegral x
"fromIntegral/CLLong->a"  fromIntegral = \(CLLong  x) -> fromIntegral x
"fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
 #-}

ross's avatar
ross committed
161
-- | Haskell type representing the C @float@ type.
162
FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
ross's avatar
ross committed
163
-- | Haskell type representing the C @double@ type.
164
FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
165
166
-- GHC doesn't support CLDouble yet
#ifndef __GLASGOW_HASKELL__
167
-- HACK: Currently no long double in the FFI, so we simply re-use double
ross's avatar
ross committed
168
-- | Haskell type representing the C @long double@ type.
169
FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
170
#endif
171

172
173
174
175
176
177
178
179
{-# RULES
"realToFrac/a->CFloat"    realToFrac = \x -> CFloat   (realToFrac x)
"realToFrac/a->CDouble"   realToFrac = \x -> CDouble  (realToFrac x)

"realToFrac/CFloat->a"    realToFrac = \(CFloat   x) -> realToFrac x
"realToFrac/CDouble->a"   realToFrac = \(CDouble  x) -> realToFrac x
 #-}

180
181
182
183
-- GHC doesn't support CLDouble yet
-- "realToFrac/a->CLDouble"  realToFrac = \x -> CLDouble (realToFrac x)
-- "realToFrac/CLDouble->a"  realToFrac = \(CLDouble x) -> realToFrac x

ross's avatar
ross committed
184
-- | Haskell type representing the C @ptrdiff_t@ type.
185
INTEGRAL_TYPE(CPtrdiff,tyConCPtrdiff,"CPtrdiff",HTYPE_PTRDIFF_T)
ross's avatar
ross committed
186
-- | Haskell type representing the C @size_t@ type.
187
INTEGRAL_TYPE(CSize,tyConCSize,"CSize",HTYPE_SIZE_T)
ross's avatar
ross committed
188
-- | Haskell type representing the C @wchar_t@ type.
189
INTEGRAL_TYPE(CWchar,tyConCWchar,"CWchar",HTYPE_WCHAR_T)
ross's avatar
ross committed
190
-- | Haskell type representing the C @sig_atomic_t@ type.
191
192
193
194
195
196
197
198
199
200
201
202
203
204
INTEGRAL_TYPE(CSigAtomic,tyConCSigAtomic,"CSigAtomic",HTYPE_SIG_ATOMIC_T)

{-# RULES
"fromIntegral/a->CPtrdiff"   fromIntegral = \x -> CPtrdiff   (fromIntegral x)
"fromIntegral/a->CSize"      fromIntegral = \x -> CSize      (fromIntegral x)
"fromIntegral/a->CWchar"     fromIntegral = \x -> CWchar     (fromIntegral x)
"fromIntegral/a->CSigAtomic" fromIntegral = \x -> CSigAtomic (fromIntegral x)

"fromIntegral/CPtrdiff->a"   fromIntegral = \(CPtrdiff   x) -> fromIntegral x
"fromIntegral/CSize->a"      fromIntegral = \(CSize      x) -> fromIntegral x
"fromIntegral/CWchar->a"     fromIntegral = \(CWchar     x) -> fromIntegral x
"fromIntegral/CSigAtomic->a" fromIntegral = \(CSigAtomic x) -> fromIntegral x
 #-}

ross's avatar
ross committed
205
-- | Haskell type representing the C @clock_t@ type.
206
ARITHMETIC_TYPE(CClock,tyConCClock,"CClock",HTYPE_CLOCK_T)
ross's avatar
ross committed
207
-- | Haskell type representing the C @time_t@ type.
208
ARITHMETIC_TYPE(CTime,tyConCTime,"CTime",HTYPE_TIME_T)
209
210
211
212
-- | Haskell type representing the C @useconds_t@ type.
ARITHMETIC_TYPE(CUSeconds,tyConCUSeconds,"CUSeconds",HTYPE_USECONDS_T)
-- | Haskell type representing the C @suseconds_t@ type.
ARITHMETIC_TYPE(CSUSeconds,tyConCSUSeconds,"CSUSeconds",HTYPE_SUSECONDS_T)
213
214

-- FIXME: Implement and provide instances for Eq and Storable
ross's avatar
ross committed
215
-- | Haskell type representing the C @FILE@ type.
216
data CFile = CFile
ross's avatar
ross committed
217
-- | Haskell type representing the C @fpos_t@ type.
218
data CFpos = CFpos
ross's avatar
ross committed
219
-- | Haskell type representing the C @jmp_buf@ type.
220
221
data CJmpBuf = CJmpBuf

222
223
224
225
226
227
228
229
230
231
232
233
INTEGRAL_TYPE(CIntPtr,tyConCIntPtr,"CIntPtr",HTYPE_INTPTR_T)
INTEGRAL_TYPE(CUIntPtr,tyConCUIntPtr,"CUIntPtr",HTYPE_UINTPTR_T)
INTEGRAL_TYPE(CIntMax,tyConCIntMax,"CIntMax",HTYPE_INTMAX_T)
INTEGRAL_TYPE(CUIntMax,tyConCUIntMax,"CUIntMax",HTYPE_UINTMAX_T)

{-# RULES
"fromIntegral/a->CIntPtr"  fromIntegral = \x -> CIntPtr  (fromIntegral x)
"fromIntegral/a->CUIntPtr" fromIntegral = \x -> CUIntPtr (fromIntegral x)
"fromIntegral/a->CIntMax"  fromIntegral = \x -> CIntMax  (fromIntegral x)
"fromIntegral/a->CUIntMax" fromIntegral = \x -> CUIntMax (fromIntegral x)
 #-}

234
-- C99 types which are still missing include:
235
-- wint_t, wctrans_t, wctype_t
236

ross's avatar
ross committed
237
238
239
240
241
242
243
244
{- $ctypes

These types are needed to accurately represent C function prototypes,
in order to access C library interfaces in Haskell.  The Haskell system
is not required to represent those types exactly as C does, but the
following guarantees are provided concerning a Haskell type @CT@
representing a C type @t@:

ross's avatar
ross committed
245
246
247
248
* If a C function prototype has @t@ as an argument or result type, the
  use of @CT@ in the corresponding position in a foreign declaration
  permits the Haskell program to access the full range of values encoded
  by the C type; and conversely, any Haskell value for @CT@ has a valid
ross's avatar
ross committed
249
250
  representation in C.

ross's avatar
ross committed
251
* @'sizeOf' ('Prelude.undefined' :: CT)@ will yield the same value as
ross's avatar
ross committed
252
253
  @sizeof (t)@ in C.

ross's avatar
ross committed
254
255
* @'alignment' ('Prelude.undefined' :: CT)@ matches the alignment
  constraint enforced by the C implementation for @t@.
ross's avatar
ross committed
256

ross's avatar
ross committed
257
258
* The members 'peek' and 'poke' of the 'Storable' class map all values
  of @CT@ to the corresponding value of @t@ and vice versa.
ross's avatar
ross committed
259

ross's avatar
ross committed
260
261
262
* When an instance of 'Prelude.Bounded' is defined for @CT@, the values
  of 'Prelude.minBound' and 'Prelude.maxBound' coincide with @t_MIN@
  and @t_MAX@ in C.
ross's avatar
ross committed
263

ross's avatar
ross committed
264
265
266
* When an instance of 'Prelude.Eq' or 'Prelude.Ord' is defined for @CT@,
  the predicates defined by the type class implement the same relation
  as the corresponding predicate in C on @t@.
ross's avatar
ross committed
267

ross's avatar
ross committed
268
269
270
271
272
* When an instance of 'Prelude.Num', 'Prelude.Read', 'Prelude.Integral',
  'Prelude.Fractional', 'Prelude.Floating', 'Prelude.RealFrac', or
  'Prelude.RealFloat' is defined for @CT@, the arithmetic operations
  defined by the type class implement the same function as the
  corresponding arithmetic operations (if available) in C on @t@.
ross's avatar
ross committed
273
274
275
276
277
278
279

* When an instance of 'Bits' is defined for @CT@, the bitwise operation
  defined by the type class implement the same function as the
  corresponding bitwise operation in C on @t@.

-}

Don Stewart's avatar
Don Stewart committed
280
#else   /* __NHC__ */
281
282

import NHC.FFI
283
284
285
286
287
288
289
290
  ( CChar(..),    CSChar(..),   CUChar(..)
  , CShort(..),   CUShort(..),  CInt(..),      CUInt(..)
  , CLong(..),    CULong(..),   CLLong(..),    CULLong(..)
  , CPtrdiff(..), CSize(..),    CWchar(..),    CSigAtomic(..)
  , CClock(..),   CTime(..),    CUSeconds(..), CSUSeconds(..)
  , CFloat(..),   CDouble(..),  CLDouble(..)
  , CIntPtr(..),  CUIntPtr(..), CIntMax(..),   CUIntMax(..)
  , CFile,        CFpos,        CJmpBuf
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  , Storable(..)
  )
import Data.Bits
import NHC.SizedTypes

#define INSTANCE_BITS(T) \
instance Bits T where { \
  (T x) .&.     (T y)   = T (x .&.   y) ; \
  (T x) .|.     (T y)   = T (x .|.   y) ; \
  (T x) `xor`   (T y)   = T (x `xor` y) ; \
  complement    (T x)   = T (complement x) ; \
  shift         (T x) n = T (shift x n) ; \
  rotate        (T x) n = T (rotate x n) ; \
  bit                 n = T (bit n) ; \
  setBit        (T x) n = T (setBit x n) ; \
  clearBit      (T x) n = T (clearBit x n) ; \
  complementBit (T x) n = T (complementBit x n) ; \
  testBit       (T x) n = testBit x n ; \
  bitSize       (T x)   = bitSize x ; \
tibbe's avatar
tibbe committed
310
311
  isSigned      (T x)   = isSigned x ; \
  popCount      (T x)   = popCount x }
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327

INSTANCE_BITS(CChar)
INSTANCE_BITS(CSChar)
INSTANCE_BITS(CUChar)
INSTANCE_BITS(CShort)
INSTANCE_BITS(CUShort)
INSTANCE_BITS(CInt)
INSTANCE_BITS(CUInt)
INSTANCE_BITS(CLong)
INSTANCE_BITS(CULong)
INSTANCE_BITS(CLLong)
INSTANCE_BITS(CULLong)
INSTANCE_BITS(CPtrdiff)
INSTANCE_BITS(CWchar)
INSTANCE_BITS(CSigAtomic)
INSTANCE_BITS(CSize)
328
329
330
331
INSTANCE_BITS(CIntPtr)
INSTANCE_BITS(CUIntPtr)
INSTANCE_BITS(CIntMax)
INSTANCE_BITS(CUIntMax)
332

333
#endif
dterei's avatar
dterei committed
334