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, setIdNoDiscard, setGlobalIdDetails,
23 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
24 zapLamIdInfo, zapDemandIdInfo,
27 isImplicitId, isDeadBinder,
28 isSpecPragmaId, isExportedId, isLocalId, isGlobalId,
30 isPrimOpId, isPrimOpId_maybe,
31 isDataConId, isDataConId_maybe,
32 isDataConWrapId, isDataConWrapId_maybe,
36 -- Inline pragma stuff
37 idInlinePragma, setInlinePragma, modifyInlinePragma,
40 -- One shot lambda stuff
41 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
71 #include "HsVersions.h"
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,
82 globalIdDetails, setGlobalIdDetails
84 import qualified Var ( mkLocalId, mkGlobalId, mkSpecPragmaId )
85 import Type ( Type, typePrimRep, addFreeTyVars,
86 usOnce, seqType, splitTyConApp_maybe )
90 import Demand ( Demand )
91 import Name ( Name, OccName,
92 mkSysLocalName, mkLocalName,
95 import OccName ( UserFS, mkWorkerOcc )
96 import PrimRep ( PrimRep )
97 import TysPrim ( statePrimTyCon )
98 import FieldLabel ( FieldLabel )
99 import SrcLoc ( SrcLoc )
101 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques,
102 getNumBuiltinUniques )
104 infixl 1 `setIdUnfolding`,
110 `setIdSpecialisation`,
115 -- infixl so you can say (id `set` a `set` b)
120 %************************************************************************
122 \subsection{Simple Id construction}
124 %************************************************************************
126 Absolutely all Ids are made by mkId. It is just like Var.mkId,
127 but in addition it pins free-tyvar-info onto the Id's type,
128 where it can easily be found.
131 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
132 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
134 mkSpecPragmaId :: OccName -> Unique -> Type -> SrcLoc -> Id
135 mkSpecPragmaId occ uniq ty loc = Var.mkSpecPragmaId (mkLocalName uniq occ loc)
139 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
140 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
144 mkLocalId :: Name -> Type -> Id
145 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
147 -- SysLocal: for an Id being created by the compiler out of thin air...
148 -- UserLocal: an Id with a name the user might recognize...
149 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
150 mkSysLocal :: UserFS -> Unique -> Type -> Id
151 mkVanillaGlobal :: Name -> Type -> IdInfo -> Id
153 mkSysLocal fs uniq ty = mkLocalId (mkSysLocalName uniq fs) ty
154 mkUserLocal occ uniq ty loc = mkLocalId (mkLocalName uniq occ loc) ty
155 mkVanillaGlobal = mkGlobalId VanillaGlobal
158 Make some local @Ids@ for a template @CoreExpr@. These have bogus
159 @Uniques@, but that's OK because the templates are supposed to be
160 instantiated before use.
163 -- "Wild Id" typically used when you need a binder that you don't expect to use
164 mkWildId :: Type -> Id
165 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
167 mkWorkerId :: Unique -> Id -> Type -> Id
168 -- A worker gets a local name. CoreTidy will globalise it if necessary.
169 mkWorkerId uniq unwrkr ty
170 = mkLocalId wkr_name ty
172 wkr_name = mkLocalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcLoc unwrkr)
174 -- "Template locals" typically used in unfoldings
175 mkTemplateLocals :: [Type] -> [Id]
176 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
177 (getBuiltinUniques (length tys))
180 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
181 -- The Int gives the starting point for unique allocation
182 mkTemplateLocalsNum n tys = zipWith (mkSysLocal SLIT("tpl"))
183 (getNumBuiltinUniques n (length tys))
186 mkTemplateLocal :: Int -> Type -> Id
187 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
191 %************************************************************************
193 \subsection[Id-general-funs]{General @Id@-related functions}
195 %************************************************************************
198 setIdType :: Id -> Type -> Id
199 -- Add free tyvar info to the type
200 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
202 idPrimRep :: Id -> PrimRep
203 idPrimRep id = typePrimRep (idType id)
207 %************************************************************************
209 \subsection{Special Ids}
211 %************************************************************************
213 The @SpecPragmaId@ exists only to make Ids that are
214 on the *LHS* of bindings created by SPECIALISE pragmas;
216 The SpecPragmaId is never itself mentioned; it
217 exists solely so that the specialiser will find
218 the call to f, and make specialised version of it.
219 The SpecPragmaId binding is discarded by the specialiser
220 when it gathers up overloaded calls.
221 Meanwhile, it is not discarded as dead code.
225 recordSelectorFieldLabel :: Id -> FieldLabel
226 recordSelectorFieldLabel id = case globalIdDetails id of
227 RecordSelId lbl -> lbl
229 isRecordSelector id = case globalIdDetails id of
230 RecordSelId lbl -> True
233 isPrimOpId id = case globalIdDetails id of
237 isPrimOpId_maybe id = case globalIdDetails id of
238 PrimOpId op -> Just op
241 isDataConId id = case globalIdDetails id of
245 isDataConId_maybe id = case globalIdDetails id of
246 DataConId con -> Just con
249 isDataConWrapId_maybe id = case globalIdDetails id of
250 DataConWrapId con -> Just con
253 isDataConWrapId id = case globalIdDetails id of
254 DataConWrapId con -> True
257 -- hasNoBinding returns True of an Id which may not have a
258 -- binding, even though it is defined in this module. Notably,
259 -- the constructors of a dictionary are in this situation.
260 hasNoBinding id = case globalIdDetails id of
265 isImplicitId :: Id -> Bool
266 -- isImplicitId tells whether an Id's info is implied by other
267 -- declarations, so we don't need to put its signature in an interface
268 -- file, even if it's mentioned in some other interface unfolding.
270 = case globalIdDetails id of
271 RecordSelId _ -> True -- Includes dictionary selectors
274 DataConWrapId _ -> True
275 -- These are are implied by their type or class decl;
276 -- remember that all type and class decls appear in the interface file.
277 -- The dfun id must *not* be omitted, because it carries version info for
283 isDeadBinder :: Id -> Bool
284 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
285 | otherwise = False -- TyVars count as not dead
289 %************************************************************************
291 \subsection{IdInfo stuff}
293 %************************************************************************
296 ---------------------------------
298 idArityInfo :: Id -> ArityInfo
299 idArityInfo id = arityInfo (idInfo id)
301 idArity :: Id -> Arity
302 idArity id = arityLowerBound (idArityInfo id)
304 setIdArityInfo :: Id -> ArityInfo -> Id
305 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
307 ---------------------------------
309 idStrictness :: Id -> StrictnessInfo
310 idStrictness id = strictnessInfo (idInfo id)
312 setIdStrictness :: Id -> StrictnessInfo -> Id
313 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
315 -- isBottomingId returns true if an application to n args would diverge
316 isBottomingId :: Id -> Bool
317 isBottomingId id = isBottomingStrictness (idStrictness id)
319 ---------------------------------
320 -- TYPE GENERALISATION
321 idTyGenInfo :: Id -> TyGenInfo
322 idTyGenInfo id = tyGenInfo (idInfo id)
324 setIdTyGenInfo :: Id -> TyGenInfo -> Id
325 setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id
327 ---------------------------------
329 idWorkerInfo :: Id -> WorkerInfo
330 idWorkerInfo id = workerInfo (idInfo id)
332 setIdWorkerInfo :: Id -> WorkerInfo -> Id
333 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
335 ---------------------------------
337 idUnfolding :: Id -> Unfolding
338 idUnfolding id = unfoldingInfo (idInfo id)
340 setIdUnfolding :: Id -> Unfolding -> Id
341 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
343 ---------------------------------
345 idDemandInfo :: Id -> Demand
346 idDemandInfo id = demandInfo (idInfo id)
348 setIdDemandInfo :: Id -> Demand -> Id
349 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
351 ---------------------------------
353 idSpecialisation :: Id -> CoreRules
354 idSpecialisation id = specInfo (idInfo id)
356 setIdSpecialisation :: Id -> CoreRules -> Id
357 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
359 ---------------------------------
361 idCgInfo :: Id -> CgInfo
363 idCgInfo id = case cgInfo (idInfo id) of
364 NoCgInfo -> pprPanic "idCgInfo" (ppr id)
367 idCgInfo id = cgInfo (idInfo id)
370 setIdCgInfo :: Id -> CgInfo -> Id
371 setIdCgInfo id cg_info = modifyIdInfo (`setCgInfo` cg_info) id
373 ---------------------------------
375 idCafInfo :: Id -> CafInfo
376 idCafInfo id = cgCafInfo (idCgInfo id)
378 ---------------------------------
381 idCgArity :: Id -> Arity
382 idCgArity id = cgArity (idCgInfo id)
384 ---------------------------------
386 idCprInfo :: Id -> CprInfo
387 idCprInfo id = cprInfo (idInfo id)
389 setIdCprInfo :: Id -> CprInfo -> Id
390 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
392 ---------------------------------
394 idOccInfo :: Id -> OccInfo
395 idOccInfo id = occInfo (idInfo id)
397 setIdOccInfo :: Id -> OccInfo -> Id
398 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
402 ---------------------------------
404 The inline pragma tells us to be very keen to inline this Id, but it's still
405 OK not to if optimisation is switched off.
408 idInlinePragma :: Id -> InlinePragInfo
409 idInlinePragma id = inlinePragInfo (idInfo id)
411 setInlinePragma :: Id -> InlinePragInfo -> Id
412 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
414 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
415 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
419 ---------------------------------
422 idLBVarInfo :: Id -> LBVarInfo
423 idLBVarInfo id = lbvarInfo (idInfo id)
425 isOneShotLambda :: Id -> Bool
426 isOneShotLambda id = analysis || hack
427 where analysis = case idLBVarInfo id of
428 LBVarInfo u | u == usOnce -> True
430 hack = case splitTyConApp_maybe (idType id) of
431 Just (tycon,_) | tycon == statePrimTyCon -> True
434 -- The last clause is a gross hack. It claims that
435 -- every function over realWorldStatePrimTy is a one-shot
436 -- function. This is pretty true in practice, and makes a big
437 -- difference. For example, consider
438 -- a `thenST` \ r -> ...E...
439 -- The early full laziness pass, if it doesn't know that r is one-shot
440 -- will pull out E (let's say it doesn't mention r) to give
441 -- let lvl = E in a `thenST` \ r -> ...lvl...
442 -- When `thenST` gets inlined, we end up with
443 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
444 -- and we don't re-inline E.
446 -- It would be better to spot that r was one-shot to start with, but
447 -- I don't want to rely on that.
449 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
450 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
452 setOneShotLambda :: Id -> Id
453 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
455 clearOneShotLambda :: Id -> Id
456 clearOneShotLambda id
457 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
460 -- But watch out: this may change the type of something else
462 -- If we change the one-shot-ness of x, f's type changes
466 zapLamIdInfo :: Id -> Id
467 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id
469 zapDemandIdInfo id = maybeModifyIdInfo zapDemandInfo id