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