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 )
103 infixl 1 `setIdUnfolding`,
109 `setIdSpecialisation`,
114 -- infixl so you can say (id `set` a `set` b)
119 %************************************************************************
121 \subsection{Simple Id construction}
123 %************************************************************************
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.
130 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
131 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
133 mkSpecPragmaId :: OccName -> Unique -> Type -> SrcLoc -> Id
134 mkSpecPragmaId occ uniq ty loc = Var.mkSpecPragmaId (mkLocalName uniq occ loc)
138 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
139 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
143 mkLocalId :: Name -> Type -> Id
144 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
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
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
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.
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
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
171 wkr_name = mkLocalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcLoc unwrkr)
173 -- "Template locals" typically used in unfoldings
174 mkTemplateLocals :: [Type] -> [Id]
175 mkTemplateLocals tys = zipWith mkTemplateLocal [1..] tys
177 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
178 -- The Int gives the starting point for unique allocation
179 mkTemplateLocalsNum n tys = zipWith mkTemplateLocal [n..] tys
181 mkTemplateLocal :: Int -> Type -> Id
182 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
186 %************************************************************************
188 \subsection[Id-general-funs]{General @Id@-related functions}
190 %************************************************************************
193 setIdType :: Id -> Type -> Id
194 -- Add free tyvar info to the type
195 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
197 idPrimRep :: Id -> PrimRep
198 idPrimRep id = typePrimRep (idType id)
202 %************************************************************************
204 \subsection{Special Ids}
206 %************************************************************************
208 The @SpecPragmaId@ exists only to make Ids that are
209 on the *LHS* of bindings created by SPECIALISE pragmas;
211 The SpecPragmaId is never itself mentioned; it
212 exists solely so that the specialiser will find
213 the call to f, and make specialised version of it.
214 The SpecPragmaId binding is discarded by the specialiser
215 when it gathers up overloaded calls.
216 Meanwhile, it is not discarded as dead code.
220 recordSelectorFieldLabel :: Id -> FieldLabel
221 recordSelectorFieldLabel id = case globalIdDetails id of
222 RecordSelId lbl -> lbl
224 isRecordSelector id = case globalIdDetails id of
225 RecordSelId lbl -> True
228 isPrimOpId id = case globalIdDetails id of
232 isPrimOpId_maybe id = case globalIdDetails id of
233 PrimOpId op -> Just op
236 isDataConId id = case globalIdDetails id of
240 isDataConId_maybe id = case globalIdDetails id of
241 DataConId con -> Just con
244 isDataConWrapId_maybe id = case globalIdDetails id of
245 DataConWrapId con -> Just con
248 isDataConWrapId id = case globalIdDetails id of
249 DataConWrapId con -> True
252 -- hasNoBinding returns True of an Id which may not have a
253 -- binding, even though it is defined in this module. Notably,
254 -- the constructors of a dictionary are in this situation.
255 hasNoBinding id = case globalIdDetails id of
260 isImplicitId :: Id -> Bool
261 -- isImplicitId tells whether an Id's info is implied by other
262 -- declarations, so we don't need to put its signature in an interface
263 -- file, even if it's mentioned in some other interface unfolding.
265 = case globalIdDetails id of
266 RecordSelId _ -> True -- Includes dictionary selectors
269 DataConWrapId _ -> True
270 -- These are are implied by their type or class decl;
271 -- remember that all type and class decls appear in the interface file.
272 -- The dfun id must *not* be omitted, because it carries version info for
278 isDeadBinder :: Id -> Bool
279 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
280 | otherwise = False -- TyVars count as not dead
284 %************************************************************************
286 \subsection{IdInfo stuff}
288 %************************************************************************
291 ---------------------------------
293 idArityInfo :: Id -> ArityInfo
294 idArityInfo id = arityInfo (idInfo id)
296 idArity :: Id -> Arity
297 idArity id = arityLowerBound (idArityInfo id)
299 setIdArityInfo :: Id -> ArityInfo -> Id
300 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
302 ---------------------------------
304 idStrictness :: Id -> StrictnessInfo
305 idStrictness id = strictnessInfo (idInfo id)
307 setIdStrictness :: Id -> StrictnessInfo -> Id
308 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
310 -- isBottomingId returns true if an application to n args would diverge
311 isBottomingId :: Id -> Bool
312 isBottomingId id = isBottomingStrictness (idStrictness id)
314 ---------------------------------
315 -- TYPE GENERALISATION
316 idTyGenInfo :: Id -> TyGenInfo
317 idTyGenInfo id = tyGenInfo (idInfo id)
319 setIdTyGenInfo :: Id -> TyGenInfo -> Id
320 setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id
322 ---------------------------------
324 idWorkerInfo :: Id -> WorkerInfo
325 idWorkerInfo id = workerInfo (idInfo id)
327 setIdWorkerInfo :: Id -> WorkerInfo -> Id
328 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
330 ---------------------------------
332 idUnfolding :: Id -> Unfolding
333 idUnfolding id = unfoldingInfo (idInfo id)
335 setIdUnfolding :: Id -> Unfolding -> Id
336 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
338 ---------------------------------
340 idDemandInfo :: Id -> Demand
341 idDemandInfo id = demandInfo (idInfo id)
343 setIdDemandInfo :: Id -> Demand -> Id
344 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
346 ---------------------------------
348 idSpecialisation :: Id -> CoreRules
349 idSpecialisation id = specInfo (idInfo id)
351 setIdSpecialisation :: Id -> CoreRules -> Id
352 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
354 ---------------------------------
356 idCgInfo :: Id -> CgInfo
358 idCgInfo id = case cgInfo (idInfo id) of
359 NoCgInfo -> pprPanic "idCgInfo" (ppr id)
362 idCgInfo id = cgInfo (idInfo id)
365 setIdCgInfo :: Id -> CgInfo -> Id
366 setIdCgInfo id cg_info = modifyIdInfo (`setCgInfo` cg_info) id
368 ---------------------------------
370 idCafInfo :: Id -> CafInfo
371 idCafInfo id = cgCafInfo (idCgInfo id)
373 ---------------------------------
376 idCgArity :: Id -> Arity
377 idCgArity id = cgArity (idCgInfo id)
379 ---------------------------------
381 idCprInfo :: Id -> CprInfo
382 idCprInfo id = cprInfo (idInfo id)
384 setIdCprInfo :: Id -> CprInfo -> Id
385 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
387 ---------------------------------
389 idOccInfo :: Id -> OccInfo
390 idOccInfo id = occInfo (idInfo id)
392 setIdOccInfo :: Id -> OccInfo -> Id
393 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
397 ---------------------------------
399 The inline pragma tells us to be very keen to inline this Id, but it's still
400 OK not to if optimisation is switched off.
403 idInlinePragma :: Id -> InlinePragInfo
404 idInlinePragma id = inlinePragInfo (idInfo id)
406 setInlinePragma :: Id -> InlinePragInfo -> Id
407 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
409 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
410 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
414 ---------------------------------
417 idLBVarInfo :: Id -> LBVarInfo
418 idLBVarInfo id = lbvarInfo (idInfo id)
420 isOneShotLambda :: Id -> Bool
421 isOneShotLambda id = analysis || hack
422 where analysis = case idLBVarInfo id of
423 LBVarInfo u | u == usOnce -> True
425 hack = case splitTyConApp_maybe (idType id) of
426 Just (tycon,_) | tycon == statePrimTyCon -> True
429 -- The last clause is a gross hack. It claims that
430 -- every function over realWorldStatePrimTy is a one-shot
431 -- function. This is pretty true in practice, and makes a big
432 -- difference. For example, consider
433 -- a `thenST` \ r -> ...E...
434 -- The early full laziness pass, if it doesn't know that r is one-shot
435 -- will pull out E (let's say it doesn't mention r) to give
436 -- let lvl = E in a `thenST` \ r -> ...lvl...
437 -- When `thenST` gets inlined, we end up with
438 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
439 -- and we don't re-inline E.
441 -- It would be better to spot that r was one-shot to start with, but
442 -- I don't want to rely on that.
444 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
445 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
447 setOneShotLambda :: Id -> Id
448 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
450 clearOneShotLambda :: Id -> Id
451 clearOneShotLambda id
452 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
455 -- But watch out: this may change the type of something else
457 -- If we change the one-shot-ness of x, f's type changes
461 zapLamIdInfo :: Id -> Id
462 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id
464 zapDemandIdInfo id = maybeModifyIdInfo zapDemandInfo id