Tidy up the treatment of dead binders
[ghc-hetmet.git] / compiler / basicTypes / Id.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5 \section[Id]{@Ids@: Value and constructor identifiers}
6
7 \begin{code}
8 -- |
9 -- #name_types#
10 -- GHC uses several kinds of name internally:
11 --
12 -- * 'OccName.OccName': see "OccName#name_types"
13 --
14 -- * 'RdrName.RdrName': see "RdrName#name_types"
15 --
16 -- * 'Name.Name': see "Name#name_types"
17 --
18 -- * 'Id.Id' represents names that not only have a 'Name.Name' but also a 'TypeRep.Type' and some additional
19 --   details (a 'IdInfo.IdInfo' and one of 'Var.LocalIdDetails' or 'IdInfo.GlobalIdDetails') that
20 --   are added, modified and inspected by various compiler passes. These 'Var.Var' names may either 
21 --   be global or local, see "Var#globalvslocal"
22 --
23 -- * 'Var.Var': see "Var#name_types"
24 module Id (
25         -- * The main types
26         Id, DictId,
27
28         -- ** Simple construction
29         mkGlobalId, mkVanillaGlobal, mkVanillaGlobalWithInfo,
30         mkLocalId, mkLocalIdWithInfo,
31         mkSysLocal, mkSysLocalM, mkUserLocal, mkUserLocalM,
32         mkTemplateLocals, mkTemplateLocalsNum, mkTemplateLocal,
33         mkWorkerId, mkExportedLocalId,
34
35         -- ** Taking an Id apart
36         idName, idType, idUnique, idInfo,
37         isId, globalIdDetails, idPrimRep,
38         recordSelectorFieldLabel,
39
40         -- ** Modifying an Id
41         setIdName, setIdUnique, Id.setIdType, 
42         setIdExported, setIdNotExported, 
43         globaliseId, localiseId, 
44         setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
45         zapLamIdInfo, zapDemandIdInfo, zapFragileIdInfo, transferPolyIdInfo,
46         
47
48         -- ** Predicates on Ids
49         isImplicitId, isDeadBinder, isDictId, isStrictId,
50         isExportedId, isLocalId, isGlobalId,
51         isRecordSelector, isNaughtyRecordSelector,
52         isClassOpId_maybe,
53         isPrimOpId, isPrimOpId_maybe, 
54         isFCallId, isFCallId_maybe,
55         isDataConWorkId, isDataConWorkId_maybe, isDataConId_maybe, idDataCon,
56         isBottomingId, idIsFrom,
57         isTickBoxOp, isTickBoxOp_maybe,
58         hasNoBinding, 
59
60         -- ** Inline pragma stuff
61         idInlinePragma, setInlinePragma, modifyInlinePragma, 
62
63         -- ** One-shot lambdas
64         isOneShotBndr, isOneShotLambda, isStateHackType,
65         setOneShotLambda, clearOneShotLambda,
66
67         -- ** Reading 'IdInfo' fields
68         idArity, 
69         idNewDemandInfo, idNewDemandInfo_maybe,
70         idNewStrictness, idNewStrictness_maybe, 
71         idWorkerInfo,
72         idUnfolding,
73         idSpecialisation, idCoreRules, idHasRules,
74         idCafInfo,
75         idLBVarInfo,
76         idOccInfo,
77
78 #ifdef OLD_STRICTNESS
79         idDemandInfo, 
80         idStrictness, 
81         idCprInfo,
82 #endif
83
84         -- ** Writing 'IdInfo' fields
85         setIdUnfolding,
86         setIdArity,
87         setIdNewDemandInfo, 
88         setIdNewStrictness, zapIdNewStrictness,
89         setIdWorkerInfo,
90         setIdSpecialisation,
91         setIdCafInfo,
92         setIdOccInfo, zapIdOccInfo,
93
94 #ifdef OLD_STRICTNESS
95         setIdStrictness, 
96         setIdDemandInfo, 
97         setIdCprInfo,
98 #endif
99     ) where
100
101 #include "HsVersions.h"
102
103 import CoreSyn ( CoreRule, Unfolding )
104
105 import IdInfo
106 import BasicTypes
107 import qualified Var
108 import Var
109 import TyCon
110 import Type
111 import TcType
112 import TysPrim
113 #ifdef OLD_STRICTNESS
114 import qualified Demand
115 #endif
116 import DataCon
117 import NewDemand
118 import Name
119 import Module
120 import Class
121 import PrimOp
122 import ForeignCall
123 import OccName
124 import Maybes
125 import SrcLoc
126 import Outputable
127 import Unique
128 import UniqSupply
129 import FastString
130 import StaticFlags
131
132 -- infixl so you can say (id `set` a `set` b)
133 infixl  1 `setIdUnfolding`,
134           `setIdArity`,
135           `setIdNewDemandInfo`,
136           `setIdNewStrictness`,
137           `setIdWorkerInfo`,
138           `setIdSpecialisation`,
139           `setInlinePragma`,
140           `idCafInfo`
141 #ifdef OLD_STRICTNESS
142           ,`idCprInfo`
143           ,`setIdStrictness`
144           ,`setIdDemandInfo`
145 #endif
146 \end{code}
147
148 %************************************************************************
149 %*                                                                      *
150 \subsection{Basic Id manipulation}
151 %*                                                                      *
152 %************************************************************************
153
154 \begin{code}
155 idName   :: Id -> Name
156 idName    = Var.varName
157
158 idUnique :: Id -> Unique
159 idUnique  = varUnique
160
161 idType   :: Id -> Kind
162 idType    = varType
163
164 idInfo :: Id -> IdInfo
165 idInfo = varIdInfo
166
167 idPrimRep :: Id -> PrimRep
168 idPrimRep id = typePrimRep (idType id)
169
170 globalIdDetails :: Id -> GlobalIdDetails
171 globalIdDetails = globalIdVarDetails
172
173
174 setIdName :: Id -> Name -> Id
175 setIdName = setVarName
176
177 setIdUnique :: Id -> Unique -> Id
178 setIdUnique = setVarUnique
179
180 -- | Not only does this set the 'Id' 'Type', it also evaluates the type to try and
181 -- reduce space usage
182 setIdType :: Id -> Type -> Id
183 setIdType id ty = seqType ty `seq` Var.setVarType id ty
184
185 setIdExported :: Id -> Id
186 setIdExported = setIdVarExported
187
188 setIdNotExported :: Id -> Id
189 setIdNotExported = setIdVarNotExported
190
191 localiseId :: Id -> Id
192 -- Make an with the same unique and type as the 
193 -- incoming Id, but with an *Internal* Name and *LocalId* flavour
194 localiseId id 
195   | isLocalId id && isInternalName name
196   = id
197   | otherwise
198   = mkLocalIdWithInfo (localiseName name) (idType id) (idInfo id)
199   where
200     name = idName id
201
202 globaliseId :: GlobalIdDetails -> Id -> Id
203 globaliseId = globaliseIdVar
204
205 lazySetIdInfo :: Id -> IdInfo -> Id
206 lazySetIdInfo = lazySetVarIdInfo
207
208 setIdInfo :: Id -> IdInfo -> Id
209 setIdInfo id info = seqIdInfo info `seq` (lazySetIdInfo id info)
210         -- Try to avoid spack leaks by seq'ing
211
212 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
213 modifyIdInfo fn id = setIdInfo id (fn (idInfo id))
214
215 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
216 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
217 maybeModifyIdInfo (Just new_info) id = lazySetIdInfo id new_info
218 maybeModifyIdInfo Nothing         id = id
219 \end{code}
220
221 %************************************************************************
222 %*                                                                      *
223 \subsection{Simple Id construction}
224 %*                                                                      *
225 %************************************************************************
226
227 Absolutely all Ids are made by mkId.  It is just like Var.mkId,
228 but in addition it pins free-tyvar-info onto the Id's type, 
229 where it can easily be found.
230
231 Note [Free type variables]
232 ~~~~~~~~~~~~~~~~~~~~~~~~~~
233 At one time we cached the free type variables of the type of an Id
234 at the root of the type in a TyNote.  The idea was to avoid repeating
235 the free-type-variable calculation.  But it turned out to slow down
236 the compiler overall. I don't quite know why; perhaps finding free
237 type variables of an Id isn't all that common whereas applying a 
238 substitution (which changes the free type variables) is more common.
239 Anyway, we removed it in March 2008.
240
241 \begin{code}
242 -- | For an explanation of global vs. local 'Id's, see "Var#globalvslocal"
243 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
244 mkGlobalId = mkGlobalIdVar
245
246 -- | Make a global 'Id' without any extra information at all
247 mkVanillaGlobal :: Name -> Type -> Id
248 mkVanillaGlobal name ty = mkVanillaGlobalWithInfo name ty vanillaIdInfo
249
250 -- | Make a global 'Id' with no global information but some generic 'IdInfo'
251 mkVanillaGlobalWithInfo :: Name -> Type -> IdInfo -> Id
252 mkVanillaGlobalWithInfo = mkGlobalId VanillaGlobal
253
254
255 -- | For an explanation of global vs. local 'Id's, see "Var#globalvslocal"
256 mkLocalId :: Name -> Type -> Id
257 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
258
259 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
260 mkLocalIdWithInfo = mkLocalIdVar
261         -- Note [Free type variables]
262
263 -- | Create a local 'Id' that is marked as exported. This prevents things attached to it from being removed as dead code.
264 mkExportedLocalId :: Name -> Type -> Id
265 mkExportedLocalId name ty = mkExportedLocalIdVar name ty vanillaIdInfo
266         -- Note [Free type variables]
267
268
269 -- | Create a system local 'Id'. These are local 'Id's (see "Var#globalvslocal") that are created by the compiler out of thin air
270 mkSysLocal :: FastString -> Unique -> Type -> Id
271 mkSysLocal fs uniq ty = mkLocalId (mkSystemVarName uniq fs) ty
272
273 mkSysLocalM :: MonadUnique m => FastString -> Type -> m Id
274 mkSysLocalM fs ty = getUniqueM >>= (\uniq -> return (mkSysLocal fs uniq ty))
275
276
277 -- | Create a user local 'Id'. These are local 'Id's (see "Var#globalvslocal") with a name and location that the user might recognize
278 mkUserLocal :: OccName -> Unique -> Type -> SrcSpan -> Id
279 mkUserLocal occ uniq ty loc = mkLocalId (mkInternalName uniq occ loc) ty
280
281 mkUserLocalM :: MonadUnique m => OccName -> Type -> SrcSpan -> m Id
282 mkUserLocalM occ ty loc = getUniqueM >>= (\uniq -> return (mkUserLocal occ uniq ty loc))
283
284 \end{code}
285
286 Make some local @Ids@ for a template @CoreExpr@.  These have bogus
287 @Uniques@, but that's OK because the templates are supposed to be
288 instantiated before use.
289  
290 \begin{code}
291 -- | Workers get local names. "CoreTidy" will externalise these if necessary
292 mkWorkerId :: Unique -> Id -> Type -> Id
293 mkWorkerId uniq unwrkr ty
294   = mkLocalId wkr_name ty
295   where
296     wkr_name = mkInternalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcSpan unwrkr)
297
298 -- | Create a /template local/: a family of system local 'Id's in bijection with @Int@s, typically used in unfoldings
299 mkTemplateLocal :: Int -> Type -> Id
300 mkTemplateLocal i ty = mkSysLocal (fsLit "tpl") (mkBuiltinUnique i) ty
301
302 -- | Create a template local for a series of types
303 mkTemplateLocals :: [Type] -> [Id]
304 mkTemplateLocals = mkTemplateLocalsNum 1
305
306 -- | Create a template local for a series of type, but start from a specified template local
307 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
308 mkTemplateLocalsNum n tys = zipWith mkTemplateLocal [n..] tys
309 \end{code}
310
311
312 %************************************************************************
313 %*                                                                      *
314 \subsection{Basic predicates on @Id@s}
315 %*                                                                      *
316 %************************************************************************
317
318 \begin{code}
319 isId :: Id -> Bool
320 isId = isIdVar
321
322 -- | For an explanation of global vs. local 'Id's, see "Var#globalvslocal"
323 isLocalId :: Id -> Bool
324 isLocalId = isLocalIdVar
325
326 -- | For an explanation of global vs. local 'Id's, see "Var#globalvslocal"
327 isGlobalId :: Id -> Bool
328 isGlobalId = isGlobalIdVar
329
330 -- | Determines whether an 'Id' is marked as exported and hence will not be considered dead code
331 isExportedId :: Id -> Bool
332 isExportedId = isExportedIdVar
333 \end{code}
334
335 %************************************************************************
336 %*                                                                      *
337 \subsection{Special Ids}
338 %*                                                                      *
339 %************************************************************************
340
341 \begin{code}
342 -- | If the 'Id' is that for a record selector, extract the 'sel_tycon' and label. Panic otherwise
343 recordSelectorFieldLabel :: Id -> (TyCon, FieldLabel)
344 recordSelectorFieldLabel id
345   = case globalIdDetails id of
346         RecordSelId { sel_tycon = tycon, sel_label = lbl } -> (tycon,lbl)
347         _ -> panic "recordSelectorFieldLabel"
348
349 isRecordSelector        :: Id -> Bool
350 isNaughtyRecordSelector :: Id -> Bool
351 isPrimOpId              :: Id -> Bool
352 isFCallId               :: Id -> Bool
353 isDataConWorkId         :: Id -> Bool
354
355 isClassOpId_maybe       :: Id -> Maybe Class
356 isPrimOpId_maybe        :: Id -> Maybe PrimOp
357 isFCallId_maybe         :: Id -> Maybe ForeignCall
358 isDataConWorkId_maybe   :: Id -> Maybe DataCon
359
360 isRecordSelector id = case globalIdDetails id of
361                         RecordSelId {}  -> True
362                         _               -> False
363
364 isNaughtyRecordSelector id = case globalIdDetails id of
365                         RecordSelId { sel_naughty = n } -> n
366                         _                               -> False
367
368 isClassOpId_maybe id = case globalIdDetails id of
369                         ClassOpId cls -> Just cls
370                         _other        -> Nothing
371
372 isPrimOpId id = case globalIdDetails id of
373                         PrimOpId _ -> True
374                         _          -> False
375
376 isPrimOpId_maybe id = case globalIdDetails id of
377                         PrimOpId op -> Just op
378                         _           -> Nothing
379
380 isFCallId id = case globalIdDetails id of
381                         FCallId _ -> True
382                         _         -> False
383
384 isFCallId_maybe id = case globalIdDetails id of
385                         FCallId call -> Just call
386                         _            -> Nothing
387
388 isDataConWorkId id = case globalIdDetails id of
389                         DataConWorkId _ -> True
390                         _               -> False
391
392 isDataConWorkId_maybe id = case globalIdDetails id of
393                         DataConWorkId con -> Just con
394                         _                 -> Nothing
395
396 isDataConId_maybe :: Id -> Maybe DataCon
397 isDataConId_maybe id = case globalIdDetails id of
398                          DataConWorkId con -> Just con
399                          DataConWrapId con -> Just con
400                          _                 -> Nothing
401
402 idDataCon :: Id -> DataCon
403 -- ^ Get from either the worker or the wrapper 'Id' to the 'DataCon'. Currently used only in the desugarer.
404 --
405 -- INVARIANT: @idDataCon (dataConWrapId d) = d@: remember, 'dataConWrapId' can return either the wrapper or the worker
406 idDataCon id = isDataConId_maybe id `orElse` pprPanic "idDataCon" (ppr id)
407
408
409 isDictId :: Id -> Bool
410 isDictId id = isDictTy (idType id)
411
412 hasNoBinding :: Id -> Bool
413 -- ^ Returns @True@ of an 'Id' which may not have a
414 -- binding, even though it is defined in this module.
415
416 -- Data constructor workers used to be things of this kind, but
417 -- they aren't any more.  Instead, we inject a binding for 
418 -- them at the CorePrep stage. 
419 -- EXCEPT: unboxed tuples, which definitely have no binding
420 hasNoBinding id = case globalIdDetails id of
421                         PrimOpId _       -> True        -- See Note [Primop wrappers]
422                         FCallId _        -> True
423                         DataConWorkId dc -> isUnboxedTupleCon dc
424                         _                -> False
425
426 isImplicitId :: Id -> Bool
427 -- ^ 'isImplicitId' tells whether an 'Id's info is implied by other
428 -- declarations, so we don't need to put its signature in an interface
429 -- file, even if it's mentioned in some other interface unfolding.
430 isImplicitId id
431   = case globalIdDetails id of
432         RecordSelId {}  -> True
433         FCallId _       -> True
434         PrimOpId _      -> True
435         ClassOpId _     -> True
436         DataConWorkId _ -> True
437         DataConWrapId _ -> True
438                 -- These are are implied by their type or class decl;
439                 -- remember that all type and class decls appear in the interface file.
440                 -- The dfun id is not an implicit Id; it must *not* be omitted, because 
441                 -- it carries version info for the instance decl
442         _               -> False
443
444 idIsFrom :: Module -> Id -> Bool
445 idIsFrom mod id = nameIsLocalOrFrom mod (idName id)
446 \end{code}
447
448 Note [Primop wrappers]
449 ~~~~~~~~~~~~~~~~~~~~~~
450 Currently hasNoBinding claims that PrimOpIds don't have a curried
451 function definition.  But actually they do, in GHC.PrimopWrappers,
452 which is auto-generated from prelude/primops.txt.pp.  So actually, hasNoBinding
453 could return 'False' for PrimOpIds.
454
455 But we'd need to add something in CoreToStg to swizzle any unsaturated
456 applications of GHC.Prim.plusInt# to GHC.PrimopWrappers.plusInt#.
457
458 Nota Bene: GHC.PrimopWrappers is needed *regardless*, because it's
459 used by GHCi, which does not implement primops direct at all.
460
461
462
463 \begin{code}
464 isDeadBinder :: Id -> Bool
465 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
466                   | otherwise = False   -- TyVars count as not dead
467 \end{code}
468
469 \begin{code}
470 isTickBoxOp :: Id -> Bool
471 isTickBoxOp id = 
472   case globalIdDetails id of
473     TickBoxOpId _    -> True
474     _                -> False
475
476 isTickBoxOp_maybe :: Id -> Maybe TickBoxOp
477 isTickBoxOp_maybe id = 
478   case globalIdDetails id of
479     TickBoxOpId tick -> Just tick
480     _                -> Nothing
481 \end{code}
482
483 %************************************************************************
484 %*                                                                      *
485 \subsection{IdInfo stuff}
486 %*                                                                      *
487 %************************************************************************
488
489 \begin{code}
490         ---------------------------------
491         -- ARITY
492 idArity :: Id -> Arity
493 idArity id = arityInfo (idInfo id)
494
495 setIdArity :: Id -> Arity -> Id
496 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
497
498 #ifdef OLD_STRICTNESS
499         ---------------------------------
500         -- (OLD) STRICTNESS 
501 idStrictness :: Id -> StrictnessInfo
502 idStrictness id = strictnessInfo (idInfo id)
503
504 setIdStrictness :: Id -> StrictnessInfo -> Id
505 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
506 #endif
507
508 -- | Returns true if an application to n args would diverge
509 isBottomingId :: Id -> Bool
510 isBottomingId id = isBottomingSig (idNewStrictness id)
511
512 idNewStrictness_maybe :: Id -> Maybe StrictSig
513 idNewStrictness :: Id -> StrictSig
514
515 idNewStrictness_maybe id = newStrictnessInfo (idInfo id)
516 idNewStrictness       id = idNewStrictness_maybe id `orElse` topSig
517
518 setIdNewStrictness :: Id -> StrictSig -> Id
519 setIdNewStrictness id sig = modifyIdInfo (`setNewStrictnessInfo` Just sig) id
520
521 zapIdNewStrictness :: Id -> Id
522 zapIdNewStrictness id = modifyIdInfo (`setNewStrictnessInfo` Nothing) id
523
524 -- | This predicate says whether the 'Id' has a strict demand placed on it or
525 -- has a type such that it can always be evaluated strictly (e.g., an
526 -- unlifted type, but see the comment for 'isStrictType').  We need to
527 -- check separately whether the 'Id' has a so-called \"strict type\" because if
528 -- the demand for the given @id@ hasn't been computed yet but @id@ has a strict
529 -- type, we still want @isStrictId id@ to be @True@.
530 isStrictId :: Id -> Bool
531 isStrictId id
532   = ASSERT2( isId id, text "isStrictId: not an id: " <+> ppr id )
533            (isStrictDmd (idNewDemandInfo id)) || 
534            (isStrictType (idType id))
535
536         ---------------------------------
537         -- WORKER ID
538 idWorkerInfo :: Id -> WorkerInfo
539 idWorkerInfo id = workerInfo (idInfo id)
540
541 setIdWorkerInfo :: Id -> WorkerInfo -> Id
542 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
543
544         ---------------------------------
545         -- UNFOLDING
546 idUnfolding :: Id -> Unfolding
547 idUnfolding id = unfoldingInfo (idInfo id)
548
549 setIdUnfolding :: Id -> Unfolding -> Id
550 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
551
552 #ifdef OLD_STRICTNESS
553         ---------------------------------
554         -- (OLD) DEMAND
555 idDemandInfo :: Id -> Demand.Demand
556 idDemandInfo id = demandInfo (idInfo id)
557
558 setIdDemandInfo :: Id -> Demand.Demand -> Id
559 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
560 #endif
561
562 idNewDemandInfo_maybe :: Id -> Maybe NewDemand.Demand
563 idNewDemandInfo       :: Id -> NewDemand.Demand
564
565 idNewDemandInfo_maybe id = newDemandInfo (idInfo id)
566 idNewDemandInfo       id = newDemandInfo (idInfo id) `orElse` NewDemand.topDmd
567
568 setIdNewDemandInfo :: Id -> NewDemand.Demand -> Id
569 setIdNewDemandInfo id dmd = modifyIdInfo (`setNewDemandInfo` Just dmd) id
570
571         ---------------------------------
572         -- SPECIALISATION
573 idSpecialisation :: Id -> SpecInfo
574 idSpecialisation id = specInfo (idInfo id)
575
576 idCoreRules :: Id -> [CoreRule]
577 idCoreRules id = specInfoRules (idSpecialisation id)
578
579 idHasRules :: Id -> Bool
580 idHasRules id = not (isEmptySpecInfo (idSpecialisation id))
581
582 setIdSpecialisation :: Id -> SpecInfo -> Id
583 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
584
585         ---------------------------------
586         -- CAF INFO
587 idCafInfo :: Id -> CafInfo
588 #ifdef OLD_STRICTNESS
589 idCafInfo id = case cgInfo (idInfo id) of
590                   NoCgInfo -> pprPanic "idCafInfo" (ppr id)
591                   info     -> cgCafInfo info
592 #else
593 idCafInfo id = cafInfo (idInfo id)
594 #endif
595
596 setIdCafInfo :: Id -> CafInfo -> Id
597 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
598
599         ---------------------------------
600         -- CPR INFO
601 #ifdef OLD_STRICTNESS
602 idCprInfo :: Id -> CprInfo
603 idCprInfo id = cprInfo (idInfo id)
604
605 setIdCprInfo :: Id -> CprInfo -> Id
606 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
607 #endif
608
609         ---------------------------------
610         -- Occcurrence INFO
611 idOccInfo :: Id -> OccInfo
612 idOccInfo id = occInfo (idInfo id)
613
614 setIdOccInfo :: Id -> OccInfo -> Id
615 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
616
617 zapIdOccInfo :: Id -> Id
618 zapIdOccInfo b = b `setIdOccInfo` NoOccInfo
619 \end{code}
620
621
622         ---------------------------------
623         -- INLINING
624 The inline pragma tells us to be very keen to inline this Id, but it's still
625 OK not to if optimisation is switched off.
626
627 \begin{code}
628 idInlinePragma :: Id -> InlinePragInfo
629 idInlinePragma id = inlinePragInfo (idInfo id)
630
631 setInlinePragma :: Id -> InlinePragInfo -> Id
632 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
633
634 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
635 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
636 \end{code}
637
638
639         ---------------------------------
640         -- ONE-SHOT LAMBDAS
641 \begin{code}
642 idLBVarInfo :: Id -> LBVarInfo
643 idLBVarInfo id = lbvarInfo (idInfo id)
644
645 -- | Returns whether the lambda associated with the 'Id' is certainly applied at most once
646 -- OR we are applying the \"state hack\" which makes it appear as if theis is the case for
647 -- lambdas used in @IO@. You should prefer using this over 'isOneShotLambda'
648 isOneShotBndr :: Id -> Bool
649 -- This one is the "business end", called externally.
650 -- Its main purpose is to encapsulate the Horrible State Hack
651 isOneShotBndr id = isOneShotLambda id || isStateHackType (idType id)
652
653 -- | Should we apply the state hack to values of this 'Type'?
654 isStateHackType :: Type -> Bool
655 isStateHackType ty
656   | opt_NoStateHack 
657   = False
658   | otherwise
659   = case splitTyConApp_maybe ty of
660         Just (tycon,_) -> tycon == statePrimTyCon
661         _              -> False
662         -- This is a gross hack.  It claims that 
663         -- every function over realWorldStatePrimTy is a one-shot
664         -- function.  This is pretty true in practice, and makes a big
665         -- difference.  For example, consider
666         --      a `thenST` \ r -> ...E...
667         -- The early full laziness pass, if it doesn't know that r is one-shot
668         -- will pull out E (let's say it doesn't mention r) to give
669         --      let lvl = E in a `thenST` \ r -> ...lvl...
670         -- When `thenST` gets inlined, we end up with
671         --      let lvl = E in \s -> case a s of (r, s') -> ...lvl...
672         -- and we don't re-inline E.
673         --
674         -- It would be better to spot that r was one-shot to start with, but
675         -- I don't want to rely on that.
676         --
677         -- Another good example is in fill_in in PrelPack.lhs.  We should be able to
678         -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
679
680
681 -- | Returns whether the lambda associated with the 'Id' is certainly applied at most once.
682 -- You probably want to use 'isOneShotBndr' instead
683 isOneShotLambda :: Id -> Bool
684 isOneShotLambda id = case idLBVarInfo id of
685                        IsOneShotLambda  -> True
686                        NoLBVarInfo      -> False
687
688 setOneShotLambda :: Id -> Id
689 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
690
691 clearOneShotLambda :: Id -> Id
692 clearOneShotLambda id 
693   | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
694   | otherwise          = id                     
695
696 -- The OneShotLambda functions simply fiddle with the IdInfo flag
697 -- But watch out: this may change the type of something else
698 --      f = \x -> e
699 -- If we change the one-shot-ness of x, f's type changes
700 \end{code}
701
702 \begin{code}
703 zapInfo :: (IdInfo -> Maybe IdInfo) -> Id -> Id
704 zapInfo zapper id = maybeModifyIdInfo (zapper (idInfo id)) id
705
706 zapLamIdInfo :: Id -> Id
707 zapLamIdInfo = zapInfo zapLamInfo
708
709 zapDemandIdInfo :: Id -> Id
710 zapDemandIdInfo = zapInfo zapDemandInfo
711
712 zapFragileIdInfo :: Id -> Id
713 zapFragileIdInfo = zapInfo zapFragileInfo 
714 \end{code}
715
716 Note [transferPolyIdInfo]
717 ~~~~~~~~~~~~~~~~~~~~~~~~~
718 Suppose we have
719
720    f = /\a. let g = rhs in ...
721
722 where g has interesting strictness information.  Then if we float thus
723
724    g' = /\a. rhs
725    f = /\a. ...[g' a/g]
726
727 we *do not* want to lose the strictness information on g.  Nor arity.
728
729 It's simple to retain strictness and arity, but not so simple to retain
730         worker info
731         rules
732 so we simply discard those.  Sooner or later this may bite us.
733
734 This transfer is used in two places: 
735         FloatOut (long-distance let-floating)
736         SimplUtils.abstractFloats (short-distance let-floating)
737
738 \begin{code}
739 transferPolyIdInfo :: Id -> Id -> Id
740 transferPolyIdInfo old_id new_id
741   = modifyIdInfo transfer new_id
742   where
743     old_info = idInfo old_id
744     transfer new_info = new_info `setNewStrictnessInfo` (newStrictnessInfo old_info)
745                                  `setArityInfo` (arityInfo old_info)
746 \end{code}