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