Id.lhs 13.1 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
	-- Simple construction
11
	mkId, mkVanillaId, mkSysLocal, mkUserLocal,
12
	mkTemplateLocals, mkTemplateLocalsNum, mkWildId, mkTemplateLocal,
13 14

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

19
	-- Modifying an Id
20
	setIdName, setIdUnique, setIdType, setIdNoDiscard, 
21
	setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
22
	zapFragileIdInfo, zapLamIdInfo,
23 24

	-- Predicates
25
	isImplicitId, isDeadBinder,
26
	externallyVisibleId,
27
	isIP,
28
	isSpecPragmaId,	isRecordSelector,
29 30 31
	isPrimOpId, isPrimOpId_maybe, isDictFunId,
	isDataConId, isDataConId_maybe, 
	isDataConWrapId, isDataConWrapId_maybe,
32
	isBottomingId,
33
	isExportedId, isLocalId, 
34 35 36 37 38
	hasNoBinding,

	-- Inline pragma stuff
	idInlinePragma, setInlinePragma, modifyInlinePragma, 

39

40
	-- One shot lambda stuff
41
	isOneShotLambda, setOneShotLambda, clearOneShotLambda,
42

43 44
	-- IdInfo stuff
	setIdUnfolding,
45
	setIdArityInfo,
46 47
	setIdDemandInfo,
	setIdStrictness,
48
        setIdTyGenInfo,
49
	setIdWorkerInfo,
50 51
	setIdSpecialisation,
	setIdCafInfo,
52
	setIdCprInfo,
53
	setIdOccInfo,
54

55 56 57 58
	idArity, idArityInfo, 
	idFlavour,
	idDemandInfo,
	idStrictness,
59
        idTyGenInfo,
60 61 62 63 64 65
	idWorkerInfo,
	idUnfolding,
	idSpecialisation,
	idCafInfo,
	idCprInfo,
	idLBVarInfo,
66
	idOccInfo,
67

68
    ) where
69

70
#include "HsVersions.h"
sof's avatar
sof committed
71 72


73
import CoreSyn		( Unfolding, CoreRules )
74
import BasicTypes	( Arity )
75 76 77 78
import Var		( Id, DictId,
			  isId, mkIdVar,
			  idName, idType, idUnique, idInfo,
			  setIdName, setVarType, setIdUnique, 
79 80
			  setIdInfo, lazySetIdInfo, modifyIdInfo, 
			  maybeModifyIdInfo,
81 82
			  externallyVisibleId
			)
83
import Type		( Type, typePrimRep, addFreeTyVars, 
84
                          usOnce, seqType, splitTyConApp_maybe )
85 86 87

import IdInfo 

88
import Demand		( Demand )
sof's avatar
sof committed
89
import Name	 	( Name, OccName,
90
			  mkSysLocalName, mkLocalName,
91
			  getOccName, isIPOcc
sof's avatar
sof committed
92
			) 
93
import OccName		( UserFS )
94
import PrimRep		( PrimRep )
95
import TysPrim		( statePrimTyCon )
96
import FieldLabel	( FieldLabel )
97
import SrcLoc		( SrcLoc )
98 99
import Unique		( Unique, mkBuiltinUnique, getBuiltinUniques, 
			  getNumBuiltinUniques )
100
import Outputable
101

102
infixl 	1 `setIdUnfolding`,
103
	  `setIdArityInfo`,
104 105
	  `setIdDemandInfo`,
	  `setIdStrictness`,
106
	  `setIdTyGenInfo`,
107
	  `setIdWorkerInfo`,
108
	  `setIdSpecialisation`,
109
	  `setInlinePragma`,
110 111
	  `idCafInfo`,
	  `idCprInfo`
112

113
	-- infixl so you can say (id `set` a `set` b)
114 115 116
\end{code}


117

118 119
%************************************************************************
%*									*
120
\subsection{Simple Id construction}
121 122 123
%*									*
%************************************************************************

124 125 126 127
Absolutely all Ids are made by mkId.  It 
	a) Pins free-tyvar-info onto the Id's type, 
	   where it can easily be found.
	b) Ensures that exported Ids are 
128

129 130
\begin{code}
mkId :: Name -> Type -> IdInfo -> Id
131
mkId name ty info = mkIdVar name (addFreeTyVars ty) info
132
\end{code}
133

134 135 136
\begin{code}
mkVanillaId :: Name -> Type -> Id
mkVanillaId name ty = mkId name ty vanillaIdInfo
137 138 139

-- SysLocal: for an Id being created by the compiler out of thin air...
-- UserLocal: an Id with a name the user might recognize...
140 141
mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
mkSysLocal  :: UserFS  -> Unique -> Type -> Id
142

143 144
mkSysLocal  fs uniq ty      = mkVanillaId (mkSysLocalName uniq fs)      ty
mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName    uniq occ loc) ty
145
\end{code}
146 147 148 149 150 151

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}
152 153
-- "Wild Id" typically used when you need a binder that you don't expect to use
mkWildId :: Type -> Id
154
mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
155 156

