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