[project @ 2001-05-14 12:55:11 by simonpj]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Id.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[Id]{@Ids@: Value and constructor identifiers}
5
6 \begin{code}
7 module Id (
8         Id, DictId,
9
10         -- Simple construction
11         mkGlobalId, mkLocalId, mkSpecPragmaId, mkLocalIdWithInfo,
12         mkSysLocal, mkUserLocal, mkVanillaGlobal,
13         mkTemplateLocals, mkTemplateLocalsNum, mkWildId, mkTemplateLocal,
14         mkWorkerId,
15
16         -- Taking an Id apart
17         idName, idType, idUnique, idInfo,
18         idPrimRep, isId, globalIdDetails,
19         recordSelectorFieldLabel,
20
21         -- Modifying an Id
22         setIdName, setIdUnique, setIdType, setIdNoDiscard, setGlobalIdDetails,
23         setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
24         zapLamIdInfo, zapDemandIdInfo, 
25
26         -- Predicates
27         isImplicitId, isDeadBinder,
28         isSpecPragmaId, isExportedId, isLocalId, isGlobalId,
29         isRecordSelector,
30         isPrimOpId, isPrimOpId_maybe, 
31         isDataConId, isDataConId_maybe, 
32         isDataConWrapId, isDataConWrapId_maybe,
33         isBottomingId,
34         hasNoBinding,
35
36         -- Inline pragma stuff
37         idInlinePragma, setInlinePragma, modifyInlinePragma, 
38
39
40         -- One shot lambda stuff
41         isOneShotLambda, setOneShotLambda, clearOneShotLambda,
42
43         -- IdInfo stuff
44         setIdUnfolding,
45         setIdArityInfo,
46         setIdDemandInfo,
47         setIdStrictness,
48         setIdTyGenInfo,
49         setIdWorkerInfo,
50         setIdSpecialisation,
51         setIdCgInfo,
52         setIdCprInfo,
53         setIdOccInfo,
54
55         idArity, idArityInfo, 
56         idDemandInfo,
57         idStrictness,
58         idTyGenInfo,
59         idWorkerInfo,
60         idUnfolding,
61         idSpecialisation,
62         idCgInfo,
63         idCafInfo,
64         idCgArity,
65         idCprInfo,
66         idLBVarInfo,
67         idOccInfo,
68
69     ) where
70
71 #include "HsVersions.h"
72
73
74 import CoreSyn          ( Unfolding, CoreRules )
75 import BasicTypes       ( Arity )
76 import Var              ( Id, DictId,
77                           isId, isExportedId, isSpecPragmaId, isLocalId,
78                           idName, idType, idUnique, idInfo, isGlobalId,
79                           setIdName, setVarType, setIdUnique, setIdNoDiscard,
80                           setIdInfo, lazySetIdInfo, modifyIdInfo, 
81                           maybeModifyIdInfo,
82                           globalIdDetails, setGlobalIdDetails
83                         )
84 import qualified Var    ( mkLocalId, mkGlobalId, mkSpecPragmaId )
85 import Type             ( Type, typePrimRep, addFreeTyVars, 
86                           usOnce, seqType, splitTyConApp_maybe )
87
88 import IdInfo 
89
90 import Demand           ( Demand )
91 import Name             ( Name, OccName,
92                           mkSysLocalName, mkLocalName,
93                           getOccName, getSrcLoc
94                         ) 
95 import OccName          ( UserFS, mkWorkerOcc )
96 import PrimRep          ( PrimRep )
97 import TysPrim          ( statePrimTyCon )
98 import FieldLabel       ( FieldLabel )
99 import SrcLoc           ( SrcLoc )
100 import Outputable
101 import Unique           ( Unique, mkBuiltinUnique, getBuiltinUniques, 
102                           getNumBuiltinUniques )
103
104 infixl  1 `setIdUnfolding`,
105           `setIdArityInfo`,
106           `setIdDemandInfo`,
107           `setIdStrictness`,
108           `setIdTyGenInfo`,
109           `setIdWorkerInfo`,
110           `setIdSpecialisation`,
111           `setInlinePragma`,
112           `idCafInfo`,
113           `idCprInfo`
114
115         -- infixl so you can say (id `set` a `set` b)
116 \end{code}
117
118
119
120 %************************************************************************
121 %*                                                                      *
122 \subsection{Simple Id construction}
123 %*                                                                      *
124 %************************************************************************
125
126 Absolutely all Ids are made by mkId.  It is just like Var.mkId,
127 but in addition it pins free-tyvar-info onto the Id's type, 
128 where it can easily be found.
129
130 \begin{code}
131 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
132 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
133
134 mkSpecPragmaId :: OccName -> Unique -> Type -> SrcLoc -> Id
135 mkSpecPragmaId occ uniq ty loc = Var.mkSpecPragmaId (mkLocalName uniq occ loc)
136                                                     (addFreeTyVars ty)
137                                                     vanillaIdInfo
138
139 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
140 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
141 \end{code}
142
143 \begin{code}
144 mkLocalId :: Name -> Type -> Id
145 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
146
147 -- SysLocal: for an Id being created by the compiler out of thin air...
148 -- UserLocal: an Id with a name the user might recognize...
149 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
150 mkSysLocal  :: UserFS  -> Unique -> Type -> Id
151 mkVanillaGlobal :: Name -> Type -> IdInfo -> Id
152
153 mkSysLocal  fs uniq ty      = mkLocalId (mkSysLocalName uniq fs)      ty
154 mkUserLocal occ uniq ty loc = mkLocalId (mkLocalName    uniq occ loc) ty
155 mkVanillaGlobal             = mkGlobalId VanillaGlobal
156 \end{code}
157
158 Make some local @Ids@ for a template @CoreExpr@.  These have bogus
159 @Uniques@, but that's OK because the templates are supposed to be
160 instantiated before use.
161  
162 \begin{code}
163 -- "Wild Id" typically used when you need a binder that you don't expect to use
164 mkWildId :: Type -> Id
165 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
166
167 mkWorkerId :: Unique -> Id -> Type -> Id
168 -- A worker gets a local name.  CoreTidy will globalise it if necessary.
169 mkWorkerId uniq unwrkr ty
170   = mkLocalId wkr_name ty
171   where
172     wkr_name = mkLocalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcLoc unwrkr)
173
174 -- "Template locals" typically used in unfoldings
175 mkTemplateLocals :: [Type] -> [Id]
176 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
177                                (getBuiltinUniques (length tys))
178                                tys
179
180 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
181 -- The Int gives the starting point for unique allocation
182 mkTemplateLocalsNum n tys = zipWith (mkSysLocal SLIT("tpl"))
183                                     (getNumBuiltinUniques n (length tys))
184                                     tys
185
186 mkTemplateLocal :: Int -> Type -> Id
187 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
188 \end{code}
189
190
191 %************************************************************************
192 %*                                                                      *
193 \subsection[Id-general-funs]{General @Id@-related functions}
194 %*                                                                      *
195 %************************************************************************
196
197 \begin{code}
198 setIdType :: Id -> Type -> Id
199         -- Add free tyvar info to the type
200 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
201
202 idPrimRep :: Id -> PrimRep
203 idPrimRep id = typePrimRep (idType id)
204 \end{code}
205
206
207 %************************************************************************
208 %*                                                                      *
209 \subsection{Special Ids}
210 %*                                                                      *
211 %************************************************************************
212
213 The @SpecPragmaId@ exists only to make Ids that are
214 on the *LHS* of bindings created by SPECIALISE pragmas; 
215 eg:             s = f Int d
216 The SpecPragmaId is never itself mentioned; it
217 exists solely so that the specialiser will find
218 the call to f, and make specialised version of it.
219 The SpecPragmaId binding is discarded by the specialiser
220 when it gathers up overloaded calls.
221 Meanwhile, it is not discarded as dead code.
222
223
224 \begin{code}
225 recordSelectorFieldLabel :: Id -> FieldLabel
226 recordSelectorFieldLabel id = case globalIdDetails id of
227                                  RecordSelId lbl -> lbl
228
229 isRecordSelector id = case globalIdDetails id of
230                         RecordSelId lbl -> True
231                         other           -> False
232
233 isPrimOpId id = case globalIdDetails id of
234                     PrimOpId op -> True
235                     other       -> False
236
237 isPrimOpId_maybe id = case globalIdDetails id of
238                             PrimOpId op -> Just op
239                             other       -> Nothing
240
241 isDataConId id = case globalIdDetails id of
242                         DataConId _ -> True
243                         other       -> False
244
245 isDataConId_maybe id = case globalIdDetails id of
246                           DataConId con -> Just con
247                           other         -> Nothing
248
249 isDataConWrapId_maybe id = case globalIdDetails id of
250                                   DataConWrapId con -> Just con
251                                   other             -> Nothing
252
253 isDataConWrapId id = case globalIdDetails id of
254                         DataConWrapId con -> True
255                         other             -> False
256
257         -- hasNoBinding returns True of an Id which may not have a
258         -- binding, even though it is defined in this module.  Notably,
259         -- the constructors of a dictionary are in this situation.
260 hasNoBinding id = case globalIdDetails id of
261                         DataConId _ -> True
262                         PrimOpId _  -> True
263                         other       -> False
264
265 isImplicitId :: Id -> Bool
266         -- isImplicitId tells whether an Id's info is implied by other
267         -- declarations, so we don't need to put its signature in an interface
268         -- file, even if it's mentioned in some other interface unfolding.
269 isImplicitId id
270   = case globalIdDetails id of
271         RecordSelId _   -> True -- Includes dictionary selectors
272         PrimOpId _      -> True
273         DataConId _     -> True
274         DataConWrapId _ -> True
275                 -- These are are implied by their type or class decl;
276                 -- remember that all type and class decls appear in the interface file.
277                 -- The dfun id must *not* be omitted, because it carries version info for
278                 -- the instance decl
279         other           -> False
280 \end{code}
281
282 \begin{code}
283 isDeadBinder :: Id -> Bool
284 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
285                   | otherwise = False   -- TyVars count as not dead
286 \end{code}
287
288
289 %************************************************************************
290 %*                                                                      *
291 \subsection{IdInfo stuff}
292 %*                                                                      *
293 %************************************************************************
294
295 \begin{code}
296         ---------------------------------
297         -- ARITY
298 idArityInfo :: Id -> ArityInfo
299 idArityInfo id = arityInfo (idInfo id)
300
301 idArity :: Id -> Arity
302 idArity id = arityLowerBound (idArityInfo id)
303
304 setIdArityInfo :: Id -> ArityInfo -> Id
305 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
306
307         ---------------------------------
308         -- STRICTNESS
309 idStrictness :: Id -> StrictnessInfo
310 idStrictness id = strictnessInfo (idInfo id)
311
312 setIdStrictness :: Id -> StrictnessInfo -> Id
313 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
314
315 -- isBottomingId returns true if an application to n args would diverge
316 isBottomingId :: Id -> Bool
317 isBottomingId id = isBottomingStrictness (idStrictness id)
318
319         ---------------------------------
320         -- TYPE GENERALISATION
321 idTyGenInfo :: Id -> TyGenInfo
322 idTyGenInfo id = tyGenInfo (idInfo id)
323
324 setIdTyGenInfo :: Id -> TyGenInfo -> Id
325 setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id
326
327         ---------------------------------
328         -- WORKER ID
329 idWorkerInfo :: Id -> WorkerInfo
330 idWorkerInfo id = workerInfo (idInfo id)
331
332 setIdWorkerInfo :: Id -> WorkerInfo -> Id
333 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
334
335         ---------------------------------
336         -- UNFOLDING
337 idUnfolding :: Id -> Unfolding
338 idUnfolding id = unfoldingInfo (idInfo id)
339
340 setIdUnfolding :: Id -> Unfolding -> Id
341 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
342
343         ---------------------------------
344         -- DEMAND
345 idDemandInfo :: Id -> Demand
346 idDemandInfo id = demandInfo (idInfo id)
347
348 setIdDemandInfo :: Id -> Demand -> Id
349 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
350
351         ---------------------------------
352         -- SPECIALISATION
353 idSpecialisation :: Id -> CoreRules
354 idSpecialisation id = specInfo (idInfo id)
355
356 setIdSpecialisation :: Id -> CoreRules -> Id
357 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
358
359         ---------------------------------
360         -- CG INFO
361 idCgInfo :: Id -> CgInfo
362 #ifdef DEBUG
363 idCgInfo id = case cgInfo (idInfo id) of
364                   NoCgInfo -> pprPanic "idCgInfo" (ppr id)
365                   info     -> info
366 #else
367 idCgInfo id = cgInfo (idInfo id)
368 #endif          
369
370 setIdCgInfo :: Id -> CgInfo -> Id
371 setIdCgInfo id cg_info = modifyIdInfo (`setCgInfo` cg_info) id
372
373         ---------------------------------
374         -- CAF INFO
375 idCafInfo :: Id -> CafInfo
376 idCafInfo id = cgCafInfo (idCgInfo id)
377
378         ---------------------------------
379         -- CG ARITY
380
381 idCgArity :: Id -> Arity
382 idCgArity id = cgArity (idCgInfo id)
383
384         ---------------------------------
385         -- CPR INFO
386 idCprInfo :: Id -> CprInfo
387 idCprInfo id = cprInfo (idInfo id)
388
389 setIdCprInfo :: Id -> CprInfo -> Id
390 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
391
392         ---------------------------------
393         -- Occcurrence INFO
394 idOccInfo :: Id -> OccInfo
395 idOccInfo id = occInfo (idInfo id)
396
397 setIdOccInfo :: Id -> OccInfo -> Id
398 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
399 \end{code}
400
401
402         ---------------------------------
403         -- INLINING
404 The inline pragma tells us to be very keen to inline this Id, but it's still
405 OK not to if optimisation is switched off.
406
407 \begin{code}
408 idInlinePragma :: Id -> InlinePragInfo
409 idInlinePragma id = inlinePragInfo (idInfo id)
410
411 setInlinePragma :: Id -> InlinePragInfo -> Id
412 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
413
414 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
415 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
416 \end{code}
417
418
419         ---------------------------------
420         -- ONE-SHOT LAMBDAS
421 \begin{code}
422 idLBVarInfo :: Id -> LBVarInfo
423 idLBVarInfo id = lbvarInfo (idInfo id)
424
425 isOneShotLambda :: Id -> Bool
426 isOneShotLambda id = analysis || hack
427   where analysis = case idLBVarInfo id of
428                      LBVarInfo u    | u == usOnce             -> True
429                      other                                    -> False
430         hack     = case splitTyConApp_maybe (idType id) of
431                      Just (tycon,_) | tycon == statePrimTyCon -> True
432                      other                                    -> False
433
434         -- The last clause is a gross hack.  It claims that 
435         -- every function over realWorldStatePrimTy is a one-shot
436         -- function.  This is pretty true in practice, and makes a big
437         -- difference.  For example, consider
438         --      a `thenST` \ r -> ...E...
439         -- The early full laziness pass, if it doesn't know that r is one-shot
440         -- will pull out E (let's say it doesn't mention r) to give
441         --      let lvl = E in a `thenST` \ r -> ...lvl...
442         -- When `thenST` gets inlined, we end up with
443         --      let lvl = E in \s -> case a s of (r, s') -> ...lvl...
444         -- and we don't re-inline E.
445         --
446         -- It would be better to spot that r was one-shot to start with, but
447         -- I don't want to rely on that.
448         --
449         -- Another good example is in fill_in in PrelPack.lhs.  We should be able to
450         -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
451
452 setOneShotLambda :: Id -> Id
453 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
454
455 clearOneShotLambda :: Id -> Id
456 clearOneShotLambda id 
457   | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
458   | otherwise          = id                     
459
460 -- But watch out: this may change the type of something else
461 --      f = \x -> e
462 -- If we change the one-shot-ness of x, f's type changes
463 \end{code}
464
465 \begin{code}
466 zapLamIdInfo :: Id -> Id
467 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id
468
469 zapDemandIdInfo id = maybeModifyIdInfo zapDemandInfo id
470 \end{code}