d97fd961ebba417f24a67ac57c8e8f608615d960
[ghc.git] / compiler / main / PprTyThing.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Pretty-printing TyThings
4 --
5 -- (c) The GHC Team 2005
6 --
7 -----------------------------------------------------------------------------
8
9 module PprTyThing (
10 PrintExplicitForalls,
11 pprTyThing,
12 pprTyThingInContext,
13 pprTyThingLoc,
14 pprTyThingInContextLoc,
15 pprTyThingHdr,
16 pprTypeForUser
17 ) where
18
19 import qualified GHC
20
21 import GHC ( TyThing(..) )
22 import DataCon
23 import Id
24 import TyCon
25 import Coercion( pprCoAxiom )
26 import HscTypes( tyThingParent_maybe )
27 import TcType
28 import Name
29 import Outputable
30 import FastString
31
32 -- -----------------------------------------------------------------------------
33 -- Pretty-printing entities that we get from the GHC API
34
35 -- This should be a good source of sample code for using the GHC API to
36 -- inspect source code entities.
37
38 type PrintExplicitForalls = Bool
39
40 type ShowSub = [Name]
41 -- [] <=> print all sub-components of the current thing
42 -- (n:ns) <=> print sub-component 'n' with ShowSub=ns
43 -- elide other sub-components to "..."
44 showAll :: ShowSub
45 showAll = []
46
47 showSub :: NamedThing n => ShowSub -> n -> Bool
48 showSub [] _ = True
49 showSub (n:_) thing = n == getName thing
50
51 showSub_maybe :: NamedThing n => ShowSub -> n -> Maybe ShowSub
52 showSub_maybe [] _ = Just []
53 showSub_maybe (n:ns) thing = if n == getName thing then Just ns
54 else Nothing
55
56 ----------------------------
57 -- | Pretty-prints a 'TyThing' with its defining location.
58 pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
59 pprTyThingLoc pefas tyThing
60 = showWithLoc loc (pprTyThing pefas tyThing)
61 where loc = pprNameLoc (GHC.getName tyThing)
62
63 -- | Pretty-prints a 'TyThing'.
64 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
65 pprTyThing pefas thing = ppr_ty_thing pefas showAll thing
66
67 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
68 -- is a data constructor, record selector, or class method, then
69 -- the entity's parent declaration is pretty-printed with irrelevant
70 -- parts omitted.
71 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
72 pprTyThingInContext pefas thing
73 = go [] thing
74 where
75 go ss thing = case tyThingParent_maybe thing of
76 Just parent -> go (getName thing : ss) parent
77 Nothing -> ppr_ty_thing pefas ss thing
78
79 -- | Like 'pprTyThingInContext', but adds the defining location.
80 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
81 pprTyThingInContextLoc pefas tyThing
82 = showWithLoc (pprNameLoc (GHC.getName tyThing))
83 (pprTyThingInContext pefas tyThing)
84
85 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
86 -- the function is equivalent to 'pprTyThing' but for type constructors
87 -- and classes it prints only the header part of the declaration.
88 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
89 pprTyThingHdr pefas (AnId id) = pprId pefas id
90 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
91 pprTyThingHdr pefas (ATyCon tyCon) = pprTyConHdr pefas tyCon
92 pprTyThingHdr _ (ACoAxiom ax) = pprCoAxiom ax
93 pprTyThingHdr pefas (AClass cls) = pprClassHdr pefas cls
94
95 ------------------------
96 ppr_ty_thing :: PrintExplicitForalls -> ShowSub -> TyThing -> SDoc
97 ppr_ty_thing pefas _ (AnId id) = pprId pefas id
98 ppr_ty_thing pefas _ (ADataCon dataCon) = pprDataConSig pefas dataCon
99 ppr_ty_thing pefas ss (ATyCon tyCon) = pprTyCon pefas ss tyCon
100 ppr_ty_thing _ _ (ACoAxiom ax) = pprCoAxiom ax
101 ppr_ty_thing pefas ss (AClass cls) = pprClass pefas ss cls
102
103 pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc
104 pprTyConHdr _ tyCon
105 | Just (fam_tc, tys) <- tyConFamInst_maybe tyCon
106 = ptext keyword <+> ptext (sLit "instance") <+> pprTypeApp fam_tc tys
107 | otherwise
108 = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
109 where
110 vars | GHC.isPrimTyCon tyCon ||
111 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
112 | otherwise = GHC.tyConTyVars tyCon
113
114 keyword | GHC.isSynTyCon tyCon = sLit "type"
115 | GHC.isNewTyCon tyCon = sLit "newtype"
116 | otherwise = sLit "data"
117
118 opt_family
119 | GHC.isFamilyTyCon tyCon = ptext (sLit "family")
120 | otherwise = empty
121
122 opt_stupid -- The "stupid theta" part of the declaration
123 | isAlgTyCon tyCon = GHC.pprThetaArrowTy (tyConStupidTheta tyCon)
124 | otherwise = empty -- Returns 'empty' if null theta
125
126 pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc
127 pprDataConSig pefas dataCon
128 = ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
129
130 pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc
131 pprClassHdr _ cls
132 = ptext (sLit "class") <+>
133 GHC.pprThetaArrowTy (GHC.classSCTheta cls) <+>
134 ppr_bndr cls <+>
135 hsep (map ppr tyVars) <+>
136 GHC.pprFundeps funDeps
137 where
138 (tyVars, funDeps) = GHC.classTvsFds cls
139
140 pprId :: PrintExplicitForalls -> Var -> SDoc
141 pprId pefas ident
142 = hang (ppr_bndr ident <+> dcolon)
143 2 (pprTypeForUser pefas (GHC.idType ident))
144
145 pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc
146 -- We do two things here.
147 -- a) We tidy the type, regardless
148 -- b) If PrintExplicitForAlls is True, we discard the foralls
149 -- but we do so `deeply'
150 -- Prime example: a class op might have type
151 -- forall a. C a => forall b. Ord b => stuff
152 -- Then we want to display
153 -- (C a, Ord b) => stuff
154 pprTypeForUser print_foralls ty
155 | print_foralls = ppr tidy_ty
156 | otherwise = ppr (mkPhiTy ctxt ty')
157 where
158 tidy_ty = tidyTopType ty
159 (_, ctxt, ty') = tcSplitSigmaTy tidy_ty
160
161 pprTyCon :: PrintExplicitForalls -> ShowSub -> TyCon -> SDoc
162 pprTyCon pefas ss tyCon
163 | GHC.isSynTyCon tyCon
164 = if GHC.isFamilyTyCon tyCon
165 then pprTyConHdr pefas tyCon <+> dcolon <+>
166 pprTypeForUser pefas (GHC.synTyConResKind tyCon)
167 else
168 let rhs_type = GHC.synTyConType tyCon
169 in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type)
170 | otherwise
171 = pprAlgTyCon pefas ss tyCon
172
173 pprAlgTyCon :: PrintExplicitForalls -> ShowSub -> TyCon -> SDoc
174 pprAlgTyCon pefas ss tyCon
175 | gadt = pprTyConHdr pefas tyCon <+> ptext (sLit "where") $$
176 nest 2 (vcat (ppr_trim (map show_con datacons)))
177 | otherwise = hang (pprTyConHdr pefas tyCon)
178 2 (add_bars (ppr_trim (map show_con datacons)))
179 where
180 datacons = GHC.tyConDataCons tyCon
181 gadt = any (not . GHC.isVanillaDataCon) datacons
182
183 ok_con dc = showSub ss dc || any (showSub ss) (dataConFieldLabels dc)
184 show_con dc
185 | ok_con dc = Just (pprDataConDecl pefas ss gadt dc)
186 | otherwise = Nothing
187
188 pprDataConDecl :: PrintExplicitForalls -> ShowSub -> Bool -> GHC.DataCon -> SDoc
189 pprDataConDecl pefas ss gadt_style dataCon
190 | not gadt_style = ppr_fields tys_w_strs
191 | otherwise = ppr_bndr dataCon <+> dcolon <+>
192 sep [ pp_foralls, GHC.pprThetaArrowTy theta, pp_tau ]
193 -- Printing out the dataCon as a type signature, in GADT style
194 where
195 (forall_tvs, theta, tau) = tcSplitSigmaTy (GHC.dataConUserType dataCon)
196 (arg_tys, res_ty) = tcSplitFunTys tau
197 labels = GHC.dataConFieldLabels dataCon
198 stricts = GHC.dataConStrictMarks dataCon
199 tys_w_strs = zip stricts arg_tys
200 pp_foralls | pefas = GHC.pprForAll forall_tvs
201 | otherwise = empty
202
203 pp_tau = foldr add (ppr res_ty) tys_w_strs
204 add str_ty pp_ty = pprParendBangTy str_ty <+> arrow <+> pp_ty
205
206 pprParendBangTy (bang,ty) = ppr bang <> GHC.pprParendType ty
207
208 pprBangTy bang ty = ppr bang <> ppr ty
209
210 maybe_show_label (lbl,(strict,tp))
211 | showSub ss lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
212 | otherwise = Nothing
213
214 ppr_fields [ty1, ty2]
215 | GHC.dataConIsInfix dataCon && null labels
216 = sep [pprParendBangTy ty1, pprInfixName dataCon, pprParendBangTy ty2]
217 ppr_fields fields
218 | null labels
219 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
220 | otherwise
221 = ppr_bndr dataCon
222 <+> (braces $ sep $ punctuate comma $ ppr_trim $
223 map maybe_show_label (zip labels fields))
224
225 pprClass :: PrintExplicitForalls -> ShowSub -> GHC.Class -> SDoc
226 pprClass pefas ss cls
227 | null methods && null assoc_ts
228 = pprClassHdr pefas cls
229 | otherwise
230 = vcat [ pprClassHdr pefas cls <+> ptext (sLit "where")
231 , nest 2 (vcat $ ppr_trim $
232 map show_at assoc_ts ++ map show_meth methods)]
233 where
234 methods = GHC.classMethods cls
235 assoc_ts = GHC.classATs cls
236 show_meth id | showSub ss id = Just (pprClassMethod pefas id)
237 | otherwise = Nothing
238 show_at tc = case showSub_maybe ss tc of
239 Just ss' -> Just (pprTyCon pefas ss' tc)
240 Nothing -> Nothing
241
242 pprClassMethod :: PrintExplicitForalls -> Id -> SDoc
243 pprClassMethod pefas id
244 = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
245 where
246 -- Here's the magic incantation to strip off the dictionary
247 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
248 --
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)
254 -- class C a b where
255 -- op :: a1 -> b
256
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
260
261 ppr_trim :: [Maybe SDoc] -> [SDoc]
262 -- Collapse a group of Nothings to a single "..."
263 ppr_trim xs
264 = snd (foldr go (False, []) xs)
265 where
266 go (Just doc) (_, so_far) = (False, doc : so_far)
267 go Nothing (True, so_far) = (True, so_far)
268 go Nothing (False, so_far) = (True, ptext (sLit "...") : so_far)
269
270 add_bars :: [SDoc] -> SDoc
271 add_bars [] = empty
272 add_bars [c] = equals <+> c
273 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
274
275 -- Wrap operators in ()
276 ppr_bndr :: GHC.NamedThing a => a -> SDoc
277 ppr_bndr a = GHC.pprParenSymName a
278
279 showWithLoc :: SDoc -> SDoc -> SDoc
280 showWithLoc loc doc
281 = hang doc 2 (char '\t' <> comment <+> loc)
282 -- The tab tries to make them line up a bit
283 where
284 comment = ptext (sLit "--")
285