[project @ 2004-01-05 10:41:04 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, mkSysLocalUnencoded, mkUserLocal, mkVanillaGlobal,
13         mkTemplateLocals, mkTemplateLocalsNum, mkWildId, mkTemplateLocal,
14         mkWorkerId, mkExportedLocalId,
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, Id.setIdType, setIdLocalExported, 
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         isFCallId, isFCallId_maybe,
32         isDataConWorkId, isDataConWorkId_maybe, 
33         isBottomingId, idIsFrom,
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         setIdArity,
46         setIdNewDemandInfo, 
47         setIdNewStrictness, zapIdNewStrictness,
48         setIdWorkerInfo,
49         setIdSpecialisation,
50         setIdCafInfo,
51         setIdOccInfo,
52
53 #ifdef OLD_STRICTNESS
54         idDemandInfo, 
55         idStrictness, 
56         idCprInfo,
57         setIdStrictness, 
58         setIdDemandInfo, 
59         setIdCprInfo,
60 #endif
61
62         idArity, 
63         idNewDemandInfo, idNewDemandInfo_maybe,
64         idNewStrictness, idNewStrictness_maybe, 
65         idWorkerInfo,
66         idUnfolding,
67         idSpecialisation, idCoreRules,
68         idCafInfo,
69         idLBVarInfo,
70         idOccInfo,
71
72 #ifdef OLD_STRICTNESS
73         newStrictnessFromOld    -- Temporary
74 #endif
75
76     ) where
77
78 #include "HsVersions.h"
79
80
81 import CoreSyn          ( Unfolding, CoreRules, IdCoreRule, rulesRules )
82 import BasicTypes       ( Arity )
83 import Var              ( Id, DictId,
84                           isId, isExportedId, isSpecPragmaId, isLocalId,
85                           idName, idType, idUnique, idInfo, isGlobalId,
86                           setIdName, setIdType, setIdUnique, setIdLocalExported,
87                           setIdInfo, lazySetIdInfo, modifyIdInfo, 
88                           maybeModifyIdInfo,
89                           globalIdDetails
90                         )
91 import qualified Var    ( mkLocalId, mkGlobalId, mkSpecPragmaId, mkExportedLocalId )
92 import Type             ( Type, typePrimRep, addFreeTyVars, seqType)
93
94 import IdInfo 
95
96 #ifdef OLD_STRICTNESS
97 import qualified Demand ( Demand )
98 #endif
99 import DataCon          ( isUnboxedTupleCon )
100 import NewDemand        ( Demand, StrictSig, topDmd, topSig, isBottomingSig )
101 import Name             ( Name, OccName, nameIsLocalOrFrom, 
102                           mkSystemName, mkSystemNameEncoded, mkInternalName,
103                           getOccName, getSrcLoc
104                         ) 
105 import Module           ( Module )
106 import OccName          ( EncodedFS, mkWorkerOcc )
107 import PrimRep          ( PrimRep )
108 import FieldLabel       ( FieldLabel )
109 import Maybes           ( orElse )
110 import SrcLoc           ( SrcLoc )
111 import Outputable
112 import Unique           ( Unique, mkBuiltinUnique )
113
114 -- infixl so you can say (id `set` a `set` b)
115 infixl  1 `setIdUnfolding`,
116           `setIdArity`,
117           `setIdNewDemandInfo`,
118           `setIdNewStrictness`,
119           `setIdWorkerInfo`,
120           `setIdSpecialisation`,
121           `setInlinePragma`,
122           `idCafInfo`
123 #ifdef OLD_STRICTNESS
124           ,`idCprInfo`
125           ,`setIdStrictness`
126           ,`setIdDemandInfo`
127 #endif
128 \end{code}
129
130
131
132 %************************************************************************
133 %*                                                                      *
134 \subsection{Simple Id construction}
135 %*                                                                      *
136 %************************************************************************
137
138 Absolutely all Ids are made by mkId.  It is just like Var.mkId,
139 but in addition it pins free-tyvar-info onto the Id's type, 
140 where it can easily be found.
141
142 \begin{code}
143 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
144 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
145
146 mkSpecPragmaId :: Name -> Type -> Id
147 mkSpecPragmaId name ty = Var.mkSpecPragmaId name (addFreeTyVars ty) vanillaIdInfo
148
149 mkExportedLocalId :: Name -> Type -> Id
150 mkExportedLocalId name ty = Var.mkExportedLocalId name (addFreeTyVars ty) vanillaIdInfo
151
152 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
153 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
154 \end{code}
155
156 \begin{code}
157 mkLocalId :: Name -> Type -> Id
158 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
159
160 -- SysLocal: for an Id being created by the compiler out of thin air...
161 -- UserLocal: an Id with a name the user might recognize...
162 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
163 mkSysLocal  :: EncodedFS  -> Unique -> Type -> Id
164 mkVanillaGlobal :: Name -> Type -> IdInfo -> Id
165
166 -- for SysLocal, we assume the base name is already encoded, to avoid
167 -- re-encoding the same string over and over again.
168 mkSysLocal          fs uniq ty = mkLocalId (mkSystemNameEncoded uniq fs) ty
169
170 -- version to use when the faststring needs to be encoded
171 mkSysLocalUnencoded fs uniq ty = mkLocalId (mkSystemName uniq fs)        ty
172
173 mkUserLocal occ uniq ty loc = mkLocalId (mkInternalName    uniq occ loc) ty
174 mkVanillaGlobal             = mkGlobalId VanillaGlobal
175 \end{code}
176
177 Make some local @Ids@ for a template @CoreExpr@.  These have bogus
178 @Uniques@, but that's OK because the templates are supposed to be
179 instantiated before use.
180  
181 \begin{code}
182 -- "Wild Id" typically used when you need a binder that you don't expect to use
183 mkWildId :: Type -> Id
184 mkWildId ty = mkSysLocal FSLIT("wild") (mkBuiltinUnique 1) ty
185
186 mkWorkerId :: Unique -> Id -> Type -> Id
187 -- A worker gets a local name.  CoreTidy will externalise it if necessary.
188 mkWorkerId uniq unwrkr ty
189   = mkLocalId wkr_name ty
190   where
191     wkr_name = mkInternalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcLoc unwrkr)
192
193 -- "Template locals" typically used in unfoldings
194 mkTemplateLocals :: [Type] -> [Id]
195 mkTemplateLocals tys = zipWith mkTemplateLocal [1..] tys
196
197 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
198 -- The Int gives the starting point for unique allocation
199 mkTemplateLocalsNum n tys = zipWith mkTemplateLocal [n..] tys
200
201 mkTemplateLocal :: Int -> Type -> Id
202 mkTemplateLocal i ty = mkSysLocal FSLIT("tpl") (mkBuiltinUnique i) ty
203 \end{code}
204
205
206 %************************************************************************
207 %*                                                                      *
208 \subsection[Id-general-funs]{General @Id@-related functions}
209 %*                                                                      *
210 %************************************************************************
211
212 \begin{code}
213 setIdType :: Id -> Type -> Id
214         -- Add free tyvar info to the type
215 setIdType id ty = seqType ty `seq` Var.setIdType id (addFreeTyVars ty)
216
217 idPrimRep :: Id -> PrimRep
218 idPrimRep id = typePrimRep (idType id)
219 \end{code}
220
221
222 %************************************************************************
223 %*                                                                      *
224 \subsection{Special Ids}
225 %*                                                                      *
226 %************************************************************************
227
228 The @SpecPragmaId@ exists only to make Ids that are
229 on the *LHS* of bindings created by SPECIALISE pragmas; 
230 eg:             s = f Int d
231 The SpecPragmaId is never itself mentioned; it
232 exists solely so that the specialiser will find
233 the call to f, and make specialised version of it.
234 The SpecPragmaId binding is discarded by the specialiser
235 when it gathers up overloaded calls.
236 Meanwhile, it is not discarded as dead code.
237
238
239 \begin{code}
240 recordSelectorFieldLabel :: Id -> FieldLabel
241 recordSelectorFieldLabel id = case globalIdDetails id of
242                                  RecordSelId lbl -> lbl
243                                  other -> panic "recordSelectorFieldLabel"
244
245 isRecordSelector id = case globalIdDetails id of
246                         RecordSelId lbl -> True
247                         other           -> False
248
249 isPrimOpId id = case globalIdDetails id of
250                     PrimOpId op -> True
251                     other       -> False
252
253 isPrimOpId_maybe id = case globalIdDetails id of
254                             PrimOpId op -> Just op
255                             other       -> Nothing
256
257 isFCallId id = case globalIdDetails id of
258                     FCallId call -> True
259                     other        -> False
260
261 isFCallId_maybe id = case globalIdDetails id of
262                             FCallId call -> Just call
263                             other        -> Nothing
264
265 isDataConWorkId id = case globalIdDetails id of
266                         DataConWorkId _ -> True
267                         other           -> False
268
269 isDataConWorkId_maybe id = case globalIdDetails id of
270                           DataConWorkId con -> Just con
271                           other             -> Nothing
272
273 -- hasNoBinding returns True of an Id which may not have a
274 -- binding, even though it is defined in this module.  
275 -- Data constructor workers used to be things of this kind, but
276 -- they aren't any more.  Instead, we inject a binding for 
277 -- them at the CorePrep stage. 
278 -- EXCEPT: unboxed tuples, which definitely have no binding
279 hasNoBinding id = case globalIdDetails id of
280                         PrimOpId _       -> True
281                         FCallId _        -> True
282                         DataConWorkId dc -> isUnboxedTupleCon dc
283                         other            -> False
284
285 isImplicitId :: Id -> Bool
286         -- isImplicitId tells whether an Id's info is implied by other
287         -- declarations, so we don't need to put its signature in an interface
288         -- file, even if it's mentioned in some other interface unfolding.
289 isImplicitId id
290   = case globalIdDetails id of
291         RecordSelId _   -> True
292         FCallId _       -> True
293         PrimOpId _      -> True
294         ClassOpId _     -> True
295         DataConWorkId _ -> 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 is not an implicit Id; it must *not* be omitted, because 
300                 -- it carries version info for the instance decl
301         other           -> False
302
303 idIsFrom :: Module -> Id -> Bool
304 idIsFrom mod id = nameIsLocalOrFrom mod (idName id)
305 \end{code}
306
307 \begin{code}
308 isDeadBinder :: Id -> Bool
309 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
310                   | otherwise = False   -- TyVars count as not dead
311 \end{code}
312
313
314 %************************************************************************
315 %*                                                                      *
316 \subsection{IdInfo stuff}
317 %*                                                                      *
318 %************************************************************************
319
320 \begin{code}
321         ---------------------------------
322         -- ARITY
323 idArity :: Id -> Arity
324 idArity id = arityInfo (idInfo id)
325
326 setIdArity :: Id -> Arity -> Id
327 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
328
329 #ifdef OLD_STRICTNESS
330         ---------------------------------
331         -- (OLD) 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 #endif
338
339 -- isBottomingId returns true if an application to n args would diverge
340 isBottomingId :: Id -> Bool
341 isBottomingId id = isBottomingSig (idNewStrictness id)
342
343 idNewStrictness_maybe :: Id -> Maybe StrictSig
344 idNewStrictness :: Id -> StrictSig
345
346 idNewStrictness_maybe id = newStrictnessInfo (idInfo id)
347 idNewStrictness       id = idNewStrictness_maybe id `orElse` topSig
348
349 setIdNewStrictness :: Id -> StrictSig -> Id
350 setIdNewStrictness id sig = modifyIdInfo (`setNewStrictnessInfo` Just sig) id
351
352 zapIdNewStrictness :: Id -> Id
353 zapIdNewStrictness id = modifyIdInfo (`setNewStrictnessInfo` Nothing) id
354
355         ---------------------------------
356         -- WORKER ID
357 idWorkerInfo :: Id -> WorkerInfo
358 idWorkerInfo id = workerInfo (idInfo id)
359
360 setIdWorkerInfo :: Id -> WorkerInfo -> Id
361 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
362
363         ---------------------------------
364         -- UNFOLDING
365 idUnfolding :: Id -> Unfolding
366 idUnfolding id = unfoldingInfo (idInfo id)
367
368 setIdUnfolding :: Id -> Unfolding -> Id
369 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
370
371 #ifdef OLD_STRICTNESS
372         ---------------------------------
373         -- (OLD) DEMAND
374 idDemandInfo :: Id -> Demand.Demand
375 idDemandInfo id = demandInfo (idInfo id)
376
377 setIdDemandInfo :: Id -> Demand.Demand -> Id
378 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
379 #endif
380
381 idNewDemandInfo_maybe :: Id -> Maybe NewDemand.Demand
382 idNewDemandInfo       :: Id -> NewDemand.Demand
383
384 idNewDemandInfo_maybe id = newDemandInfo (idInfo id)
385 idNewDemandInfo       id = newDemandInfo (idInfo id) `orElse` NewDemand.topDmd
386
387 setIdNewDemandInfo :: Id -> NewDemand.Demand -> Id
388 setIdNewDemandInfo id dmd = modifyIdInfo (`setNewDemandInfo` Just dmd) id
389
390         ---------------------------------
391         -- SPECIALISATION
392 idSpecialisation :: Id -> CoreRules
393 idSpecialisation id = specInfo (idInfo id)
394
395 idCoreRules :: Id -> [IdCoreRule]
396 idCoreRules id = [(id,rule) | rule <- rulesRules (idSpecialisation id)]
397
398 setIdSpecialisation :: Id -> CoreRules -> Id
399 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
400
401         ---------------------------------
402         -- CAF INFO
403 idCafInfo :: Id -> CafInfo
404 #ifdef OLD_STRICTNESS
405 idCafInfo id = case cgInfo (idInfo id) of
406                   NoCgInfo -> pprPanic "idCafInfo" (ppr id)
407                   info     -> cgCafInfo info
408 #else
409 idCafInfo id = cafInfo (idInfo id)
410 #endif
411
412 setIdCafInfo :: Id -> CafInfo -> Id
413 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
414
415         ---------------------------------
416         -- CPR INFO
417 #ifdef OLD_STRICTNESS
418 idCprInfo :: Id -> CprInfo
419 idCprInfo id = cprInfo (idInfo id)
420
421 setIdCprInfo :: Id -> CprInfo -> Id
422 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
423 #endif
424
425         ---------------------------------
426         -- Occcurrence INFO
427 idOccInfo :: Id -> OccInfo
428 idOccInfo id = occInfo (idInfo id)
429
430 setIdOccInfo :: Id -> OccInfo -> Id
431 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
432 \end{code}
433
434
435         ---------------------------------
436         -- INLINING
437 The inline pragma tells us to be very keen to inline this Id, but it's still
438 OK not to if optimisation is switched off.
439
440 \begin{code}
441 idInlinePragma :: Id -> InlinePragInfo
442 idInlinePragma id = inlinePragInfo (idInfo id)
443
444 setInlinePragma :: Id -> InlinePragInfo -> Id
445 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
446
447 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
448 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
449 \end{code}
450
451
452         ---------------------------------
453         -- ONE-SHOT LAMBDAS
454 \begin{code}
455 idLBVarInfo :: Id -> LBVarInfo
456 idLBVarInfo id = lbvarInfo (idInfo id)
457
458 isOneShotLambda :: Id -> Bool
459 isOneShotLambda id = case idLBVarInfo id of
460                        IsOneShotLambda  -> True
461                        NoLBVarInfo      -> False
462
463 setOneShotLambda :: Id -> Id
464 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
465
466 clearOneShotLambda :: Id -> Id
467 clearOneShotLambda id 
468   | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
469   | otherwise          = id                     
470
471 -- But watch out: this may change the type of something else
472 --      f = \x -> e
473 -- If we change the one-shot-ness of x, f's type changes
474 \end{code}
475
476 \begin{code}
477 zapLamIdInfo :: Id -> Id
478 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id
479
480 zapDemandIdInfo id = maybeModifyIdInfo zapDemandInfo id
481 \end{code}
482