Id.lhs 9.07 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, GenId,
9

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

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

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

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

	-- Inline pragma stuff
	getInlinePragma, setInlinePragma, modifyInlinePragma, 
	idWantsToBeINLINEd, idMustBeINLINEd, idMustNotBeINLINEd,
	isSpecPragmaId,
32
	
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

	isRecordSelector,
	isPrimitiveId_maybe, isDataConId_maybe,
	isConstantId,
	isBottomingId, 

	-- IdInfo stuff
	setIdUnfolding,
	setIdArity,
	setIdDemandInfo,
	setIdStrictness,
	setIdSpecialisation,
	setIdUpdateInfo,
	setIdCafInfo,

48 49 50 51
	getIdArity,
	getIdDemandInfo,
	getIdStrictness,
	getIdUnfolding,
52
	getIdSpecialisation,
53 54
	getIdUpdateInfo,
	getIdCafInfo
55

56
    ) where
57

58
#include "HsVersions.h"
sof's avatar
sof committed
59

sof's avatar
sof committed
60
import {-# SOURCE #-} CoreUnfold ( Unfolding )
sof's avatar
sof committed
61

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

84 85 86 87 88 89 90 91
infixl 	1 `setIdUnfolding`,
	  `setIdArity`,
	  `setIdDemandInfo`,
	  `setIdStrictness`,
	  `setIdSpecialisation`,
	  `setIdUpdateInfo`,
	  `setInlinePragma`
	-- infixl so you can say (id `set` a `set` b)
92 93 94
\end{code}


95

96 97
%************************************************************************
%*									*
98
\subsection{Simple Id construction}
99 100 101
%*									*
%************************************************************************

102
\begin{code}
103 104 105 106 107
mkVanillaId :: Name -> (GenType flexi) -> GenId flexi
mkVanillaId name ty = mkId name ty VanillaId noIdInfo

mkImportedId :: Name -> Type -> IdInfo -> Id
mkImportedId name ty info = mkId name ty VanillaId info
108

109 110 111 112 113 114 115 116 117 118 119
mkUserId :: Name -> GenType flexi -> GenId flexi
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...
mkUserLocal :: OccName -> Unique -> GenType flexi -> GenId flexi
mkSysLocal  ::            Unique -> GenType flexi -> GenId flexi

mkSysLocal  uniq ty     = mkVanillaId (mkSysLocalName uniq)  ty
mkUserLocal occ uniq ty = mkVanillaId (mkLocalName uniq occ) ty
\end{code}
120 121 122 123 124 125

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}
126 127 128 129 130
-- "Wild Id" typically used when you need a binder that you don't expect to use
mkWildId :: Type -> Id
mkWildId ty = mkSysLocal (mkBuiltinUnique 1) ty

-- "Template locals" typically used in unfoldings
131
mkTemplateLocals :: [Type] -> [Id]
132 133 134
mkTemplateLocals tys = zipWith mkSysLocal
			       (getBuiltinUniques (length tys))
			       tys
135 136 137
\end{code}


138 139 140 141 142 143 144
%************************************************************************
%*									*
\subsection[Id-general-funs]{General @Id@-related functions}
%*									*
%************************************************************************

\begin{code}
145 146
idFreeTyVars :: (GenId flexi) -> (GenTyVarSet flexi)
idFreeTyVars id = tyVarsOfType (idType id)
147

148 149 150
setIdType :: GenId flexi1 -> GenType flexi2 -> GenId flexi2
	-- Add free tyvar info to the type
setIdType id ty = setVarType id (addFreeTyVars ty)
sof's avatar
sof committed
151

152 153
idPrimRep :: Id -> PrimRep
idPrimRep id = typePrimRep (idType id)
154 155
\end{code}

156 157 158 159
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.

160
\begin{code}
161 162 163
omitIfaceSigForId :: Id -> Bool
omitIfaceSigForId id
  | isWiredInName (idName id)
164 165 166
  = True

  | otherwise
167 168 169 170 171 172 173 174 175
  = case varDetails id of
	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!
176 177
\end{code}

178
See notes with setNameVisibility (Name.lhs)
179 180

\begin{code}
181 182 183
setIdVisibility :: Maybe Module -> Unique -> Id -> Id
setIdVisibility maybe_mod u id
  = setIdName id (setNameVisibility maybe_mod u (idName id))
184

185 186 187
mkIdVisible :: Module -> Unique -> Id -> Id
mkIdVisible mod u id 
  = setIdName id (mkNameVisible mod u (idName id))
188
\end{code}
sof's avatar
sof committed
189

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

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

201 202 203
isRecordSelector id = case varDetails id of
			RecordSelId lbl -> True
			other	  	-> False
sof's avatar
sof committed
204

205 206 207
isPrimitiveId_maybe id = case varDetails id of
			    ConstantId (PrimOp op) -> Just op
			    other		   -> Nothing
sof's avatar
sof committed
208

