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, mkSysLocalUnencoded, mkUserLocal, mkVanillaGlobal,
13 mkTemplateLocals, mkTemplateLocalsNum, mkWildId, mkTemplateLocal,
14 mkWorkerId, mkExportedLocalId,
17 idName, idType, idUnique, idInfo,
18 idPrimRep, isId, globalIdDetails,
19 recordSelectorFieldLabel,
22 setIdName, setIdUnique, Id.setIdType, setIdLocalExported, setGlobalIdDetails,
23 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
24 zapLamIdInfo, zapDemandIdInfo,
27 isImplicitId, isDeadBinder,
28 isSpecPragmaId, isExportedId, isLocalId, isGlobalId,
30 isPrimOpId, isPrimOpId_maybe,
31 isFCallId, isFCallId_maybe,
32 isDataConWorkId, isDataConWorkId_maybe,
33 isBottomingId, idIsFrom,
36 -- Inline pragma stuff
37 idInlinePragma, setInlinePragma, modifyInlinePragma,
40 -- One shot lambda stuff
41 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
47 setIdNewStrictness, zapIdNewStrictness,
63 idNewDemandInfo, idNewDemandInfo_maybe,
64 idNewStrictness, idNewStrictness_maybe,
67 idSpecialisation, idCoreRules,
73 newStrictnessFromOld -- Temporary
78 #include "HsVersions.h"
81 import CoreSyn ( Unfolding, CoreRules, IdCoreRule, rulesRules )
82 import BasicTypes ( Arity )
83 import Var ( Id, DictId,
84 isId, isExportedId, isSpecPragmaId, isLocalId,
85 idName, idType, idUnique, idInfo, isGlobalId,
86 setIdName, setIdType, setIdUnique, setIdLocalExported,
87 setIdInfo, lazySetIdInfo, modifyIdInfo,
89 globalIdDetails, setGlobalIdDetails
91 import qualified Var ( mkLocalId, mkGlobalId, mkSpecPragmaId, mkExportedLocalId )
92 import Type ( Type, typePrimRep, addFreeTyVars, seqType)
97 import qualified Demand ( Demand )
99 import DataCon ( isUnboxedTupleCon )
100 import NewDemand ( Demand, StrictSig, topDmd, topSig, isBottomingSig )
101 import Name ( Name, OccName, nameIsLocalOrFrom,
102 mkSystemName, mkSystemNameEncoded, mkInternalName,
103 getOccName, getSrcLoc
105 import Module ( Module )
106 import OccName ( EncodedFS, mkWorkerOcc )
107 import PrimRep ( PrimRep )
108 import FieldLabel ( FieldLabel )
109 import Maybes ( orElse )
110 import SrcLoc ( SrcLoc )
112 import Unique ( Unique, mkBuiltinUnique )
114 -- infixl so you can say (id `set` a `set` b)
115 infixl 1 `setIdUnfolding`,
117 `setIdNewDemandInfo`,
118 `setIdNewStrictness`,
120 `setIdSpecialisation`,
123 #ifdef OLD_STRICTNESS
132 %************************************************************************
134 \subsection{Simple Id construction}
136 %************************************************************************
138 Absolutely all Ids are made by mkId. It is just like Var.mkId,
139 but in addition it pins free-tyvar-info onto the Id's type,
140 where it can easily be found.
143 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
144 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
146 mkSpecPragmaId :: Name -> Type -> Id
147 mkSpecPragmaId name ty = Var.mkSpecPragmaId name (addFreeTyVars ty) vanillaIdInfo
149 mkExportedLocalId :: Name -> Type -> Id
150 mkExportedLocalId name ty = Var.mkExportedLocalId name (addFreeTyVars ty) vanillaIdInfo
152 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
153 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
157 mkLocalId :: Name -> Type -> Id
158 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
160 -- SysLocal: for an Id being created by the compiler out of thin air...
161 -- UserLocal: an Id with a name the user might recognize...
162 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
163 mkSysLocal :: EncodedFS -> Unique -> Type -> Id
164 mkVanillaGlobal :: Name -> Type -> IdInfo -> Id
166 -- for SysLocal, we assume the base name is already encoded, to avoid
167 -- re-encoding the same string over and over again.
168 mkSysLocal fs uniq ty = mkLocalId (mkSystemNameEncoded uniq fs) ty
170 -- version to use when the faststring needs to be encoded
171 mkSysLocalUnencoded fs uniq ty = mkLocalId (mkSystemName uniq fs) ty
173 mkUserLocal occ uniq ty loc = mkLocalId (mkInternalName uniq occ loc) ty
174 mkVanillaGlobal = mkGlobalId VanillaGlobal
177 Make some local @Ids@ for a template @CoreExpr@. These have bogus
178 @Uniques@, but that's OK because the templates are supposed to be
179 instantiated before use.
182 -- "Wild Id" typically used when you need a binder that you don't expect to use
183 mkWildId :: Type -> Id
184 mkWildId ty = mkSysLocal FSLIT("wild") (mkBuiltinUnique 1) ty
186 mkWorkerId :: Unique -> Id -> Type -> Id
187 -- A worker gets a local name. CoreTidy will externalise it if necessary.
188 mkWorkerId uniq unwrkr ty
189 = mkLocalId wkr_name ty
191 wkr_name = mkInternalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcLoc unwrkr)
193 -- "Template locals" typically used in unfoldings
194 mkTemplateLocals :: [Type] -> [Id]
195 mkTemplateLocals tys = zipWith mkTemplateLocal [1..] tys
197 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
198 -- The Int gives the starting point for unique allocation
199 mkTemplateLocalsNum n tys = zipWith mkTemplateLocal [n..] tys
201 mkTemplateLocal :: Int -> Type -> Id
202 mkTemplateLocal i ty = mkSysLocal FSLIT("tpl") (mkBuiltinUnique i) ty
206 %************************************************************************
208 \subsection[Id-general-funs]{General @Id@-related functions}
210 %************************************************************************
213 setIdType :: Id -> Type -> Id
214 -- Add free tyvar info to the type
215 setIdType id ty = seqType ty `seq` Var.setIdType id (addFreeTyVars ty)
217 idPrimRep :: Id -> PrimRep
218 idPrimRep id = typePrimRep (idType id)
222 %************************************************************************
224 \subsection{Special Ids}
226 %************************************************************************
228 The @SpecPragmaId@ exists only to make Ids that are
229 on the *LHS* of bindings created by SPECIALISE pragmas;
231 The SpecPragmaId is never itself mentioned; it
232 exists solely so that the specialiser will find
233 the call to f, and make specialised version of it.
234 The SpecPragmaId binding is discarded by the specialiser
235 when it gathers up overloaded calls.
236 Meanwhile, it is not discarded as dead code.
240 recordSelectorFieldLabel :: Id -> FieldLabel
241 recordSelectorFieldLabel id = case globalIdDetails id of
242 RecordSelId lbl -> lbl
243 other -> panic "recordSelectorFieldLabel"
245 isRecordSelector id = case globalIdDetails id of
246 RecordSelId lbl -> True
249 isPrimOpId id = case globalIdDetails id of
253 isPrimOpId_maybe id = case globalIdDetails id of
254 PrimOpId op -> Just op
257 isFCallId id = case globalIdDetails id of
261 isFCallId_maybe id = case globalIdDetails id of
262 FCallId call -> Just call
265 isDataConWorkId id = case globalIdDetails id of
266 DataConWorkId _ -> True
269 isDataConWorkId_maybe id = case globalIdDetails id of
270 DataConWorkId con -> Just con
273 -- hasNoBinding returns True of an Id which may not have a
274 -- binding, even though it is defined in this module.
275 -- Data constructor workers used to be things of this kind, but
276 -- they aren't any more. Instead, we inject a binding for
277 -- them at the CorePrep stage.
278 -- EXCEPT: unboxed tuples, which definitely have no binding
279 hasNoBinding id = case globalIdDetails id of
282 DataConWorkId dc -> isUnboxedTupleCon dc
285 isImplicitId :: Id -> Bool
286 -- isImplicitId tells whether an Id's info is implied by other
287 -- declarations, so we don't need to put its signature in an interface
288 -- file, even if it's mentioned in some other interface unfolding.
290 = case globalIdDetails id of
291 RecordSelId _ -> True
295 DataConWorkId _ -> True
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 is not an implicit Id; it must *not* be omitted, because
300 -- it carries version info for the instance decl
303 idIsFrom :: Module -> Id -> Bool
304 idIsFrom mod id = nameIsLocalOrFrom mod (idName id)
308 isDeadBinder :: Id -> Bool
309 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
310 | otherwise = False -- TyVars count as not dead
314 %************************************************************************
316 \subsection{IdInfo stuff}
318 %************************************************************************
321 ---------------------------------
323 idArity :: Id -> Arity
324 idArity id = arityInfo (idInfo id)
326 setIdArity :: Id -> Arity -> Id
327 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
329 #ifdef OLD_STRICTNESS
330 ---------------------------------
332 idStrictness :: Id -> StrictnessInfo
333 idStrictness id = strictnessInfo (idInfo id)
335 setIdStrictness :: Id -> StrictnessInfo -> Id
336 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
339 -- isBottomingId returns true if an application to n args would diverge
340 isBottomingId :: Id -> Bool
341 isBottomingId id = isBottomingSig (idNewStrictness id)
343 idNewStrictness_maybe :: Id -> Maybe StrictSig
344 idNewStrictness :: Id -> StrictSig
346 idNewStrictness_maybe id = newStrictnessInfo (idInfo id)
347 idNewStrictness id = idNewStrictness_maybe id `orElse` topSig
349 setIdNewStrictness :: Id -> StrictSig -> Id
350 setIdNewStrictness id sig = modifyIdInfo (`setNewStrictnessInfo` Just sig) id
352 zapIdNewStrictness :: Id -> Id
353 zapIdNewStrictness id = modifyIdInfo (`setNewStrictnessInfo` Nothing) id
355 ---------------------------------
357 idWorkerInfo :: Id -> WorkerInfo
358 idWorkerInfo id = workerInfo (idInfo id)
360 setIdWorkerInfo :: Id -> WorkerInfo -> Id
361 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
363 ---------------------------------
365 idUnfolding :: Id -> Unfolding
366 idUnfolding id = unfoldingInfo (idInfo id)
368 setIdUnfolding :: Id -> Unfolding -> Id
369 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
371 #ifdef OLD_STRICTNESS
372 ---------------------------------
374 idDemandInfo :: Id -> Demand.Demand
375 idDemandInfo id = demandInfo (idInfo id)
377 setIdDemandInfo :: Id -> Demand.Demand -> Id
378 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
381 idNewDemandInfo_maybe :: Id -> Maybe NewDemand.Demand
382 idNewDemandInfo :: Id -> NewDemand.Demand
384 idNewDemandInfo_maybe id = newDemandInfo (idInfo id)
385 idNewDemandInfo id = newDemandInfo (idInfo id) `orElse` NewDemand.topDmd
387 setIdNewDemandInfo :: Id -> NewDemand.Demand -> Id
388 setIdNewDemandInfo id dmd = modifyIdInfo (`setNewDemandInfo` Just dmd) id
390 ---------------------------------
392 idSpecialisation :: Id -> CoreRules
393 idSpecialisation id = specInfo (idInfo id)
395 idCoreRules :: Id -> [IdCoreRule]
396 idCoreRules id = [(id,rule) | rule <- rulesRules (idSpecialisation id)]
398 setIdSpecialisation :: Id -> CoreRules -> Id
399 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
401 ---------------------------------
403 idCafInfo :: Id -> CafInfo
404 #ifdef OLD_STRICTNESS
405 idCafInfo id = case cgInfo (idInfo id) of
406 NoCgInfo -> pprPanic "idCafInfo" (ppr id)
407 info -> cgCafInfo info
409 idCafInfo id = cafInfo (idInfo id)
412 setIdCafInfo :: Id -> CafInfo -> Id
413 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
415 ---------------------------------
417 #ifdef OLD_STRICTNESS
418 idCprInfo :: Id -> CprInfo
419 idCprInfo id = cprInfo (idInfo id)
421 setIdCprInfo :: Id -> CprInfo -> Id
422 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
425 ---------------------------------
427 idOccInfo :: Id -> OccInfo
428 idOccInfo id = occInfo (idInfo id)
430 setIdOccInfo :: Id -> OccInfo -> Id
431 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
435 ---------------------------------
437 The inline pragma tells us to be very keen to inline this Id, but it's still
438 OK not to if optimisation is switched off.
441 idInlinePragma :: Id -> InlinePragInfo
442 idInlinePragma id = inlinePragInfo (idInfo id)
444 setInlinePragma :: Id -> InlinePragInfo -> Id
445 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
447 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
448 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
452 ---------------------------------
455 idLBVarInfo :: Id -> LBVarInfo
456 idLBVarInfo id = lbvarInfo (idInfo id)
458 isOneShotLambda :: Id -> Bool
459 isOneShotLambda id = case idLBVarInfo id of
460 IsOneShotLambda -> True
463 setOneShotLambda :: Id -> Id
464 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
466 clearOneShotLambda :: Id -> Id
467 clearOneShotLambda id
468 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
471 -- But watch out: this may change the type of something else
473 -- If we change the one-shot-ness of x, f's type changes
477 zapLamIdInfo :: Id -> Id
478 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id
480 zapDemandIdInfo id = maybeModifyIdInfo zapDemandInfo id