-- "Template locals" typically used in unfoldings
157
mkTemplateLocals :: [Type] -> [Id]
158
mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
159 160
			       (getBuiltinUniques (length tys))
			       tys
161

162 163 164 165 166
mkTemplateLocalsNum :: Int -> [Type] -> [Id]
mkTemplateLocalsNum n tys = zipWith (mkSysLocal SLIT("tpl"))
			       (getNumBuiltinUniques n (length tys))
			       tys

167 168
mkTemplateLocal :: Int -> Type -> Id
mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
169 170 171
\end{code}


172 173 174 175 176 177 178
%************************************************************************
%*									*
\subsection[Id-general-funs]{General @Id@-related functions}
%*									*
%************************************************************************

\begin{code}
179
setIdType :: Id -> Type -> Id
180
	-- Add free tyvar info to the type
181
setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
sof's avatar
sof committed
182

183 184
idPrimRep :: Id -> PrimRep
idPrimRep id = typePrimRep (idType id)
185 186 187 188 189
\end{code}


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

sof's avatar
sof committed
194
\begin{code}
195 196 197 198 199 200 201
idFlavour :: Id -> IdFlavour
idFlavour id = flavourInfo (idInfo id)

setIdNoDiscard :: Id -> Id
setIdNoDiscard id	-- Make an Id into a NoDiscardId, unless it is already
  = modifyIdInfo setNoDiscardInfo id

202
recordSelectorFieldLabel :: Id -> FieldLabel
203
recordSelectorFieldLabel id = case idFlavour id of
204
				RecordSelId lbl -> lbl
sof's avatar
sof committed
205

206
isRecordSelector id = case idFlavour id of
207 208
			RecordSelId lbl -> True
			other	  	-> False
sof's avatar
sof committed
209

210 211 212 213 214 215 216 217 218 219 220
isPrimOpId id = case idFlavour id of
		    PrimOpId op -> True
		    other	-> False

isPrimOpId_maybe id = case idFlavour id of
			    PrimOpId op -> Just op
			    other	-> Nothing

isDataConId id = case idFlavour id of
			DataConId _ -> True
			other	    -> False
sof's avatar
sof committed
221

222
isDataConId_maybe id = case idFlavour id of
223 224
			  DataConId con -> Just con
			  other	        -> Nothing
sof's avatar
sof committed
225

226 227 228
isDataConWrapId_maybe id = case idFlavour id of
				  DataConWrapId con -> Just con
				  other	            -> Nothing
229

230 231 232
isDataConWrapId id = case idFlavour id of
			DataConWrapId con -> True
			other	          -> False
233

234 235 236 237
isSpecPragmaId id = case idFlavour id of
			SpecPragmaId -> True
			other	     -> False

238
hasNoBinding id = case idFlavour id of
239 240 241
			DataConId _ -> True
			PrimOpId _  -> True
			other	    -> False
242
	-- hasNoBinding returns True of an Id which may not have a
243 244 245
	-- binding, even though it is defined in this module.  Notably,
	-- the constructors of a dictionary are in this situation.

246 247 248 249
isDictFunId id = case idFlavour id of
		   DictFunId -> True
		   other     -> False

250 251
-- Don't drop a binding for an exported Id,
-- if it otherwise looks dead.  
252
-- Perhaps a better name would be isDiscardableId
253
isExportedId :: Id -> Bool
254 255 256 257 258 259 260
isExportedId id = case idFlavour id of
			VanillaId  -> False
			other	   -> True

isLocalId :: Id -> Bool
-- True of Ids that are locally defined, but are not constants
-- like data constructors, record selectors, and the like. 
261
-- See comments with CoreFVs.isLocalVar
262 263 264 265 266 267 268 269 270 271
isLocalId id 
#ifdef DEBUG
  | not (isId id) = pprTrace "isLocalid" (ppr id) False
  | otherwise
#endif
  = case idFlavour id of
	 VanillaId    -> True
	 ExportedId   -> True
	 SpecPragmaId -> True
	 other	      -> False
272 273
\end{code}

274

275 276 277
isImplicitId 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.
278 279

\begin{code}
280 281
isImplicitId :: Id -> Bool
isImplicitId id
282
  = case idFlavour id of
283 284 285 286 287
	RecordSelId _   -> True	-- Includes dictionary selectors
        PrimOpId _      -> True
        DataConId _     -> True
	DataConWrapId _ -> True
		-- These are are implied by their type or class decl;
288 289 290
		-- 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
291
	other		-> False
292 293 294 295
\end{code}

\begin{code}
isDeadBinder :: Id -> Bool
296
isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
297
		  | otherwise = False	-- TyVars count as not dead
298 299

isIP id = isIPOcc (getOccName id)
300 301 302
\end{code}


303 304
%************************************************************************
%*									*
305
\subsection{IdInfo stuff}
306 307 308
%*									*
%************************************************************************

309
\begin{code}
310 311
	---------------------------------
	-- ARITY
312 313 314 315 316
idArityInfo :: Id -> ArityInfo
idArityInfo id = arityInfo (idInfo id)

idArity :: Id -> Arity
idArity id = arityLowerBound (idArityInfo id)
317

318 319
setIdArityInfo :: Id -> ArityInfo -> Id
setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
320

