Super-monster patch implementing the new typechecker -- at last
[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, pprTyThingParent_maybe,
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 IdInfo
25 import TyCon
26 import TcType
27 import Var
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 ShowMe = Name -> Bool
41 -- The ShowMe function says which sub-components to print
42 -- True <=> print
43 -- False <=> elide to "..."
44
45 ----------------------------
46 -- | Pretty-prints a 'TyThing' with its defining location.
47 pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
48 pprTyThingLoc pefas tyThing
49 = showWithLoc loc (pprTyThing pefas tyThing)
50 where loc = pprNameLoc (GHC.getName tyThing)
51
52 -- | Pretty-prints a 'TyThing'.
53 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
54 pprTyThing pefas thing = ppr_ty_thing pefas (const True) thing
55
56 ppr_ty_thing :: PrintExplicitForalls -> ShowMe -> TyThing -> SDoc
57 ppr_ty_thing pefas _ (AnId id) = pprId pefas id
58 ppr_ty_thing pefas _ (ADataCon dataCon) = pprDataConSig pefas dataCon
59 ppr_ty_thing pefas show_me (ATyCon tyCon) = pprTyCon pefas show_me tyCon
60 ppr_ty_thing pefas show_me (AClass cls) = pprClass pefas show_me cls
61
62 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
63 -- is a data constructor, record selector, or class method, then
64 -- the entity's parent declaration is pretty-printed with irrelevant
65 -- parts omitted.
66 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
67 pprTyThingInContext pefas thing
68 | Just parent <- pprTyThingParent_maybe thing
69 = ppr_ty_thing pefas (== GHC.getName thing) parent
70 | otherwise
71 = pprTyThing pefas thing
72
73 -- | Like 'pprTyThingInContext', but adds the defining location.
74 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
75 pprTyThingInContextLoc pefas tyThing
76 = showWithLoc (pprNameLoc (GHC.getName tyThing))
77 (pprTyThingInContext pefas tyThing)
78
79 pprTyThingParent_maybe :: TyThing -> Maybe TyThing
80 -- (pprTyThingParent_maybe x) returns (Just p)
81 -- when pprTyThingInContext sould print a declaration for p
82 -- (albeit with some "..." in it) when asked to show x
83 pprTyThingParent_maybe (ADataCon dc) = Just (ATyCon (dataConTyCon dc))
84 pprTyThingParent_maybe (AnId id) = case idDetails id of
85 RecSelId { sel_tycon = tc } -> Just (ATyCon tc)
86 ClassOpId cls -> Just (AClass cls)
87 _other -> Nothing
88 pprTyThingParent_maybe _other = Nothing
89
90 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
91 -- the function is equivalent to 'pprTyThing' but for type constructors
92 -- and classes it prints only the header part of the declaration.
93 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
94 pprTyThingHdr pefas (AnId id) = pprId pefas id
95 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
96 pprTyThingHdr pefas (ATyCon tyCon) = pprTyConHdr pefas tyCon
97 pprTyThingHdr pefas (AClass cls) = pprClassHdr pefas cls
98
99 pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc
100 pprTyConHdr _ tyCon
101 | Just (_fam_tc, tys) <- tyConFamInst_maybe tyCon
102 = ptext keyword <+> ptext (sLit "instance") <+> pprTypeApp tyCon tys
103 | otherwise
104 = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
105 where
106 vars | GHC.isPrimTyCon tyCon ||
107 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
108 | otherwise = GHC.tyConTyVars tyCon
109
110 keyword | GHC.isSynTyCon tyCon = sLit "type"
111 | GHC.isNewTyCon tyCon = sLit "newtype"
112 | otherwise = sLit "data"
113
114 opt_family
115 | GHC.isFamilyTyCon tyCon = ptext (sLit "family")
116 | otherwise = empty
117
118 opt_stupid -- The "stupid theta" part of the declaration
119 | isAlgTyCon tyCon = GHC.pprThetaArrow (tyConStupidTheta tyCon)
120 | otherwise = empty -- Returns 'empty' if null theta
121
122 pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc
123 pprDataConSig pefas dataCon
124 = ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
125
126 pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc
127 pprClassHdr _ cls
128 = ptext (sLit "class") <+>
129 GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
130 ppr_bndr cls <+>
131 hsep (map ppr tyVars) <+>
132 GHC.pprFundeps funDeps
133 where
134 (tyVars, funDeps) = GHC.classTvsFds cls
135
136 pprId :: PrintExplicitForalls -> Var -> SDoc
137 pprId pefas ident
138 = hang (ppr_bndr ident <+> dcolon)
139 2 (pprTypeForUser pefas (GHC.idType ident))
140
141 pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc
142 -- We do two things here.
143 -- a) We tidy the type, regardless
144 -- b) If PrintExplicitForAlls is True, we discard the foralls
145 -- but we do so `deeply'
146 -- Prime example: a class op might have type
147 -- forall a. C a => forall b. Ord b => stuff
148 -- Then we want to display
149 -- (C a, Ord b) => stuff
150 pprTypeForUser print_foralls ty
151 | print_foralls = ppr tidy_ty
152 | otherwise = ppr (mkPhiTy ctxt ty')
153 where
154 tidy_ty = tidyTopType ty
155 (_, ctxt, ty') = tcSplitSigmaTy tidy_ty
156
157 pprTyCon :: PrintExplicitForalls -> ShowMe -> TyCon -> SDoc
158 pprTyCon pefas show_me tyCon
159 | GHC.isSynTyCon tyCon
160 = if GHC.isFamilyTyCon tyCon
161 then pprTyConHdr pefas tyCon <+> dcolon <+>
162 pprTypeForUser pefas (GHC.synTyConResKind tyCon)
163 else
164 let rhs_type = GHC.synTyConType tyCon
165 in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type)
166 | otherwise
167 = pprAlgTyCon pefas show_me tyCon
168
169 pprAlgTyCon :: PrintExplicitForalls -> ShowMe -> TyCon -> SDoc
170 pprAlgTyCon pefas show_me tyCon
171 | gadt = pprTyConHdr pefas tyCon <+> ptext (sLit "where") $$
172 nest 2 (vcat (ppr_trim show_con datacons))
173 | otherwise = hang (pprTyConHdr pefas tyCon)
174 2 (add_bars (ppr_trim show_con datacons))
175 where
176 datacons = GHC.tyConDataCons tyCon
177 gadt = any (not . GHC.isVanillaDataCon) datacons
178
179 ok_con dc = show_me (dataConName dc) || any show_me (dataConFieldLabels dc)
180 show_con dc
181 | ok_con dc = Just (pprDataConDecl pefas show_me gadt dc)
182 | otherwise = Nothing
183
184 pprDataConDecl :: PrintExplicitForalls -> ShowMe -> Bool -> GHC.DataCon -> SDoc
185 pprDataConDecl pefas show_me gadt_style dataCon
186 | not gadt_style = ppr_fields tys_w_strs
187 | otherwise = ppr_bndr dataCon <+> dcolon <+>
188 sep [ pp_foralls, GHC.pprThetaArrow theta, pp_tau ]
189 -- Printing out the dataCon as a type signature, in GADT style
190 where
191 (forall_tvs, theta, tau) = tcSplitSigmaTy (GHC.dataConUserType dataCon)
192 (arg_tys, res_ty) = tcSplitFunTys tau
193 labels = GHC.dataConFieldLabels dataCon
194 stricts = GHC.dataConStrictMarks dataCon
195 tys_w_strs = zip stricts arg_tys
196 pp_foralls | pefas = GHC.pprForAll forall_tvs
197 | otherwise = empty
198
199 pp_tau = foldr add (ppr res_ty) tys_w_strs
200 add str_ty pp_ty = pprParendBangTy str_ty <+> arrow <+> pp_ty
201
202 pprParendBangTy (bang,ty) = ppr bang <> GHC.pprParendType ty
203
204 pprBangTy bang ty = ppr bang <> ppr ty
205
206 maybe_show_label (lbl,(strict,tp))
207 | show_me lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
208 | otherwise = Nothing
209
210 ppr_fields [ty1, ty2]
211 | GHC.dataConIsInfix dataCon && null labels
212 = sep [pprParendBangTy ty1, pprInfixName dataCon, pprParendBangTy ty2]
213 ppr_fields fields
214 | null labels
215 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
216 | otherwise
217 = ppr_bndr dataCon <+>
218 braces (sep (punctuate comma (ppr_trim maybe_show_label
219 (zip labels fields))))
220
221 pprClass :: PrintExplicitForalls -> ShowMe -> GHC.Class -> SDoc
222 pprClass pefas show_me cls
223 | null methods
224 = pprClassHdr pefas cls
225 | otherwise
226 = hang (pprClassHdr pefas cls <+> ptext (sLit "where"))
227 2 (vcat (ppr_trim show_meth methods))
228 where
229 methods = GHC.classMethods cls
230 show_meth id | show_me (idName id) = Just (pprClassMethod pefas id)
231 | otherwise = Nothing
232
233 pprClassMethod :: PrintExplicitForalls -> Id -> SDoc
234 pprClassMethod pefas id
235 = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
236 where
237 -- Here's the magic incantation to strip off the dictionary
238 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
239 --
240 -- It's important to tidy it *before* splitting it up, so that if
241 -- we have class C a b where
242 -- op :: forall a. a -> b
243 -- then the inner forall on op gets renamed to a1, and we print
244 -- (when dropping foralls)
245 -- class C a b where
246 -- op :: a1 -> b
247
248 tidy_sel_ty = tidyTopType (GHC.idType id)
249 (_sel_tyvars, rho_ty) = GHC.splitForAllTys tidy_sel_ty
250 op_ty = GHC.funResultTy rho_ty
251
252 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
253 ppr_trim show xs
254 = snd (foldr go (False, []) xs)
255 where
256 go x (eliding, so_far)
257 | Just doc <- show x = (False, doc : so_far)
258 | otherwise = if eliding then (True, so_far)
259 else (True, ptext (sLit "...") : so_far)
260
261 add_bars :: [SDoc] -> SDoc
262 add_bars [] = empty
263 add_bars [c] = equals <+> c
264 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
265
266 -- Wrap operators in ()
267 ppr_bndr :: GHC.NamedThing a => a -> SDoc
268 ppr_bndr a = GHC.pprParenSymName a
269
270 showWithLoc :: SDoc -> SDoc -> SDoc
271 showWithLoc loc doc
272 = hang doc 2 (char '\t' <> comment <+> loc)
273 -- The tab tries to make them line up a bit
274 where
275 comment = ptext (sLit "--")
276