Id.lhs 9.28 KB
Newer Older
1 2
%
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 4 5 6
%
\section[Id]{@Ids@: Value and constructor identifiers}

\begin{code}
7
module Id (
8
	Id, DictId,
9

10 11
	-- Simple construction
	mkVanillaId, mkImportedId, mkSysLocal, mkUserLocal,
12
	mkTemplateLocals, mkTemplateLocal, mkWildId, mkUserId,
13 14

	-- Taking an Id apart
15
	idName, idType, idUnique, idInfo, idDetails,
16
	idPrimRep, isId,
17
	recordSelectorFieldLabel,
18

19
	-- Modifying an Id
20
	setIdName, setIdUnique, setIdType, setIdInfo,
21 22

	-- Predicates
23
	omitIfaceSigForId,
24
	externallyVisibleId,
25 26 27 28 29 30
	idFreeTyVars, 

	-- Inline pragma stuff
	getInlinePragma, setInlinePragma, modifyInlinePragma, 
	idWantsToBeINLINEd, idMustBeINLINEd, idMustNotBeINLINEd,
	isSpecPragmaId,
31
	
32 33 34 35

	isRecordSelector,
	isPrimitiveId_maybe, isDataConId_maybe,
	isConstantId,
36
	isBottomingId, idAppIsBottom,
37 38 39 40 41 42

	-- IdInfo stuff
	setIdUnfolding,
	setIdArity,
	setIdDemandInfo,
	setIdStrictness,
43
	setIdWorkerInfo,
44 45 46
	setIdSpecialisation,
	setIdUpdateInfo,
	setIdCafInfo,
47
	setIdCprInfo,
48

49 50 51
	getIdArity,
	getIdDemandInfo,
	getIdStrictness,
52
	getIdWorkerInfo,
53
	getIdUnfolding,
54
	getIdSpecialisation,
55
	getIdUpdateInfo,
56 57
	getIdCafInfo,
	getIdCprInfo
58

59
    ) where
60

61
#include "HsVersions.h"
sof's avatar
sof committed
62

sof's avatar
sof committed
63
import {-# SOURCE #-} CoreUnfold ( Unfolding )
sof's avatar
sof committed
64

65
import Var		( Id, DictId, VarDetails(..), 
66
			  isId, mkId, 
67
			  idName, idType, idUnique, idInfo, idDetails,
68 69 70 71
			  setIdName, setVarType, setIdUnique, setIdInfo, modifyIdInfo,
			  externallyVisibleId
			)
import VarSet
72
import Type		( Type, tyVarsOfType, typePrimRep, addFreeTyVars )
73
import IdInfo
74
import Demand		( Demand )
sof's avatar
sof committed
75
import Name	 	( Name, OccName,
76
			  mkSysLocalName, mkLocalName,
77
			  isWiredInName
sof's avatar
sof committed
78
			) 
79 80
import Const		( Con(..) )
import PrimRep		( PrimRep )
81
import PrimOp		( PrimOp )
82
import FieldLabel	( FieldLabel(..) )
83
import SrcLoc		( SrcLoc )
84
import Unique		( Unique, mkBuiltinUnique, getBuiltinUniques )
85
import Outputable
86

87 88 89 90
infixl 	1 `setIdUnfolding`,
	  `setIdArity`,
	  `setIdDemandInfo`,
	  `setIdStrictness`,
91
	  `setIdWorkerInfo`,
92 93
	  `setIdSpecialisation`,
	  `setIdUpdateInfo`,
94 95 96 97
	  `setInlinePragma`,
	  `getIdCafInfo`,
	  `getIdCprInfo`

98
	-- infixl so you can say (id `set` a `set` b)
99 100 101
\end{code}


102

103 104
%************************************************************************
%*									*
105
\subsection{Simple Id construction}
106 107 108
%*									*
%************************************************************************

109
\begin{code}
110 111
mkVanillaId :: Name -> Type -> Id
mkVanillaId name ty = mkId name (addFreeTyVars ty) VanillaId noIdInfo
112 113

mkImportedId :: Name -> Type -> IdInfo -> Id
114
mkImportedId name ty info = mkId name (addFreeTyVars ty) VanillaId info
115

116
mkUserId :: Name -> Type -> Id
117 118 119 120
mkUserId name ty = mkVanillaId name ty

-- SysLocal: for an Id being created by the compiler out of thin air...
-- UserLocal: an Id with a name the user might recognize...
121
mkUserLocal :: OccName     -> Unique -> Type -> SrcLoc -> Id
122
mkSysLocal  :: FAST_STRING -> Unique -> Type -> Id
123

124 125
mkSysLocal  fs uniq ty      = mkVanillaId (mkSysLocalName uniq fs)      ty
mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName    uniq occ loc) ty
126
\end{code}
127 128 129 130 131 132