321 322
	---------------------------------
	-- STRICTNESS
323 324
idStrictness :: Id -> StrictnessInfo
idStrictness id = strictnessInfo (idInfo id)
325

326
setIdStrictness :: Id -> StrictnessInfo -> Id
327
setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
328

329 330
-- isBottomingId returns true if an application to n args would diverge
isBottomingId :: Id -> Bool
331
isBottomingId id = isBottomingStrictness (idStrictness id)
332

333 334 335 336 337 338 339 340
	---------------------------------
	-- TYPE GENERALISATION
idTyGenInfo :: Id -> TyGenInfo
idTyGenInfo id = tyGenInfo (idInfo id)

setIdTyGenInfo :: Id -> TyGenInfo -> Id
setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id

341 342
	---------------------------------
	-- WORKER ID
343 344
idWorkerInfo :: Id -> WorkerInfo
idWorkerInfo id = workerInfo (idInfo id)
345 346

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

349 350
	---------------------------------
	-- UNFOLDING
351 352
idUnfolding :: Id -> Unfolding
idUnfolding id = unfoldingInfo (idInfo id)
353

354
setIdUnfolding :: Id -> Unfolding -> Id
355
setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
sof's avatar
sof committed
356

357 358
	---------------------------------
	-- DEMAND
359 360
idDemandInfo :: Id -> Demand
idDemandInfo id = demandInfo (idInfo id)
361

362
setIdDemandInfo :: Id -> Demand -> Id
363
setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
364

365 366
	---------------------------------
	-- SPECIALISATION
367 368
idSpecialisation :: Id -> CoreRules
idSpecialisation id = specInfo (idInfo id)
369

370 371
setIdSpecialisation :: Id -> CoreRules -> Id
setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
372

373 374
	---------------------------------
	-- CAF INFO
375 376
idCafInfo :: Id -> CafInfo
idCafInfo id = cafInfo (idInfo id)
377

378
setIdCafInfo :: Id -> CafInfo -> Id
379
setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
380 381 382

	---------------------------------
	-- CPR INFO
383 384
idCprInfo :: Id -> CprInfo
idCprInfo id = cprInfo (idInfo id)
385 386

setIdCprInfo :: Id -> CprInfo -> Id
387
setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
388 389 390

	---------------------------------
	-- Occcurrence INFO
391 392
idOccInfo :: Id -> OccInfo
idOccInfo id = occInfo (idInfo id)
393 394 395

setIdOccInfo :: Id -> OccInfo -> Id
setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
396 397
\end{code}

sof's avatar
sof committed
398

399 400 401 402
	---------------------------------
	-- 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.
403 404

\begin{code}
405 406
idInlinePragma :: Id -> InlinePragInfo
idInlinePragma id = inlinePragInfo (idInfo id)
407

408
setInlinePragma :: Id -> InlinePragInfo -> Id
409
setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
410

411
modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
412
modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
413
\end{code}
414 415 416 417 418


	---------------------------------
	-- ONE-SHOT LAMBDAS
\begin{code}
419 420 421
idLBVarInfo :: Id -> LBVarInfo
idLBVarInfo id = lbvarInfo (idInfo id)

422
isOneShotLambda :: Id -> Bool
423 424 425 426 427 428 429 430
isOneShotLambda id = analysis || hack
  where analysis = case idLBVarInfo id of
                     LBVarInfo u    | u == usOnce             -> True
                     other                                    -> False
        hack     = case splitTyConApp_maybe (idType id) of
                     Just (tycon,_) | tycon == statePrimTyCon -> True
                     other                                    -> False

431 432 433 434 435 436 437 438 439 440 441
	-- The last clause is a gross hack.  It claims that 
	-- every function over realWorldStatePrimTy is a one-shot
	-- function.  This is pretty true in practice, and makes a big
	-- difference.  For example, consider
	--	a `thenST` \ r -> ...E...
	-- The early full laziness pass, if it doesn't know that r is one-shot
	-- will pull out E (let's say it doesn't mention r) to give
	--	let lvl = E in a `thenST` \ r -> ...lvl...
	-- When `thenST` gets inlined, we end up with
	--	let lvl = E in \s -> case a s of (r, s') -> ...lvl...
	-- and we don't re-inline E.
442
	--
443 444
	-- It would be better to spot that r was one-shot to start with, but
	-- I don't want to rely on that.
445 446 447
	--
	-- Another good example is in fill_in in PrelPack.lhs.  We should be able to
	-- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
448 449

setOneShotLambda :: Id -> Id
450
setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
451 452 453 454 455 456 457 458 459

clearOneShotLambda :: Id -> Id
clearOneShotLambda id 
  | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
  | otherwise	       = id			

-- But watch out: this may change the type of something else
--	f = \x -> e
-- If we change the one-shot-ness of x, f's type changes
460
\end{code}
461 462 463 464 465 466 467 468 469

\begin{code}
zapFragileIdInfo :: Id -> Id
zapFragileIdInfo id = maybeModifyIdInfo zapFragileInfo id

zapLamIdInfo :: Id -> Id
zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id
\end{code}