2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Id]{@Ids@: Value and constructor identifiers}
10 -- Simple construction
11 mkVanillaId, mkImportedId, mkSysLocal, mkUserLocal,
12 mkTemplateLocals, mkWildId, mkUserId,
15 idName, idType, idUnique, idInfo, idDetails,
17 recordSelectorFieldLabel,
20 setIdName, setIdUnique, setIdType, setIdInfo, mkIdVisible,
27 -- Inline pragma stuff
28 getInlinePragma, setInlinePragma, modifyInlinePragma,
29 idWantsToBeINLINEd, idMustBeINLINEd, idMustNotBeINLINEd,
34 isPrimitiveId_maybe, isDataConId_maybe,
36 isBottomingId, idAppIsBottom,
57 #include "HsVersions.h"
59 import {-# SOURCE #-} CoreUnfold ( Unfolding )
61 import Var ( Id, DictId, VarDetails(..),
63 idName, idType, idUnique, idInfo, idDetails,
64 setIdName, setVarType, setIdUnique, setIdInfo, modifyIdInfo,
68 import Type ( Type, tyVarsOfType, typePrimRep, addFreeTyVars )
70 import Demand ( Demand )
71 import Name ( Name, OccName, Module,
72 mkSysLocalName, mkLocalName,
73 isWiredInName, mkNameVisible
75 import Const ( Con(..) )
76 import PrimRep ( PrimRep )
77 import PrimOp ( PrimOp )
78 import FieldLabel ( FieldLabel(..) )
79 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques )
82 infixl 1 `setIdUnfolding`,
86 `setIdSpecialisation`,
89 -- infixl so you can say (id `set` a `set` b)
94 %************************************************************************
96 \subsection{Simple Id construction}
98 %************************************************************************
101 mkVanillaId :: Name -> Type -> Id
102 mkVanillaId name ty = mkId name (addFreeTyVars ty) VanillaId noIdInfo
104 mkImportedId :: Name -> Type -> IdInfo -> Id
105 mkImportedId name ty info = mkId name (addFreeTyVars ty) VanillaId info
107 mkUserId :: Name -> Type -> Id
108 mkUserId name ty = mkVanillaId name ty
110 -- SysLocal: for an Id being created by the compiler out of thin air...
111 -- UserLocal: an Id with a name the user might recognize...
112 mkUserLocal :: OccName -> Unique -> Type -> Id
113 mkSysLocal :: FAST_STRING -> Unique -> Type -> Id
115 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
116 mkUserLocal occ uniq ty = mkVanillaId (mkLocalName uniq occ) ty
119 Make some local @Ids@ for a template @CoreExpr@. These have bogus
120 @Uniques@, but that's OK because the templates are supposed to be
121 instantiated before use.
124 -- "Wild Id" typically used when you need a binder that you don't expect to use
125 mkWildId :: Type -> Id
126 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
128 -- "Template locals" typically used in unfoldings
129 mkTemplateLocals :: [Type] -> [Id]
130 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
131 (getBuiltinUniques (length tys))
136 %************************************************************************
138 \subsection[Id-general-funs]{General @Id@-related functions}
140 %************************************************************************
143 idFreeTyVars :: Id -> TyVarSet
144 idFreeTyVars id = tyVarsOfType (idType id)
146 setIdType :: Id -> Type -> Id
147 -- Add free tyvar info to the type
148 setIdType id ty = setVarType id (addFreeTyVars ty)
150 idPrimRep :: Id -> PrimRep
151 idPrimRep id = typePrimRep (idType id)
154 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
155 so we don't need to put its signature in an interface file, even if it's mentioned
156 in some other interface unfolding.
159 omitIfaceSigForId :: Id -> Bool
161 | isWiredInName (idName id)
165 = case idDetails id of
166 RecordSelId _ -> True -- Includes dictionary selectors
168 -- ConstantIds are implied by their type or class decl;
169 -- remember that all type and class decls appear in the interface file.
170 -- The dfun id must *not* be omitted, because it carries version info for
173 other -> False -- Don't omit!
177 mkIdVisible :: Module -> Unique -> Id -> Id
179 = setIdName id (mkNameVisible mod u (idName id))
182 %************************************************************************
184 \subsection{Special Ids}
186 %************************************************************************
189 recordSelectorFieldLabel :: Id -> FieldLabel
190 recordSelectorFieldLabel id = case idDetails id of
191 RecordSelId lbl -> lbl
193 isRecordSelector id = case idDetails id of
194 RecordSelId lbl -> True
197 isPrimitiveId_maybe id = case idDetails id of
198 ConstantId (PrimOp op) -> Just op
201 isDataConId_maybe id = case idDetails id of
202 ConstantId (DataCon con) -> Just con
205 isConstantId id = case idDetails id of
211 %************************************************************************
213 \subsection{IdInfo stuff}
215 %************************************************************************
218 ---------------------------------
220 getIdArity :: Id -> ArityInfo
221 getIdArity id = arityInfo (idInfo id)
223 setIdArity :: Id -> ArityInfo -> Id
224 setIdArity id arity = modifyIdInfo id (arity `setArityInfo`)
226 ---------------------------------
228 getIdStrictness :: Id -> StrictnessInfo
229 getIdStrictness id = strictnessInfo (idInfo id)
231 setIdStrictness :: Id -> StrictnessInfo -> Id
232 setIdStrictness id strict_info = modifyIdInfo id (strict_info `setStrictnessInfo`)
234 -- isBottomingId returns true if an application to n args would diverge
235 isBottomingId :: Id -> Bool
236 isBottomingId id = isBottomingStrictness (strictnessInfo (idInfo id))
238 idAppIsBottom :: Id -> Int -> Bool
239 idAppIsBottom id n = appIsBottom (strictnessInfo (idInfo id)) n
241 ---------------------------------
243 getIdUnfolding :: Id -> Unfolding
244 getIdUnfolding id = unfoldingInfo (idInfo id)
246 setIdUnfolding :: Id -> Unfolding -> Id
247 setIdUnfolding id unfolding = modifyIdInfo id (unfolding `setUnfoldingInfo`)
249 ---------------------------------
251 getIdDemandInfo :: Id -> Demand
252 getIdDemandInfo id = demandInfo (idInfo id)
254 setIdDemandInfo :: Id -> Demand -> Id
255 setIdDemandInfo id demand_info = modifyIdInfo id (demand_info `setDemandInfo`)
257 ---------------------------------
259 getIdUpdateInfo :: Id -> UpdateInfo
260 getIdUpdateInfo id = updateInfo (idInfo id)
262 setIdUpdateInfo :: Id -> UpdateInfo -> Id
263 setIdUpdateInfo id upd_info = modifyIdInfo id (upd_info `setUpdateInfo`)
265 ---------------------------------
267 getIdSpecialisation :: Id -> IdSpecEnv
268 getIdSpecialisation id = specInfo (idInfo id)
270 setIdSpecialisation :: Id -> IdSpecEnv -> Id
271 setIdSpecialisation id spec_info = modifyIdInfo id (spec_info `setSpecInfo`)
273 ---------------------------------
275 getIdCafInfo :: Id -> CafInfo
276 getIdCafInfo id = cafInfo (idInfo id)
278 setIdCafInfo :: Id -> CafInfo -> Id
279 setIdCafInfo id caf_info = modifyIdInfo id (caf_info `setCafInfo`)
283 ---------------------------------
285 The inline pragma tells us to be very keen to inline this Id, but it's still
286 OK not to if optimisation is switched off.
289 getInlinePragma :: Id -> InlinePragInfo
290 getInlinePragma id = inlinePragInfo (idInfo id)
292 setInlinePragma :: Id -> InlinePragInfo -> Id
293 setInlinePragma id prag = modifyIdInfo id (setInlinePragInfo prag)
295 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
296 modifyInlinePragma id fn = modifyIdInfo id (\info -> setInlinePragInfo (fn (inlinePragInfo info)) info)
298 idWantsToBeINLINEd :: Id -> Bool
299 idWantsToBeINLINEd id = case getInlinePragma id of
300 IWantToBeINLINEd -> True
301 IMustBeINLINEd -> True
304 idMustNotBeINLINEd id = case getInlinePragma id of
305 IMustNotBeINLINEd -> True
306 IAmASpecPragmaId -> True
307 IAmALoopBreaker -> True
310 idMustBeINLINEd id = case getInlinePragma id of
311 IMustBeINLINEd -> True
314 isSpecPragmaId id = case getInlinePragma id of
315 IAmASpecPragmaId -> True