Make some local @Ids@ for a template @CoreExpr@.  These have bogus
@Uniques@, but that's OK because the templates are supposed to be
instantiated before use.

\begin{code}
133 134
-- "Wild Id" typically used when you need a binder that you don't expect to use
mkWildId :: Type -> Id
135
mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
136 137

-- "Template locals" typically used in unfoldings
138
mkTemplateLocals :: [Type] -> [Id]
139
mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
140 141
			       (getBuiltinUniques (length tys))
			       tys
142 143 144

mkTemplateLocal :: Int -> Type -> Id
mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
145 146 147
\end{code}


148 149 150 151 152 153 154
%************************************************************************
%*									*
\subsection[Id-general-funs]{General @Id@-related functions}
%*									*
%************************************************************************

\begin{code}
155
idFreeTyVars :: Id -> TyVarSet
156
idFreeTyVars id = tyVarsOfType (idType id)
157

158
setIdType :: Id -> Type -> Id
159 160
	-- Add free tyvar info to the type
setIdType id ty = setVarType id (addFreeTyVars ty)
sof's avatar
sof committed
161

162 163
idPrimRep :: Id -> PrimRep
idPrimRep id = typePrimRep (idType id)
164 165
\end{code}

166 167 168 169
omitIfaceSigForId tells whether an Id's info is implied by other declarations,
so we don't need to put its signature in an interface file, even if it's mentioned
in some other interface unfolding.

170
\begin{code}
171 172 173
omitIfaceSigForId :: Id -> Bool
omitIfaceSigForId id
  | isWiredInName (idName id)
174 175 176
  = True

  | otherwise
177
  = case idDetails id of
178 179 180 181 182 183 184 185
	RecordSelId _  -> True	-- Includes dictionary selectors
        ConstantId _   -> True
		-- ConstantIds are implied by their type or class decl;
		-- remember that all type and class decls appear in the interface file.
		-- The dfun id must *not* be omitted, because it carries version info for
		-- the instance decl

	other	       -> False	-- Don't omit!
186 187 188 189
\end{code}

%************************************************************************
%*									*
190
\subsection{Special Ids}
191 192 193
%*									*
%************************************************************************

sof's avatar
sof committed
194
\begin{code}
195
recordSelectorFieldLabel :: Id -> FieldLabel
196
recordSelectorFieldLabel id = case idDetails id of
197
				RecordSelId lbl -> lbl
sof's avatar
sof committed
198

199
isRecordSelector id = case idDetails id of
200 201
			RecordSelId lbl -> True
			other	  	-> False
sof's avatar
sof committed
202

203
isPrimitiveId_maybe id = case idDetails id of
204 205
			    ConstantId (PrimOp op) -> Just op
			    other		   -> Nothing
sof's avatar
sof committed
206

207
isDataConId_maybe id = case idDetails id of
208 209
			  ConstantId (DataCon con) -> Just con
			  other		           -> Nothing
sof's avatar
sof committed
210

211
isConstantId id = case idDetails id of
212 213
		    ConstantId _ -> True
		    other	 -> False
214 215
\end{code}

216 217 218

%************************************************************************
%*									*
219
\subsection{IdInfo stuff}
220 221 222
%*									*
%************************************************************************

223
\begin{code}
224 225
	---------------------------------
	-- ARITY
226
getIdArity :: Id -> ArityInfo
227
getIdArity id = arityInfo (idInfo id)
228

229
setIdArity :: Id -> ArityInfo -> Id
230
setIdArity id arity = modifyIdInfo id (arity `setArityInfo`)
231

232 233
	---------------------------------
	-- STRICTNESS
234
getIdStrictness :: Id -> StrictnessInfo
235
getIdStrictness id = strictnessInfo (idInfo id)
236

237
setIdStrictness :: Id -> StrictnessInfo -> Id
238
setIdStrictness id strict_info = modifyIdInfo id (strict_info `setStrictnessInfo`)
239

