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