[project @ 2000-11-14 08:07: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         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, isLocalId, 
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                           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
137 mkImportedId :: Name -> Type -> IdInfo -> Id
138 mkImportedId name ty info = mkId name ty (info `setFlavourInfo` ImportedId)
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 -- Perhaps a better name would be isDiscardableId
259 isExportedId :: Id -> Bool
260 isExportedId id = case idFlavour id of
261                         VanillaId  -> False
262                         other      -> True
263
264 isLocalId :: Id -> Bool
265 -- True of Ids that are locally defined, but are not constants
266 -- like data constructors, record selectors, and the like. 
267 -- See comments with CoreSyn.isLocalVar
268 isLocalId id = case idFlavour id of
269                  VanillaId    -> True
270                  ExportedId   -> True
271                  SpecPragmaId -> True
272                  other        -> False
273 \end{code}
274
275
276 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
277 so we don't need to put its signature in an interface file, even if it's mentioned
278 in some other interface unfolding.
279
280 \begin{code}
281 omitIfaceSigForId :: Id -> Bool
282 omitIfaceSigForId id
283   = ASSERT2( not (omit && nameIsLocallyDefined (idName id)
284                        && idTyGenInfo id /= TyGenNever),
285              ppr id )
286     -- mustn't omit type signature for a name whose type might change!
287     omit
288   where
289     omit = omitIfaceSigForId' id
290
291 omitIfaceSigForId' id
292   = case idFlavour id of
293         RecordSelId _   -> True -- Includes dictionary selectors
294         PrimOpId _      -> True
295         DataConId _     -> True
296         DataConWrapId _ -> True
297                 -- These are are implied by their type or class decl;
298                 -- remember that all type and class decls appear in the interface file.
299                 -- The dfun id must *not* be omitted, because it carries version info for
300                 -- the instance decl
301
302         other          -> False -- Don't omit!
303
304 -- Certain names must be exported with their original occ names, because
305 -- these names are bound by either a class declaration or a data declaration
306 -- or an explicit user export.
307 exportWithOrigOccName :: Id -> Bool
308 exportWithOrigOccName id = omitIfaceSigForId id || isExportedId id
309 \end{code}
310
311 \begin{code}
312 isDeadBinder :: Id -> Bool
313 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
314                   | otherwise = False   -- TyVars count as not dead
315
316 isIP id = isIPOcc (getOccName id)
317 \end{code}
318
319
320 %************************************************************************
321 %*                                                                      *
322 \subsection{IdInfo stuff}
323 %*                                                                      *
324 %************************************************************************
325
326 \begin{code}
327         ---------------------------------
328         -- ARITY
329 idArityInfo :: Id -> ArityInfo
330 idArityInfo id = arityInfo (idInfo id)
331
332 idArity :: Id -> Arity
333 idArity id = arityLowerBound (idArityInfo id)
334
335 setIdArityInfo :: Id -> ArityInfo -> Id
336 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
337
338         ---------------------------------
339         -- STRICTNESS
340 idStrictness :: Id -> StrictnessInfo
341 idStrictness id = strictnessInfo (idInfo id)
342
343 setIdStrictness :: Id -> StrictnessInfo -> Id
344 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
345
346 -- isBottomingId returns true if an application to n args would diverge
347 isBottomingId :: Id -> Bool
348 isBottomingId id = isBottomingStrictness (idStrictness id)
349
350         ---------------------------------
351         -- TYPE GENERALISATION
352 idTyGenInfo :: Id -> TyGenInfo
353 idTyGenInfo id = tyGenInfo (idInfo id)
354
355 setIdTyGenInfo :: Id -> TyGenInfo -> Id
356 setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id
357
358         ---------------------------------
359         -- WORKER ID
360 idWorkerInfo :: Id -> WorkerInfo
361 idWorkerInfo id = workerInfo (idInfo id)
362
363 setIdWorkerInfo :: Id -> WorkerInfo -> Id
364 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
365
366         ---------------------------------
367         -- UNFOLDING
368 idUnfolding :: Id -> Unfolding
369 idUnfolding id = unfoldingInfo (idInfo id)
370
371 setIdUnfolding :: Id -> Unfolding -> Id
372 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
373
374         ---------------------------------
375         -- DEMAND
376 idDemandInfo :: Id -> Demand
377 idDemandInfo id = demandInfo (idInfo id)
378
379 setIdDemandInfo :: Id -> Demand -> Id
380 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
381
382         ---------------------------------
383         -- SPECIALISATION
384 idSpecialisation :: Id -> CoreRules
385 idSpecialisation id = specInfo (idInfo id)
386
387 setIdSpecialisation :: Id -> CoreRules -> Id
388 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
389
390         ---------------------------------
391         -- CAF INFO
392 idCafInfo :: Id -> CafInfo
393 idCafInfo id = cafInfo (idInfo id)
394
395 setIdCafInfo :: Id -> CafInfo -> Id
396 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
397
398         ---------------------------------
399         -- CPR INFO
400 idCprInfo :: Id -> CprInfo
401 idCprInfo id = cprInfo (idInfo id)
402
403 setIdCprInfo :: Id -> CprInfo -> Id
404 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
405
406         ---------------------------------
407         -- Occcurrence INFO
408 idOccInfo :: Id -> OccInfo
409 idOccInfo id = occInfo (idInfo id)
410
411 setIdOccInfo :: Id -> OccInfo -> Id
412 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
413 \end{code}
414
415
416         ---------------------------------
417         -- INLINING
418 The inline pragma tells us to be very keen to inline this Id, but it's still
419 OK not to if optimisation is switched off.
420
421 \begin{code}
422 idInlinePragma :: Id -> InlinePragInfo
423 idInlinePragma id = inlinePragInfo (idInfo id)
424
425 setInlinePragma :: Id -> InlinePragInfo -> Id
426 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
427
428 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
429 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
430 \end{code}
431
432
433         ---------------------------------
434         -- ONE-SHOT LAMBDAS
435 \begin{code}
436 idLBVarInfo :: Id -> LBVarInfo
437 idLBVarInfo id = lbvarInfo (idInfo id)
438
439 isOneShotLambda :: Id -> Bool
440 isOneShotLambda id = analysis || hack
441   where analysis = case idLBVarInfo id of
442                      LBVarInfo u    | u == usOnce             -> True
443                      other                                    -> False
444         hack     = case splitTyConApp_maybe (idType id) of
445                      Just (tycon,_) | tycon == statePrimTyCon -> True
446                      other                                    -> False
447
448         -- The last clause is a gross hack.  It claims that 
449         -- every function over realWorldStatePrimTy is a one-shot
450         -- function.  This is pretty true in practice, and makes a big
451         -- difference.  For example, consider
452         --      a `thenST` \ r -> ...E...
453         -- The early full laziness pass, if it doesn't know that r is one-shot
454         -- will pull out E (let's say it doesn't mention r) to give
455         --      let lvl = E in a `thenST` \ r -> ...lvl...
456         -- When `thenST` gets inlined, we end up with
457         --      let lvl = E in \s -> case a s of (r, s') -> ...lvl...
458         -- and we don't re-inline E.
459         --
460         -- It would be better to spot that r was one-shot to start with, but
461         -- I don't want to rely on that.
462         --
463         -- Another good example is in fill_in in PrelPack.lhs.  We should be able to
464         -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
465
466 setOneShotLambda :: Id -> Id
467 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
468
469 clearOneShotLambda :: Id -> Id
470 clearOneShotLambda id 
471   | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
472   | otherwise          = id                     
473
474 -- But watch out: this may change the type of something else
475 --      f = \x -> e
476 -- If we change the one-shot-ness of x, f's type changes
477 \end{code}
478
479 \begin{code}
480 zapFragileIdInfo :: Id -> Id
481 zapFragileIdInfo id = maybeModifyIdInfo zapFragileInfo id
482
483 zapLamIdInfo :: Id -> Id
484 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id
485 \end{code}
486