2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[TysPrim]{Wired-in knowledge about primitive types}
8 alphaTyVars, betaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
9 alphaTy, betaTy, gammaTy, deltaTy,
10 openAlphaTy, openAlphaTyVar, openAlphaTyVars,
14 charPrimTyCon, charPrimTy,
15 intPrimTyCon, intPrimTy,
16 wordPrimTyCon, wordPrimTy,
17 addrPrimTyCon, addrPrimTy,
18 floatPrimTyCon, floatPrimTy,
19 doublePrimTyCon, doublePrimTy,
21 statePrimTyCon, mkStatePrimTy,
22 realWorldTyCon, realWorldTy, realWorldStatePrimTy,
24 arrayPrimTyCon, mkArrayPrimTy,
25 byteArrayPrimTyCon, byteArrayPrimTy,
26 mutableArrayPrimTyCon, mkMutableArrayPrimTy,
27 mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
28 mutVarPrimTyCon, mkMutVarPrimTy,
30 mVarPrimTyCon, mkMVarPrimTy,
31 stablePtrPrimTyCon, mkStablePtrPrimTy,
32 stableNamePrimTyCon, mkStableNamePrimTy,
33 bcoPrimTyCon, bcoPrimTy,
34 weakPrimTyCon, mkWeakPrimTy,
35 foreignObjPrimTyCon, foreignObjPrimTy,
36 threadIdPrimTyCon, threadIdPrimTy,
38 int32PrimTyCon, int32PrimTy,
39 word32PrimTyCon, word32PrimTy,
41 int64PrimTyCon, int64PrimTy,
42 word64PrimTyCon, word64PrimTy
45 #include "HsVersions.h"
47 import Var ( TyVar, mkTyVar )
48 import Name ( Name, mkInternalName )
49 import OccName ( mkVarOcc )
50 import PrimRep ( PrimRep(..) )
51 import TyCon ( TyCon, ArgVrcs, mkPrimTyCon, mkLiftedPrimTyCon )
52 import Type ( mkTyConApp, mkTyConTy, mkTyVarTys, mkTyVarTy,
53 unliftedTypeKind, liftedTypeKind, openTypeKind,
56 import SrcLoc ( noSrcLoc )
57 import Unique ( mkAlphaTyVarUnique )
59 import FastString ( mkFastString )
62 import Char ( ord, chr )
65 %************************************************************************
67 \subsection{Primitive type constructors}
69 %************************************************************************
86 , mutableArrayPrimTyCon
87 , mutableByteArrayPrimTyCon
102 %************************************************************************
104 \subsection{Support code}
106 %************************************************************************
108 alphaTyVars is a list of type variables for use in templates:
109 ["a", "b", ..., "z", "t1", "t2", ... ]
112 tyVarList :: Kind -> [TyVar]
113 tyVarList kind = [ mkTyVar (mkInternalName (mkAlphaTyVarUnique u)
114 (mkVarOcc (mkFastString name))
117 let name | c <= 'z' = [c]
118 | otherwise = 't':show u
119 where c = chr (u-2 + ord 'a')
122 alphaTyVars :: [TyVar]
123 alphaTyVars = tyVarList liftedTypeKind
125 betaTyVars = tail alphaTyVars
127 alphaTyVar, betaTyVar, gammaTyVar :: TyVar
128 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
130 alphaTys = mkTyVarTys alphaTyVars
131 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
133 -- openAlphaTyVar is prepared to be instantiated
134 -- to a lifted or unlifted type variable. It's used for the
135 -- result type for "error", so that we can have (error Int# "Help")
136 openAlphaTyVars :: [TyVar]
137 openAlphaTyVars@(openAlphaTyVar:_) = tyVarList openTypeKind
139 openAlphaTy = mkTyVarTy openAlphaTyVar
141 vrcPos,vrcZero :: (Bool,Bool)
142 vrcPos = (True,False)
143 vrcZero = (False,False)
145 vrcsP,vrcsZ,vrcsZP :: ArgVrcs
148 vrcsZP = [vrcZero,vrcPos]
152 %************************************************************************
154 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
156 %************************************************************************
160 pcPrimTyCon :: Name -> ArgVrcs -> PrimRep -> TyCon
161 pcPrimTyCon name arg_vrcs rep
162 = mkPrimTyCon name kind arity arg_vrcs rep
164 arity = length arg_vrcs
165 kind = mkArrowKinds (replicate arity liftedTypeKind) result_kind
166 result_kind = unliftedTypeKind -- all primitive types are unlifted
168 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
169 pcPrimTyCon0 name rep
170 = mkPrimTyCon name result_kind 0 [] rep
172 result_kind = unliftedTypeKind -- all primitive types are unlifted
174 charPrimTy = mkTyConTy charPrimTyCon
175 charPrimTyCon = pcPrimTyCon0 charPrimTyConName CharRep
177 intPrimTy = mkTyConTy intPrimTyCon
178 intPrimTyCon = pcPrimTyCon0 intPrimTyConName IntRep
180 int32PrimTy = mkTyConTy int32PrimTyCon
181 int32PrimTyCon = pcPrimTyCon0 int32PrimTyConName Int32Rep
183 int64PrimTy = mkTyConTy int64PrimTyCon
184 int64PrimTyCon = pcPrimTyCon0 int64PrimTyConName Int64Rep
186 wordPrimTy = mkTyConTy wordPrimTyCon
187 wordPrimTyCon = pcPrimTyCon0 wordPrimTyConName WordRep
189 word32PrimTy = mkTyConTy word32PrimTyCon
190 word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName Word32Rep
192 word64PrimTy = mkTyConTy word64PrimTyCon
193 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
195 addrPrimTy = mkTyConTy addrPrimTyCon
196 addrPrimTyCon = pcPrimTyCon0 addrPrimTyConName AddrRep
198 floatPrimTy = mkTyConTy floatPrimTyCon
199 floatPrimTyCon = pcPrimTyCon0 floatPrimTyConName FloatRep
201 doublePrimTy = mkTyConTy doublePrimTyCon
202 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
206 %************************************************************************
208 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
210 %************************************************************************
212 State# is the primitive, unlifted type of states. It has one type parameter,
218 where s is a type variable. The only purpose of the type parameter is to
219 keep different state threads separate. It is represented by nothing at all.
222 mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
223 statePrimTyCon = pcPrimTyCon statePrimTyConName vrcsZ VoidRep
226 RealWorld is deeply magical. It is *primitive*, but it is not
227 *unlifted* (hence PtrRep). We never manipulate values of type
228 RealWorld; it's only used in the type system, to parameterise State#.
231 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind 0 [] PtrRep
232 realWorldTy = mkTyConTy realWorldTyCon
233 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
236 Note: the ``state-pairing'' types are not truly primitive, so they are
237 defined in \tr{TysWiredIn.lhs}, not here.
240 %************************************************************************
242 \subsection[TysPrim-arrays]{The primitive array types}
244 %************************************************************************
247 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName vrcsP PtrRep
248 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName vrcsZP PtrRep
249 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName vrcsZ PtrRep
250 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName PtrRep
252 mkArrayPrimTy elt = mkTyConApp arrayPrimTyCon [elt]
253 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
254 mkMutableArrayPrimTy s elt = mkTyConApp mutableArrayPrimTyCon [s, elt]
255 mkMutableByteArrayPrimTy s = mkTyConApp mutableByteArrayPrimTyCon [s]
258 %************************************************************************
260 \subsection[TysPrim-mut-var]{The mutable variable type}
262 %************************************************************************
265 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName vrcsZP PtrRep
267 mkMutVarPrimTy s elt = mkTyConApp mutVarPrimTyCon [s, elt]
270 %************************************************************************
272 \subsection[TysPrim-synch-var]{The synchronizing variable type}
274 %************************************************************************
277 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName vrcsZP PtrRep
279 mkMVarPrimTy s elt = mkTyConApp mVarPrimTyCon [s, elt]
282 %************************************************************************
284 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
286 %************************************************************************
289 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName vrcsP StablePtrRep
291 mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
294 %************************************************************************
296 \subsection[TysPrim-stable-names]{The stable-name type}
298 %************************************************************************
301 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName vrcsP PtrRep
303 mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
306 %************************************************************************
308 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
310 %************************************************************************
312 A Foreign Object is just a boxed, unlifted, Addr#. They're needed
313 because finalisers (weak pointers) can't watch Addr#s, they can only
314 watch heap-resident objects.
316 We can't use a lifted Addr# (such as Addr) because race conditions
317 could bite us. For example, if the program deconstructed the Addr
318 before passing its contents to a ccall, and a weak pointer was
319 watching the Addr, the weak pointer might deduce that the Addr was
320 dead before it really was.
323 foreignObjPrimTy = mkTyConTy foreignObjPrimTyCon
324 foreignObjPrimTyCon = pcPrimTyCon0 foreignObjPrimTyConName PtrRep
327 %************************************************************************
329 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
331 %************************************************************************
334 bcoPrimTy = mkTyConTy bcoPrimTyCon
335 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName PtrRep
338 %************************************************************************
340 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
342 %************************************************************************
345 weakPrimTyCon = pcPrimTyCon weakPrimTyConName vrcsP PtrRep
347 mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
350 %************************************************************************
352 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
354 %************************************************************************
356 A thread id is represented by a pointer to the TSO itself, to ensure
357 that they are always unique and we can always find the TSO for a given
358 thread id. However, this has the unfortunate consequence that a
359 ThreadId# for a given thread is treated as a root by the garbage
360 collector and can keep TSOs around for too long.
362 Hence the programmer API for thread manipulation uses a weak pointer
363 to the thread id internally.
366 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
367 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName PtrRep