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