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