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, mkTemplateLocalsNum, 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,
30 isSpecPragmaId, isRecordSelector,
31 isPrimOpId, isPrimOpId_maybe,
32 isDataConId, isDataConId_maybe, isDataConWrapId,
33 isDataConWrapId_maybe,
35 isExportedId, isLocalId,
38 -- Inline pragma stuff
39 idInlinePragma, setInlinePragma, modifyInlinePragma,
42 -- One shot lambda stuff
43 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
72 #include "HsVersions.h"
75 import CoreSyn ( Unfolding, CoreRules )
76 import BasicTypes ( Arity )
77 import Var ( Id, DictId,
79 idName, idType, idUnique, idInfo,
80 setIdName, setVarType, setIdUnique,
81 setIdInfo, lazySetIdInfo, modifyIdInfo,
86 import Type ( Type, tyVarsOfType, typePrimRep, addFreeTyVars,
87 usOnce, seqType, splitTyConApp_maybe )
91 import Demand ( Demand )
92 import Name ( Name, OccName,
93 mkSysLocalName, mkLocalName,
97 import OccName ( UserFS )
98 import PrimRep ( PrimRep )
99 import TysPrim ( statePrimTyCon )
100 import FieldLabel ( FieldLabel )
101 import SrcLoc ( SrcLoc )
102 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques,
103 getNumBuiltinUniques )
106 infixl 1 `setIdUnfolding`,
112 `setIdSpecialisation`,
117 -- infixl so you can say (id `set` a `set` b)
122 %************************************************************************
124 \subsection{Simple Id construction}
126 %************************************************************************
128 Absolutely all Ids are made by mkId. It
129 a) Pins free-tyvar-info onto the Id's type,
130 where it can easily be found.
131 b) Ensures that exported Ids are
134 mkId :: Name -> Type -> IdInfo -> Id
135 mkId name ty info = mkIdVar name (addFreeTyVars ty) info
137 mkImportedId :: Name -> Type -> IdInfo -> Id
138 mkImportedId name ty info = mkId name ty (info `setFlavourInfo` ImportedId)
142 mkVanillaId :: Name -> Type -> Id
143 mkVanillaId name ty = mkId name ty vanillaIdInfo
145 -- SysLocal: for an Id being created by the compiler out of thin air...
146 -- UserLocal: an Id with a name the user might recognize...
147 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
148 mkSysLocal :: UserFS -> Unique -> Type -> Id
150 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
151 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName uniq occ loc) ty
154 Make some local @Ids@ for a template @CoreExpr@. These have bogus
155 @Uniques@, but that's OK because the templates are supposed to be
156 instantiated before use.
159 -- "Wild Id" typically used when you need a binder that you don't expect to use
160 mkWildId :: Type -> Id
161 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
163 -- "Template locals" typically used in unfoldings
164 mkTemplateLocals :: [Type] -> [Id]
165 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
166 (getBuiltinUniques (length tys))
169 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
170 mkTemplateLocalsNum n tys = zipWith (mkSysLocal SLIT("tpl"))
171 (getNumBuiltinUniques n (length tys))
174 mkTemplateLocal :: Int -> Type -> Id
175 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
179 %************************************************************************
181 \subsection[Id-general-funs]{General @Id@-related functions}
183 %************************************************************************
186 idFreeTyVars :: Id -> TyVarSet
187 idFreeTyVars id = tyVarsOfType (idType id)
189 setIdType :: Id -> Type -> Id
190 -- Add free tyvar info to the type
191 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
193 idPrimRep :: Id -> PrimRep
194 idPrimRep id = typePrimRep (idType id)
198 %************************************************************************
200 \subsection{Special Ids}
202 %************************************************************************
205 idFlavour :: Id -> IdFlavour
206 idFlavour id = flavourInfo (idInfo id)
208 setIdNoDiscard :: Id -> Id
209 setIdNoDiscard id -- Make an Id into a NoDiscardId, unless it is already
210 = modifyIdInfo setNoDiscardInfo id
212 recordSelectorFieldLabel :: Id -> FieldLabel
213 recordSelectorFieldLabel id = case idFlavour id of
214 RecordSelId lbl -> lbl
216 isRecordSelector id = case idFlavour id of
217 RecordSelId lbl -> True
220 isPrimOpId id = case idFlavour id of
224 isPrimOpId_maybe id = case idFlavour id of
225 PrimOpId op -> Just op
228 isDataConId id = case idFlavour id of
232 isDataConId_maybe id = case idFlavour id of
233 DataConId con -> Just con
236 isDataConWrapId_maybe id = case idFlavour id of
237 DataConWrapId con -> Just con
240 isDataConWrapId id = case idFlavour id of
241 DataConWrapId con -> True
244 isSpecPragmaId id = case idFlavour id of
248 hasNoBinding id = case idFlavour id of
252 -- hasNoBinding returns True of an Id which may not have a
253 -- binding, even though it is defined in this module. Notably,
254 -- the constructors of a dictionary are in this situation.
256 -- Don't drop a binding for an exported Id,
257 -- if it otherwise looks dead.
258 -- Perhaps a better name would be isDiscardableId
259 isExportedId :: Id -> Bool
260 isExportedId id = case idFlavour id of
264 isLocalId :: Id -> Bool
265 -- True of Ids that are locally defined, but are not constants
266 -- like data constructors, record selectors, and the like.
267 -- See comments with CoreSyn.isLocalVar
268 isLocalId id = case idFlavour id of
276 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
277 so we don't need to put its signature in an interface file, even if it's mentioned
278 in some other interface unfolding.
281 omitIfaceSigForId :: Id -> Bool
283 = ASSERT2( not (omit && nameIsLocallyDefined (idName id)
284 && idTyGenInfo id /= TyGenNever),
286 -- mustn't omit type signature for a name whose type might change!
289 omit = omitIfaceSigForId' id
291 omitIfaceSigForId' id
292 = case idFlavour id of
293 RecordSelId _ -> True -- Includes dictionary selectors
296 DataConWrapId _ -> True
297 -- These are are implied by their type or class decl;
298 -- remember that all type and class decls appear in the interface file.
299 -- The dfun id must *not* be omitted, because it carries version info for
302 other -> False -- Don't omit!
304 -- Certain names must be exported with their original occ names, because
305 -- these names are bound by either a class declaration or a data declaration
306 -- or an explicit user export.
307 exportWithOrigOccName :: Id -> Bool
308 exportWithOrigOccName id = omitIfaceSigForId id || isExportedId id
312 isDeadBinder :: Id -> Bool
313 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
314 | otherwise = False -- TyVars count as not dead
316 isIP id = isIPOcc (getOccName id)
320 %************************************************************************
322 \subsection{IdInfo stuff}
324 %************************************************************************
327 ---------------------------------
329 idArityInfo :: Id -> ArityInfo
330 idArityInfo id = arityInfo (idInfo id)
332 idArity :: Id -> Arity
333 idArity id = arityLowerBound (idArityInfo id)
335 setIdArityInfo :: Id -> ArityInfo -> Id
336 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
338 ---------------------------------
340 idStrictness :: Id -> StrictnessInfo
341 idStrictness id = strictnessInfo (idInfo id)
343 setIdStrictness :: Id -> StrictnessInfo -> Id
344 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
346 -- isBottomingId returns true if an application to n args would diverge
347 isBottomingId :: Id -> Bool
348 isBottomingId id = isBottomingStrictness (idStrictness id)
350 ---------------------------------
351 -- TYPE GENERALISATION
352 idTyGenInfo :: Id -> TyGenInfo
353 idTyGenInfo id = tyGenInfo (idInfo id)
355 setIdTyGenInfo :: Id -> TyGenInfo -> Id
356 setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id
358 ---------------------------------
360 idWorkerInfo :: Id -> WorkerInfo
361 idWorkerInfo id = workerInfo (idInfo id)
363 setIdWorkerInfo :: Id -> WorkerInfo -> Id
364 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
366 ---------------------------------
368 idUnfolding :: Id -> Unfolding
369 idUnfolding id = unfoldingInfo (idInfo id)
371 setIdUnfolding :: Id -> Unfolding -> Id
372 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
374 ---------------------------------
376 idDemandInfo :: Id -> Demand
377 idDemandInfo id = demandInfo (idInfo id)
379 setIdDemandInfo :: Id -> Demand -> Id
380 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
382 ---------------------------------
384 idSpecialisation :: Id -> CoreRules
385 idSpecialisation id = specInfo (idInfo id)
387 setIdSpecialisation :: Id -> CoreRules -> Id
388 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
390 ---------------------------------
392 idCafInfo :: Id -> CafInfo
393 idCafInfo id = cafInfo (idInfo id)
395 setIdCafInfo :: Id -> CafInfo -> Id
396 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
398 ---------------------------------
400 idCprInfo :: Id -> CprInfo
401 idCprInfo id = cprInfo (idInfo id)
403 setIdCprInfo :: Id -> CprInfo -> Id
404 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
406 ---------------------------------
408 idOccInfo :: Id -> OccInfo
409 idOccInfo id = occInfo (idInfo id)
411 setIdOccInfo :: Id -> OccInfo -> Id
412 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
416 ---------------------------------
418 The inline pragma tells us to be very keen to inline this Id, but it's still
419 OK not to if optimisation is switched off.
422 idInlinePragma :: Id -> InlinePragInfo
423 idInlinePragma id = inlinePragInfo (idInfo id)
425 setInlinePragma :: Id -> InlinePragInfo -> Id
426 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
428 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
429 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
433 ---------------------------------
436 idLBVarInfo :: Id -> LBVarInfo
437 idLBVarInfo id = lbvarInfo (idInfo id)
439 isOneShotLambda :: Id -> Bool
440 isOneShotLambda id = analysis || hack
441 where analysis = case idLBVarInfo id of
442 LBVarInfo u | u == usOnce -> True
444 hack = case splitTyConApp_maybe (idType id) of
445 Just (tycon,_) | tycon == statePrimTyCon -> True
448 -- The last clause is a gross hack. It claims that
449 -- every function over realWorldStatePrimTy is a one-shot
450 -- function. This is pretty true in practice, and makes a big
451 -- difference. For example, consider
452 -- a `thenST` \ r -> ...E...
453 -- The early full laziness pass, if it doesn't know that r is one-shot
454 -- will pull out E (let's say it doesn't mention r) to give
455 -- let lvl = E in a `thenST` \ r -> ...lvl...
456 -- When `thenST` gets inlined, we end up with
457 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
458 -- and we don't re-inline E.
460 -- It would be better to spot that r was one-shot to start with, but
461 -- I don't want to rely on that.
463 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
464 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
466 setOneShotLambda :: Id -> Id
467 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
469 clearOneShotLambda :: Id -> Id
470 clearOneShotLambda id
471 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
474 -- But watch out: this may change the type of something else
476 -- If we change the one-shot-ness of x, f's type changes
480 zapFragileIdInfo :: Id -> Id
481 zapFragileIdInfo id = maybeModifyIdInfo zapFragileInfo id
483 zapLamIdInfo :: Id -> Id
484 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id