Fixed warnings in basicTypes/Id
[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 module Id (
9         Id, DictId,
10
11         -- Simple construction
12         mkGlobalId, mkLocalId, mkLocalIdWithInfo, 
13         mkSysLocal, mkUserLocal, mkVanillaGlobal,
14         mkTemplateLocals, mkTemplateLocalsNum, mkWildId, mkTemplateLocal,
15         mkWorkerId, mkExportedLocalId,
16
17         -- Taking an Id apart
18         idName, idType, idUnique, idInfo,
19         isId, globalIdDetails, idPrimRep,
20         recordSelectorFieldLabel,
21
22         -- Modifying an Id
23         setIdName, setIdUnique, Id.setIdType, setIdExported, setIdNotExported, 
24         setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
25         zapLamIdInfo, zapDemandIdInfo, zapFragileIdInfo,
26
27         -- Predicates
28         isImplicitId, isDeadBinder, isDictId, isStrictId,
29         isExportedId, isLocalId, isGlobalId,
30         isRecordSelector, isNaughtyRecordSelector,
31         isClassOpId_maybe,
32         isPrimOpId, isPrimOpId_maybe, 
33         isFCallId, isFCallId_maybe,
34         isDataConWorkId, isDataConWorkId_maybe, isDataConId_maybe, idDataCon,
35         isBottomingId, idIsFrom,
36         isTickBoxOp, isTickBoxOp_maybe,
37         hasNoBinding, 
38
39         -- Inline pragma stuff
40         idInlinePragma, setInlinePragma, modifyInlinePragma, 
41
42
43         -- One shot lambda stuff
44         isOneShotBndr, isOneShotLambda, isStateHackType,
45         setOneShotLambda, clearOneShotLambda,
46
47         -- IdInfo stuff
48         setIdUnfolding,
49         setIdArity,
50         setIdNewDemandInfo, 
51         setIdNewStrictness, zapIdNewStrictness,
52         setIdWorkerInfo,
53         setIdSpecialisation,
54         setIdCafInfo,
55         setIdOccInfo,
56
57 #ifdef OLD_STRICTNESS
58         idDemandInfo, 
59         idStrictness, 
60         idCprInfo,
61         setIdStrictness, 
62         setIdDemandInfo, 
63         setIdCprInfo,
64 #endif
65
66         idArity, 
67         idNewDemandInfo, idNewDemandInfo_maybe,
68         idNewStrictness, idNewStrictness_maybe, 
69         idWorkerInfo,
70         idUnfolding,
71         idSpecialisation, idCoreRules, idHasRules,
72         idCafInfo,
73         idLBVarInfo,
74         idOccInfo,
75
76 #ifdef OLD_STRICTNESS
77         newStrictnessFromOld    -- Temporary
78 #endif
79
80     ) where
81
82 #include "HsVersions.h"
83
84 import CoreSyn
85 import BasicTypes
86 import qualified Var
87 import Var hiding (mkLocalId, mkGlobalId, mkExportedLocalId)
88 import TyCon
89 import Type
90 import TcType
91 import TysPrim
92 import IdInfo 
93 #ifdef OLD_STRICTNESS
94 import qualified Demand
95 #endif
96 import DataCon
97 import NewDemand
98 import Name
99 import Module
100 import Class
101 import PrimOp
102 import ForeignCall
103 import OccName
104 import Maybes
105 import SrcLoc
106 import Outputable
107 import Unique
108 import FastString
109 import StaticFlags
110
111 -- infixl so you can say (id `set` a `set` b)
112 infixl  1 `setIdUnfolding`,
113           `setIdArity`,
114           `setIdNewDemandInfo`,
115           `setIdNewStrictness`,
116           `setIdWorkerInfo`,
117           `setIdSpecialisation`,
118           `setInlinePragma`,
119           `idCafInfo`
120 #ifdef OLD_STRICTNESS
121           ,`idCprInfo`
122           ,`setIdStrictness`
123           ,`setIdDemandInfo`
124 #endif
125 \end{code}
126
127
128
129 %************************************************************************
130 %*                                                                      *
131 \subsection{Simple Id construction}
132 %*                                                                      *
133 %************************************************************************
134
135 Absolutely all Ids are made by mkId.  It is just like Var.mkId,
136 but in addition it pins free-tyvar-info onto the Id's type, 
137 where it can easily be found.
138
139 \begin{code}
140 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
141 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
142
143 mkExportedLocalId :: Name -> Type -> Id
144 mkExportedLocalId name ty = Var.mkExportedLocalId name (addFreeTyVars ty) vanillaIdInfo
145
146 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
147 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
148 \end{code}
149
150 \begin{code}
151 mkLocalId :: Name -> Type -> Id
152 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
153
154 -- SysLocal: for an Id being created by the compiler out of thin air...
155 mkSysLocal :: FastString -> Unique -> Type -> Id
156 mkSysLocal fs uniq ty = mkLocalId (mkSystemVarName uniq fs) ty
157
158
159 -- UserLocal: an Id with a name the user might recognize...
160 mkUserLocal :: OccName -> Unique -> Type -> SrcSpan -> Id
161 mkVanillaGlobal :: Name -> Type -> IdInfo -> Id
162
163 mkUserLocal occ uniq ty loc = mkLocalId (mkInternalName    uniq occ loc) ty
164 mkVanillaGlobal             = mkGlobalId VanillaGlobal
165 \end{code}
166
167 Make some local @Ids@ for a template @CoreExpr@.  These have bogus
168 @Uniques@, but that's OK because the templates are supposed to be
169 instantiated before use.
170  
171 \begin{code}
172 -- "Wild Id" typically used when you need a binder that you don't expect to use
173 mkWildId :: Type -> Id
174 mkWildId ty = mkSysLocal FSLIT("wild") (mkBuiltinUnique 1) ty
175
176 mkWorkerId :: Unique -> Id -> Type -> Id
177 -- A worker gets a local name.  CoreTidy will externalise it if necessary.
178 mkWorkerId uniq unwrkr ty
179   = mkLocalId wkr_name ty
180   where
181     wkr_name = mkInternalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcSpan unwrkr)
182
183 -- "Template locals" typically used in unfoldings
184 mkTemplateLocals :: [Type] -> [Id]
185 mkTemplateLocals tys = zipWith mkTemplateLocal [1..] tys
186
187 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
188 -- The Int gives the starting point for unique allocation
189 mkTemplateLocalsNum n tys = zipWith mkTemplateLocal [n..] tys
190
191 mkTemplateLocal :: Int -> Type -> Id
192 mkTemplateLocal i ty = mkSysLocal FSLIT("tpl") (mkBuiltinUnique i) ty
193 \end{code}
194
195
196 %************************************************************************
197 %*                                                                      *
198 \subsection[Id-general-funs]{General @Id@-related functions}
199 %*                                                                      *
200 %************************************************************************
201
202 \begin{code}
203 setIdType :: Id -> Type -> Id
204         -- Add free tyvar info to the type
205 setIdType id ty = seqType ty `seq` Var.setIdType id (addFreeTyVars ty)
206
207 idPrimRep :: Id -> PrimRep
208 idPrimRep id = typePrimRep (idType id)
209 \end{code}
210
211
212 %************************************************************************
213 %*                                                                      *
214 \subsection{Special Ids}
215 %*                                                                      *
216 %************************************************************************
217
218 \begin{code}
219 recordSelectorFieldLabel :: Id -> (TyCon, FieldLabel)
220 recordSelectorFieldLabel id
221   = case globalIdDetails id of
222         RecordSelId { sel_tycon = tycon, sel_label = lbl } -> (tycon,lbl)
223         _ -> panic "recordSelectorFieldLabel"
224
225 isRecordSelector        :: Var -> Bool
226 isNaughtyRecordSelector :: Var -> Bool
227 isPrimOpId              :: Var -> Bool
228 isFCallId               :: Var -> Bool
229 isDataConWorkId         :: Var -> Bool
230 hasNoBinding            :: Var -> Bool
231
232 isClassOpId_maybe       :: Var -> Maybe Class
233 isPrimOpId_maybe        :: Var -> Maybe PrimOp
234 isFCallId_maybe         :: Var -> Maybe ForeignCall
235 isDataConWorkId_maybe   :: Var -> Maybe DataCon
236
237 isRecordSelector id = case globalIdDetails id of
238                         RecordSelId {}  -> True
239                         _               -> False
240
241 isNaughtyRecordSelector id = case globalIdDetails id of
242                         RecordSelId { sel_naughty = n } -> n
243                         _                               -> False
244
245 isClassOpId_maybe id = case globalIdDetails id of
246                         ClassOpId cls -> Just cls
247                         _other        -> Nothing
248
249 isPrimOpId id = case globalIdDetails id of
250                         PrimOpId _ -> True
251                         _          -> False
252
253 isPrimOpId_maybe id = case globalIdDetails id of
254                         PrimOpId op -> Just op
255                         _           -> Nothing
256
257 isFCallId id = case globalIdDetails id of
258                         FCallId _ -> True
259                         _         -> False
260
261 isFCallId_maybe id = case globalIdDetails id of
262                         FCallId call -> Just call
263                         _            -> Nothing
264
265 isDataConWorkId id = case globalIdDetails id of
266                         DataConWorkId _ -> True
267                         _               -> False
268
269 isDataConWorkId_maybe id = case globalIdDetails id of
270                         DataConWorkId con -> Just con
271                         _                 -> Nothing
272
273 isDataConId_maybe :: Id -> Maybe DataCon
274 isDataConId_maybe id = case globalIdDetails id of
275                          DataConWorkId con -> Just con
276                          DataConWrapId con -> Just con
277                          _                 -> Nothing
278
279 idDataCon :: Id -> DataCon
280 -- Get from either the worker or the wrapper to the DataCon
281 -- Currently used only in the desugarer
282 --       INVARIANT: idDataCon (dataConWrapId d) = d
283 -- (Remember, dataConWrapId can return either the wrapper or the worker.)
284 idDataCon id = case globalIdDetails id of
285                   DataConWorkId con -> con
286                   DataConWrapId con -> con
287                   _                 -> pprPanic "idDataCon" (ppr id)
288
289
290 isDictId :: Id -> Bool
291 isDictId id = isDictTy (idType id)
292
293 -- hasNoBinding returns True of an Id which may not have a
294 -- binding, even though it is defined in this module.  
295 -- Data constructor workers used to be things of this kind, but
296 -- they aren't any more.  Instead, we inject a binding for 
297 -- them at the CorePrep stage. 
298 -- EXCEPT: unboxed tuples, which definitely have no binding
299 hasNoBinding id = case globalIdDetails id of
300                         PrimOpId _       -> True        -- See Note [Primop wrappers]
301                         FCallId _        -> True
302                         DataConWorkId dc -> isUnboxedTupleCon dc
303                         _                -> False
304
305 isImplicitId :: Id -> Bool
306         -- isImplicitId tells whether an Id's info is implied by other
307         -- declarations, so we don't need to put its signature in an interface
308         -- file, even if it's mentioned in some other interface unfolding.
309 isImplicitId id
310   = case globalIdDetails id of
311         RecordSelId {}  -> True
312         FCallId _       -> True
313         PrimOpId _      -> True
314         ClassOpId _     -> True
315         DataConWorkId _ -> True
316         DataConWrapId _ -> True
317                 -- These are are implied by their type or class decl;
318                 -- remember that all type and class decls appear in the interface file.
319                 -- The dfun id is not an implicit Id; it must *not* be omitted, because 
320                 -- it carries version info for the instance decl
321         _               -> False
322
323 idIsFrom :: Module -> Id -> Bool
324 idIsFrom mod id = nameIsLocalOrFrom mod (idName id)
325 \end{code}
326
327 Note [Primop wrappers]
328 ~~~~~~~~~~~~~~~~~~~~~~
329 Currently hasNoBinding claims that PrimOpIds don't have a curried
330 function definition.  But actually they do, in GHC.PrimopWrappers,
331 which is auto-generated from prelude/primops.txt.pp.  So actually, hasNoBinding
332 could return 'False' for PrimOpIds.
333
334 But we'd need to add something in CoreToStg to swizzle any unsaturated
335 applications of GHC.Prim.plusInt# to GHC.PrimopWrappers.plusInt#.
336
337 Nota Bene: GHC.PrimopWrappers is needed *regardless*, because it's
338 used by GHCi, which does not implement primops direct at all.
339
340
341
342 \begin{code}
343 isDeadBinder :: Id -> Bool
344 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
345                   | otherwise = False   -- TyVars count as not dead
346 \end{code}
347
348 \begin{code}
349 isTickBoxOp :: Id -> Bool
350 isTickBoxOp id = 
351   case globalIdDetails id of
352     TickBoxOpId _    -> True
353     _                -> False
354
355 isTickBoxOp_maybe :: Id -> Maybe TickBoxOp
356 isTickBoxOp_maybe id = 
357   case globalIdDetails id of
358     TickBoxOpId tick -> Just tick
359     _                -> Nothing
360 \end{code}
361
362 %************************************************************************
363 %*                                                                      *
364 \subsection{IdInfo stuff}
365 %*                                                                      *
366 %************************************************************************
367
368 \begin{code}
369         ---------------------------------
370         -- ARITY
371 idArity :: Id -> Arity
372 idArity id = arityInfo (idInfo id)
373
374 setIdArity :: Id -> Arity -> Id
375 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
376
377 #ifdef OLD_STRICTNESS
378         ---------------------------------
379         -- (OLD) STRICTNESS 
380 idStrictness :: Id -> StrictnessInfo
381 idStrictness id = strictnessInfo (idInfo id)
382
383 setIdStrictness :: Id -> StrictnessInfo -> Id
384 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
385 #endif
386
387 -- isBottomingId returns true if an application to n args would diverge
388 isBottomingId :: Id -> Bool
389 isBottomingId id = isBottomingSig (idNewStrictness id)
390
391 idNewStrictness_maybe :: Id -> Maybe StrictSig
392 idNewStrictness :: Id -> StrictSig
393
394 idNewStrictness_maybe id = newStrictnessInfo (idInfo id)
395 idNewStrictness       id = idNewStrictness_maybe id `orElse` topSig
396
397 setIdNewStrictness :: Id -> StrictSig -> Id
398 setIdNewStrictness id sig = modifyIdInfo (`setNewStrictnessInfo` Just sig) id
399
400 zapIdNewStrictness :: Id -> Id
401 zapIdNewStrictness id = modifyIdInfo (`setNewStrictnessInfo` Nothing) id
402 \end{code}
403
404 This predicate says whether the id has a strict demand placed on it or
405 has a type such that it can always be evaluated strictly (e.g., an
406 unlifted type, but see the comment for isStrictType).  We need to
407 check separately whether <id> has a so-called "strict type" because if
408 the demand for <id> hasn't been computed yet but <id> has a strict
409 type, we still want (isStrictId <id>) to be True.
410 \begin{code}
411 isStrictId :: Id -> Bool
412 isStrictId id
413   = ASSERT2( isId id, text "isStrictId: not an id: " <+> ppr id )
414            (isStrictDmd (idNewDemandInfo id)) || 
415            (isStrictType (idType id))
416
417         ---------------------------------
418         -- WORKER ID
419 idWorkerInfo :: Id -> WorkerInfo
420 idWorkerInfo id = workerInfo (idInfo id)
421
422 setIdWorkerInfo :: Id -> WorkerInfo -> Id
423 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
424
425         ---------------------------------
426         -- UNFOLDING
427 idUnfolding :: Id -> Unfolding
428 idUnfolding id = unfoldingInfo (idInfo id)
429
430 setIdUnfolding :: Id -> Unfolding -> Id
431 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
432
433 #ifdef OLD_STRICTNESS
434         ---------------------------------
435         -- (OLD) DEMAND
436 idDemandInfo :: Id -> Demand.Demand
437 idDemandInfo id = demandInfo (idInfo id)
438
439 setIdDemandInfo :: Id -> Demand.Demand -> Id
440 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
441 #endif
442
443 idNewDemandInfo_maybe :: Id -> Maybe NewDemand.Demand
444 idNewDemandInfo       :: Id -> NewDemand.Demand
445
446 idNewDemandInfo_maybe id = newDemandInfo (idInfo id)
447 idNewDemandInfo       id = newDemandInfo (idInfo id) `orElse` NewDemand.topDmd
448
449 setIdNewDemandInfo :: Id -> NewDemand.Demand -> Id
450 setIdNewDemandInfo id dmd = modifyIdInfo (`setNewDemandInfo` Just dmd) id
451
452         ---------------------------------
453         -- SPECIALISATION
454 idSpecialisation :: Id -> SpecInfo
455 idSpecialisation id = specInfo (idInfo id)
456
457 idCoreRules :: Id -> [CoreRule]
458 idCoreRules id = specInfoRules (idSpecialisation id)
459
460 idHasRules :: Id -> Bool
461 idHasRules id = not (isEmptySpecInfo (idSpecialisation id))
462
463 setIdSpecialisation :: Id -> SpecInfo -> Id
464 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
465
466         ---------------------------------
467         -- CAF INFO
468 idCafInfo :: Id -> CafInfo
469 #ifdef OLD_STRICTNESS
470 idCafInfo id = case cgInfo (idInfo id) of
471                   NoCgInfo -> pprPanic "idCafInfo" (ppr id)
472                   info     -> cgCafInfo info
473 #else
474 idCafInfo id = cafInfo (idInfo id)
475 #endif
476
477 setIdCafInfo :: Id -> CafInfo -> Id
478 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
479
480         ---------------------------------
481         -- CPR INFO
482 #ifdef OLD_STRICTNESS
483 idCprInfo :: Id -> CprInfo
484 idCprInfo id = cprInfo (idInfo id)
485
486 setIdCprInfo :: Id -> CprInfo -> Id
487 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
488 #endif
489
490         ---------------------------------
491         -- Occcurrence INFO
492 idOccInfo :: Id -> OccInfo
493 idOccInfo id = occInfo (idInfo id)
494
495 setIdOccInfo :: Id -> OccInfo -> Id
496 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
497 \end{code}
498
499
500         ---------------------------------
501         -- INLINING
502 The inline pragma tells us to be very keen to inline this Id, but it's still
503 OK not to if optimisation is switched off.
504
505 \begin{code}
506 idInlinePragma :: Id -> InlinePragInfo
507 idInlinePragma id = inlinePragInfo (idInfo id)
508
509 setInlinePragma :: Id -> InlinePragInfo -> Id
510 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
511
512 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
513 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
514 \end{code}
515
516
517         ---------------------------------
518         -- ONE-SHOT LAMBDAS
519 \begin{code}
520 idLBVarInfo :: Id -> LBVarInfo
521 idLBVarInfo id = lbvarInfo (idInfo id)
522
523 isOneShotBndr :: Id -> Bool
524 -- This one is the "business end", called externally.
525 -- Its main purpose is to encapsulate the Horrible State Hack
526 isOneShotBndr id = isOneShotLambda id || isStateHackType (idType id)
527
528 isStateHackType :: Type -> Bool
529 isStateHackType ty
530   | opt_NoStateHack 
531   = False
532   | otherwise
533   = case splitTyConApp_maybe ty of
534         Just (tycon,_) -> tycon == statePrimTyCon
535         _              -> False
536         -- This is a gross hack.  It claims that 
537         -- every function over realWorldStatePrimTy is a one-shot
538         -- function.  This is pretty true in practice, and makes a big
539         -- difference.  For example, consider
540         --      a `thenST` \ r -> ...E...
541         -- The early full laziness pass, if it doesn't know that r is one-shot
542         -- will pull out E (let's say it doesn't mention r) to give
543         --      let lvl = E in a `thenST` \ r -> ...lvl...
544         -- When `thenST` gets inlined, we end up with
545         --      let lvl = E in \s -> case a s of (r, s') -> ...lvl...
546         -- and we don't re-inline E.
547         --
548         -- It would be better to spot that r was one-shot to start with, but
549         -- I don't want to rely on that.
550         --
551         -- Another good example is in fill_in in PrelPack.lhs.  We should be able to
552         -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
553
554
555 -- The OneShotLambda functions simply fiddle with the IdInfo flag
556 isOneShotLambda :: Id -> Bool
557 isOneShotLambda id = case idLBVarInfo id of
558                        IsOneShotLambda  -> True
559                        NoLBVarInfo      -> False
560
561 setOneShotLambda :: Id -> Id
562 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
563
564 clearOneShotLambda :: Id -> Id
565 clearOneShotLambda id 
566   | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
567   | otherwise          = id                     
568
569 -- But watch out: this may change the type of something else
570 --      f = \x -> e
571 -- If we change the one-shot-ness of x, f's type changes
572 \end{code}
573
574 \begin{code}
575 zapInfo :: (IdInfo -> Maybe IdInfo) -> Id -> Id
576 zapInfo zapper id = maybeModifyIdInfo (zapper (idInfo id)) id
577
578 zapLamIdInfo :: Id -> Id
579 zapLamIdInfo = zapInfo zapLamInfo
580
581 zapDemandIdInfo :: Id -> Id
582 zapDemandIdInfo = zapInfo zapDemandInfo
583
584 zapFragileIdInfo :: Id -> Id
585 zapFragileIdInfo = zapInfo zapFragileInfo 
586 \end{code}
587