[project @ 2002-03-14 15:27:15 by simonpj]
[ghc-hetmet.git] / ghc / compiler / prelude / TysPrim.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1998
3 %
4 \section[TysPrim]{Wired-in knowledge about primitive types}
5
6 This module tracks the ``state interface'' document, ``GHC prelude:
7 types and operations.''
8
9 \begin{code}
10 module TysPrim(
11         alphaTyVars, betaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
12         alphaTy, betaTy, gammaTy, deltaTy,
13         openAlphaTy, openAlphaTyVar, openAlphaTyVars,
14
15         primTyCons,
16
17         charPrimTyCon,          charPrimTy,
18         intPrimTyCon,           intPrimTy,
19         wordPrimTyCon,          wordPrimTy,
20         addrPrimTyCon,          addrPrimTy,
21         floatPrimTyCon,         floatPrimTy,
22         doublePrimTyCon,        doublePrimTy,
23
24         statePrimTyCon,         mkStatePrimTy,
25         realWorldTyCon,         realWorldTy, realWorldStatePrimTy,
26
27         arrayPrimTyCon,                 mkArrayPrimTy, 
28         byteArrayPrimTyCon,             byteArrayPrimTy,
29         mutableArrayPrimTyCon,          mkMutableArrayPrimTy,
30         mutableByteArrayPrimTyCon,      mkMutableByteArrayPrimTy,
31         mutVarPrimTyCon,                mkMutVarPrimTy,
32
33         mVarPrimTyCon,                  mkMVarPrimTy,   
34         stablePtrPrimTyCon,             mkStablePtrPrimTy,
35         stableNamePrimTyCon,            mkStableNamePrimTy,
36         bcoPrimTyCon,                   bcoPrimTy,
37         weakPrimTyCon,                  mkWeakPrimTy,
38         foreignObjPrimTyCon,            foreignObjPrimTy,
39         threadIdPrimTyCon,              threadIdPrimTy,
40         
41         int32PrimTyCon,         int32PrimTy,
42         word32PrimTyCon,        word32PrimTy,
43
44         int64PrimTyCon,         int64PrimTy,
45         word64PrimTyCon,        word64PrimTy,
46
47         primRepTyCon
48   ) where
49
50 #include "HsVersions.h"
51
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, 
59                           Kind, mkArrowKinds
60                         )
61 import SrcLoc           ( noSrcLoc )
62 import Unique           ( mkAlphaTyVarUnique )
63 import PrelNames
64 import FastString       ( mkFastString )
65 import Outputable
66
67 import Char             ( ord, chr )
68 \end{code}
69
70 %************************************************************************
71 %*                                                                      *
72 \subsection{Primitive type constructors}
73 %*                                                                      *
74 %************************************************************************
75
76 \begin{code}
77 primTyCons :: [TyCon]
78 primTyCons 
79   = [ addrPrimTyCon
80     , arrayPrimTyCon
81     , byteArrayPrimTyCon
82     , charPrimTyCon
83     , doublePrimTyCon
84     , floatPrimTyCon
85     , intPrimTyCon
86     , int32PrimTyCon
87     , int64PrimTyCon
88     , foreignObjPrimTyCon
89     , bcoPrimTyCon
90     , weakPrimTyCon
91     , mutableArrayPrimTyCon
92     , mutableByteArrayPrimTyCon
93     , mVarPrimTyCon
94     , mutVarPrimTyCon
95     , realWorldTyCon
96     , stablePtrPrimTyCon
97     , stableNamePrimTyCon
98     , statePrimTyCon
99     , threadIdPrimTyCon
100     , wordPrimTyCon
101     , word32PrimTyCon
102     , word64PrimTyCon
103     ]
104 \end{code}
105
106
107 %************************************************************************
108 %*                                                                      *
109 \subsection{Support code}
110 %*                                                                      *
111 %************************************************************************
112
113 alphaTyVars is a list of type variables for use in templates: 
114         ["a", "b", ..., "z", "t1", "t2", ... ]
115
116 \begin{code}
117 tyVarList :: Kind -> [TyVar]
118 tyVarList kind = [ mkTyVar (mkInternalName (mkAlphaTyVarUnique u) 
119                                 (mkVarOcc (mkFastString name))
120                                 noSrcLoc) kind
121                  | u <- [2..],
122                    let name | c <= 'z'  = [c]
123                             | otherwise = 't':show u
124                             where c = chr (u-2 + ord 'a')
125                  ]
126
127 alphaTyVars :: [TyVar]
128 alphaTyVars = tyVarList liftedTypeKind
129
130 betaTyVars = tail alphaTyVars
131
132 alphaTyVar, betaTyVar, gammaTyVar :: TyVar
133 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
134
135 alphaTys = mkTyVarTys alphaTyVars
136 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
137
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
143
144 openAlphaTy = mkTyVarTy openAlphaTyVar
145
146 vrcPos,vrcZero :: (Bool,Bool)
147 vrcPos  = (True,False)
148 vrcZero = (False,False)
149
150 vrcsP,vrcsZ,vrcsZP :: ArgVrcs
151 vrcsP  = [vrcPos]
152 vrcsZ  = [vrcZero]
153 vrcsZP = [vrcZero,vrcPos]
154 \end{code}
155
156
157 %************************************************************************
158 %*                                                                      *
159 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
160 %*                                                                      *
161 %************************************************************************
162
163 \begin{code}
164 -- only used herein
165 pcPrimTyCon :: Name -> ArgVrcs -> PrimRep -> TyCon
166 pcPrimTyCon name arg_vrcs rep
167   = mkPrimTyCon name kind arity arg_vrcs rep
168   where
169     arity       = length arg_vrcs
170     kind        = mkArrowKinds (replicate arity liftedTypeKind) result_kind
171     result_kind = unliftedTypeKind -- all primitive types are unlifted
172
173 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
174 pcPrimTyCon0 name rep
175   = mkPrimTyCon name result_kind 0 [] rep
176   where
177     result_kind = unliftedTypeKind -- all primitive types are unlifted
178
179 charPrimTy      = mkTyConTy charPrimTyCon
180 charPrimTyCon   = pcPrimTyCon0 charPrimTyConName CharRep
181
182 intPrimTy       = mkTyConTy intPrimTyCon
183 intPrimTyCon    = pcPrimTyCon0 intPrimTyConName IntRep
184
185 int32PrimTy     = mkTyConTy int32PrimTyCon
186 int32PrimTyCon  = pcPrimTyCon0 int32PrimTyConName Int32Rep
187
188 int64PrimTy     = mkTyConTy int64PrimTyCon
189 int64PrimTyCon  = pcPrimTyCon0 int64PrimTyConName Int64Rep
190
191 wordPrimTy      = mkTyConTy wordPrimTyCon
192 wordPrimTyCon   = pcPrimTyCon0 wordPrimTyConName WordRep
193
194 word32PrimTy    = mkTyConTy word32PrimTyCon
195 word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName Word32Rep
196
197 word64PrimTy    = mkTyConTy word64PrimTyCon
198 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
199
200 addrPrimTy      = mkTyConTy addrPrimTyCon
201 addrPrimTyCon   = pcPrimTyCon0 addrPrimTyConName AddrRep
202
203 floatPrimTy     = mkTyConTy floatPrimTyCon
204 floatPrimTyCon  = pcPrimTyCon0 floatPrimTyConName FloatRep
205
206 doublePrimTy    = mkTyConTy doublePrimTyCon
207 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
208 \end{code}
209
210
211 %************************************************************************
212 %*                                                                      *
213 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
214 %*                                                                      *
215 %************************************************************************
216
217 State# is the primitive, unlifted type of states.  It has one type parameter,
218 thus
219         State# RealWorld
220 or
221         State# s
222
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.
225
226 \begin{code}
227 mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
228 statePrimTyCon   = pcPrimTyCon statePrimTyConName vrcsZ VoidRep
229 \end{code}
230
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#.
234
235 \begin{code}
236 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind 0 [] PrimPtrRep
237 realWorldTy          = mkTyConTy realWorldTyCon
238 realWorldStatePrimTy = mkStatePrimTy realWorldTy        -- State# RealWorld
239 \end{code}
240
241 Note: the ``state-pairing'' types are not truly primitive, so they are
242 defined in \tr{TysWiredIn.lhs}, not here.
243
244
245 %************************************************************************
246 %*                                                                      *
247 \subsection[TysPrim-arrays]{The primitive array types}
248 %*                                                                      *
249 %************************************************************************
250
251 \begin{code}
252 arrayPrimTyCon            = pcPrimTyCon  arrayPrimTyConName            vrcsP  ArrayRep
253 mutableArrayPrimTyCon     = pcPrimTyCon  mutableArrayPrimTyConName     vrcsZP ArrayRep
254 mutableByteArrayPrimTyCon = pcPrimTyCon  mutableByteArrayPrimTyConName vrcsZ  ByteArrayRep
255 byteArrayPrimTyCon        = pcPrimTyCon0 byteArrayPrimTyConName               ByteArrayRep
256
257 mkArrayPrimTy elt           = mkTyConApp arrayPrimTyCon [elt]
258 byteArrayPrimTy             = mkTyConTy byteArrayPrimTyCon
259 mkMutableArrayPrimTy s elt  = mkTyConApp mutableArrayPrimTyCon [s, elt]
260 mkMutableByteArrayPrimTy s  = mkTyConApp mutableByteArrayPrimTyCon [s]
261 \end{code}
262
263 %************************************************************************
264 %*                                                                      *
265 \subsection[TysPrim-mut-var]{The mutable variable type}
266 %*                                                                      *
267 %************************************************************************
268
269 \begin{code}
270 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName vrcsZP PrimPtrRep
271
272 mkMutVarPrimTy s elt        = mkTyConApp mutVarPrimTyCon [s, elt]
273 \end{code}
274
275 %************************************************************************
276 %*                                                                      *
277 \subsection[TysPrim-synch-var]{The synchronizing variable type}
278 %*                                                                      *
279 %************************************************************************
280
281 \begin{code}
282 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName vrcsZP PrimPtrRep
283
284 mkMVarPrimTy s elt          = mkTyConApp mVarPrimTyCon [s, elt]
285 \end{code}
286
287 %************************************************************************
288 %*                                                                      *
289 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
290 %*                                                                      *
291 %************************************************************************
292
293 \begin{code}
294 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName vrcsP StablePtrRep
295
296 mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
297 \end{code}
298
299 %************************************************************************
300 %*                                                                      *
301 \subsection[TysPrim-stable-names]{The stable-name type}
302 %*                                                                      *
303 %************************************************************************
304
305 \begin{code}
306 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName vrcsP StableNameRep
307
308 mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
309 \end{code}
310
311 %************************************************************************
312 %*                                                                      *
313 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
314 %*                                                                      *
315 %************************************************************************
316
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.  
320
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.
326
327 \begin{code}
328 foreignObjPrimTy    = mkTyConTy foreignObjPrimTyCon
329 foreignObjPrimTyCon = pcPrimTyCon0 foreignObjPrimTyConName ForeignObjRep
330 \end{code}
331   
332 %************************************************************************
333 %*                                                                      *
334 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
335 %*                                                                      *
336 %************************************************************************
337
338 \begin{code}
339 bcoPrimTy    = mkTyConTy bcoPrimTyCon
340 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName BCORep
341 \end{code}
342   
343 %************************************************************************
344 %*                                                                      *
345 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
346 %*                                                                      *
347 %************************************************************************
348
349 \begin{code}
350 weakPrimTyCon = pcPrimTyCon weakPrimTyConName vrcsP WeakPtrRep
351
352 mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
353 \end{code}
354
355 %************************************************************************
356 %*                                                                      *
357 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
358 %*                                                                      *
359 %************************************************************************
360
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.
366
367 Hence the programmer API for thread manipulation uses a weak pointer
368 to the thread id internally.
369
370 \begin{code}
371 threadIdPrimTy    = mkTyConTy threadIdPrimTyCon
372 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName ThreadIdRep
373 \end{code}
374
375 %************************************************************************
376 %*                                                                      *
377 \subsection[TysPrim-PrimRep]{Making types from PrimReps}
378 %*                                                                      *
379 %************************************************************************
380
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.
384
385 \begin{code}
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)
401 \end{code}