209 210 211
isDataConId_maybe id = case varDetails id of
			  ConstantId (DataCon con) -> Just con
			  other		           -> Nothing
sof's avatar
sof committed
212

213 214 215
isConstantId id = case varDetails id of
		    ConstantId _ -> True
		    other	 -> False
216 217
\end{code}

218 219 220

%************************************************************************
%*									*
221
\subsection{IdInfo stuff}
222 223 224
%*									*
%************************************************************************

225
\begin{code}
226 227 228 229
	---------------------------------
	-- ARITY
getIdArity :: GenId flexi -> ArityInfo
getIdArity id = arityInfo (idInfo id)
230

231 232
setIdArity :: GenId flexi -> ArityInfo -> GenId flexi
setIdArity id arity = modifyIdInfo id (arity `setArityInfo`)
233

234 235 236 237
	---------------------------------
	-- STRICTNESS
getIdStrictness :: GenId flexi -> StrictnessInfo
getIdStrictness id = strictnessInfo (idInfo id)
238

239 240
setIdStrictness :: GenId flexi -> StrictnessInfo -> GenId flexi
setIdStrictness id strict_info = modifyIdInfo id (strict_info `setStrictnessInfo`)
241

242 243
isBottomingId :: GenId flexi -> Bool
isBottomingId id = bottomIsGuaranteed (strictnessInfo (idInfo id))
244

245 246 247 248
	---------------------------------
	-- UNFOLDING
getIdUnfolding :: GenId flexi -> Unfolding
getIdUnfolding id = unfoldingInfo (idInfo id)
249

250 251
setIdUnfolding :: GenId flexi -> Unfolding -> GenId flexi
setIdUnfolding id unfolding = modifyIdInfo id (unfolding `setUnfoldingInfo`)
sof's avatar
sof committed
252

253 254 255
	---------------------------------
	-- DEMAND
getIdDemandInfo :: GenId flexi -> Demand
256
getIdDemandInfo id = demandInfo (idInfo id)
257

258 259
setIdDemandInfo :: GenId flexi -> Demand -> GenId flexi
setIdDemandInfo id demand_info = modifyIdInfo id (demand_info `setDemandInfo`)
260

261 262 263
	---------------------------------
	-- UPDATE INFO
getIdUpdateInfo :: GenId flexi -> UpdateInfo
264
getIdUpdateInfo id = updateInfo (idInfo id)
265

266 267
setIdUpdateInfo :: GenId flexi -> UpdateInfo -> GenId flexi
setIdUpdateInfo id upd_info = modifyIdInfo id (upd_info `setUpdateInfo`)
268

269 270 271
	---------------------------------
	-- SPECIALISATION
getIdSpecialisation :: GenId flexi -> IdSpecEnv
272
getIdSpecialisation id = specInfo (idInfo id)
273

274 275
setIdSpecialisation :: GenId flexi -> IdSpecEnv -> GenId flexi
setIdSpecialisation id spec_info = modifyIdInfo id (spec_info `setSpecInfo`)
276

277 278 279 280
	---------------------------------
	-- CAF INFO
getIdCafInfo :: GenId flexi -> CafInfo
getIdCafInfo id = cafInfo (idInfo id)
281

282 283
setIdCafInfo :: GenId flexi -> CafInfo -> GenId flexi
setIdCafInfo id caf_info = modifyIdInfo id (caf_info `setCafInfo`)
284 285
\end{code}

sof's avatar
sof committed
286

287 288 289 290
	---------------------------------
	-- 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.
291 292

\begin{code}
293 294
getInlinePragma :: GenId flexi -> InlinePragInfo
getInlinePragma id = inlinePragInfo (idInfo id)
295

296 297
setInlinePragma :: GenId flexi -> InlinePragInfo -> GenId flexi
setInlinePragma id prag = modifyIdInfo id (setInlinePragInfo prag)
298

299 300
modifyInlinePragma :: GenId flexi -> (InlinePragInfo -> InlinePragInfo) -> GenId flexi
modifyInlinePragma id fn = modifyIdInfo id (\info -> setInlinePragInfo (fn (inlinePragInfo info)) info)
301

302 303 304 305 306
idWantsToBeINLINEd :: GenId flexi -> Bool
idWantsToBeINLINEd id = case getInlinePragma id of
			  IWantToBeINLINEd -> True
			  IMustBeINLINEd   -> True
			  other		   -> False
307

308 309 310 311 312
idMustNotBeINLINEd id = case getInlinePragma id of
			  IMustNotBeINLINEd -> True
			  IAmASpecPragmaId  -> True
			  IAmALoopBreaker   -> True
			  other		    -> False
313

314 315 316
idMustBeINLINEd id =  case getInlinePragma id of
			IMustBeINLINEd -> True
			other	       -> False
317

318 319 320
isSpecPragmaId id = case getInlinePragma id of
			IAmASpecPragmaId -> True
			other		 -> False
321
\end{code}