240 241 242 243 244 245
-- isBottomingId returns true if an application to n args would diverge
isBottomingId :: Id -> Bool
isBottomingId id = isBottomingStrictness (strictnessInfo (idInfo id))

idAppIsBottom :: Id -> Int -> Bool
idAppIsBottom id n = appIsBottom (strictnessInfo (idInfo id)) n
246

247 248 249 250 251 252 253 254
	---------------------------------
	-- WORKER ID
getIdWorkerInfo :: Id -> WorkerInfo
getIdWorkerInfo id = workerInfo (idInfo id)

setIdWorkerInfo :: Id -> WorkerInfo -> Id
setIdWorkerInfo id work_info = modifyIdInfo id (work_info `setWorkerInfo`)

255 256
	---------------------------------
	-- UNFOLDING
257
getIdUnfolding :: Id -> Unfolding
258
getIdUnfolding id = unfoldingInfo (idInfo id)
259

260
setIdUnfolding :: Id -> Unfolding -> Id
261
setIdUnfolding id unfolding = modifyIdInfo id (unfolding `setUnfoldingInfo`)
sof's avatar
sof committed
262

263 264
	---------------------------------
	-- DEMAND
265
getIdDemandInfo :: Id -> Demand
266
getIdDemandInfo id = demandInfo (idInfo id)
267

268
setIdDemandInfo :: Id -> Demand -> Id
269
setIdDemandInfo id demand_info = modifyIdInfo id (demand_info `setDemandInfo`)
270

271 272
	---------------------------------
	-- UPDATE INFO
273
getIdUpdateInfo :: Id -> UpdateInfo
274
getIdUpdateInfo id = updateInfo (idInfo id)
275

276
setIdUpdateInfo :: Id -> UpdateInfo -> Id
277
setIdUpdateInfo id upd_info = modifyIdInfo id (upd_info `setUpdateInfo`)
278

279 280
	---------------------------------
	-- SPECIALISATION
281
getIdSpecialisation :: Id -> IdSpecEnv
282
getIdSpecialisation id = specInfo (idInfo id)
283

284
setIdSpecialisation :: Id -> IdSpecEnv -> Id
285
setIdSpecialisation id spec_info = modifyIdInfo id (spec_info `setSpecInfo`)
286

287 288
	---------------------------------
	-- CAF INFO
289
getIdCafInfo :: Id -> CafInfo
290
getIdCafInfo id = cafInfo (idInfo id)
291

292
setIdCafInfo :: Id -> CafInfo -> Id
293
setIdCafInfo id caf_info = modifyIdInfo id (caf_info `setCafInfo`)
294 295 296 297 298 299 300 301 302

	---------------------------------
	-- CPR INFO
getIdCprInfo :: Id -> CprInfo
getIdCprInfo id = cprInfo (idInfo id)

setIdCprInfo :: Id -> CprInfo -> Id
setIdCprInfo id cpr_info = modifyIdInfo id (cpr_info `setCprInfo`)

303 304
\end{code}

sof's avatar
sof committed
305

306 307 308 309
	---------------------------------
	-- INLINING
The inline pragma tells us to be very keen to inline this Id, but it's still
OK not to if optimisation is switched off.
310 311

\begin{code}
312
getInlinePragma :: Id -> InlinePragInfo
313
getInlinePragma id = inlinePragInfo (idInfo id)
314

315
setInlinePragma :: Id -> InlinePragInfo -> Id
316
setInlinePragma id prag = modifyIdInfo id (setInlinePragInfo prag)
317

318
modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
319
modifyInlinePragma id fn = modifyIdInfo id (\info -> setInlinePragInfo (fn (inlinePragInfo info)) info)
320

321
idWantsToBeINLINEd :: Id -> Bool
322 323 324 325
idWantsToBeINLINEd id = case getInlinePragma id of
			  IWantToBeINLINEd -> True
			  IMustBeINLINEd   -> True
			  other		   -> False
326

327 328 329 330 331
idMustNotBeINLINEd id = case getInlinePragma id of
			  IMustNotBeINLINEd -> True
			  IAmASpecPragmaId  -> True
			  IAmALoopBreaker   -> True
			  other		    -> False
332

333 334 335
idMustBeINLINEd id =  case getInlinePragma id of
			IMustBeINLINEd -> True
			other	       -> False
336

337 338 339
isSpecPragmaId id = case getInlinePragma id of
			IAmASpecPragmaId -> True
			other		 -> False
340
\end{code}