testsuite: Assert that testsuite ways are known
[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, FlexibleContexts, MultiWayIf, FlexibleInstances, DeriveDataTypeable #-}
9
10 -- |
11 -- #name_types#
12 -- GHC uses several kinds of name internally:
13 --
14 -- * 'OccName.OccName': see "OccName#name_types"
15 --
16 -- * 'RdrName.RdrName': see "RdrName#name_types"
17 --
18 -- * 'Name.Name': see "Name#name_types"
19 --
20 -- * 'Id.Id': see "Id#name_types"
21 --
22 -- * 'Var.Var' is a synonym for the 'Id.Id' type but it may additionally
23 -- potentially contain type variables, which have a 'TyCoRep.Kind'
24 -- rather than a 'TyCoRep.Type' and only contain some extra
25 -- details during typechecking.
26 --
27 -- These 'Var.Var' names may either be global or local, see "Var#globalvslocal"
28 --
29 -- #globalvslocal#
30 -- Global 'Id's and 'Var's are those that are imported or correspond
31 -- to a data constructor, primitive operation, or record selectors.
32 -- Local 'Id's and 'Var's are those bound within an expression
33 -- (e.g. by a lambda) or at the top level of the module being compiled.
34
35 module Var (
36 -- * The main data type and synonyms
37 Var, CoVar, Id, NcId, DictId, DFunId, EvVar, EqVar, EvId, IpId, JoinId,
38 TyVar, TcTyVar, TypeVar, KindVar, TKVar, TyCoVar,
39
40 -- * In and Out variants
41 InVar, InCoVar, InId, InTyVar,
42 OutVar, OutCoVar, OutId, OutTyVar,
43
44 -- ** Taking 'Var's apart
45 varName, varUnique, varType,
46
47 -- ** Modifying 'Var's
48 setVarName, setVarUnique, setVarType, updateVarType,
49 updateVarTypeM,
50
51 -- ** Constructing, taking apart, modifying 'Id's
52 mkGlobalVar, mkLocalVar, mkExportedLocalVar, mkCoVar,
53 idInfo, idDetails,
54 lazySetIdInfo, setIdDetails, globaliseId,
55 setIdExported, setIdNotExported,
56
57 -- ** Predicates
58 isId, isTyVar, isTcTyVar,
59 isLocalVar, isLocalId, isCoVar, isNonCoVarId, isTyCoVar,
60 isGlobalId, isExportedId,
61 mustHaveLocalBinding,
62
63 -- * ArgFlags
64 ArgFlag(..), isVisibleArgFlag, isInvisibleArgFlag, sameVis,
65 AnonArgFlag(..), ForallVisFlag(..), argToForallVisFlag,
66
67 -- * TyVar's
68 VarBndr(..), TyCoVarBinder, TyVarBinder,
69 binderVar, binderVars, binderArgFlag, binderType,
70 mkTyCoVarBinder, mkTyCoVarBinders,
71 mkTyVarBinder, mkTyVarBinders,
72 isTyVarBinder,
73
74 -- ** Constructing TyVar's
75 mkTyVar, mkTcTyVar,
76
77 -- ** Taking 'TyVar's apart
78 tyVarName, tyVarKind, tcTyVarDetails, setTcTyVarDetails,
79
80 -- ** Modifying 'TyVar's
81 setTyVarName, setTyVarUnique, setTyVarKind, updateTyVarKind,
82 updateTyVarKindM,
83
84 nonDetCmpVar
85
86 ) where
87
88 #include "HsVersions.h"
89
90 import GhcPrelude
91
92 import {-# SOURCE #-} TyCoRep( Type, Kind )
93 import {-# SOURCE #-} TyCoPpr( pprKind )
94 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails, vanillaSkolemTv )
95 import {-# SOURCE #-} IdInfo( IdDetails, IdInfo, coVarDetails, isCoVarDetails,
96 vanillaIdInfo, pprIdDetails )
97
98 import Name hiding (varName)
99 import Unique ( Uniquable, Unique, getKey, getUnique
100 , mkUniqueGrimily, nonDetCmpUnique )
101 import Util
102 import Binary
103 import DynFlags
104 import Outputable
105
106 import Data.Data
107
108 {-
109 ************************************************************************
110 * *
111 Synonyms
112 * *
113 ************************************************************************
114 -- These synonyms are here and not in Id because otherwise we need a very
115 -- large number of SOURCE imports of Id.hs :-(
116 -}
117
118 -- | Identifier
119 type Id = Var -- A term-level identifier
120 -- predicate: isId
121
122 -- | Coercion Variable
123 type CoVar = Id -- See Note [Evidence: EvIds and CoVars]
124 -- predicate: isCoVar
125
126 -- |
127 type NcId = Id -- A term-level (value) variable that is
128 -- /not/ an (unlifted) coercion
129 -- predicate: isNonCoVarId
130
131 -- | Type or kind Variable
132 type TyVar = Var -- Type *or* kind variable (historical)
133
134 -- | Type or Kind Variable
135 type TKVar = Var -- Type *or* kind variable (historical)
136
137 -- | Type variable that might be a metavariable
138 type TcTyVar = Var
139
140 -- | Type Variable
141 type TypeVar = Var -- Definitely a type variable
142
143 -- | Kind Variable
144 type KindVar = Var -- Definitely a kind variable
145 -- See Note [Kind and type variables]
146
147 -- See Note [Evidence: EvIds and CoVars]
148 -- | Evidence Identifier
149 type EvId = Id -- Term-level evidence: DictId, IpId, or EqVar
150
151 -- | Evidence Variable
152 type EvVar = EvId -- ...historical name for EvId
153
154 -- | Dictionary Function Identifier
155 type DFunId = Id -- A dictionary function
156
157 -- | Dictionary Identifier
158 type DictId = EvId -- A dictionary variable
159
160 -- | Implicit parameter Identifier
161 type IpId = EvId -- A term-level implicit parameter
162
163 -- | Equality Variable
164 type EqVar = EvId -- Boxed equality evidence
165 type JoinId = Id -- A join variable
166
167 -- | Type or Coercion Variable
168 type TyCoVar = Id -- Type, *or* coercion variable
169 -- predicate: isTyCoVar
170
171
172 {- Many passes apply a substitution, and it's very handy to have type
173 synonyms to remind us whether or not the substitution has been applied -}
174
175 type InVar = Var
176 type InTyVar = TyVar
177 type InCoVar = CoVar
178 type InId = Id
179 type OutVar = Var
180 type OutTyVar = TyVar
181 type OutCoVar = CoVar
182 type OutId = Id
183
184
185
186 {- Note [Evidence: EvIds and CoVars]
187 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188 * An EvId (evidence Id) is a term-level evidence variable
189 (dictionary, implicit parameter, or equality). Could be boxed or unboxed.
190
191 * DictId, IpId, and EqVar are synonyms when we know what kind of
192 evidence we are talking about. For example, an EqVar has type (t1 ~ t2).
193
194 * A CoVar is always an un-lifted coercion, of type (t1 ~# t2) or (t1 ~R# t2)
195
196 Note [Kind and type variables]
197 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198 Before kind polymorphism, TyVar were used to mean type variables. Now
199 they are used to mean kind *or* type variables. KindVar is used when we
200 know for sure that it is a kind variable. In future, we might want to
201 go over the whole compiler code to use:
202 - TKVar to mean kind or type variables
203 - TypeVar to mean type variables only
204 - KindVar to mean kind variables
205
206
207 ************************************************************************
208 * *
209 \subsection{The main data type declarations}
210 * *
211 ************************************************************************
212
213
214 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
215 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
216 strictness). The essential info about different kinds of @Vars@ is
217 in its @VarDetails@.
218 -}
219
220 -- | Variable
221 --
222 -- Essentially a typed 'Name', that may also contain some additional information
223 -- about the 'Var' and its use sites.
224 data Var
225 = TyVar { -- Type and kind variables
226 -- see Note [Kind and type variables]
227 varName :: !Name,
228 realUnique :: {-# UNPACK #-} !Int,
229 -- ^ Key for fast comparison
230 -- Identical to the Unique in the name,
231 -- cached here for speed
232 varType :: Kind -- ^ The type or kind of the 'Var' in question
233 }
234
235 | TcTyVar { -- Used only during type inference
236 -- Used for kind variables during
237 -- inference, as well
238 varName :: !Name,
239 realUnique :: {-# UNPACK #-} !Int,
240 varType :: Kind,
241 tc_tv_details :: TcTyVarDetails
242 }
243
244 | Id {
245 varName :: !Name,
246 realUnique :: {-# UNPACK #-} !Int,
247 varType :: Type,
248 idScope :: IdScope,
249 id_details :: IdDetails, -- Stable, doesn't change
250 id_info :: IdInfo } -- Unstable, updated by simplifier
251
252 -- | Identifier Scope
253 data IdScope -- See Note [GlobalId/LocalId]
254 = GlobalId
255 | LocalId ExportFlag
256
257 data ExportFlag -- See Note [ExportFlag on binders]
258 = NotExported -- ^ Not exported: may be discarded as dead code.
259 | Exported -- ^ Exported: kept alive
260
261 {- Note [ExportFlag on binders]
262 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
263 An ExportFlag of "Exported" on a top-level binder says "keep this
264 binding alive; do not drop it as dead code". This transitively
265 keeps alive all the other top-level bindings that this binding refers
266 to. This property is persisted all the way down the pipeline, so that
267 the binding will be compiled all the way to object code, and its
268 symbols will appear in the linker symbol table.
269
270 However, note that this use of "exported" is quite different to the
271 export list on a Haskell module. Setting the ExportFlag on an Id does
272 /not/ mean that if you import the module (in Haskell source code) you
273 will see this Id. Of course, things that appear in the export list
274 of the source Haskell module do indeed have their ExportFlag set.
275 But many other things, such as dictionary functions, are kept alive
276 by having their ExportFlag set, even though they are not exported
277 in the source-code sense.
278
279 We should probably use a different term for ExportFlag, like
280 KeepAlive.
281
282 Note [GlobalId/LocalId]
283 ~~~~~~~~~~~~~~~~~~~~~~~
284 A GlobalId is
285 * always a constant (top-level)
286 * imported, or data constructor, or primop, or record selector
287 * has a Unique that is globally unique across the whole
288 GHC invocation (a single invocation may compile multiple modules)
289 * never treated as a candidate by the free-variable finder;
290 it's a constant!
291
292 A LocalId is
293 * bound within an expression (lambda, case, local let(rec))
294 * or defined at top level in the module being compiled
295 * always treated as a candidate by the free-variable finder
296
297 After CoreTidy, top-level LocalIds are turned into GlobalIds
298 -}
299
300 instance Outputable Var where
301 ppr var = sdocWithDynFlags $ \dflags ->
302 getPprStyle $ \ppr_style ->
303 if | debugStyle ppr_style && (not (gopt Opt_SuppressVarKinds dflags))
304 -> parens (ppr (varName var) <+> ppr_debug var ppr_style <+>
305 dcolon <+> pprKind (tyVarKind var))
306 | otherwise
307 -> ppr (varName var) <> ppr_debug var ppr_style
308
309 ppr_debug :: Var -> PprStyle -> SDoc
310 ppr_debug (TyVar {}) sty
311 | debugStyle sty = brackets (text "tv")
312 ppr_debug (TcTyVar {tc_tv_details = d}) sty
313 | dumpStyle sty || debugStyle sty = brackets (pprTcTyVarDetails d)
314 ppr_debug (Id { idScope = s, id_details = d }) sty
315 | debugStyle sty = brackets (ppr_id_scope s <> pprIdDetails d)
316 ppr_debug _ _ = empty
317
318 ppr_id_scope :: IdScope -> SDoc
319 ppr_id_scope GlobalId = text "gid"
320 ppr_id_scope (LocalId Exported) = text "lidx"
321 ppr_id_scope (LocalId NotExported) = text "lid"
322
323 instance NamedThing Var where
324 getName = varName
325
326 instance Uniquable Var where
327 getUnique = varUnique
328
329 instance Eq Var where
330 a == b = realUnique a == realUnique b
331
332 instance Ord Var where
333 a <= b = realUnique a <= realUnique b
334 a < b = realUnique a < realUnique b
335 a >= b = realUnique a >= realUnique b
336 a > b = realUnique a > realUnique b
337 a `compare` b = a `nonDetCmpVar` b
338
339 -- | Compare Vars by their Uniques.
340 -- This is what Ord Var does, provided here to make it explicit at the
341 -- call-site that it can introduce non-determinism.
342 -- See Note [Unique Determinism]
343 nonDetCmpVar :: Var -> Var -> Ordering
344 nonDetCmpVar a b = varUnique a `nonDetCmpUnique` varUnique b
345
346 instance Data Var where
347 -- don't traverse?
348 toConstr _ = abstractConstr "Var"
349 gunfold _ _ = error "gunfold"
350 dataTypeOf _ = mkNoRepType "Var"
351
352 instance HasOccName Var where
353 occName = nameOccName . varName
354
355 varUnique :: Var -> Unique
356 varUnique var = mkUniqueGrimily (realUnique var)
357
358 setVarUnique :: Var -> Unique -> Var
359 setVarUnique var uniq
360 = var { realUnique = getKey uniq,
361 varName = setNameUnique (varName var) uniq }
362
363 setVarName :: Var -> Name -> Var
364 setVarName var new_name
365 = var { realUnique = getKey (getUnique new_name),
366 varName = new_name }
367
368 setVarType :: Id -> Type -> Id
369 setVarType id ty = id { varType = ty }
370
371 updateVarType :: (Type -> Type) -> Id -> Id
372 updateVarType f id = id { varType = f (varType id) }
373
374 updateVarTypeM :: Monad m => (Type -> m Type) -> Id -> m Id
375 updateVarTypeM f id = do { ty' <- f (varType id)
376 ; return (id { varType = ty' }) }
377
378 {- *********************************************************************
379 * *
380 * ArgFlag
381 * *
382 ********************************************************************* -}
383
384 -- | Argument Flag
385 --
386 -- Is something required to appear in source Haskell ('Required'),
387 -- permitted by request ('Specified') (visible type application), or
388 -- prohibited entirely from appearing in source Haskell ('Inferred')?
389 -- See Note [VarBndrs, TyCoVarBinders, TyConBinders, and visibility] in TyCoRep
390 data ArgFlag = Inferred | Specified | Required
391 deriving (Eq, Ord, Data)
392 -- (<) on ArgFlag means "is less visible than"
393
394 -- | Does this 'ArgFlag' classify an argument that is written in Haskell?
395 isVisibleArgFlag :: ArgFlag -> Bool
396 isVisibleArgFlag Required = True
397 isVisibleArgFlag _ = False
398
399 -- | Does this 'ArgFlag' classify an argument that is not written in Haskell?
400 isInvisibleArgFlag :: ArgFlag -> Bool
401 isInvisibleArgFlag = not . isVisibleArgFlag
402
403 -- | Do these denote the same level of visibility? 'Required'
404 -- arguments are visible, others are not. So this function
405 -- equates 'Specified' and 'Inferred'. Used for printing.
406 sameVis :: ArgFlag -> ArgFlag -> Bool
407 sameVis Required Required = True
408 sameVis Required _ = False
409 sameVis _ Required = False
410 sameVis _ _ = True
411
412 instance Outputable ArgFlag where
413 ppr Required = text "[req]"
414 ppr Specified = text "[spec]"
415 ppr Inferred = text "[infrd]"
416
417 instance Binary ArgFlag where
418 put_ bh Required = putByte bh 0
419 put_ bh Specified = putByte bh 1
420 put_ bh Inferred = putByte bh 2
421
422 get bh = do
423 h <- getByte bh
424 case h of
425 0 -> return Required
426 1 -> return Specified
427 _ -> return Inferred
428
429 -- | The non-dependent version of 'ArgFlag'.
430
431 -- Appears here partly so that it's together with its friend ArgFlag,
432 -- but also because it is used in IfaceType, rather early in the
433 -- compilation chain
434 -- See Note [AnonArgFlag vs. ForallVisFlag]
435 data AnonArgFlag
436 = VisArg -- ^ Used for @(->)@: an ordinary non-dependent arrow.
437 -- The argument is visible in source code.
438 | InvisArg -- ^ Used for @(=>)@: a non-dependent predicate arrow.
439 -- The argument is invisible in source code.
440 deriving (Eq, Ord, Data)
441
442 instance Outputable AnonArgFlag where
443 ppr VisArg = text "[vis]"
444 ppr InvisArg = text "[invis]"
445
446 instance Binary AnonArgFlag where
447 put_ bh VisArg = putByte bh 0
448 put_ bh InvisArg = putByte bh 1
449
450 get bh = do
451 h <- getByte bh
452 case h of
453 0 -> return VisArg
454 _ -> return InvisArg
455
456 -- | Is a @forall@ invisible (e.g., @forall a b. {...}@, with a dot) or visible
457 -- (e.g., @forall a b -> {...}@, with an arrow)?
458
459 -- See Note [AnonArgFlag vs. ForallVisFlag]
460 data ForallVisFlag
461 = ForallVis -- ^ A visible @forall@ (with an arrow)
462 | ForallInvis -- ^ An invisible @forall@ (with a dot)
463 deriving (Eq, Ord, Data)
464
465 instance Outputable ForallVisFlag where
466 ppr f = text $ case f of
467 ForallVis -> "ForallVis"
468 ForallInvis -> "ForallInvis"
469
470 -- | Convert an 'ArgFlag' to its corresponding 'ForallVisFlag'.
471 argToForallVisFlag :: ArgFlag -> ForallVisFlag
472 argToForallVisFlag Required = ForallVis
473 argToForallVisFlag Specified = ForallInvis
474 argToForallVisFlag Inferred = ForallInvis
475
476 {-
477 Note [AnonArgFlag vs. ForallVisFlag]
478 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
479 The AnonArgFlag and ForallVisFlag data types are quite similar at a first
480 glance:
481
482 data AnonArgFlag = VisArg | InvisArg
483 data ForallVisFlag = ForallVis | ForallInvis
484
485 Both data types keep track of visibility of some sort. AnonArgFlag tracks
486 whether a FunTy has a visible argument (->) or an invisible predicate argument
487 (=>). ForallVisFlag tracks whether a `forall` quantifier is visible
488 (forall a -> {...}) or invisible (forall a. {...}).
489
490 Given their similarities, it's tempting to want to combine these two data types
491 into one, but they actually represent distinct concepts. AnonArgFlag reflects a
492 property of *Core* types, whereas ForallVisFlag reflects a property of the GHC
493 AST. In other words, AnonArgFlag is all about internals, whereas ForallVisFlag
494 is all about surface syntax. Therefore, they are kept as separate data types.
495 -}
496
497 {- *********************************************************************
498 * *
499 * VarBndr, TyCoVarBinder
500 * *
501 ********************************************************************* -}
502
503 -- Variable Binder
504 --
505 -- VarBndr is polymorphic in both var and visibility fields.
506 -- Currently there are six different uses of 'VarBndr':
507 -- * Var.TyVarBinder = VarBndr TyVar ArgFlag
508 -- * Var.TyCoVarBinder = VarBndr TyCoVar ArgFlag
509 -- * TyCon.TyConBinder = VarBndr TyVar TyConBndrVis
510 -- * TyCon.TyConTyCoBinder = VarBndr TyCoVar TyConBndrVis
511 -- * IfaceType.IfaceForAllBndr = VarBndr IfaceBndr ArgFlag
512 -- * IfaceType.IfaceTyConBinder = VarBndr IfaceBndr TyConBndrVis
513 data VarBndr var argf = Bndr var argf
514 deriving( Data )
515
516 -- | Variable Binder
517 --
518 -- A 'TyCoVarBinder' is the binder of a ForAllTy
519 -- It's convenient to define this synonym here rather its natural
520 -- home in TyCoRep, because it's used in DataCon.hs-boot
521 --
522 -- A 'TyVarBinder' is a binder with only TyVar
523 type TyCoVarBinder = VarBndr TyCoVar ArgFlag
524 type TyVarBinder = VarBndr TyVar ArgFlag
525
526 binderVar :: VarBndr tv argf -> tv
527 binderVar (Bndr v _) = v
528
529 binderVars :: [VarBndr tv argf] -> [tv]
530 binderVars tvbs = map binderVar tvbs
531
532 binderArgFlag :: VarBndr tv argf -> argf
533 binderArgFlag (Bndr _ argf) = argf
534
535 binderType :: VarBndr TyCoVar argf -> Type
536 binderType (Bndr tv _) = varType tv
537
538 -- | Make a named binder
539 mkTyCoVarBinder :: ArgFlag -> TyCoVar -> TyCoVarBinder
540 mkTyCoVarBinder vis var = Bndr var vis
541
542 -- | Make a named binder
543 -- 'var' should be a type variable
544 mkTyVarBinder :: ArgFlag -> TyVar -> TyVarBinder
545 mkTyVarBinder vis var
546 = ASSERT( isTyVar var )
547 Bndr var vis
548
549 -- | Make many named binders
550 mkTyCoVarBinders :: ArgFlag -> [TyCoVar] -> [TyCoVarBinder]
551 mkTyCoVarBinders vis = map (mkTyCoVarBinder vis)
552
553 -- | Make many named binders
554 -- Input vars should be type variables
555 mkTyVarBinders :: ArgFlag -> [TyVar] -> [TyVarBinder]
556 mkTyVarBinders vis = map (mkTyVarBinder vis)
557
558 isTyVarBinder :: TyCoVarBinder -> Bool
559 isTyVarBinder (Bndr v _) = isTyVar v
560
561 instance Outputable tv => Outputable (VarBndr tv ArgFlag) where
562 ppr (Bndr v Required) = ppr v
563 ppr (Bndr v Specified) = char '@' <> ppr v
564 ppr (Bndr v Inferred) = braces (ppr v)
565
566 instance (Binary tv, Binary vis) => Binary (VarBndr tv vis) where
567 put_ bh (Bndr tv vis) = do { put_ bh tv; put_ bh vis }
568
569 get bh = do { tv <- get bh; vis <- get bh; return (Bndr tv vis) }
570
571 instance NamedThing tv => NamedThing (VarBndr tv flag) where
572 getName (Bndr tv _) = getName tv
573
574 {-
575 ************************************************************************
576 * *
577 * Type and kind variables *
578 * *
579 ************************************************************************
580 -}
581
582 tyVarName :: TyVar -> Name
583 tyVarName = varName
584
585 tyVarKind :: TyVar -> Kind
586 tyVarKind = varType
587
588 setTyVarUnique :: TyVar -> Unique -> TyVar
589 setTyVarUnique = setVarUnique
590
591 setTyVarName :: TyVar -> Name -> TyVar
592 setTyVarName = setVarName
593
594 setTyVarKind :: TyVar -> Kind -> TyVar
595 setTyVarKind tv k = tv {varType = k}
596
597 updateTyVarKind :: (Kind -> Kind) -> TyVar -> TyVar
598 updateTyVarKind update tv = tv {varType = update (tyVarKind tv)}
599
600 updateTyVarKindM :: (Monad m) => (Kind -> m Kind) -> TyVar -> m TyVar
601 updateTyVarKindM update tv
602 = do { k' <- update (tyVarKind tv)
603 ; return $ tv {varType = k'} }
604
605 mkTyVar :: Name -> Kind -> TyVar
606 mkTyVar name kind = TyVar { varName = name
607 , realUnique = getKey (nameUnique name)
608 , varType = kind
609 }
610
611 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
612 mkTcTyVar name kind details
613 = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
614 TcTyVar { varName = name,
615 realUnique = getKey (nameUnique name),
616 varType = kind,
617 tc_tv_details = details
618 }
619
620 tcTyVarDetails :: TyVar -> TcTyVarDetails
621 -- See Note [TcTyVars in the typechecker] in TcType
622 tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
623 tcTyVarDetails (TyVar {}) = vanillaSkolemTv
624 tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var <+> dcolon <+> pprKind (tyVarKind var))
625
626 setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
627 setTcTyVarDetails tv details = tv { tc_tv_details = details }
628
629 {-
630 %************************************************************************
631 %* *
632 \subsection{Ids}
633 * *
634 ************************************************************************
635 -}
636
637 idInfo :: HasDebugCallStack => Id -> IdInfo
638 idInfo (Id { id_info = info }) = info
639 idInfo other = pprPanic "idInfo" (ppr other)
640
641 idDetails :: Id -> IdDetails
642 idDetails (Id { id_details = details }) = details
643 idDetails other = pprPanic "idDetails" (ppr other)
644
645 -- The next three have a 'Var' suffix even though they always build
646 -- Ids, because Id.hs uses 'mkGlobalId' etc with different types
647 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
648 mkGlobalVar details name ty info
649 = mk_id name ty GlobalId details info
650
651 mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
652 mkLocalVar details name ty info
653 = mk_id name ty (LocalId NotExported) details info
654
655 mkCoVar :: Name -> Type -> CoVar
656 -- Coercion variables have no IdInfo
657 mkCoVar name ty = mk_id name ty (LocalId NotExported) coVarDetails vanillaIdInfo
658
659 -- | Exported 'Var's will not be removed as dead code
660 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
661 mkExportedLocalVar details name ty info
662 = mk_id name ty (LocalId Exported) details info
663
664 mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
665 mk_id name ty scope details info
666 = Id { varName = name,
667 realUnique = getKey (nameUnique name),
668 varType = ty,
669 idScope = scope,
670 id_details = details,
671 id_info = info }
672
673 -------------------
674 lazySetIdInfo :: Id -> IdInfo -> Var
675 lazySetIdInfo id info = id { id_info = info }
676
677 setIdDetails :: Id -> IdDetails -> Id
678 setIdDetails id details = id { id_details = details }
679
680 globaliseId :: Id -> Id
681 -- ^ If it's a local, make it global
682 globaliseId id = id { idScope = GlobalId }
683
684 setIdExported :: Id -> Id
685 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
686 -- and class operations, which are born as global 'Id's and automatically exported
687 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
688 setIdExported id@(Id { idScope = GlobalId }) = id
689 setIdExported tv = pprPanic "setIdExported" (ppr tv)
690
691 setIdNotExported :: Id -> Id
692 -- ^ We can only do this to LocalIds
693 setIdNotExported id = ASSERT( isLocalId id )
694 id { idScope = LocalId NotExported }
695
696 {-
697 ************************************************************************
698 * *
699 \subsection{Predicates over variables}
700 * *
701 ************************************************************************
702 -}
703
704 -- | Is this a type-level (i.e., computationally irrelevant, thus erasable)
705 -- variable? Satisfies @isTyVar = not . isId@.
706 isTyVar :: Var -> Bool -- True of both TyVar and TcTyVar
707 isTyVar (TyVar {}) = True
708 isTyVar (TcTyVar {}) = True
709 isTyVar _ = False
710
711 isTcTyVar :: Var -> Bool -- True of TcTyVar only
712 isTcTyVar (TcTyVar {}) = True
713 isTcTyVar _ = False
714
715 isTyCoVar :: Var -> Bool
716 isTyCoVar v = isTyVar v || isCoVar v
717
718 -- | Is this a value-level (i.e., computationally relevant) 'Id'entifier?
719 -- Satisfies @isId = not . isTyVar@.
720 isId :: Var -> Bool
721 isId (Id {}) = True
722 isId _ = False
723
724 -- | Is this a coercion variable?
725 -- Satisfies @'isId' v ==> 'isCoVar' v == not ('isNonCoVarId' v)@.
726 isCoVar :: Var -> Bool
727 isCoVar (Id { id_details = details }) = isCoVarDetails details
728 isCoVar _ = False
729
730 -- | Is this a term variable ('Id') that is /not/ a coercion variable?
731 -- Satisfies @'isId' v ==> 'isCoVar' v == not ('isNonCoVarId' v)@.
732 isNonCoVarId :: Var -> Bool
733 isNonCoVarId (Id { id_details = details }) = not (isCoVarDetails details)
734 isNonCoVarId _ = False
735
736 isLocalId :: Var -> Bool
737 isLocalId (Id { idScope = LocalId _ }) = True
738 isLocalId _ = False
739
740 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
741 -- These are the variables that we need to pay attention to when finding free
742 -- variables, or doing dependency analysis.
743 isLocalVar :: Var -> Bool
744 isLocalVar v = not (isGlobalId v)
745
746 isGlobalId :: Var -> Bool
747 isGlobalId (Id { idScope = GlobalId }) = True
748 isGlobalId _ = False
749
750 -- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
751 -- that must have a binding in this module. The converse
752 -- is not quite right: there are some global 'Id's that must have
753 -- bindings, such as record selectors. But that doesn't matter,
754 -- because it's only used for assertions
755 mustHaveLocalBinding :: Var -> Bool
756 mustHaveLocalBinding var = isLocalVar var
757
758 -- | 'isExportedIdVar' means \"don't throw this away\"
759 isExportedId :: Var -> Bool
760 isExportedId (Id { idScope = GlobalId }) = True
761 isExportedId (Id { idScope = LocalId Exported}) = True
762 isExportedId _ = False