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,
30 -- Inline pragma stuff
31 getInlinePragma, setInlinePragma, modifyInlinePragma,
33 isSpecPragmaId, isRecordSelector,
34 isPrimitiveId_maybe, isDataConId_maybe,
35 isConstantId, isBottomingId, idAppIsBottom,
36 isExportedId, isUserExportedId,
39 -- One shot lambda stuff
40 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
67 #include "HsVersions.h"
69 import {-# SOURCE #-} CoreUnfold ( Unfolding )
70 import {-# SOURCE #-} CoreSyn ( CoreRules )
72 import Var ( Id, DictId,
74 idName, idType, idUnique, idInfo,
75 setIdName, setVarType, setIdUnique,
76 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
80 import Type ( Type, tyVarsOfType, typePrimRep, addFreeTyVars, seqType, splitTyConApp_maybe )
84 import Demand ( Demand, isStrict, wwLazy )
85 import Name ( Name, OccName,
86 mkSysLocalName, mkLocalName,
87 isWiredInName, isUserExportedName
89 import OccName ( UserFS )
90 import Const ( Con(..) )
91 import PrimRep ( PrimRep )
92 import PrimOp ( PrimOp )
93 import TysPrim ( statePrimTyCon )
94 import FieldLabel ( FieldLabel(..) )
95 import SrcLoc ( SrcLoc )
96 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques )
99 infixl 1 `setIdUnfolding`,
104 `setIdSpecialisation`,
110 -- infixl so you can say (id `set` a `set` b)
115 %************************************************************************
117 \subsection{Simple Id construction}
119 %************************************************************************
121 Absolutely all Ids are made by mkId. It
122 a) Pins free-tyvar-info onto the Id's type,
123 where it can easily be found.
124 b) Ensures that exported Ids are
127 mkId :: Name -> Type -> IdInfo -> Id
128 mkId name ty info = mkIdVar name (addFreeTyVars ty) info'
130 info' | isUserExportedName name = setNoDiscardInfo info
135 mkVanillaId :: Name -> Type -> Id
136 mkVanillaId name ty = mkId name ty vanillaIdInfo
138 -- SysLocal: for an Id being created by the compiler out of thin air...
139 -- UserLocal: an Id with a name the user might recognize...
140 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
141 mkSysLocal :: UserFS -> Unique -> Type -> Id
143 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
144 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName uniq occ loc) ty
147 Make some local @Ids@ for a template @CoreExpr@. These have bogus
148 @Uniques@, but that's OK because the templates are supposed to be
149 instantiated before use.
152 -- "Wild Id" typically used when you need a binder that you don't expect to use
153 mkWildId :: Type -> Id
154 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
156 -- "Template locals" typically used in unfoldings
157 mkTemplateLocals :: [Type] -> [Id]
158 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
159 (getBuiltinUniques (length tys))
162 mkTemplateLocal :: Int -> Type -> Id
163 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
167 %************************************************************************
169 \subsection[Id-general-funs]{General @Id@-related functions}
171 %************************************************************************
174 idFreeTyVars :: Id -> TyVarSet
175 idFreeTyVars id = tyVarsOfType (idType id)
177 setIdType :: Id -> Type -> Id
178 -- Add free tyvar info to the type
179 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
181 idPrimRep :: Id -> PrimRep
182 idPrimRep id = typePrimRep (idType id)
186 %************************************************************************
188 \subsection{Special Ids}
190 %************************************************************************
193 idFlavour :: Id -> IdFlavour
194 idFlavour id = flavourInfo (idInfo id)
196 setIdNoDiscard :: Id -> Id
197 setIdNoDiscard id -- Make an Id into a NoDiscardId, unless it is already
198 = modifyIdInfo setNoDiscardInfo id
200 recordSelectorFieldLabel :: Id -> FieldLabel
201 recordSelectorFieldLabel id = case idFlavour id of
202 RecordSelId lbl -> lbl
204 isRecordSelector id = case idFlavour id of
205 RecordSelId lbl -> True
208 isPrimitiveId_maybe id = case idFlavour id of
209 ConstantId (PrimOp op) -> Just op
212 isDataConId_maybe id = case idFlavour id of
213 ConstantId (DataCon con) -> Just con
216 isConstantId id = case idFlavour id of
220 isSpecPragmaId id = case idFlavour id of
224 mayHaveNoBinding id = isConstantId id
225 -- mayHaveNoBinding returns True of an Id which may not have a
226 -- binding, even though it is defined in this module. Notably,
227 -- the constructors of a dictionary are in this situation.
229 -- mayHaveNoBinding returns True of some things that *do* have a local binding,
230 -- so it's only an approximation. That's ok... it's only use for assertions.
232 -- Don't drop a binding for an exported Id,
233 -- if it otherwise looks dead.
234 isExportedId :: Id -> Bool
235 isExportedId id = case idFlavour id of
237 other -> True -- All the others are no-discard
239 -- Say if an Id was exported by the user
240 -- Implies isExportedId (see mkId above)
241 isUserExportedId :: Id -> Bool
242 isUserExportedId id = isUserExportedName (idName id)
246 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
247 so we don't need to put its signature in an interface file, even if it's mentioned
248 in some other interface unfolding.
251 omitIfaceSigForId :: Id -> Bool
253 | isWiredInName (idName id)
257 = case idFlavour id of
258 RecordSelId _ -> True -- Includes dictionary selectors
260 -- ConstantIds are implied by their type or class decl;
261 -- remember that all type and class decls appear in the interface file.
262 -- The dfun id must *not* be omitted, because it carries version info for
265 other -> False -- Don't omit!
267 -- Certain names must be exported with their original occ names, because
268 -- these names are bound by either a class declaration or a data declaration
269 -- or an explicit user export.
270 exportWithOrigOccName :: Id -> Bool
271 exportWithOrigOccName id = omitIfaceSigForId id || isUserExportedId id
276 %************************************************************************
278 \subsection{IdInfo stuff}
280 %************************************************************************
283 ---------------------------------
285 getIdArity :: Id -> ArityInfo
286 getIdArity id = arityInfo (idInfo id)
288 setIdArity :: Id -> ArityInfo -> Id
289 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
291 ---------------------------------
293 getIdStrictness :: Id -> StrictnessInfo
294 getIdStrictness id = strictnessInfo (idInfo id)
296 setIdStrictness :: Id -> StrictnessInfo -> Id
297 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
299 -- isBottomingId returns true if an application to n args would diverge
300 isBottomingId :: Id -> Bool
301 isBottomingId id = isBottomingStrictness (strictnessInfo (idInfo id))
303 idAppIsBottom :: Id -> Int -> Bool
304 idAppIsBottom id n = appIsBottom (strictnessInfo (idInfo id)) n
306 ---------------------------------
308 getIdWorkerInfo :: Id -> WorkerInfo
309 getIdWorkerInfo id = workerInfo (idInfo id)
311 setIdWorkerInfo :: Id -> WorkerInfo -> Id
312 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
314 ---------------------------------
316 getIdUnfolding :: Id -> Unfolding
317 getIdUnfolding id = unfoldingInfo (idInfo id)
319 setIdUnfolding :: Id -> Unfolding -> Id
320 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
322 ---------------------------------
324 getIdDemandInfo :: Id -> Demand
325 getIdDemandInfo id = demandInfo (idInfo id)
327 setIdDemandInfo :: Id -> Demand -> Id
328 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
330 ---------------------------------
332 getIdUpdateInfo :: Id -> UpdateInfo
333 getIdUpdateInfo id = updateInfo (idInfo id)
335 setIdUpdateInfo :: Id -> UpdateInfo -> Id
336 setIdUpdateInfo id upd_info = modifyIdInfo (`setUpdateInfo` upd_info) id
338 ---------------------------------
340 getIdSpecialisation :: Id -> CoreRules
341 getIdSpecialisation id = specInfo (idInfo id)
343 setIdSpecialisation :: Id -> CoreRules -> Id
344 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
346 ---------------------------------
348 getIdCafInfo :: Id -> CafInfo
349 getIdCafInfo id = cafInfo (idInfo id)
351 setIdCafInfo :: Id -> CafInfo -> Id
352 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
354 ---------------------------------
356 getIdCprInfo :: Id -> CprInfo
357 getIdCprInfo id = cprInfo (idInfo id)
359 setIdCprInfo :: Id -> CprInfo -> Id
360 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
362 ---------------------------------
364 getIdOccInfo :: Id -> OccInfo
365 getIdOccInfo id = occInfo (idInfo id)
367 setIdOccInfo :: Id -> OccInfo -> Id
368 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
372 ---------------------------------
374 The inline pragma tells us to be very keen to inline this Id, but it's still
375 OK not to if optimisation is switched off.
378 getInlinePragma :: Id -> InlinePragInfo
379 getInlinePragma id = inlinePragInfo (idInfo id)
381 setInlinePragma :: Id -> InlinePragInfo -> Id
382 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
384 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
385 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
389 ---------------------------------
392 isOneShotLambda :: Id -> Bool
393 isOneShotLambda id = case lbvarInfo (idInfo id) of
394 IsOneShotLambda -> True
395 NoLBVarInfo -> case splitTyConApp_maybe (idType id) of
396 Just (tycon,_) -> tycon == statePrimTyCon
398 -- The last clause is a gross hack. It claims that
399 -- every function over realWorldStatePrimTy is a one-shot
400 -- function. This is pretty true in practice, and makes a big
401 -- difference. For example, consider
402 -- a `thenST` \ r -> ...E...
403 -- The early full laziness pass, if it doesn't know that r is one-shot
404 -- will pull out E (let's say it doesn't mention r) to give
405 -- let lvl = E in a `thenST` \ r -> ...lvl...
406 -- When `thenST` gets inlined, we end up with
407 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
408 -- and we don't re-inline E.
410 -- It would be better to spot that r was one-shot to start with, but
411 -- I don't want to rely on that.
413 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
414 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
416 setOneShotLambda :: Id -> Id
417 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
419 clearOneShotLambda :: Id -> Id
420 clearOneShotLambda id
421 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
424 -- But watch out: this may change the type of something else
426 -- If we change the one-shot-ness of x, f's type changes
430 zapFragileIdInfo :: Id -> Id
431 zapFragileIdInfo id = maybeModifyIdInfo zapFragileInfo id
433 zapLamIdInfo :: Id -> Id
434 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id