@@ -35,7 +35,7 @@ module IfaceSyn (
3535 -- Pretty printing
3636 pprIfaceExpr ,
3737 pprIfaceDecl ,
38- ShowSub (.. ), ShowHowMuch (.. )
38+ AltPpr ( .. ), ShowSub (.. ), ShowHowMuch (.. ), showToIface , showToHeader
3939 ) where
4040
4141#include "HsVersions.h"
@@ -572,7 +572,7 @@ instance HasOccName IfaceDecl where
572572 occName = getOccName
573573
574574instance Outputable IfaceDecl where
575- ppr = pprIfaceDecl showAll
575+ ppr = pprIfaceDecl showToIface
576576
577577{-
578578Note [Minimal complete definition] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -583,61 +583,72 @@ filtering of method signatures. Instead we just check if anything at all is
583583filtered and hide it in that case.
584584-}
585585
586- -- TODO: Kill this and Note [Printing IfaceDecl binders]
587586data ShowSub
588587 = ShowSub
589- { ss_ppr_bndr :: OccName -> SDoc -- Pretty-printer for binders in IfaceDecl
590- -- See Note [Printing IfaceDecl binders]
591- , ss_how_much :: ShowHowMuch }
588+ { ss_how_much :: ShowHowMuch
589+ , ss_forall :: ShowForAllFlag }
590+
591+ -- See Note [Printing IfaceDecl binders]
592+ -- The alternative pretty printer referred to in the note.
593+ newtype AltPpr = AltPpr (Maybe (OccName -> SDoc ))
592594
593595data ShowHowMuch
594- = ShowHeader -- Header information only, not rhs
595- | ShowSome [OccName ] -- [] <=> Print all sub-components
596- -- (n:ns) <=> print sub-component 'n' with ShowSub=ns
597- -- elide other sub-components to "..."
598- -- May 14: the list is max 1 element long at the moment
599- | ShowIface -- Everything including GHC-internal information (used in --show-iface)
596+ = ShowHeader AltPpr -- ^ Header information only, not rhs
597+ | ShowSome [OccName ] AltPpr
598+ -- ^ Show only some sub-components. Specifically,
599+ --
600+ -- [@[]@] Print all sub-components.
601+ -- [@(n:ns)@] Print sub-component @n@ with @ShowSub = ns@;
602+ -- elide other sub-components to @...@
603+ -- May 14: the list is max 1 element long at the moment
604+ | ShowIface
605+ -- ^ Everything including GHC-internal information (used in --show-iface)
606+
607+ {-
608+ Note [Printing IfaceDecl binders]
609+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
610+ The binders in an IfaceDecl are just OccNames, so we don't know what module they
611+ come from. But when we pretty-print a TyThing by converting to an IfaceDecl
612+ (see PprTyThing), the TyThing may come from some other module so we really need
613+ the module qualifier. We solve this by passing in a pretty-printer for the
614+ binders.
615+
616+ When printing an interface file (--show-iface), we want to print
617+ everything unqualified, so we can just print the OccName directly.
618+ -}
600619
601620instance Outputable ShowHowMuch where
602- ppr ShowHeader = text " ShowHeader"
603- ppr ShowIface = text " ShowIface"
604- ppr (ShowSome occs) = text " ShowSome" <+> ppr occs
621+ ppr (ShowHeader _) = text " ShowHeader"
622+ ppr ShowIface = text " ShowIface"
623+ ppr (ShowSome occs _) = text " ShowSome" <+> ppr occs
624+
625+ showToHeader :: ShowSub
626+ showToHeader = ShowSub { ss_how_much = ShowHeader $ AltPpr Nothing
627+ , ss_forall = ShowForAllWhen }
605628
606- showAll :: ShowSub
607- showAll = ShowSub { ss_how_much = ShowIface , ss_ppr_bndr = ppr }
629+ showToIface :: ShowSub
630+ showToIface = ShowSub { ss_how_much = ShowIface
631+ , ss_forall = ShowForAllWhen }
608632
609633ppShowIface :: ShowSub -> SDoc -> SDoc
610634ppShowIface (ShowSub { ss_how_much = ShowIface }) doc = doc
611635ppShowIface _ _ = Outputable. empty
612636
613637-- show if all sub-components or the complete interface is shown
614638ppShowAllSubs :: ShowSub -> SDoc -> SDoc -- Note [Minimal complete definition]
615- ppShowAllSubs (ShowSub { ss_how_much = ShowSome [] }) doc = doc
616- ppShowAllSubs (ShowSub { ss_how_much = ShowIface }) doc = doc
617- ppShowAllSubs _ _ = Outputable. empty
639+ ppShowAllSubs (ShowSub { ss_how_much = ShowSome [] _ }) doc = doc
640+ ppShowAllSubs (ShowSub { ss_how_much = ShowIface }) doc = doc
641+ ppShowAllSubs _ _ = Outputable. empty
618642
619643ppShowRhs :: ShowSub -> SDoc -> SDoc
620- ppShowRhs (ShowSub { ss_how_much = ShowHeader }) _ = Outputable. empty
621- ppShowRhs _ doc = doc
644+ ppShowRhs (ShowSub { ss_how_much = ShowHeader _ }) _ = Outputable. empty
645+ ppShowRhs _ doc = doc
622646
623647showSub :: HasOccName n => ShowSub -> n -> Bool
624- showSub (ShowSub { ss_how_much = ShowHeader }) _ = False
625- showSub (ShowSub { ss_how_much = ShowSome (n: _) }) thing = n == occName thing
648+ showSub (ShowSub { ss_how_much = ShowHeader _ }) _ = False
649+ showSub (ShowSub { ss_how_much = ShowSome (n: _) _ }) thing = n == occName thing
626650showSub (ShowSub { ss_how_much = _ }) _ = True
627651
628- {-
629- Note [Printing IfaceDecl binders]
630- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
631- The binders in an IfaceDecl are just OccNames, so we don't know what module they
632- come from. But when we pretty-print a TyThing by converting to an IfaceDecl
633- (see PprTyThing), the TyThing may come from some other module so we really need
634- the module qualifier. We solve this by passing in a pretty-printer for the
635- binders.
636-
637- When printing an interface file (--show-iface), we want to print
638- everything unqualified, so we can just print the OccName directly.
639- -}
640-
641652ppr_trim :: [Maybe SDoc ] -> [SDoc ]
642653-- Collapse a group of Nothings to a single "..."
643654ppr_trim xs
@@ -683,7 +694,9 @@ pprIfaceDecl ss (IfaceData { ifName = tycon, ifCType = ctype,
683694 pp_roles
684695 | is_data_instance = empty
685696 | otherwise = pprRoles (== Representational )
686- (pprPrefixIfDeclBndr ss (occName tycon))
697+ (pprPrefixIfDeclBndr
698+ (ss_how_much ss)
699+ (occName tycon))
687700 binders roles
688701 -- Don't display roles for data family instances (yet)
689702 -- See discussion on Trac #8672.
@@ -714,7 +727,11 @@ pprIfaceDecl ss (IfaceClass { ifATs = ats, ifSigs = sigs
714727 , ifRoles = roles
715728 , ifFDs = fds, ifMinDef = minDef
716729 , ifBinders = binders })
717- = vcat [ pprRoles (== Nominal ) (pprPrefixIfDeclBndr ss (occName clas)) binders roles
730+ = vcat [ pprRoles
731+ (== Nominal )
732+ (pprPrefixIfDeclBndr (ss_how_much ss) (occName clas))
733+ binders
734+ roles
718735 , text " class" <+> pprIfaceDeclHead context ss clas binders Nothing
719736 <+> pprFundeps fds <+> pp_where
720737 , nest 2 (vcat [ vcat asocs, vcat dsigs
@@ -788,7 +805,11 @@ pprIfaceDecl ss (IfaceFamily { ifName = tycon
788805
789806 pp_branches (IfaceClosedSynFamilyTyCon (Just (ax, brs)))
790807 = hang (text " where" )
791- 2 (vcat (map (pprAxBranch (pprPrefixIfDeclBndr ss (occName tycon))) brs)
808+ 2 (vcat (map (pprAxBranch
809+ (pprPrefixIfDeclBndr
810+ (ss_how_much ss)
811+ (occName tycon))
812+ ) brs)
792813 $$ ppShowIface ss (text " axiom" <+> ppr ax))
793814 pp_branches _ = Outputable. empty
794815
@@ -814,8 +835,8 @@ pprIfaceDecl _ (IfacePatSyn { ifName = name,
814835
815836pprIfaceDecl ss (IfaceId { ifName = var, ifType = ty,
816837 ifIdDetails = details, ifIdInfo = info })
817- = vcat [ hang (pprPrefixIfDeclBndr ss (occName var) <+> dcolon)
818- 2 (pprIfaceSigmaType ty)
838+ = vcat [ hang (pprPrefixIfDeclBndr (ss_how_much ss) (occName var) <+> dcolon)
839+ 2 (pprIfaceSigmaType (ss_forall ss) ty)
819840 , ppShowIface ss (ppr details)
820841 , ppShowIface ss (ppr info) ]
821842
@@ -839,14 +860,22 @@ pprRoles suppress_if tyCon bndrs roles
839860 in ppUnless (all suppress_if roles || null froles) $
840861 text " type role" <+> tyCon <+> hsep (map ppr froles)
841862
842- pprInfixIfDeclBndr , pprPrefixIfDeclBndr :: ShowSub -> OccName -> SDoc
843- pprInfixIfDeclBndr (ShowSub { ss_ppr_bndr = ppr_bndr } ) name
863+ pprInfixIfDeclBndr :: ShowHowMuch -> OccName -> SDoc
864+ pprInfixIfDeclBndr (ShowSome _ ( AltPpr ( Just ppr_bndr)) ) name
844865 = pprInfixVar (isSymOcc name) (ppr_bndr name)
845- pprPrefixIfDeclBndr (ShowSub { ss_ppr_bndr = ppr_bndr }) name
866+ pprInfixIfDeclBndr _ name
867+ = pprInfixVar (isSymOcc name) (ppr name)
868+
869+ pprPrefixIfDeclBndr :: ShowHowMuch -> OccName -> SDoc
870+ pprPrefixIfDeclBndr (ShowHeader (AltPpr (Just ppr_bndr))) name
846871 = parenSymOcc name (ppr_bndr name)
872+ pprPrefixIfDeclBndr (ShowSome _ (AltPpr (Just ppr_bndr))) name
873+ = parenSymOcc name (ppr_bndr name)
874+ pprPrefixIfDeclBndr _ name
875+ = parenSymOcc name (ppr name)
847876
848877instance Outputable IfaceClassOp where
849- ppr = pprIfaceClassOp showAll
878+ ppr = pprIfaceClassOp showToIface
850879
851880pprIfaceClassOp :: ShowSub -> IfaceClassOp -> SDoc
852881pprIfaceClassOp ss (IfaceClassOp n ty dm)
@@ -856,10 +885,13 @@ pprIfaceClassOp ss (IfaceClassOp n ty dm)
856885 = text " default" <+> pp_sig n dm_ty
857886 | otherwise
858887 = empty
859- pp_sig n ty = pprPrefixIfDeclBndr ss (occName n) <+> dcolon <+> pprIfaceSigmaType ty
888+ pp_sig n ty
889+ = pprPrefixIfDeclBndr (ss_how_much ss) (occName n)
890+ <+> dcolon
891+ <+> pprIfaceSigmaType ShowForAllWhen ty
860892
861893instance Outputable IfaceAT where
862- ppr = pprIfaceAT showAll
894+ ppr = pprIfaceAT showToIface
863895
864896pprIfaceAT :: ShowSub -> IfaceAT -> SDoc
865897pprIfaceAT ss (IfaceAT d mb_def)
@@ -887,7 +919,7 @@ pprIfaceDeclHead :: IfaceContext -> ShowSub -> Name
887919pprIfaceDeclHead context ss tc_occ bndrs m_res_kind
888920 = sdocWithDynFlags $ \ dflags ->
889921 sep [ pprIfaceContextArr context
890- , pprPrefixIfDeclBndr ss (occName tc_occ)
922+ , pprPrefixIfDeclBndr (ss_how_much ss) (occName tc_occ)
891923 <+> pprIfaceTyConBinders (suppressIfaceInvisibles dflags bndrs bndrs)
892924 , maybe empty (\ res_kind -> dcolon <+> pprIfaceType res_kind) m_res_kind ]
893925
@@ -911,12 +943,16 @@ pprIfaceConDecl ss gadt_style fls tycon tc_binders parent
911943 | gadt_style = pp_prefix_con <+> dcolon <+> ppr_ty
912944 | not (null fields) = pp_prefix_con <+> pp_field_args
913945 | is_infix
914- , [ty1, ty2] <- pp_args = sep [ty1, pprInfixIfDeclBndr ss (occName name), ty2]
946+ , [ty1, ty2] <- pp_args = sep [ ty1
947+ , pprInfixIfDeclBndr how_much (occName name)
948+ , ty2]
949+
915950 | otherwise = pp_prefix_con <+> sep pp_args
916951 where
952+ how_much = ss_how_much ss
917953 tys_w_strs :: [(IfaceBang , IfaceType )]
918954 tys_w_strs = zip stricts arg_tys
919- pp_prefix_con = pprPrefixIfDeclBndr ss (occName name)
955+ pp_prefix_con = pprPrefixIfDeclBndr how_much (occName name)
920956
921957 (univ_tvs, pp_res_ty) = mk_user_con_res_ty eq_spec
922958 ppr_ty = pprIfaceForAllPart (map tv_to_forall_bndr univ_tvs ++ ex_tvs)
@@ -949,8 +985,10 @@ pprIfaceConDecl ss gadt_style fls tycon tc_binders parent
949985
950986 maybe_show_label :: IfaceTopBndr -> (IfaceBang , IfaceType ) -> Maybe SDoc
951987 maybe_show_label sel bty
952- | showSub ss sel = Just (pprPrefixIfDeclBndr ss lbl <+> dcolon <+> pprBangTy bty)
953- | otherwise = Nothing
988+ | showSub ss sel =
989+ Just (pprPrefixIfDeclBndr how_much lbl <+> dcolon <+> pprBangTy bty)
990+ | otherwise =
991+ Nothing
954992 where
955993 -- IfaceConDecl contains the name of the selector function, so
956994 -- we have to look up the field label (in case
@@ -971,7 +1009,7 @@ pprIfaceConDecl ss gadt_style fls tycon tc_binders parent
9711009 con_univ_tvs = filterOut done_univ_tv (map ifTyConBinderTyVar tc_binders)
9721010
9731011 ppr_tc_app gadt_subst dflags
974- = pprPrefixIfDeclBndr ss (occName tycon)
1012+ = pprPrefixIfDeclBndr how_much (occName tycon)
9751013 <+> sep [ pprParendIfaceType (substIfaceTyVar gadt_subst tv)
9761014 | (tv,_kind)
9771015 <- map ifTyConBinderTyVar $
0 commit comments