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,
69 #include "HsVersions.h"
72 import CoreSyn ( Unfolding, CoreRules )
73 import BasicTypes ( Arity )
74 import Var ( Id, DictId,
75 isId, isExportedId, isSpecPragmaId, isLocalId,
76 idName, idType, idUnique, idInfo, isGlobalId,
77 setIdName, setVarType, setIdUnique, setIdNoDiscard,
78 setIdInfo, lazySetIdInfo, modifyIdInfo,
80 globalIdDetails, setGlobalIdDetails
82 import qualified Var ( mkLocalId, mkGlobalId, mkSpecPragmaId )
83 import Type ( Type, typePrimRep, addFreeTyVars,
84 usOnce, seqType, splitTyConApp_maybe )
88 import Demand ( Demand )
89 import Name ( Name, OccName,
90 mkSysLocalName, mkLocalName,
93 import OccName ( UserFS, mkWorkerOcc )
94 import PrimRep ( PrimRep )
95 import TysPrim ( statePrimTyCon )
96 import FieldLabel ( FieldLabel )
97 import SrcLoc ( SrcLoc )
98 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques,
99 getNumBuiltinUniques )
102 infixl 1 `setIdUnfolding`,
108 `setIdSpecialisation`,
113 -- infixl so you can say (id `set` a `set` b)
118 %************************************************************************
120 \subsection{Simple Id construction}
122 %************************************************************************
124 Absolutely all Ids are made by mkId. It is just like Var.mkId,
125 but in addition it pins free-tyvar-info onto the Id's type,
126 where it can easily be found.
129 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
130 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
132 mkSpecPragmaId :: OccName -> Unique -> Type -> SrcLoc -> Id
133 mkSpecPragmaId occ uniq ty loc = Var.mkSpecPragmaId (mkLocalName uniq occ loc)
137 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
138 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
142 mkLocalId :: Name -> Type -> Id
143 mkLocalId name ty = mkLocalIdWithInfo name ty noCafIdInfo
145 -- SysLocal: for an Id being created by the compiler out of thin air...
146 -- UserLocal: an Id with a name the user might recognize...
147 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
148 mkSysLocal :: UserFS -> Unique -> Type -> Id
149 mkVanillaGlobal :: Name -> Type -> IdInfo -> Id
151 mkSysLocal fs uniq ty = mkLocalId (mkSysLocalName uniq fs) ty
152 mkUserLocal occ uniq ty loc = mkLocalId (mkLocalName uniq occ loc) ty
153 mkVanillaGlobal = mkGlobalId VanillaGlobal
156 Make some local @Ids@ for a template @CoreExpr@. These have bogus
157 @Uniques@, but that's OK because the templates are supposed to be
158 instantiated before use.
161 -- "Wild Id" typically used when you need a binder that you don't expect to use
162 mkWildId :: Type -> Id
163 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
165 mkWorkerId :: Unique -> Id -> Type -> Id
166 -- A worker gets a local name. CoreTidy will globalise it if necessary.
167 mkWorkerId uniq unwrkr ty
168 = mkLocalId wkr_name ty
170 wkr_name = mkLocalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcLoc unwrkr)
172 -- "Template locals" typically used in unfoldings
173 mkTemplateLocals :: [Type] -> [Id]
174 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
175 (getBuiltinUniques (length tys))
178 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
179 -- The Int gives the starting point for unique allocation
180 mkTemplateLocalsNum n tys = zipWith (mkSysLocal SLIT("tpl"))
181 (getNumBuiltinUniques n (length tys))
184 mkTemplateLocal :: Int -> Type -> Id
185 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
189 %************************************************************************
191 \subsection[Id-general-funs]{General @Id@-related functions}
193 %************************************************************************
196 setIdType :: Id -> Type -> Id
197 -- Add free tyvar info to the type
198 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
200 idPrimRep :: Id -> PrimRep
201 idPrimRep id = typePrimRep (idType id)
205 %************************************************************************
207 \subsection{Special Ids}
209 %************************************************************************
211 The @SpecPragmaId@ exists only to make Ids that are
212 on the *LHS* of bindings created by SPECIALISE pragmas;
214 The SpecPragmaId is never itself mentioned; it
215 exists solely so that the specialiser will find
216 the call to f, and make specialised version of it.
217 The SpecPragmaId binding is discarded by the specialiser
218 when it gathers up overloaded calls.
219 Meanwhile, it is not discarded as dead code.
223 recordSelectorFieldLabel :: Id -> FieldLabel
224 recordSelectorFieldLabel id = case globalIdDetails id of
225 RecordSelId lbl -> lbl
227 isRecordSelector id = case globalIdDetails id of
228 RecordSelId lbl -> True
231 isPrimOpId id = case globalIdDetails id of
235 isPrimOpId_maybe id = case globalIdDetails id of
236 PrimOpId op -> Just op
239 isDataConId id = case globalIdDetails id of
243 isDataConId_maybe id = case globalIdDetails id of
244 DataConId con -> Just con
247 isDataConWrapId_maybe id = case globalIdDetails id of
248 DataConWrapId con -> Just con
251 isDataConWrapId id = case globalIdDetails id of
252 DataConWrapId con -> True
255 -- hasNoBinding returns True of an Id which may not have a
256 -- binding, even though it is defined in this module. Notably,
257 -- the constructors of a dictionary are in this situation.
258 hasNoBinding id = case globalIdDetails id of
263 isImplicitId :: Id -> Bool
264 -- isImplicitId tells whether an Id's info is implied by other
265 -- declarations, so we don't need to put its signature in an interface
266 -- file, even if it's mentioned in some other interface unfolding.
268 = case globalIdDetails id of
269 RecordSelId _ -> True -- Includes dictionary selectors
272 DataConWrapId _ -> True
273 -- These are are implied by their type or class decl;
274 -- remember that all type and class decls appear in the interface file.
275 -- The dfun id must *not* be omitted, because it carries version info for
281 isDeadBinder :: Id -> Bool
282 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
283 | otherwise = False -- TyVars count as not dead
287 %************************************************************************
289 \subsection{IdInfo stuff}
291 %************************************************************************
294 ---------------------------------
296 idArityInfo :: Id -> ArityInfo
297 idArityInfo id = arityInfo (idInfo id)
299 idArity :: Id -> Arity
300 idArity id = arityLowerBound (idArityInfo id)
302 setIdArityInfo :: Id -> ArityInfo -> Id
303 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
305 ---------------------------------
307 idStrictness :: Id -> StrictnessInfo
308 idStrictness id = strictnessInfo (idInfo id)
310 setIdStrictness :: Id -> StrictnessInfo -> Id
311 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
313 -- isBottomingId returns true if an application to n args would diverge
314 isBottomingId :: Id -> Bool
315 isBottomingId id = isBottomingStrictness (idStrictness id)
317 ---------------------------------
318 -- TYPE GENERALISATION
319 idTyGenInfo :: Id -> TyGenInfo
320 idTyGenInfo id = tyGenInfo (idInfo id)
322 setIdTyGenInfo :: Id -> TyGenInfo -> Id
323 setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id
325 ---------------------------------
327 idWorkerInfo :: Id -> WorkerInfo
328 idWorkerInfo id = workerInfo (idInfo id)
330 setIdWorkerInfo :: Id -> WorkerInfo -> Id
331 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
333 ---------------------------------
335 idUnfolding :: Id -> Unfolding
336 idUnfolding id = unfoldingInfo (idInfo id)
338 setIdUnfolding :: Id -> Unfolding -> Id
339 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
341 ---------------------------------
343 idDemandInfo :: Id -> Demand
344 idDemandInfo id = demandInfo (idInfo id)
346 setIdDemandInfo :: Id -> Demand -> Id
347 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
349 ---------------------------------
351 idSpecialisation :: Id -> CoreRules
352 idSpecialisation id = specInfo (idInfo id)
354 setIdSpecialisation :: Id -> CoreRules -> Id
355 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
357 ---------------------------------
359 idCafInfo :: Id -> CafInfo
360 idCafInfo id = cafInfo (idInfo id)
362 setIdCafInfo :: Id -> CafInfo -> Id
363 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
365 ---------------------------------
367 idCprInfo :: Id -> CprInfo
368 idCprInfo id = cprInfo (idInfo id)
370 setIdCprInfo :: Id -> CprInfo -> Id
371 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
373 ---------------------------------
375 idOccInfo :: Id -> OccInfo
376 idOccInfo id = occInfo (idInfo id)
378 setIdOccInfo :: Id -> OccInfo -> Id
379 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
383 ---------------------------------
385 The inline pragma tells us to be very keen to inline this Id, but it's still
386 OK not to if optimisation is switched off.
389 idInlinePragma :: Id -> InlinePragInfo
390 idInlinePragma id = inlinePragInfo (idInfo id)
392 setInlinePragma :: Id -> InlinePragInfo -> Id
393 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
395 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
396 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
400 ---------------------------------
403 idLBVarInfo :: Id -> LBVarInfo
404 idLBVarInfo id = lbvarInfo (idInfo id)
406 isOneShotLambda :: Id -> Bool
407 isOneShotLambda id = analysis || hack
408 where analysis = case idLBVarInfo id of
409 LBVarInfo u | u == usOnce -> True
411 hack = case splitTyConApp_maybe (idType id) of
412 Just (tycon,_) | tycon == statePrimTyCon -> True
415 -- The last clause is a gross hack. It claims that
416 -- every function over realWorldStatePrimTy is a one-shot
417 -- function. This is pretty true in practice, and makes a big
418 -- difference. For example, consider
419 -- a `thenST` \ r -> ...E...
420 -- The early full laziness pass, if it doesn't know that r is one-shot
421 -- will pull out E (let's say it doesn't mention r) to give
422 -- let lvl = E in a `thenST` \ r -> ...lvl...
423 -- When `thenST` gets inlined, we end up with
424 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
425 -- and we don't re-inline E.
427 -- It would be better to spot that r was one-shot to start with, but
428 -- I don't want to rely on that.
430 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
431 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
433 setOneShotLambda :: Id -> Id
434 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
436 clearOneShotLambda :: Id -> Id
437 clearOneShotLambda id
438 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
441 -- But watch out: this may change the type of something else
443 -- If we change the one-shot-ness of x, f's type changes
447 zapLamIdInfo :: Id -> Id
448 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id
450 zapDemandIdInfo id = maybeModifyIdInfo zapDemandInfo id