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,
72 #include "HsVersions.h"
75 import CoreSyn ( Unfolding, CoreRules )
76 import BasicTypes ( Arity )
77 import Var ( Id, DictId,
79 idName, idType, idUnique, idInfo,
80 setIdName, setVarType, setIdUnique,
81 setIdInfo, lazySetIdInfo, modifyIdInfo,
86 import Type ( Type, tyVarsOfType, typePrimRep, addFreeTyVars,
87 usOnce, seqType, splitTyConApp_maybe )
91 import Demand ( Demand )
92 import Name ( Name, OccName,
93 mkSysLocalName, mkLocalName,
94 isUserExportedName, nameIsLocallyDefined,
97 import OccName ( UserFS )
98 import PrimRep ( PrimRep )
99 import TysPrim ( statePrimTyCon )
100 import FieldLabel ( FieldLabel )
101 import SrcLoc ( SrcLoc )
102 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques,
103 getNumBuiltinUniques )
106 infixl 1 `setIdUnfolding`,
112 `setIdSpecialisation`,
117 -- infixl so you can say (id `set` a `set` b)
122 %************************************************************************
124 \subsection{Simple Id construction}
126 %************************************************************************
128 Absolutely all Ids are made by mkId. It
129 a) Pins free-tyvar-info onto the Id's type,
130 where it can easily be found.
131 b) Ensures that exported Ids are
134 mkId :: Name -> Type -> IdInfo -> Id
135 mkId name ty info = mkIdVar name (addFreeTyVars ty) info'
137 info' | isUserExportedName name = setNoDiscardInfo info
142 mkVanillaId :: Name -> Type -> Id
143 mkVanillaId name ty = mkId name ty vanillaIdInfo
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
150 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
151 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName uniq occ loc) ty
154 Make some local @Ids@ for a template @CoreExpr@. These have bogus
155 @Uniques@, but that's OK because the templates are supposed to be
156 instantiated before use.
159 -- "Wild Id" typically used when you need a binder that you don't expect to use
160 mkWildId :: Type -> Id
161 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
163 -- "Template locals" typically used in unfoldings
164 mkTemplateLocals :: [Type] -> [Id]
165 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
166 (getBuiltinUniques (length tys))
169 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
170 mkTemplateLocalsNum n tys = zipWith (mkSysLocal SLIT("tpl"))
171 (getNumBuiltinUniques n (length tys))
174 mkTemplateLocal :: Int -> Type -> Id
175 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
179 %************************************************************************
181 \subsection[Id-general-funs]{General @Id@-related functions}
183 %************************************************************************
186 idFreeTyVars :: Id -> TyVarSet
187 idFreeTyVars id = tyVarsOfType (idType id)
189 setIdType :: Id -> Type -> Id
190 -- Add free tyvar info to the type
191 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
193 idPrimRep :: Id -> PrimRep
194 idPrimRep id = typePrimRep (idType id)
198 %************************************************************************
200 \subsection{Special Ids}
202 %************************************************************************
205 idFlavour :: Id -> IdFlavour
206 idFlavour id = flavourInfo (idInfo id)
208 setIdNoDiscard :: Id -> Id
209 setIdNoDiscard id -- Make an Id into a NoDiscardId, unless it is already
210 = modifyIdInfo setNoDiscardInfo id
212 recordSelectorFieldLabel :: Id -> FieldLabel
213 recordSelectorFieldLabel id = case idFlavour id of
214 RecordSelId lbl -> lbl
216 isRecordSelector id = case idFlavour id of
217 RecordSelId lbl -> True
220 isPrimOpId id = case idFlavour id of
224 isPrimOpId_maybe id = case idFlavour id of
225 PrimOpId op -> Just op
228 isDataConId id = case idFlavour id of
232 isDataConId_maybe id = case idFlavour id of
233 DataConId con -> Just con
236 isDataConWrapId_maybe id = case idFlavour id of
237 DataConWrapId con -> Just con
240 isDataConWrapId id = case idFlavour id of
241 DataConWrapId con -> True
244 isSpecPragmaId id = case idFlavour id of
248 hasNoBinding id = case idFlavour id of
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.
256 -- Don't drop a binding for an exported Id,
257 -- if it otherwise looks dead.
258 isExportedId :: Id -> Bool
259 isExportedId id = isUserExportedId id -- Try this
263 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
280 = ASSERT2( not (omit && nameIsLocallyDefined (idName id)
281 && idTyGenInfo id /= TyGenNever),
283 -- mustn't omit type signature for a name whose type might change!
286 omit = omitIfaceSigForId' id
288 omitIfaceSigForId' id
289 = case idFlavour id of
290 RecordSelId _ -> True -- Includes dictionary selectors
293 DataConWrapId _ -> True
294 -- These are are implied by their type or class decl;
295 -- remember that all type and class decls appear in the interface file.
296 -- The dfun id must *not* be omitted, because it carries version info for
299 other -> False -- Don't omit!
301 -- Certain names must be exported with their original occ names, because
302 -- these names are bound by either a class declaration or a data declaration
303 -- or an explicit user export.
304 exportWithOrigOccName :: Id -> Bool
305 exportWithOrigOccName id = omitIfaceSigForId id || isUserExportedId id
309 isDeadBinder :: Id -> Bool
310 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
311 | otherwise = False -- TyVars count as not dead
313 isIP id = isIPOcc (getOccName id)
317 %************************************************************************
319 \subsection{IdInfo stuff}
321 %************************************************************************
324 ---------------------------------
326 idArityInfo :: Id -> ArityInfo
327 idArityInfo id = arityInfo (idInfo id)
329 idArity :: Id -> Arity
330 idArity id = arityLowerBound (idArityInfo id)
332 setIdArityInfo :: Id -> ArityInfo -> Id
333 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
335 ---------------------------------
337 idStrictness :: Id -> StrictnessInfo
338 idStrictness id = strictnessInfo (idInfo id)
340 setIdStrictness :: Id -> StrictnessInfo -> Id
341 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
343 -- isBottomingId returns true if an application to n args would diverge
344 isBottomingId :: Id -> Bool
345 isBottomingId id = isBottomingStrictness (idStrictness id)
347 ---------------------------------
348 -- TYPE GENERALISATION
349 idTyGenInfo :: Id -> TyGenInfo
350 idTyGenInfo id = tyGenInfo (idInfo id)
352 setIdTyGenInfo :: Id -> TyGenInfo -> Id
353 setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id
355 ---------------------------------
357 idWorkerInfo :: Id -> WorkerInfo
358 idWorkerInfo id = workerInfo (idInfo id)
360 setIdWorkerInfo :: Id -> WorkerInfo -> Id
361 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
363 ---------------------------------
365 idUnfolding :: Id -> Unfolding
366 idUnfolding id = unfoldingInfo (idInfo id)
368 setIdUnfolding :: Id -> Unfolding -> Id
369 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
371 ---------------------------------
373 idDemandInfo :: Id -> Demand
374 idDemandInfo id = demandInfo (idInfo id)
376 setIdDemandInfo :: Id -> Demand -> Id
377 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
379 ---------------------------------
381 idSpecialisation :: Id -> CoreRules
382 idSpecialisation id = specInfo (idInfo id)
384 setIdSpecialisation :: Id -> CoreRules -> Id
385 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
387 ---------------------------------
389 idCafInfo :: Id -> CafInfo
390 idCafInfo id = cafInfo (idInfo id)
392 setIdCafInfo :: Id -> CafInfo -> Id
393 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
395 ---------------------------------
397 idCprInfo :: Id -> CprInfo
398 idCprInfo id = cprInfo (idInfo id)
400 setIdCprInfo :: Id -> CprInfo -> Id
401 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
403 ---------------------------------
405 idOccInfo :: Id -> OccInfo
406 idOccInfo id = occInfo (idInfo id)
408 setIdOccInfo :: Id -> OccInfo -> Id
409 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
413 ---------------------------------
415 The inline pragma tells us to be very keen to inline this Id, but it's still
416 OK not to if optimisation is switched off.
419 idInlinePragma :: Id -> InlinePragInfo
420 idInlinePragma id = inlinePragInfo (idInfo id)
422 setInlinePragma :: Id -> InlinePragInfo -> Id
423 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
425 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
426 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
430 ---------------------------------
433 idLBVarInfo :: Id -> LBVarInfo
434 idLBVarInfo id = lbvarInfo (idInfo id)
436 isOneShotLambda :: Id -> Bool
437 isOneShotLambda id = analysis || hack
438 where analysis = case idLBVarInfo id of
439 LBVarInfo u | u == usOnce -> True
441 hack = case splitTyConApp_maybe (idType id) of
442 Just (tycon,_) | tycon == statePrimTyCon -> True
445 -- The last clause is a gross hack. It claims that
446 -- every function over realWorldStatePrimTy is a one-shot
447 -- function. This is pretty true in practice, and makes a big
448 -- difference. For example, consider
449 -- a `thenST` \ r -> ...E...
450 -- The early full laziness pass, if it doesn't know that r is one-shot
451 -- will pull out E (let's say it doesn't mention r) to give
452 -- let lvl = E in a `thenST` \ r -> ...lvl...
453 -- When `thenST` gets inlined, we end up with
454 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
455 -- and we don't re-inline E.
457 -- It would be better to spot that r was one-shot to start with, but
458 -- I don't want to rely on that.
460 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
461 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
463 setOneShotLambda :: Id -> Id
464 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
466 clearOneShotLambda :: Id -> Id
467 clearOneShotLambda id
468 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
471 -- But watch out: this may change the type of something else
473 -- If we change the one-shot-ness of x, f's type changes
477 zapFragileIdInfo :: Id -> Id
478 zapFragileIdInfo id = maybeModifyIdInfo zapFragileInfo id
480 zapLamIdInfo :: Id -> Id
481 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id