2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Id]{@Ids@: Value and constructor identifiers}
10 -- Simple construction
11 mkGlobalId, mkLocalId, mkSpecPragmaId, mkLocalIdWithInfo,
12 mkSysLocal, mkUserLocal, mkVanillaGlobal,
13 mkTemplateLocals, mkTemplateLocalsNum, mkWildId, mkTemplateLocal,
17 idName, idType, idUnique, idInfo,
18 idPrimRep, isId, globalIdDetails,
19 recordSelectorFieldLabel,
22 setIdName, setIdUnique, setIdType, setIdLocalExported, setGlobalIdDetails,
23 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
24 zapLamIdInfo, zapDemandIdInfo,
27 isImplicitId, isDeadBinder,
28 isSpecPragmaId, isExportedId, isLocalId, isGlobalId,
30 isPrimOpId, isPrimOpId_maybe,
31 isFCallId, isFCallId_maybe,
32 isDataConId, isDataConId_maybe,
33 isDataConWrapId, isDataConWrapId_maybe,
37 -- Inline pragma stuff
38 idInlinePragma, setInlinePragma, modifyInlinePragma,
41 -- One shot lambda stuff
42 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
47 setIdDemandInfo, setIdNewDemandInfo,
48 setIdStrictness, setIdNewStrictness, zapIdNewStrictness,
57 idDemandInfo, idNewDemandInfo,
58 idStrictness, idNewStrictness, idNewStrictness_maybe,
69 newStrictnessFromOld -- Temporary
73 #include "HsVersions.h"
76 import CoreSyn ( Unfolding, CoreRules )
77 import BasicTypes ( Arity )
78 import Var ( Id, DictId,
79 isId, isExportedId, isSpecPragmaId, isLocalId,
80 idName, idType, idUnique, idInfo, isGlobalId,
81 setIdName, setVarType, setIdUnique, setIdLocalExported,
82 setIdInfo, lazySetIdInfo, modifyIdInfo,
84 globalIdDetails, setGlobalIdDetails
86 import qualified Var ( mkLocalId, mkGlobalId, mkSpecPragmaId )
87 import Type ( Type, typePrimRep, addFreeTyVars,
88 usOnce, eqUsage, seqType, splitTyConApp_maybe )
92 import qualified Demand ( Demand )
93 import NewDemand ( Demand, StrictSig, topSig, isBottomingSig )
94 import Name ( Name, OccName,
95 mkSysLocalName, mkLocalName,
98 import OccName ( UserFS, mkWorkerOcc )
99 import PrimRep ( PrimRep )
100 import TysPrim ( statePrimTyCon )
101 import FieldLabel ( FieldLabel )
102 import Maybes ( orElse )
103 import SrcLoc ( SrcLoc )
105 import Unique ( Unique, mkBuiltinUnique )
107 infixl 1 `setIdUnfolding`,
111 `setIdNewDemandInfo`,
112 `setIdNewStrictness`,
115 `setIdSpecialisation`,
120 -- infixl so you can say (id `set` a `set` b)
125 %************************************************************************
127 \subsection{Simple Id construction}
129 %************************************************************************
131 Absolutely all Ids are made by mkId. It is just like Var.mkId,
132 but in addition it pins free-tyvar-info onto the Id's type,
133 where it can easily be found.
136 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
137 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
139 mkSpecPragmaId :: OccName -> Unique -> Type -> SrcLoc -> Id
140 mkSpecPragmaId occ uniq ty loc = Var.mkSpecPragmaId (mkLocalName uniq occ loc)
144 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
145 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
149 mkLocalId :: Name -> Type -> Id
150 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
152 -- SysLocal: for an Id being created by the compiler out of thin air...
153 -- UserLocal: an Id with a name the user might recognize...
154 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
155 mkSysLocal :: UserFS -> Unique -> Type -> Id
156 mkVanillaGlobal :: Name -> Type -> IdInfo -> Id
158 mkSysLocal fs uniq ty = mkLocalId (mkSysLocalName uniq fs) ty
159 mkUserLocal occ uniq ty loc = mkLocalId (mkLocalName uniq occ loc) ty
160 mkVanillaGlobal = mkGlobalId VanillaGlobal
163 Make some local @Ids@ for a template @CoreExpr@. These have bogus
164 @Uniques@, but that's OK because the templates are supposed to be
165 instantiated before use.
168 -- "Wild Id" typically used when you need a binder that you don't expect to use
169 mkWildId :: Type -> Id
170 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
172 mkWorkerId :: Unique -> Id -> Type -> Id
173 -- A worker gets a local name. CoreTidy will globalise it if necessary.
174 mkWorkerId uniq unwrkr ty
175 = mkLocalId wkr_name ty
177 wkr_name = mkLocalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcLoc unwrkr)
179 -- "Template locals" typically used in unfoldings
180 mkTemplateLocals :: [Type] -> [Id]
181 mkTemplateLocals tys = zipWith mkTemplateLocal [1..] tys
183 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
184 -- The Int gives the starting point for unique allocation
185 mkTemplateLocalsNum n tys = zipWith mkTemplateLocal [n..] tys
187 mkTemplateLocal :: Int -> Type -> Id
188 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
192 %************************************************************************
194 \subsection[Id-general-funs]{General @Id@-related functions}
196 %************************************************************************
199 setIdType :: Id -> Type -> Id
200 -- Add free tyvar info to the type
201 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
203 idPrimRep :: Id -> PrimRep
204 idPrimRep id = typePrimRep (idType id)
208 %************************************************************************
210 \subsection{Special Ids}
212 %************************************************************************
214 The @SpecPragmaId@ exists only to make Ids that are
215 on the *LHS* of bindings created by SPECIALISE pragmas;
217 The SpecPragmaId is never itself mentioned; it
218 exists solely so that the specialiser will find
219 the call to f, and make specialised version of it.
220 The SpecPragmaId binding is discarded by the specialiser
221 when it gathers up overloaded calls.
222 Meanwhile, it is not discarded as dead code.
226 recordSelectorFieldLabel :: Id -> FieldLabel
227 recordSelectorFieldLabel id = case globalIdDetails id of
228 RecordSelId lbl -> lbl
230 isRecordSelector id = case globalIdDetails id of
231 RecordSelId lbl -> True
234 isPrimOpId id = case globalIdDetails id of
238 isPrimOpId_maybe id = case globalIdDetails id of
239 PrimOpId op -> Just op
242 isFCallId id = case globalIdDetails id of
246 isFCallId_maybe id = case globalIdDetails id of
247 FCallId call -> Just call
250 isDataConId id = case globalIdDetails id of
254 isDataConId_maybe id = case globalIdDetails id of
255 DataConId con -> Just con
258 isDataConWrapId_maybe id = case globalIdDetails id of
259 DataConWrapId con -> Just con
262 isDataConWrapId id = case globalIdDetails id of
263 DataConWrapId con -> True
266 -- hasNoBinding returns True of an Id which may not have a
267 -- binding, even though it is defined in this module.
268 -- Data constructor workers used to be things of this kind, but
269 -- they aren't any more. Instead, we inject a binding for
270 -- them at the CorePrep stage.
271 hasNoBinding id = case globalIdDetails id of
276 isImplicitId :: Id -> Bool
277 -- isImplicitId tells whether an Id's info is implied by other
278 -- declarations, so we don't need to put its signature in an interface
279 -- file, even if it's mentioned in some other interface unfolding.
281 = case globalIdDetails id of
282 RecordSelId _ -> True -- Includes dictionary selectors
286 DataConWrapId _ -> True
287 -- These are are implied by their type or class decl;
288 -- remember that all type and class decls appear in the interface file.
289 -- The dfun id must *not* be omitted, because it carries version info for
295 isDeadBinder :: Id -> Bool
296 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
297 | otherwise = False -- TyVars count as not dead
301 %************************************************************************
303 \subsection{IdInfo stuff}
305 %************************************************************************
308 ---------------------------------
310 idArity :: Id -> Arity
311 idArity id = arityInfo (idInfo id)
313 setIdArity :: Id -> Arity -> Id
314 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
316 ---------------------------------
318 idStrictness :: Id -> StrictnessInfo
319 idStrictness id = strictnessInfo (idInfo id)
321 setIdStrictness :: Id -> StrictnessInfo -> Id
322 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
324 -- isBottomingId returns true if an application to n args would diverge
325 isBottomingId :: Id -> Bool
326 isBottomingId id = isBottomingSig (idNewStrictness id)
328 idNewStrictness_maybe :: Id -> Maybe StrictSig
329 idNewStrictness :: Id -> StrictSig
331 idNewStrictness_maybe id = newStrictnessInfo (idInfo id)
332 idNewStrictness id = idNewStrictness_maybe id `orElse` topSig
334 setIdNewStrictness :: Id -> StrictSig -> Id
335 setIdNewStrictness id sig = modifyIdInfo (`setNewStrictnessInfo` Just sig) id
337 zapIdNewStrictness :: Id -> Id
338 zapIdNewStrictness id = modifyIdInfo (`setNewStrictnessInfo` Nothing) id
340 ---------------------------------
341 -- TYPE GENERALISATION
342 idTyGenInfo :: Id -> TyGenInfo
343 idTyGenInfo id = tyGenInfo (idInfo id)
345 setIdTyGenInfo :: Id -> TyGenInfo -> Id
346 setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id
348 ---------------------------------
350 idWorkerInfo :: Id -> WorkerInfo
351 idWorkerInfo id = workerInfo (idInfo id)
353 setIdWorkerInfo :: Id -> WorkerInfo -> Id
354 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
356 ---------------------------------
358 idUnfolding :: Id -> Unfolding
359 idUnfolding id = unfoldingInfo (idInfo id)
361 setIdUnfolding :: Id -> Unfolding -> Id
362 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
364 ---------------------------------
366 idDemandInfo :: Id -> Demand.Demand
367 idDemandInfo id = demandInfo (idInfo id)
369 setIdDemandInfo :: Id -> Demand.Demand -> Id
370 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
372 idNewDemandInfo :: Id -> NewDemand.Demand
373 idNewDemandInfo id = newDemandInfo (idInfo id)
375 setIdNewDemandInfo :: Id -> NewDemand.Demand -> Id
376 setIdNewDemandInfo id dmd = modifyIdInfo (`setNewDemandInfo` dmd) id
378 ---------------------------------
380 idSpecialisation :: Id -> CoreRules
381 idSpecialisation id = specInfo (idInfo id)
383 setIdSpecialisation :: Id -> CoreRules -> Id
384 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
386 ---------------------------------
388 idCgInfo :: Id -> CgInfo
390 idCgInfo id = case cgInfo (idInfo id) of
391 NoCgInfo -> pprPanic "idCgInfo" (ppr id)
394 idCgInfo id = cgInfo (idInfo id)
397 setIdCgInfo :: Id -> CgInfo -> Id
398 setIdCgInfo id cg_info = modifyIdInfo (`setCgInfo` cg_info) id
400 ---------------------------------
402 idCafInfo :: Id -> CafInfo
404 idCafInfo id = case cgInfo (idInfo id) of
405 NoCgInfo -> pprPanic "idCafInfo" (ppr id)
406 info -> cgCafInfo info
408 idCafInfo id = cgCafInfo (idCgInfo id)
411 ---------------------------------
413 idCprInfo :: Id -> CprInfo
414 idCprInfo id = cprInfo (idInfo id)
416 setIdCprInfo :: Id -> CprInfo -> Id
417 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
419 ---------------------------------
421 idOccInfo :: Id -> OccInfo
422 idOccInfo id = occInfo (idInfo id)
424 setIdOccInfo :: Id -> OccInfo -> Id
425 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
429 ---------------------------------
431 The inline pragma tells us to be very keen to inline this Id, but it's still
432 OK not to if optimisation is switched off.
435 idInlinePragma :: Id -> InlinePragInfo
436 idInlinePragma id = inlinePragInfo (idInfo id)
438 setInlinePragma :: Id -> InlinePragInfo -> Id
439 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
441 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
442 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
446 ---------------------------------
449 idLBVarInfo :: Id -> LBVarInfo
450 idLBVarInfo id = lbvarInfo (idInfo id)
452 isOneShotLambda :: Id -> Bool
453 isOneShotLambda id = analysis || hack
454 where analysis = case idLBVarInfo id of
455 LBVarInfo u | u `eqUsage` usOnce -> True
457 hack = case splitTyConApp_maybe (idType id) of
458 Just (tycon,_) | tycon == statePrimTyCon -> True
461 -- The last clause is a gross hack. It claims that
462 -- every function over realWorldStatePrimTy is a one-shot
463 -- function. This is pretty true in practice, and makes a big
464 -- difference. For example, consider
465 -- a `thenST` \ r -> ...E...
466 -- The early full laziness pass, if it doesn't know that r is one-shot
467 -- will pull out E (let's say it doesn't mention r) to give
468 -- let lvl = E in a `thenST` \ r -> ...lvl...
469 -- When `thenST` gets inlined, we end up with
470 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
471 -- and we don't re-inline E.
473 -- It would be better to spot that r was one-shot to start with, but
474 -- I don't want to rely on that.
476 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
477 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
479 setOneShotLambda :: Id -> Id
480 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
482 clearOneShotLambda :: Id -> Id
483 clearOneShotLambda id
484 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
487 -- But watch out: this may change the type of something else
489 -- If we change the one-shot-ness of x, f's type changes
493 zapLamIdInfo :: Id -> Id
494 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id
496 zapDemandIdInfo id = maybeModifyIdInfo zapDemandInfo id