Merge branch 'fix#5464' of http://darcs.haskell.org/ghc into fix#5464
[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 (pprDefinedAt (GHC.getName tyThing)) (pprTyThing pefas tyThing)
61
62 -- | Pretty-prints a 'TyThing'.
63 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
64 pprTyThing pefas thing = ppr_ty_thing pefas showAll thing
65
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
69 -- parts omitted.
70 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
71 pprTyThingInContext pefas thing
72 = go [] thing
73 where
74 go ss thing = case tyThingParent_maybe thing of
75 Just parent -> go (getName thing : ss) parent
76 Nothing -> ppr_ty_thing pefas ss thing
77
78 -- | Like 'pprTyThingInContext', but adds the defining location.
79 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
80 pprTyThingInContextLoc pefas tyThing
81 = showWithLoc (pprDefinedAt (GHC.getName tyThing))
82 (pprTyThingInContext pefas tyThing)
83
84 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
85 -- the function is equivalent to 'pprTyThing' but for type constructors
86 -- and classes it prints only the header part of the declaration.
87 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
88 pprTyThingHdr pefas (AnId id) = pprId pefas id
89 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
90 pprTyThingHdr pefas (ATyCon tyCon) = pprTyConHdr pefas tyCon
91 pprTyThingHdr _ (ACoAxiom ax) = pprCoAxiom ax
92
93 ------------------------
94 ppr_ty_thing :: PrintExplicitForalls -> ShowSub -> TyThing -> SDoc
95 ppr_ty_thing pefas _ (AnId id) = pprId pefas id
96 ppr_ty_thing pefas _ (ADataCon dataCon) = pprDataConSig pefas dataCon
97 ppr_ty_thing pefas ss (ATyCon tyCon) = pprTyCon pefas ss tyCon
98 ppr_ty_thing _ _ (ACoAxiom ax) = pprCoAxiom ax
99 pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc
100 pprTyConHdr pefas tyCon
101 | Just (fam_tc, tys) <- tyConFamInst_maybe tyCon
102 = ptext keyword <+> ptext (sLit "instance") <+> pprTypeApp fam_tc tys
103 | Just cls <- tyConClass_maybe tyCon
104 = pprClassHdr pefas cls
105 | otherwise
106 = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
107 where
108 vars | GHC.isPrimTyCon tyCon ||
109 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
110 | otherwise = GHC.tyConTyVars tyCon
111
112 keyword | GHC.isSynTyCon tyCon = sLit "type"
113 | GHC.isNewTyCon tyCon = sLit "newtype"
114 | otherwise = sLit "data"
115
116 opt_family
117 | GHC.isFamilyTyCon tyCon = ptext (sLit "family")
118 | otherwise = empty
119
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
123
124 pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc
125 pprDataConSig pefas dataCon
126 = ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
127
128 pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc
129 pprClassHdr _ cls
130 = ptext (sLit "class") <+>
131 GHC.pprThetaArrowTy (GHC.classSCTheta cls) <+>
132 ppr_bndr cls <+>
133 hsep (map ppr tyVars) <+>
134 GHC.pprFundeps funDeps
135 where
136 (tyVars, funDeps) = GHC.classTvsFds cls
137
138 pprId :: PrintExplicitForalls -> Var -> SDoc
139 pprId pefas ident
140 = hang (ppr_bndr ident <+> dcolon)
141 2 (pprTypeForUser pefas (GHC.idType ident))
142
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')
155 where
156 tidy_ty = tidyTopType ty
157 (_, ctxt, ty') = tcSplitSigmaTy tidy_ty
158
159 pprTyCon :: PrintExplicitForalls -> ShowSub -> TyCon -> SDoc
160 pprTyCon pefas ss tyCon
161 | GHC.isSynTyCon tyCon
162 = if GHC.isFamilyTyCon tyCon
163 then pprTyConHdr pefas tyCon <+> dcolon <+>
164 pprTypeForUser pefas (GHC.synTyConResKind tyCon)
165 else
166 let rhs_type = GHC.synTyConType tyCon
167 in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type)
168 | Just cls <- GHC.tyConClass_maybe tyCon
169 = pprClass pefas ss cls
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