1 -----------------------------------------------------------------------------
3 -- Pretty-printing TyThings
5 -- (c) The GHC Team 2005
7 -----------------------------------------------------------------------------
12 pprTyThingInContext, pprTyThingParent_maybe,
14 pprTyThingInContextLoc,
21 import GHC ( TyThing(..) )
26 import Coercion( pprCoAxiom )
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 _ _ (ACoAxiom ax) = pprCoAxiom ax
61 ppr_ty_thing pefas show_me (AClass cls) = pprClass pefas show_me cls
63 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
64 -- is a data constructor, record selector, or class method, then
65 -- the entity's parent declaration is pretty-printed with irrelevant
67 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
68 pprTyThingInContext pefas thing
69 | Just parent <- pprTyThingParent_maybe thing
70 = ppr_ty_thing pefas (== GHC.getName thing) parent
72 = pprTyThing pefas thing
74 -- | Like 'pprTyThingInContext', but adds the defining location.
75 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
76 pprTyThingInContextLoc pefas tyThing
77 = showWithLoc (pprNameLoc (GHC.getName tyThing))
78 (pprTyThingInContext pefas tyThing)
80 pprTyThingParent_maybe :: TyThing -> Maybe TyThing
81 -- (pprTyThingParent_maybe x) returns (Just p)
82 -- when pprTyThingInContext sould print a declaration for p
83 -- (albeit with some "..." in it) when asked to show x
84 pprTyThingParent_maybe (ADataCon dc) = Just (ATyCon (dataConTyCon dc))
85 pprTyThingParent_maybe (AnId id) = case idDetails id of
86 RecSelId { sel_tycon = tc } -> Just (ATyCon tc)
87 ClassOpId cls -> Just (AClass cls)
89 pprTyThingParent_maybe _other = Nothing
91 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
92 -- the function is equivalent to 'pprTyThing' but for type constructors
93 -- and classes it prints only the header part of the declaration.
94 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
95 pprTyThingHdr pefas (AnId id) = pprId pefas id
96 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
97 pprTyThingHdr pefas (ATyCon tyCon) = pprTyConHdr pefas tyCon
98 pprTyThingHdr _ (ACoAxiom ax) = pprCoAxiom ax
99 pprTyThingHdr pefas (AClass cls) = pprClassHdr pefas cls
101 pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc
103 | Just (_fam_tc, tys) <- tyConFamInst_maybe tyCon
104 = ptext keyword <+> ptext (sLit "instance") <+> pprTypeApp tyCon tys
106 = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
108 vars | GHC.isPrimTyCon tyCon ||
109 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
110 | otherwise = GHC.tyConTyVars tyCon
112 keyword | GHC.isSynTyCon tyCon = sLit "type"
113 | GHC.isNewTyCon tyCon = sLit "newtype"
114 | otherwise = sLit "data"
117 | GHC.isFamilyTyCon tyCon = ptext (sLit "family")
120 opt_stupid -- The "stupid theta" part of the declaration
121 | isAlgTyCon tyCon = GHC.pprThetaArrowTy (tyConStupidTheta tyCon)
122 | otherwise = empty -- Returns 'empty' if null theta
124 pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc
125 pprDataConSig pefas dataCon
126 = ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
128 pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc
130 = ptext (sLit "class") <+>
131 GHC.pprThetaArrowTy (GHC.classSCTheta cls) <+>
133 hsep (map ppr tyVars) <+>
134 GHC.pprFundeps funDeps
136 (tyVars, funDeps) = GHC.classTvsFds cls
138 pprId :: PrintExplicitForalls -> Var -> SDoc
140 = hang (ppr_bndr ident <+> dcolon)
141 2 (pprTypeForUser pefas (GHC.idType ident))
143 pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc
144 -- We do two things here.
145 -- a) We tidy the type, regardless
146 -- b) If PrintExplicitForAlls is True, we discard the foralls
147 -- but we do so `deeply'
148 -- Prime example: a class op might have type
149 -- forall a. C a => forall b. Ord b => stuff
150 -- Then we want to display
151 -- (C a, Ord b) => stuff
152 pprTypeForUser print_foralls ty
153 | print_foralls = ppr tidy_ty
154 | otherwise = ppr (mkPhiTy ctxt ty')
156 tidy_ty = tidyTopType ty
157 (_, ctxt, ty') = tcSplitSigmaTy tidy_ty
159 pprTyCon :: PrintExplicitForalls -> ShowMe -> TyCon -> SDoc
160 pprTyCon pefas show_me tyCon
161 | GHC.isSynTyCon tyCon
162 = if GHC.isFamilyTyCon tyCon
163 then pprTyConHdr pefas tyCon <+> dcolon <+>
164 pprTypeForUser pefas (GHC.synTyConResKind tyCon)
166 let rhs_type = GHC.synTyConType tyCon
167 in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type)
169 = pprAlgTyCon pefas show_me tyCon
171 pprAlgTyCon :: PrintExplicitForalls -> ShowMe -> TyCon -> SDoc
172 pprAlgTyCon pefas show_me tyCon
173 | gadt = pprTyConHdr pefas tyCon <+> ptext (sLit "where") $$
174 nest 2 (vcat (ppr_trim show_con datacons))
175 | otherwise = hang (pprTyConHdr pefas tyCon)
176 2 (add_bars (ppr_trim show_con datacons))
178 datacons = GHC.tyConDataCons tyCon
179 gadt = any (not . GHC.isVanillaDataCon) datacons
181 ok_con dc = show_me (dataConName dc) || any show_me (dataConFieldLabels dc)
183 | ok_con dc = Just (pprDataConDecl pefas show_me gadt dc)
184 | otherwise = Nothing
186 pprDataConDecl :: PrintExplicitForalls -> ShowMe -> Bool -> GHC.DataCon -> SDoc
187 pprDataConDecl pefas show_me gadt_style dataCon
188 | not gadt_style = ppr_fields tys_w_strs
189 | otherwise = ppr_bndr dataCon <+> dcolon <+>
190 sep [ pp_foralls, GHC.pprThetaArrowTy theta, pp_tau ]
191 -- Printing out the dataCon as a type signature, in GADT style
193 (forall_tvs, theta, tau) = tcSplitSigmaTy (GHC.dataConUserType dataCon)
194 (arg_tys, res_ty) = tcSplitFunTys tau
195 labels = GHC.dataConFieldLabels dataCon
196 stricts = GHC.dataConStrictMarks dataCon
197 tys_w_strs = zip stricts arg_tys
198 pp_foralls | pefas = GHC.pprForAll forall_tvs
201 pp_tau = foldr add (ppr res_ty) tys_w_strs
202 add str_ty pp_ty = pprParendBangTy str_ty <+> arrow <+> pp_ty
204 pprParendBangTy (bang,ty) = ppr bang <> GHC.pprParendType ty
206 pprBangTy bang ty = ppr bang <> ppr ty
208 maybe_show_label (lbl,(strict,tp))
209 | show_me lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
210 | otherwise = Nothing
212 ppr_fields [ty1, ty2]
213 | GHC.dataConIsInfix dataCon && null labels
214 = sep [pprParendBangTy ty1, pprInfixName dataCon, pprParendBangTy ty2]
217 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
219 = ppr_bndr dataCon <+>
220 braces (sep (punctuate comma (ppr_trim maybe_show_label
221 (zip labels fields))))
223 pprClass :: PrintExplicitForalls -> ShowMe -> GHC.Class -> SDoc
224 pprClass pefas show_me cls
226 = pprClassHdr pefas cls
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 | show_me (idName id) = Just (pprClassMethod pefas id)
233 | otherwise = Nothing
235 pprClassMethod :: PrintExplicitForalls -> Id -> SDoc
236 pprClassMethod pefas id
237 = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
239 -- Here's the magic incantation to strip off the dictionary
240 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
242 -- It's important to tidy it *before* splitting it up, so that if
243 -- we have class C a b where
244 -- op :: forall a. a -> b
245 -- then the inner forall on op gets renamed to a1, and we print
246 -- (when dropping foralls)
250 tidy_sel_ty = tidyTopType (GHC.idType id)
251 (_sel_tyvars, rho_ty) = GHC.splitForAllTys tidy_sel_ty
252 op_ty = GHC.funResultTy rho_ty
254 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
256 = snd (foldr go (False, []) xs)
258 go x (eliding, so_far)
259 | Just doc <- show x = (False, doc : so_far)
260 | otherwise = if eliding then (True, so_far)
261 else (True, ptext (sLit "...") : so_far)
263 add_bars :: [SDoc] -> SDoc
265 add_bars [c] = equals <+> c
266 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
268 -- Wrap operators in ()
269 ppr_bndr :: GHC.NamedThing a => a -> SDoc
270 ppr_bndr a = GHC.pprParenSymName a
272 showWithLoc :: SDoc -> SDoc -> SDoc
274 = hang doc 2 (char '\t' <> comment <+> loc)
275 -- The tab tries to make them line up a bit
277 comment = ptext (sLit "--")