fix some coercion kind representation things, extend exprIsConApp_maybe to non-vanilla
[ghc-hetmet.git] / compiler / basicTypes / Var.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section{@Vars@: Variables}
5
6 \begin{code}
7 module Var (
8         Var, 
9         varName, varUnique, 
10         setVarName, setVarUnique, 
11
12         -- TyVars
13         TyVar, mkTyVar, mkTcTyVar, mkWildTyVar,
14         tyVarName, tyVarKind,
15         setTyVarName, setTyVarUnique, setTyVarKind,
16         tcTyVarDetails,
17
18         -- CoVars
19         CoVar, coVarName, setCoVarUnique, setCoVarName, mkCoVar, isCoVar,
20
21         -- Ids
22         Id, DictId,
23         idName, idType, idUnique, idInfo, modifyIdInfo, maybeModifyIdInfo,
24         setIdName, setIdUnique, setIdType, setIdInfo, lazySetIdInfo, 
25         setIdExported, setIdNotExported, 
26
27         globalIdDetails, globaliseId, 
28
29         mkLocalId, mkExportedLocalId, mkGlobalId, 
30
31         isTyVar, isTcTyVar, isId, isLocalVar, isLocalId,
32         isGlobalId, isExportedId, 
33         mustHaveLocalBinding
34     ) where
35
36 #include "HsVersions.h"
37
38 import {-# SOURCE #-}   TypeRep( Type, Kind, isCoSuperKind )
39 import {-# SOURCE #-}   TcType( TcTyVarDetails, pprTcTyVarDetails )
40 import {-# SOURCE #-}   IdInfo( GlobalIdDetails, notGlobalId, IdInfo, seqIdInfo )
41
42 import Name             ( Name, NamedThing(..),
43                           setNameUnique, nameUnique, mkSysTvName
44                         )
45 import Unique           ( Unique, Uniquable(..), mkUniqueGrimily, getKey#,
46                           mkBuiltinUnique )
47 import FastTypes
48 import Outputable
49 \end{code}
50
51
52 %************************************************************************
53 %*                                                                      *
54 \subsection{The main data type declarations}
55 %*                                                                      *
56 %************************************************************************
57
58
59 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
60 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
61 strictness).  The essential info about different kinds of @Vars@ is
62 in its @VarDetails@.
63
64 \begin{code}
65 data Var
66   = TyVar {
67         varName    :: !Name,
68         realUnique :: FastInt,          -- Key for fast comparison
69                                         -- Identical to the Unique in the name,
70                                         -- cached here for speed
71         tyVarKind :: Kind }
72
73   | TcTyVar {                           -- Used only during type inference
74                                         -- Used for kind variables during 
75                                         -- inference, as well
76         varName        :: !Name,
77         realUnique     :: FastInt,
78         tyVarKind      :: Kind,
79         tcTyVarDetails :: TcTyVarDetails }
80
81   | GlobalId {                  -- Used for imported Ids, dict selectors etc
82                                 -- See Note [GlobalId/LocalId] below
83         varName    :: !Name,    -- Always an External or WiredIn Name
84         realUnique :: FastInt,
85         idType     :: Type,
86         idInfo     :: IdInfo,
87         gblDetails :: GlobalIdDetails }
88
89   | LocalId {                   -- Used for locally-defined Ids 
90                                 -- See Note [GlobalId/LocalId] below
91         varName    :: !Name,
92         realUnique :: FastInt,
93         idType     :: Type,
94         idInfo     :: IdInfo,
95         lclDetails :: LocalIdDetails }
96
97 data LocalIdDetails 
98   = NotExported -- Not exported
99   | Exported    -- Exported
100   -- Exported Ids are kept alive; 
101   -- NotExported things may be discarded as dead code.
102 \end{code}
103
104 Note [GlobalId/LocalId]
105 ~~~~~~~~~~~~~~~~~~~~~~~
106 A GlobalId is
107   * always a constant (top-level)
108   * imported, or data constructor, or primop, or record selector
109   * has a Unique that is globally unique across the whole
110     GHC invocation (a single invocation may compile multiple modules)
111   * never treated as a candidate by the free-variable finder;
112         it's a constant!
113
114 A LocalId is 
115   * bound within an expression (lambda, case, local let(rec))
116   * or defined at top level in the module being compiled
117   * always treated as a candidate by the free-variable finder
118
119 After CoreTidy, top-level LocalIds are turned into GlobalIds
120  
121
122 \begin{code}
123 instance Outputable Var where
124   ppr var = ppr (varName var) <+> ifPprDebug (brackets extra)
125         where
126           extra = case var of
127                         GlobalId {} -> ptext SLIT("gid")
128                         LocalId  {} -> ptext SLIT("lid")
129                         TyVar    {} -> ptext SLIT("tv")
130                         TcTyVar {tcTyVarDetails = details} -> pprTcTyVarDetails details
131
132 instance Show Var where
133   showsPrec p var = showsPrecSDoc p (ppr var)
134
135 instance NamedThing Var where
136   getName = varName
137
138 instance Uniquable Var where
139   getUnique = varUnique
140
141 instance Eq Var where
142     a == b = realUnique a ==# realUnique b
143
144 instance Ord Var where
145     a <= b = realUnique a <=# realUnique b
146     a <  b = realUnique a <#  realUnique b
147     a >= b = realUnique a >=# realUnique b
148     a >  b = realUnique a >#  realUnique b
149     a `compare` b = varUnique a `compare` varUnique b
150 \end{code}
151
152
153 \begin{code}
154 varUnique :: Var -> Unique
155 varUnique var = mkUniqueGrimily (iBox (realUnique var))
156
157 setVarUnique :: Var -> Unique -> Var
158 setVarUnique var uniq 
159   = var { realUnique = getKey# uniq, 
160           varName = setNameUnique (varName var) uniq }
161
162 setVarName :: Var -> Name -> Var
163 setVarName var new_name
164   = var { realUnique = getKey# (getUnique new_name), 
165           varName = new_name }
166 \end{code}
167
168
169 %************************************************************************
170 %*                                                                      *
171 \subsection{Type variables}
172 %*                                                                      *
173 %************************************************************************
174
175 \begin{code}
176 type TyVar = Var
177
178 tyVarName = varName
179
180 setTyVarUnique = setVarUnique
181 setTyVarName   = setVarName
182
183 setTyVarKind :: TyVar -> Kind -> TyVar
184 setTyVarKind tv k = tv {tyVarKind = k}
185 \end{code}
186
187 \begin{code}
188 mkTyVar :: Name -> Kind -> TyVar
189 mkTyVar name kind = TyVar { varName    = name
190                           , realUnique = getKey# (nameUnique name)
191                           , tyVarKind  = kind
192                         }
193
194 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
195 mkTcTyVar name kind details
196   = TcTyVar {   varName    = name,
197                 realUnique = getKey# (nameUnique name),
198                 tyVarKind  = kind,
199                 tcTyVarDetails = details
200         }
201
202 mkWildTyVar :: Kind -> TyVar
203 mkWildTyVar kind 
204   = TyVar { varName = mkSysTvName wild_uniq FSLIT("co_wild"),
205             realUnique = _ILIT(1),
206             tyVarKind = kind }
207   where
208     wild_uniq = (mkBuiltinUnique 1)
209 \end{code}
210
211 %************************************************************************
212 %*                                                                      *
213 \subsection{Coercion variables}
214 %*                                                                      *
215 %************************************************************************
216
217 \begin{code}
218 type CoVar = Var        -- A coercion variable is simply a type 
219                         -- variable of kind (ty1 :=: ty2)
220 coVarName = varName
221
222 setCoVarUnique = setVarUnique
223 setCoVarName   = setVarName
224
225 mkCoVar :: Name -> Kind -> CoVar
226 mkCoVar name kind = mkTyVar name kind
227
228 isCoVar :: TyVar -> Bool
229 isCoVar ty = isCoSuperKind (tyVarKind ty)
230 \end{code}
231
232 %************************************************************************
233 %*                                                                      *
234 \subsection{Id Construction}
235 %*                                                                      *
236 %************************************************************************
237
238 Most Id-related functions are in Id.lhs and MkId.lhs
239
240 \begin{code}
241 type Id     = Var
242 type DictId = Id
243 \end{code}
244
245 \begin{code}
246 idName    = varName
247 idUnique  = varUnique
248
249 setIdUnique :: Id -> Unique -> Id
250 setIdUnique = setVarUnique
251
252 setIdName :: Id -> Name -> Id
253 setIdName = setVarName
254
255 setIdType :: Id -> Type -> Id
256 setIdType id ty = id {idType = ty}
257
258 setIdExported :: Id -> Id
259 -- Can be called on GlobalIds, such as data cons and class ops,
260 -- which are "born" as GlobalIds and automatically exported
261 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
262 setIdExported other_id        = ASSERT( isId other_id ) other_id
263
264 setIdNotExported :: Id -> Id
265 -- We can only do this to LocalIds
266 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
267
268 globaliseId :: GlobalIdDetails -> Id -> Id
269 -- If it's a local, make it global
270 globaliseId details id = GlobalId { varName    = varName id,
271                                     realUnique = realUnique id,
272                                     idType     = idType id,
273                                     idInfo     = idInfo id,
274                                     gblDetails = details }
275
276 lazySetIdInfo :: Id -> IdInfo -> Id
277 lazySetIdInfo id info = id {idInfo = info}
278
279 setIdInfo :: Id -> IdInfo -> Id
280 setIdInfo id info = seqIdInfo info `seq` id {idInfo = info}
281         -- Try to avoid spack leaks by seq'ing
282
283 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
284 modifyIdInfo fn id
285   = seqIdInfo new_info `seq` id {idInfo = new_info}
286   where
287     new_info = fn (idInfo id)
288
289 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
290 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
291 maybeModifyIdInfo (Just new_info) id = id {idInfo = new_info}
292 maybeModifyIdInfo Nothing         id = id
293 \end{code}
294
295 %************************************************************************
296 %*                                                                      *
297 \subsection{Predicates over variables
298 %*                                                                      *
299 %************************************************************************
300
301 \begin{code}
302 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
303 mkGlobalId details name ty info 
304   = GlobalId {  varName    = name, 
305                 realUnique = getKey# (nameUnique name),         -- Cache the unique
306                 idType     = ty,        
307                 gblDetails = details,
308                 idInfo     = info }
309
310 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
311 mk_local_id name ty details info
312   = LocalId {   varName    = name, 
313                 realUnique = getKey# (nameUnique name),         -- Cache the unique
314                 idType     = ty,        
315                 lclDetails = details,
316                 idInfo     = info }
317
318 mkLocalId :: Name -> Type -> IdInfo -> Id
319 mkLocalId name ty info = mk_local_id name ty NotExported info
320
321 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
322 mkExportedLocalId name ty info = mk_local_id name ty Exported info
323 \end{code}
324
325 \begin{code}
326 isTyVar, isTcTyVar          :: Var -> Bool
327 isId, isLocalVar, isLocalId :: Var -> Bool
328 isGlobalId, isExportedId    :: Var -> Bool
329 mustHaveLocalBinding        :: Var -> Bool
330
331 isTyVar (TyVar {})   = True
332 isTyVar (TcTyVar {}) = True
333 isTyVar other        = False
334
335 isTcTyVar (TcTyVar {}) = True
336 isTcTyVar other        = False
337
338 isId (LocalId {})  = True
339 isId (GlobalId {}) = True
340 isId other         = False
341
342 isLocalId (LocalId {}) = True
343 isLocalId other        = False
344
345 -- isLocalVar returns True for type variables as well as local Ids
346 -- These are the variables that we need to pay attention to when finding free
347 -- variables, or doing dependency analysis.
348 isLocalVar (GlobalId {}) = False 
349 isLocalVar other         = True
350
351 -- mustHaveLocalBinding returns True of Ids and TyVars
352 -- that must have a binding in this module.  The converse
353 -- is not quite right: there are some GlobalIds that must have
354 -- bindings, such as record selectors.  But that doesn't matter,
355 -- because it's only used for assertions
356 mustHaveLocalBinding var = isLocalVar var
357
358 isGlobalId (GlobalId {}) = True
359 isGlobalId other         = False
360
361 -- isExportedId means "don't throw this away"
362 isExportedId (GlobalId {}) = True
363 isExportedId (LocalId {lclDetails = details}) 
364   = case details of
365         Exported   -> True
366         other      -> False
367 isExportedId other = False
368 \end{code}
369
370 \begin{code}
371 globalIdDetails :: Var -> GlobalIdDetails
372 -- Works OK on local Ids too, returning notGlobalId
373 globalIdDetails (GlobalId {gblDetails = details}) = details
374 globalIdDetails other                             = notGlobalId
375 \end{code}
376