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