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,
48 setIdNewStrictness, zapIdNewStrictness,
64 idNewDemandInfo, idNewDemandInfo_maybe,
65 idNewStrictness, idNewStrictness_maybe,
68 idSpecialisation, idCoreRules,
75 newStrictnessFromOld -- Temporary
80 #include "HsVersions.h"
83 import CoreSyn ( Unfolding, CoreRules, IdCoreRule, rulesRules )
84 import BasicTypes ( Arity )
85 import Var ( Id, DictId,
86 isId, isExportedId, isSpecPragmaId, isLocalId,
87 idName, idType, idUnique, idInfo, isGlobalId,
88 setIdName, setVarType, setIdUnique, setIdLocalExported,
89 setIdInfo, lazySetIdInfo, modifyIdInfo,
91 globalIdDetails, setGlobalIdDetails
93 import qualified Var ( mkLocalId, mkGlobalId, mkSpecPragmaId )
94 import Type ( Type, typePrimRep, addFreeTyVars,
95 usOnce, eqUsage, seqType, splitTyConApp_maybe )
99 import qualified Demand ( Demand )
100 import NewDemand ( Demand, StrictSig, topDmd, topSig, isBottomingSig )
101 import Name ( Name, OccName,
102 mkSystemName, mkInternalName,
103 getOccName, getSrcLoc
105 import OccName ( EncodedFS, mkWorkerOcc )
106 import PrimRep ( PrimRep )
107 import TysPrim ( statePrimTyCon )
108 import FieldLabel ( FieldLabel )
109 import Maybes ( orElse )
110 import SrcLoc ( SrcLoc )
112 import Unique ( Unique, mkBuiltinUnique )
114 -- infixl so you can say (id `set` a `set` b)
115 infixl 1 `setIdUnfolding`,
117 `setIdNewDemandInfo`,
118 `setIdNewStrictness`,
120 `setIdSpecialisation`,
123 #ifdef OLD_STRICTNESS
132 %************************************************************************
134 \subsection{Simple Id construction}
136 %************************************************************************
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.
143 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
144 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
146 mkSpecPragmaId :: Name -> Type -> Id
147 mkSpecPragmaId name ty = Var.mkSpecPragmaId name (addFreeTyVars ty) vanillaIdInfo
149 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
150 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
154 mkLocalId :: Name -> Type -> Id
155 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
157 -- SysLocal: for an Id being created by the compiler out of thin air...
158 -- UserLocal: an Id with a name the user might recognize...
159 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
160 mkSysLocal :: EncodedFS -> Unique -> Type -> Id
161 mkVanillaGlobal :: Name -> Type -> IdInfo -> Id
163 -- for SysLocal, we assume the base name is already encoded, to avoid
164 -- re-encoding the same string over and over again.
165 mkSysLocal fs uniq ty = mkLocalId (mkSystemName uniq fs) ty
166 mkUserLocal occ uniq ty loc = mkLocalId (mkInternalName uniq occ loc) ty
167 mkVanillaGlobal = mkGlobalId VanillaGlobal
170 Make some local @Ids@ for a template @CoreExpr@. These have bogus
171 @Uniques@, but that's OK because the templates are supposed to be
172 instantiated before use.
175 -- "Wild Id" typically used when you need a binder that you don't expect to use
176 mkWildId :: Type -> Id
177 mkWildId ty = mkSysLocal FSLIT("wild") (mkBuiltinUnique 1) ty
179 mkWorkerId :: Unique -> Id -> Type -> Id
180 -- A worker gets a local name. CoreTidy will externalise it if necessary.
181 mkWorkerId uniq unwrkr ty
182 = mkLocalId wkr_name ty
184 wkr_name = mkInternalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcLoc unwrkr)
186 -- "Template locals" typically used in unfoldings
187 mkTemplateLocals :: [Type] -> [Id]
188 mkTemplateLocals tys = zipWith mkTemplateLocal [1..] tys
190 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
191 -- The Int gives the starting point for unique allocation
192 mkTemplateLocalsNum n tys = zipWith mkTemplateLocal [n..] tys
194 mkTemplateLocal :: Int -> Type -> Id
195 mkTemplateLocal i ty = mkSysLocal FSLIT("tpl") (mkBuiltinUnique i) ty
199 %************************************************************************
201 \subsection[Id-general-funs]{General @Id@-related functions}
203 %************************************************************************
206 setIdType :: Id -> Type -> Id
207 -- Add free tyvar info to the type
208 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
210 idPrimRep :: Id -> PrimRep
211 idPrimRep id = typePrimRep (idType id)
215 %************************************************************************
217 \subsection{Special Ids}
219 %************************************************************************
221 The @SpecPragmaId@ exists only to make Ids that are
222 on the *LHS* of bindings created by SPECIALISE pragmas;
224 The SpecPragmaId is never itself mentioned; it
225 exists solely so that the specialiser will find
226 the call to f, and make specialised version of it.
227 The SpecPragmaId binding is discarded by the specialiser
228 when it gathers up overloaded calls.
229 Meanwhile, it is not discarded as dead code.
233 recordSelectorFieldLabel :: Id -> FieldLabel
234 recordSelectorFieldLabel id = case globalIdDetails id of
235 RecordSelId lbl -> lbl
237 isRecordSelector id = case globalIdDetails id of
238 RecordSelId lbl -> True
241 isPrimOpId id = case globalIdDetails id of
245 isPrimOpId_maybe id = case globalIdDetails id of
246 PrimOpId op -> Just op
249 isFCallId id = case globalIdDetails id of
253 isFCallId_maybe id = case globalIdDetails id of
254 FCallId call -> Just call
257 isDataConId id = case globalIdDetails id of
261 isDataConId_maybe id = case globalIdDetails id of
262 DataConId con -> Just con
265 isDataConWrapId_maybe id = case globalIdDetails id of
266 DataConWrapId con -> Just con
269 isDataConWrapId id = case globalIdDetails id of
270 DataConWrapId con -> True
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 hasNoBinding id = case globalIdDetails id of
283 isImplicitId :: Id -> Bool
284 -- isImplicitId tells whether an Id's info is implied by other
285 -- declarations, so we don't need to put its signature in an interface
286 -- file, even if it's mentioned in some other interface unfolding.
288 = case globalIdDetails id of
289 RecordSelId _ -> True -- Includes dictionary selectors
293 DataConWrapId _ -> True
294 -- These are are implied by their type or class decl;
295 -- remember that all type and class decls appear in the interface file.
296 -- The dfun id must *not* be omitted, because it carries version info for
302 isDeadBinder :: Id -> Bool
303 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
304 | otherwise = False -- TyVars count as not dead
308 %************************************************************************
310 \subsection{IdInfo stuff}
312 %************************************************************************
315 ---------------------------------
317 idArity :: Id -> Arity
318 idArity id = arityInfo (idInfo id)
320 setIdArity :: Id -> Arity -> Id
321 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
323 #ifdef OLD_STRICTNESS
324 ---------------------------------
326 idStrictness :: Id -> StrictnessInfo
327 idStrictness id = strictnessInfo (idInfo id)
329 setIdStrictness :: Id -> StrictnessInfo -> Id
330 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
333 -- isBottomingId returns true if an application to n args would diverge
334 isBottomingId :: Id -> Bool
335 isBottomingId id = isBottomingSig (idNewStrictness id)
337 idNewStrictness_maybe :: Id -> Maybe StrictSig
338 idNewStrictness :: Id -> StrictSig
340 idNewStrictness_maybe id = newStrictnessInfo (idInfo id)
341 idNewStrictness id = idNewStrictness_maybe id `orElse` topSig
343 setIdNewStrictness :: Id -> StrictSig -> Id
344 setIdNewStrictness id sig = modifyIdInfo (`setNewStrictnessInfo` Just sig) id
346 zapIdNewStrictness :: Id -> Id
347 zapIdNewStrictness id = modifyIdInfo (`setNewStrictnessInfo` Nothing) id
349 ---------------------------------
351 idWorkerInfo :: Id -> WorkerInfo
352 idWorkerInfo id = workerInfo (idInfo id)
354 setIdWorkerInfo :: Id -> WorkerInfo -> Id
355 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
357 ---------------------------------
359 idUnfolding :: Id -> Unfolding
360 idUnfolding id = unfoldingInfo (idInfo id)
362 setIdUnfolding :: Id -> Unfolding -> Id
363 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
365 #ifdef OLD_STRICTNESS
366 ---------------------------------
368 idDemandInfo :: Id -> Demand.Demand
369 idDemandInfo id = demandInfo (idInfo id)
371 setIdDemandInfo :: Id -> Demand.Demand -> Id
372 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
375 idNewDemandInfo_maybe :: Id -> Maybe NewDemand.Demand
376 idNewDemandInfo :: Id -> NewDemand.Demand
378 idNewDemandInfo_maybe id = newDemandInfo (idInfo id)
379 idNewDemandInfo id = newDemandInfo (idInfo id) `orElse` NewDemand.topDmd
381 setIdNewDemandInfo :: Id -> NewDemand.Demand -> Id
382 setIdNewDemandInfo id dmd = modifyIdInfo (`setNewDemandInfo` Just dmd) id
384 ---------------------------------
386 idSpecialisation :: Id -> CoreRules
387 idSpecialisation id = specInfo (idInfo id)
389 idCoreRules :: Id -> [IdCoreRule]
390 idCoreRules id = [(id,rule) | rule <- rulesRules (idSpecialisation id)]
392 setIdSpecialisation :: Id -> CoreRules -> Id
393 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
395 ---------------------------------
397 idCgInfo :: Id -> CgInfo
398 #ifdef OLD_STRICTNESS
399 idCgInfo id = case cgInfo (idInfo id) of
400 NoCgInfo -> pprPanic "idCgInfo" (ppr id)
403 idCgInfo id = cgInfo (idInfo id)
406 setIdCgInfo :: Id -> CgInfo -> Id
407 setIdCgInfo id cg_info = modifyIdInfo (`setCgInfo` cg_info) id
409 ---------------------------------
411 idCafInfo :: Id -> CafInfo
412 #ifdef OLD_STRICTNESS
413 idCafInfo id = case cgInfo (idInfo id) of
414 NoCgInfo -> pprPanic "idCafInfo" (ppr id)
415 info -> cgCafInfo info
417 idCafInfo id = cgCafInfo (idCgInfo id)
419 ---------------------------------
421 #ifdef OLD_STRICTNESS
422 idCprInfo :: Id -> CprInfo
423 idCprInfo id = cprInfo (idInfo id)
425 setIdCprInfo :: Id -> CprInfo -> Id
426 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
429 ---------------------------------
431 idOccInfo :: Id -> OccInfo
432 idOccInfo id = occInfo (idInfo id)
434 setIdOccInfo :: Id -> OccInfo -> Id
435 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
439 ---------------------------------
441 The inline pragma tells us to be very keen to inline this Id, but it's still
442 OK not to if optimisation is switched off.
445 idInlinePragma :: Id -> InlinePragInfo
446 idInlinePragma id = inlinePragInfo (idInfo id)
448 setInlinePragma :: Id -> InlinePragInfo -> Id
449 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
451 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
452 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
456 ---------------------------------
459 idLBVarInfo :: Id -> LBVarInfo
460 idLBVarInfo id = lbvarInfo (idInfo id)
462 isOneShotLambda :: Id -> Bool
463 isOneShotLambda id = analysis || hack
464 where analysis = case idLBVarInfo id of
465 LBVarInfo u | u `eqUsage` usOnce -> True
467 hack = case splitTyConApp_maybe (idType id) of
468 Just (tycon,_) | tycon == statePrimTyCon -> True
471 -- The last clause is a gross hack. It claims that
472 -- every function over realWorldStatePrimTy is a one-shot
473 -- function. This is pretty true in practice, and makes a big
474 -- difference. For example, consider
475 -- a `thenST` \ r -> ...E...
476 -- The early full laziness pass, if it doesn't know that r is one-shot
477 -- will pull out E (let's say it doesn't mention r) to give
478 -- let lvl = E in a `thenST` \ r -> ...lvl...
479 -- When `thenST` gets inlined, we end up with
480 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
481 -- and we don't re-inline E.
483 -- It would be better to spot that r was one-shot to start with, but
484 -- I don't want to rely on that.
486 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
487 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
489 setOneShotLambda :: Id -> Id
490 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
492 clearOneShotLambda :: Id -> Id
493 clearOneShotLambda id
494 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
497 -- But watch out: this may change the type of something else
499 -- If we change the one-shot-ness of x, f's type changes
503 zapLamIdInfo :: Id -> Id
504 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id
506 zapDemandIdInfo id = maybeModifyIdInfo zapDemandInfo id