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,
26 exportWithOrigOccName,
31 -- Inline pragma stuff
32 getInlinePragma, setInlinePragma, modifyInlinePragma,
34 isSpecPragmaId, isRecordSelector,
35 isPrimitiveId_maybe, isDataConId_maybe,
36 isConstantId, isConstantId_maybe, isBottomingId, idAppIsBottom,
37 isExportedId, isUserExportedId,
40 -- One shot lambda stuff
41 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
68 #include "HsVersions.h"
70 import {-# SOURCE #-} CoreUnfold ( Unfolding )
71 import {-# SOURCE #-} CoreSyn ( CoreRules )
73 import Var ( Id, DictId,
75 idName, idType, idUnique, idInfo,
76 setIdName, setVarType, setIdUnique,
77 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
81 import Type ( Type, tyVarsOfType, typePrimRep, addFreeTyVars, seqType, splitTyConApp_maybe )
85 import Demand ( Demand, isStrict, wwLazy )
86 import Name ( Name, OccName,
87 mkSysLocalName, mkLocalName,
88 isWiredInName, isUserExportedName,
91 import OccName ( UserFS )
92 import Const ( Con(..) )
93 import PrimRep ( PrimRep )
94 import PrimOp ( PrimOp )
95 import TysPrim ( statePrimTyCon )
96 import FieldLabel ( FieldLabel(..) )
97 import SrcLoc ( SrcLoc )
98 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques )
101 infixl 1 `setIdUnfolding`,
106 `setIdSpecialisation`,
112 -- infixl so you can say (id `set` a `set` b)
117 %************************************************************************
119 \subsection{Simple Id construction}
121 %************************************************************************
123 Absolutely all Ids are made by mkId. It
124 a) Pins free-tyvar-info onto the Id's type,
125 where it can easily be found.
126 b) Ensures that exported Ids are
129 mkId :: Name -> Type -> IdInfo -> Id
130 mkId name ty info = mkIdVar name (addFreeTyVars ty) info'
132 info' | isUserExportedName name = setNoDiscardInfo info
137 mkVanillaId :: Name -> Type -> Id
138 mkVanillaId name ty = mkId name ty vanillaIdInfo
140 -- SysLocal: for an Id being created by the compiler out of thin air...
141 -- UserLocal: an Id with a name the user might recognize...
142 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
143 mkSysLocal :: UserFS -> Unique -> Type -> Id
145 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
146 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName uniq occ loc) ty
149 Make some local @Ids@ for a template @CoreExpr@. These have bogus
150 @Uniques@, but that's OK because the templates are supposed to be
151 instantiated before use.
154 -- "Wild Id" typically used when you need a binder that you don't expect to use
155 mkWildId :: Type -> Id
156 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
158 -- "Template locals" typically used in unfoldings
159 mkTemplateLocals :: [Type] -> [Id]
160 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
161 (getBuiltinUniques (length tys))
164 mkTemplateLocal :: Int -> Type -> Id
165 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
169 %************************************************************************
171 \subsection[Id-general-funs]{General @Id@-related functions}
173 %************************************************************************
176 idFreeTyVars :: Id -> TyVarSet
177 idFreeTyVars id = tyVarsOfType (idType id)
179 setIdType :: Id -> Type -> Id
180 -- Add free tyvar info to the type
181 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
183 idPrimRep :: Id -> PrimRep
184 idPrimRep id = typePrimRep (idType id)
188 %************************************************************************
190 \subsection{Special Ids}
192 %************************************************************************
195 idFlavour :: Id -> IdFlavour
196 idFlavour id = flavourInfo (idInfo id)
198 setIdNoDiscard :: Id -> Id
199 setIdNoDiscard id -- Make an Id into a NoDiscardId, unless it is already
200 = modifyIdInfo setNoDiscardInfo id
202 recordSelectorFieldLabel :: Id -> FieldLabel
203 recordSelectorFieldLabel id = case idFlavour id of
204 RecordSelId lbl -> lbl
206 isRecordSelector id = case idFlavour id of
207 RecordSelId lbl -> True
210 isPrimitiveId_maybe id = case idFlavour id of
211 ConstantId (PrimOp op) -> Just op
214 isDataConId_maybe id = case idFlavour id of
215 ConstantId (DataCon con) -> Just con
218 isConstantId id = case idFlavour id of
222 isConstantId_maybe id = case idFlavour id of
223 ConstantId const -> Just const
226 isSpecPragmaId id = case idFlavour id of
230 mayHaveNoBinding id = isConstantId id
231 -- mayHaveNoBinding returns True of an Id which may not have a
232 -- binding, even though it is defined in this module. Notably,
233 -- the constructors of a dictionary are in this situation.
235 -- mayHaveNoBinding returns True of some things that *do* have a local binding,
236 -- so it's only an approximation. That's ok... it's only use for assertions.
238 -- Don't drop a binding for an exported Id,
239 -- if it otherwise looks dead.
240 isExportedId :: Id -> Bool
241 isExportedId id = case idFlavour id of
243 other -> True -- All the others are no-discard
245 -- Say if an Id was exported by the user
246 -- Implies isExportedId (see mkId above)
247 isUserExportedId :: Id -> Bool
248 isUserExportedId id = isUserExportedName (idName id)
252 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
253 so we don't need to put its signature in an interface file, even if it's mentioned
254 in some other interface unfolding.
257 omitIfaceSigForId :: Id -> Bool
259 | isWiredInName (idName id)
263 = case idFlavour id of
264 RecordSelId _ -> True -- Includes dictionary selectors
266 -- ConstantIds are implied by their type or class decl;
267 -- remember that all type and class decls appear in the interface file.
268 -- The dfun id must *not* be omitted, because it carries version info for
271 other -> False -- Don't omit!
273 -- Certain names must be exported with their original occ names, because
274 -- these names are bound by either a class declaration or a data declaration
275 -- or an explicit user export.
276 exportWithOrigOccName :: Id -> Bool
277 exportWithOrigOccName id = omitIfaceSigForId id || isUserExportedId id
279 isIP id = isIPOcc (getOccName id)
284 %************************************************************************
286 \subsection{IdInfo stuff}
288 %************************************************************************
291 ---------------------------------
293 getIdArity :: Id -> ArityInfo
294 getIdArity id = arityInfo (idInfo id)
296 setIdArity :: Id -> ArityInfo -> Id
297 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
299 ---------------------------------
301 getIdStrictness :: Id -> StrictnessInfo
302 getIdStrictness id = strictnessInfo (idInfo id)
304 setIdStrictness :: Id -> StrictnessInfo -> Id
305 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
307 -- isBottomingId returns true if an application to n args would diverge
308 isBottomingId :: Id -> Bool
309 isBottomingId id = isBottomingStrictness (strictnessInfo (idInfo id))
311 idAppIsBottom :: Id -> Int -> Bool
312 idAppIsBottom id n = appIsBottom (strictnessInfo (idInfo id)) n
314 ---------------------------------
316 getIdWorkerInfo :: Id -> WorkerInfo
317 getIdWorkerInfo id = workerInfo (idInfo id)
319 setIdWorkerInfo :: Id -> WorkerInfo -> Id
320 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
322 ---------------------------------
324 getIdUnfolding :: Id -> Unfolding
325 getIdUnfolding id = unfoldingInfo (idInfo id)
327 setIdUnfolding :: Id -> Unfolding -> Id
328 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
330 ---------------------------------
332 getIdDemandInfo :: Id -> Demand
333 getIdDemandInfo id = demandInfo (idInfo id)
335 setIdDemandInfo :: Id -> Demand -> Id
336 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
338 ---------------------------------
340 getIdUpdateInfo :: Id -> UpdateInfo
341 getIdUpdateInfo id = updateInfo (idInfo id)
343 setIdUpdateInfo :: Id -> UpdateInfo -> Id
344 setIdUpdateInfo id upd_info = modifyIdInfo (`setUpdateInfo` upd_info) id
346 ---------------------------------
348 getIdSpecialisation :: Id -> CoreRules
349 getIdSpecialisation id = specInfo (idInfo id)
351 setIdSpecialisation :: Id -> CoreRules -> Id
352 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
354 ---------------------------------
356 getIdCafInfo :: Id -> CafInfo
357 getIdCafInfo id = cafInfo (idInfo id)
359 setIdCafInfo :: Id -> CafInfo -> Id
360 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
362 ---------------------------------
364 getIdCprInfo :: Id -> CprInfo
365 getIdCprInfo id = cprInfo (idInfo id)
367 setIdCprInfo :: Id -> CprInfo -> Id
368 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
370 ---------------------------------
372 getIdOccInfo :: Id -> OccInfo
373 getIdOccInfo id = occInfo (idInfo id)
375 setIdOccInfo :: Id -> OccInfo -> Id
376 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
380 ---------------------------------
382 The inline pragma tells us to be very keen to inline this Id, but it's still
383 OK not to if optimisation is switched off.
386 getInlinePragma :: Id -> InlinePragInfo
387 getInlinePragma id = inlinePragInfo (idInfo id)
389 setInlinePragma :: Id -> InlinePragInfo -> Id
390 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
392 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
393 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
397 ---------------------------------
400 isOneShotLambda :: Id -> Bool
401 isOneShotLambda id = case lbvarInfo (idInfo id) of
402 IsOneShotLambda -> True
403 NoLBVarInfo -> case splitTyConApp_maybe (idType id) of
404 Just (tycon,_) -> tycon == statePrimTyCon
406 -- The last clause is a gross hack. It claims that
407 -- every function over realWorldStatePrimTy is a one-shot
408 -- function. This is pretty true in practice, and makes a big
409 -- difference. For example, consider
410 -- a `thenST` \ r -> ...E...
411 -- The early full laziness pass, if it doesn't know that r is one-shot
412 -- will pull out E (let's say it doesn't mention r) to give
413 -- let lvl = E in a `thenST` \ r -> ...lvl...
414 -- When `thenST` gets inlined, we end up with
415 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
416 -- and we don't re-inline E.
418 -- It would be better to spot that r was one-shot to start with, but
419 -- I don't want to rely on that.
421 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
422 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
424 setOneShotLambda :: Id -> Id
425 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
427 clearOneShotLambda :: Id -> Id
428 clearOneShotLambda id
429 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
432 -- But watch out: this may change the type of something else
434 -- If we change the one-shot-ness of x, f's type changes
438 zapFragileIdInfo :: Id -> Id
439 zapFragileIdInfo id = maybeModifyIdInfo zapFragileInfo id
441 zapLamIdInfo :: Id -> Id
442 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id