2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Id]{@Ids@: Value and constructor identifiers}
10 -- Simple construction
11 mkGlobalId, mkLocalId, mkSpecPragmaId, mkLocalIdWithInfo,
12 mkSysLocal, mkUserLocal, mkVanillaGlobal,
13 mkTemplateLocals, mkTemplateLocalsNum, mkWildId, mkTemplateLocal,
17 idName, idType, idUnique, idInfo,
18 idPrimRep, isId, globalIdDetails,
19 recordSelectorFieldLabel,
22 setIdName, setIdUnique, setIdType, setIdNoDiscard, setGlobalIdDetails,
23 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
24 zapLamIdInfo, zapDemandIdInfo,
27 isImplicitId, isDeadBinder,
28 isSpecPragmaId, isExportedId, isLocalId, isGlobalId,
30 isPrimOpId, isPrimOpId_maybe,
31 isDataConId, isDataConId_maybe,
32 isDataConWrapId, isDataConWrapId_maybe,
36 -- Inline pragma stuff
37 idInlinePragma, setInlinePragma, modifyInlinePragma,
40 -- One shot lambda stuff
41 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
71 #include "HsVersions.h"
74 import CoreSyn ( Unfolding, CoreRules )
75 import BasicTypes ( Arity )
76 import Var ( Id, DictId,
77 isId, isExportedId, isSpecPragmaId, isLocalId,
78 idName, idType, idUnique, idInfo, isGlobalId,
79 setIdName, setVarType, setIdUnique, setIdNoDiscard,
80 setIdInfo, lazySetIdInfo, modifyIdInfo,
82 globalIdDetails, setGlobalIdDetails
84 import qualified Var ( mkLocalId, mkGlobalId, mkSpecPragmaId )
85 import Type ( Type, typePrimRep, addFreeTyVars,
86 usOnce, seqType, splitTyConApp_maybe )
90 import Demand ( Demand )
91 import Name ( Name, OccName,
92 mkSysLocalName, mkLocalName,
95 import OccName ( UserFS, mkWorkerOcc )
96 import PrimRep ( PrimRep )
97 import TysPrim ( statePrimTyCon )
98 import FieldLabel ( FieldLabel )
99 import SrcLoc ( SrcLoc )
100 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques,
101 getNumBuiltinUniques )
103 infixl 1 `setIdUnfolding`,
109 `setIdSpecialisation`,
114 -- infixl so you can say (id `set` a `set` b)
119 %************************************************************************
121 \subsection{Simple Id construction}
123 %************************************************************************
125 Absolutely all Ids are made by mkId. It is just like Var.mkId,
126 but in addition it pins free-tyvar-info onto the Id's type,
127 where it can easily be found.
130 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
131 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
133 mkSpecPragmaId :: OccName -> Unique -> Type -> SrcLoc -> Id
134 mkSpecPragmaId occ uniq ty loc = Var.mkSpecPragmaId (mkLocalName uniq occ loc)
138 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
139 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
143 mkLocalId :: Name -> Type -> Id
144 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
146 -- SysLocal: for an Id being created by the compiler out of thin air...
147 -- UserLocal: an Id with a name the user might recognize...
148 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
149 mkSysLocal :: UserFS -> Unique -> Type -> Id
150 mkVanillaGlobal :: Name -> Type -> IdInfo -> Id
152 mkSysLocal fs uniq ty = mkLocalId (mkSysLocalName uniq fs) ty
153 mkUserLocal occ uniq ty loc = mkLocalId (mkLocalName uniq occ loc) ty
154 mkVanillaGlobal = mkGlobalId VanillaGlobal
157 Make some local @Ids@ for a template @CoreExpr@. These have bogus
158 @Uniques@, but that's OK because the templates are supposed to be
159 instantiated before use.
162 -- "Wild Id" typically used when you need a binder that you don't expect to use
163 mkWildId :: Type -> Id
164 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
166 mkWorkerId :: Unique -> Id -> Type -> Id
167 -- A worker gets a local name. CoreTidy will globalise it if necessary.
168 mkWorkerId uniq unwrkr ty
169 = mkLocalId wkr_name ty
171 wkr_name = mkLocalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcLoc unwrkr)
173 -- "Template locals" typically used in unfoldings
174 mkTemplateLocals :: [Type] -> [Id]
175 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
176 (getBuiltinUniques (length tys))
179 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
180 -- The Int gives the starting point for unique allocation
181 mkTemplateLocalsNum n tys = zipWith (mkSysLocal SLIT("tpl"))
182 (getNumBuiltinUniques n (length tys))
185 mkTemplateLocal :: Int -> Type -> Id
186 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
190 %************************************************************************
192 \subsection[Id-general-funs]{General @Id@-related functions}
194 %************************************************************************
197 setIdType :: Id -> Type -> Id
198 -- Add free tyvar info to the type
199 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
201 idPrimRep :: Id -> PrimRep
202 idPrimRep id = typePrimRep (idType id)
206 %************************************************************************
208 \subsection{Special Ids}
210 %************************************************************************
212 The @SpecPragmaId@ exists only to make Ids that are
213 on the *LHS* of bindings created by SPECIALISE pragmas;
215 The SpecPragmaId is never itself mentioned; it
216 exists solely so that the specialiser will find
217 the call to f, and make specialised version of it.
218 The SpecPragmaId binding is discarded by the specialiser
219 when it gathers up overloaded calls.
220 Meanwhile, it is not discarded as dead code.
224 recordSelectorFieldLabel :: Id -> FieldLabel
225 recordSelectorFieldLabel id = case globalIdDetails id of
226 RecordSelId lbl -> lbl
228 isRecordSelector id = case globalIdDetails id of
229 RecordSelId lbl -> True
232 isPrimOpId id = case globalIdDetails id of
236 isPrimOpId_maybe id = case globalIdDetails id of
237 PrimOpId op -> Just op
240 isDataConId id = case globalIdDetails id of
244 isDataConId_maybe id = case globalIdDetails id of
245 DataConId con -> Just con
248 isDataConWrapId_maybe id = case globalIdDetails id of
249 DataConWrapId con -> Just con
252 isDataConWrapId id = case globalIdDetails id of
253 DataConWrapId con -> True
256 -- hasNoBinding returns True of an Id which may not have a
257 -- binding, even though it is defined in this module. Notably,
258 -- the constructors of a dictionary are in this situation.
259 hasNoBinding id = case globalIdDetails id of
264 isImplicitId :: Id -> Bool
265 -- isImplicitId tells whether an Id's info is implied by other
266 -- declarations, so we don't need to put its signature in an interface
267 -- file, even if it's mentioned in some other interface unfolding.
269 = case globalIdDetails id of
270 RecordSelId _ -> True -- Includes dictionary selectors
273 DataConWrapId _ -> True
274 -- These are are implied by their type or class decl;
275 -- remember that all type and class decls appear in the interface file.
276 -- The dfun id must *not* be omitted, because it carries version info for
282 isDeadBinder :: Id -> Bool
283 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
284 | otherwise = False -- TyVars count as not dead
288 %************************************************************************
290 \subsection{IdInfo stuff}
292 %************************************************************************
295 ---------------------------------
297 idArityInfo :: Id -> ArityInfo
298 idArityInfo id = arityInfo (idInfo id)
300 idArity :: Id -> Arity
301 idArity id = arityLowerBound (idArityInfo id)
303 setIdArityInfo :: Id -> ArityInfo -> Id
304 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
306 ---------------------------------
308 idStrictness :: Id -> StrictnessInfo
309 idStrictness id = strictnessInfo (idInfo id)
311 setIdStrictness :: Id -> StrictnessInfo -> Id
312 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
314 -- isBottomingId returns true if an application to n args would diverge
315 isBottomingId :: Id -> Bool
316 isBottomingId id = isBottomingStrictness (idStrictness id)
318 ---------------------------------
319 -- TYPE GENERALISATION
320 idTyGenInfo :: Id -> TyGenInfo
321 idTyGenInfo id = tyGenInfo (idInfo id)
323 setIdTyGenInfo :: Id -> TyGenInfo -> Id
324 setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id
326 ---------------------------------
328 idWorkerInfo :: Id -> WorkerInfo
329 idWorkerInfo id = workerInfo (idInfo id)
331 setIdWorkerInfo :: Id -> WorkerInfo -> Id
332 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
334 ---------------------------------
336 idUnfolding :: Id -> Unfolding
337 idUnfolding id = unfoldingInfo (idInfo id)
339 setIdUnfolding :: Id -> Unfolding -> Id
340 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
342 ---------------------------------
344 idDemandInfo :: Id -> Demand
345 idDemandInfo id = demandInfo (idInfo id)
347 setIdDemandInfo :: Id -> Demand -> Id
348 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
350 ---------------------------------
352 idSpecialisation :: Id -> CoreRules
353 idSpecialisation id = specInfo (idInfo id)
355 setIdSpecialisation :: Id -> CoreRules -> Id
356 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
358 ---------------------------------
360 idCgInfo :: Id -> CgInfo
361 idCgInfo id = cgInfo (idInfo id)
363 setIdCgInfo :: Id -> CgInfo -> Id
364 setIdCgInfo id cg_info = modifyIdInfo (`setCgInfo` cg_info) id
366 ---------------------------------
368 idCafInfo :: Id -> CafInfo
369 idCafInfo id = cgCafInfo (idCgInfo id)
371 ---------------------------------
374 idCgArity :: Id -> Arity
375 idCgArity id = cgArity (idCgInfo id)
377 ---------------------------------
379 idCprInfo :: Id -> CprInfo
380 idCprInfo id = cprInfo (idInfo id)
382 setIdCprInfo :: Id -> CprInfo -> Id
383 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
385 ---------------------------------
387 idOccInfo :: Id -> OccInfo
388 idOccInfo id = occInfo (idInfo id)
390 setIdOccInfo :: Id -> OccInfo -> Id
391 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
395 ---------------------------------
397 The inline pragma tells us to be very keen to inline this Id, but it's still
398 OK not to if optimisation is switched off.
401 idInlinePragma :: Id -> InlinePragInfo
402 idInlinePragma id = inlinePragInfo (idInfo id)
404 setInlinePragma :: Id -> InlinePragInfo -> Id
405 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
407 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
408 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
412 ---------------------------------
415 idLBVarInfo :: Id -> LBVarInfo
416 idLBVarInfo id = lbvarInfo (idInfo id)
418 isOneShotLambda :: Id -> Bool
419 isOneShotLambda id = analysis || hack
420 where analysis = case idLBVarInfo id of
421 LBVarInfo u | u == usOnce -> True
423 hack = case splitTyConApp_maybe (idType id) of
424 Just (tycon,_) | tycon == statePrimTyCon -> True
427 -- The last clause is a gross hack. It claims that
428 -- every function over realWorldStatePrimTy is a one-shot
429 -- function. This is pretty true in practice, and makes a big
430 -- difference. For example, consider
431 -- a `thenST` \ r -> ...E...
432 -- The early full laziness pass, if it doesn't know that r is one-shot
433 -- will pull out E (let's say it doesn't mention r) to give
434 -- let lvl = E in a `thenST` \ r -> ...lvl...
435 -- When `thenST` gets inlined, we end up with
436 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
437 -- and we don't re-inline E.
439 -- It would be better to spot that r was one-shot to start with, but
440 -- I don't want to rely on that.
442 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
443 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
445 setOneShotLambda :: Id -> Id
446 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
448 clearOneShotLambda :: Id -> Id
449 clearOneShotLambda id
450 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
453 -- But watch out: this may change the type of something else
455 -- If we change the one-shot-ness of x, f's type changes
459 zapLamIdInfo :: Id -> Id
460 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id
462 zapDemandIdInfo id = maybeModifyIdInfo zapDemandInfo id