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