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, 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,
31 -- Inline pragma stuff
32 idInlinePragma, setInlinePragma, modifyInlinePragma,
34 isSpecPragmaId, isRecordSelector,
35 isPrimOpId, isPrimOpId_maybe,
36 isDataConId, isDataConId_maybe, isDataConWrapId, isDataConWrapId_maybe,
38 isExportedId, isUserExportedId,
41 -- One shot lambda stuff
42 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
71 #include "HsVersions.h"
73 import {-# SOURCE #-} CoreUnfold ( Unfolding )
74 import {-# SOURCE #-} CoreSyn ( CoreRules )
76 import BasicTypes ( Arity )
77 import Var ( Id, DictId,
79 idName, idType, idUnique, idInfo,
80 setIdName, setVarType, setIdUnique,
81 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
85 import Type ( Type, tyVarsOfType, typePrimRep, addFreeTyVars, seqType, splitTyConApp_maybe )
89 import Demand ( Demand, isStrict, wwLazy )
90 import Name ( Name, OccName,
91 mkSysLocalName, mkLocalName,
92 isWiredInName, isUserExportedName,
95 import OccName ( UserFS )
96 import PrimRep ( PrimRep )
97 import PrimOp ( PrimOp, primOpIsCheap )
98 import TysPrim ( statePrimTyCon )
99 import FieldLabel ( FieldLabel(..) )
100 import SrcLoc ( SrcLoc )
101 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques )
104 infixl 1 `setIdUnfolding`,
109 `setIdSpecialisation`,
115 -- infixl so you can say (id `set` a `set` b)
120 %************************************************************************
122 \subsection{Simple Id construction}
124 %************************************************************************
126 Absolutely all Ids are made by mkId. It
127 a) Pins free-tyvar-info onto the Id's type,
128 where it can easily be found.
129 b) Ensures that exported Ids are
132 mkId :: Name -> Type -> IdInfo -> Id
133 mkId name ty info = mkIdVar name (addFreeTyVars ty) info'
135 info' | isUserExportedName name = setNoDiscardInfo info
140 mkVanillaId :: Name -> Type -> Id
141 mkVanillaId name ty = mkId name ty vanillaIdInfo
143 -- SysLocal: for an Id being created by the compiler out of thin air...
144 -- UserLocal: an Id with a name the user might recognize...
145 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
146 mkSysLocal :: UserFS -> Unique -> Type -> Id
148 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
149 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName uniq occ loc) ty
152 Make some local @Ids@ for a template @CoreExpr@. These have bogus
153 @Uniques@, but that's OK because the templates are supposed to be
154 instantiated before use.
157 -- "Wild Id" typically used when you need a binder that you don't expect to use
158 mkWildId :: Type -> Id
159 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
161 -- "Template locals" typically used in unfoldings
162 mkTemplateLocals :: [Type] -> [Id]
163 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
164 (getBuiltinUniques (length tys))
167 mkTemplateLocal :: Int -> Type -> Id
168 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
172 %************************************************************************
174 \subsection[Id-general-funs]{General @Id@-related functions}
176 %************************************************************************
179 idFreeTyVars :: Id -> TyVarSet
180 idFreeTyVars id = tyVarsOfType (idType id)
182 setIdType :: Id -> Type -> Id
183 -- Add free tyvar info to the type
184 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
186 idPrimRep :: Id -> PrimRep
187 idPrimRep id = typePrimRep (idType id)
191 %************************************************************************
193 \subsection{Special Ids}
195 %************************************************************************
198 idFlavour :: Id -> IdFlavour
199 idFlavour id = flavourInfo (idInfo id)
201 setIdNoDiscard :: Id -> Id
202 setIdNoDiscard id -- Make an Id into a NoDiscardId, unless it is already
203 = modifyIdInfo setNoDiscardInfo id
205 recordSelectorFieldLabel :: Id -> FieldLabel
206 recordSelectorFieldLabel id = case idFlavour id of
207 RecordSelId lbl -> lbl
209 isRecordSelector id = case idFlavour id of
210 RecordSelId lbl -> True
213 isPrimOpId id = case idFlavour id of
217 isPrimOpId_maybe id = case idFlavour id of
218 PrimOpId op -> Just op
221 isDataConId id = case idFlavour id of
225 isDataConId_maybe id = case idFlavour id of
226 DataConId con -> Just con
229 isDataConWrapId_maybe id = case idFlavour id of
230 DataConWrapId con -> Just con
233 isDataConWrapId id = case idFlavour id of
234 DataConWrapId con -> True
237 isSpecPragmaId id = case idFlavour id of
241 mayHaveNoBinding id = case idFlavour id of
245 -- mayHaveNoBinding returns True of an Id which may not have a
246 -- binding, even though it is defined in this module. Notably,
247 -- the constructors of a dictionary are in this situation.
249 -- mayHaveNoBinding returns True of some things that *do* have a local binding,
250 -- so it's only an approximation. That's ok... it's only use for assertions.
252 -- Don't drop a binding for an exported Id,
253 -- if it otherwise looks dead.
254 isExportedId :: Id -> Bool
255 isExportedId id = case idFlavour id of
257 other -> True -- All the others are no-discard
259 -- Say if an Id was exported by the user
260 -- Implies isExportedId (see mkId above)
261 isUserExportedId :: Id -> Bool
262 isUserExportedId id = isUserExportedName (idName id)
266 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
267 so we don't need to put its signature in an interface file, even if it's mentioned
268 in some other interface unfolding.
271 omitIfaceSigForId :: Id -> Bool
273 | isWiredInName (idName id)
277 = case idFlavour id of
278 RecordSelId _ -> True -- Includes dictionary selectors
281 DataConWrapId _ -> True
282 -- These are are implied by their type or class decl;
283 -- remember that all type and class decls appear in the interface file.
284 -- The dfun id must *not* be omitted, because it carries version info for
287 other -> False -- Don't omit!
289 -- Certain names must be exported with their original occ names, because
290 -- these names are bound by either a class declaration or a data declaration
291 -- or an explicit user export.
292 exportWithOrigOccName :: Id -> Bool
293 exportWithOrigOccName id = omitIfaceSigForId id || isUserExportedId id
297 isDeadBinder :: Id -> Bool
298 isDeadBinder bndr | isId bndr = case idOccInfo bndr of
301 | otherwise = False -- TyVars count as not dead
303 isIP id = isIPOcc (getOccName id)
307 %************************************************************************
309 \subsection{IdInfo stuff}
311 %************************************************************************
314 ---------------------------------
316 idArityInfo :: Id -> ArityInfo
317 idArityInfo id = arityInfo (idInfo id)
319 idArity :: Id -> Arity
320 idArity id = arityLowerBound (idArityInfo id)
322 setIdArityInfo :: Id -> ArityInfo -> Id
323 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
325 ---------------------------------
327 idStrictness :: Id -> StrictnessInfo
328 idStrictness id = strictnessInfo (idInfo id)
330 setIdStrictness :: Id -> StrictnessInfo -> Id
331 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
333 -- isBottomingId returns true if an application to n args would diverge
334 isBottomingId :: Id -> Bool
335 isBottomingId id = isBottomingStrictness (idStrictness id)
337 ---------------------------------
339 idWorkerInfo :: Id -> WorkerInfo
340 idWorkerInfo id = workerInfo (idInfo id)
342 setIdWorkerInfo :: Id -> WorkerInfo -> Id
343 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
345 ---------------------------------
347 idUnfolding :: Id -> Unfolding
348 idUnfolding id = unfoldingInfo (idInfo id)
350 setIdUnfolding :: Id -> Unfolding -> Id
351 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
353 ---------------------------------
355 idDemandInfo :: Id -> Demand
356 idDemandInfo id = demandInfo (idInfo id)
358 setIdDemandInfo :: Id -> Demand -> Id
359 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
361 ---------------------------------
363 idUpdateInfo :: Id -> UpdateInfo
364 idUpdateInfo id = updateInfo (idInfo id)
366 setIdUpdateInfo :: Id -> UpdateInfo -> Id
367 setIdUpdateInfo id upd_info = modifyIdInfo (`setUpdateInfo` upd_info) id
369 ---------------------------------
371 idSpecialisation :: Id -> CoreRules
372 idSpecialisation id = specInfo (idInfo id)
374 setIdSpecialisation :: Id -> CoreRules -> Id
375 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
377 ---------------------------------
379 idCafInfo :: Id -> CafInfo
380 idCafInfo id = cafInfo (idInfo id)
382 setIdCafInfo :: Id -> CafInfo -> Id
383 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
385 ---------------------------------
387 idCprInfo :: Id -> CprInfo
388 idCprInfo id = cprInfo (idInfo id)
390 setIdCprInfo :: Id -> CprInfo -> Id
391 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
393 ---------------------------------
395 idOccInfo :: Id -> OccInfo
396 idOccInfo id = occInfo (idInfo id)
398 setIdOccInfo :: Id -> OccInfo -> Id
399 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
403 ---------------------------------
405 The inline pragma tells us to be very keen to inline this Id, but it's still
406 OK not to if optimisation is switched off.
409 idInlinePragma :: Id -> InlinePragInfo
410 idInlinePragma id = inlinePragInfo (idInfo id)
412 setInlinePragma :: Id -> InlinePragInfo -> Id
413 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
415 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
416 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
420 ---------------------------------
423 idLBVarInfo :: Id -> LBVarInfo
424 idLBVarInfo id = lbvarInfo (idInfo id)
426 isOneShotLambda :: Id -> Bool
427 isOneShotLambda id = case idLBVarInfo id of
428 IsOneShotLambda -> True
429 NoLBVarInfo -> case splitTyConApp_maybe (idType id) of
430 Just (tycon,_) -> tycon == statePrimTyCon
432 -- The last clause is a gross hack. It claims that
433 -- every function over realWorldStatePrimTy is a one-shot
434 -- function. This is pretty true in practice, and makes a big
435 -- difference. For example, consider
436 -- a `thenST` \ r -> ...E...
437 -- The early full laziness pass, if it doesn't know that r is one-shot
438 -- will pull out E (let's say it doesn't mention r) to give
439 -- let lvl = E in a `thenST` \ r -> ...lvl...
440 -- When `thenST` gets inlined, we end up with
441 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
442 -- and we don't re-inline E.
444 -- It would be better to spot that r was one-shot to start with, but
445 -- I don't want to rely on that.
447 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
448 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
450 setOneShotLambda :: Id -> Id
451 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
453 clearOneShotLambda :: Id -> Id
454 clearOneShotLambda id
455 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
458 -- But watch out: this may change the type of something else
460 -- If we change the one-shot-ness of x, f's type changes
464 zapFragileIdInfo :: Id -> Id
465 zapFragileIdInfo id = maybeModifyIdInfo zapFragileInfo id
467 zapLamIdInfo :: Id -> Id
468 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id