Add selectors for common fields (DataCon/PatSyn) to ConLike
[ghc.git] / compiler / basicTypes / Var.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5 \section{@Vars@: Variables}
6 -}
7
8 {-# LANGUAGE CPP, DeriveDataTypeable #-}
9 -- |
10 -- #name_types#
11 -- GHC uses several kinds of name internally:
12 --
13 -- * 'OccName.OccName': see "OccName#name_types"
14 --
15 -- * 'RdrName.RdrName': see "RdrName#name_types"
16 --
17 -- * 'Name.Name': see "Name#name_types"
18 --
19 -- * 'Id.Id': see "Id#name_types"
20 --
21 -- * 'Var.Var' is a synonym for the 'Id.Id' type but it may additionally
22 -- potentially contain type variables, which have a 'TypeRep.Kind'
23 -- rather than a 'TypeRep.Type' and only contain some extra
24 -- details during typechecking.
25 --
26 -- These 'Var.Var' names may either be global or local, see "Var#globalvslocal"
27 --
28 -- #globalvslocal#
29 -- Global 'Id's and 'Var's are those that are imported or correspond
30 -- to a data constructor, primitive operation, or record selectors.
31 -- Local 'Id's and 'Var's are those bound within an expression
32 -- (e.g. by a lambda) or at the top level of the module being compiled.
33
34 module Var (
35 -- * The main data type and synonyms
36 Var, CoVar, Id, DictId, DFunId, EvVar, EqVar, EvId, IpId,
37 TyVar, TypeVar, KindVar, TKVar,
38
39 -- ** Taking 'Var's apart
40 varName, varUnique, varType,
41
42 -- ** Modifying 'Var's
43 setVarName, setVarUnique, setVarType,
44
45 -- ** Constructing, taking apart, modifying 'Id's
46 mkGlobalVar, mkLocalVar, mkExportedLocalVar, mkCoVar,
47 idInfo, idDetails,
48 lazySetIdInfo, setIdDetails, globaliseId,
49 setIdExported, setIdNotExported,
50
51 -- ** Predicates
52 isId, isTKVar, isTyVar, isTcTyVar,
53 isLocalVar, isLocalId,
54 isGlobalId, isExportedId,
55 mustHaveLocalBinding,
56
57 -- ** Constructing 'TyVar's
58 mkTyVar, mkTcTyVar, mkKindVar,
59
60 -- ** Taking 'TyVar's apart
61 tyVarName, tyVarKind, tcTyVarDetails, setTcTyVarDetails,
62
63 -- ** Modifying 'TyVar's
64 setTyVarName, setTyVarUnique, setTyVarKind, updateTyVarKind,
65 updateTyVarKindM
66
67 ) where
68
69 #include "HsVersions.h"
70
71 import {-# SOURCE #-} TypeRep( Type, Kind, SuperKind )
72 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails )
73 import {-# SOURCE #-} IdInfo( IdDetails, IdInfo, coVarDetails, vanillaIdInfo, pprIdDetails )
74
75 import Name hiding (varName)
76 import Unique
77 import Util
78 import FastTypes
79 import FastString
80 import Outputable
81
82 import Data.Data
83
84 {-
85 ************************************************************************
86 * *
87 Synonyms
88 * *
89 ************************************************************************
90 -- These synonyms are here and not in Id because otherwise we need a very
91 -- large number of SOURCE imports of Id.hs :-(
92 -}
93
94 type Id = Var -- A term-level identifier
95
96 type TyVar = Var -- Type *or* kind variable (historical)
97
98 type TKVar = Var -- Type *or* kind variable (historical)
99 type TypeVar = Var -- Definitely a type variable
100 type KindVar = Var -- Definitely a kind variable
101 -- See Note [Kind and type variables]
102
103 -- See Note [Evidence: EvIds and CoVars]
104 type EvId = Id -- Term-level evidence: DictId, IpId, or EqVar
105 type EvVar = EvId -- ...historical name for EvId
106 type DFunId = Id -- A dictionary function
107 type DictId = EvId -- A dictionary variable
108 type IpId = EvId -- A term-level implicit parameter
109 type EqVar = EvId -- Boxed equality evidence
110
111 type CoVar = Id -- See Note [Evidence: EvIds and CoVars]
112
113 {-
114 Note [Evidence: EvIds and CoVars]
115 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116 * An EvId (evidence Id) is a *boxed*, term-level evidence variable
117 (dictionary, implicit parameter, or equality).
118
119 * A CoVar (coercion variable) is an *unboxed* term-level evidence variable
120 of type (t1 ~# t2). So it's the unboxed version of an EqVar.
121
122 * Only CoVars can occur in Coercions, EqVars appear in TcCoercions.
123
124 * DictId, IpId, and EqVar are synonyms when we know what kind of
125 evidence we are talking about. For example, an EqVar has type (t1 ~ t2).
126
127 Note [Kind and type variables]
128 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129 Before kind polymorphism, TyVar were used to mean type variables. Now
130 they are use to mean kind *or* type variables. KindVar is used when we
131 know for sure that it is a kind variable. In future, we might want to
132 go over the whole compiler code to use:
133 - TKVar to mean kind or type variables
134 - TypeVar to mean type variables only
135 - KindVar to mean kind variables
136
137
138 ************************************************************************
139 * *
140 \subsection{The main data type declarations}
141 * *
142 ************************************************************************
143
144
145 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
146 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
147 strictness). The essential info about different kinds of @Vars@ is
148 in its @VarDetails@.
149 -}
150
151 -- | Essentially a typed 'Name', that may also contain some additional information
152 -- about the 'Var' and it's use sites.
153 data Var
154 = TyVar { -- Type and kind variables
155 -- see Note [Kind and type variables]
156 varName :: !Name,
157 realUnique :: FastInt, -- Key for fast comparison
158 -- Identical to the Unique in the name,
159 -- cached here for speed
160 varType :: Kind -- ^ The type or kind of the 'Var' in question
161 }
162
163 | TcTyVar { -- Used only during type inference
164 -- Used for kind variables during
165 -- inference, as well
166 varName :: !Name,
167 realUnique :: FastInt,
168 varType :: Kind,
169 tc_tv_details :: TcTyVarDetails }
170
171 | Id {
172 varName :: !Name,
173 realUnique :: FastInt,
174 varType :: Type,
175 idScope :: IdScope,
176 id_details :: IdDetails, -- Stable, doesn't change
177 id_info :: IdInfo } -- Unstable, updated by simplifier
178 deriving Typeable
179
180 data IdScope -- See Note [GlobalId/LocalId]
181 = GlobalId
182 | LocalId ExportFlag
183
184 data ExportFlag
185 = NotExported -- ^ Not exported: may be discarded as dead code.
186 | Exported -- ^ Exported: kept alive
187
188 {-
189 Note [GlobalId/LocalId]
190 ~~~~~~~~~~~~~~~~~~~~~~~
191 A GlobalId is
192 * always a constant (top-level)
193 * imported, or data constructor, or primop, or record selector
194 * has a Unique that is globally unique across the whole
195 GHC invocation (a single invocation may compile multiple modules)
196 * never treated as a candidate by the free-variable finder;
197 it's a constant!
198
199 A LocalId is
200 * bound within an expression (lambda, case, local let(rec))
201 * or defined at top level in the module being compiled
202 * always treated as a candidate by the free-variable finder
203
204 After CoreTidy, top-level LocalIds are turned into GlobalIds
205 -}
206
207 instance Outputable Var where
208 ppr var = ppr (varName var) <> getPprStyle (ppr_debug var)
209
210 ppr_debug :: Var -> PprStyle -> SDoc
211 ppr_debug (TyVar {}) sty
212 | debugStyle sty = brackets (ptext (sLit "tv"))
213 ppr_debug (TcTyVar {tc_tv_details = d}) sty
214 | dumpStyle sty || debugStyle sty = brackets (pprTcTyVarDetails d)
215 ppr_debug (Id { idScope = s, id_details = d }) sty
216 | debugStyle sty = brackets (ppr_id_scope s <> pprIdDetails d)
217 ppr_debug _ _ = empty
218
219 ppr_id_scope :: IdScope -> SDoc
220 ppr_id_scope GlobalId = ptext (sLit "gid")
221 ppr_id_scope (LocalId Exported) = ptext (sLit "lidx")
222 ppr_id_scope (LocalId NotExported) = ptext (sLit "lid")
223
224 instance NamedThing Var where
225 getName = varName
226
227 instance Uniquable Var where
228 getUnique = varUnique
229
230 instance Eq Var where
231 a == b = realUnique a ==# realUnique b
232
233 instance Ord Var where
234 a <= b = realUnique a <=# realUnique b
235 a < b = realUnique a <# realUnique b
236 a >= b = realUnique a >=# realUnique b
237 a > b = realUnique a ># realUnique b
238 a `compare` b = varUnique a `compare` varUnique b
239
240 instance Data Var where
241 -- don't traverse?
242 toConstr _ = abstractConstr "Var"
243 gunfold _ _ = error "gunfold"
244 dataTypeOf _ = mkNoRepType "Var"
245
246 varUnique :: Var -> Unique
247 varUnique var = mkUniqueGrimily (iBox (realUnique var))
248
249 setVarUnique :: Var -> Unique -> Var
250 setVarUnique var uniq
251 = var { realUnique = getKeyFastInt uniq,
252 varName = setNameUnique (varName var) uniq }
253
254 setVarName :: Var -> Name -> Var
255 setVarName var new_name
256 = var { realUnique = getKeyFastInt (getUnique new_name),
257 varName = new_name }
258
259 setVarType :: Id -> Type -> Id
260 setVarType id ty = id { varType = ty }
261
262 {-
263 ************************************************************************
264 * *
265 \subsection{Type and kind variables}
266 * *
267 ************************************************************************
268 -}
269
270 tyVarName :: TyVar -> Name
271 tyVarName = varName
272
273 tyVarKind :: TyVar -> Kind
274 tyVarKind = varType
275
276 setTyVarUnique :: TyVar -> Unique -> TyVar
277 setTyVarUnique = setVarUnique
278
279 setTyVarName :: TyVar -> Name -> TyVar
280 setTyVarName = setVarName
281
282 setTyVarKind :: TyVar -> Kind -> TyVar
283 setTyVarKind tv k = tv {varType = k}
284
285 updateTyVarKind :: (Kind -> Kind) -> TyVar -> TyVar
286 updateTyVarKind update tv = tv {varType = update (tyVarKind tv)}
287
288 updateTyVarKindM :: (Monad m) => (Kind -> m Kind) -> TyVar -> m TyVar
289 updateTyVarKindM update tv
290 = do { k' <- update (tyVarKind tv)
291 ; return $ tv {varType = k'} }
292
293 mkTyVar :: Name -> Kind -> TyVar
294 mkTyVar name kind = TyVar { varName = name
295 , realUnique = getKeyFastInt (nameUnique name)
296 , varType = kind
297 }
298
299 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
300 mkTcTyVar name kind details
301 = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
302 TcTyVar { varName = name,
303 realUnique = getKeyFastInt (nameUnique name),
304 varType = kind,
305 tc_tv_details = details
306 }
307
308 tcTyVarDetails :: TyVar -> TcTyVarDetails
309 tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
310 tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var)
311
312 setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
313 setTcTyVarDetails tv details = tv { tc_tv_details = details }
314
315 mkKindVar :: Name -> SuperKind -> KindVar
316 -- mkKindVar take a SuperKind as argument because we don't have access
317 -- to superKind here.
318 mkKindVar name kind = TyVar
319 { varName = name
320 , realUnique = getKeyFastInt (nameUnique name)
321 , varType = kind }
322
323 {-
324 ************************************************************************
325 * *
326 \subsection{Ids}
327 * *
328 ************************************************************************
329 -}
330
331 idInfo :: Id -> IdInfo
332 idInfo (Id { id_info = info }) = info
333 idInfo other = pprPanic "idInfo" (ppr other)
334
335 idDetails :: Id -> IdDetails
336 idDetails (Id { id_details = details }) = details
337 idDetails other = pprPanic "idDetails" (ppr other)
338
339 -- The next three have a 'Var' suffix even though they always build
340 -- Ids, because Id.hs uses 'mkGlobalId' etc with different types
341 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
342 mkGlobalVar details name ty info
343 = mk_id name ty GlobalId details info
344
345 mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
346 mkLocalVar details name ty info
347 = mk_id name ty (LocalId NotExported) details info
348
349 mkCoVar :: Name -> Type -> CoVar
350 -- Coercion variables have no IdInfo
351 mkCoVar name ty = mk_id name ty (LocalId NotExported) coVarDetails vanillaIdInfo
352
353 -- | Exported 'Var's will not be removed as dead code
354 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
355 mkExportedLocalVar details name ty info
356 = mk_id name ty (LocalId Exported) details info
357
358 mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
359 mk_id name ty scope details info
360 = Id { varName = name,
361 realUnique = getKeyFastInt (nameUnique name),
362 varType = ty,
363 idScope = scope,
364 id_details = details,
365 id_info = info }
366
367 -------------------
368 lazySetIdInfo :: Id -> IdInfo -> Var
369 lazySetIdInfo id info = id { id_info = info }
370
371 setIdDetails :: Id -> IdDetails -> Id
372 setIdDetails id details = id { id_details = details }
373
374 globaliseId :: Id -> Id
375 -- ^ If it's a local, make it global
376 globaliseId id = id { idScope = GlobalId }
377
378 setIdExported :: Id -> Id
379 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
380 -- and class operations, which are born as global 'Id's and automatically exported
381 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
382 setIdExported id@(Id { idScope = GlobalId }) = id
383 setIdExported tv = pprPanic "setIdExported" (ppr tv)
384
385 setIdNotExported :: Id -> Id
386 -- ^ We can only do this to LocalIds
387 setIdNotExported id = ASSERT( isLocalId id )
388 id { idScope = LocalId NotExported }
389
390 {-
391 ************************************************************************
392 * *
393 \subsection{Predicates over variables}
394 * *
395 ************************************************************************
396 -}
397
398 isTyVar :: Var -> Bool
399 isTyVar = isTKVar -- Historical
400
401 isTKVar :: Var -> Bool -- True of both type and kind variables
402 isTKVar (TyVar {}) = True
403 isTKVar (TcTyVar {}) = True
404 isTKVar _ = False
405
406 isTcTyVar :: Var -> Bool
407 isTcTyVar (TcTyVar {}) = True
408 isTcTyVar _ = False
409
410 isId :: Var -> Bool
411 isId (Id {}) = True
412 isId _ = False
413
414 isLocalId :: Var -> Bool
415 isLocalId (Id { idScope = LocalId _ }) = True
416 isLocalId _ = False
417
418 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
419 -- These are the variables that we need to pay attention to when finding free
420 -- variables, or doing dependency analysis.
421 isLocalVar :: Var -> Bool
422 isLocalVar v = not (isGlobalId v)
423
424 isGlobalId :: Var -> Bool
425 isGlobalId (Id { idScope = GlobalId }) = True
426 isGlobalId _ = False
427
428 -- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
429 -- that must have a binding in this module. The converse
430 -- is not quite right: there are some global 'Id's that must have
431 -- bindings, such as record selectors. But that doesn't matter,
432 -- because it's only used for assertions
433 mustHaveLocalBinding :: Var -> Bool
434 mustHaveLocalBinding var = isLocalVar var
435
436 -- | 'isExportedIdVar' means \"don't throw this away\"
437 isExportedId :: Var -> Bool
438 isExportedId (Id { idScope = GlobalId }) = True
439 isExportedId (Id { idScope = LocalId Exported}) = True
440 isExportedId _ = False