X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2Fmain%2FPprTyThing.hs;h=06096602853e0848e54032d19972b187fd852d25;hp=2763b052fd294344ef4e5c800bd6be1192533abe;hb=4c6a3f787abcaed009a574196d82237d9ae64fc8;hpb=0065d5ab628975892cea1ec7303f968c3338cbe1 diff --git a/compiler/main/PprTyThing.hs b/compiler/main/PprTyThing.hs index 2763b05..0609660 100644 --- a/compiler/main/PprTyThing.hs +++ b/compiler/main/PprTyThing.hs @@ -7,18 +7,27 @@ ----------------------------------------------------------------------------- module PprTyThing ( + PrintExplicitForalls, pprTyThing, pprTyThingInContext, pprTyThingLoc, pprTyThingInContextLoc, - pprTyThingHdr + pprTyThingHdr, + pprTypeForUser ) where #include "HsVersions.h" import qualified GHC -import GHC ( TyThing(..), SrcLoc ) + +import GHC ( TyThing(..) ) +import TyCon +import Type ( TyThing(..), tidyTopType, pprTypeApp ) +import TcType +import Var +import Name import Outputable +import FastString -- ----------------------------------------------------------------------------- -- Pretty-printing entities that we get from the GHC API @@ -26,46 +35,52 @@ import Outputable -- This should be a good source of sample code for using the GHC API to -- inspect source code entities. +type PrintExplicitForalls = Bool + -- | Pretty-prints a 'TyThing' with its defining location. -pprTyThingLoc :: Bool -> TyThing -> SDoc -pprTyThingLoc exts tyThing - = showWithLoc loc (pprTyThing exts tyThing) - where loc = GHC.nameSrcLoc (GHC.getName tyThing) +pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc +pprTyThingLoc pefas tyThing + = showWithLoc loc (pprTyThing pefas tyThing) + where loc = pprNameLoc (GHC.getName tyThing) -- | Pretty-prints a 'TyThing'. -pprTyThing :: Bool -> TyThing -> SDoc -pprTyThing exts (AnId id) = pprId exts id -pprTyThing exts (ADataCon dataCon) = pprDataConSig exts dataCon -pprTyThing exts (ATyCon tyCon) = pprTyCon exts tyCon -pprTyThing exts (AClass cls) = pprClass exts cls - +pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc +pprTyThing pefas (AnId id) = pprId pefas id +pprTyThing pefas (ADataCon dataCon) = pprDataConSig pefas dataCon +pprTyThing pefas (ATyCon tyCon) = pprTyCon pefas tyCon +pprTyThing pefas (AClass cls) = pprClass pefas cls + -- | Like 'pprTyThingInContext', but adds the defining location. -pprTyThingInContextLoc :: Bool -> TyThing -> SDoc -pprTyThingInContextLoc exts tyThing - = showWithLoc loc (pprTyThingInContext exts tyThing) - where loc = GHC.nameSrcLoc (GHC.getName tyThing) +pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc +pprTyThingInContextLoc pefas tyThing + = showWithLoc loc (pprTyThingInContext pefas tyThing) + where loc = pprNameLoc (GHC.getName tyThing) -- | Pretty-prints a 'TyThing' in context: that is, if the entity -- is a data constructor, record selector, or class method, then -- the entity's parent declaration is pretty-printed with irrelevant -- parts omitted. -pprTyThingInContext :: Bool -> TyThing -> SDoc -pprTyThingInContext exts (AnId id) = pprIdInContext exts id -pprTyThingInContext exts (ADataCon dataCon) = pprDataCon exts dataCon -pprTyThingInContext exts (ATyCon tyCon) = pprTyCon exts tyCon -pprTyThingInContext exts (AClass cls) = pprClass exts cls +pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc +pprTyThingInContext pefas (AnId id) = pprIdInContext pefas id +pprTyThingInContext pefas (ADataCon dataCon) = pprDataCon pefas dataCon +pprTyThingInContext pefas (ATyCon tyCon) = pprTyCon pefas tyCon +pprTyThingInContext pefas (AClass cls) = pprClass pefas cls -- | Pretty-prints the 'TyThing' header. For functions and data constructors -- the function is equivalent to 'pprTyThing' but for type constructors -- and classes it prints only the header part of the declaration. -pprTyThingHdr :: Bool -> TyThing -> SDoc -pprTyThingHdr exts (AnId id) = pprId exts id -pprTyThingHdr exts (ADataCon dataCon) = pprDataConSig exts dataCon -pprTyThingHdr exts (ATyCon tyCon) = pprTyConHdr exts tyCon -pprTyThingHdr exts (AClass cls) = pprClassHdr exts cls - -pprTyConHdr exts tyCon = - ptext keyword <+> ppr_bndr tyCon <+> hsep (map ppr vars) +pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc +pprTyThingHdr pefas (AnId id) = pprId pefas id +pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon +pprTyThingHdr pefas (ATyCon tyCon) = pprTyConHdr pefas tyCon +pprTyThingHdr pefas (AClass cls) = pprClassHdr pefas cls + +pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc +pprTyConHdr _ tyCon + | Just (_fam_tc, tys) <- tyConFamInst_maybe tyCon + = ptext keyword <+> ptext SLIT("instance") <+> pprTypeApp tyCon (ppr_bndr tyCon) tys + | otherwise + = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars) where vars | GHC.isPrimTyCon tyCon || GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars @@ -75,10 +90,20 @@ pprTyConHdr exts tyCon = | GHC.isNewTyCon tyCon = SLIT("newtype") | otherwise = SLIT("data") -pprDataConSig exts dataCon = - ppr_bndr dataCon <+> dcolon <+> pprType exts (GHC.dataConType dataCon) + opt_family + | GHC.isOpenTyCon tyCon = ptext SLIT("family") + | otherwise = empty -pprClassHdr exts cls = + opt_stupid -- The "stupid theta" part of the declaration + | isAlgTyCon tyCon = GHC.pprThetaArrow (tyConStupidTheta tyCon) + | otherwise = empty -- Returns 'empty' if null theta + +pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc +pprDataConSig pefas dataCon = + ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon) + +pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc +pprClassHdr _ cls = let (tyVars, funDeps) = GHC.classTvsFds cls in ptext SLIT("class") <+> GHC.pprThetaArrow (GHC.classSCTheta cls) <+> @@ -86,54 +111,81 @@ pprClassHdr exts cls = hsep (map ppr tyVars) <+> GHC.pprFundeps funDeps -pprIdInContext exts id - | GHC.isRecordSelector id = pprRecordSelector exts id - | Just cls <- GHC.isClassOpId_maybe id = pprClassOneMethod exts cls id - | otherwise = pprId exts id +pprIdInContext :: PrintExplicitForalls -> Var -> SDoc +pprIdInContext pefas id + | GHC.isRecordSelector id = pprRecordSelector pefas id + | Just cls <- GHC.isClassOpId_maybe id = pprClassOneMethod pefas cls id + | otherwise = pprId pefas id -pprRecordSelector exts id - = pprAlgTyCon exts tyCon show_con show_label +pprRecordSelector :: PrintExplicitForalls -> Id -> SDoc +pprRecordSelector pefas id + = pprAlgTyCon pefas tyCon show_con show_label where (tyCon,label) = GHC.recordSelectorFieldLabel id show_con dataCon = label `elem` GHC.dataConFieldLabels dataCon show_label label' = label == label' -pprId exts id - = hang (ppr_bndr id <+> dcolon) 2 - (pprType exts (GHC.idType id)) - -pprType True ty = ppr ty -pprType False ty = ppr (GHC.dropForAlls ty) +pprId :: PrintExplicitForalls -> Var -> SDoc +pprId pefas ident + = hang (ppr_bndr ident <+> dcolon) + 2 (pprTypeForUser pefas (GHC.idType ident)) + +pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc +-- We do two things here. +-- a) We tidy the type, regardless +-- b) If PrintExplicitForAlls is True, we discard the foralls +-- but we do so `deeply' +-- Prime example: a class op might have type +-- forall a. C a => forall b. Ord b => stuff +-- Then we want to display +-- (C a, Ord b) => stuff +pprTypeForUser print_foralls ty + | print_foralls = ppr tidy_ty + | otherwise = ppr (mkPhiTy [p | (_tvs, ps) <- ctxt, p <- ps] ty') + where + tidy_ty = tidyTopType ty + (ctxt, ty') = tcMultiSplitSigmaTy tidy_ty -pprTyCon exts tyCon +pprTyCon :: PrintExplicitForalls -> TyCon -> SDoc +pprTyCon pefas tyCon | GHC.isSynTyCon tyCon - = let rhs_type = GHC.synTyConRhs tyCon - in hang (pprTyConHdr exts tyCon <+> equals) 2 (pprType exts rhs_type) + = if GHC.isOpenTyCon tyCon + then pprTyConHdr pefas tyCon <+> dcolon <+> + pprTypeForUser pefas (GHC.synTyConResKind tyCon) + else + let rhs_type = GHC.synTyConType tyCon + in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type) | otherwise - = pprAlgTyCon exts tyCon (const True) (const True) + = pprAlgTyCon pefas tyCon (const True) (const True) -pprAlgTyCon exts tyCon ok_con ok_label - | gadt = pprTyConHdr exts tyCon <+> ptext SLIT("where") $$ +pprAlgTyCon :: PrintExplicitForalls -> TyCon -> (GHC.DataCon -> Bool) + -> (FieldLabel -> Bool) -> SDoc +pprAlgTyCon pefas tyCon ok_con ok_label + | gadt = pprTyConHdr pefas tyCon <+> ptext SLIT("where") $$ nest 2 (vcat (ppr_trim show_con datacons)) - | otherwise = hang (pprTyConHdr exts tyCon) + | otherwise = hang (pprTyConHdr pefas tyCon) 2 (add_bars (ppr_trim show_con datacons)) where datacons = GHC.tyConDataCons tyCon gadt = any (not . GHC.isVanillaDataCon) datacons show_con dataCon - | ok_con dataCon = Just (pprDataConDecl exts gadt ok_label dataCon) + | ok_con dataCon = Just (pprDataConDecl pefas gadt ok_label dataCon) | otherwise = Nothing -pprDataCon exts dataCon = pprAlgTyCon exts tyCon (== dataCon) (const True) +pprDataCon :: PrintExplicitForalls -> GHC.DataCon -> SDoc +pprDataCon pefas dataCon = pprAlgTyCon pefas tyCon (== dataCon) (const True) where tyCon = GHC.dataConTyCon dataCon -pprDataConDecl exts gadt_style show_label dataCon +pprDataConDecl :: PrintExplicitForalls -> Bool -> (FieldLabel -> Bool) + -> GHC.DataCon -> SDoc +pprDataConDecl _ gadt_style show_label dataCon | not gadt_style = ppr_fields tys_w_strs | otherwise = ppr_bndr dataCon <+> dcolon <+> sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ] where - (tyvars, theta, argTypes, tyCon, res_tys) = GHC.dataConSig dataCon + (tyvars, theta, argTypes, res_ty) = GHC.dataConSig dataCon + tyCon = GHC.dataConTyCon dataCon labels = GHC.dataConFieldLabels dataCon qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars stricts = GHC.dataConStrictMarks dataCon @@ -145,9 +197,8 @@ pprDataConDecl exts gadt_style show_label dataCon hsep (map ppr qualVars) <> dot -- printing out the dataCon as a type signature, in GADT style - pp_tau = foldr add pp_res_ty tys_w_strs - pp_res_ty = ppr_bndr tyCon <+> hsep (map GHC.pprParendType res_tys) - add (str,ty) pp_ty = pprBangTy str ty <+> arrow <+> pp_ty + pp_tau = foldr add (ppr res_ty) tys_w_strs + add str_ty pp_ty = pprParendBangTy str_ty <+> arrow <+> pp_ty pprParendBangTy (strict,ty) | GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty @@ -172,30 +223,43 @@ pprDataConDecl exts gadt_style show_label dataCon braces (sep (punctuate comma (ppr_trim maybe_show_label (zip labels fields)))) -pprClass exts cls +pprClass :: PrintExplicitForalls -> GHC.Class -> SDoc +pprClass pefas cls | null methods = - pprClassHdr exts cls + pprClassHdr pefas cls | otherwise = - hang (pprClassHdr exts cls <+> ptext SLIT("where")) - 2 (vcat (map (pprClassMethod exts) methods)) + hang (pprClassHdr pefas cls <+> ptext SLIT("where")) + 2 (vcat (map (pprClassMethod pefas) methods)) where methods = GHC.classMethods cls -pprClassOneMethod exts cls this_one = - hang (pprClassHdr exts cls <+> ptext SLIT("where")) - 2 (vcat (ppr_trim show_meth methods)) +pprClassOneMethod :: PrintExplicitForalls -> GHC.Class -> Id -> SDoc +pprClassOneMethod pefas cls this_one + = hang (pprClassHdr pefas cls <+> ptext SLIT("where")) + 2 (vcat (ppr_trim show_meth methods)) where methods = GHC.classMethods cls - show_meth id | id == this_one = Just (pprClassMethod exts id) + show_meth id | id == this_one = Just (pprClassMethod pefas id) | otherwise = Nothing -pprClassMethod exts id = - hang (ppr_bndr id <+> dcolon) 2 (pprType exts (classOpType id)) +pprClassMethod :: PrintExplicitForalls -> Id -> SDoc +pprClassMethod pefas id + = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty) where -- Here's the magic incantation to strip off the dictionary -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl. - classOpType id = GHC.funResultTy rho_ty - where (_sel_tyvars, rho_ty) = GHC.splitForAllTys (GHC.idType id) + -- + -- It's important to tidy it *before* splitting it up, so that if + -- we have class C a b where + -- op :: forall a. a -> b + -- then the inner forall on op gets renamed to a1, and we print + -- (when dropping foralls) + -- class C a b where + -- op :: a1 -> b + + tidy_sel_ty = tidyTopType (GHC.idType id) + (_sel_tyvars, rho_ty) = GHC.splitForAllTys tidy_sel_ty + op_ty = GHC.funResultTy rho_ty ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc] ppr_trim show xs @@ -206,6 +270,7 @@ ppr_trim show xs | otherwise = if eliding then (True, so_far) else (True, ptext SLIT("...") : so_far) +add_bars :: [SDoc] -> SDoc add_bars [] = empty add_bars [c] = equals <+> c add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs) @@ -214,9 +279,9 @@ add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs) ppr_bndr :: GHC.NamedThing a => a -> SDoc ppr_bndr a = GHC.pprParenSymName a -showWithLoc :: SrcLoc -> SDoc -> SDoc +showWithLoc :: SDoc -> SDoc -> SDoc showWithLoc loc doc - = hang doc 2 (char '\t' <> comment <+> GHC.pprDefnLoc loc) + = hang doc 2 (char '\t' <> comment <+> loc) -- The tab tries to make them line up a bit where comment = ptext SLIT("--")