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