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, modifyIdInfo, maybeModifyIdInfo,
28 -- Inline pragma stuff
29 getInlinePragma, setInlinePragma, modifyInlinePragma,
30 idMustBeINLINEd, idMustNotBeINLINEd,
32 isSpecPragmaId, isRecordSelector,
33 isPrimitiveId_maybe, isDataConId_maybe,
34 isConstantId, isBottomingId, idAppIsBottom,
35 isExportedId, isUserExportedId,
37 -- One shot lambda stuff
38 isOneShotLambda, setOneShotLambda,
63 #include "HsVersions.h"
65 import {-# SOURCE #-} CoreUnfold ( Unfolding )
66 import {-# SOURCE #-} CoreSyn ( CoreRules )
68 import Var ( Id, DictId,
70 idName, idType, idUnique, idInfo,
71 setIdName, setVarType, setIdUnique,
72 setIdInfo, modifyIdInfo, maybeModifyIdInfo,
76 import Type ( Type, tyVarsOfType, typePrimRep, addFreeTyVars )
78 import Demand ( Demand, isStrict, wwLazy )
79 import Name ( Name, OccName,
80 mkSysLocalName, mkLocalName,
81 isWiredInName, isUserExportedName
83 import Const ( Con(..) )
84 import PrimRep ( PrimRep )
85 import PrimOp ( PrimOp )
86 import TysPrim ( realWorldStatePrimTy )
87 import FieldLabel ( FieldLabel(..) )
88 import SrcLoc ( SrcLoc )
89 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques )
92 infixl 1 `setIdUnfolding`,
97 `setIdSpecialisation`,
103 -- infixl so you can say (id `set` a `set` b)
108 %************************************************************************
110 \subsection{Simple Id construction}
112 %************************************************************************
114 Absolutely all Ids are made by mkId. It
115 a) Pins free-tyvar-info onto the Id's type,
116 where it can easily be found.
117 b) Ensures that exported Ids are
120 mkId :: Name -> Type -> IdInfo -> Id
121 mkId name ty info = mkIdVar name (addFreeTyVars ty) info'
123 info' | isUserExportedName name = setNoDiscardInfo info
128 mkVanillaId :: Name -> Type -> Id
129 mkVanillaId name ty = mkId name ty vanillaIdInfo
131 -- SysLocal: for an Id being created by the compiler out of thin air...
132 -- UserLocal: an Id with a name the user might recognize...
133 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
134 mkSysLocal :: FAST_STRING -> Unique -> Type -> Id
136 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
137 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName uniq occ loc) ty
140 Make some local @Ids@ for a template @CoreExpr@. These have bogus
141 @Uniques@, but that's OK because the templates are supposed to be
142 instantiated before use.
145 -- "Wild Id" typically used when you need a binder that you don't expect to use
146 mkWildId :: Type -> Id
147 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
149 -- "Template locals" typically used in unfoldings
150 mkTemplateLocals :: [Type] -> [Id]
151 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
152 (getBuiltinUniques (length tys))
155 mkTemplateLocal :: Int -> Type -> Id
156 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
160 %************************************************************************
162 \subsection[Id-general-funs]{General @Id@-related functions}
164 %************************************************************************
167 idFreeTyVars :: Id -> TyVarSet
168 idFreeTyVars id = tyVarsOfType (idType id)
170 setIdType :: Id -> Type -> Id
171 -- Add free tyvar info to the type
172 setIdType id ty = setVarType id (addFreeTyVars ty)
174 idPrimRep :: Id -> PrimRep
175 idPrimRep id = typePrimRep (idType id)
179 %************************************************************************
181 \subsection{Special Ids}
183 %************************************************************************
186 idFlavour :: Id -> IdFlavour
187 idFlavour id = flavourInfo (idInfo id)
189 setIdNoDiscard :: Id -> Id
190 setIdNoDiscard id -- Make an Id into a NoDiscardId, unless it is already
191 = modifyIdInfo setNoDiscardInfo id
193 recordSelectorFieldLabel :: Id -> FieldLabel
194 recordSelectorFieldLabel id = case idFlavour id of
195 RecordSelId lbl -> lbl
197 isRecordSelector id = case idFlavour id of
198 RecordSelId lbl -> True
201 isPrimitiveId_maybe id = case idFlavour id of
202 ConstantId (PrimOp op) -> Just op
205 isDataConId_maybe id = case idFlavour id of
206 ConstantId (DataCon con) -> Just con
209 isConstantId id = case idFlavour id of
213 isSpecPragmaId id = case idFlavour id of
217 -- Don't drop a binding for an exported Id,
218 -- if it otherwise looks dead.
219 isExportedId :: Id -> Bool
220 isExportedId id = case idFlavour id of
222 other -> True -- All the others are no-discard
224 -- Say if an Id was exported by the user
225 -- Implies isExportedId (see mkId above)
226 isUserExportedId :: Id -> Bool
227 isUserExportedId id = isUserExportedName (idName id)
231 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
232 so we don't need to put its signature in an interface file, even if it's mentioned
233 in some other interface unfolding.
236 omitIfaceSigForId :: Id -> Bool
238 | isWiredInName (idName id)
242 = case idFlavour id of
243 RecordSelId _ -> True -- Includes dictionary selectors
245 -- ConstantIds are implied by their type or class decl;
246 -- remember that all type and class decls appear in the interface file.
247 -- The dfun id must *not* be omitted, because it carries version info for
250 other -> False -- Don't omit!
255 %************************************************************************
257 \subsection{IdInfo stuff}
259 %************************************************************************
262 ---------------------------------
264 getIdArity :: Id -> ArityInfo
265 getIdArity id = arityInfo (idInfo id)
267 setIdArity :: Id -> ArityInfo -> Id
268 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
270 ---------------------------------
272 getIdStrictness :: Id -> StrictnessInfo
273 getIdStrictness id = strictnessInfo (idInfo id)
275 setIdStrictness :: Id -> StrictnessInfo -> Id
276 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
278 -- isBottomingId returns true if an application to n args would diverge
279 isBottomingId :: Id -> Bool
280 isBottomingId id = isBottomingStrictness (strictnessInfo (idInfo id))
282 idAppIsBottom :: Id -> Int -> Bool
283 idAppIsBottom id n = appIsBottom (strictnessInfo (idInfo id)) n
285 ---------------------------------
287 getIdWorkerInfo :: Id -> WorkerInfo
288 getIdWorkerInfo id = workerInfo (idInfo id)
290 setIdWorkerInfo :: Id -> WorkerInfo -> Id
291 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
293 ---------------------------------
295 getIdUnfolding :: Id -> Unfolding
296 getIdUnfolding id = unfoldingInfo (idInfo id)
298 setIdUnfolding :: Id -> Unfolding -> Id
299 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
301 ---------------------------------
303 getIdDemandInfo :: Id -> Demand
304 getIdDemandInfo id = demandInfo (idInfo id)
306 setIdDemandInfo :: Id -> Demand -> Id
307 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
309 ---------------------------------
311 getIdUpdateInfo :: Id -> UpdateInfo
312 getIdUpdateInfo id = updateInfo (idInfo id)
314 setIdUpdateInfo :: Id -> UpdateInfo -> Id
315 setIdUpdateInfo id upd_info = modifyIdInfo (`setUpdateInfo` upd_info) id
317 ---------------------------------
319 getIdSpecialisation :: Id -> CoreRules
320 getIdSpecialisation id = specInfo (idInfo id)
322 setIdSpecialisation :: Id -> CoreRules -> Id
323 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
325 ---------------------------------
327 getIdCafInfo :: Id -> CafInfo
328 getIdCafInfo id = cafInfo (idInfo id)
330 setIdCafInfo :: Id -> CafInfo -> Id
331 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
333 ---------------------------------
335 getIdCprInfo :: Id -> CprInfo
336 getIdCprInfo id = cprInfo (idInfo id)
338 setIdCprInfo :: Id -> CprInfo -> Id
339 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
343 ---------------------------------
345 The inline pragma tells us to be very keen to inline this Id, but it's still
346 OK not to if optimisation is switched off.
349 getInlinePragma :: Id -> InlinePragInfo
350 getInlinePragma id = inlinePragInfo (idInfo id)
352 setInlinePragma :: Id -> InlinePragInfo -> Id
353 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
355 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
356 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
358 idMustNotBeINLINEd id = case getInlinePragma id of
359 IMustNotBeINLINEd -> True
360 IAmALoopBreaker -> True
363 idMustBeINLINEd id = case getInlinePragma id of
364 IMustBeINLINEd -> True
369 ---------------------------------
372 isOneShotLambda :: Id -> Bool
373 isOneShotLambda id = case lbvarInfo (idInfo id) of
374 IsOneShotLambda -> True
375 NoLBVarInfo -> idType id == realWorldStatePrimTy
376 -- The last clause is a gross hack. It claims that
377 -- every function over realWorldStatePrimTy is a one-shot
378 -- function. This is pretty true in practice, and makes a big
379 -- difference. For example, consider
380 -- a `thenST` \ r -> ...E...
381 -- The early full laziness pass, if it doesn't know that r is one-shot
382 -- will pull out E (let's say it doesn't mention r) to give
383 -- let lvl = E in a `thenST` \ r -> ...lvl...
384 -- When `thenST` gets inlined, we end up with
385 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
386 -- and we don't re-inline E.
388 -- It would be better to spot that r was one-shot to start with, but
389 -- I don't want to rely on that.
391 setOneShotLambda :: Id -> Id
392 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id