{-# LANGUAGE CPP #-}
module PprTyThing (
pprTyThing,
pprTyThingInContext,
pprTyThingLoc,
pprTyThingInContextLoc,
pprTyThingHdr,
pprTypeForUser,
pprFamInst
) where
#include "HsVersions.h"
import GhcPrelude
import Type ( ArgFlag(..), TyThing(..), mkTyVarBinders, pprUserForAll )
import IfaceSyn ( ShowSub(..), ShowHowMuch(..), AltPpr(..)
, showToHeader, pprIfaceDecl )
import CoAxiom ( coAxiomTyCon )
import HscTypes( tyThingParent_maybe )
import MkIface ( tyThingToIfaceDecl )
import Type ( tidyOpenType )
import FamInstEnv( FamInst(..), FamFlavor(..) )
import Type( Type, pprTypeApp, pprSigmaType )
import Name
import VarEnv( emptyTidyEnv )
import Outputable
pprFamInst :: FamInst -> SDoc
pprFamInst :: FamInst -> SDoc
pprFamInst (FamInst { fi_flavor :: FamInst -> FamFlavor
fi_flavor = DataFamilyInst rep_tc :: TyCon
rep_tc })
= TyThing -> SDoc
pprTyThingInContextLoc (TyCon -> TyThing
ATyCon TyCon
rep_tc)
pprFamInst (FamInst { fi_flavor :: FamInst -> FamFlavor
fi_flavor = FamFlavor
SynFamilyInst, fi_axiom :: FamInst -> CoAxiom Unbranched
fi_axiom = CoAxiom Unbranched
axiom
, fi_tvs :: FamInst -> [TyVar]
fi_tvs = [TyVar]
tvs, fi_tys :: FamInst -> [Type]
fi_tys = [Type]
lhs_tys, fi_rhs :: FamInst -> Type
fi_rhs = Type
rhs })
= SDoc -> SDoc -> SDoc
showWithLoc (Name -> SDoc
pprDefinedAt (CoAxiom Unbranched -> Name
forall a. NamedThing a => a -> Name
getName CoAxiom Unbranched
axiom)) (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$
SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text "type instance"
SDoc -> SDoc -> SDoc
<+> [TyCoVarBinder] -> SDoc
pprUserForAll (ArgFlag -> [TyVar] -> [TyCoVarBinder]
mkTyVarBinders ArgFlag
Specified [TyVar]
tvs)
SDoc -> SDoc -> SDoc
<+> TyCon -> [Type] -> SDoc
pprTypeApp (CoAxiom Unbranched -> TyCon
forall (br :: BranchFlag). CoAxiom br -> TyCon
coAxiomTyCon CoAxiom Unbranched
axiom) [Type]
lhs_tys)
2 (SDoc
equals SDoc -> SDoc -> SDoc
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
rhs)
pprTyThingLoc :: TyThing -> SDoc
pprTyThingLoc :: TyThing -> SDoc
pprTyThingLoc tyThing :: TyThing
tyThing
= SDoc -> SDoc -> SDoc
showWithLoc (Name -> SDoc
pprDefinedAt (TyThing -> Name
forall a. NamedThing a => a -> Name
getName TyThing
tyThing))
(ShowSub -> TyThing -> SDoc
pprTyThing ShowSub
showToHeader TyThing
tyThing)
pprTyThingHdr :: TyThing -> SDoc
pprTyThingHdr :: TyThing -> SDoc
pprTyThingHdr = ShowSub -> TyThing -> SDoc
pprTyThing ShowSub
showToHeader
pprTyThingInContext :: ShowSub -> TyThing -> SDoc
pprTyThingInContext :: ShowSub -> TyThing -> SDoc
pprTyThingInContext show_sub :: ShowSub
show_sub thing :: TyThing
thing
= [OccName] -> TyThing -> SDoc
go [] TyThing
thing
where
go :: [OccName] -> TyThing -> SDoc
go ss :: [OccName]
ss thing :: TyThing
thing
= case TyThing -> Maybe TyThing
tyThingParent_maybe TyThing
thing of
Just parent :: TyThing
parent ->
[OccName] -> TyThing -> SDoc
go (TyThing -> OccName
forall a. NamedThing a => a -> OccName
getOccName TyThing
thing OccName -> [OccName] -> [OccName]
forall a. a -> [a] -> [a]
: [OccName]
ss) TyThing
parent
Nothing ->
ShowSub -> TyThing -> SDoc
pprTyThing
(ShowSub
show_sub { ss_how_much :: ShowHowMuch
ss_how_much = [OccName] -> AltPpr -> ShowHowMuch
ShowSome [OccName]
ss (Maybe (OccName -> SDoc) -> AltPpr
AltPpr Maybe (OccName -> SDoc)
forall a. Maybe a
Nothing) })
TyThing
thing
pprTyThingInContextLoc :: TyThing -> SDoc
pprTyThingInContextLoc :: TyThing -> SDoc
pprTyThingInContextLoc tyThing :: TyThing
tyThing
= SDoc -> SDoc -> SDoc
showWithLoc (Name -> SDoc
pprDefinedAt (TyThing -> Name
forall a. NamedThing a => a -> Name
getName TyThing
tyThing))
(ShowSub -> TyThing -> SDoc
pprTyThingInContext ShowSub
showToHeader TyThing
tyThing)
pprTyThing :: ShowSub -> TyThing -> SDoc
pprTyThing :: ShowSub -> TyThing -> SDoc
pprTyThing ss :: ShowSub
ss ty_thing :: TyThing
ty_thing
= ShowSub -> IfaceDecl -> SDoc
pprIfaceDecl ShowSub
ss' (TyThing -> IfaceDecl
tyThingToIfaceDecl TyThing
ty_thing)
where
ss' :: ShowSub
ss' = case ShowSub -> ShowHowMuch
ss_how_much ShowSub
ss of
ShowHeader (AltPpr Nothing) -> ShowSub
ss { ss_how_much :: ShowHowMuch
ss_how_much = AltPpr -> ShowHowMuch
ShowHeader AltPpr
ppr' }
ShowSome xs :: [OccName]
xs (AltPpr Nothing) -> ShowSub
ss { ss_how_much :: ShowHowMuch
ss_how_much = [OccName] -> AltPpr -> ShowHowMuch
ShowSome [OccName]
xs AltPpr
ppr' }
_ -> ShowSub
ss
ppr' :: AltPpr
ppr' = Maybe (OccName -> SDoc) -> AltPpr
AltPpr (Maybe (OccName -> SDoc) -> AltPpr)
-> Maybe (OccName -> SDoc) -> AltPpr
forall a b. (a -> b) -> a -> b
$ Name -> Maybe (OccName -> SDoc)
ppr_bndr (Name -> Maybe (OccName -> SDoc))
-> Name -> Maybe (OccName -> SDoc)
forall a b. (a -> b) -> a -> b
$ TyThing -> Name
forall a. NamedThing a => a -> Name
getName TyThing
ty_thing
ppr_bndr :: Name -> Maybe (OccName -> SDoc)
ppr_bndr :: Name -> Maybe (OccName -> SDoc)
ppr_bndr name :: Name
name
| Name -> Bool
isBuiltInSyntax Name
name
= Maybe (OccName -> SDoc)
forall a. Maybe a
Nothing
| Bool
otherwise
= case Name -> Maybe Module
nameModule_maybe Name
name of
Just mod :: Module
mod -> (OccName -> SDoc) -> Maybe (OccName -> SDoc)
forall a. a -> Maybe a
Just ((OccName -> SDoc) -> Maybe (OccName -> SDoc))
-> (OccName -> SDoc) -> Maybe (OccName -> SDoc)
forall a b. (a -> b) -> a -> b
$ \occ :: OccName
occ -> (PprStyle -> SDoc) -> SDoc
getPprStyle ((PprStyle -> SDoc) -> SDoc) -> (PprStyle -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: PprStyle
sty ->
PprStyle -> Module -> OccName -> SDoc
pprModulePrefix PprStyle
sty Module
mod OccName
occ SDoc -> SDoc -> SDoc
<> OccName -> SDoc
forall a. Outputable a => a -> SDoc
ppr OccName
occ
Nothing -> WARN( True, ppr name ) Nothing
pprTypeForUser :: Type -> SDoc
pprTypeForUser :: Type -> SDoc
pprTypeForUser ty :: Type
ty
= Type -> SDoc
pprSigmaType Type
tidy_ty
where
(_, tidy_ty :: Type
tidy_ty) = TidyEnv -> Type -> (TidyEnv, Type)
tidyOpenType TidyEnv
emptyTidyEnv Type
ty
showWithLoc :: SDoc -> SDoc -> SDoc
showWithLoc :: SDoc -> SDoc -> SDoc
showWithLoc loc :: SDoc
loc doc :: SDoc
doc
= SDoc -> Int -> SDoc -> SDoc
hang SDoc
doc 2 (Char -> SDoc
char '\t' SDoc -> SDoc -> SDoc
<> SDoc
comment SDoc -> SDoc -> SDoc
<+> SDoc
loc)
where
comment :: SDoc
comment = String -> SDoc
text "--"