{-# LANGUAGE FlexibleInstances  #-}
{-# LANGUAGE LambdaCase         #-}
{-# LANGUAGE DeriveDataTypeable #-}

{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998


The @TyCon@ datatype
-}

module GHC.Core.TyCon(
        -- * Main TyCon data types
        TyCon,
        AlgTyConRhs(..), visibleDataCons,
        AlgTyConFlav(..), isNoParent,
        FamTyConFlav(..), Role(..), Injectivity(..),
        RuntimeRepInfo(..), TyConFlavour(..),

        -- * TyConBinder
        TyConBinder, TyConBndrVis(..), TyConTyCoBinder,
        mkNamedTyConBinder, mkNamedTyConBinders,
        mkRequiredTyConBinder,
        mkAnonTyConBinder, mkAnonTyConBinders,
        tyConBinderArgFlag, tyConBndrVisArgFlag, isNamedTyConBinder,
        isVisibleTyConBinder, isInvisibleTyConBinder, isVisibleTcbVis,

        -- ** Field labels
        tyConFieldLabels, lookupTyConFieldLabel,

        -- ** Constructing TyCons
        mkAlgTyCon,
        mkClassTyCon,
        mkFunTyCon,
        mkPrimTyCon,
        mkTupleTyCon,
        mkSumTyCon,
        mkDataTyConRhs,
        mkLevPolyDataTyConRhs,
        mkSynonymTyCon,
        mkFamilyTyCon,
        mkPromotedDataCon,
        mkTcTyCon,
        noTcTyConScopedTyVars,

        -- ** Predicates on TyCons
        isAlgTyCon, isVanillaAlgTyCon, isConstraintKindCon,
        isClassTyCon, isFamInstTyCon,
        isFunTyCon,
        isPrimTyCon,
        isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon,
        isUnboxedSumTyCon, isPromotedTupleTyCon,
        isLiftedAlgTyCon,
        isTypeSynonymTyCon,
        mustBeSaturated,
        isPromotedDataCon, isPromotedDataCon_maybe,
        isKindTyCon, isLiftedTypeKindTyConName,
        isTauTyCon, isFamFreeTyCon, isForgetfulSynTyCon,

        isDataTyCon,
        isEnumerationTyCon,
        isNewTyCon, isAbstractTyCon,
        isFamilyTyCon, isOpenFamilyTyCon,
        isTypeFamilyTyCon, isDataFamilyTyCon,
        isOpenTypeFamilyTyCon, isClosedSynFamilyTyConWithAxiom_maybe,
        tyConInjectivityInfo,
        isBuiltInSynFamTyCon_maybe,
        isGadtSyntaxTyCon, isInjectiveTyCon, isGenerativeTyCon, isGenInjAlgRhs,
        isTyConAssoc, tyConAssoc_maybe, tyConFlavourAssoc_maybe,
        isImplicitTyCon,
        isTyConWithSrcDataCons,
        isTcTyCon, setTcTyConKind,
        tcHasFixedRuntimeRep,
        isConcreteTyCon,

        -- ** Extracting information out of TyCons
        tyConName,
        tyConSkolem,
        tyConKind,
        tyConUnique,
        tyConTyVars, tyConVisibleTyVars,
        tyConCType, tyConCType_maybe,
        tyConDataCons, tyConDataCons_maybe,
        tyConSingleDataCon_maybe, tyConSingleDataCon,
        tyConAlgDataCons_maybe,
        tyConSingleAlgDataCon_maybe,
        tyConFamilySize,
        tyConStupidTheta,
        tyConArity,
        tyConNullaryTy,
        tyConRoles,
        tyConFlavour,
        tyConTuple_maybe, tyConClass_maybe, tyConATs,
        tyConFamInst_maybe, tyConFamInstSig_maybe, tyConFamilyCoercion_maybe,
        tyConFamilyResVar_maybe,
        synTyConDefn_maybe, synTyConRhs_maybe,
        famTyConFlav_maybe, famTcResVar,
        algTyConRhs,
        newTyConRhs, newTyConEtadArity, newTyConEtadRhs,
        unwrapNewTyCon_maybe, unwrapNewTyConEtad_maybe,
        newTyConDataCon_maybe,
        algTcFields,
        tyConRuntimeRepInfo,
        tyConBinders, tyConResKind, tyConInvisTVBinders,
        tcTyConScopedTyVars, tcTyConIsPoly,
        mkTyConTagMap,

        -- ** Manipulating TyCons
        expandSynTyCon_maybe,
        newTyConCo, newTyConCo_maybe,
        pprPromotionQuote, mkTyConKind,

        -- ** Predicated on TyConFlavours
        tcFlavourIsOpen,

        -- * Runtime type representation
        TyConRepName, tyConRepName_maybe,
        mkPrelTyConRepName,
        tyConRepModOcc,

        -- * Primitive representations of Types
        PrimRep(..), PrimElemRep(..),
        primElemRepToPrimRep,
        isVoidRep, isGcPtrRep,
        primRepSizeB,
        primElemRepSizeB,
        primRepIsFloat,
        primRepsCompatible,
        primRepCompatible,

) where

import GHC.Prelude
import GHC.Platform

import {-# SOURCE #-} GHC.Core.TyCo.Rep
   ( Kind, Type, PredType, mkForAllTy, mkFunTyMany, mkNakedTyConTy )
import {-# SOURCE #-} GHC.Core.TyCo.Ppr
   ( pprType )
import {-# SOURCE #-} GHC.Builtin.Types
   ( runtimeRepTyCon, constraintKind, levityTyCon
   , multiplicityTyCon
   , vecCountTyCon, vecElemTyCon, liftedTypeKind )
import {-# SOURCE #-} GHC.Core.DataCon
   ( DataCon, dataConFieldLabels
   , dataConTyCon, dataConFullSig
   , isUnboxedSumDataCon )
import {-# SOURCE #-} GHC.Core.Type
   ( isLiftedTypeKind )
import GHC.Builtin.Uniques
  ( tyConRepNameUnique
  , dataConTyRepNameUnique )

import GHC.Utils.Binary
import GHC.Types.Var
import GHC.Types.Var.Set
import GHC.Core.Class
import GHC.Types.Basic
import GHC.Types.ForeignCall
import GHC.Types.Name
import GHC.Types.Name.Env
import GHC.Core.Coercion.Axiom
import GHC.Builtin.Names
import GHC.Data.Maybe
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Utils.Panic.Plain
import GHC.Data.FastString.Env
import GHC.Types.FieldLabel
import GHC.Settings.Constants
import GHC.Utils.Misc
import GHC.Types.Unique.Set
import GHC.Unit.Module

import qualified Data.Data as Data

{-
-----------------------------------------------
        Notes about type families
-----------------------------------------------

Note [Type synonym families]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Type synonym families, also known as "type functions", map directly
  onto the type functions in FC:

        type family F a :: *
        type instance F Int = Bool
        ..etc...

* Reply "yes" to isTypeFamilyTyCon, and isFamilyTyCon

* From the user's point of view (F Int) and Bool are simply
  equivalent types.

* A Haskell 98 type synonym is a degenerate form of a type synonym
  family.

* Type functions can't appear in the LHS of a type function:
        type instance F (F Int) = ...   -- BAD!

* Translation of type family decl:
        type family F a :: *
  translates to
    a FamilyTyCon 'F', whose FamTyConFlav is OpenSynFamilyTyCon

        type family G a :: * where
          G Int = Bool
          G Bool = Char
          G a = ()
  translates to
    a FamilyTyCon 'G', whose FamTyConFlav is ClosedSynFamilyTyCon, with the
    appropriate CoAxiom representing the equations

We also support injective type families -- see Note [Injective type families]

Note [Data type families]
~~~~~~~~~~~~~~~~~~~~~~~~~
See also Note [Wrappers for data instance tycons] in GHC.Types.Id.Make

* Data type families are declared thus
        data family T a :: *
        data instance T Int = T1 | T2 Bool

  Here T is the "family TyCon".

* Reply "yes" to isDataFamilyTyCon, and isFamilyTyCon

* The user does not see any "equivalent types" as they did with type
  synonym families.  They just see constructors with types
        T1 :: T Int
        T2 :: Bool -> T Int

* Here's the FC version of the above declarations:

        data T a
        data R:TInt = T1 | T2 Bool
        axiom ax_ti : T Int ~R R:TInt

  Note that this is a *representational* coercion
  The R:TInt is the "representation TyCons".
  It has an AlgTyConFlav of
        DataFamInstTyCon T [Int] ax_ti

* The axiom ax_ti may be eta-reduced; see
  Note [Eta reduction for data families] in GHC.Core.Coercion.Axiom

* Data family instances may have a different arity than the data family.
  See Note [Arity of data families] in GHC.Core.FamInstEnv

* The data constructor T2 has a wrapper (which is what the
  source-level "T2" invokes):

        $WT2 :: Bool -> T Int
        $WT2 b = T2 b `cast` sym ax_ti

* A data instance can declare a fully-fledged GADT:

        data instance T (a,b) where
          X1 :: T (Int,Bool)
          X2 :: a -> b -> T (a,b)

  Here's the FC version of the above declaration:

        data R:TPair a b where
          X1 :: R:TPair Int Bool
          X2 :: a -> b -> R:TPair a b
        axiom ax_pr :: T (a,b)  ~R  R:TPair a b

        $WX1 :: forall a b. a -> b -> T (a,b)
        $WX1 a b (x::a) (y::b) = X2 a b x y `cast` sym (ax_pr a b)

  The R:TPair are the "representation TyCons".
  We have a bit of work to do, to unpick the result types of the
  data instance declaration for T (a,b), to get the result type in the
  representation; e.g.  T (a,b) --> R:TPair a b

  The representation TyCon R:TList, has an AlgTyConFlav of

        DataFamInstTyCon T [(a,b)] ax_pr

* Notice that T is NOT translated to a FC type function; it just
  becomes a "data type" with no constructors, which can be coerced
  into R:TInt, R:TPair by the axioms.  These axioms
  axioms come into play when (and *only* when) you
        - use a data constructor
        - do pattern matching
  Rather like newtype, in fact

  As a result

  - T behaves just like a data type so far as decomposition is concerned

  - (T Int) is not implicitly converted to R:TInt during type inference.
    Indeed the latter type is unknown to the programmer.

  - There *is* an instance for (T Int) in the type-family instance
    environment, but it is looked up (via tcLookupDataFamilyInst)
    in can_eq_nc (via tcTopNormaliseNewTypeTF_maybe) when trying to
    solve representational equalities like
         T Int ~R# Bool
    Here we look up (T Int), convert it to R:TInt, and then unwrap the
    newtype R:TInt.

    It is also looked up in reduceTyFamApp_maybe.

  - It's fine to have T in the LHS of a type function:
    type instance F (T a) = [a]

  It was this last point that confused me!  The big thing is that you
  should not think of a data family T as a *type function* at all, not
  even an injective one!  We can't allow even injective type functions
  on the LHS of a type function:
        type family injective G a :: *
        type instance F (G Int) = Bool
  is no good, even if G is injective, because consider
        type instance G Int = Bool
        type instance F Bool = Char

  So a data type family is not an injective type function. It's just a
  data type with some axioms that connect it to other data types.

* The tyConTyVars of the representation tycon are the tyvars that the
  user wrote in the patterns. This is important in GHC.Tc.Deriv, where we
  bring these tyvars into scope before type-checking the deriving
  clause. This fact is arranged for in TcInstDecls.tcDataFamInstDecl.

Note [Associated families and their parent class]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*Associated* families are just like *non-associated* families, except
that they have a famTcParent field of (Just cls_tc), which identifies the
parent class.

However there is an important sharing relationship between
  * the tyConTyVars of the parent Class
  * the tyConTyVars of the associated TyCon

   class C a b where
     data T p a
     type F a q b

Here the 'a' and 'b' are shared with the 'Class'; that is, they have
the same Unique.

This is important. In an instance declaration we expect
  * all the shared variables to be instantiated the same way
  * the non-shared variables of the associated type should not
    be instantiated at all

  instance C [x] (Tree y) where
     data T p [x] = T1 x | T2 p
     type F [x] q (Tree y) = (x,y,q)

Note [TyCon Role signatures]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Every tycon has a role signature, assigning a role to each of the tyConTyVars
(or of equal length to the tyConArity, if there are no tyConTyVars). An
example demonstrates these best: say we have a tycon T, with parameters a at
nominal, b at representational, and c at phantom. Then, to prove
representational equality between T a1 b1 c1 and T a2 b2 c2, we need to have
nominal equality between a1 and a2, representational equality between b1 and
b2, and nothing in particular (i.e., phantom equality) between c1 and c2. This
might happen, say, with the following declaration:

  data T a b c where
    MkT :: b -> T Int b c

Data and class tycons have their roles inferred (see inferRoles in GHC.Tc.TyCl.Utils),
as do vanilla synonym tycons. Family tycons have all parameters at role N,
though it is conceivable that we could relax this restriction. (->)'s and
tuples' parameters are at role R. Each primitive tycon declares its roles;
it's worth noting that (~#)'s parameters are at role N. Promoted data
constructors' type arguments are at role R. All kind arguments are at role
N.

Note [Unboxed tuple RuntimeRep vars]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The contents of an unboxed tuple may have any representation. Accordingly,
the kind of the unboxed tuple constructor is runtime-representation
polymorphic.

Type constructor (2 kind arguments)
   (#,#) :: forall (q :: RuntimeRep) (r :: RuntimeRep).
                   TYPE q -> TYPE r -> TYPE (TupleRep [q, r])
Data constructor (4 type arguments)
   (#,#) :: forall (q :: RuntimeRep) (r :: RuntimeRep)
                   (a :: TYPE q) (b :: TYPE r). a -> b -> (# a, b #)

These extra tyvars (q and r) cause some delicate processing around tuples,
where we need to manually insert RuntimeRep arguments.
The same situation happens with unboxed sums: each alternative
has its own RuntimeRep.
For boxed tuples, there is no representation polymorphism, and therefore
we add RuntimeReps only for the unboxed version.

Type constructor (no kind arguments)
   (,) :: Type -> Type -> Type
Data constructor (2 type arguments)
   (,) :: forall a b. a -> b -> (a, b)


Note [Injective type families]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We allow injectivity annotations for type families (both open and closed):

  type family F (a :: k) (b :: k) = r | r -> a
  type family G a b = res | res -> a b where ...

Injectivity information is stored in the `famTcInj` field of `FamilyTyCon`.
`famTcInj` maybe stores a list of Bools, where each entry corresponds to a
single element of `tyConTyVars` (both lists should have identical length). If no
injectivity annotation was provided `famTcInj` is Nothing. From this follows an
invariant that if `famTcInj` is a Just then at least one element in the list
must be True.

See also:
 * [Injectivity annotation] in GHC.Hs.Decls
 * [Renaming injectivity annotation] in GHC.Rename.Module
 * [Verifying injectivity annotation] in GHC.Core.FamInstEnv
 * [Type inference for type families with injectivity] in GHC.Tc.Solver.Interact

Note [Sharing nullary TyConApps]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nullary type constructor applications are extremely common. For this reason
each TyCon carries with it a @TyConApp tycon []@. This ensures that
'mkTyConTy' does not need to allocate and eliminates quite a bit of heap
residency. Furthermore, we use 'mkTyConTy' in the nullary case of 'mkTyConApp',
ensuring that this function also benefits from sharing.

This optimisation improves allocations in the Cabal test by around 0.3% and
decreased cache misses measurably.

See #19367.


************************************************************************
*                                                                      *
                    TyConBinder, TyConTyCoBinder
*                                                                      *
************************************************************************
-}

type TyConBinder     = VarBndr TyVar   TyConBndrVis
type TyConTyCoBinder = VarBndr TyCoVar TyConBndrVis
     -- Only PromotedDataCon has TyConTyCoBinders
     -- See Note [Promoted GADT data constructors]

data TyConBndrVis
  = NamedTCB ArgFlag
  | AnonTCB  AnonArgFlag

instance Outputable TyConBndrVis where
  ppr :: TyConBndrVis -> SDoc
ppr (NamedTCB ArgFlag
flag) = String -> SDoc
text String
"NamedTCB" SDoc -> SDoc -> SDoc
<> ArgFlag -> SDoc
forall a. Outputable a => a -> SDoc
ppr ArgFlag
flag
  ppr (AnonTCB AnonArgFlag
af)    = String -> SDoc
text String
"AnonTCB"  SDoc -> SDoc -> SDoc
<> AnonArgFlag -> SDoc
forall a. Outputable a => a -> SDoc
ppr AnonArgFlag
af

mkAnonTyConBinder :: AnonArgFlag -> TyVar -> TyConBinder
mkAnonTyConBinder :: AnonArgFlag -> TyVar -> TyConBinder
mkAnonTyConBinder AnonArgFlag
af TyVar
tv = Bool -> TyConBinder -> TyConBinder
forall a. HasCallStack => Bool -> a -> a
assert (TyVar -> Bool
isTyVar TyVar
tv) (TyConBinder -> TyConBinder) -> TyConBinder -> TyConBinder
forall a b. (a -> b) -> a -> b
$
                          TyVar -> TyConBndrVis -> TyConBinder
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv (AnonArgFlag -> TyConBndrVis
AnonTCB AnonArgFlag
af)

mkAnonTyConBinders :: AnonArgFlag -> [TyVar] -> [TyConBinder]
mkAnonTyConBinders :: AnonArgFlag -> [TyVar] -> [TyConBinder]
mkAnonTyConBinders AnonArgFlag
af [TyVar]
tvs = (TyVar -> TyConBinder) -> [TyVar] -> [TyConBinder]
forall a b. (a -> b) -> [a] -> [b]
map (AnonArgFlag -> TyVar -> TyConBinder
mkAnonTyConBinder AnonArgFlag
af) [TyVar]
tvs

mkNamedTyConBinder :: ArgFlag -> TyVar -> TyConBinder
-- The odd argument order supports currying
mkNamedTyConBinder :: ArgFlag -> TyVar -> TyConBinder
mkNamedTyConBinder ArgFlag
vis TyVar
tv = Bool -> TyConBinder -> TyConBinder
forall a. HasCallStack => Bool -> a -> a
assert (TyVar -> Bool
isTyVar TyVar
tv) (TyConBinder -> TyConBinder) -> TyConBinder -> TyConBinder
forall a b. (a -> b) -> a -> b
$
                            TyVar -> TyConBndrVis -> TyConBinder
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv (ArgFlag -> TyConBndrVis
NamedTCB ArgFlag
vis)

mkNamedTyConBinders :: ArgFlag -> [TyVar] -> [TyConBinder]
-- The odd argument order supports currying
mkNamedTyConBinders :: ArgFlag -> [TyVar] -> [TyConBinder]
mkNamedTyConBinders ArgFlag
vis [TyVar]
tvs = (TyVar -> TyConBinder) -> [TyVar] -> [TyConBinder]
forall a b. (a -> b) -> [a] -> [b]
map (ArgFlag -> TyVar -> TyConBinder
mkNamedTyConBinder ArgFlag
vis) [TyVar]
tvs

-- | Make a Required TyConBinder. It chooses between NamedTCB and
-- AnonTCB based on whether the tv is mentioned in the dependent set
mkRequiredTyConBinder :: TyCoVarSet  -- these are used dependently
                      -> TyVar
                      -> TyConBinder
mkRequiredTyConBinder :: TyCoVarSet -> TyVar -> TyConBinder
mkRequiredTyConBinder TyCoVarSet
dep_set TyVar
tv
  | TyVar
tv TyVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
dep_set = ArgFlag -> TyVar -> TyConBinder
mkNamedTyConBinder ArgFlag
Required TyVar
tv
  | Bool
otherwise               = AnonArgFlag -> TyVar -> TyConBinder
mkAnonTyConBinder  AnonArgFlag
VisArg   TyVar
tv

tyConBinderArgFlag :: TyConBinder -> ArgFlag
tyConBinderArgFlag :: TyConBinder -> ArgFlag
tyConBinderArgFlag (Bndr TyVar
_ TyConBndrVis
vis) = TyConBndrVis -> ArgFlag
tyConBndrVisArgFlag TyConBndrVis
vis

tyConBndrVisArgFlag :: TyConBndrVis -> ArgFlag
tyConBndrVisArgFlag :: TyConBndrVis -> ArgFlag
tyConBndrVisArgFlag (NamedTCB ArgFlag
vis)     = ArgFlag
vis
tyConBndrVisArgFlag (AnonTCB AnonArgFlag
VisArg)   = ArgFlag
Required
tyConBndrVisArgFlag (AnonTCB AnonArgFlag
InvisArg) = ArgFlag
Inferred    -- See Note [AnonTCB InvisArg]

isNamedTyConBinder :: TyConBinder -> Bool
-- Identifies kind variables
-- E.g. data T k (a:k) = blah
-- Here 'k' is a NamedTCB, a variable used in the kind of other binders
isNamedTyConBinder :: TyConBinder -> Bool
isNamedTyConBinder (Bndr TyVar
_ (NamedTCB {})) = Bool
True
isNamedTyConBinder TyConBinder
_                      = Bool
False

isVisibleTyConBinder :: VarBndr tv TyConBndrVis -> Bool
-- Works for IfaceTyConBinder too
isVisibleTyConBinder :: forall tv. VarBndr tv TyConBndrVis -> Bool
isVisibleTyConBinder (Bndr tv
_ TyConBndrVis
tcb_vis) = TyConBndrVis -> Bool
isVisibleTcbVis TyConBndrVis
tcb_vis

isVisibleTcbVis :: TyConBndrVis -> Bool
isVisibleTcbVis :: TyConBndrVis -> Bool
isVisibleTcbVis (NamedTCB ArgFlag
vis)     = ArgFlag -> Bool
isVisibleArgFlag ArgFlag
vis
isVisibleTcbVis (AnonTCB AnonArgFlag
VisArg)   = Bool
True
isVisibleTcbVis (AnonTCB AnonArgFlag
InvisArg) = Bool
False

isInvisibleTyConBinder :: VarBndr tv TyConBndrVis -> Bool
-- Works for IfaceTyConBinder too
isInvisibleTyConBinder :: forall tv. VarBndr tv TyConBndrVis -> Bool
isInvisibleTyConBinder VarBndr tv TyConBndrVis
tcb = Bool -> Bool
not (VarBndr tv TyConBndrVis -> Bool
forall tv. VarBndr tv TyConBndrVis -> Bool
isVisibleTyConBinder VarBndr tv TyConBndrVis
tcb)

-- Build the 'tyConKind' from the binders and the result kind.
-- Keep in sync with 'mkTyConKind' in GHC.Iface.Type.
mkTyConKind :: [TyConBinder] -> Kind -> Kind
mkTyConKind :: [TyConBinder] -> Kind -> Kind
mkTyConKind [TyConBinder]
bndrs Kind
res_kind = (TyConBinder -> Kind -> Kind) -> Kind -> [TyConBinder] -> Kind
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr TyConBinder -> Kind -> Kind
mk Kind
res_kind [TyConBinder]
bndrs
  where
    mk :: TyConBinder -> Kind -> Kind
    mk :: TyConBinder -> Kind -> Kind
mk (Bndr TyVar
tv (AnonTCB AnonArgFlag
af))   Kind
k = AnonArgFlag -> Kind -> Kind -> Kind
mkFunTyMany AnonArgFlag
af (TyVar -> Kind
varType TyVar
tv) Kind
k
    mk (Bndr TyVar
tv (NamedTCB ArgFlag
vis)) Kind
k = TyVar -> ArgFlag -> Kind -> Kind
mkForAllTy TyVar
tv ArgFlag
vis Kind
k

tyConInvisTVBinders :: [TyConBinder]   -- From the TyCon
                    -> [InvisTVBinder] -- Suitable for the foralls of a term function
-- See Note [Building TyVarBinders from TyConBinders]
tyConInvisTVBinders :: [TyConBinder] -> [InvisTVBinder]
tyConInvisTVBinders [TyConBinder]
tc_bndrs
 = (TyConBinder -> InvisTVBinder) -> [TyConBinder] -> [InvisTVBinder]
forall a b. (a -> b) -> [a] -> [b]
map TyConBinder -> InvisTVBinder
mk_binder [TyConBinder]
tc_bndrs
 where
   mk_binder :: TyConBinder -> InvisTVBinder
mk_binder (Bndr TyVar
tv TyConBndrVis
tc_vis) = Specificity -> TyVar -> InvisTVBinder
forall vis. vis -> TyVar -> VarBndr TyVar vis
mkTyVarBinder Specificity
vis TyVar
tv
      where
        vis :: Specificity
vis = case TyConBndrVis
tc_vis of
                AnonTCB AnonArgFlag
VisArg           -> Specificity
SpecifiedSpec
                AnonTCB AnonArgFlag
InvisArg         -> Specificity
InferredSpec   -- See Note [AnonTCB InvisArg]
                NamedTCB ArgFlag
Required        -> Specificity
SpecifiedSpec
                NamedTCB (Invisible Specificity
vis) -> Specificity
vis

-- Returns only tyvars, as covars are always inferred
tyConVisibleTyVars :: TyCon -> [TyVar]
tyConVisibleTyVars :: TyCon -> [TyVar]
tyConVisibleTyVars TyCon
tc
  = [ TyVar
tv | Bndr TyVar
tv TyConBndrVis
vis <- TyCon -> [TyConBinder]
tyConBinders TyCon
tc
         , TyConBndrVis -> Bool
isVisibleTcbVis TyConBndrVis
vis ]

{- Note [AnonTCB InvisArg]
~~~~~~~~~~~~~~~~~~~~~~~~~~
It's pretty rare to have an (AnonTCB InvisArg) binder.  The
only way it can occur is through equality constraints in kinds. These
can arise in one of two ways:

* In a PromotedDataCon whose kind has an equality constraint:

    'MkT :: forall a b. (a~b) => blah

  See Note [Constraints in kinds] in GHC.Core.TyCo.Rep, and
  Note [Promoted data constructors] in this module.
* In a data type whose kind has an equality constraint, as in the
  following example from #12102:

    data T :: forall a. (IsTypeLit a ~ 'True) => a -> Type

When mapping an (AnonTCB InvisArg) to an ArgFlag, in
tyConBndrVisArgFlag, we use "Inferred" to mean "the user cannot
specify this arguments, even with visible type/kind application;
instead the type checker must fill it in.

We map (AnonTCB VisArg) to Required, of course: the user must
provide it. It would be utterly wrong to do this for constraint
arguments, which is why AnonTCB must have the AnonArgFlag in
the first place.

Note [Building TyVarBinders from TyConBinders]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We sometimes need to build the quantified type of a value from
the TyConBinders of a type or class.  For that we need not
TyConBinders but TyVarBinders (used in forall-type)  E.g:

 *  From   data T a = MkT (Maybe a)
    we are going to make a data constructor with type
           MkT :: forall a. Maybe a -> T a
    See the TyCoVarBinders passed to buildDataCon

 * From    class C a where { op :: a -> Maybe a }
   we are going to make a default method
           $dmop :: forall a. C a => a -> Maybe a
   See the TyCoVarBinders passed to mkSigmaTy in mkDefaultMethodType

Both of these are user-callable.  (NB: default methods are not callable
directly by the user but rather via the code generated by 'deriving',
which uses visible type application; see mkDefMethBind.)

Since they are user-callable we must get their type-argument visibility
information right; and that info is in the TyConBinders.
Here is an example:

  data App a b = MkApp (a b) -- App :: forall {k}. (k->*) -> k -> *

The TyCon has

  tyConTyBinders = [ Named (Bndr (k :: *) Inferred), Anon (k->*), Anon k ]

The TyConBinders for App line up with App's kind, given above.

But the DataCon MkApp has the type
  MkApp :: forall {k} (a:k->*) (b:k). a b -> App k a b

That is, its TyCoVarBinders should be

  dataConUnivTyVarBinders = [ Bndr (k:*)    Inferred
                            , Bndr (a:k->*) Specified
                            , Bndr (b:k)    Specified ]

So tyConTyVarBinders converts TyCon's TyConBinders into TyVarBinders:
  - variable names from the TyConBinders
  - but changing Anon/Required to Specified

The last part about Required->Specified comes from this:
  data T k (a:k) b = MkT (a b)
Here k is Required in T's kind, but we don't have Required binders in
the TyCoBinders for a term (see Note [No Required TyCoBinder in terms]
in GHC.Core.TyCo.Rep), so we change it to Specified when making MkT's TyCoBinders
-}


{- Note [The binders/kind/arity fields of a TyCon]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All TyCons have this group of fields
  tyConBinders   :: [TyConBinder/TyConTyCoBinder]
  tyConResKind   :: Kind
  tyConTyVars    :: [TyVar]   -- Cached = binderVars tyConBinders
                              --   NB: Currently (Aug 2018), TyCons that own this
                              --   field really only contain TyVars. So it is
                              --   [TyVar] instead of [TyCoVar].
  tyConKind      :: Kind      -- Cached = mkTyConKind tyConBinders tyConResKind
  tyConArity     :: Arity     -- Cached = length tyConBinders

They fit together like so:

* tyConBinders gives the telescope of type/coercion variables on the LHS of the
  type declaration.  For example:

    type App a (b :: k) = a b

  tyConBinders = [ Bndr (k::*)   (NamedTCB Inferred)
                 , Bndr (a:k->*) AnonTCB
                 , Bndr (b:k)    AnonTCB ]

  Note that there are three binders here, including the
  kind variable k.

  See Note [tyConBinders and lexical scoping]

* See Note [VarBndrs, TyCoVarBinders, TyConBinders, and visibility] in GHC.Core.TyCo.Rep
  for what the visibility flag means.

* Each TyConBinder tyConBinders has a TyVar (sometimes it is TyCoVar), and
  that TyVar may scope over some other part of the TyCon's definition. Eg
      type T a = a -> a
  we have
      tyConBinders = [ Bndr (a:*) AnonTCB ]
      synTcRhs     = a -> a
  So the 'a' scopes over the synTcRhs

* From the tyConBinders and tyConResKind we can get the tyConKind
  E.g for our App example:
      App :: forall k. (k->*) -> k -> *

  We get a 'forall' in the kind for each NamedTCB, and an arrow
  for each AnonTCB

  tyConKind is the full kind of the TyCon, not just the result kind

* For type families, tyConArity is the arguments this TyCon must be
  applied to, to be considered saturated.  Here we mean "applied to in
  the actual Type", not surface syntax; i.e. including implicit kind
  variables.  So it's just (length tyConBinders)

* For an algebraic data type, or data instance, the tyConResKind is
  always (TYPE r); that is, the tyConBinders are enough to saturate
  the type constructor.  I'm not quite sure why we have this invariant,
  but it's enforced by splitTyConKind

Note [tyConBinders and lexical scoping]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In a TyCon, and a PolyTcTyCon, we obey the following rule:

   The Name of the TyConBinder is precisely
       the lexically scoped Name from the original declaration
       (precisely = both OccName and Unique)

For example,
   data T a (b :: wombat) = MkT
We will get tyConBinders of [k, wombat, a::k, b::wombat]
The 'k' is made up; the user didn't specify it.  But for the kind of 'b'
we must use 'wombat'.

Why do we have this invariant?

* Similarly, when typechecking default definitions for class methods, in
  GHC.Tc.TyCl.Class.tcClassDecl2, we only have the (final) Class available;
  but the variables bound in that class must be in scope.  Eample (#19738):

    type P :: k -> Type
    data P a = MkP

    type T :: k -> Constraint
    class T (a :: j) where
      f :: P a
      f = MkP @j @a  -- 'j' must be in scope when we typecheck 'f'

* When typechecking `deriving` clauses for top-level data declarations, the
  tcTyConScopedTyVars are brought into scope in through the `di_scoped_tvs`
  field of GHC.Tc.Deriv.DerivInfo. Example (#16731):

    class C x1 x2

    type T :: a -> Type
    data T (x :: z) deriving (C z)

  When typechecking `C z`, we want `z` to map to `a`, which is exactly what the
  tcTyConScopedTyVars for T give us.
-}

instance OutputableBndr tv => Outputable (VarBndr tv TyConBndrVis) where
  ppr :: VarBndr tv TyConBndrVis -> SDoc
ppr (Bndr tv
v TyConBndrVis
bi) = TyConBndrVis -> SDoc
ppr_bi TyConBndrVis
bi SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
parens (BindingSite -> tv -> SDoc
forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
LetBind tv
v)
    where
      ppr_bi :: TyConBndrVis -> SDoc
ppr_bi (AnonTCB AnonArgFlag
VisArg)     = String -> SDoc
text String
"anon-vis"
      ppr_bi (AnonTCB AnonArgFlag
InvisArg)   = String -> SDoc
text String
"anon-invis"
      ppr_bi (NamedTCB ArgFlag
Required)  = String -> SDoc
text String
"req"
      ppr_bi (NamedTCB ArgFlag
Specified) = String -> SDoc
text String
"spec"
      ppr_bi (NamedTCB ArgFlag
Inferred)  = String -> SDoc
text String
"inf"

instance Binary TyConBndrVis where
  put_ :: BinHandle -> TyConBndrVis -> IO ()
put_ BinHandle
bh (AnonTCB AnonArgFlag
af)   = do { BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0; BinHandle -> AnonArgFlag -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh AnonArgFlag
af }
  put_ BinHandle
bh (NamedTCB ArgFlag
vis) = do { BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1; BinHandle -> ArgFlag -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh ArgFlag
vis }

  get :: BinHandle -> IO TyConBndrVis
get BinHandle
bh = do { Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
              ; case Word8
h of
                  Word8
0 -> do { AnonArgFlag
af  <- BinHandle -> IO AnonArgFlag
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh; TyConBndrVis -> IO TyConBndrVis
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (AnonArgFlag -> TyConBndrVis
AnonTCB AnonArgFlag
af) }
                  Word8
_ -> do { ArgFlag
vis <- BinHandle -> IO ArgFlag
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh; TyConBndrVis -> IO TyConBndrVis
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ArgFlag -> TyConBndrVis
NamedTCB ArgFlag
vis) } }


{- *********************************************************************
*                                                                      *
               The TyCon type
*                                                                      *
************************************************************************
-}


-- | TyCons represent type constructors. Type constructors are introduced by
-- things such as:
--
-- 1) Data declarations: @data Foo = ...@ creates the @Foo@ type constructor of
--    kind @*@
--
-- 2) Type synonyms: @type Foo = ...@ creates the @Foo@ type constructor
--
-- 3) Newtypes: @newtype Foo a = MkFoo ...@ creates the @Foo@ type constructor
--    of kind @* -> *@
--
-- 4) Class declarations: @class Foo where@ creates the @Foo@ type constructor
--    of kind @*@
--
-- This data type also encodes a number of primitive, built in type constructors
-- such as those for function and tuple types.

-- If you edit this type, you may need to update the GHC formalism
-- See Note [GHC Formalism] in GHC.Core.Lint
data TyCon
  = -- | The function type constructor, @(->)@
    FunTyCon {
        TyCon -> Unique
tyConUnique :: Unique,   -- ^ A Unique of this TyCon. Invariant:
                                 -- identical to Unique of Name stored in
                                 -- tyConName field.

        TyCon -> Name
tyConName   :: Name,     -- ^ Name of the constructor

        -- See Note [The binders/kind/arity fields of a TyCon]
        TyCon -> [TyConBinder]
tyConBinders :: [TyConBinder],    -- ^ Full binders
        TyCon -> Kind
tyConResKind :: Kind,             -- ^ Result kind
        TyCon -> Kind
tyConKind    :: Kind,             -- ^ Kind of this TyCon
        TyCon -> Int
tyConArity   :: Arity,            -- ^ Arity
        TyCon -> Kind
tyConNullaryTy :: Type,

        TyCon -> Name
tcRepName :: TyConRepName
    }

  -- | Algebraic data types, from
  --     - @data@ declarations
  --     - @newtype@ declarations
  --     - data instance declarations
  --     - type instance declarations
  --     - the TyCon generated by a class declaration
  --     - boxed tuples
  --     - unboxed tuples
  --     - constraint tuples
  --     - unboxed sums
  -- Data/newtype/type /families/ are handled by 'FamilyTyCon'.
  -- See 'AlgTyConRhs' for more information.
  | AlgTyCon {
        tyConUnique  :: Unique,  -- ^ A Unique of this TyCon. Invariant:
                                 -- identical to Unique of Name stored in
                                 -- tyConName field.

        tyConName    :: Name,    -- ^ Name of the constructor

        -- See Note [The binders/kind/arity fields of a TyCon]
        tyConBinders :: [TyConBinder], -- ^ Full binders
        TyCon -> [TyVar]
tyConTyVars  :: [TyVar],          -- ^ TyVar binders
        tyConResKind :: Kind,             -- ^ Result kind
        tyConKind    :: Kind,             -- ^ Kind of this TyCon
        tyConArity   :: Arity,            -- ^ Arity
        tyConNullaryTy :: Type,           -- ^ A pre-allocated @TyConApp tycon []@

              -- The tyConTyVars scope over:
              --
              -- 1. The 'algTcStupidTheta'
              -- 2. The cached types in algTyConRhs.NewTyCon
              -- 3. The family instance types if present
              --
              -- Note that it does /not/ scope over the data
              -- constructors.

        TyCon -> [Role]
tcRoles      :: [Role],  -- ^ The role for each type variable
                                 -- This list has length = tyConArity
                                 -- See also Note [TyCon Role signatures]

        TyCon -> Maybe CType
tyConCType   :: Maybe CType,-- ^ The C type that should be used
                                    -- for this type when using the FFI
                                    -- and CAPI

        TyCon -> Bool
algTcGadtSyntax  :: Bool,   -- ^ Was the data type declared with GADT
                                    -- syntax?  If so, that doesn't mean it's a
                                    -- true GADT; only that the "where" form
                                    -- was used.  This field is used only to
                                    -- guide pretty-printing

        TyCon -> [Kind]
algTcStupidTheta :: [PredType], -- ^ The \"stupid theta\" for the data
                                        -- type (always empty for GADTs).  A
                                        -- \"stupid theta\" is the context to
                                        -- the left of an algebraic type
                                        -- declaration, e.g. @Eq a@ in the
                                        -- declaration @data Eq a => T a ...@.
                                        -- See @Note [The stupid context]@ in
                                        -- "GHC.Core.DataCon".

        TyCon -> AlgTyConRhs
algTcRhs    :: AlgTyConRhs, -- ^ Contains information about the
                                    -- data constructors of the algebraic type

        TyCon -> FieldLabelEnv
algTcFields :: FieldLabelEnv, -- ^ Maps a label to information
                                      -- about the field

        TyCon -> AlgTyConFlav
algTcFlavour :: AlgTyConFlav   -- ^ The flavour of this algebraic tycon.
                                       -- Gives the class or family declaration
                                       -- 'TyCon' for derived 'TyCon's representing
                                       -- class or family instances, respectively.

    }

  -- | Represents type synonyms
  | SynonymTyCon {
        tyConUnique  :: Unique,  -- ^ A Unique of this TyCon. Invariant:
                                 -- identical to Unique of Name stored in
                                 -- tyConName field.

        tyConName    :: Name,    -- ^ Name of the constructor

        -- See Note [The binders/kind/arity fields of a TyCon]
        tyConBinders :: [TyConBinder], -- ^ Full binders
        tyConTyVars  :: [TyVar],          -- ^ TyVar binders
        tyConResKind :: Kind,             -- ^ Result kind
        tyConKind    :: Kind,             -- ^ Kind of this TyCon
        tyConArity   :: Arity,            -- ^ Arity
        tyConNullaryTy :: Type,           -- ^ A pre-allocated @TyConApp tycon []@
             -- tyConTyVars scope over: synTcRhs

        tcRoles      :: [Role],  -- ^ The role for each type variable
                                 -- This list has length = tyConArity
                                 -- See also Note [TyCon Role signatures]

        TyCon -> Kind
synTcRhs     :: Type,    -- ^ Contains information about the expansion
                                 -- of the synonym

        TyCon -> Bool
synIsTau     :: Bool,   -- True <=> the RHS of this synonym does not
                                 --          have any foralls, after expanding any
                                 --          nested synonyms
        TyCon -> Bool
synIsFamFree  :: Bool,   -- True <=> the RHS of this synonym does not mention
                                 --          any type synonym families (data families
                                 --          are fine), again after expanding any
                                 --          nested synonyms
        TyCon -> Bool
synIsForgetful :: Bool   -- True <=  at least one argument is not mentioned
                                 --          in the RHS (or is mentioned only under
                                 --          forgetful synonyms)
                                 -- Test is conservative, so True does not guarantee
                                 -- forgetfulness.
    }

  -- | Represents families (both type and data)
  -- Argument roles are all Nominal
  | FamilyTyCon {
        tyConUnique  :: Unique,  -- ^ A Unique of this TyCon. Invariant:
                                 -- identical to Unique of Name stored in
                                 -- tyConName field.

        tyConName    :: Name,    -- ^ Name of the constructor

        -- See Note [The binders/kind/arity fields of a TyCon]
        tyConBinders :: [TyConBinder], -- ^ Full binders
        tyConTyVars  :: [TyVar],          -- ^ TyVar binders
        tyConResKind :: Kind,             -- ^ Result kind
        tyConKind    :: Kind,             -- ^ Kind of this TyCon
        tyConArity   :: Arity,            -- ^ Arity
        tyConNullaryTy :: Type,           -- ^ A pre-allocated @TyConApp tycon []@
            -- tyConTyVars connect an associated family TyCon
            -- with its parent class; see GHC.Tc.Validity.checkConsistentFamInst

        TyCon -> Maybe Name
famTcResVar  :: Maybe Name,   -- ^ Name of result type variable, used
                                      -- for pretty-printing with --show-iface
                                      -- and for reifying TyCon in Template
                                      -- Haskell

        TyCon -> FamTyConFlav
famTcFlav    :: FamTyConFlav, -- ^ Type family flavour: open, closed,
                                      -- abstract, built-in. See comments for
                                      -- FamTyConFlav

        TyCon -> Maybe TyCon
famTcParent  :: Maybe TyCon,  -- ^ For *associated* type/data families
                                      -- The class tycon in which the family is declared
                                      -- See Note [Associated families and their parent class]

        TyCon -> Injectivity
famTcInj     :: Injectivity   -- ^ is this a type family injective in
                                      -- its type variables? Nothing if no
                                      -- injectivity annotation was given
    }

  -- | Primitive types; cannot be defined in Haskell. This includes
  -- the usual suspects (such as @Int#@) as well as foreign-imported
  -- types and kinds (@*@, @#@, and @?@)
  | PrimTyCon {
        tyConUnique   :: Unique, -- ^ A Unique of this TyCon. Invariant:
                                 -- identical to Unique of Name stored in
                                 -- tyConName field.

        tyConName     :: Name,   -- ^ Name of the constructor

        -- See Note [The binders/kind/arity fields of a TyCon]
        tyConBinders :: [TyConBinder], -- ^ Full binders
        tyConResKind :: Kind,             -- ^ Result kind
        tyConKind    :: Kind,             -- ^ Kind of this TyCon
        tyConArity   :: Arity,            -- ^ Arity
        tyConNullaryTy :: Type,           -- ^ A pre-allocated @TyConApp tycon []@

        tcRoles       :: [Role], -- ^ The role for each type variable
                                 -- This list has length = tyConArity
                                 -- See also Note [TyCon Role signatures]

        TyCon -> Name
primRepName :: TyConRepName   -- ^ The 'Typeable' representation.
                                      -- A cached version of
                                      -- @'mkPrelTyConRepName' ('tyConName' tc)@.
    }

  -- | Represents promoted data constructor.
  | PromotedDataCon {          -- See Note [Promoted data constructors]
        tyConUnique  :: Unique,     -- ^ Same Unique as the data constructor
        tyConName    :: Name,       -- ^ Same Name as the data constructor

        -- See Note [The binders/kind/arity fields of a TyCon]
        tyConBinders :: [TyConTyCoBinder], -- ^ Full binders
           -- TyConTyCoBinder: see Note [Promoted GADT data construtors]
        tyConResKind :: Kind,             -- ^ Result kind
        tyConKind    :: Kind,             -- ^ Kind of this TyCon
        tyConArity   :: Arity,            -- ^ Arity
        tyConNullaryTy :: Type,           -- ^ A pre-allocated @TyConApp tycon []@

        tcRoles       :: [Role],    -- ^ Roles: N for kind vars, R for type vars
        TyCon -> DataCon
dataCon       :: DataCon,   -- ^ Corresponding data constructor
        tcRepName     :: TyConRepName,
        TyCon -> RuntimeRepInfo
promDcRepInfo :: RuntimeRepInfo  -- ^ See comments with 'RuntimeRepInfo'
    }

  -- | These exist only during type-checking. See Note [How TcTyCons work]
  -- in "GHC.Tc.TyCl"
  | TcTyCon {
        tyConUnique :: Unique,
        tyConName   :: Name,

        -- See Note [The binders/kind/arity fields of a TyCon]
        tyConBinders :: [TyConBinder], -- ^ Full binders
        tyConTyVars  :: [TyVar],       -- ^ TyVar binders
        tyConResKind :: Kind,          -- ^ Result kind
        tyConKind    :: Kind,          -- ^ Kind of this TyCon
        tyConArity   :: Arity,         -- ^ Arity
        tyConNullaryTy :: Type,           -- ^ A pre-allocated @TyConApp tycon []@

          -- NB: the TyConArity of a TcTyCon must match
          -- the number of Required (positional, user-specified)
          -- arguments to the type constructor; see the use
          -- of tyConArity in generaliseTcTyCon

        TyCon -> [(Name, TyVar)]
tcTyConScopedTyVars :: [(Name,TcTyVar)],
          -- ^ Scoped tyvars over the tycon's body
          -- The range is always a skolem or TcTyVar, be
          -- MonoTcTyCon only: see Note [Scoped tyvars in a TcTyCon]

        TyCon -> Bool
tcTyConIsPoly     :: Bool, -- ^ Is this TcTyCon already generalized?
                                   -- Used only to make zonking more efficient

        TyCon -> TyConFlavour
tcTyConFlavour :: TyConFlavour
                           -- ^ What sort of 'TyCon' this represents.
      }

{- Note [Scoped tyvars in a TcTyCon]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The tcTyConScopedTyVars field records the lexicial-binding connection
between the original, user-specified Name (i.e. thing in scope) and
the TcTyVar that the Name is bound to.

Order *does* matter; the tcTyConScopedTyvars list consists of
     specified_tvs ++ required_tvs

where
   * specified ones first
   * required_tvs the same as tyConTyVars
   * tyConArity = length required_tvs

tcTyConScopedTyVars are used only for MonoTcTyCons, not PolyTcTyCons.
See Note [TcTyCon, MonoTcTyCon, and PolyTcTyCon] in GHC.Tc.Utils.TcType.

Note [Promoted GADT data constructors]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Any promoted GADT data constructor will have a type with equality
constraints in its type; e.g.
    K :: forall a b. (a ~# [b]) => a -> b -> T a

So, when promoted to become a type constructor, the tyConBinders
will include CoVars.  That is why we use [TyConTyCoBinder] for the
tyconBinders field.  TyConTyCoBinder is a synonym for TyConBinder,
but with the clue that the binder can be a CoVar not just a TyVar.

Note [Representation-polymorphic TyCons]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To check for representation-polymorphism directly in the typechecker,
e.g. when using GHC.Tc.Utils.TcMType.checkTypeHasFixedRuntimeRep,
we need to compute whether a type has a syntactically fixed RuntimeRep,
as per Note [Fixed RuntimeRep] in GHC.Tc.Utils.Concrete.

It's useful to have a quick way to check whether a saturated application
of a type constructor has a fixed RuntimeRep. That is, we want
to know, given a TyCon 'T' of arity 'n', does

  T a_1 ... a_n

always have a fixed RuntimeRep? That is, is it always the case
that this application has a kind of the form

  T a_1 ... a_n :: TYPE rep

in which 'rep' is a concrete 'RuntimeRep'?
('Concrete' in the sense of Note [The Concrete mechanism] in GHC.Tc.Utils.Concrete:
it contains no type-family applications or type variables.)

To answer this question, we have 'tcHasFixedRuntimeRep'.
If 'tcHasFixedRuntimeRep' returns 'True', it means we're sure that
every saturated application of `T` has a fixed RuntimeRep.
However, if it returns 'False', we don't know: perhaps some application might not
have a fixed RuntimeRep.

Examples:

  - For type families, we won't know in general whether an application
    will have a fixed RuntimeRep:

      type F :: k -> k
      type family F a where {..}

    `tcHasFixedRuntimeRep F = False'

  - For newtypes, we're usually OK:

      newtype N a b c = MkN Int

    No matter what arguments we apply `N` to, we always get something of
    kind `Type`, which has a fixed RuntimeRep.
    Thus `tcHasFixedRuntimeRep N = True`.

    However, with `-XUnliftedNewtypes`, we can have representation-polymorphic
    newtypes:

      type UN :: TYPE rep -> TYPE rep
      newtype UN a = MkUN a

    `tcHasFixedRuntimeRep UN = False`

    For example, `UN @Int8Rep Int8#` is represented by an 8-bit value,
    while `UN @LiftedRep Int` is represented by a heap pointer.

    To distinguish whether we are dealing with a representation-polymorphic newtype,
    we keep track of which situation we are in using the 'nt_fixed_rep'
    field of the 'NewTyCon' constructor of 'AlgTyConRhs', and read this field
    to compute 'tcHasFixedRuntimeRep'.

  - A similar story can be told for datatypes: we're usually OK,
    except with `-XUnliftedDatatypes` which allows for levity polymorphism,
    e.g.:

      type UC :: TYPE (BoxedRep l) -> TYPE (BoxedRep l)
      type UC a = MkUC a

    `tcHasFixedRuntimeRep UC = False`

    Here, we keep track of whether we are dealing with a levity-polymorphic
    unlifted datatype using the 'data_fixed_lev' field of the 'DataTyCon'
    constructor of 'AlgTyConRhs'.

    N.B.: technically, the representation of a datatype is fixed,
    as it is always a pointer. However, we currently require that we
    know the specific `RuntimeRep`: knowing that it's `BoxedRep l`
    for a type-variable `l` isn't enough. See #15532.
-}

-- | Represents right-hand-sides of 'TyCon's for algebraic types
data AlgTyConRhs

    -- | Says that we know nothing about this data type, except that
    -- it's represented by a pointer.  Used when we export a data type
    -- abstractly into an .hi file.
  = AbstractTyCon

    -- | Information about those 'TyCon's derived from a @data@
    -- declaration. This includes data types with no constructors at
    -- all.
  | DataTyCon {
        AlgTyConRhs -> [DataCon]
data_cons :: [DataCon],
                          -- ^ The data type constructors; can be empty if the
                          --   user declares the type to have no constructors
                          --
                          -- INVARIANT: Kept in order of increasing 'DataCon'
                          -- tag (see the tag assignment in mkTyConTagMap)
        AlgTyConRhs -> Int
data_cons_size :: Int,
                          -- ^ Cached value: length data_cons
        AlgTyConRhs -> Bool
is_enum :: Bool,  -- ^ Cached value: is this an enumeration type?
                          --   See Note [Enumeration types]
        AlgTyConRhs -> Bool
data_fixed_lev :: Bool
                        -- ^ 'True' if the data type constructor has
                        -- a known, fixed levity when fully applied
                        -- to its arguments, False otherwise.
                        --
                        -- This can only be 'False' with UnliftedDatatypes,
                        -- e.g.
                        --
                        -- > data A :: TYPE (BoxedRep l) where { MkA :: Int -> A }
                        --
                        -- This boolean is cached to make it cheaper to check
                        -- for levity and representation-polymorphism in
                        -- tcHasFixedRuntimeRep.
    }

  | TupleTyCon {                   -- A boxed, unboxed, or constraint tuple
        AlgTyConRhs -> DataCon
data_con :: DataCon,       -- NB: it can be an *unboxed* tuple
        AlgTyConRhs -> TupleSort
tup_sort :: TupleSort      -- ^ Is this a boxed, unboxed or constraint
                                   -- tuple?
    }

  -- | An unboxed sum type.
  | SumTyCon {
        data_cons :: [DataCon],
        data_cons_size :: Int  -- ^ Cached value: length data_cons
    }

  -- | Information about those 'TyCon's derived from a @newtype@ declaration
  | NewTyCon {
        data_con :: DataCon,    -- ^ The unique constructor for the @newtype@.
                                --   It has no existentials

        AlgTyConRhs -> Kind
nt_rhs :: Type,         -- ^ Cached value: the argument type of the
                                -- constructor, which is just the representation
                                -- type of the 'TyCon' (remember that @newtype@s
                                -- do not exist at runtime so need a different
                                -- representation type).
                                --
                                -- The free 'TyVar's of this type are the
                                -- 'tyConTyVars' from the corresponding 'TyCon'

        AlgTyConRhs -> ([TyVar], Kind)
nt_etad_rhs :: ([TyVar], Type),
                        -- ^ Same as the 'nt_rhs', but this time eta-reduced.
                        -- Hence the list of 'TyVar's in this field may be
                        -- shorter than the declared arity of the 'TyCon'.

                        -- See Note [Newtype eta]
        AlgTyConRhs -> CoAxiom Unbranched
nt_co :: CoAxiom Unbranched,
                             -- The axiom coercion that creates the @newtype@
                             -- from the representation 'Type'.  The axiom witnesses
                             -- a representational coercion:
                             --   nt_co :: N ty1 ~R# rep_tys

                             -- See Note [Newtype coercions]
                             -- Invariant: arity = #tvs in nt_etad_rhs;
                             -- See Note [Newtype eta]
                             -- Watch out!  If any newtypes become transparent
                             -- again check #1072.
        AlgTyConRhs -> Bool
nt_fixed_rep :: Bool
                        -- ^ 'True' if the newtype has a known, fixed representation
                        -- when fully applied to its arguments, 'False' otherwise.
                        -- This can only ever be 'False' with UnliftedNewtypes.
                        --
                        -- Example:
                        --
                        -- > newtype N (a :: TYPE r) = MkN a
                        --
                        -- Invariant: nt_fixed_rep nt = tcHasFixedRuntimeRep (nt_rhs nt)
                        --
                        -- This boolean is cached to make it cheaper to check if a
                        -- variable binding is representation-polymorphic
                        -- in tcHasFixedRuntimeRep.
    }

mkSumTyConRhs :: [DataCon] -> AlgTyConRhs
mkSumTyConRhs :: [DataCon] -> AlgTyConRhs
mkSumTyConRhs [DataCon]
data_cons = [DataCon] -> Int -> AlgTyConRhs
SumTyCon [DataCon]
data_cons ([DataCon] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [DataCon]
data_cons)

-- | Create an 'AlgTyConRhs' from the data constructors,
-- for a potentially levity-polymorphic datatype (with `UnliftedDatatypes`).
mkLevPolyDataTyConRhs :: Bool -- ^ whether the 'DataCon' has a fixed levity
                      -> [DataCon]
                      -> AlgTyConRhs
mkLevPolyDataTyConRhs :: Bool -> [DataCon] -> AlgTyConRhs
mkLevPolyDataTyConRhs Bool
fixed_lev [DataCon]
cons
  = DataTyCon {
        data_cons :: [DataCon]
data_cons = [DataCon]
cons,
        data_cons_size :: Int
data_cons_size = [DataCon] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [DataCon]
cons,
        is_enum :: Bool
is_enum = Bool -> Bool
not ([DataCon] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [DataCon]
cons) Bool -> Bool -> Bool
&& (DataCon -> Bool) -> [DataCon] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all DataCon -> Bool
is_enum_con [DataCon]
cons,
                  -- See Note [Enumeration types] in GHC.Core.TyCon
        data_fixed_lev :: Bool
data_fixed_lev = Bool
fixed_lev
    }
  where
    is_enum_con :: DataCon -> Bool
is_enum_con DataCon
con
       | ([TyVar]
_univ_tvs, [TyVar]
ex_tvs, [EqSpec]
eq_spec, [Kind]
theta, [Scaled Kind]
arg_tys, Kind
_res)
           <- DataCon
-> ([TyVar], [TyVar], [EqSpec], [Kind], [Scaled Kind], Kind)
dataConFullSig DataCon
con
       = [TyVar] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TyVar]
ex_tvs Bool -> Bool -> Bool
&& [EqSpec] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [EqSpec]
eq_spec Bool -> Bool -> Bool
&& [Kind] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Kind]
theta Bool -> Bool -> Bool
&& [Scaled Kind] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Scaled Kind]
arg_tys

-- | Create an 'AlgTyConRhs' from the data constructors.
--
-- Use 'mkLevPolyDataConRhs' if the datatype can be levity-polymorphic.
mkDataTyConRhs :: [DataCon] -> AlgTyConRhs
mkDataTyConRhs :: [DataCon] -> AlgTyConRhs
mkDataTyConRhs = Bool -> [DataCon] -> AlgTyConRhs
mkLevPolyDataTyConRhs Bool
False

-- | Some promoted datacons signify extra info relevant to GHC. For example,
-- the @IntRep@ constructor of @RuntimeRep@ corresponds to the 'IntRep'
-- constructor of 'PrimRep'. This data structure allows us to store this
-- information right in the 'TyCon'. The other approach would be to look
-- up things like @RuntimeRep@'s @PrimRep@ by known-key every time.
-- See also Note [Getting from RuntimeRep to PrimRep] in "GHC.Types.RepType"
data RuntimeRepInfo
  = NoRRI       -- ^ an ordinary promoted data con
  | RuntimeRep ([Type] -> [PrimRep])
      -- ^ A constructor of @RuntimeRep@. The argument to the function should
      -- be the list of arguments to the promoted datacon.
  | VecCount Int         -- ^ A constructor of @VecCount@
  | VecElem PrimElemRep  -- ^ A constructor of @VecElem@
  | LiftedInfo           -- ^ A constructor of @Levity@
  | UnliftedInfo         -- ^ A constructor of @Levity@

-- | Extract those 'DataCon's that we are able to learn about.  Note
-- that visibility in this sense does not correspond to visibility in
-- the context of any particular user program!
visibleDataCons :: AlgTyConRhs -> [DataCon]
visibleDataCons :: AlgTyConRhs -> [DataCon]
visibleDataCons (AbstractTyCon {})            = []
visibleDataCons (DataTyCon{ data_cons :: AlgTyConRhs -> [DataCon]
data_cons = [DataCon]
cs }) = [DataCon]
cs
visibleDataCons (NewTyCon{ data_con :: AlgTyConRhs -> DataCon
data_con = DataCon
c })    = [DataCon
c]
visibleDataCons (TupleTyCon{ data_con :: AlgTyConRhs -> DataCon
data_con = DataCon
c })  = [DataCon
c]
visibleDataCons (SumTyCon{ data_cons :: AlgTyConRhs -> [DataCon]
data_cons = [DataCon]
cs })  = [DataCon]
cs

-- | Describes the flavour of an algebraic type constructor. For
-- classes and data families, this flavour includes a reference to
-- the parent 'TyCon'.
data AlgTyConFlav
  = -- | An ordinary algebraic type constructor. This includes unlifted and
    -- representation-polymorphic datatypes and newtypes and unboxed tuples,
    -- but NOT unboxed sums; see UnboxedSumTyCon.
    VanillaAlgTyCon
       TyConRepName   -- For Typeable

    -- | An unboxed sum type constructor. This is distinct from VanillaAlgTyCon
    -- because we currently don't allow unboxed sums to be Typeable since
    -- there are too many of them. See #13276.
  | UnboxedSumTyCon

  -- | Type constructors representing a class dictionary.
  -- See Note [ATyCon for classes] in "GHC.Core.TyCo.Rep"
  | ClassTyCon
        Class           -- INVARIANT: the classTyCon of this Class is the
                        -- current tycon
        TyConRepName

  -- | Type constructors representing an *instance* of a *data* family.
  -- Parameters:
  --
  --  1) The type family in question
  --
  --  2) Instance types; free variables are the 'tyConTyVars'
  --  of the current 'TyCon' (not the family one). INVARIANT:
  --  the number of types matches the arity of the family 'TyCon'
  --
  --  3) A 'CoTyCon' identifying the representation
  --  type with the type instance family
  | DataFamInstTyCon          -- See Note [Data type families]
        (CoAxiom Unbranched)  -- The coercion axiom.
               -- A *Representational* coercion,
               -- of kind   T ty1 ty2   ~R   R:T a b c
               -- where T is the family TyCon,
               -- and R:T is the representation TyCon (ie this one)
               -- and a,b,c are the tyConTyVars of this TyCon
               --
               -- BUT may be eta-reduced; see
               --     Note [Eta reduction for data families] in
               --     GHC.Core.Coercion.Axiom

          -- Cached fields of the CoAxiom, but adjusted to
          -- use the tyConTyVars of this TyCon
        TyCon   -- The family TyCon
        [Type]  -- Argument types (mentions the tyConTyVars of this TyCon)
                -- No shorter in length than the tyConTyVars of the family TyCon
                -- How could it be longer? See [Arity of data families] in GHC.Core.FamInstEnv

        -- E.g.  data instance T [a] = ...
        -- gives a representation tycon:
        --      data R:TList a = ...
        --      axiom co a :: T [a] ~ R:TList a
        -- with R:TList's algTcFlavour = DataFamInstTyCon T [a] co

instance Outputable AlgTyConFlav where
    ppr :: AlgTyConFlav -> SDoc
ppr (VanillaAlgTyCon {})        = String -> SDoc
text String
"Vanilla ADT"
    ppr (UnboxedSumTyCon {})        = String -> SDoc
text String
"Unboxed sum"
    ppr (ClassTyCon Class
cls Name
_)          = String -> SDoc
text String
"Class parent" SDoc -> SDoc -> SDoc
<+> Class -> SDoc
forall a. Outputable a => a -> SDoc
ppr Class
cls
    ppr (DataFamInstTyCon CoAxiom Unbranched
_ TyCon
tc [Kind]
tys) = String -> SDoc
text String
"Family parent (family instance)"
                                      SDoc -> SDoc -> SDoc
<+> TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc SDoc -> SDoc -> SDoc
<+> [SDoc] -> SDoc
sep ((Kind -> SDoc) -> [Kind] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Kind -> SDoc
pprType [Kind]
tys)

-- | Checks the invariants of a 'AlgTyConFlav' given the appropriate type class
-- name, if any
okParent :: Name -> AlgTyConFlav -> Bool
okParent :: Name -> AlgTyConFlav -> Bool
okParent Name
_       (VanillaAlgTyCon {})            = Bool
True
okParent Name
_       (UnboxedSumTyCon {})            = Bool
True
okParent Name
tc_name (ClassTyCon Class
cls Name
_)              = Name
tc_name Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon -> Name
tyConName (Class -> TyCon
classTyCon Class
cls)
okParent Name
_       (DataFamInstTyCon CoAxiom Unbranched
_ TyCon
fam_tc [Kind]
tys) = [Kind]
tys [Kind] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthAtLeast` TyCon -> Int
tyConArity TyCon
fam_tc

isNoParent :: AlgTyConFlav -> Bool
isNoParent :: AlgTyConFlav -> Bool
isNoParent (VanillaAlgTyCon {}) = Bool
True
isNoParent AlgTyConFlav
_                   = Bool
False

--------------------

data Injectivity
  = NotInjective
  | Injective [Bool]   -- 1-1 with tyConTyVars (incl kind vars)
  deriving( Injectivity -> Injectivity -> Bool
(Injectivity -> Injectivity -> Bool)
-> (Injectivity -> Injectivity -> Bool) -> Eq Injectivity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Injectivity -> Injectivity -> Bool
== :: Injectivity -> Injectivity -> Bool
$c/= :: Injectivity -> Injectivity -> Bool
/= :: Injectivity -> Injectivity -> Bool
Eq )

-- | Information pertaining to the expansion of a type synonym (@type@)
data FamTyConFlav
  = -- | Represents an open type family without a fixed right hand
    -- side.  Additional instances can appear at any time.
    --
    -- These are introduced by either a top level declaration:
    --
    -- > data family T a :: *
    --
    -- Or an associated data type declaration, within a class declaration:
    --
    -- > class C a b where
    -- >   data T b :: *
     DataFamilyTyCon
       TyConRepName

     -- | An open type synonym family  e.g. @type family F x y :: * -> *@
   | OpenSynFamilyTyCon

   -- | A closed type synonym family  e.g.
   -- @type family F x where { F Int = Bool }@
   | ClosedSynFamilyTyCon (Maybe (CoAxiom Branched))
     -- See Note [Closed type families]

   -- | A closed type synonym family declared in an hs-boot file with
   -- type family F a where ..
   | AbstractClosedSynFamilyTyCon

   -- | Built-in type family used by the TypeNats solver
   | BuiltInSynFamTyCon BuiltInSynFamily

instance Outputable FamTyConFlav where
    ppr :: FamTyConFlav -> SDoc
ppr (DataFamilyTyCon Name
n) = String -> SDoc
text String
"data family" SDoc -> SDoc -> SDoc
<+> Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
n
    ppr FamTyConFlav
OpenSynFamilyTyCon = String -> SDoc
text String
"open type family"
    ppr (ClosedSynFamilyTyCon Maybe (CoAxiom Branched)
Nothing) = String -> SDoc
text String
"closed type family"
    ppr (ClosedSynFamilyTyCon (Just CoAxiom Branched
coax)) = String -> SDoc
text String
"closed type family" SDoc -> SDoc -> SDoc
<+> CoAxiom Branched -> SDoc
forall a. Outputable a => a -> SDoc
ppr CoAxiom Branched
coax
    ppr FamTyConFlav
AbstractClosedSynFamilyTyCon = String -> SDoc
text String
"abstract closed type family"
    ppr (BuiltInSynFamTyCon BuiltInSynFamily
_) = String -> SDoc
text String
"built-in type family"

{- Note [Closed type families]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* In an open type family you can add new instances later.  This is the
  usual case.

* In a closed type family you can only put equations where the family
  is defined.

A non-empty closed type family has a single axiom with multiple
branches, stored in the 'ClosedSynFamilyTyCon' constructor.  A closed
type family with no equations does not have an axiom, because there is
nothing for the axiom to prove!


Note [Promoted data constructors]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All data constructors can be promoted to become a type constructor,
via the PromotedDataCon alternative in GHC.Core.TyCon.

* The TyCon promoted from a DataCon has the *same* Name and Unique as
  the DataCon.  Eg. If the data constructor Data.Maybe.Just(unique 78,
  say) is promoted to a TyCon whose name is Data.Maybe.Just(unique 78)

* We promote the *user* type of the DataCon.  Eg
     data T = MkT {-# UNPACK #-} !(Bool, Bool)
  The promoted kind is
     'MkT :: (Bool,Bool) -> T
  *not*
     'MkT :: Bool -> Bool -> T

* Similarly for GADTs:
     data G a where
       MkG :: forall b. b -> G [b]
  The promoted data constructor has kind
       'MkG :: forall b. b -> G [b]
  *not*
       'MkG :: forall a b. (a ~# [b]) => b -> G a

Note [Enumeration types]
~~~~~~~~~~~~~~~~~~~~~~~~
We define datatypes with no constructors to *not* be
enumerations; this fixes trac #2578,  Otherwise we
end up generating an empty table for
  <mod>_<type>_closure_tbl
which is used by tagToEnum# to map Int# to constructors
in an enumeration. The empty table apparently upset
the linker.

Moreover, all the data constructor must be enumerations, meaning
they have type  (forall abc. T a b c).  GADTs are not enumerations.
For example consider
    data T a where
      T1 :: T Int
      T2 :: T Bool
      T3 :: T a
What would [T1 ..] be?  [T1,T3] :: T Int? Easiest thing is to exclude them.
See #4528.

Note [Newtype coercions]
~~~~~~~~~~~~~~~~~~~~~~~~
The NewTyCon field nt_co is a CoAxiom which is used for coercing from
the representation type of the newtype, to the newtype itself. For
example,

   newtype T a = MkT (a -> a)

the NewTyCon for T will contain nt_co = CoT where CoT :: forall a. T a ~ a -> a.

We might also eta-contract the axiom: see Note [Newtype eta].

Note [Newtype eta]
~~~~~~~~~~~~~~~~~~
Consider
        newtype Parser a = MkParser (IO a) deriving Monad
Are these two types equal? That is, does a coercion exist between them?
        Monad Parser
        Monad IO
(We need this coercion to make the derived instance for Monad Parser.)

Well, yes.  But to see that easily we eta-reduce the RHS type of
Parser, in this case to IO, so that even unsaturated applications of
Parser will work right.  So instead of
   axParser :: forall a. Parser a ~ IO a
we generate an eta-reduced axiom
   axParser :: Parser ~ IO

This eta reduction is done when the type constructor is built, in
GHC.Tc.TyCl.Build.mkNewTyConRhs, and cached in NewTyCon.

Here's an example that I think showed up in practice.
Source code:
        newtype T a = MkT [a]
        newtype Foo m = MkFoo (forall a. m a -> Int)

        w1 :: Foo []
        w1 = ...

        w2 :: Foo T
        w2 = MkFoo (\(MkT x) -> case w1 of MkFoo f -> f x)

After desugaring, and discarding the data constructors for the newtypes,
we would like to get:
        w2 = w1 `cast` Foo axT

so that w2 and w1 share the same code. To do this, the coercion axiom
axT must have
        kind:    axT :: T ~ []
 and    arity:   0

See also Note [Newtype eta and homogeneous axioms] in GHC.Tc.TyCl.Build.

************************************************************************
*                                                                      *
                 TyConRepName
*                                                                      *
********************************************************************* -}

type TyConRepName = Name
   -- The Name of the top-level declaration for the Typeable world
   --    $tcMaybe :: Data.Typeable.Internal.TyCon
   --    $tcMaybe = TyCon { tyConName = "Maybe", ... }

tyConRepName_maybe :: TyCon -> Maybe TyConRepName
tyConRepName_maybe :: TyCon -> Maybe Name
tyConRepName_maybe (FunTyCon   { tcRepName :: TyCon -> Name
tcRepName = Name
rep_nm })
  = Name -> Maybe Name
forall a. a -> Maybe a
Just Name
rep_nm
tyConRepName_maybe (PrimTyCon  { primRepName :: TyCon -> Name
primRepName = Name
rep_nm })
  = Name -> Maybe Name
forall a. a -> Maybe a
Just Name
rep_nm
tyConRepName_maybe (AlgTyCon { algTcFlavour :: TyCon -> AlgTyConFlav
algTcFlavour = AlgTyConFlav
parent }) = case AlgTyConFlav
parent of
  VanillaAlgTyCon Name
rep_nm -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
rep_nm
  AlgTyConFlav
UnboxedSumTyCon        -> Maybe Name
forall a. Maybe a
Nothing
  ClassTyCon Class
_ Name
rep_nm    -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
rep_nm
  DataFamInstTyCon {}    -> Maybe Name
forall a. Maybe a
Nothing
tyConRepName_maybe (FamilyTyCon { famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = DataFamilyTyCon Name
rep_nm })
  = Name -> Maybe Name
forall a. a -> Maybe a
Just Name
rep_nm
tyConRepName_maybe (PromotedDataCon { dataCon :: TyCon -> DataCon
dataCon = DataCon
dc, tcRepName :: TyCon -> Name
tcRepName = Name
rep_nm })
  | DataCon -> Bool
isUnboxedSumDataCon DataCon
dc   -- see #13276
  = Maybe Name
forall a. Maybe a
Nothing
  | Bool
otherwise
  = Name -> Maybe Name
forall a. a -> Maybe a
Just Name
rep_nm
tyConRepName_maybe TyCon
_ = Maybe Name
forall a. Maybe a
Nothing

-- | Make a 'Name' for the 'Typeable' representation of the given wired-in type
mkPrelTyConRepName :: Name -> TyConRepName
-- See Note [Grand plan for Typeable] in "GHC.Tc.Instance.Typeable".
mkPrelTyConRepName :: Name -> Name
mkPrelTyConRepName Name
tc_name  -- Prelude tc_name is always External,
                            -- so nameModule will work
  = Unique -> Module -> OccName -> SrcSpan -> Name
mkExternalName Unique
rep_uniq Module
rep_mod OccName
rep_occ (Name -> SrcSpan
nameSrcSpan Name
tc_name)
  where
    name_occ :: OccName
name_occ  = Name -> OccName
nameOccName Name
tc_name
    name_mod :: Module
name_mod  = (() :: Constraint) => Name -> Module
Name -> Module
nameModule  Name
tc_name
    name_uniq :: Unique
name_uniq = Name -> Unique
nameUnique  Name
tc_name
    rep_uniq :: Unique
rep_uniq | OccName -> Bool
isTcOcc OccName
name_occ = Unique -> Unique
tyConRepNameUnique   Unique
name_uniq
             | Bool
otherwise        = Unique -> Unique
dataConTyRepNameUnique Unique
name_uniq
    (Module
rep_mod, OccName
rep_occ) = Module -> OccName -> (Module, OccName)
tyConRepModOcc Module
name_mod OccName
name_occ

-- | The name (and defining module) for the Typeable representation (TyCon) of a
-- type constructor.
--
-- See Note [Grand plan for Typeable] in "GHC.Tc.Instance.Typeable".
tyConRepModOcc :: Module -> OccName -> (Module, OccName)
tyConRepModOcc :: Module -> OccName -> (Module, OccName)
tyConRepModOcc Module
tc_module OccName
tc_occ = (Module
rep_module, OccName -> OccName
mkTyConRepOcc OccName
tc_occ)
  where
    rep_module :: Module
rep_module
      | Module
tc_module Module -> Module -> Bool
forall a. Eq a => a -> a -> Bool
== Module
gHC_PRIM = Module
gHC_TYPES
      | Bool
otherwise             = Module
tc_module


{- *********************************************************************
*                                                                      *
                 PrimRep
*                                                                      *
************************************************************************

Note [rep swamp]
~~~~~~~~~~~~~~~~
GHC has a rich selection of types that represent "primitive types" of
one kind or another.  Each of them makes a different set of
distinctions, and mostly the differences are for good reasons,
although it's probably true that we could merge some of these.

Roughly in order of "includes more information":

 - A Width ("GHC.Cmm.Type") is simply a binary value with the specified
   number of bits.  It may represent a signed or unsigned integer, a
   floating-point value, or an address.

    data Width = W8 | W16 | W32 | W64  | W128

 - Size, which is used in the native code generator, is Width +
   floating point information.

   data Size = II8 | II16 | II32 | II64 | FF32 | FF64

   it is necessary because e.g. the instruction to move a 64-bit float
   on x86 (movsd) is different from the instruction to move a 64-bit
   integer (movq), so the mov instruction is parameterised by Size.

 - CmmType wraps Width with more information: GC ptr, float, or
   other value.

    data CmmType = CmmType CmmCat Width

    data CmmCat     -- "Category" (not exported)
       = GcPtrCat   -- GC pointer
       | BitsCat    -- Non-pointer
       | FloatCat   -- Float

   It is important to have GcPtr information in Cmm, since we generate
   info tables containing pointerhood for the GC from this.  As for
   why we have float (and not signed/unsigned) here, see Note [Signed
   vs unsigned].

 - ArgRep makes only the distinctions necessary for the call and
   return conventions of the STG machine.  It is essentially CmmType
   + void.

 - PrimRep makes a few more distinctions than ArgRep: it divides
   non-GC-pointers into signed/unsigned and addresses, information
   that is necessary for passing these values to foreign functions.

There's another tension here: whether the type encodes its size in
bytes, or whether its size depends on the machine word size.  Width
and CmmType have the size built-in, whereas ArgRep and PrimRep do not.

This means to turn an ArgRep/PrimRep into a CmmType requires DynFlags.

On the other hand, CmmType includes some "nonsense" values, such as
CmmType GcPtrCat W32 on a 64-bit machine.

The PrimRep type is closely related to the user-visible RuntimeRep type.
See Note [RuntimeRep and PrimRep] in GHC.Types.RepType.

-}

-- | A 'PrimRep' is an abstraction of a type.  It contains information that
-- the code generator needs in order to pass arguments, return results,
-- and store values of this type. See also Note [RuntimeRep and PrimRep] in
-- "GHC.Types.RepType" and Note [VoidRep] in "GHC.Types.RepType".
data PrimRep
  = VoidRep
  | LiftedRep
  | UnliftedRep   -- ^ Unlifted pointer
  | Int8Rep       -- ^ Signed, 8-bit value
  | Int16Rep      -- ^ Signed, 16-bit value
  | Int32Rep      -- ^ Signed, 32-bit value
  | Int64Rep      -- ^ Signed, 64 bit value
  | IntRep        -- ^ Signed, word-sized value
  | Word8Rep      -- ^ Unsigned, 8 bit value
  | Word16Rep     -- ^ Unsigned, 16 bit value
  | Word32Rep     -- ^ Unsigned, 32 bit value
  | Word64Rep     -- ^ Unsigned, 64 bit value
  | WordRep       -- ^ Unsigned, word-sized value
  | AddrRep       -- ^ A pointer, but /not/ to a Haskell value (use '(Un)liftedRep')
  | FloatRep
  | DoubleRep
  | VecRep Int PrimElemRep  -- ^ A vector
  deriving( Typeable PrimRep
Typeable PrimRep
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> PrimRep -> c PrimRep)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c PrimRep)
-> (PrimRep -> Constr)
-> (PrimRep -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c PrimRep))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c PrimRep))
-> ((forall b. Data b => b -> b) -> PrimRep -> PrimRep)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> PrimRep -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> PrimRep -> r)
-> (forall u. (forall d. Data d => d -> u) -> PrimRep -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> PrimRep -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> PrimRep -> m PrimRep)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> PrimRep -> m PrimRep)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> PrimRep -> m PrimRep)
-> Data PrimRep
PrimRep -> Constr
PrimRep -> DataType
(forall b. Data b => b -> b) -> PrimRep -> PrimRep
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> PrimRep -> u
forall u. (forall d. Data d => d -> u) -> PrimRep -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> PrimRep -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> PrimRep -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> PrimRep -> m PrimRep
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> PrimRep -> m PrimRep
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PrimRep
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> PrimRep -> c PrimRep
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c PrimRep)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c PrimRep)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> PrimRep -> c PrimRep
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> PrimRep -> c PrimRep
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PrimRep
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PrimRep
$ctoConstr :: PrimRep -> Constr
toConstr :: PrimRep -> Constr
$cdataTypeOf :: PrimRep -> DataType
dataTypeOf :: PrimRep -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c PrimRep)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c PrimRep)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c PrimRep)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c PrimRep)
$cgmapT :: (forall b. Data b => b -> b) -> PrimRep -> PrimRep
gmapT :: (forall b. Data b => b -> b) -> PrimRep -> PrimRep
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> PrimRep -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> PrimRep -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> PrimRep -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> PrimRep -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> PrimRep -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> PrimRep -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> PrimRep -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> PrimRep -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> PrimRep -> m PrimRep
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> PrimRep -> m PrimRep
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> PrimRep -> m PrimRep
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> PrimRep -> m PrimRep
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> PrimRep -> m PrimRep
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> PrimRep -> m PrimRep
Data.Data, PrimRep -> PrimRep -> Bool
(PrimRep -> PrimRep -> Bool)
-> (PrimRep -> PrimRep -> Bool) -> Eq PrimRep
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PrimRep -> PrimRep -> Bool
== :: PrimRep -> PrimRep -> Bool
$c/= :: PrimRep -> PrimRep -> Bool
/= :: PrimRep -> PrimRep -> Bool
Eq, Eq PrimRep
Eq PrimRep
-> (PrimRep -> PrimRep -> Ordering)
-> (PrimRep -> PrimRep -> Bool)
-> (PrimRep -> PrimRep -> Bool)
-> (PrimRep -> PrimRep -> Bool)
-> (PrimRep -> PrimRep -> Bool)
-> (PrimRep -> PrimRep -> PrimRep)
-> (PrimRep -> PrimRep -> PrimRep)
-> Ord PrimRep
PrimRep -> PrimRep -> Bool
PrimRep -> PrimRep -> Ordering
PrimRep -> PrimRep -> PrimRep
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: PrimRep -> PrimRep -> Ordering
compare :: PrimRep -> PrimRep -> Ordering
$c< :: PrimRep -> PrimRep -> Bool
< :: PrimRep -> PrimRep -> Bool
$c<= :: PrimRep -> PrimRep -> Bool
<= :: PrimRep -> PrimRep -> Bool
$c> :: PrimRep -> PrimRep -> Bool
> :: PrimRep -> PrimRep -> Bool
$c>= :: PrimRep -> PrimRep -> Bool
>= :: PrimRep -> PrimRep -> Bool
$cmax :: PrimRep -> PrimRep -> PrimRep
max :: PrimRep -> PrimRep -> PrimRep
$cmin :: PrimRep -> PrimRep -> PrimRep
min :: PrimRep -> PrimRep -> PrimRep
Ord, Int -> PrimRep -> ShowS
[PrimRep] -> ShowS
PrimRep -> String
(Int -> PrimRep -> ShowS)
-> (PrimRep -> String) -> ([PrimRep] -> ShowS) -> Show PrimRep
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PrimRep -> ShowS
showsPrec :: Int -> PrimRep -> ShowS
$cshow :: PrimRep -> String
show :: PrimRep -> String
$cshowList :: [PrimRep] -> ShowS
showList :: [PrimRep] -> ShowS
Show )

data PrimElemRep
  = Int8ElemRep
  | Int16ElemRep
  | Int32ElemRep
  | Int64ElemRep
  | Word8ElemRep
  | Word16ElemRep
  | Word32ElemRep
  | Word64ElemRep
  | FloatElemRep
  | DoubleElemRep
   deriving( Typeable PrimElemRep
Typeable PrimElemRep
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> PrimElemRep -> c PrimElemRep)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c PrimElemRep)
-> (PrimElemRep -> Constr)
-> (PrimElemRep -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c PrimElemRep))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c PrimElemRep))
-> ((forall b. Data b => b -> b) -> PrimElemRep -> PrimElemRep)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> PrimElemRep -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> PrimElemRep -> r)
-> (forall u. (forall d. Data d => d -> u) -> PrimElemRep -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> PrimElemRep -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> PrimElemRep -> m PrimElemRep)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> PrimElemRep -> m PrimElemRep)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> PrimElemRep -> m PrimElemRep)
-> Data PrimElemRep
PrimElemRep -> Constr
PrimElemRep -> DataType
(forall b. Data b => b -> b) -> PrimElemRep -> PrimElemRep
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> PrimElemRep -> u
forall u. (forall d. Data d => d -> u) -> PrimElemRep -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> PrimElemRep -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> PrimElemRep -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> PrimElemRep -> m PrimElemRep
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> PrimElemRep -> m PrimElemRep
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PrimElemRep
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> PrimElemRep -> c PrimElemRep
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c PrimElemRep)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c PrimElemRep)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> PrimElemRep -> c PrimElemRep
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> PrimElemRep -> c PrimElemRep
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PrimElemRep
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PrimElemRep
$ctoConstr :: PrimElemRep -> Constr
toConstr :: PrimElemRep -> Constr
$cdataTypeOf :: PrimElemRep -> DataType
dataTypeOf :: PrimElemRep -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c PrimElemRep)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c PrimElemRep)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c PrimElemRep)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c PrimElemRep)
$cgmapT :: (forall b. Data b => b -> b) -> PrimElemRep -> PrimElemRep
gmapT :: (forall b. Data b => b -> b) -> PrimElemRep -> PrimElemRep
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> PrimElemRep -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> PrimElemRep -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> PrimElemRep -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> PrimElemRep -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> PrimElemRep -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> PrimElemRep -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> PrimElemRep -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> PrimElemRep -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> PrimElemRep -> m PrimElemRep
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> PrimElemRep -> m PrimElemRep
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> PrimElemRep -> m PrimElemRep
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> PrimElemRep -> m PrimElemRep
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> PrimElemRep -> m PrimElemRep
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> PrimElemRep -> m PrimElemRep
Data.Data, PrimElemRep -> PrimElemRep -> Bool
(PrimElemRep -> PrimElemRep -> Bool)
-> (PrimElemRep -> PrimElemRep -> Bool) -> Eq PrimElemRep
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PrimElemRep -> PrimElemRep -> Bool
== :: PrimElemRep -> PrimElemRep -> Bool
$c/= :: PrimElemRep -> PrimElemRep -> Bool
/= :: PrimElemRep -> PrimElemRep -> Bool
Eq, Eq PrimElemRep
Eq PrimElemRep
-> (PrimElemRep -> PrimElemRep -> Ordering)
-> (PrimElemRep -> PrimElemRep -> Bool)
-> (PrimElemRep -> PrimElemRep -> Bool)
-> (PrimElemRep -> PrimElemRep -> Bool)
-> (PrimElemRep -> PrimElemRep -> Bool)
-> (PrimElemRep -> PrimElemRep -> PrimElemRep)
-> (PrimElemRep -> PrimElemRep -> PrimElemRep)
-> Ord PrimElemRep
PrimElemRep -> PrimElemRep -> Bool
PrimElemRep -> PrimElemRep -> Ordering
PrimElemRep -> PrimElemRep -> PrimElemRep
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: PrimElemRep -> PrimElemRep -> Ordering
compare :: PrimElemRep -> PrimElemRep -> Ordering
$c< :: PrimElemRep -> PrimElemRep -> Bool
< :: PrimElemRep -> PrimElemRep -> Bool
$c<= :: PrimElemRep -> PrimElemRep -> Bool
<= :: PrimElemRep -> PrimElemRep -> Bool
$c> :: PrimElemRep -> PrimElemRep -> Bool
> :: PrimElemRep -> PrimElemRep -> Bool
$c>= :: PrimElemRep -> PrimElemRep -> Bool
>= :: PrimElemRep -> PrimElemRep -> Bool
$cmax :: PrimElemRep -> PrimElemRep -> PrimElemRep
max :: PrimElemRep -> PrimElemRep -> PrimElemRep
$cmin :: PrimElemRep -> PrimElemRep -> PrimElemRep
min :: PrimElemRep -> PrimElemRep -> PrimElemRep
Ord, Int -> PrimElemRep -> ShowS
[PrimElemRep] -> ShowS
PrimElemRep -> String
(Int -> PrimElemRep -> ShowS)
-> (PrimElemRep -> String)
-> ([PrimElemRep] -> ShowS)
-> Show PrimElemRep
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PrimElemRep -> ShowS
showsPrec :: Int -> PrimElemRep -> ShowS
$cshow :: PrimElemRep -> String
show :: PrimElemRep -> String
$cshowList :: [PrimElemRep] -> ShowS
showList :: [PrimElemRep] -> ShowS
Show, Int -> PrimElemRep
PrimElemRep -> Int
PrimElemRep -> [PrimElemRep]
PrimElemRep -> PrimElemRep
PrimElemRep -> PrimElemRep -> [PrimElemRep]
PrimElemRep -> PrimElemRep -> PrimElemRep -> [PrimElemRep]
(PrimElemRep -> PrimElemRep)
-> (PrimElemRep -> PrimElemRep)
-> (Int -> PrimElemRep)
-> (PrimElemRep -> Int)
-> (PrimElemRep -> [PrimElemRep])
-> (PrimElemRep -> PrimElemRep -> [PrimElemRep])
-> (PrimElemRep -> PrimElemRep -> [PrimElemRep])
-> (PrimElemRep -> PrimElemRep -> PrimElemRep -> [PrimElemRep])
-> Enum PrimElemRep
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: PrimElemRep -> PrimElemRep
succ :: PrimElemRep -> PrimElemRep
$cpred :: PrimElemRep -> PrimElemRep
pred :: PrimElemRep -> PrimElemRep
$ctoEnum :: Int -> PrimElemRep
toEnum :: Int -> PrimElemRep
$cfromEnum :: PrimElemRep -> Int
fromEnum :: PrimElemRep -> Int
$cenumFrom :: PrimElemRep -> [PrimElemRep]
enumFrom :: PrimElemRep -> [PrimElemRep]
$cenumFromThen :: PrimElemRep -> PrimElemRep -> [PrimElemRep]
enumFromThen :: PrimElemRep -> PrimElemRep -> [PrimElemRep]
$cenumFromTo :: PrimElemRep -> PrimElemRep -> [PrimElemRep]
enumFromTo :: PrimElemRep -> PrimElemRep -> [PrimElemRep]
$cenumFromThenTo :: PrimElemRep -> PrimElemRep -> PrimElemRep -> [PrimElemRep]
enumFromThenTo :: PrimElemRep -> PrimElemRep -> PrimElemRep -> [PrimElemRep]
Enum )

instance Outputable PrimRep where
  ppr :: PrimRep -> SDoc
ppr PrimRep
r = String -> SDoc
text (PrimRep -> String
forall a. Show a => a -> String
show PrimRep
r)

instance Outputable PrimElemRep where
  ppr :: PrimElemRep -> SDoc
ppr PrimElemRep
r = String -> SDoc
text (PrimElemRep -> String
forall a. Show a => a -> String
show PrimElemRep
r)

instance Binary PrimRep where
  put_ :: BinHandle -> PrimRep -> IO ()
put_ BinHandle
bh PrimRep
VoidRep        = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0
  put_ BinHandle
bh PrimRep
LiftedRep      = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1
  put_ BinHandle
bh PrimRep
UnliftedRep    = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
2
  put_ BinHandle
bh PrimRep
Int8Rep        = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
3
  put_ BinHandle
bh PrimRep
Int16Rep       = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
4
  put_ BinHandle
bh PrimRep
Int32Rep       = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
5
  put_ BinHandle
bh PrimRep
Int64Rep       = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
6
  put_ BinHandle
bh PrimRep
IntRep         = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
7
  put_ BinHandle
bh PrimRep
Word8Rep       = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
8
  put_ BinHandle
bh PrimRep
Word16Rep      = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
9
  put_ BinHandle
bh PrimRep
Word32Rep      = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
10
  put_ BinHandle
bh PrimRep
Word64Rep      = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
11
  put_ BinHandle
bh PrimRep
WordRep        = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
12
  put_ BinHandle
bh PrimRep
AddrRep        = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
13
  put_ BinHandle
bh PrimRep
FloatRep       = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
14
  put_ BinHandle
bh PrimRep
DoubleRep      = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
15
  put_ BinHandle
bh (VecRep Int
n PrimElemRep
per) = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
16 IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> BinHandle -> Int -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh Int
n IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> BinHandle -> PrimElemRep -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh PrimElemRep
per
  get :: BinHandle -> IO PrimRep
get  BinHandle
bh = do
    Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
    case Word8
h of
      Word8
0  -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
VoidRep
      Word8
1  -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
LiftedRep
      Word8
2  -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
UnliftedRep
      Word8
3  -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
Int8Rep
      Word8
4  -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
Int16Rep
      Word8
5  -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
Int32Rep
      Word8
6  -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
Int64Rep
      Word8
7  -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
IntRep
      Word8
8  -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
Word8Rep
      Word8
9  -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
Word16Rep
      Word8
10 -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
Word32Rep
      Word8
11 -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
Word64Rep
      Word8
12 -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
WordRep
      Word8
13 -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
AddrRep
      Word8
14 -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
FloatRep
      Word8
15 -> PrimRep -> IO PrimRep
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimRep
DoubleRep
      Word8
16 -> Int -> PrimElemRep -> PrimRep
VecRep (Int -> PrimElemRep -> PrimRep)
-> IO Int -> IO (PrimElemRep -> PrimRep)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BinHandle -> IO Int
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh IO (PrimElemRep -> PrimRep) -> IO PrimElemRep -> IO PrimRep
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> BinHandle -> IO PrimElemRep
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
      Word8
_  -> String -> SDoc -> IO PrimRep
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"Binary:PrimRep" (Int -> SDoc
int (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
h))

instance Binary PrimElemRep where
  put_ :: BinHandle -> PrimElemRep -> IO ()
put_ BinHandle
bh PrimElemRep
per = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PrimElemRep -> Int
forall a. Enum a => a -> Int
fromEnum PrimElemRep
per))
  get :: BinHandle -> IO PrimElemRep
get  BinHandle
bh = Int -> PrimElemRep
forall a. Enum a => Int -> a
toEnum (Int -> PrimElemRep) -> (Word8 -> Int) -> Word8 -> PrimElemRep
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> PrimElemRep) -> IO Word8 -> IO PrimElemRep
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BinHandle -> IO Word8
getByte BinHandle
bh

isVoidRep :: PrimRep -> Bool
isVoidRep :: PrimRep -> Bool
isVoidRep PrimRep
VoidRep = Bool
True
isVoidRep PrimRep
_other  = Bool
False

isGcPtrRep :: PrimRep -> Bool
isGcPtrRep :: PrimRep -> Bool
isGcPtrRep PrimRep
LiftedRep   = Bool
True
isGcPtrRep PrimRep
UnliftedRep = Bool
True
isGcPtrRep PrimRep
_           = Bool
False

-- A PrimRep is compatible with another iff one can be coerced to the other.
-- See Note [Bad unsafe coercion] in GHC.Core.Lint for when are two types coercible.
primRepCompatible :: Platform -> PrimRep -> PrimRep -> Bool
primRepCompatible :: Platform -> PrimRep -> PrimRep -> Bool
primRepCompatible Platform
platform PrimRep
rep1 PrimRep
rep2 =
    (PrimRep -> Bool
isUnboxed PrimRep
rep1 Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== PrimRep -> Bool
isUnboxed PrimRep
rep2) Bool -> Bool -> Bool
&&
    (Platform -> PrimRep -> Int
primRepSizeB Platform
platform PrimRep
rep1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Platform -> PrimRep -> Int
primRepSizeB Platform
platform PrimRep
rep2) Bool -> Bool -> Bool
&&
    (PrimRep -> Maybe Bool
primRepIsFloat PrimRep
rep1 Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== PrimRep -> Maybe Bool
primRepIsFloat PrimRep
rep2)
  where
    isUnboxed :: PrimRep -> Bool
isUnboxed = Bool -> Bool
not (Bool -> Bool) -> (PrimRep -> Bool) -> PrimRep -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrimRep -> Bool
isGcPtrRep

-- More general version of `primRepCompatible` for types represented by zero or
-- more than one PrimReps.
primRepsCompatible :: Platform -> [PrimRep] -> [PrimRep] -> Bool
primRepsCompatible :: Platform -> [PrimRep] -> [PrimRep] -> Bool
primRepsCompatible Platform
platform [PrimRep]
reps1 [PrimRep]
reps2 =
    [PrimRep] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [PrimRep]
reps1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [PrimRep] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [PrimRep]
reps2 Bool -> Bool -> Bool
&&
    [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ((PrimRep -> PrimRep -> Bool) -> [PrimRep] -> [PrimRep] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (Platform -> PrimRep -> PrimRep -> Bool
primRepCompatible Platform
platform) [PrimRep]
reps1 [PrimRep]
reps2)

-- | The size of a 'PrimRep' in bytes.
--
-- This applies also when used in a constructor, where we allow packing the
-- fields. For instance, in @data Foo = Foo Float# Float#@ the two fields will
-- take only 8 bytes, which for 64-bit arch will be equal to 1 word.
-- See also mkVirtHeapOffsetsWithPadding for details of how data fields are
-- laid out.
primRepSizeB :: Platform -> PrimRep -> Int
primRepSizeB :: Platform -> PrimRep -> Int
primRepSizeB Platform
platform = \case
   PrimRep
IntRep           -> Platform -> Int
platformWordSizeInBytes Platform
platform
   PrimRep
WordRep          -> Platform -> Int
platformWordSizeInBytes Platform
platform
   PrimRep
Int8Rep          -> Int
1
   PrimRep
Int16Rep         -> Int
2
   PrimRep
Int32Rep         -> Int
4
   PrimRep
Int64Rep         -> Int
8
   PrimRep
Word8Rep         -> Int
1
   PrimRep
Word16Rep        -> Int
2
   PrimRep
Word32Rep        -> Int
4
   PrimRep
Word64Rep        -> Int
8
   PrimRep
FloatRep         -> Int
fLOAT_SIZE
   PrimRep
DoubleRep        -> Int
dOUBLE_SIZE
   PrimRep
AddrRep          -> Platform -> Int
platformWordSizeInBytes Platform
platform
   PrimRep
LiftedRep        -> Platform -> Int
platformWordSizeInBytes Platform
platform
   PrimRep
UnliftedRep      -> Platform -> Int
platformWordSizeInBytes Platform
platform
   PrimRep
VoidRep          -> Int
0
   (VecRep Int
len PrimElemRep
rep) -> Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
* Platform -> PrimElemRep -> Int
primElemRepSizeB Platform
platform PrimElemRep
rep

primElemRepSizeB :: Platform -> PrimElemRep -> Int
primElemRepSizeB :: Platform -> PrimElemRep -> Int
primElemRepSizeB Platform
platform = Platform -> PrimRep -> Int
primRepSizeB Platform
platform (PrimRep -> Int) -> (PrimElemRep -> PrimRep) -> PrimElemRep -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrimElemRep -> PrimRep
primElemRepToPrimRep

primElemRepToPrimRep :: PrimElemRep -> PrimRep
primElemRepToPrimRep :: PrimElemRep -> PrimRep
primElemRepToPrimRep PrimElemRep
Int8ElemRep   = PrimRep
Int8Rep
primElemRepToPrimRep PrimElemRep
Int16ElemRep  = PrimRep
Int16Rep
primElemRepToPrimRep PrimElemRep
Int32ElemRep  = PrimRep
Int32Rep
primElemRepToPrimRep PrimElemRep
Int64ElemRep  = PrimRep
Int64Rep
primElemRepToPrimRep PrimElemRep
Word8ElemRep  = PrimRep
Word8Rep
primElemRepToPrimRep PrimElemRep
Word16ElemRep = PrimRep
Word16Rep
primElemRepToPrimRep PrimElemRep
Word32ElemRep = PrimRep
Word32Rep
primElemRepToPrimRep PrimElemRep
Word64ElemRep = PrimRep
Word64Rep
primElemRepToPrimRep PrimElemRep
FloatElemRep  = PrimRep
FloatRep
primElemRepToPrimRep PrimElemRep
DoubleElemRep = PrimRep
DoubleRep

-- | Return if Rep stands for floating type,
-- returns Nothing for vector types.
primRepIsFloat :: PrimRep -> Maybe Bool
primRepIsFloat :: PrimRep -> Maybe Bool
primRepIsFloat  PrimRep
FloatRep     = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
primRepIsFloat  PrimRep
DoubleRep    = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
primRepIsFloat  (VecRep Int
_ PrimElemRep
_) = Maybe Bool
forall a. Maybe a
Nothing
primRepIsFloat  PrimRep
_            = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False

{-
************************************************************************
*                                                                      *
                             Field labels
*                                                                      *
************************************************************************
-}

-- | The labels for the fields of this particular 'TyCon'
tyConFieldLabels :: TyCon -> [FieldLabel]
tyConFieldLabels :: TyCon -> [FieldLabel]
tyConFieldLabels TyCon
tc = FieldLabelEnv -> [FieldLabel]
forall a. DFastStringEnv a -> [a]
dFsEnvElts (FieldLabelEnv -> [FieldLabel]) -> FieldLabelEnv -> [FieldLabel]
forall a b. (a -> b) -> a -> b
$ TyCon -> FieldLabelEnv
tyConFieldLabelEnv TyCon
tc

-- | The labels for the fields of this particular 'TyCon'
tyConFieldLabelEnv :: TyCon -> FieldLabelEnv
tyConFieldLabelEnv :: TyCon -> FieldLabelEnv
tyConFieldLabelEnv TyCon
tc
  | TyCon -> Bool
isAlgTyCon TyCon
tc = TyCon -> FieldLabelEnv
algTcFields TyCon
tc
  | Bool
otherwise     = FieldLabelEnv
forall a. DFastStringEnv a
emptyDFsEnv

-- | Look up a field label belonging to this 'TyCon'
lookupTyConFieldLabel :: FieldLabelString -> TyCon -> Maybe FieldLabel
lookupTyConFieldLabel :: FieldLabelString -> TyCon -> Maybe FieldLabel
lookupTyConFieldLabel FieldLabelString
lbl TyCon
tc = FieldLabelEnv -> FieldLabelString -> Maybe FieldLabel
forall a. DFastStringEnv a -> FieldLabelString -> Maybe a
lookupDFsEnv (TyCon -> FieldLabelEnv
tyConFieldLabelEnv TyCon
tc) FieldLabelString
lbl

-- | Make a map from strings to FieldLabels from all the data
-- constructors of this algebraic tycon
fieldsOfAlgTcRhs :: AlgTyConRhs -> FieldLabelEnv
fieldsOfAlgTcRhs :: AlgTyConRhs -> FieldLabelEnv
fieldsOfAlgTcRhs AlgTyConRhs
rhs = [(FieldLabelString, FieldLabel)] -> FieldLabelEnv
forall a. [(FieldLabelString, a)] -> DFastStringEnv a
mkDFsEnv [ (FieldLabel -> FieldLabelString
flLabel FieldLabel
fl, FieldLabel
fl)
                                | FieldLabel
fl <- [DataCon] -> [FieldLabel]
forall {t :: * -> *}. Foldable t => t DataCon -> [FieldLabel]
dataConsFields (AlgTyConRhs -> [DataCon]
visibleDataCons AlgTyConRhs
rhs) ]
  where
    -- Duplicates in this list will be removed by 'mkFsEnv'
    dataConsFields :: t DataCon -> [FieldLabel]
dataConsFields t DataCon
dcs = (DataCon -> [FieldLabel]) -> t DataCon -> [FieldLabel]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap DataCon -> [FieldLabel]
dataConFieldLabels t DataCon
dcs


{-
************************************************************************
*                                                                      *
\subsection{TyCon Construction}
*                                                                      *
************************************************************************

Note: the TyCon constructors all take a Kind as one argument, even though
they could, in principle, work out their Kind from their other arguments.
But to do so they need functions from Types, and that makes a nasty
module mutual-recursion.  And they aren't called from many places.
So we compromise, and move their Kind calculation to the call site.
-}

-- | Given the name of the function type constructor and it's kind, create the
-- corresponding 'TyCon'. It is recommended to use 'GHC.Builtin.Types.funTyCon' if you want
-- this functionality
mkFunTyCon :: Name -> [TyConBinder] -> Name -> TyCon
mkFunTyCon :: Name -> [TyConBinder] -> Name -> TyCon
mkFunTyCon Name
name [TyConBinder]
binders Name
rep_nm
  = let tc :: TyCon
tc =
          FunTyCon {
              tyConUnique :: Unique
tyConUnique  = Name -> Unique
nameUnique Name
name,
              tyConName :: Name
tyConName    = Name
name,
              tyConBinders :: [TyConBinder]
tyConBinders = [TyConBinder]
binders,
              tyConResKind :: Kind
tyConResKind = Kind
liftedTypeKind,
              tyConKind :: Kind
tyConKind    = [TyConBinder] -> Kind -> Kind
mkTyConKind [TyConBinder]
binders Kind
liftedTypeKind,
              tyConArity :: Int
tyConArity   = [TyConBinder] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TyConBinder]
binders,
              tyConNullaryTy :: Kind
tyConNullaryTy = TyCon -> Kind
mkNakedTyConTy TyCon
tc,
              tcRepName :: Name
tcRepName    = Name
rep_nm
          }
    in TyCon
tc

-- | This is the making of an algebraic 'TyCon'.
mkAlgTyCon :: Name
           -> [TyConBinder]  -- ^ Binders of the 'TyCon'
           -> Kind              -- ^ Result kind
           -> [Role]            -- ^ The roles for each TyVar
           -> Maybe CType       -- ^ The C type this type corresponds to
                                --   when using the CAPI FFI
           -> [PredType]        -- ^ Stupid theta: see 'algTcStupidTheta'
           -> AlgTyConRhs       -- ^ Information about data constructors
           -> AlgTyConFlav      -- ^ What flavour is it?
                                -- (e.g. vanilla, type family)
           -> Bool              -- ^ Was the 'TyCon' declared with GADT syntax?
           -> TyCon
mkAlgTyCon :: Name
-> [TyConBinder]
-> Kind
-> [Role]
-> Maybe CType
-> [Kind]
-> AlgTyConRhs
-> AlgTyConFlav
-> Bool
-> TyCon
mkAlgTyCon Name
name [TyConBinder]
binders Kind
res_kind [Role]
roles Maybe CType
cType [Kind]
stupid AlgTyConRhs
rhs AlgTyConFlav
parent Bool
gadt_syn
  = let tc :: TyCon
tc =
          AlgTyCon {
              tyConName :: Name
tyConName        = Name
name,
              tyConUnique :: Unique
tyConUnique      = Name -> Unique
nameUnique Name
name,
              tyConBinders :: [TyConBinder]
tyConBinders     = [TyConBinder]
binders,
              tyConResKind :: Kind
tyConResKind     = Kind
res_kind,
              tyConKind :: Kind
tyConKind        = [TyConBinder] -> Kind -> Kind
mkTyConKind [TyConBinder]
binders Kind
res_kind,
              tyConArity :: Int
tyConArity       = [TyConBinder] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TyConBinder]
binders,
              tyConNullaryTy :: Kind
tyConNullaryTy   = TyCon -> Kind
mkNakedTyConTy TyCon
tc,
              tyConTyVars :: [TyVar]
tyConTyVars      = [TyConBinder] -> [TyVar]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [TyConBinder]
binders,
              tcRoles :: [Role]
tcRoles          = [Role]
roles,
              tyConCType :: Maybe CType
tyConCType       = Maybe CType
cType,
              algTcStupidTheta :: [Kind]
algTcStupidTheta = [Kind]
stupid,
              algTcRhs :: AlgTyConRhs
algTcRhs         = AlgTyConRhs
rhs,
              algTcFields :: FieldLabelEnv
algTcFields      = AlgTyConRhs -> FieldLabelEnv
fieldsOfAlgTcRhs AlgTyConRhs
rhs,
              algTcFlavour :: AlgTyConFlav
algTcFlavour     = Bool -> SDoc -> AlgTyConFlav -> AlgTyConFlav
forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr (Name -> AlgTyConFlav -> Bool
okParent Name
name AlgTyConFlav
parent) (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
name SDoc -> SDoc -> SDoc
$$ AlgTyConFlav -> SDoc
forall a. Outputable a => a -> SDoc
ppr AlgTyConFlav
parent) AlgTyConFlav
parent,
              algTcGadtSyntax :: Bool
algTcGadtSyntax  = Bool
gadt_syn
          }
    in TyCon
tc

-- | Simpler specialization of 'mkAlgTyCon' for classes
mkClassTyCon :: Name -> [TyConBinder]
             -> [Role] -> AlgTyConRhs -> Class
             -> Name -> TyCon
mkClassTyCon :: Name
-> [TyConBinder] -> [Role] -> AlgTyConRhs -> Class -> Name -> TyCon
mkClassTyCon Name
name [TyConBinder]
binders [Role]
roles AlgTyConRhs
rhs Class
clas Name
tc_rep_name
  = Name
-> [TyConBinder]
-> Kind
-> [Role]
-> Maybe CType
-> [Kind]
-> AlgTyConRhs
-> AlgTyConFlav
-> Bool
-> TyCon
mkAlgTyCon Name
name [TyConBinder]
binders Kind
constraintKind [Role]
roles Maybe CType
forall a. Maybe a
Nothing [] AlgTyConRhs
rhs
               (Class -> Name -> AlgTyConFlav
ClassTyCon Class
clas Name
tc_rep_name)
               Bool
False

mkTupleTyCon :: Name
             -> [TyConBinder]
             -> Kind    -- ^ Result kind of the 'TyCon'
             -> Arity   -- ^ Arity of the tuple 'TyCon'
             -> DataCon
             -> TupleSort    -- ^ Whether the tuple is boxed or unboxed
             -> AlgTyConFlav
             -> TyCon
mkTupleTyCon :: Name
-> [TyConBinder]
-> Kind
-> Int
-> DataCon
-> TupleSort
-> AlgTyConFlav
-> TyCon
mkTupleTyCon Name
name [TyConBinder]
binders Kind
res_kind Int
arity DataCon
con TupleSort
sort AlgTyConFlav
parent
  = let tc :: TyCon
tc =
          AlgTyCon {
              tyConUnique :: Unique
tyConUnique      = Name -> Unique
nameUnique Name
name,
              tyConName :: Name
tyConName        = Name
name,
              tyConBinders :: [TyConBinder]
tyConBinders     = [TyConBinder]
binders,
              tyConTyVars :: [TyVar]
tyConTyVars      = [TyConBinder] -> [TyVar]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [TyConBinder]
binders,
              tyConResKind :: Kind
tyConResKind     = Kind
res_kind,
              tyConKind :: Kind
tyConKind        = [TyConBinder] -> Kind -> Kind
mkTyConKind [TyConBinder]
binders Kind
res_kind,
              tyConArity :: Int
tyConArity       = Int
arity,
              tyConNullaryTy :: Kind
tyConNullaryTy   = TyCon -> Kind
mkNakedTyConTy TyCon
tc,
              tcRoles :: [Role]
tcRoles          = Int -> Role -> [Role]
forall a. Int -> a -> [a]
replicate Int
arity Role
Representational,
              tyConCType :: Maybe CType
tyConCType       = Maybe CType
forall a. Maybe a
Nothing,
              algTcGadtSyntax :: Bool
algTcGadtSyntax  = Bool
False,
              algTcStupidTheta :: [Kind]
algTcStupidTheta = [],
              algTcRhs :: AlgTyConRhs
algTcRhs         = TupleTyCon { data_con :: DataCon
data_con = DataCon
con,
                                              tup_sort :: TupleSort
tup_sort = TupleSort
sort },
              algTcFields :: FieldLabelEnv
algTcFields      = FieldLabelEnv
forall a. DFastStringEnv a
emptyDFsEnv,
              algTcFlavour :: AlgTyConFlav
algTcFlavour     = AlgTyConFlav
parent
          }
    in TyCon
tc

mkSumTyCon :: Name
             -> [TyConBinder]
             -> Kind    -- ^ Kind of the resulting 'TyCon'
             -> Arity   -- ^ Arity of the sum
             -> [TyVar] -- ^ 'TyVar's scoped over: see 'tyConTyVars'
             -> [DataCon]
             -> AlgTyConFlav
             -> TyCon
mkSumTyCon :: Name
-> [TyConBinder]
-> Kind
-> Int
-> [TyVar]
-> [DataCon]
-> AlgTyConFlav
-> TyCon
mkSumTyCon Name
name [TyConBinder]
binders Kind
res_kind Int
arity [TyVar]
tyvars [DataCon]
cons AlgTyConFlav
parent
  = let tc :: TyCon
tc =
          AlgTyCon {
              tyConUnique :: Unique
tyConUnique      = Name -> Unique
nameUnique Name
name,
              tyConName :: Name
tyConName        = Name
name,
              tyConBinders :: [TyConBinder]
tyConBinders     = [TyConBinder]
binders,
              tyConTyVars :: [TyVar]
tyConTyVars      = [TyVar]
tyvars,
              tyConResKind :: Kind
tyConResKind     = Kind
res_kind,
              tyConKind :: Kind
tyConKind        = [TyConBinder] -> Kind -> Kind
mkTyConKind [TyConBinder]
binders Kind
res_kind,
              tyConArity :: Int
tyConArity       = Int
arity,
              tyConNullaryTy :: Kind
tyConNullaryTy   = TyCon -> Kind
mkNakedTyConTy TyCon
tc,
              tcRoles :: [Role]
tcRoles          = Int -> Role -> [Role]
forall a. Int -> a -> [a]
replicate Int
arity Role
Representational,
              tyConCType :: Maybe CType
tyConCType       = Maybe CType
forall a. Maybe a
Nothing,
              algTcGadtSyntax :: Bool
algTcGadtSyntax  = Bool
False,
              algTcStupidTheta :: [Kind]
algTcStupidTheta = [],
              algTcRhs :: AlgTyConRhs
algTcRhs         = [DataCon] -> AlgTyConRhs
mkSumTyConRhs [DataCon]
cons,
              algTcFields :: FieldLabelEnv
algTcFields      = FieldLabelEnv
forall a. DFastStringEnv a
emptyDFsEnv,
              algTcFlavour :: AlgTyConFlav
algTcFlavour     = AlgTyConFlav
parent
          }
    in TyCon
tc

-- | Makes a tycon suitable for use during type-checking. It stores
-- a variety of details about the definition of the TyCon, but no
-- right-hand side. It lives only during the type-checking of a
-- mutually-recursive group of tycons; it is then zonked to a proper
-- TyCon in zonkTcTyCon.
-- See also Note [Kind checking recursive type and class declarations]
-- in "GHC.Tc.TyCl".
mkTcTyCon :: Name
          -> [TyConBinder]
          -> Kind                -- ^ /result/ kind only
          -> [(Name,TcTyVar)]    -- ^ Scoped type variables;
                                 -- see Note [How TcTyCons work] in GHC.Tc.TyCl
          -> Bool                -- ^ Is this TcTyCon generalised already?
          -> TyConFlavour        -- ^ What sort of 'TyCon' this represents
          -> TyCon
mkTcTyCon :: Name
-> [TyConBinder]
-> Kind
-> [(Name, TyVar)]
-> Bool
-> TyConFlavour
-> TyCon
mkTcTyCon Name
name [TyConBinder]
binders Kind
res_kind [(Name, TyVar)]
scoped_tvs Bool
poly TyConFlavour
flav
  = let tc :: TyCon
tc =
          TcTyCon { tyConUnique :: Unique
tyConUnique  = Name -> Unique
forall a. Uniquable a => a -> Unique
getUnique Name
name
                  , tyConName :: Name
tyConName    = Name
name
                  , tyConTyVars :: [TyVar]
tyConTyVars  = [TyConBinder] -> [TyVar]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [TyConBinder]
binders
                  , tyConBinders :: [TyConBinder]
tyConBinders = [TyConBinder]
binders
                  , tyConResKind :: Kind
tyConResKind = Kind
res_kind
                  , tyConKind :: Kind
tyConKind    = [TyConBinder] -> Kind -> Kind
mkTyConKind [TyConBinder]
binders Kind
res_kind
                  , tyConArity :: Int
tyConArity   = [TyConBinder] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TyConBinder]
binders
                  , tyConNullaryTy :: Kind
tyConNullaryTy = TyCon -> Kind
mkNakedTyConTy TyCon
tc
                  , tcTyConScopedTyVars :: [(Name, TyVar)]
tcTyConScopedTyVars = [(Name, TyVar)]
scoped_tvs
                  , tcTyConIsPoly :: Bool
tcTyConIsPoly       = Bool
poly
                  , tcTyConFlavour :: TyConFlavour
tcTyConFlavour      = TyConFlavour
flav }
    in TyCon
tc

-- | No scoped type variables (to be used with mkTcTyCon).
noTcTyConScopedTyVars :: [(Name, TcTyVar)]
noTcTyConScopedTyVars :: [(Name, TyVar)]
noTcTyConScopedTyVars = []

-- | Create an primitive 'TyCon', such as @Int#@, @Type@ or @RealWorld#@
-- Primitive TyCons are marshalable iff not lifted.
-- If you'd like to change this, modify marshalablePrimTyCon.
mkPrimTyCon :: Name -> [TyConBinder]
            -> Kind    -- ^ /result/ kind
                       -- Must answer 'True' to 'isFixedRuntimeRepKind' (i.e., no representation polymorphism).
                       -- (If you need a representation-polymorphic PrimTyCon,
                       -- change tcHasFixedRuntimeRep, marshalablePrimTyCon, reifyTyCon for PrimTyCons.)
            -> [Role]
            -> TyCon
mkPrimTyCon :: Name -> [TyConBinder] -> Kind -> [Role] -> TyCon
mkPrimTyCon Name
name [TyConBinder]
binders Kind
res_kind [Role]
roles
  = let tc :: TyCon
tc =
          PrimTyCon {
              tyConName :: Name
tyConName    = Name
name,
              tyConUnique :: Unique
tyConUnique  = Name -> Unique
nameUnique Name
name,
              tyConBinders :: [TyConBinder]
tyConBinders = [TyConBinder]
binders,
              tyConResKind :: Kind
tyConResKind = Kind
res_kind,
              tyConKind :: Kind
tyConKind    = [TyConBinder] -> Kind -> Kind
mkTyConKind [TyConBinder]
binders Kind
res_kind,
              tyConArity :: Int
tyConArity   = [Role] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Role]
roles,
              tyConNullaryTy :: Kind
tyConNullaryTy = TyCon -> Kind
mkNakedTyConTy TyCon
tc,
              tcRoles :: [Role]
tcRoles      = [Role]
roles,
              primRepName :: Name
primRepName  = Name -> Name
mkPrelTyConRepName Name
name
          }
    in TyCon
tc

-- | Create a type synonym 'TyCon'
mkSynonymTyCon :: Name -> [TyConBinder] -> Kind   -- ^ /result/ kind
               -> [Role] -> Type -> Bool -> Bool -> Bool -> TyCon
mkSynonymTyCon :: Name
-> [TyConBinder]
-> Kind
-> [Role]
-> Kind
-> Bool
-> Bool
-> Bool
-> TyCon
mkSynonymTyCon Name
name [TyConBinder]
binders Kind
res_kind [Role]
roles Kind
rhs Bool
is_tau Bool
is_fam_free Bool
is_forgetful
  = let tc :: TyCon
tc =
          SynonymTyCon {
              tyConName :: Name
tyConName      = Name
name,
              tyConUnique :: Unique
tyConUnique    = Name -> Unique
nameUnique Name
name,
              tyConBinders :: [TyConBinder]
tyConBinders   = [TyConBinder]
binders,
              tyConResKind :: Kind
tyConResKind   = Kind
res_kind,
              tyConKind :: Kind
tyConKind      = [TyConBinder] -> Kind -> Kind
mkTyConKind [TyConBinder]
binders Kind
res_kind,
              tyConArity :: Int
tyConArity     = [TyConBinder] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TyConBinder]
binders,
              tyConNullaryTy :: Kind
tyConNullaryTy = TyCon -> Kind
mkNakedTyConTy TyCon
tc,
              tyConTyVars :: [TyVar]
tyConTyVars    = [TyConBinder] -> [TyVar]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [TyConBinder]
binders,
              tcRoles :: [Role]
tcRoles        = [Role]
roles,
              synTcRhs :: Kind
synTcRhs       = Kind
rhs,
              synIsTau :: Bool
synIsTau       = Bool
is_tau,
              synIsFamFree :: Bool
synIsFamFree   = Bool
is_fam_free,
              synIsForgetful :: Bool
synIsForgetful = Bool
is_forgetful
          }
    in TyCon
tc

-- | Create a type family 'TyCon'
mkFamilyTyCon :: Name -> [TyConBinder] -> Kind  -- ^ /result/ kind
              -> Maybe Name -> FamTyConFlav
              -> Maybe Class -> Injectivity -> TyCon
mkFamilyTyCon :: Name
-> [TyConBinder]
-> Kind
-> Maybe Name
-> FamTyConFlav
-> Maybe Class
-> Injectivity
-> TyCon
mkFamilyTyCon Name
name [TyConBinder]
binders Kind
res_kind Maybe Name
resVar FamTyConFlav
flav Maybe Class
parent Injectivity
inj
  = let tc :: TyCon
tc =
          FamilyTyCon
            { tyConUnique :: Unique
tyConUnique  = Name -> Unique
nameUnique Name
name
            , tyConName :: Name
tyConName    = Name
name
            , tyConBinders :: [TyConBinder]
tyConBinders = [TyConBinder]
binders
            , tyConResKind :: Kind
tyConResKind = Kind
res_kind
            , tyConKind :: Kind
tyConKind    = [TyConBinder] -> Kind -> Kind
mkTyConKind [TyConBinder]
binders Kind
res_kind
            , tyConArity :: Int
tyConArity   = [TyConBinder] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TyConBinder]
binders
            , tyConNullaryTy :: Kind
tyConNullaryTy = TyCon -> Kind
mkNakedTyConTy TyCon
tc
            , tyConTyVars :: [TyVar]
tyConTyVars  = [TyConBinder] -> [TyVar]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [TyConBinder]
binders
            , famTcResVar :: Maybe Name
famTcResVar  = Maybe Name
resVar
            , famTcFlav :: FamTyConFlav
famTcFlav    = FamTyConFlav
flav
            , famTcParent :: Maybe TyCon
famTcParent  = Class -> TyCon
classTyCon (Class -> TyCon) -> Maybe Class -> Maybe TyCon
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Class
parent
            , famTcInj :: Injectivity
famTcInj     = Injectivity
inj
            }
    in TyCon
tc


-- | Create a promoted data constructor 'TyCon'
-- Somewhat dodgily, we give it the same Name
-- as the data constructor itself; when we pretty-print
-- the TyCon we add a quote; see the Outputable TyCon instance
mkPromotedDataCon :: DataCon -> Name -> TyConRepName
                  -> [TyConTyCoBinder] -> Kind -> [Role]
                  -> RuntimeRepInfo -> TyCon
mkPromotedDataCon :: DataCon
-> Name
-> Name
-> [TyConBinder]
-> Kind
-> [Role]
-> RuntimeRepInfo
-> TyCon
mkPromotedDataCon DataCon
con Name
name Name
rep_name [TyConBinder]
binders Kind
res_kind [Role]
roles RuntimeRepInfo
rep_info
  = let tc :: TyCon
tc =
          PromotedDataCon {
            tyConUnique :: Unique
tyConUnique   = Name -> Unique
nameUnique Name
name,
            tyConName :: Name
tyConName     = Name
name,
            tyConArity :: Int
tyConArity    = [Role] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Role]
roles,
            tyConNullaryTy :: Kind
tyConNullaryTy = TyCon -> Kind
mkNakedTyConTy TyCon
tc,
            tcRoles :: [Role]
tcRoles       = [Role]
roles,
            tyConBinders :: [TyConBinder]
tyConBinders  = [TyConBinder]
binders,
            tyConResKind :: Kind
tyConResKind  = Kind
res_kind,
            tyConKind :: Kind
tyConKind     = [TyConBinder] -> Kind -> Kind
mkTyConKind [TyConBinder]
binders Kind
res_kind,
            dataCon :: DataCon
dataCon       = DataCon
con,
            tcRepName :: Name
tcRepName     = Name
rep_name,
            promDcRepInfo :: RuntimeRepInfo
promDcRepInfo = RuntimeRepInfo
rep_info
          }
    in TyCon
tc

isFunTyCon :: TyCon -> Bool
isFunTyCon :: TyCon -> Bool
isFunTyCon (FunTyCon {}) = Bool
True
isFunTyCon TyCon
_             = Bool
False

-- | Test if the 'TyCon' is algebraic but abstract (invisible data constructors)
isAbstractTyCon :: TyCon -> Bool
isAbstractTyCon :: TyCon -> Bool
isAbstractTyCon (AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AbstractTyCon {} }) = Bool
True
isAbstractTyCon TyCon
_ = Bool
False

-- | Does this 'TyCon' represent something that cannot be defined in Haskell?
isPrimTyCon :: TyCon -> Bool
isPrimTyCon :: TyCon -> Bool
isPrimTyCon (PrimTyCon {}) = Bool
True
isPrimTyCon TyCon
_              = Bool
False

-- | Returns @True@ if the supplied 'TyCon' resulted from either a
-- @data@ or @newtype@ declaration
isAlgTyCon :: TyCon -> Bool
isAlgTyCon :: TyCon -> Bool
isAlgTyCon (AlgTyCon {})   = Bool
True
isAlgTyCon TyCon
_               = Bool
False

-- | Returns @True@ for vanilla AlgTyCons -- that is, those created
-- with a @data@ or @newtype@ declaration.
isVanillaAlgTyCon :: TyCon -> Bool
isVanillaAlgTyCon :: TyCon -> Bool
isVanillaAlgTyCon (AlgTyCon { algTcFlavour :: TyCon -> AlgTyConFlav
algTcFlavour = VanillaAlgTyCon Name
_ }) = Bool
True
isVanillaAlgTyCon TyCon
_                                              = Bool
False

-- | Returns @True@ for the 'TyCon' of the 'Constraint' kind.
{-# INLINE isConstraintKindCon #-} -- See Note [Inlining coreView] in GHC.Core.Type
isConstraintKindCon :: TyCon -> Bool
-- NB: We intentionally match on AlgTyCon, because 'constraintKindTyCon' is
-- always an AlgTyCon (see 'pcTyCon' in TysWiredIn) and the record selector
-- for 'tyConUnique' would generate unreachable code for every other data
-- constructor of TyCon (see #18026).
isConstraintKindCon :: TyCon -> Bool
isConstraintKindCon AlgTyCon { tyConUnique :: TyCon -> Unique
tyConUnique = Unique
u } = Unique
u Unique -> Unique -> Bool
forall a. Eq a => a -> a -> Bool
== Unique
constraintKindTyConKey
isConstraintKindCon TyCon
_                            = Bool
False

isDataTyCon :: TyCon -> Bool
-- ^ Returns @True@ for data types that are /definitely/ represented by
-- heap-allocated constructors.  These are scrutinised by Core-level
-- @case@ expressions, and they get info tables allocated for them.
--
-- Generally, the function will be true for all @data@ types and false
-- for @newtype@s, unboxed tuples, unboxed sums and type family
-- 'TyCon's. But it is not guaranteed to return @True@ in all cases
-- that it could.
--
-- NB: for a data type family, only the /instance/ 'TyCon's
--     get an info table.  The family declaration 'TyCon' does not
isDataTyCon :: TyCon -> Bool
isDataTyCon (AlgTyCon {algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs})
  = case AlgTyConRhs
rhs of
        TupleTyCon { tup_sort :: AlgTyConRhs -> TupleSort
tup_sort = TupleSort
sort }
                           -> Boxity -> Bool
isBoxed (TupleSort -> Boxity
tupleSortBoxity TupleSort
sort)
        SumTyCon {}        -> Bool
False
        DataTyCon {}       -> Bool
True
        NewTyCon {}        -> Bool
False
        AbstractTyCon {}   -> Bool
False      -- We don't know, so return False
isDataTyCon TyCon
_ = Bool
False

-- | 'isInjectiveTyCon' is true of 'TyCon's for which this property holds
-- (where X is the role passed in):
--   If (T a1 b1 c1) ~X (T a2 b2 c2), then (a1 ~X1 a2), (b1 ~X2 b2), and (c1 ~X3 c2)
-- (where X1, X2, and X3, are the roles given by tyConRolesX tc X)
-- See also Note [Decomposing equality] in "GHC.Tc.Solver.Canonical"
isInjectiveTyCon :: TyCon -> Role -> Bool
isInjectiveTyCon :: TyCon -> Role -> Bool
isInjectiveTyCon TyCon
_                             Role
Phantom          = Bool
False
isInjectiveTyCon (FunTyCon {})                 Role
_                = Bool
True
isInjectiveTyCon (AlgTyCon {})                 Role
Nominal          = Bool
True
isInjectiveTyCon (AlgTyCon {algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs})   Role
Representational
  = AlgTyConRhs -> Bool
isGenInjAlgRhs AlgTyConRhs
rhs
isInjectiveTyCon (SynonymTyCon {})             Role
_                = Bool
False
isInjectiveTyCon (FamilyTyCon { famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = DataFamilyTyCon Name
_ })
                                               Role
Nominal          = Bool
True
isInjectiveTyCon (FamilyTyCon { famTcInj :: TyCon -> Injectivity
famTcInj = Injective [Bool]
inj }) Role
Nominal = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and [Bool]
inj
isInjectiveTyCon (FamilyTyCon {})              Role
_                = Bool
False
isInjectiveTyCon (PrimTyCon {})                Role
_                = Bool
True
isInjectiveTyCon (PromotedDataCon {})          Role
_                = Bool
True
isInjectiveTyCon (TcTyCon {})                  Role
_                = Bool
True
  -- Reply True for TcTyCon to minimise knock on type errors
  -- See Note [How TcTyCons work] item (1) in GHC.Tc.TyCl

-- | 'isGenerativeTyCon' is true of 'TyCon's for which this property holds
-- (where X is the role passed in):
--   If (T tys ~X t), then (t's head ~X T).
-- See also Note [Decomposing equality] in "GHC.Tc.Solver.Canonical"
isGenerativeTyCon :: TyCon -> Role -> Bool
isGenerativeTyCon :: TyCon -> Role -> Bool
isGenerativeTyCon (FamilyTyCon { famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = DataFamilyTyCon Name
_ }) Role
Nominal = Bool
True
isGenerativeTyCon (FamilyTyCon {}) Role
_ = Bool
False
  -- in all other cases, injectivity implies generativity
isGenerativeTyCon TyCon
tc               Role
r = TyCon -> Role -> Bool
isInjectiveTyCon TyCon
tc Role
r

-- | Is this an 'AlgTyConRhs' of a 'TyCon' that is generative and injective
-- with respect to representational equality?
isGenInjAlgRhs :: AlgTyConRhs -> Bool
isGenInjAlgRhs :: AlgTyConRhs -> Bool
isGenInjAlgRhs (TupleTyCon {})          = Bool
True
isGenInjAlgRhs (SumTyCon {})            = Bool
True
isGenInjAlgRhs (DataTyCon {})           = Bool
True
isGenInjAlgRhs (AbstractTyCon {})       = Bool
False
isGenInjAlgRhs (NewTyCon {})            = Bool
False

-- | Is this 'TyCon' that for a @newtype@
isNewTyCon :: TyCon -> Bool
isNewTyCon :: TyCon -> Bool
isNewTyCon (AlgTyCon {algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = NewTyCon {}}) = Bool
True
isNewTyCon TyCon
_                                   = Bool
False

-- | Take a 'TyCon' apart into the 'TyVar's it scopes over, the 'Type' it
-- expands into, and (possibly) a coercion from the representation type to the
-- @newtype@.
-- Returns @Nothing@ if this is not possible.
unwrapNewTyCon_maybe :: TyCon -> Maybe ([TyVar], Type, CoAxiom Unbranched)
unwrapNewTyCon_maybe :: TyCon -> Maybe ([TyVar], Kind, CoAxiom Unbranched)
unwrapNewTyCon_maybe (AlgTyCon { tyConTyVars :: TyCon -> [TyVar]
tyConTyVars = [TyVar]
tvs,
                                 algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = NewTyCon { nt_co :: AlgTyConRhs -> CoAxiom Unbranched
nt_co = CoAxiom Unbranched
co,
                                                       nt_rhs :: AlgTyConRhs -> Kind
nt_rhs = Kind
rhs }})
                           = ([TyVar], Kind, CoAxiom Unbranched)
-> Maybe ([TyVar], Kind, CoAxiom Unbranched)
forall a. a -> Maybe a
Just ([TyVar]
tvs, Kind
rhs, CoAxiom Unbranched
co)
unwrapNewTyCon_maybe TyCon
_     = Maybe ([TyVar], Kind, CoAxiom Unbranched)
forall a. Maybe a
Nothing

unwrapNewTyConEtad_maybe :: TyCon -> Maybe ([TyVar], Type, CoAxiom Unbranched)
unwrapNewTyConEtad_maybe :: TyCon -> Maybe ([TyVar], Kind, CoAxiom Unbranched)
unwrapNewTyConEtad_maybe (AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = NewTyCon { nt_co :: AlgTyConRhs -> CoAxiom Unbranched
nt_co = CoAxiom Unbranched
co,
                                                           nt_etad_rhs :: AlgTyConRhs -> ([TyVar], Kind)
nt_etad_rhs = ([TyVar]
tvs,Kind
rhs) }})
                           = ([TyVar], Kind, CoAxiom Unbranched)
-> Maybe ([TyVar], Kind, CoAxiom Unbranched)
forall a. a -> Maybe a
Just ([TyVar]
tvs, Kind
rhs, CoAxiom Unbranched
co)
unwrapNewTyConEtad_maybe TyCon
_ = Maybe ([TyVar], Kind, CoAxiom Unbranched)
forall a. Maybe a
Nothing

-- | Is this a 'TyCon' representing a regular H98 type synonym (@type@)?
{-# INLINE isTypeSynonymTyCon #-}  -- See Note [Inlining coreView] in GHC.Core.Type
isTypeSynonymTyCon :: TyCon -> Bool
isTypeSynonymTyCon :: TyCon -> Bool
isTypeSynonymTyCon (SynonymTyCon {}) = Bool
True
isTypeSynonymTyCon TyCon
_                 = Bool
False

isTauTyCon :: TyCon -> Bool
isTauTyCon :: TyCon -> Bool
isTauTyCon (SynonymTyCon { synIsTau :: TyCon -> Bool
synIsTau = Bool
is_tau }) = Bool
is_tau
isTauTyCon TyCon
_                                    = Bool
True

-- | Is this tycon neither a type family nor a synonym that expands
-- to a type family?
isFamFreeTyCon :: TyCon -> Bool
isFamFreeTyCon :: TyCon -> Bool
isFamFreeTyCon (SynonymTyCon { synIsFamFree :: TyCon -> Bool
synIsFamFree = Bool
fam_free }) = Bool
fam_free
isFamFreeTyCon (FamilyTyCon { famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = FamTyConFlav
flav })         = FamTyConFlav -> Bool
isDataFamFlav FamTyConFlav
flav
isFamFreeTyCon TyCon
_                                          = Bool
True

-- | Is this a forgetful type synonym? If this is a type synonym whose
-- RHS does not mention one (or more) of its bound variables, returns
-- True. Thus, False means that all bound variables appear on the RHS;
-- True may not mean anything, as the test to set this flag is
-- conservative.
isForgetfulSynTyCon :: TyCon -> Bool
isForgetfulSynTyCon :: TyCon -> Bool
isForgetfulSynTyCon (SynonymTyCon { synIsForgetful :: TyCon -> Bool
synIsForgetful = Bool
forget }) = Bool
forget
isForgetfulSynTyCon TyCon
_                                          = Bool
False

-- As for newtypes, it is in some contexts important to distinguish between
-- closed synonyms and synonym families, as synonym families have no unique
-- right hand side to which a synonym family application can expand.
--

-- | True iff we can decompose (T a b c) into ((T a b) c)
--   I.e. is it injective and generative w.r.t nominal equality?
--   That is, if (T a b) ~N d e f, is it always the case that
--            (T ~N d), (a ~N e) and (b ~N f)?
-- Specifically NOT true of synonyms (open and otherwise)
--
-- It'd be unusual to call mustBeSaturated on a regular H98
-- type synonym, because you should probably have expanded it first
-- But regardless, it's not decomposable
mustBeSaturated :: TyCon -> Bool
mustBeSaturated :: TyCon -> Bool
mustBeSaturated = TyConFlavour -> Bool
tcFlavourMustBeSaturated (TyConFlavour -> Bool) -> (TyCon -> TyConFlavour) -> TyCon -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyCon -> TyConFlavour
tyConFlavour

-- | Is this an algebraic 'TyCon' declared with the GADT syntax?
isGadtSyntaxTyCon :: TyCon -> Bool
isGadtSyntaxTyCon :: TyCon -> Bool
isGadtSyntaxTyCon (AlgTyCon { algTcGadtSyntax :: TyCon -> Bool
algTcGadtSyntax = Bool
res }) = Bool
res
isGadtSyntaxTyCon TyCon
_                                    = Bool
False

-- | Is this an algebraic 'TyCon' which is just an enumeration of values?
isEnumerationTyCon :: TyCon -> Bool
-- See Note [Enumeration types] in GHC.Core.TyCon
isEnumerationTyCon :: TyCon -> Bool
isEnumerationTyCon (AlgTyCon { tyConArity :: TyCon -> Int
tyConArity = Int
arity, algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs })
  = case AlgTyConRhs
rhs of
       DataTyCon { is_enum :: AlgTyConRhs -> Bool
is_enum = Bool
res } -> Bool
res
       TupleTyCon {}               -> Int
arity Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
       AlgTyConRhs
_                           -> Bool
False
isEnumerationTyCon TyCon
_ = Bool
False

-- | Is this a 'TyCon', synonym or otherwise, that defines a family?
isFamilyTyCon :: TyCon -> Bool
isFamilyTyCon :: TyCon -> Bool
isFamilyTyCon (FamilyTyCon {}) = Bool
True
isFamilyTyCon TyCon
_                = Bool
False

-- | Is this a 'TyCon', synonym or otherwise, that defines a family with
-- instances?
isOpenFamilyTyCon :: TyCon -> Bool
isOpenFamilyTyCon :: TyCon -> Bool
isOpenFamilyTyCon (FamilyTyCon {famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = FamTyConFlav
flav })
  | FamTyConFlav
OpenSynFamilyTyCon <- FamTyConFlav
flav = Bool
True
  | DataFamilyTyCon {} <- FamTyConFlav
flav = Bool
True
isOpenFamilyTyCon TyCon
_            = Bool
False

-- | Is this a synonym 'TyCon' that can have may have further instances appear?
isTypeFamilyTyCon :: TyCon -> Bool
isTypeFamilyTyCon :: TyCon -> Bool
isTypeFamilyTyCon (FamilyTyCon { famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = FamTyConFlav
flav }) = Bool -> Bool
not (FamTyConFlav -> Bool
isDataFamFlav FamTyConFlav
flav)
isTypeFamilyTyCon TyCon
_                                  = Bool
False

-- | Is this a synonym 'TyCon' that can have may have further instances appear?
isDataFamilyTyCon :: TyCon -> Bool
isDataFamilyTyCon :: TyCon -> Bool
isDataFamilyTyCon (FamilyTyCon { famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = FamTyConFlav
flav }) = FamTyConFlav -> Bool
isDataFamFlav FamTyConFlav
flav
isDataFamilyTyCon TyCon
_                                  = Bool
False

-- | Is this an open type family TyCon?
isOpenTypeFamilyTyCon :: TyCon -> Bool
isOpenTypeFamilyTyCon :: TyCon -> Bool
isOpenTypeFamilyTyCon (FamilyTyCon {famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = FamTyConFlav
OpenSynFamilyTyCon }) = Bool
True
isOpenTypeFamilyTyCon TyCon
_                                               = Bool
False

-- | Is this a non-empty closed type family? Returns 'Nothing' for
-- abstract or empty closed families.
isClosedSynFamilyTyConWithAxiom_maybe :: TyCon -> Maybe (CoAxiom Branched)
isClosedSynFamilyTyConWithAxiom_maybe :: TyCon -> Maybe (CoAxiom Branched)
isClosedSynFamilyTyConWithAxiom_maybe
  (FamilyTyCon {famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = ClosedSynFamilyTyCon Maybe (CoAxiom Branched)
mb}) = Maybe (CoAxiom Branched)
mb
isClosedSynFamilyTyConWithAxiom_maybe TyCon
_               = Maybe (CoAxiom Branched)
forall a. Maybe a
Nothing

-- | @'tyConInjectivityInfo' tc@ returns @'Injective' is@ if @tc@ is an
-- injective tycon (where @is@ states for which 'tyConBinders' @tc@ is
-- injective), or 'NotInjective' otherwise.
tyConInjectivityInfo :: TyCon -> Injectivity
tyConInjectivityInfo :: TyCon -> Injectivity
tyConInjectivityInfo TyCon
tc
  | FamilyTyCon { famTcInj :: TyCon -> Injectivity
famTcInj = Injectivity
inj } <- TyCon
tc
  = Injectivity
inj
  | TyCon -> Role -> Bool
isInjectiveTyCon TyCon
tc Role
Nominal
  = [Bool] -> Injectivity
Injective (Int -> Bool -> [Bool]
forall a. Int -> a -> [a]
replicate (TyCon -> Int
tyConArity TyCon
tc) Bool
True)
  | Bool
otherwise
  = Injectivity
NotInjective

isBuiltInSynFamTyCon_maybe :: TyCon -> Maybe BuiltInSynFamily
isBuiltInSynFamTyCon_maybe :: TyCon -> Maybe BuiltInSynFamily
isBuiltInSynFamTyCon_maybe
  (FamilyTyCon {famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = BuiltInSynFamTyCon BuiltInSynFamily
ops }) = BuiltInSynFamily -> Maybe BuiltInSynFamily
forall a. a -> Maybe a
Just BuiltInSynFamily
ops
isBuiltInSynFamTyCon_maybe TyCon
_                          = Maybe BuiltInSynFamily
forall a. Maybe a
Nothing

isDataFamFlav :: FamTyConFlav -> Bool
isDataFamFlav :: FamTyConFlav -> Bool
isDataFamFlav (DataFamilyTyCon {}) = Bool
True   -- Data family
isDataFamFlav FamTyConFlav
_                    = Bool
False  -- Type synonym family

-- | Is this TyCon for an associated type?
isTyConAssoc :: TyCon -> Bool
isTyConAssoc :: TyCon -> Bool
isTyConAssoc = Maybe TyCon -> Bool
forall a. Maybe a -> Bool
isJust (Maybe TyCon -> Bool) -> (TyCon -> Maybe TyCon) -> TyCon -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyCon -> Maybe TyCon
tyConAssoc_maybe

-- | Get the enclosing class TyCon (if there is one) for the given TyCon.
tyConAssoc_maybe :: TyCon -> Maybe TyCon
tyConAssoc_maybe :: TyCon -> Maybe TyCon
tyConAssoc_maybe = TyConFlavour -> Maybe TyCon
tyConFlavourAssoc_maybe (TyConFlavour -> Maybe TyCon)
-> (TyCon -> TyConFlavour) -> TyCon -> Maybe TyCon
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyCon -> TyConFlavour
tyConFlavour

-- | Get the enclosing class TyCon (if there is one) for the given TyConFlavour
tyConFlavourAssoc_maybe :: TyConFlavour -> Maybe TyCon
tyConFlavourAssoc_maybe :: TyConFlavour -> Maybe TyCon
tyConFlavourAssoc_maybe (DataFamilyFlavour Maybe TyCon
mb_parent)     = Maybe TyCon
mb_parent
tyConFlavourAssoc_maybe (OpenTypeFamilyFlavour Maybe TyCon
mb_parent) = Maybe TyCon
mb_parent
tyConFlavourAssoc_maybe TyConFlavour
_                                 = Maybe TyCon
forall a. Maybe a
Nothing

-- The unit tycon didn't used to be classed as a tuple tycon
-- but I thought that was silly so I've undone it
-- If it can't be for some reason, it should be a AlgTyCon
isTupleTyCon :: TyCon -> Bool
-- ^ Does this 'TyCon' represent a tuple?
--
-- NB: when compiling @Data.Tuple@, the tycons won't reply @True@ to
-- 'isTupleTyCon', because they are built as 'AlgTyCons'.  However they
-- get spat into the interface file as tuple tycons, so I don't think
-- it matters.
isTupleTyCon :: TyCon -> Bool
isTupleTyCon (AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = TupleTyCon {} }) = Bool
True
isTupleTyCon TyCon
_ = Bool
False

tyConTuple_maybe :: TyCon -> Maybe TupleSort
tyConTuple_maybe :: TyCon -> Maybe TupleSort
tyConTuple_maybe (AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs })
  | TupleTyCon { tup_sort :: AlgTyConRhs -> TupleSort
tup_sort = TupleSort
sort} <- AlgTyConRhs
rhs = TupleSort -> Maybe TupleSort
forall a. a -> Maybe a
Just TupleSort
sort
tyConTuple_maybe TyCon
_                       = Maybe TupleSort
forall a. Maybe a
Nothing

-- | Is this the 'TyCon' for an unboxed tuple?
isUnboxedTupleTyCon :: TyCon -> Bool
isUnboxedTupleTyCon :: TyCon -> Bool
isUnboxedTupleTyCon (AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs })
  | TupleTyCon { tup_sort :: AlgTyConRhs -> TupleSort
tup_sort = TupleSort
sort } <- AlgTyConRhs
rhs
  = Bool -> Bool
not (Boxity -> Bool
isBoxed (TupleSort -> Boxity
tupleSortBoxity TupleSort
sort))
isUnboxedTupleTyCon TyCon
_ = Bool
False

-- | Is this the 'TyCon' for a boxed tuple?
isBoxedTupleTyCon :: TyCon -> Bool
isBoxedTupleTyCon :: TyCon -> Bool
isBoxedTupleTyCon (AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs })
  | TupleTyCon { tup_sort :: AlgTyConRhs -> TupleSort
tup_sort = TupleSort
sort } <- AlgTyConRhs
rhs
  = Boxity -> Bool
isBoxed (TupleSort -> Boxity
tupleSortBoxity TupleSort
sort)
isBoxedTupleTyCon TyCon
_ = Bool
False

-- | Is this the 'TyCon' for an unboxed sum?
isUnboxedSumTyCon :: TyCon -> Bool
isUnboxedSumTyCon :: TyCon -> Bool
isUnboxedSumTyCon (AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs })
  | SumTyCon {} <- AlgTyConRhs
rhs
  = Bool
True
isUnboxedSumTyCon TyCon
_ = Bool
False

isLiftedAlgTyCon :: TyCon -> Bool
isLiftedAlgTyCon :: TyCon -> Bool
isLiftedAlgTyCon (AlgTyCon { tyConResKind :: TyCon -> Kind
tyConResKind = Kind
res_kind })
  = Kind -> Bool
isLiftedTypeKind Kind
res_kind
isLiftedAlgTyCon TyCon
_ = Bool
False

-- | Is this the 'TyCon' for a /promoted/ tuple?
isPromotedTupleTyCon :: TyCon -> Bool
isPromotedTupleTyCon :: TyCon -> Bool
isPromotedTupleTyCon TyCon
tyCon
  | Just DataCon
dataCon <- TyCon -> Maybe DataCon
isPromotedDataCon_maybe TyCon
tyCon
  , TyCon -> Bool
isTupleTyCon (DataCon -> TyCon
dataConTyCon DataCon
dataCon) = Bool
True
  | Bool
otherwise                           = Bool
False

-- | Is this a PromotedDataCon?
isPromotedDataCon :: TyCon -> Bool
isPromotedDataCon :: TyCon -> Bool
isPromotedDataCon (PromotedDataCon {}) = Bool
True
isPromotedDataCon TyCon
_                    = Bool
False

-- | Retrieves the promoted DataCon if this is a PromotedDataCon;
isPromotedDataCon_maybe :: TyCon -> Maybe DataCon
isPromotedDataCon_maybe :: TyCon -> Maybe DataCon
isPromotedDataCon_maybe (PromotedDataCon { dataCon :: TyCon -> DataCon
dataCon = DataCon
dc }) = DataCon -> Maybe DataCon
forall a. a -> Maybe a
Just DataCon
dc
isPromotedDataCon_maybe TyCon
_ = Maybe DataCon
forall a. Maybe a
Nothing

-- | Is this tycon really meant for use at the kind level? That is,
-- should it be permitted without -XDataKinds?
isKindTyCon :: TyCon -> Bool
isKindTyCon :: TyCon -> Bool
isKindTyCon TyCon
tc = TyCon -> Unique
forall a. Uniquable a => a -> Unique
getUnique TyCon
tc Unique -> UniqSet Unique -> Bool
forall a. Uniquable a => a -> UniqSet a -> Bool
`elementOfUniqSet` UniqSet Unique
kindTyConKeys

-- | These TyCons should be allowed at the kind level, even without
-- -XDataKinds.
kindTyConKeys :: UniqSet Unique
kindTyConKeys :: UniqSet Unique
kindTyConKeys = [UniqSet Unique] -> UniqSet Unique
forall a. [UniqSet a] -> UniqSet a
unionManyUniqSets
  ( [Unique] -> UniqSet Unique
forall a. Uniquable a => [a] -> UniqSet a
mkUniqSet [ Unique
liftedTypeKindTyConKey, Unique
liftedRepTyConKey, Unique
constraintKindTyConKey, Unique
tYPETyConKey ]
  UniqSet Unique -> [UniqSet Unique] -> [UniqSet Unique]
forall a. a -> [a] -> [a]
: (TyCon -> UniqSet Unique) -> [TyCon] -> [UniqSet Unique]
forall a b. (a -> b) -> [a] -> [b]
map ([Unique] -> UniqSet Unique
forall a. Uniquable a => [a] -> UniqSet a
mkUniqSet ([Unique] -> UniqSet Unique)
-> (TyCon -> [Unique]) -> TyCon -> UniqSet Unique
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyCon -> [Unique]
tycon_with_datacons) [ TyCon
runtimeRepTyCon, TyCon
levityTyCon
                                          , TyCon
multiplicityTyCon
                                          , TyCon
vecCountTyCon, TyCon
vecElemTyCon ] )
  where
    tycon_with_datacons :: TyCon -> [Unique]
tycon_with_datacons TyCon
tc = TyCon -> Unique
forall a. Uniquable a => a -> Unique
getUnique TyCon
tc Unique -> [Unique] -> [Unique]
forall a. a -> [a] -> [a]
: (DataCon -> Unique) -> [DataCon] -> [Unique]
forall a b. (a -> b) -> [a] -> [b]
map DataCon -> Unique
forall a. Uniquable a => a -> Unique
getUnique (TyCon -> [DataCon]
tyConDataCons TyCon
tc)

isLiftedTypeKindTyConName :: Name -> Bool
isLiftedTypeKindTyConName :: Name -> Bool
isLiftedTypeKindTyConName = (Name -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
liftedTypeKindTyConKey)

-- | Identifies implicit tycons that, in particular, do not go into interface
-- files (because they are implicitly reconstructed when the interface is
-- read).
--
-- Note that:
--
-- * Associated families are implicit, as they are re-constructed from
--   the class declaration in which they reside, and
--
-- * Family instances are /not/ implicit as they represent the instance body
--   (similar to a @dfun@ does that for a class instance).
--
-- * Tuples are implicit iff they have a wired-in name
--   (namely: boxed and unboxed tuples are wired-in and implicit,
--            but constraint tuples are not)
isImplicitTyCon :: TyCon -> Bool
isImplicitTyCon :: TyCon -> Bool
isImplicitTyCon (FunTyCon {})        = Bool
True
isImplicitTyCon (PrimTyCon {})       = Bool
True
isImplicitTyCon (PromotedDataCon {}) = Bool
True
isImplicitTyCon (AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs, tyConName :: TyCon -> Name
tyConName = Name
name })
  | TupleTyCon {} <- AlgTyConRhs
rhs             = Name -> Bool
isWiredInName Name
name
  | SumTyCon {} <- AlgTyConRhs
rhs               = Bool
True
  | Bool
otherwise                        = Bool
False
isImplicitTyCon (FamilyTyCon { famTcParent :: TyCon -> Maybe TyCon
famTcParent = Maybe TyCon
parent }) = Maybe TyCon -> Bool
forall a. Maybe a -> Bool
isJust Maybe TyCon
parent
isImplicitTyCon (SynonymTyCon {})    = Bool
False
isImplicitTyCon (TcTyCon {})         = Bool
False

tyConCType_maybe :: TyCon -> Maybe CType
tyConCType_maybe :: TyCon -> Maybe CType
tyConCType_maybe tc :: TyCon
tc@(AlgTyCon {}) = TyCon -> Maybe CType
tyConCType TyCon
tc
tyConCType_maybe TyCon
_ = Maybe CType
forall a. Maybe a
Nothing

-- | Is this a TcTyCon? (That is, one only used during type-checking?)
isTcTyCon :: TyCon -> Bool
isTcTyCon :: TyCon -> Bool
isTcTyCon (TcTyCon {}) = Bool
True
isTcTyCon TyCon
_            = Bool
False

setTcTyConKind :: TyCon -> Kind -> TyCon
-- Update the Kind of a TcTyCon
-- The new kind is always a zonked version of its previous
-- kind, so we don't need to update any other fields.
-- See Note [The Purely Kinded Type Invariant (PKTI)] in GHC.Tc.Gen.HsType
setTcTyConKind :: TyCon -> Kind -> TyCon
setTcTyConKind tc :: TyCon
tc@(TcTyCon {}) Kind
kind = let tc' :: TyCon
tc' = TyCon
tc { tyConKind :: Kind
tyConKind = Kind
kind
                                                   , tyConNullaryTy :: Kind
tyConNullaryTy = TyCon -> Kind
mkNakedTyConTy TyCon
tc'
                                                       -- see Note [Sharing nullary TyCons]
                                                   }
                                      in TyCon
tc'
setTcTyConKind TyCon
tc              Kind
_    = String -> SDoc -> TyCon
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"setTcTyConKind" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc)

-- | Does this 'TyCon' have a syntactically fixed RuntimeRep when fully applied,
-- as per Note [Fixed RuntimeRep] in GHC.Tc.Utils.Concrete?
--
-- False is safe. True means we're sure.
-- Does only a quick check, based on the TyCon's category.
--
-- See Note [Representation-polymorphic TyCons]
tcHasFixedRuntimeRep :: TyCon -> Bool
tcHasFixedRuntimeRep :: TyCon -> Bool
tcHasFixedRuntimeRep FunTyCon{}           = Bool
True
tcHasFixedRuntimeRep (AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs }) = case AlgTyConRhs
rhs of
  AbstractTyCon {} -> Bool
False
          -- An abstract TyCon might not have a fixed runtime representation.
          -- Note that this is an entirely different matter from the concreteness
          -- of the 'TyCon', in the sense of 'isConcreteTyCon'.

  DataTyCon { data_fixed_lev :: AlgTyConRhs -> Bool
data_fixed_lev = Bool
fixed_lev } -> Bool
fixed_lev
          -- A datatype might not have a fixed levity with UnliftedDatatypes (#20423).
          -- NB: the current representation-polymorphism checks require that
          -- the representation be fully-known, including levity variables.
          -- This might be relaxed in the future (#15532).

  TupleTyCon { tup_sort :: AlgTyConRhs -> TupleSort
tup_sort = TupleSort
tuple_sort } -> Boxity -> Bool
isBoxed (TupleSort -> Boxity
tupleSortBoxity TupleSort
tuple_sort)

  SumTyCon {} -> Bool
False   -- only unboxed sums here

  NewTyCon { nt_fixed_rep :: AlgTyConRhs -> Bool
nt_fixed_rep = Bool
fixed_rep } -> Bool
fixed_rep
         -- A newtype might not have a fixed runtime representation
         -- with UnliftedNewtypes (#17360)

tcHasFixedRuntimeRep SynonymTyCon{}       = Bool
False   -- conservative choice
tcHasFixedRuntimeRep FamilyTyCon{}        = Bool
False
tcHasFixedRuntimeRep PrimTyCon{}          = Bool
True
tcHasFixedRuntimeRep TcTyCon{}            = Bool
False
tcHasFixedRuntimeRep tc :: TyCon
tc@PromotedDataCon{} = String -> SDoc -> Bool
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tcHasFixedRuntimeRep datacon" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc)

-- | Is this 'TyCon' concrete (i.e. not a synonym/type family)?
--
-- Used for representation polymorphism checks.
isConcreteTyCon :: TyCon -> Bool
isConcreteTyCon :: TyCon -> Bool
isConcreteTyCon = TyConFlavour -> Bool
isConcreteTyConFlavour (TyConFlavour -> Bool) -> (TyCon -> TyConFlavour) -> TyCon -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyCon -> TyConFlavour
tyConFlavour

-- | Is this 'TyConFlavour' concrete (i.e. not a synonym/type family)?
--
-- Used for representation polymorphism checks.
isConcreteTyConFlavour :: TyConFlavour -> Bool
isConcreteTyConFlavour :: TyConFlavour -> Bool
isConcreteTyConFlavour = \case
  TyConFlavour
ClassFlavour             -> Bool
True
  TupleFlavour {}          -> Bool
True
  TyConFlavour
SumFlavour               -> Bool
True
  TyConFlavour
DataTypeFlavour          -> Bool
True
  TyConFlavour
NewtypeFlavour           -> Bool
True
  TyConFlavour
AbstractTypeFlavour      -> Bool
True  -- See Note [Concrete types] in GHC.Tc.Utils.Concrete
  DataFamilyFlavour {}     -> Bool
False
  OpenTypeFamilyFlavour {} -> Bool
False
  TyConFlavour
ClosedTypeFamilyFlavour  -> Bool
False
  TyConFlavour
TypeSynonymFlavour       -> Bool
False
  TyConFlavour
BuiltInTypeFlavour       -> Bool
True
  TyConFlavour
PromotedDataConFlavour   -> Bool
True

{-
-----------------------------------------------
--      Expand type-constructor applications
-----------------------------------------------
-}

expandSynTyCon_maybe
        :: TyCon
        -> [tyco]                 -- ^ Arguments to 'TyCon'
        -> Maybe ([(TyVar,tyco)],
                  Type,
                  [tyco])         -- ^ Returns a 'TyVar' substitution, the body
                                  -- type of the synonym (not yet substituted)
                                  -- and any arguments remaining from the
                                  -- application
-- ^ Expand a type synonym application
-- Return Nothing if the TyCon is not a synonym,
-- or if not enough arguments are supplied
expandSynTyCon_maybe :: forall tyco.
TyCon -> [tyco] -> Maybe ([(TyVar, tyco)], Kind, [tyco])
expandSynTyCon_maybe TyCon
tc [tyco]
tys
  | SynonymTyCon { tyConTyVars :: TyCon -> [TyVar]
tyConTyVars = [TyVar]
tvs, synTcRhs :: TyCon -> Kind
synTcRhs = Kind
rhs, tyConArity :: TyCon -> Int
tyConArity = Int
arity } <- TyCon
tc
  = if Int
arity Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
    then ([(TyVar, tyco)], Kind, [tyco])
-> Maybe ([(TyVar, tyco)], Kind, [tyco])
forall a. a -> Maybe a
Just ([], Kind
rhs, [tyco]
tys)  -- Avoid a bit of work in the case of nullary synonyms
    else case [tyco]
tys [tyco] -> Int -> Ordering
forall a. [a] -> Int -> Ordering
`listLengthCmp` Int
arity of
              Ordering
GT -> ([(TyVar, tyco)], Kind, [tyco])
-> Maybe ([(TyVar, tyco)], Kind, [tyco])
forall a. a -> Maybe a
Just ([TyVar]
tvs [TyVar] -> [tyco] -> [(TyVar, tyco)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [tyco]
tys, Kind
rhs, Int -> [tyco] -> [tyco]
forall a. Int -> [a] -> [a]
drop Int
arity [tyco]
tys)
              Ordering
EQ -> ([(TyVar, tyco)], Kind, [tyco])
-> Maybe ([(TyVar, tyco)], Kind, [tyco])
forall a. a -> Maybe a
Just ([TyVar]
tvs [TyVar] -> [tyco] -> [(TyVar, tyco)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [tyco]
tys, Kind
rhs, [])
              Ordering
LT -> Maybe ([(TyVar, tyco)], Kind, [tyco])
forall a. Maybe a
Nothing
   | Bool
otherwise
   = Maybe ([(TyVar, tyco)], Kind, [tyco])
forall a. Maybe a
Nothing

----------------

-- | Check if the tycon actually refers to a proper `data` or `newtype`
--  with user defined constructors rather than one from a class or other
--  construction.

-- NB: This is only used in GHC.Tc.Gen.Export.checkPatSynParent to determine if an
-- exported tycon can have a pattern synonym bundled with it, e.g.,
-- module Foo (TyCon(.., PatSyn)) where
isTyConWithSrcDataCons :: TyCon -> Bool
isTyConWithSrcDataCons :: TyCon -> Bool
isTyConWithSrcDataCons (AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs, algTcFlavour :: TyCon -> AlgTyConFlav
algTcFlavour = AlgTyConFlav
parent }) =
  case AlgTyConRhs
rhs of
    DataTyCon {}  -> Bool
isSrcParent
    NewTyCon {}   -> Bool
isSrcParent
    TupleTyCon {} -> Bool
isSrcParent
    AlgTyConRhs
_ -> Bool
False
  where
    isSrcParent :: Bool
isSrcParent = AlgTyConFlav -> Bool
isNoParent AlgTyConFlav
parent
isTyConWithSrcDataCons (FamilyTyCon { famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = DataFamilyTyCon {} })
                         = Bool
True -- #14058
isTyConWithSrcDataCons TyCon
_ = Bool
False


-- | As 'tyConDataCons_maybe', but returns the empty list of constructors if no
-- constructors could be found
tyConDataCons :: TyCon -> [DataCon]
-- It's convenient for tyConDataCons to return the
-- empty list for type synonyms etc
tyConDataCons :: TyCon -> [DataCon]
tyConDataCons TyCon
tycon = TyCon -> Maybe [DataCon]
tyConDataCons_maybe TyCon
tycon Maybe [DataCon] -> [DataCon] -> [DataCon]
forall a. Maybe a -> a -> a
`orElse` []

-- | Determine the 'DataCon's originating from the given 'TyCon', if the 'TyCon'
-- is the sort that can have any constructors (note: this does not include
-- abstract algebraic types)
tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
tyConDataCons_maybe (AlgTyCon {algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs})
  = case AlgTyConRhs
rhs of
       DataTyCon { data_cons :: AlgTyConRhs -> [DataCon]
data_cons = [DataCon]
cons } -> [DataCon] -> Maybe [DataCon]
forall a. a -> Maybe a
Just [DataCon]
cons
       NewTyCon { data_con :: AlgTyConRhs -> DataCon
data_con = DataCon
con }    -> [DataCon] -> Maybe [DataCon]
forall a. a -> Maybe a
Just [DataCon
con]
       TupleTyCon { data_con :: AlgTyConRhs -> DataCon
data_con = DataCon
con }  -> [DataCon] -> Maybe [DataCon]
forall a. a -> Maybe a
Just [DataCon
con]
       SumTyCon { data_cons :: AlgTyConRhs -> [DataCon]
data_cons = [DataCon]
cons }  -> [DataCon] -> Maybe [DataCon]
forall a. a -> Maybe a
Just [DataCon]
cons
       AlgTyConRhs
_                              -> Maybe [DataCon]
forall a. Maybe a
Nothing
tyConDataCons_maybe TyCon
_ = Maybe [DataCon]
forall a. Maybe a
Nothing

-- | If the given 'TyCon' has a /single/ data constructor, i.e. it is a @data@
-- type with one alternative, a tuple type or a @newtype@ then that constructor
-- is returned. If the 'TyCon' has more than one constructor, or represents a
-- primitive or function type constructor then @Nothing@ is returned.
tyConSingleDataCon_maybe :: TyCon -> Maybe DataCon
tyConSingleDataCon_maybe :: TyCon -> Maybe DataCon
tyConSingleDataCon_maybe (AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs })
  = case AlgTyConRhs
rhs of
      DataTyCon { data_cons :: AlgTyConRhs -> [DataCon]
data_cons = [DataCon
c] } -> DataCon -> Maybe DataCon
forall a. a -> Maybe a
Just DataCon
c
      TupleTyCon { data_con :: AlgTyConRhs -> DataCon
data_con = DataCon
c }   -> DataCon -> Maybe DataCon
forall a. a -> Maybe a
Just DataCon
c
      NewTyCon { data_con :: AlgTyConRhs -> DataCon
data_con = DataCon
c }     -> DataCon -> Maybe DataCon
forall a. a -> Maybe a
Just DataCon
c
      AlgTyConRhs
_                             -> Maybe DataCon
forall a. Maybe a
Nothing
tyConSingleDataCon_maybe TyCon
_           = Maybe DataCon
forall a. Maybe a
Nothing

-- | Like 'tyConSingleDataCon_maybe', but panics if 'Nothing'.
tyConSingleDataCon :: TyCon -> DataCon
tyConSingleDataCon :: TyCon -> DataCon
tyConSingleDataCon TyCon
tc
  = case TyCon -> Maybe DataCon
tyConSingleDataCon_maybe TyCon
tc of
      Just DataCon
c  -> DataCon
c
      Maybe DataCon
Nothing -> String -> SDoc -> DataCon
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tyConDataCon" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc)

-- | Like 'tyConSingleDataCon_maybe', but returns 'Nothing' for newtypes.
tyConSingleAlgDataCon_maybe :: TyCon -> Maybe DataCon
tyConSingleAlgDataCon_maybe :: TyCon -> Maybe DataCon
tyConSingleAlgDataCon_maybe TyCon
tycon
  | TyCon -> Bool
isNewTyCon TyCon
tycon = Maybe DataCon
forall a. Maybe a
Nothing
  | Bool
otherwise        = TyCon -> Maybe DataCon
tyConSingleDataCon_maybe TyCon
tycon

-- | Returns @Just dcs@ if the given 'TyCon' is a @data@ type, a tuple type
-- or a sum type with data constructors dcs. If the 'TyCon' has more than one
-- constructor, or represents a primitive or function type constructor then
-- @Nothing@ is returned.
--
-- Like 'tyConDataCons_maybe', but returns 'Nothing' for newtypes.
tyConAlgDataCons_maybe :: TyCon -> Maybe [DataCon]
tyConAlgDataCons_maybe :: TyCon -> Maybe [DataCon]
tyConAlgDataCons_maybe TyCon
tycon
  | TyCon -> Bool
isNewTyCon TyCon
tycon = Maybe [DataCon]
forall a. Maybe a
Nothing
  | Bool
otherwise        = TyCon -> Maybe [DataCon]
tyConDataCons_maybe TyCon
tycon

-- | Determine the number of value constructors a 'TyCon' has. Panics if the
-- 'TyCon' is not algebraic or a tuple
tyConFamilySize  :: TyCon -> Int
tyConFamilySize :: TyCon -> Int
tyConFamilySize tc :: TyCon
tc@(AlgTyCon { algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs })
  = case AlgTyConRhs
rhs of
      DataTyCon { data_cons_size :: AlgTyConRhs -> Int
data_cons_size = Int
size } -> Int
size
      NewTyCon {}                    -> Int
1
      TupleTyCon {}                  -> Int
1
      SumTyCon { data_cons_size :: AlgTyConRhs -> Int
data_cons_size = Int
size }  -> Int
size
      AlgTyConRhs
_                              -> String -> SDoc -> Int
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tyConFamilySize 1" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc)
tyConFamilySize TyCon
tc = String -> SDoc -> Int
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tyConFamilySize 2" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc)

-- | Extract an 'AlgTyConRhs' with information about data constructors from an
-- algebraic or tuple 'TyCon'. Panics for any other sort of 'TyCon'
algTyConRhs :: TyCon -> AlgTyConRhs
algTyConRhs :: TyCon -> AlgTyConRhs
algTyConRhs (AlgTyCon {algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs}) = AlgTyConRhs
rhs
algTyConRhs TyCon
other = String -> SDoc -> AlgTyConRhs
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"algTyConRhs" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
other)

-- | Extract type variable naming the result of injective type family
tyConFamilyResVar_maybe :: TyCon -> Maybe Name
tyConFamilyResVar_maybe :: TyCon -> Maybe Name
tyConFamilyResVar_maybe (FamilyTyCon {famTcResVar :: TyCon -> Maybe Name
famTcResVar = Maybe Name
res}) = Maybe Name
res
tyConFamilyResVar_maybe TyCon
_                                 = Maybe Name
forall a. Maybe a
Nothing

-- | Get the list of roles for the type parameters of a TyCon
tyConRoles :: TyCon -> [Role]
-- See also Note [TyCon Role signatures]
tyConRoles :: TyCon -> [Role]
tyConRoles TyCon
tc
  = case TyCon
tc of
    { FunTyCon {}                         -> [Role
Nominal, Role
Nominal, Role
Nominal, Role
Representational, Role
Representational]
    ; AlgTyCon { tcRoles :: TyCon -> [Role]
tcRoles = [Role]
roles }        -> [Role]
roles
    ; SynonymTyCon { tcRoles :: TyCon -> [Role]
tcRoles = [Role]
roles }    -> [Role]
roles
    ; FamilyTyCon {}                      -> Role -> [Role]
const_role Role
Nominal
    ; PrimTyCon { tcRoles :: TyCon -> [Role]
tcRoles = [Role]
roles }       -> [Role]
roles
    ; PromotedDataCon { tcRoles :: TyCon -> [Role]
tcRoles = [Role]
roles } -> [Role]
roles
    ; TcTyCon {}                          -> Role -> [Role]
const_role Role
Nominal
    }
  where
    const_role :: Role -> [Role]
const_role Role
r = Int -> Role -> [Role]
forall a. Int -> a -> [a]
replicate (TyCon -> Int
tyConArity TyCon
tc) Role
r

-- | Extract the bound type variables and type expansion of a type synonym
-- 'TyCon'. Panics if the 'TyCon' is not a synonym
newTyConRhs :: TyCon -> ([TyVar], Type)
newTyConRhs :: TyCon -> ([TyVar], Kind)
newTyConRhs (AlgTyCon {tyConTyVars :: TyCon -> [TyVar]
tyConTyVars = [TyVar]
tvs, algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = NewTyCon { nt_rhs :: AlgTyConRhs -> Kind
nt_rhs = Kind
rhs }})
    = ([TyVar]
tvs, Kind
rhs)
newTyConRhs TyCon
tycon = String -> SDoc -> ([TyVar], Kind)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"newTyConRhs" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tycon)

-- | The number of type parameters that need to be passed to a newtype to
-- resolve it. May be less than in the definition if it can be eta-contracted.
newTyConEtadArity :: TyCon -> Int
newTyConEtadArity :: TyCon -> Int
newTyConEtadArity (AlgTyCon {algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = NewTyCon { nt_etad_rhs :: AlgTyConRhs -> ([TyVar], Kind)
nt_etad_rhs = ([TyVar], Kind)
tvs_rhs }})
        = [TyVar] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (([TyVar], Kind) -> [TyVar]
forall a b. (a, b) -> a
fst ([TyVar], Kind)
tvs_rhs)
newTyConEtadArity TyCon
tycon = String -> SDoc -> Int
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"newTyConEtadArity" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tycon)

-- | Extract the bound type variables and type expansion of an eta-contracted
-- type synonym 'TyCon'.  Panics if the 'TyCon' is not a synonym
newTyConEtadRhs :: TyCon -> ([TyVar], Type)
newTyConEtadRhs :: TyCon -> ([TyVar], Kind)
newTyConEtadRhs (AlgTyCon {algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = NewTyCon { nt_etad_rhs :: AlgTyConRhs -> ([TyVar], Kind)
nt_etad_rhs = ([TyVar], Kind)
tvs_rhs }}) = ([TyVar], Kind)
tvs_rhs
newTyConEtadRhs TyCon
tycon = String -> SDoc -> ([TyVar], Kind)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"newTyConEtadRhs" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tycon)

-- | Extracts the @newtype@ coercion from such a 'TyCon', which can be used to
-- construct something with the @newtype@s type from its representation type
-- (right hand side). If the supplied 'TyCon' is not a @newtype@, returns
-- @Nothing@
newTyConCo_maybe :: TyCon -> Maybe (CoAxiom Unbranched)
newTyConCo_maybe :: TyCon -> Maybe (CoAxiom Unbranched)
newTyConCo_maybe (AlgTyCon {algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = NewTyCon { nt_co :: AlgTyConRhs -> CoAxiom Unbranched
nt_co = CoAxiom Unbranched
co }}) = CoAxiom Unbranched -> Maybe (CoAxiom Unbranched)
forall a. a -> Maybe a
Just CoAxiom Unbranched
co
newTyConCo_maybe TyCon
_                                               = Maybe (CoAxiom Unbranched)
forall a. Maybe a
Nothing

newTyConCo :: TyCon -> CoAxiom Unbranched
newTyConCo :: TyCon -> CoAxiom Unbranched
newTyConCo TyCon
tc = case TyCon -> Maybe (CoAxiom Unbranched)
newTyConCo_maybe TyCon
tc of
                 Just CoAxiom Unbranched
co -> CoAxiom Unbranched
co
                 Maybe (CoAxiom Unbranched)
Nothing -> String -> SDoc -> CoAxiom Unbranched
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"newTyConCo" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc)

newTyConDataCon_maybe :: TyCon -> Maybe DataCon
newTyConDataCon_maybe :: TyCon -> Maybe DataCon
newTyConDataCon_maybe (AlgTyCon {algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = NewTyCon { data_con :: AlgTyConRhs -> DataCon
data_con = DataCon
con }}) = DataCon -> Maybe DataCon
forall a. a -> Maybe a
Just DataCon
con
newTyConDataCon_maybe TyCon
_ = Maybe DataCon
forall a. Maybe a
Nothing

-- | Find the \"stupid theta\" of the 'TyCon'. A \"stupid theta\" is the context
-- to the left of an algebraic type declaration, e.g. @Eq a@ in the declaration
-- @data Eq a => T a ...@. See @Note [The stupid context]@ in "GHC.Core.DataCon".
tyConStupidTheta :: TyCon -> [PredType]
tyConStupidTheta :: TyCon -> [Kind]
tyConStupidTheta (AlgTyCon {algTcStupidTheta :: TyCon -> [Kind]
algTcStupidTheta = [Kind]
stupid}) = [Kind]
stupid
tyConStupidTheta (FunTyCon {}) = []
tyConStupidTheta TyCon
tycon = String -> SDoc -> [Kind]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tyConStupidTheta" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tycon)

-- | Extract the 'TyVar's bound by a vanilla type synonym
-- and the corresponding (unsubstituted) right hand side.
synTyConDefn_maybe :: TyCon -> Maybe ([TyVar], Type)
synTyConDefn_maybe :: TyCon -> Maybe ([TyVar], Kind)
synTyConDefn_maybe (SynonymTyCon {tyConTyVars :: TyCon -> [TyVar]
tyConTyVars = [TyVar]
tyvars, synTcRhs :: TyCon -> Kind
synTcRhs = Kind
ty})
  = ([TyVar], Kind) -> Maybe ([TyVar], Kind)
forall a. a -> Maybe a
Just ([TyVar]
tyvars, Kind
ty)
synTyConDefn_maybe TyCon
_ = Maybe ([TyVar], Kind)
forall a. Maybe a
Nothing

-- | Extract the information pertaining to the right hand side of a type synonym
-- (@type@) declaration.
synTyConRhs_maybe :: TyCon -> Maybe Type
synTyConRhs_maybe :: TyCon -> Maybe Kind
synTyConRhs_maybe (SynonymTyCon {synTcRhs :: TyCon -> Kind
synTcRhs = Kind
rhs}) = Kind -> Maybe Kind
forall a. a -> Maybe a
Just Kind
rhs
synTyConRhs_maybe TyCon
_                               = Maybe Kind
forall a. Maybe a
Nothing

-- | Extract the flavour of a type family (with all the extra information that
-- it carries)
famTyConFlav_maybe :: TyCon -> Maybe FamTyConFlav
famTyConFlav_maybe :: TyCon -> Maybe FamTyConFlav
famTyConFlav_maybe (FamilyTyCon {famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = FamTyConFlav
flav}) = FamTyConFlav -> Maybe FamTyConFlav
forall a. a -> Maybe a
Just FamTyConFlav
flav
famTyConFlav_maybe TyCon
_                                = Maybe FamTyConFlav
forall a. Maybe a
Nothing

-- | Is this 'TyCon' that for a class instance?
isClassTyCon :: TyCon -> Bool
isClassTyCon :: TyCon -> Bool
isClassTyCon (AlgTyCon {algTcFlavour :: TyCon -> AlgTyConFlav
algTcFlavour = ClassTyCon {}}) = Bool
True
isClassTyCon TyCon
_                                        = Bool
False

-- | If this 'TyCon' is that for a class instance, return the class it is for.
-- Otherwise returns @Nothing@
tyConClass_maybe :: TyCon -> Maybe Class
tyConClass_maybe :: TyCon -> Maybe Class
tyConClass_maybe (AlgTyCon {algTcFlavour :: TyCon -> AlgTyConFlav
algTcFlavour = ClassTyCon Class
clas Name
_}) = Class -> Maybe Class
forall a. a -> Maybe a
Just Class
clas
tyConClass_maybe TyCon
_                                            = Maybe Class
forall a. Maybe a
Nothing

-- | Return the associated types of the 'TyCon', if any
tyConATs :: TyCon -> [TyCon]
tyConATs :: TyCon -> [TyCon]
tyConATs (AlgTyCon {algTcFlavour :: TyCon -> AlgTyConFlav
algTcFlavour = ClassTyCon Class
clas Name
_}) = Class -> [TyCon]
classATs Class
clas
tyConATs TyCon
_                                            = []

----------------------------------------------------------------------------
-- | Is this 'TyCon' that for a data family instance?
isFamInstTyCon :: TyCon -> Bool
isFamInstTyCon :: TyCon -> Bool
isFamInstTyCon (AlgTyCon {algTcFlavour :: TyCon -> AlgTyConFlav
algTcFlavour = DataFamInstTyCon {} })
  = Bool
True
isFamInstTyCon TyCon
_ = Bool
False

tyConFamInstSig_maybe :: TyCon -> Maybe (TyCon, [Type], CoAxiom Unbranched)
tyConFamInstSig_maybe :: TyCon -> Maybe (TyCon, [Kind], CoAxiom Unbranched)
tyConFamInstSig_maybe (AlgTyCon {algTcFlavour :: TyCon -> AlgTyConFlav
algTcFlavour = DataFamInstTyCon CoAxiom Unbranched
ax TyCon
f [Kind]
ts })
  = (TyCon, [Kind], CoAxiom Unbranched)
-> Maybe (TyCon, [Kind], CoAxiom Unbranched)
forall a. a -> Maybe a
Just (TyCon
f, [Kind]
ts, CoAxiom Unbranched
ax)
tyConFamInstSig_maybe TyCon
_ = Maybe (TyCon, [Kind], CoAxiom Unbranched)
forall a. Maybe a
Nothing

-- | If this 'TyCon' is that of a data family instance, return the family in question
-- and the instance types. Otherwise, return @Nothing@
tyConFamInst_maybe :: TyCon -> Maybe (TyCon, [Type])
tyConFamInst_maybe :: TyCon -> Maybe (TyCon, [Kind])
tyConFamInst_maybe (AlgTyCon {algTcFlavour :: TyCon -> AlgTyConFlav
algTcFlavour = DataFamInstTyCon CoAxiom Unbranched
_ TyCon
f [Kind]
ts })
  = (TyCon, [Kind]) -> Maybe (TyCon, [Kind])
forall a. a -> Maybe a
Just (TyCon
f, [Kind]
ts)
tyConFamInst_maybe TyCon
_ = Maybe (TyCon, [Kind])
forall a. Maybe a
Nothing

-- | If this 'TyCon' is that of a data family instance, return a 'TyCon' which
-- represents a coercion identifying the representation type with the type
-- instance family.  Otherwise, return @Nothing@
tyConFamilyCoercion_maybe :: TyCon -> Maybe (CoAxiom Unbranched)
tyConFamilyCoercion_maybe :: TyCon -> Maybe (CoAxiom Unbranched)
tyConFamilyCoercion_maybe (AlgTyCon {algTcFlavour :: TyCon -> AlgTyConFlav
algTcFlavour = DataFamInstTyCon CoAxiom Unbranched
ax TyCon
_ [Kind]
_ })
  = CoAxiom Unbranched -> Maybe (CoAxiom Unbranched)
forall a. a -> Maybe a
Just CoAxiom Unbranched
ax
tyConFamilyCoercion_maybe TyCon
_ = Maybe (CoAxiom Unbranched)
forall a. Maybe a
Nothing

-- | Extract any 'RuntimeRepInfo' from this TyCon
tyConRuntimeRepInfo :: TyCon -> RuntimeRepInfo
tyConRuntimeRepInfo :: TyCon -> RuntimeRepInfo
tyConRuntimeRepInfo (PromotedDataCon { promDcRepInfo :: TyCon -> RuntimeRepInfo
promDcRepInfo = RuntimeRepInfo
rri }) = RuntimeRepInfo
rri
tyConRuntimeRepInfo TyCon
_                                         = RuntimeRepInfo
NoRRI
  -- could panic in that second case. But Douglas Adams told me not to.

{-
Note [Constructor tag allocation]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When typechecking we need to allocate constructor tags to constructors.
They are allocated based on the position in the data_cons field of TyCon,
with the first constructor getting fIRST_TAG.

We used to pay linear cost per constructor, with each constructor looking up
its relative index in the constructor list. That was quadratic and prohibitive
for large data types with more than 10k constructors.

The current strategy is to build a NameEnv with a mapping from constructor's
Name to ConTag and pass it down to buildDataCon for efficient lookup.

Relevant ticket: #14657
-}

mkTyConTagMap :: TyCon -> NameEnv ConTag
mkTyConTagMap :: TyCon -> NameEnv Int
mkTyConTagMap TyCon
tycon =
  [(Name, Int)] -> NameEnv Int
forall a. [(Name, a)] -> NameEnv a
mkNameEnv ([(Name, Int)] -> NameEnv Int) -> [(Name, Int)] -> NameEnv Int
forall a b. (a -> b) -> a -> b
$ (DataCon -> Name) -> [DataCon] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map DataCon -> Name
forall a. NamedThing a => a -> Name
getName (TyCon -> [DataCon]
tyConDataCons TyCon
tycon) [Name] -> [Int] -> [(Name, Int)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Int
fIRST_TAG..]
  -- See Note [Constructor tag allocation]

{-
************************************************************************
*                                                                      *
\subsection[TyCon-instances]{Instance declarations for @TyCon@}
*                                                                      *
************************************************************************

@TyCon@s are compared by comparing their @Unique@s.
-}

instance Eq TyCon where
    TyCon
a == :: TyCon -> TyCon -> Bool
== TyCon
b = TyCon -> Unique
forall a. Uniquable a => a -> Unique
getUnique TyCon
a Unique -> Unique -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon -> Unique
forall a. Uniquable a => a -> Unique
getUnique TyCon
b
    TyCon
a /= :: TyCon -> TyCon -> Bool
/= TyCon
b = TyCon -> Unique
forall a. Uniquable a => a -> Unique
getUnique TyCon
a Unique -> Unique -> Bool
forall a. Eq a => a -> a -> Bool
/= TyCon -> Unique
forall a. Uniquable a => a -> Unique
getUnique TyCon
b

instance Uniquable TyCon where
    getUnique :: TyCon -> Unique
getUnique TyCon
tc = TyCon -> Unique
tyConUnique TyCon
tc

instance Outputable TyCon where
  -- At the moment a promoted TyCon has the same Name as its
  -- corresponding TyCon, so we add the quote to distinguish it here
  ppr :: TyCon -> SDoc
ppr TyCon
tc = TyCon -> SDoc
pprPromotionQuote TyCon
tc SDoc -> SDoc -> SDoc
<> Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr (TyCon -> Name
tyConName TyCon
tc) SDoc -> SDoc -> SDoc
<> SDoc
pp_tc
    where
      pp_tc :: SDoc
pp_tc = (PprStyle -> SDoc) -> SDoc
getPprStyle ((PprStyle -> SDoc) -> SDoc) -> (PprStyle -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \PprStyle
sty ->
              (Bool -> SDoc) -> SDoc
getPprDebug ((Bool -> SDoc) -> SDoc) -> (Bool -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \Bool
debug ->
               if ((Bool
debug Bool -> Bool -> Bool
|| PprStyle -> Bool
dumpStyle PprStyle
sty) Bool -> Bool -> Bool
&& TyCon -> Bool
isTcTyCon TyCon
tc)
                  then String -> SDoc
text String
"[tc]"
                  else SDoc
empty

-- | Paints a picture of what a 'TyCon' represents, in broad strokes.
-- This is used towards more informative error messages.
data TyConFlavour
  = ClassFlavour
  | TupleFlavour Boxity
  | SumFlavour
  | DataTypeFlavour
  | NewtypeFlavour
  | AbstractTypeFlavour
  | DataFamilyFlavour (Maybe TyCon)     -- Just tc <=> (tc == associated class)
  | OpenTypeFamilyFlavour (Maybe TyCon) -- Just tc <=> (tc == associated class)
  | ClosedTypeFamilyFlavour
  | TypeSynonymFlavour
  | BuiltInTypeFlavour -- ^ e.g., the @(->)@ 'TyCon'.
  | PromotedDataConFlavour
  deriving TyConFlavour -> TyConFlavour -> Bool
(TyConFlavour -> TyConFlavour -> Bool)
-> (TyConFlavour -> TyConFlavour -> Bool) -> Eq TyConFlavour
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TyConFlavour -> TyConFlavour -> Bool
== :: TyConFlavour -> TyConFlavour -> Bool
$c/= :: TyConFlavour -> TyConFlavour -> Bool
/= :: TyConFlavour -> TyConFlavour -> Bool
Eq

instance Outputable TyConFlavour where
  ppr :: TyConFlavour -> SDoc
ppr = String -> SDoc
text (String -> SDoc)
-> (TyConFlavour -> String) -> TyConFlavour -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyConFlavour -> String
go
    where
      go :: TyConFlavour -> String
go TyConFlavour
ClassFlavour = String
"class"
      go (TupleFlavour Boxity
boxed) | Boxity -> Bool
isBoxed Boxity
boxed = String
"tuple"
                              | Bool
otherwise     = String
"unboxed tuple"
      go TyConFlavour
SumFlavour              = String
"unboxed sum"
      go TyConFlavour
DataTypeFlavour         = String
"data type"
      go TyConFlavour
NewtypeFlavour          = String
"newtype"
      go TyConFlavour
AbstractTypeFlavour     = String
"abstract type"
      go (DataFamilyFlavour (Just TyCon
_))  = String
"associated data family"
      go (DataFamilyFlavour Maybe TyCon
Nothing)   = String
"data family"
      go (OpenTypeFamilyFlavour (Just TyCon
_)) = String
"associated type family"
      go (OpenTypeFamilyFlavour Maybe TyCon
Nothing)  = String
"type family"
      go TyConFlavour
ClosedTypeFamilyFlavour = String
"type family"
      go TyConFlavour
TypeSynonymFlavour      = String
"type synonym"
      go TyConFlavour
BuiltInTypeFlavour      = String
"built-in type"
      go TyConFlavour
PromotedDataConFlavour  = String
"promoted data constructor"

tyConFlavour :: TyCon -> TyConFlavour
tyConFlavour :: TyCon -> TyConFlavour
tyConFlavour (AlgTyCon { algTcFlavour :: TyCon -> AlgTyConFlav
algTcFlavour = AlgTyConFlav
parent, algTcRhs :: TyCon -> AlgTyConRhs
algTcRhs = AlgTyConRhs
rhs })
  | ClassTyCon Class
_ Name
_ <- AlgTyConFlav
parent = TyConFlavour
ClassFlavour
  | Bool
otherwise = case AlgTyConRhs
rhs of
                  TupleTyCon { tup_sort :: AlgTyConRhs -> TupleSort
tup_sort = TupleSort
sort }
                                     -> Boxity -> TyConFlavour
TupleFlavour (TupleSort -> Boxity
tupleSortBoxity TupleSort
sort)
                  SumTyCon {}        -> TyConFlavour
SumFlavour
                  DataTyCon {}       -> TyConFlavour
DataTypeFlavour
                  NewTyCon {}        -> TyConFlavour
NewtypeFlavour
                  AbstractTyCon {}   -> TyConFlavour
AbstractTypeFlavour
tyConFlavour (FamilyTyCon { famTcFlav :: TyCon -> FamTyConFlav
famTcFlav = FamTyConFlav
flav, famTcParent :: TyCon -> Maybe TyCon
famTcParent = Maybe TyCon
parent })
  = case FamTyConFlav
flav of
      DataFamilyTyCon{}            -> Maybe TyCon -> TyConFlavour
DataFamilyFlavour Maybe TyCon
parent
      FamTyConFlav
OpenSynFamilyTyCon           -> Maybe TyCon -> TyConFlavour
OpenTypeFamilyFlavour Maybe TyCon
parent
      ClosedSynFamilyTyCon{}       -> TyConFlavour
ClosedTypeFamilyFlavour
      FamTyConFlav
AbstractClosedSynFamilyTyCon -> TyConFlavour
ClosedTypeFamilyFlavour
      BuiltInSynFamTyCon{}         -> TyConFlavour
ClosedTypeFamilyFlavour
tyConFlavour (SynonymTyCon {})    = TyConFlavour
TypeSynonymFlavour
tyConFlavour (FunTyCon {})        = TyConFlavour
BuiltInTypeFlavour
tyConFlavour (PrimTyCon {})       = TyConFlavour
BuiltInTypeFlavour
tyConFlavour (PromotedDataCon {}) = TyConFlavour
PromotedDataConFlavour
tyConFlavour (TcTyCon { tcTyConFlavour :: TyCon -> TyConFlavour
tcTyConFlavour = TyConFlavour
flav }) = TyConFlavour
flav

-- | Can this flavour of 'TyCon' appear unsaturated?
tcFlavourMustBeSaturated :: TyConFlavour -> Bool
tcFlavourMustBeSaturated :: TyConFlavour -> Bool
tcFlavourMustBeSaturated TyConFlavour
ClassFlavour            = Bool
False
tcFlavourMustBeSaturated TyConFlavour
DataTypeFlavour         = Bool
False
tcFlavourMustBeSaturated TyConFlavour
NewtypeFlavour          = Bool
False
tcFlavourMustBeSaturated DataFamilyFlavour{}     = Bool
False
tcFlavourMustBeSaturated TupleFlavour{}          = Bool
False
tcFlavourMustBeSaturated TyConFlavour
SumFlavour              = Bool
False
tcFlavourMustBeSaturated AbstractTypeFlavour {}  = Bool
False
tcFlavourMustBeSaturated TyConFlavour
BuiltInTypeFlavour      = Bool
False
tcFlavourMustBeSaturated TyConFlavour
PromotedDataConFlavour  = Bool
False
tcFlavourMustBeSaturated TyConFlavour
TypeSynonymFlavour      = Bool
True
tcFlavourMustBeSaturated OpenTypeFamilyFlavour{} = Bool
True
tcFlavourMustBeSaturated TyConFlavour
ClosedTypeFamilyFlavour = Bool
True

-- | Is this flavour of 'TyCon' an open type family or a data family?
tcFlavourIsOpen :: TyConFlavour -> Bool
tcFlavourIsOpen :: TyConFlavour -> Bool
tcFlavourIsOpen DataFamilyFlavour{}     = Bool
True
tcFlavourIsOpen OpenTypeFamilyFlavour{} = Bool
True
tcFlavourIsOpen TyConFlavour
ClosedTypeFamilyFlavour = Bool
False
tcFlavourIsOpen TyConFlavour
ClassFlavour            = Bool
False
tcFlavourIsOpen TyConFlavour
DataTypeFlavour         = Bool
False
tcFlavourIsOpen TyConFlavour
NewtypeFlavour          = Bool
False
tcFlavourIsOpen TupleFlavour{}          = Bool
False
tcFlavourIsOpen TyConFlavour
SumFlavour              = Bool
False
tcFlavourIsOpen AbstractTypeFlavour {}  = Bool
False
tcFlavourIsOpen TyConFlavour
BuiltInTypeFlavour      = Bool
False
tcFlavourIsOpen TyConFlavour
PromotedDataConFlavour  = Bool
False
tcFlavourIsOpen TyConFlavour
TypeSynonymFlavour      = Bool
False

pprPromotionQuote :: TyCon -> SDoc
-- Promoted data constructors already have a tick in their OccName
pprPromotionQuote :: TyCon -> SDoc
pprPromotionQuote TyCon
tc
  = case TyCon
tc of
      PromotedDataCon {} -> Char -> SDoc
char Char
'\'' -- Always quote promoted DataCons in types
      TyCon
_                  -> SDoc
empty

instance NamedThing TyCon where
    getName :: TyCon -> Name
getName = TyCon -> Name
tyConName

instance Data.Data TyCon where
    -- don't traverse?
    toConstr :: TyCon -> Constr
toConstr TyCon
_   = String -> Constr
abstractConstr String
"TyCon"
    gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TyCon
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
_  = String -> Constr -> c TyCon
forall a. HasCallStack => String -> a
error String
"gunfold"
    dataTypeOf :: TyCon -> DataType
dataTypeOf TyCon
_ = String -> DataType
mkNoRepType String
"TyCon"

instance Binary Injectivity where
    put_ :: BinHandle -> Injectivity -> IO ()
put_ BinHandle
bh Injectivity
NotInjective   = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0
    put_ BinHandle
bh (Injective [Bool]
xs) = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1 IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> BinHandle -> [Bool] -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh [Bool]
xs

    get :: BinHandle -> IO Injectivity
get BinHandle
bh = do { Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
                ; case Word8
h of
                    Word8
0 -> Injectivity -> IO Injectivity
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Injectivity
NotInjective
                    Word8
_ -> do { [Bool]
xs <- BinHandle -> IO [Bool]
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                            ; Injectivity -> IO Injectivity
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Bool] -> Injectivity
Injective [Bool]
xs) } }

-- | Returns whether or not this 'TyCon' is definite, or a hole
-- that may be filled in at some later point.  See Note [Skolem abstract data]
tyConSkolem :: TyCon -> Bool
tyConSkolem :: TyCon -> Bool
tyConSkolem = Name -> Bool
isHoleName (Name -> Bool) -> (TyCon -> Name) -> TyCon -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyCon -> Name
tyConName

-- Note [Skolem abstract data]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- Skolem abstract data arises from data declarations in an hsig file.
--
-- The best analogy is to interpret the types declared in signature files as
-- elaborating to universally quantified type variables; e.g.,
--
--    unit p where
--        signature H where
--            data T
--            data S
--        module M where
--            import H
--            f :: (T ~ S) => a -> b
--            f x = x
--
-- elaborates as (with some fake structural types):
--
--    p :: forall t s. { f :: forall a b. t ~ s => a -> b }
--    p = { f = \x -> x } -- ill-typed
--
-- It is clear that inside p, t ~ s is not provable (and
-- if we tried to write a function to cast t to s, that
-- would not work), but if we call p @Int @Int, clearly Int ~ Int
-- is provable.  The skolem variables are all distinct from
-- one another, but we can't make assumptions like "f is
-- inaccessible", because the skolem variables will get
-- instantiated eventually!
--
-- Skolem abstractness can apply to "non-abstract" data as well):
--
--    unit p where
--        signature H1 where
--            data T = MkT
--        signature H2 where
--            data T = MkT
--        module M where
--            import qualified H1
--            import qualified H2
--            f :: (H1.T ~ H2.T) => a -> b
--            f x = x
--
-- This is why the test is on the original name of the TyCon,
-- not whether it is abstract or not.