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