2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Id]{@Ids@: Value and constructor identifiers}
10 -- Simple construction
11 mkId, mkVanillaId, mkSysLocal, mkUserLocal,
12 mkTemplateLocals, mkTemplateLocalsNum, mkWildId, mkTemplateLocal,
15 idName, idType, idUnique, idInfo,
17 recordSelectorFieldLabel,
20 setIdName, setIdUnique, setIdType, setIdNoDiscard,
21 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
22 zapFragileIdInfo, zapLamIdInfo,
25 omitIfaceSigForId, isDeadBinder,
26 exportWithOrigOccName,
30 isSpecPragmaId, isRecordSelector,
31 isPrimOpId, isPrimOpId_maybe,
32 isDataConId, isDataConId_maybe, isDataConWrapId,
33 isDataConWrapId_maybe,
35 isExportedId, isUserExportedId,
38 -- Inline pragma stuff
39 idInlinePragma, setInlinePragma, modifyInlinePragma,
42 -- One shot lambda stuff
43 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
70 #include "HsVersions.h"
73 import CoreSyn ( Unfolding, CoreRules, CoreExpr, Expr(..),
74 AltCon (..), Alt, mkApps, Arg )
75 import BasicTypes ( Arity )
76 import Var ( Id, DictId,
78 idName, idType, idUnique, idInfo,
79 setIdName, setVarType, setIdUnique,
80 setIdInfo, lazySetIdInfo, modifyIdInfo,
85 import Type ( Type, tyVarsOfType, typePrimRep, addFreeTyVars,
86 seqType, splitAlgTyConApp_maybe, mkTyVarTy,
87 mkTyConApp, splitTyConApp_maybe)
91 import Demand ( Demand, isStrict, wwLazy )
92 import Name ( Name, OccName,
93 mkSysLocalName, mkLocalName,
94 isUserExportedName, getOccName, isIPOcc
96 import OccName ( UserFS )
97 import PrimRep ( PrimRep )
98 import PrimOp ( PrimOp, primOpIsCheap )
99 import TysPrim ( statePrimTyCon )
100 import FieldLabel ( FieldLabel )
101 import SrcLoc ( SrcLoc )
102 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques,
103 getNumBuiltinUniques )
105 import TyCon ( TyCon, AlgTyConFlavour(..), ArgVrcs, mkSynTyCon,
106 mkAlgTyConRep, tyConName,
107 tyConTyVars, tyConDataCons )
108 import DataCon ( DataCon, dataConWrapId, dataConOrigArgTys )
110 infixl 1 `setIdUnfolding`,
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
132 a) Pins free-tyvar-info onto the Id's type,
133 where it can easily be found.
134 b) Ensures that exported Ids are
137 mkId :: Name -> Type -> IdInfo -> Id
138 mkId name ty info = mkIdVar name (addFreeTyVars ty) info'
140 info' | isUserExportedName name = setNoDiscardInfo info
145 mkVanillaId :: Name -> Type -> Id
146 mkVanillaId name ty = mkId name ty vanillaIdInfo
148 -- SysLocal: for an Id being created by the compiler out of thin air...
149 -- UserLocal: an Id with a name the user might recognize...
150 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
151 mkSysLocal :: UserFS -> Unique -> Type -> Id
153 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
154 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName uniq occ loc) ty
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 -- "Template locals" typically used in unfoldings
167 mkTemplateLocals :: [Type] -> [Id]
168 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
169 (getBuiltinUniques (length tys))
172 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
173 mkTemplateLocalsNum n tys = zipWith (mkSysLocal SLIT("tpl"))
174 (getNumBuiltinUniques n (length tys))
177 mkTemplateLocal :: Int -> Type -> Id
178 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
182 %************************************************************************
184 \subsection[Id-general-funs]{General @Id@-related functions}
186 %************************************************************************
189 idFreeTyVars :: Id -> TyVarSet
190 idFreeTyVars id = tyVarsOfType (idType id)
192 setIdType :: Id -> Type -> Id
193 -- Add free tyvar info to the type
194 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
196 idPrimRep :: Id -> PrimRep
197 idPrimRep id = typePrimRep (idType id)
201 %************************************************************************
203 \subsection{Special Ids}
205 %************************************************************************
208 idFlavour :: Id -> IdFlavour
209 idFlavour id = flavourInfo (idInfo id)
211 setIdNoDiscard :: Id -> Id
212 setIdNoDiscard id -- Make an Id into a NoDiscardId, unless it is already
213 = modifyIdInfo setNoDiscardInfo id
215 recordSelectorFieldLabel :: Id -> FieldLabel
216 recordSelectorFieldLabel id = case idFlavour id of
217 RecordSelId lbl -> lbl
219 isRecordSelector id = case idFlavour id of
220 RecordSelId lbl -> True
223 isPrimOpId id = case idFlavour id of
227 isPrimOpId_maybe id = case idFlavour id of
228 PrimOpId op -> Just op
231 isDataConId id = case idFlavour id of
235 isDataConId_maybe id = case idFlavour id of
236 DataConId con -> Just con
239 isDataConWrapId_maybe id = case idFlavour id of
240 DataConWrapId con -> Just con
243 isDataConWrapId id = case idFlavour id of
244 DataConWrapId con -> True
247 isSpecPragmaId id = case idFlavour id of
251 hasNoBinding id = case idFlavour id of
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.
259 -- Don't drop a binding for an exported Id,
260 -- if it otherwise looks dead.
261 isExportedId :: Id -> Bool
262 isExportedId id = case idFlavour id of
264 other -> True -- All the others are no-discard
266 -- Say if an Id was exported by the user
267 -- Implies isExportedId (see mkId above)
268 isUserExportedId :: Id -> Bool
269 isUserExportedId id = isUserExportedName (idName id)
273 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
274 so we don't need to put its signature in an interface file, even if it's mentioned
275 in some other interface unfolding.
278 omitIfaceSigForId :: Id -> Bool
281 = case idFlavour id of
282 RecordSelId _ -> True -- Includes dictionary selectors
285 DataConWrapId _ -> True
286 -- These are are implied by their type or class decl;
287 -- remember that all type and class decls appear in the interface file.
288 -- The dfun id must *not* be omitted, because it carries version info for
291 other -> False -- Don't omit!
293 -- Certain names must be exported with their original occ names, because
294 -- these names are bound by either a class declaration or a data declaration
295 -- or an explicit user export.
296 exportWithOrigOccName :: Id -> Bool
297 exportWithOrigOccName id = omitIfaceSigForId id || isUserExportedId id
301 isDeadBinder :: Id -> Bool
302 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
303 | otherwise = False -- TyVars count as not dead
305 isIP id = isIPOcc (getOccName id)
309 %************************************************************************
311 \subsection{IdInfo stuff}
313 %************************************************************************
316 ---------------------------------
318 idArityInfo :: Id -> ArityInfo
319 idArityInfo id = arityInfo (idInfo id)
321 idArity :: Id -> Arity
322 idArity id = arityLowerBound (idArityInfo id)
324 setIdArityInfo :: Id -> ArityInfo -> Id
325 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
327 ---------------------------------
329 idStrictness :: Id -> StrictnessInfo
330 idStrictness id = strictnessInfo (idInfo id)
332 setIdStrictness :: Id -> StrictnessInfo -> Id
333 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
335 -- isBottomingId returns true if an application to n args would diverge
336 isBottomingId :: Id -> Bool
337 isBottomingId id = isBottomingStrictness (idStrictness id)
339 ---------------------------------
341 idWorkerInfo :: Id -> WorkerInfo
342 idWorkerInfo id = workerInfo (idInfo id)
344 setIdWorkerInfo :: Id -> WorkerInfo -> Id
345 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
347 ---------------------------------
349 idUnfolding :: Id -> Unfolding
350 idUnfolding id = unfoldingInfo (idInfo id)
352 setIdUnfolding :: Id -> Unfolding -> Id
353 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
355 ---------------------------------
357 idDemandInfo :: Id -> Demand
358 idDemandInfo id = demandInfo (idInfo id)
360 setIdDemandInfo :: Id -> Demand -> Id
361 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
363 ---------------------------------
365 idSpecialisation :: Id -> CoreRules
366 idSpecialisation id = specInfo (idInfo id)
368 setIdSpecialisation :: Id -> CoreRules -> Id
369 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
371 ---------------------------------
373 idCafInfo :: Id -> CafInfo
374 idCafInfo id = cafInfo (idInfo id)
376 setIdCafInfo :: Id -> CafInfo -> Id
377 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) 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 = case idLBVarInfo id of
422 IsOneShotLambda -> True
423 NoLBVarInfo -> case splitTyConApp_maybe (idType id) of
424 Just (tycon,_) -> tycon == statePrimTyCon
426 -- The last clause is a gross hack. It claims that
427 -- every function over realWorldStatePrimTy is a one-shot
428 -- function. This is pretty true in practice, and makes a big
429 -- difference. For example, consider
430 -- a `thenST` \ r -> ...E...
431 -- The early full laziness pass, if it doesn't know that r is one-shot
432 -- will pull out E (let's say it doesn't mention r) to give
433 -- let lvl = E in a `thenST` \ r -> ...lvl...
434 -- When `thenST` gets inlined, we end up with
435 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
436 -- and we don't re-inline E.
438 -- It would be better to spot that r was one-shot to start with, but
439 -- I don't want to rely on that.
441 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
442 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
444 setOneShotLambda :: Id -> Id
445 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
447 clearOneShotLambda :: Id -> Id
448 clearOneShotLambda id
449 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
452 -- But watch out: this may change the type of something else
454 -- If we change the one-shot-ness of x, f's type changes
458 zapFragileIdInfo :: Id -> Id
459 zapFragileIdInfo id = maybeModifyIdInfo zapFragileInfo id
461 zapLamIdInfo :: Id -> Id
462 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id