Typeable overhaul (see #5275)
[ghc.git] / libraries / base / Data / Typeable / Internal.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Data.Typeable.Internal
4 -- Copyright : (c) The University of Glasgow, CWI 2001--2011
5 -- License : BSD-style (see the file libraries/base/LICENSE)
6 --
7 -- The representations of the types TyCon and TypeRep, and the
8 -- function mkTyCon which is used by derived instances of Typeable to
9 -- construct a TyCon.
10 --
11 -----------------------------------------------------------------------------
12
13 {-# LANGUAGE NoImplicitPrelude,
14 MagicHash #-}
15 module Data.Typeable.Internal (
16 TypeRep(..),
17 TyCon(..),
18 mkTyCon
19 ) where
20
21 import GHC.Base
22 import GHC.Word
23 import GHC.Fingerprint.Type
24
25 -- | A concrete representation of a (monomorphic) type. 'TypeRep'
26 -- supports reasonably efficient equality.
27 data TypeRep = TypeRep {-# UNPACK #-} !Fingerprint TyCon [TypeRep]
28
29 -- Compare keys for equality
30 instance Eq TypeRep where
31 (TypeRep k1 _ _) == (TypeRep k2 _ _) = k1 == k2
32
33 -- | An abstract representation of a type constructor. 'TyCon' objects can
34 -- be built using 'mkTyCon'.
35 data TyCon = TyCon {
36 tyConHash :: {-# UNPACK #-} !Fingerprint,
37 tyConPackage :: String,
38 tyConModule :: String,
39 tyConName :: String
40 }
41
42 instance Eq TyCon where
43 (TyCon t1 _ _ _) == (TyCon t2 _ _ _) = t1 == t2
44
45 #include "MachDeps.h"
46
47 -- mkTyCon is an internal function to make it easier for GHC to
48 -- generate derived instances. GHC precomputes the MD5 hash for the
49 -- TyCon and passes it as two separate 64-bit values to mkTyCon. The
50 -- TyCon for a derived Typeable instance will end up being statically
51 -- allocated.
52
53 #if WORD_SIZE_IN_BITS < 64
54 mkTyCon :: Word64# -> Word64# -> String -> String -> String -> TyCon
55 #else
56 mkTyCon :: Word# -> Word# -> String -> String -> String -> TyCon
57 #endif
58 mkTyCon high# low# pkg modl name
59 = TyCon (Fingerprint (W64# high#) (W64# low#)) pkg modl name