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,
28 isSpecPragmaId, isRecordSelector,
29 isPrimOpId, isPrimOpId_maybe, isDictFunId,
30 isDataConId, isDataConId_maybe,
31 isDataConWrapId, isDataConWrapId_maybe,
33 isExportedId, isLocalId,
36 -- Inline pragma stuff
37 idInlinePragma, setInlinePragma, modifyInlinePragma,
40 -- One shot lambda stuff
41 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
70 #include "HsVersions.h"
73 import CoreSyn ( Unfolding, CoreRules )
74 import BasicTypes ( Arity )
75 import Var ( Id, DictId,
77 idName, idType, idUnique, idInfo,
78 setIdName, setVarType, setIdUnique,
79 setIdInfo, lazySetIdInfo, modifyIdInfo,
83 import Type ( Type, typePrimRep, addFreeTyVars,
84 usOnce, seqType, splitTyConApp_maybe )
88 import Demand ( Demand )
89 import Name ( Name, OccName,
90 mkSysLocalName, mkLocalName,
94 import OccName ( UserFS )
95 import PrimRep ( PrimRep )
96 import TysPrim ( statePrimTyCon )
97 import FieldLabel ( FieldLabel )
98 import SrcLoc ( SrcLoc )
99 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques,
100 getNumBuiltinUniques )
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
126 a) Pins free-tyvar-info onto the Id's type,
127 where it can easily be found.
128 b) Ensures that exported Ids are
131 mkId :: Name -> Type -> IdInfo -> Id
132 mkId name ty info = mkIdVar name (addFreeTyVars ty) info
136 mkVanillaId :: Name -> Type -> Id
137 mkVanillaId name ty = mkId name ty vanillaIdInfo
139 -- SysLocal: for an Id being created by the compiler out of thin air...
140 -- UserLocal: an Id with a name the user might recognize...
141 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
142 mkSysLocal :: UserFS -> Unique -> Type -> Id
144 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
145 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName uniq occ loc) ty
148 Make some local @Ids@ for a template @CoreExpr@. These have bogus
149 @Uniques@, but that's OK because the templates are supposed to be
150 instantiated before use.
153 -- "Wild Id" typically used when you need a binder that you don't expect to use
154 mkWildId :: Type -> Id
155 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
157 -- "Template locals" typically used in unfoldings
158 mkTemplateLocals :: [Type] -> [Id]
159 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
160 (getBuiltinUniques (length tys))
163 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
164 mkTemplateLocalsNum n tys = zipWith (mkSysLocal SLIT("tpl"))
165 (getNumBuiltinUniques n (length tys))
168 mkTemplateLocal :: Int -> Type -> Id
169 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
173 %************************************************************************
175 \subsection[Id-general-funs]{General @Id@-related functions}
177 %************************************************************************
180 setIdType :: Id -> Type -> Id
181 -- Add free tyvar info to the type
182 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
184 idPrimRep :: Id -> PrimRep
185 idPrimRep id = typePrimRep (idType id)
189 %************************************************************************
191 \subsection{Special Ids}
193 %************************************************************************
196 idFlavour :: Id -> IdFlavour
197 idFlavour id = flavourInfo (idInfo id)
199 setIdNoDiscard :: Id -> Id
200 setIdNoDiscard id -- Make an Id into a NoDiscardId, unless it is already
201 = modifyIdInfo setNoDiscardInfo id
203 recordSelectorFieldLabel :: Id -> FieldLabel
204 recordSelectorFieldLabel id = case idFlavour id of
205 RecordSelId lbl -> lbl
207 isRecordSelector id = case idFlavour id of
208 RecordSelId lbl -> True
211 isPrimOpId id = case idFlavour id of
215 isPrimOpId_maybe id = case idFlavour id of
216 PrimOpId op -> Just op
219 isDataConId id = case idFlavour id of
223 isDataConId_maybe id = case idFlavour id of
224 DataConId con -> Just con
227 isDataConWrapId_maybe id = case idFlavour id of
228 DataConWrapId con -> Just con
231 isDataConWrapId id = case idFlavour id of
232 DataConWrapId con -> True
235 isSpecPragmaId id = case idFlavour id of
239 hasNoBinding id = case idFlavour id of
243 -- hasNoBinding returns True of an Id which may not have a
244 -- binding, even though it is defined in this module. Notably,
245 -- the constructors of a dictionary are in this situation.
247 isDictFunId id = case idFlavour id of
251 -- Don't drop a binding for an exported Id,
252 -- if it otherwise looks dead.
253 -- Perhaps a better name would be isDiscardableId
254 isExportedId :: Id -> Bool
255 isExportedId id = case idFlavour id of
259 isLocalId :: Id -> Bool
260 -- True of Ids that are locally defined, but are not constants
261 -- like data constructors, record selectors, and the like.
262 -- See comments with CoreFVs.isLocalVar
265 | not (isId id) = pprTrace "isLocalid" (ppr id) False
268 = case idFlavour id of
276 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
277 so we don't need to put its signature in an interface file, even if it's mentioned
278 in some other interface unfolding.
281 omitIfaceSigForId :: Id -> Bool
283 = ASSERT2( not (omit && nameIsLocallyDefined (idName id)
284 && idTyGenInfo id /= TyGenNever),
286 -- mustn't omit type signature for a name whose type might change!
289 omit = omitIfaceSigForId' id
291 omitIfaceSigForId' id
292 = case idFlavour id of
293 RecordSelId _ -> True -- Includes dictionary selectors
296 DataConWrapId _ -> True
297 -- These are are implied by their type or class decl;
298 -- remember that all type and class decls appear in the interface file.
299 -- The dfun id must *not* be omitted, because it carries version info for
302 ConstantId -> False -- Ordinary Ids
305 ExportedId -> False -- I don't think these happen
306 VanillaId -> False -- ditto
307 SpecPragmaId -> False -- ditto
311 isDeadBinder :: Id -> Bool
312 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
313 | otherwise = False -- TyVars count as not dead
315 isIP id = isIPOcc (getOccName id)
319 %************************************************************************
321 \subsection{IdInfo stuff}
323 %************************************************************************
326 ---------------------------------
328 idArityInfo :: Id -> ArityInfo
329 idArityInfo id = arityInfo (idInfo id)
331 idArity :: Id -> Arity
332 idArity id = arityLowerBound (idArityInfo id)
334 setIdArityInfo :: Id -> ArityInfo -> Id
335 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
337 ---------------------------------
339 idStrictness :: Id -> StrictnessInfo
340 idStrictness id = strictnessInfo (idInfo id)
342 setIdStrictness :: Id -> StrictnessInfo -> Id
343 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
345 -- isBottomingId returns true if an application to n args would diverge
346 isBottomingId :: Id -> Bool
347 isBottomingId id = isBottomingStrictness (idStrictness id)
349 ---------------------------------
350 -- TYPE GENERALISATION
351 idTyGenInfo :: Id -> TyGenInfo
352 idTyGenInfo id = tyGenInfo (idInfo id)
354 setIdTyGenInfo :: Id -> TyGenInfo -> Id
355 setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id
357 ---------------------------------
359 idWorkerInfo :: Id -> WorkerInfo
360 idWorkerInfo id = workerInfo (idInfo id)
362 setIdWorkerInfo :: Id -> WorkerInfo -> Id
363 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
365 ---------------------------------
367 idUnfolding :: Id -> Unfolding
368 idUnfolding id = unfoldingInfo (idInfo id)
370 setIdUnfolding :: Id -> Unfolding -> Id
371 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
373 ---------------------------------
375 idDemandInfo :: Id -> Demand
376 idDemandInfo id = demandInfo (idInfo id)
378 setIdDemandInfo :: Id -> Demand -> Id
379 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
381 ---------------------------------
383 idSpecialisation :: Id -> CoreRules
384 idSpecialisation id = specInfo (idInfo id)
386 setIdSpecialisation :: Id -> CoreRules -> Id
387 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
389 ---------------------------------
391 idCafInfo :: Id -> CafInfo
392 idCafInfo id = cafInfo (idInfo id)
394 setIdCafInfo :: Id -> CafInfo -> Id
395 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
397 ---------------------------------
399 idCprInfo :: Id -> CprInfo
400 idCprInfo id = cprInfo (idInfo id)
402 setIdCprInfo :: Id -> CprInfo -> Id
403 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
405 ---------------------------------
407 idOccInfo :: Id -> OccInfo
408 idOccInfo id = occInfo (idInfo id)
410 setIdOccInfo :: Id -> OccInfo -> Id
411 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
415 ---------------------------------
417 The inline pragma tells us to be very keen to inline this Id, but it's still
418 OK not to if optimisation is switched off.
421 idInlinePragma :: Id -> InlinePragInfo
422 idInlinePragma id = inlinePragInfo (idInfo id)
424 setInlinePragma :: Id -> InlinePragInfo -> Id
425 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
427 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
428 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
432 ---------------------------------
435 idLBVarInfo :: Id -> LBVarInfo
436 idLBVarInfo id = lbvarInfo (idInfo id)
438 isOneShotLambda :: Id -> Bool
439 isOneShotLambda id = analysis || hack
440 where analysis = case idLBVarInfo id of
441 LBVarInfo u | u == usOnce -> True
443 hack = case splitTyConApp_maybe (idType id) of
444 Just (tycon,_) | tycon == statePrimTyCon -> True
447 -- The last clause is a gross hack. It claims that
448 -- every function over realWorldStatePrimTy is a one-shot
449 -- function. This is pretty true in practice, and makes a big
450 -- difference. For example, consider
451 -- a `thenST` \ r -> ...E...
452 -- The early full laziness pass, if it doesn't know that r is one-shot
453 -- will pull out E (let's say it doesn't mention r) to give
454 -- let lvl = E in a `thenST` \ r -> ...lvl...
455 -- When `thenST` gets inlined, we end up with
456 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
457 -- and we don't re-inline E.
459 -- It would be better to spot that r was one-shot to start with, but
460 -- I don't want to rely on that.
462 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
463 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
465 setOneShotLambda :: Id -> Id
466 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
468 clearOneShotLambda :: Id -> Id
469 clearOneShotLambda id
470 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
473 -- But watch out: this may change the type of something else
475 -- If we change the one-shot-ness of x, f's type changes
479 zapFragileIdInfo :: Id -> Id
480 zapFragileIdInfo id = maybeModifyIdInfo zapFragileInfo id
482 zapLamIdInfo :: Id -> Id
483 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id