2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[TysPrim]{Wired-in knowledge about primitive types}
6 This module tracks the ``state interface'' document, ``GHC prelude:
7 types and operations.''
11 alphaTyVars, betaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
12 alphaTy, betaTy, gammaTy, deltaTy,
13 openAlphaTy, openAlphaTyVar, openAlphaTyVars,
17 charPrimTyCon, charPrimTy,
18 intPrimTyCon, intPrimTy,
19 wordPrimTyCon, wordPrimTy,
20 addrPrimTyCon, addrPrimTy,
21 floatPrimTyCon, floatPrimTy,
22 doublePrimTyCon, doublePrimTy,
24 statePrimTyCon, mkStatePrimTy,
25 realWorldTyCon, realWorldTy, realWorldStatePrimTy,
27 arrayPrimTyCon, mkArrayPrimTy,
28 byteArrayPrimTyCon, byteArrayPrimTy,
29 mutableArrayPrimTyCon, mkMutableArrayPrimTy,
30 mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
31 mutVarPrimTyCon, mkMutVarPrimTy,
33 mVarPrimTyCon, mkMVarPrimTy,
34 stablePtrPrimTyCon, mkStablePtrPrimTy,
35 stableNamePrimTyCon, mkStableNamePrimTy,
36 bcoPrimTyCon, bcoPrimTy,
37 weakPrimTyCon, mkWeakPrimTy,
38 foreignObjPrimTyCon, foreignObjPrimTy,
39 threadIdPrimTyCon, threadIdPrimTy,
41 int32PrimTyCon, int32PrimTy,
42 word32PrimTyCon, word32PrimTy,
44 int64PrimTyCon, int64PrimTy,
45 word64PrimTyCon, word64PrimTy,
50 #include "HsVersions.h"
52 import Var ( TyVar, mkTyVar )
53 import Name ( Name, mkInternalName )
54 import OccName ( mkVarOcc )
55 import PrimRep ( PrimRep(..) )
56 import TyCon ( TyCon, ArgVrcs, mkPrimTyCon, mkLiftedPrimTyCon )
57 import Type ( mkTyConApp, mkTyConTy, mkTyVarTys, mkTyVarTy,
58 unliftedTypeKind, liftedTypeKind, openTypeKind,
61 import SrcLoc ( noSrcLoc )
62 import Unique ( mkAlphaTyVarUnique )
64 import FastString ( mkFastString )
67 import Char ( ord, chr )
70 %************************************************************************
72 \subsection{Primitive type constructors}
74 %************************************************************************
91 , mutableArrayPrimTyCon
92 , mutableByteArrayPrimTyCon
107 %************************************************************************
109 \subsection{Support code}
111 %************************************************************************
113 alphaTyVars is a list of type variables for use in templates:
114 ["a", "b", ..., "z", "t1", "t2", ... ]
117 tyVarList :: Kind -> [TyVar]
118 tyVarList kind = [ mkTyVar (mkInternalName (mkAlphaTyVarUnique u)
119 (mkVarOcc (mkFastString name))
122 let name | c <= 'z' = [c]
123 | otherwise = 't':show u
124 where c = chr (u-2 + ord 'a')
127 alphaTyVars :: [TyVar]
128 alphaTyVars = tyVarList liftedTypeKind
130 betaTyVars = tail alphaTyVars
132 alphaTyVar, betaTyVar, gammaTyVar :: TyVar
133 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
135 alphaTys = mkTyVarTys alphaTyVars
136 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
138 -- openAlphaTyVar is prepared to be instantiated
139 -- to a lifted or unlifted type variable. It's used for the
140 -- result type for "error", so that we can have (error Int# "Help")
141 openAlphaTyVars :: [TyVar]
142 openAlphaTyVars@(openAlphaTyVar:_) = tyVarList openTypeKind
144 openAlphaTy = mkTyVarTy openAlphaTyVar
146 vrcPos,vrcZero :: (Bool,Bool)
147 vrcPos = (True,False)
148 vrcZero = (False,False)
150 vrcsP,vrcsZ,vrcsZP :: ArgVrcs
153 vrcsZP = [vrcZero,vrcPos]
157 %************************************************************************
159 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
161 %************************************************************************
165 pcPrimTyCon :: Name -> ArgVrcs -> PrimRep -> TyCon
166 pcPrimTyCon name arg_vrcs rep
167 = mkPrimTyCon name kind arity arg_vrcs rep
169 arity = length arg_vrcs
170 kind = mkArrowKinds (replicate arity liftedTypeKind) result_kind
171 result_kind = unliftedTypeKind -- all primitive types are unlifted
173 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
174 pcPrimTyCon0 name rep
175 = mkPrimTyCon name result_kind 0 [] rep
177 result_kind = unliftedTypeKind -- all primitive types are unlifted
179 charPrimTy = mkTyConTy charPrimTyCon
180 charPrimTyCon = pcPrimTyCon0 charPrimTyConName CharRep
182 intPrimTy = mkTyConTy intPrimTyCon
183 intPrimTyCon = pcPrimTyCon0 intPrimTyConName IntRep
185 int32PrimTy = mkTyConTy int32PrimTyCon
186 int32PrimTyCon = pcPrimTyCon0 int32PrimTyConName Int32Rep
188 int64PrimTy = mkTyConTy int64PrimTyCon
189 int64PrimTyCon = pcPrimTyCon0 int64PrimTyConName Int64Rep
191 wordPrimTy = mkTyConTy wordPrimTyCon
192 wordPrimTyCon = pcPrimTyCon0 wordPrimTyConName WordRep
194 word32PrimTy = mkTyConTy word32PrimTyCon
195 word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName Word32Rep
197 word64PrimTy = mkTyConTy word64PrimTyCon
198 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
200 addrPrimTy = mkTyConTy addrPrimTyCon
201 addrPrimTyCon = pcPrimTyCon0 addrPrimTyConName AddrRep
203 floatPrimTy = mkTyConTy floatPrimTyCon
204 floatPrimTyCon = pcPrimTyCon0 floatPrimTyConName FloatRep
206 doublePrimTy = mkTyConTy doublePrimTyCon
207 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
211 %************************************************************************
213 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
215 %************************************************************************
217 State# is the primitive, unlifted type of states. It has one type parameter,
223 where s is a type variable. The only purpose of the type parameter is to
224 keep different state threads separate. It is represented by nothing at all.
227 mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
228 statePrimTyCon = pcPrimTyCon statePrimTyConName vrcsZ VoidRep
231 RealWorld is deeply magical. It is *primitive*, but it is not
232 *unlifted* (hence PrimPtrRep). We never manipulate values of type
233 RealWorld; it's only used in the type system, to parameterise State#.
236 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind 0 [] PrimPtrRep
237 realWorldTy = mkTyConTy realWorldTyCon
238 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
241 Note: the ``state-pairing'' types are not truly primitive, so they are
242 defined in \tr{TysWiredIn.lhs}, not here.
245 %************************************************************************
247 \subsection[TysPrim-arrays]{The primitive array types}
249 %************************************************************************
252 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName vrcsP ArrayRep
253 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName vrcsZP ArrayRep
254 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName vrcsZ ByteArrayRep
255 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName ByteArrayRep
257 mkArrayPrimTy elt = mkTyConApp arrayPrimTyCon [elt]
258 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
259 mkMutableArrayPrimTy s elt = mkTyConApp mutableArrayPrimTyCon [s, elt]
260 mkMutableByteArrayPrimTy s = mkTyConApp mutableByteArrayPrimTyCon [s]
263 %************************************************************************
265 \subsection[TysPrim-mut-var]{The mutable variable type}
267 %************************************************************************
270 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName vrcsZP PrimPtrRep
272 mkMutVarPrimTy s elt = mkTyConApp mutVarPrimTyCon [s, elt]
275 %************************************************************************
277 \subsection[TysPrim-synch-var]{The synchronizing variable type}
279 %************************************************************************
282 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName vrcsZP PrimPtrRep
284 mkMVarPrimTy s elt = mkTyConApp mVarPrimTyCon [s, elt]
287 %************************************************************************
289 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
291 %************************************************************************
294 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName vrcsP StablePtrRep
296 mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
299 %************************************************************************
301 \subsection[TysPrim-stable-names]{The stable-name type}
303 %************************************************************************
306 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName vrcsP StableNameRep
308 mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
311 %************************************************************************
313 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
315 %************************************************************************
317 A Foreign Object is just a boxed, unlifted, Addr#. They're needed
318 because finalisers (weak pointers) can't watch Addr#s, they can only
319 watch heap-resident objects.
321 We can't use a lifted Addr# (such as Addr) because race conditions
322 could bite us. For example, if the program deconstructed the Addr
323 before passing its contents to a ccall, and a weak pointer was
324 watching the Addr, the weak pointer might deduce that the Addr was
325 dead before it really was.
328 foreignObjPrimTy = mkTyConTy foreignObjPrimTyCon
329 foreignObjPrimTyCon = pcPrimTyCon0 foreignObjPrimTyConName ForeignObjRep
332 %************************************************************************
334 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
336 %************************************************************************
339 bcoPrimTy = mkTyConTy bcoPrimTyCon
340 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName BCORep
343 %************************************************************************
345 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
347 %************************************************************************
350 weakPrimTyCon = pcPrimTyCon weakPrimTyConName vrcsP WeakPtrRep
352 mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
355 %************************************************************************
357 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
359 %************************************************************************
361 A thread id is represented by a pointer to the TSO itself, to ensure
362 that they are always unique and we can always find the TSO for a given
363 thread id. However, this has the unfortunate consequence that a
364 ThreadId# for a given thread is treated as a root by the garbage
365 collector and can keep TSOs around for too long.
367 Hence the programmer API for thread manipulation uses a weak pointer
368 to the thread id internally.
371 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
372 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName ThreadIdRep
375 %************************************************************************
377 \subsection[TysPrim-PrimRep]{Making types from PrimReps}
379 %************************************************************************
381 Each of the primitive types from this module is equivalent to a
382 PrimRep (see PrimRep.lhs). The following function returns the
383 primitive TyCon for a given PrimRep.
386 primRepTyCon CharRep = charPrimTyCon
387 primRepTyCon Int8Rep = charPrimTyCon
388 primRepTyCon IntRep = intPrimTyCon
389 primRepTyCon WordRep = wordPrimTyCon
390 primRepTyCon Int32Rep = int32PrimTyCon
391 primRepTyCon Int64Rep = int64PrimTyCon
392 primRepTyCon Word32Rep = word32PrimTyCon
393 primRepTyCon Word64Rep = word64PrimTyCon
394 primRepTyCon AddrRep = addrPrimTyCon
395 primRepTyCon FloatRep = floatPrimTyCon
396 primRepTyCon DoubleRep = doublePrimTyCon
397 primRepTyCon StablePtrRep = stablePtrPrimTyCon
398 primRepTyCon ForeignObjRep = foreignObjPrimTyCon
399 primRepTyCon WeakPtrRep = weakPrimTyCon
400 primRepTyCon other = pprPanic "primRepTyCon" (ppr other)