[project @ 1999-07-06 16:45:31 by simonpj]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Id.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[Id]{@Ids@: Value and constructor identifiers}
5
6 \begin{code}
7 module Id (
8         Id, DictId,
9
10         -- Simple construction
11         mkId, mkVanillaId, mkSysLocal, mkUserLocal,
12         mkTemplateLocals, 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, modifyIdInfo, maybeModifyIdInfo,
22
23         -- Predicates
24         omitIfaceSigForId,
25         externallyVisibleId,
26         idFreeTyVars, 
27
28         -- Inline pragma stuff
29         getInlinePragma, setInlinePragma, modifyInlinePragma, 
30         idMustBeINLINEd, idMustNotBeINLINEd,
31
32         isSpecPragmaId, isRecordSelector,
33         isPrimitiveId_maybe, isDataConId_maybe,
34         isConstantId, isBottomingId, idAppIsBottom,
35         isExportedId, isUserExportedId,
36
37         -- One shot lambda stuff
38         isOneShotLambda, setOneShotLambda,
39
40         -- IdInfo stuff
41         setIdUnfolding,
42         setIdArity,
43         setIdDemandInfo,
44         setIdStrictness,
45         setIdWorkerInfo,
46         setIdSpecialisation,
47         setIdUpdateInfo,
48         setIdCafInfo,
49         setIdCprInfo,
50
51         getIdArity,
52         getIdDemandInfo,
53         getIdStrictness,
54         getIdWorkerInfo,
55         getIdUnfolding,
56         getIdSpecialisation,
57         getIdUpdateInfo,
58         getIdCafInfo,
59         getIdCprInfo
60
61     ) where
62
63 #include "HsVersions.h"
64
65 import {-# SOURCE #-} CoreUnfold ( Unfolding )
66 import {-# SOURCE #-} CoreSyn    ( CoreRules )
67
68 import Var              ( Id, DictId,
69                           isId, mkIdVar,
70                           idName, idType, idUnique, idInfo,
71                           setIdName, setVarType, setIdUnique, 
72                           setIdInfo, modifyIdInfo, maybeModifyIdInfo,
73                           externallyVisibleId
74                         )
75 import VarSet
76 import Type             ( Type, tyVarsOfType, typePrimRep, addFreeTyVars )
77 import IdInfo
78 import Demand           ( Demand, isStrict, wwLazy )
79 import Name             ( Name, OccName,
80                           mkSysLocalName, mkLocalName,
81                           isWiredInName, isUserExportedName
82                         ) 
83 import Const            ( Con(..) )
84 import PrimRep          ( PrimRep )
85 import PrimOp           ( PrimOp )
86 import TysPrim          ( realWorldStatePrimTy )
87 import FieldLabel       ( FieldLabel(..) )
88 import SrcLoc           ( SrcLoc )
89 import Unique           ( Unique, mkBuiltinUnique, getBuiltinUniques )
90 import Outputable
91
92 infixl  1 `setIdUnfolding`,
93           `setIdArity`,
94           `setIdDemandInfo`,
95           `setIdStrictness`,
96           `setIdWorkerInfo`,
97           `setIdSpecialisation`,
98           `setIdUpdateInfo`,
99           `setInlinePragma`,
100           `getIdCafInfo`,
101           `getIdCprInfo`
102
103         -- infixl so you can say (id `set` a `set` b)
104 \end{code}
105
106
107
108 %************************************************************************
109 %*                                                                      *
110 \subsection{Simple Id construction}
111 %*                                                                      *
112 %************************************************************************
113
114 Absolutely all Ids are made by mkId.  It 
115         a) Pins free-tyvar-info onto the Id's type, 
116            where it can easily be found.
117         b) Ensures that exported Ids are 
118
119 \begin{code}
120 mkId :: Name -> Type -> IdInfo -> Id
121 mkId name ty info = mkIdVar name (addFreeTyVars ty) info'
122                   where
123                     info' | isUserExportedName name = setNoDiscardInfo info
124                           | otherwise               = info
125 \end{code}
126
127 \begin{code}
128 mkVanillaId :: Name -> Type -> Id
129 mkVanillaId name ty = mkId name ty vanillaIdInfo
130
131 -- SysLocal: for an Id being created by the compiler out of thin air...
132 -- UserLocal: an Id with a name the user might recognize...
133 mkUserLocal :: OccName     -> Unique -> Type -> SrcLoc -> Id
134 mkSysLocal  :: FAST_STRING -> Unique -> Type -> Id
135
136 mkSysLocal  fs uniq ty      = mkVanillaId (mkSysLocalName uniq fs)      ty
137 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName    uniq occ loc) ty
138 \end{code}
139
140 Make some local @Ids@ for a template @CoreExpr@.  These have bogus
141 @Uniques@, but that's OK because the templates are supposed to be
142 instantiated before use.
143
144 \begin{code}
145 -- "Wild Id" typically used when you need a binder that you don't expect to use
146 mkWildId :: Type -> Id
147 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
148
149 -- "Template locals" typically used in unfoldings
150 mkTemplateLocals :: [Type] -> [Id]
151 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
152                                (getBuiltinUniques (length tys))
153                                tys
154
155 mkTemplateLocal :: Int -> Type -> Id
156 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
157 \end{code}
158
159
160 %************************************************************************
161 %*                                                                      *
162 \subsection[Id-general-funs]{General @Id@-related functions}
163 %*                                                                      *
164 %************************************************************************
165
166 \begin{code}
167 idFreeTyVars :: Id -> TyVarSet
168 idFreeTyVars id = tyVarsOfType (idType id)
169
170 setIdType :: Id -> Type -> Id
171         -- Add free tyvar info to the type
172 setIdType id ty = setVarType id (addFreeTyVars ty)
173
174 idPrimRep :: Id -> PrimRep
175 idPrimRep id = typePrimRep (idType id)
176 \end{code}
177
178
179 %************************************************************************
180 %*                                                                      *
181 \subsection{Special Ids}
182 %*                                                                      *
183 %************************************************************************
184
185 \begin{code}
186 idFlavour :: Id -> IdFlavour
187 idFlavour id = flavourInfo (idInfo id)
188
189 setIdNoDiscard :: Id -> Id
190 setIdNoDiscard id       -- Make an Id into a NoDiscardId, unless it is already
191   = modifyIdInfo setNoDiscardInfo id
192
193 recordSelectorFieldLabel :: Id -> FieldLabel
194 recordSelectorFieldLabel id = case idFlavour id of
195                                 RecordSelId lbl -> lbl
196
197 isRecordSelector id = case idFlavour id of
198                         RecordSelId lbl -> True
199                         other           -> False
200
201 isPrimitiveId_maybe id = case idFlavour id of
202                             ConstantId (PrimOp op) -> Just op
203                             other                  -> Nothing
204
205 isDataConId_maybe id = case idFlavour id of
206                           ConstantId (DataCon con) -> Just con
207                           other                    -> Nothing
208
209 isConstantId id = case idFlavour id of
210                     ConstantId _ -> True
211                     other        -> False
212
213 isSpecPragmaId id = case idFlavour id of
214                         SpecPragmaId -> True
215                         other        -> False
216
217 -- Don't drop a binding for an exported Id,
218 -- if it otherwise looks dead.  
219 isExportedId :: Id -> Bool
220 isExportedId id = case idFlavour id of
221                         VanillaId -> False
222                         other     -> True       -- All the others are no-discard
223
224 -- Say if an Id was exported by the user
225 -- Implies isExportedId (see mkId above)
226 isUserExportedId :: Id -> Bool
227 isUserExportedId id = isUserExportedName (idName id)
228 \end{code}
229
230
231 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
232 so we don't need to put its signature in an interface file, even if it's mentioned
233 in some other interface unfolding.
234
235 \begin{code}
236 omitIfaceSigForId :: Id -> Bool
237 omitIfaceSigForId id
238   | isWiredInName (idName id)
239   = True
240
241   | otherwise
242   = case idFlavour id of
243         RecordSelId _  -> True  -- Includes dictionary selectors
244         ConstantId _   -> True
245                 -- ConstantIds are implied by their type or class decl;
246                 -- remember that all type and class decls appear in the interface file.
247                 -- The dfun id must *not* be omitted, because it carries version info for
248                 -- the instance decl
249
250         other          -> False -- Don't omit!
251 \end{code}
252
253
254
255 %************************************************************************
256 %*                                                                      *
257 \subsection{IdInfo stuff}
258 %*                                                                      *
259 %************************************************************************
260
261 \begin{code}
262         ---------------------------------
263         -- ARITY
264 getIdArity :: Id -> ArityInfo
265 getIdArity id = arityInfo (idInfo id)
266
267 setIdArity :: Id -> ArityInfo -> Id
268 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
269
270         ---------------------------------
271         -- STRICTNESS
272 getIdStrictness :: Id -> StrictnessInfo
273 getIdStrictness id = strictnessInfo (idInfo id)
274
275 setIdStrictness :: Id -> StrictnessInfo -> Id
276 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
277
278 -- isBottomingId returns true if an application to n args would diverge
279 isBottomingId :: Id -> Bool
280 isBottomingId id = isBottomingStrictness (strictnessInfo (idInfo id))
281
282 idAppIsBottom :: Id -> Int -> Bool
283 idAppIsBottom id n = appIsBottom (strictnessInfo (idInfo id)) n
284
285         ---------------------------------
286         -- WORKER ID
287 getIdWorkerInfo :: Id -> WorkerInfo
288 getIdWorkerInfo id = workerInfo (idInfo id)
289
290 setIdWorkerInfo :: Id -> WorkerInfo -> Id
291 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
292
293         ---------------------------------
294         -- UNFOLDING
295 getIdUnfolding :: Id -> Unfolding
296 getIdUnfolding id = unfoldingInfo (idInfo id)
297
298 setIdUnfolding :: Id -> Unfolding -> Id
299 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
300
301         ---------------------------------
302         -- DEMAND
303 getIdDemandInfo :: Id -> Demand
304 getIdDemandInfo id = demandInfo (idInfo id)
305
306 setIdDemandInfo :: Id -> Demand -> Id
307 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
308
309         ---------------------------------
310         -- UPDATE INFO
311 getIdUpdateInfo :: Id -> UpdateInfo
312 getIdUpdateInfo id = updateInfo (idInfo id)
313
314 setIdUpdateInfo :: Id -> UpdateInfo -> Id
315 setIdUpdateInfo id upd_info = modifyIdInfo (`setUpdateInfo` upd_info) id
316
317         ---------------------------------
318         -- SPECIALISATION
319 getIdSpecialisation :: Id -> CoreRules
320 getIdSpecialisation id = specInfo (idInfo id)
321
322 setIdSpecialisation :: Id -> CoreRules -> Id
323 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
324
325         ---------------------------------
326         -- CAF INFO
327 getIdCafInfo :: Id -> CafInfo
328 getIdCafInfo id = cafInfo (idInfo id)
329
330 setIdCafInfo :: Id -> CafInfo -> Id
331 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
332
333         ---------------------------------
334         -- CPR INFO
335 getIdCprInfo :: Id -> CprInfo
336 getIdCprInfo id = cprInfo (idInfo id)
337
338 setIdCprInfo :: Id -> CprInfo -> Id
339 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
340 \end{code}
341
342
343         ---------------------------------
344         -- INLINING
345 The inline pragma tells us to be very keen to inline this Id, but it's still
346 OK not to if optimisation is switched off.
347
348 \begin{code}
349 getInlinePragma :: Id -> InlinePragInfo
350 getInlinePragma id = inlinePragInfo (idInfo id)
351
352 setInlinePragma :: Id -> InlinePragInfo -> Id
353 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
354
355 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
356 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
357
358 idMustNotBeINLINEd id = case getInlinePragma id of
359                           IMustNotBeINLINEd -> True
360                           IAmALoopBreaker   -> True
361                           other             -> False
362
363 idMustBeINLINEd id =  case getInlinePragma id of
364                         IMustBeINLINEd -> True
365                         other          -> False
366 \end{code}
367
368
369         ---------------------------------
370         -- ONE-SHOT LAMBDAS
371 \begin{code}
372 isOneShotLambda :: Id -> Bool
373 isOneShotLambda id = case lbvarInfo (idInfo id) of
374                         IsOneShotLambda -> True
375                         NoLBVarInfo     -> idType id == realWorldStatePrimTy
376         -- The last clause is a gross hack.  It claims that 
377         -- every function over realWorldStatePrimTy is a one-shot
378         -- function.  This is pretty true in practice, and makes a big
379         -- difference.  For example, consider
380         --      a `thenST` \ r -> ...E...
381         -- The early full laziness pass, if it doesn't know that r is one-shot
382         -- will pull out E (let's say it doesn't mention r) to give
383         --      let lvl = E in a `thenST` \ r -> ...lvl...
384         -- When `thenST` gets inlined, we end up with
385         --      let lvl = E in \s -> case a s of (r, s') -> ...lvl...
386         -- and we don't re-inline E.
387         --      
388         -- It would be better to spot that r was one-shot to start with, but
389         -- I don't want to rely on that.
390
391 setOneShotLambda :: Id -> Id
392 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
393 \end{code}