2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
8 -----------------------------------------------------------------------------
10 -- Pretty-printing TyThings
12 -- (c) The GHC Team 2005
14 -----------------------------------------------------------------------------
21 pprTyThingInContextLoc,
26 #include "HsVersions.h"
30 import GHC ( TyThing(..) )
31 import TyCon ( tyConFamInst_maybe )
32 import Type ( TyThing(..), tidyTopType, pprTypeApp )
33 import TcType ( tcMultiSplitSigmaTy, mkPhiTy )
34 import SrcLoc ( SrcSpan )
39 -- -----------------------------------------------------------------------------
40 -- Pretty-printing entities that we get from the GHC API
42 -- This should be a good source of sample code for using the GHC API to
43 -- inspect source code entities.
45 type PrintExplicitForalls = Bool
47 -- | Pretty-prints a 'TyThing' with its defining location.
48 pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
49 pprTyThingLoc pefas tyThing
50 = showWithLoc loc (pprTyThing pefas tyThing)
51 where loc = pprNameLoc (GHC.getName tyThing)
53 -- | Pretty-prints a 'TyThing'.
54 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
55 pprTyThing pefas (AnId id) = pprId pefas id
56 pprTyThing pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
57 pprTyThing pefas (ATyCon tyCon) = pprTyCon pefas tyCon
58 pprTyThing pefas (AClass cls) = pprClass pefas cls
60 -- | Like 'pprTyThingInContext', but adds the defining location.
61 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
62 pprTyThingInContextLoc pefas tyThing
63 = showWithLoc loc (pprTyThingInContext pefas tyThing)
64 where loc = pprNameLoc (GHC.getName tyThing)
66 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
67 -- is a data constructor, record selector, or class method, then
68 -- the entity's parent declaration is pretty-printed with irrelevant
70 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
71 pprTyThingInContext pefas (AnId id) = pprIdInContext pefas id
72 pprTyThingInContext pefas (ADataCon dataCon) = pprDataCon pefas dataCon
73 pprTyThingInContext pefas (ATyCon tyCon) = pprTyCon pefas tyCon
74 pprTyThingInContext pefas (AClass cls) = pprClass pefas cls
76 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
77 -- the function is equivalent to 'pprTyThing' but for type constructors
78 -- and classes it prints only the header part of the declaration.
79 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
80 pprTyThingHdr pefas (AnId id) = pprId pefas id
81 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
82 pprTyThingHdr pefas (ATyCon tyCon) = pprTyConHdr pefas tyCon
83 pprTyThingHdr pefas (AClass cls) = pprClassHdr pefas cls
85 pprTyConHdr pefas tyCon
86 | Just (fam_tc, tys) <- tyConFamInst_maybe tyCon
87 = ptext keyword <+> ptext SLIT("instance") <+> pprTypeApp tyCon (ppr_bndr tyCon) tys
89 = ptext keyword <+> opt_family <+> ppr_bndr tyCon <+> hsep (map ppr vars)
91 vars | GHC.isPrimTyCon tyCon ||
92 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
93 | otherwise = GHC.tyConTyVars tyCon
95 keyword | GHC.isSynTyCon tyCon = SLIT("type")
96 | GHC.isNewTyCon tyCon = SLIT("newtype")
97 | otherwise = SLIT("data")
100 | GHC.isOpenTyCon tyCon = ptext SLIT("family")
103 pprDataConSig pefas dataCon =
104 ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
106 pprClassHdr pefas cls =
107 let (tyVars, funDeps) = GHC.classTvsFds cls
108 in ptext SLIT("class") <+>
109 GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
111 hsep (map ppr tyVars) <+>
112 GHC.pprFundeps funDeps
114 pprIdInContext pefas id
115 | GHC.isRecordSelector id = pprRecordSelector pefas id
116 | Just cls <- GHC.isClassOpId_maybe id = pprClassOneMethod pefas cls id
117 | otherwise = pprId pefas id
119 pprRecordSelector pefas id
120 = pprAlgTyCon pefas tyCon show_con show_label
122 (tyCon,label) = GHC.recordSelectorFieldLabel id
123 show_con dataCon = label `elem` GHC.dataConFieldLabels dataCon
124 show_label label' = label == label'
126 pprId :: PrintExplicitForalls -> Var -> SDoc
128 = hang (ppr_bndr ident <+> dcolon)
129 2 (pprTypeForUser pefas (GHC.idType ident))
131 pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc
132 -- We do two things here.
133 -- a) We tidy the type, regardless
134 -- b) If PrintExplicitForAlls is True, we discard the foralls
135 -- but we do so `deeply'
136 -- Prime example: a class op might have type
137 -- forall a. C a => forall b. Ord b => stuff
138 -- Then we want to display
139 -- (C a, Ord b) => stuff
140 pprTypeForUser print_foralls ty
141 | print_foralls = ppr tidy_ty
142 | otherwise = ppr (mkPhiTy [p | (_tvs, ps) <- ctxt, p <- ps] ty')
144 tidy_ty = tidyTopType ty
145 (ctxt, ty') = tcMultiSplitSigmaTy tidy_ty
148 | GHC.isSynTyCon tyCon
149 = if GHC.isOpenTyCon tyCon
150 then pprTyConHdr pefas tyCon <+> dcolon <+>
151 pprTypeForUser pefas (GHC.synTyConResKind tyCon)
153 let rhs_type = GHC.synTyConType tyCon
154 in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type)
156 = pprAlgTyCon pefas tyCon (const True) (const True)
158 pprAlgTyCon pefas tyCon ok_con ok_label
159 | gadt = pprTyConHdr pefas tyCon <+> ptext SLIT("where") $$
160 nest 2 (vcat (ppr_trim show_con datacons))
161 | otherwise = hang (pprTyConHdr pefas tyCon)
162 2 (add_bars (ppr_trim show_con datacons))
164 datacons = GHC.tyConDataCons tyCon
165 gadt = any (not . GHC.isVanillaDataCon) datacons
168 | ok_con dataCon = Just (pprDataConDecl pefas gadt ok_label dataCon)
169 | otherwise = Nothing
171 pprDataCon pefas dataCon = pprAlgTyCon pefas tyCon (== dataCon) (const True)
172 where tyCon = GHC.dataConTyCon dataCon
174 pprDataConDecl pefas gadt_style show_label dataCon
175 | not gadt_style = ppr_fields tys_w_strs
176 | otherwise = ppr_bndr dataCon <+> dcolon <+>
177 sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
179 (tyvars, theta, argTypes, res_ty) = GHC.dataConSig dataCon
180 tyCon = GHC.dataConTyCon dataCon
181 labels = GHC.dataConFieldLabels dataCon
182 qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
183 stricts = GHC.dataConStrictMarks dataCon
184 tys_w_strs = zip stricts argTypes
187 | null qualVars = empty
188 | otherwise = ptext SLIT("forall") <+>
189 hsep (map ppr qualVars) <> dot
191 -- printing out the dataCon as a type signature, in GADT style
192 pp_tau = foldr add (ppr res_ty) tys_w_strs
193 add (str,ty) pp_ty = pprBangTy str ty <+> arrow <+> pp_ty
195 pprParendBangTy (strict,ty)
196 | GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty
197 | otherwise = GHC.pprParendType ty
200 | GHC.isMarkedStrict strict = char '!' <> ppr ty
203 maybe_show_label (lbl,(strict,tp))
204 | show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
205 | otherwise = Nothing
207 ppr_fields [ty1, ty2]
208 | GHC.dataConIsInfix dataCon && null labels
209 = sep [pprParendBangTy ty1, ppr dataCon, pprParendBangTy ty2]
212 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
214 = ppr_bndr dataCon <+>
215 braces (sep (punctuate comma (ppr_trim maybe_show_label
216 (zip labels fields))))
220 pprClassHdr pefas cls
222 hang (pprClassHdr pefas cls <+> ptext SLIT("where"))
223 2 (vcat (map (pprClassMethod pefas) methods))
225 methods = GHC.classMethods cls
227 pprClassOneMethod pefas cls this_one
228 = hang (pprClassHdr pefas cls <+> ptext SLIT("where"))
229 2 (vcat (ppr_trim show_meth methods))
231 methods = GHC.classMethods cls
232 show_meth id | id == this_one = Just (pprClassMethod pefas id)
233 | otherwise = Nothing
235 pprClassMethod pefas id
236 = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
238 -- Here's the magic incantation to strip off the dictionary
239 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
241 -- It's important to tidy it *before* splitting it up, so that if
242 -- we have class C a b where
243 -- op :: forall a. a -> b
244 -- then the inner forall on op gets renamed to a1, and we print
245 -- (when dropping foralls)
249 tidy_sel_ty = tidyTopType (GHC.idType id)
250 (_sel_tyvars, rho_ty) = GHC.splitForAllTys tidy_sel_ty
251 op_ty = GHC.funResultTy rho_ty
253 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
255 = snd (foldr go (False, []) xs)
257 go x (eliding, so_far)
258 | Just doc <- show x = (False, doc : so_far)
259 | otherwise = if eliding then (True, so_far)
260 else (True, ptext SLIT("...") : so_far)
263 add_bars [c] = equals <+> c
264 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
266 -- Wrap operators in ()
267 ppr_bndr :: GHC.NamedThing a => a -> SDoc
268 ppr_bndr a = GHC.pprParenSymName a
270 showWithLoc :: SDoc -> SDoc -> SDoc
272 = hang doc 2 (char '\t' <> comment <+> loc)
273 -- The tab tries to make them line up a bit
275 comment = ptext SLIT("--")