1 -----------------------------------------------------------------------------
3 -- Pretty-printing TyThings
5 -- (c) The GHC Team 2005
7 -----------------------------------------------------------------------------
14 pprTyThingInContextLoc,
21 import GHC ( TyThing(..) )
29 -- -----------------------------------------------------------------------------
30 -- Pretty-printing entities that we get from the GHC API
32 -- This should be a good source of sample code for using the GHC API to
33 -- inspect source code entities.
35 type PrintExplicitForalls = Bool
37 -- | Pretty-prints a 'TyThing' with its defining location.
38 pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
39 pprTyThingLoc pefas tyThing
40 = showWithLoc loc (pprTyThing pefas tyThing)
41 where loc = pprNameLoc (GHC.getName tyThing)
43 -- | Pretty-prints a 'TyThing'.
44 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
45 pprTyThing pefas (AnId id) = pprId pefas id
46 pprTyThing pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
47 pprTyThing pefas (ATyCon tyCon) = pprTyCon pefas tyCon
48 pprTyThing pefas (AClass cls) = pprClass pefas cls
50 -- | Like 'pprTyThingInContext', but adds the defining location.
51 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
52 pprTyThingInContextLoc pefas tyThing
53 = showWithLoc loc (pprTyThingInContext pefas tyThing)
54 where loc = pprNameLoc (GHC.getName tyThing)
56 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
57 -- is a data constructor, record selector, or class method, then
58 -- the entity's parent declaration is pretty-printed with irrelevant
60 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
61 pprTyThingInContext pefas (AnId id) = pprIdInContext pefas id
62 pprTyThingInContext pefas (ADataCon dataCon) = pprDataCon pefas dataCon
63 pprTyThingInContext pefas (ATyCon tyCon) = pprTyCon pefas tyCon
64 pprTyThingInContext pefas (AClass cls) = pprClass pefas cls
66 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
67 -- the function is equivalent to 'pprTyThing' but for type constructors
68 -- and classes it prints only the header part of the declaration.
69 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
70 pprTyThingHdr pefas (AnId id) = pprId pefas id
71 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
72 pprTyThingHdr pefas (ATyCon tyCon) = pprTyConHdr pefas tyCon
73 pprTyThingHdr pefas (AClass cls) = pprClassHdr pefas cls
75 pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc
77 | Just (_fam_tc, tys) <- tyConFamInst_maybe tyCon
78 = ptext keyword <+> ptext (sLit "instance") <+> pprTypeApp tyCon tys
80 = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
82 vars | GHC.isPrimTyCon tyCon ||
83 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
84 | otherwise = GHC.tyConTyVars tyCon
86 keyword | GHC.isSynTyCon tyCon = sLit "type"
87 | GHC.isNewTyCon tyCon = sLit "newtype"
88 | otherwise = sLit "data"
91 | GHC.isOpenTyCon tyCon = ptext (sLit "family")
94 opt_stupid -- The "stupid theta" part of the declaration
95 | isAlgTyCon tyCon = GHC.pprThetaArrow (tyConStupidTheta tyCon)
96 | otherwise = empty -- Returns 'empty' if null theta
98 pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc
99 pprDataConSig pefas dataCon =
100 ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
102 pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc
104 let (tyVars, funDeps) = GHC.classTvsFds cls
105 in ptext (sLit "class") <+>
106 GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
108 hsep (map ppr tyVars) <+>
109 GHC.pprFundeps funDeps
111 pprIdInContext :: PrintExplicitForalls -> Var -> SDoc
112 pprIdInContext pefas id
113 | GHC.isRecordSelector id = pprRecordSelector pefas id
114 | Just cls <- GHC.isClassOpId_maybe id = pprClassOneMethod pefas cls id
115 | otherwise = pprId pefas id
117 pprRecordSelector :: PrintExplicitForalls -> Id -> SDoc
118 pprRecordSelector pefas id
119 = pprAlgTyCon pefas tyCon show_con show_label
121 (tyCon,label) = GHC.recordSelectorFieldLabel id
122 show_con dataCon = label `elem` GHC.dataConFieldLabels dataCon
123 show_label label' = label == label'
125 pprId :: PrintExplicitForalls -> Var -> SDoc
127 = hang (ppr_bndr ident <+> dcolon)
128 2 (pprTypeForUser pefas (GHC.idType ident))
130 pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc
131 -- We do two things here.
132 -- a) We tidy the type, regardless
133 -- b) If PrintExplicitForAlls is True, we discard the foralls
134 -- but we do so `deeply'
135 -- Prime example: a class op might have type
136 -- forall a. C a => forall b. Ord b => stuff
137 -- Then we want to display
138 -- (C a, Ord b) => stuff
139 pprTypeForUser print_foralls ty
140 | print_foralls = ppr tidy_ty
141 | otherwise = ppr (mkPhiTy [p | (_tvs, ps) <- ctxt, p <- ps] ty')
143 tidy_ty = tidyTopType ty
144 (ctxt, ty') = tcMultiSplitSigmaTy tidy_ty
146 pprTyCon :: PrintExplicitForalls -> TyCon -> SDoc
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 :: PrintExplicitForalls -> TyCon -> (GHC.DataCon -> Bool)
159 -> (FieldLabel -> Bool) -> SDoc
160 pprAlgTyCon pefas tyCon ok_con ok_label
161 | gadt = pprTyConHdr pefas tyCon <+> ptext (sLit "where") $$
162 nest 2 (vcat (ppr_trim show_con datacons))
163 | otherwise = hang (pprTyConHdr pefas tyCon)
164 2 (add_bars (ppr_trim show_con datacons))
166 datacons = GHC.tyConDataCons tyCon
167 gadt = any (not . GHC.isVanillaDataCon) datacons
170 | ok_con dataCon = Just (pprDataConDecl pefas gadt ok_label dataCon)
171 | otherwise = Nothing
173 pprDataCon :: PrintExplicitForalls -> GHC.DataCon -> SDoc
174 pprDataCon pefas dataCon = pprAlgTyCon pefas tyCon (== dataCon) (const True)
175 where tyCon = GHC.dataConTyCon dataCon
177 pprDataConDecl :: PrintExplicitForalls -> Bool -> (FieldLabel -> Bool)
178 -> GHC.DataCon -> SDoc
179 pprDataConDecl _ gadt_style show_label dataCon
180 | not gadt_style = ppr_fields tys_w_strs
181 | otherwise = ppr_bndr dataCon <+> dcolon <+>
182 sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
184 (tyvars, theta, argTypes, res_ty) = GHC.dataConSig dataCon
185 tyCon = GHC.dataConTyCon dataCon
186 labels = GHC.dataConFieldLabels dataCon
187 qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
188 stricts = GHC.dataConStrictMarks dataCon
189 tys_w_strs = zip stricts argTypes
192 | null qualVars = empty
193 | otherwise = ptext (sLit "forall") <+>
194 hsep (map ppr qualVars) <> dot
196 -- printing out the dataCon as a type signature, in GADT style
197 pp_tau = foldr add (ppr res_ty) tys_w_strs
198 add str_ty pp_ty = pprParendBangTy str_ty <+> arrow <+> pp_ty
200 pprParendBangTy (strict,ty)
201 | GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty
202 | otherwise = GHC.pprParendType ty
205 | GHC.isMarkedStrict strict = char '!' <> ppr ty
208 maybe_show_label (lbl,(strict,tp))
209 | show_label 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 -> GHC.Class -> SDoc
226 pprClassHdr pefas cls
228 hang (pprClassHdr pefas cls <+> ptext (sLit "where"))
229 2 (vcat (map (pprClassMethod pefas) methods))
231 methods = GHC.classMethods cls
233 pprClassOneMethod :: PrintExplicitForalls -> GHC.Class -> Id -> SDoc
234 pprClassOneMethod pefas cls this_one
235 = hang (pprClassHdr pefas cls <+> ptext (sLit "where"))
236 2 (vcat (ppr_trim show_meth methods))
238 methods = GHC.classMethods cls
239 show_meth id | id == this_one = Just (pprClassMethod pefas id)
240 | otherwise = Nothing
242 pprClassMethod :: PrintExplicitForalls -> Id -> SDoc
243 pprClassMethod pefas id
244 = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
246 -- Here's the magic incantation to strip off the dictionary
247 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
249 -- It's important to tidy it *before* splitting it up, so that if
250 -- we have class C a b where
251 -- op :: forall a. a -> b
252 -- then the inner forall on op gets renamed to a1, and we print
253 -- (when dropping foralls)
257 tidy_sel_ty = tidyTopType (GHC.idType id)
258 (_sel_tyvars, rho_ty) = GHC.splitForAllTys tidy_sel_ty
259 op_ty = GHC.funResultTy rho_ty
261 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
263 = snd (foldr go (False, []) xs)
265 go x (eliding, so_far)
266 | Just doc <- show x = (False, doc : so_far)
267 | otherwise = if eliding then (True, so_far)
268 else (True, ptext (sLit "...") : so_far)
270 add_bars :: [SDoc] -> SDoc
272 add_bars [c] = equals <+> c
273 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
275 -- Wrap operators in ()
276 ppr_bndr :: GHC.NamedThing a => a -> SDoc
277 ppr_bndr a = GHC.pprParenSymName a
279 showWithLoc :: SDoc -> SDoc -> SDoc
281 = hang doc 2 (char '\t' <> comment <+> loc)
282 -- The tab tries to make them line up a bit
284 comment = ptext (sLit "--")