1 -----------------------------------------------------------------------------
3 -- Pretty-printing TyThings
5 -- (c) The GHC Team 2005
7 -----------------------------------------------------------------------------
12 pprTyThingInContext, pprTyThingParent_maybe,
14 pprTyThingInContextLoc,
21 import GHC ( TyThing(..) )
32 -- -----------------------------------------------------------------------------
33 -- Pretty-printing entities that we get from the GHC API
35 -- This should be a good source of sample code for using the GHC API to
36 -- inspect source code entities.
38 type PrintExplicitForalls = Bool
40 type ShowMe = Name -> Bool
41 -- The ShowMe function says which sub-components to print
43 -- False <=> elide to "..."
45 ----------------------------
46 -- | Pretty-prints a 'TyThing' with its defining location.
47 pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
48 pprTyThingLoc pefas tyThing
49 = showWithLoc loc (pprTyThing pefas tyThing)
50 where loc = pprNameLoc (GHC.getName tyThing)
52 -- | Pretty-prints a 'TyThing'.
53 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
54 pprTyThing pefas thing = ppr_ty_thing pefas (const True) thing
56 ppr_ty_thing :: PrintExplicitForalls -> ShowMe -> TyThing -> SDoc
57 ppr_ty_thing pefas _ (AnId id) = pprId pefas id
58 ppr_ty_thing pefas _ (ADataCon dataCon) = pprDataConSig pefas dataCon
59 ppr_ty_thing pefas show_me (ATyCon tyCon) = pprTyCon pefas show_me tyCon
60 ppr_ty_thing pefas show_me (AClass cls) = pprClass pefas show_me cls
62 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
63 -- is a data constructor, record selector, or class method, then
64 -- the entity's parent declaration is pretty-printed with irrelevant
66 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
67 pprTyThingInContext pefas thing
68 | Just parent <- pprTyThingParent_maybe thing
69 = ppr_ty_thing pefas (== GHC.getName thing) parent
71 = pprTyThing pefas thing
73 -- | Like 'pprTyThingInContext', but adds the defining location.
74 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
75 pprTyThingInContextLoc pefas tyThing
76 = showWithLoc (pprNameLoc (GHC.getName tyThing))
77 (pprTyThingInContext pefas tyThing)
79 pprTyThingParent_maybe :: TyThing -> Maybe TyThing
80 -- (pprTyThingParent_maybe x) returns (Just p)
81 -- when pprTyThingInContext sould print a declaration for p
82 -- (albeit with some "..." in it) when asked to show x
83 pprTyThingParent_maybe (ADataCon dc) = Just (ATyCon (dataConTyCon dc))
84 pprTyThingParent_maybe (AnId id) = case idDetails id of
85 RecSelId { sel_tycon = tc } -> Just (ATyCon tc)
86 ClassOpId cls -> Just (AClass cls)
88 pprTyThingParent_maybe _other = Nothing
90 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
91 -- the function is equivalent to 'pprTyThing' but for type constructors
92 -- and classes it prints only the header part of the declaration.
93 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
94 pprTyThingHdr pefas (AnId id) = pprId pefas id
95 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
96 pprTyThingHdr pefas (ATyCon tyCon) = pprTyConHdr pefas tyCon
97 pprTyThingHdr pefas (AClass cls) = pprClassHdr pefas cls
99 pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc
101 | Just (_fam_tc, tys) <- tyConFamInst_maybe tyCon
102 = ptext keyword <+> ptext (sLit "instance") <+> pprTypeApp tyCon tys
104 = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
106 vars | GHC.isPrimTyCon tyCon ||
107 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
108 | otherwise = GHC.tyConTyVars tyCon
110 keyword | GHC.isSynTyCon tyCon = sLit "type"
111 | GHC.isNewTyCon tyCon = sLit "newtype"
112 | otherwise = sLit "data"
115 | GHC.isFamilyTyCon tyCon = ptext (sLit "family")
118 opt_stupid -- The "stupid theta" part of the declaration
119 | isAlgTyCon tyCon = GHC.pprThetaArrow (tyConStupidTheta tyCon)
120 | otherwise = empty -- Returns 'empty' if null theta
122 pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc
123 pprDataConSig pefas dataCon
124 = ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
126 pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc
128 = ptext (sLit "class") <+>
129 GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
131 hsep (map ppr tyVars) <+>
132 GHC.pprFundeps funDeps
134 (tyVars, funDeps) = GHC.classTvsFds cls
136 pprId :: PrintExplicitForalls -> Var -> SDoc
138 = hang (ppr_bndr ident <+> dcolon)
139 2 (pprTypeForUser pefas (GHC.idType ident))
141 pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc
142 -- We do two things here.
143 -- a) We tidy the type, regardless
144 -- b) If PrintExplicitForAlls is True, we discard the foralls
145 -- but we do so `deeply'
146 -- Prime example: a class op might have type
147 -- forall a. C a => forall b. Ord b => stuff
148 -- Then we want to display
149 -- (C a, Ord b) => stuff
150 pprTypeForUser print_foralls ty
151 | print_foralls = ppr tidy_ty
152 | otherwise = ppr (mkPhiTy ctxt ty')
154 tidy_ty = tidyTopType ty
155 (_, ctxt, ty') = tcSplitSigmaTy tidy_ty
157 pprTyCon :: PrintExplicitForalls -> ShowMe -> TyCon -> SDoc
158 pprTyCon pefas show_me tyCon
159 | GHC.isSynTyCon tyCon
160 = if GHC.isFamilyTyCon tyCon
161 then pprTyConHdr pefas tyCon <+> dcolon <+>
162 pprTypeForUser pefas (GHC.synTyConResKind tyCon)
164 let rhs_type = GHC.synTyConType tyCon
165 in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type)
167 = pprAlgTyCon pefas show_me tyCon
169 pprAlgTyCon :: PrintExplicitForalls -> ShowMe -> TyCon -> SDoc
170 pprAlgTyCon pefas show_me tyCon
171 | gadt = pprTyConHdr pefas tyCon <+> ptext (sLit "where") $$
172 nest 2 (vcat (ppr_trim show_con datacons))
173 | otherwise = hang (pprTyConHdr pefas tyCon)
174 2 (add_bars (ppr_trim show_con datacons))
176 datacons = GHC.tyConDataCons tyCon
177 gadt = any (not . GHC.isVanillaDataCon) datacons
179 ok_con dc = show_me (dataConName dc) || any show_me (dataConFieldLabels dc)
181 | ok_con dc = Just (pprDataConDecl pefas show_me gadt dc)
182 | otherwise = Nothing
184 pprDataConDecl :: PrintExplicitForalls -> ShowMe -> Bool -> GHC.DataCon -> SDoc
185 pprDataConDecl pefas show_me gadt_style dataCon
186 | not gadt_style = ppr_fields tys_w_strs
187 | otherwise = ppr_bndr dataCon <+> dcolon <+>
188 sep [ pp_foralls, GHC.pprThetaArrow theta, pp_tau ]
189 -- Printing out the dataCon as a type signature, in GADT style
191 (forall_tvs, theta, tau) = tcSplitSigmaTy (GHC.dataConUserType dataCon)
192 (arg_tys, res_ty) = tcSplitFunTys tau
193 labels = GHC.dataConFieldLabels dataCon
194 stricts = GHC.dataConStrictMarks dataCon
195 tys_w_strs = zip stricts arg_tys
196 pp_foralls | pefas = GHC.pprForAll forall_tvs
199 pp_tau = foldr add (ppr res_ty) tys_w_strs
200 add str_ty pp_ty = pprParendBangTy str_ty <+> arrow <+> pp_ty
202 pprParendBangTy (bang,ty) = ppr bang <> GHC.pprParendType ty
204 pprBangTy bang ty = ppr bang <> ppr ty
206 maybe_show_label (lbl,(strict,tp))
207 | show_me lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
208 | otherwise = Nothing
210 ppr_fields [ty1, ty2]
211 | GHC.dataConIsInfix dataCon && null labels
212 = sep [pprParendBangTy ty1, pprInfixName dataCon, pprParendBangTy ty2]
215 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
217 = ppr_bndr dataCon <+>
218 braces (sep (punctuate comma (ppr_trim maybe_show_label
219 (zip labels fields))))
221 pprClass :: PrintExplicitForalls -> ShowMe -> GHC.Class -> SDoc
222 pprClass pefas show_me cls
224 = pprClassHdr pefas cls
226 = hang (pprClassHdr pefas cls <+> ptext (sLit "where"))
227 2 (vcat (ppr_trim show_meth methods))
229 methods = GHC.classMethods cls
230 show_meth id | show_me (idName id) = Just (pprClassMethod pefas id)
231 | otherwise = Nothing
233 pprClassMethod :: PrintExplicitForalls -> Id -> SDoc
234 pprClassMethod pefas id
235 = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
237 -- Here's the magic incantation to strip off the dictionary
238 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
240 -- It's important to tidy it *before* splitting it up, so that if
241 -- we have class C a b where
242 -- op :: forall a. a -> b
243 -- then the inner forall on op gets renamed to a1, and we print
244 -- (when dropping foralls)
248 tidy_sel_ty = tidyTopType (GHC.idType id)
249 (_sel_tyvars, rho_ty) = GHC.splitForAllTys tidy_sel_ty
250 op_ty = GHC.funResultTy rho_ty
252 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
254 = snd (foldr go (False, []) xs)
256 go x (eliding, so_far)
257 | Just doc <- show x = (False, doc : so_far)
258 | otherwise = if eliding then (True, so_far)
259 else (True, ptext (sLit "...") : so_far)
261 add_bars :: [SDoc] -> SDoc
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 "--")