Some minor wibbling in printing source locations
[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 pprTyThingHdr pefas (AClass cls) = pprClassHdr pefas cls
93
94 ------------------------
95 ppr_ty_thing :: PrintExplicitForalls -> ShowSub -> TyThing -> SDoc
96 ppr_ty_thing pefas _ (AnId id) = pprId pefas id
97 ppr_ty_thing pefas _ (ADataCon dataCon) = pprDataConSig pefas dataCon
98 ppr_ty_thing pefas ss (ATyCon tyCon) = pprTyCon pefas ss tyCon
99 ppr_ty_thing _ _ (ACoAxiom ax) = pprCoAxiom ax
100 ppr_ty_thing pefas ss (AClass cls) = pprClass pefas ss cls
101
102 pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc
103 pprTyConHdr _ tyCon
104 | Just (fam_tc, tys) <- tyConFamInst_maybe tyCon
105 = ptext keyword <+> ptext (sLit "instance") <+> pprTypeApp fam_tc tys
106 | otherwise
107 = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
108 where
109 vars | GHC.isPrimTyCon tyCon ||
110 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
111 | otherwise = GHC.tyConTyVars tyCon
112
113 keyword | GHC.isSynTyCon tyCon = sLit "type"
114 | GHC.isNewTyCon tyCon = sLit "newtype"
115 | otherwise = sLit "data"
116
117 opt_family
118 | GHC.isFamilyTyCon tyCon = ptext (sLit "family")
119 | otherwise = empty
120
121 opt_stupid -- The "stupid theta" part of the declaration
122 | isAlgTyCon tyCon = GHC.pprThetaArrowTy (tyConStupidTheta tyCon)
123 | otherwise = empty -- Returns 'empty' if null theta
124
125 pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc
126 pprDataConSig pefas dataCon
127 = ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
128
129 pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc
130 pprClassHdr _ cls
131 = ptext (sLit "class") <+>
132 GHC.pprThetaArrowTy (GHC.classSCTheta cls) <+>
133 ppr_bndr cls <+>
134 hsep (map ppr tyVars) <+>
135 GHC.pprFundeps funDeps
136 where
137 (tyVars, funDeps) = GHC.classTvsFds cls
138
139 pprId :: PrintExplicitForalls -> Var -> SDoc
140 pprId pefas ident
141 = hang (ppr_bndr ident <+> dcolon)
142 2 (pprTypeForUser pefas (GHC.idType ident))
143
144 pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc
145 -- We do two things here.
146 -- a) We tidy the type, regardless
147 -- b) If PrintExplicitForAlls is True, we discard the foralls
148 -- but we do so `deeply'
149 -- Prime example: a class op might have type
150 -- forall a. C a => forall b. Ord b => stuff
151 -- Then we want to display
152 -- (C a, Ord b) => stuff
153 pprTypeForUser print_foralls ty
154 | print_foralls = ppr tidy_ty
155 | otherwise = ppr (mkPhiTy ctxt ty')
156 where
157 tidy_ty = tidyTopType ty
158 (_, ctxt, ty') = tcSplitSigmaTy tidy_ty
159
160 pprTyCon :: PrintExplicitForalls -> ShowSub -> TyCon -> SDoc
161 pprTyCon pefas ss tyCon
162 | GHC.isSynTyCon tyCon
163 = if GHC.isFamilyTyCon tyCon
164 then pprTyConHdr pefas tyCon <+> dcolon <+>
165 pprTypeForUser pefas (GHC.synTyConResKind tyCon)
166 else
167 let rhs_type = GHC.synTyConType tyCon
168 in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type)
169 | otherwise
170 = pprAlgTyCon pefas ss tyCon
171
172 pprAlgTyCon :: PrintExplicitForalls -> ShowSub -> TyCon -> SDoc
173 pprAlgTyCon pefas ss tyCon
174 | gadt = pprTyConHdr pefas tyCon <+> ptext (sLit "where") $$
175 nest 2 (vcat (ppr_trim (map show_con datacons)))
176 | otherwise = hang (pprTyConHdr pefas tyCon)
177 2 (add_bars (ppr_trim (map show_con datacons)))
178 where
179 datacons = GHC.tyConDataCons tyCon
180 gadt = any (not . GHC.isVanillaDataCon) datacons
181
182 ok_con dc = showSub ss dc || any (showSub ss) (dataConFieldLabels dc)
183 show_con dc
184 | ok_con dc = Just (pprDataConDecl pefas ss gadt dc)
185 | otherwise = Nothing
186
187 pprDataConDecl :: PrintExplicitForalls -> ShowSub -> Bool -> GHC.DataCon -> SDoc
188 pprDataConDecl pefas ss gadt_style dataCon
189 | not gadt_style = ppr_fields tys_w_strs
190 | otherwise = ppr_bndr dataCon <+> dcolon <+>
191 sep [ pp_foralls, GHC.pprThetaArrowTy theta, pp_tau ]
192 -- Printing out the dataCon as a type signature, in GADT style
193 where
194 (forall_tvs, theta, tau) = tcSplitSigmaTy (GHC.dataConUserType dataCon)
195 (arg_tys, res_ty) = tcSplitFunTys tau
196 labels = GHC.dataConFieldLabels dataCon
197 stricts = GHC.dataConStrictMarks dataCon
198 tys_w_strs = zip stricts arg_tys
199 pp_foralls | pefas = GHC.pprForAll forall_tvs
200 | otherwise = empty
201
202 pp_tau = foldr add (ppr res_ty) tys_w_strs
203 add str_ty pp_ty = pprParendBangTy str_ty <+> arrow <+> pp_ty
204
205 pprParendBangTy (bang,ty) = ppr bang <> GHC.pprParendType ty
206
207 pprBangTy bang ty = ppr bang <> ppr ty
208
209 maybe_show_label (lbl,(strict,tp))
210 | showSub ss lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
211 | otherwise = Nothing
212
213 ppr_fields [ty1, ty2]
214 | GHC.dataConIsInfix dataCon && null labels
215 = sep [pprParendBangTy ty1, pprInfixName dataCon, pprParendBangTy ty2]
216 ppr_fields fields
217 | null labels
218 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
219 | otherwise
220 = ppr_bndr dataCon
221 <+> (braces $ sep $ punctuate comma $ ppr_trim $
222 map maybe_show_label (zip labels fields))
223
224 pprClass :: PrintExplicitForalls -> ShowSub -> GHC.Class -> SDoc
225 pprClass pefas ss cls
226 | null methods && null assoc_ts
227 = pprClassHdr pefas cls
228 | otherwise
229 = vcat [ pprClassHdr pefas cls <+> ptext (sLit "where")
230 , nest 2 (vcat $ ppr_trim $
231 map show_at assoc_ts ++ map show_meth methods)]
232 where
233 methods = GHC.classMethods cls
234 assoc_ts = GHC.classATs cls
235 show_meth id | showSub ss id = Just (pprClassMethod pefas id)
236 | otherwise = Nothing
237 show_at tc = case showSub_maybe ss tc of
238 Just ss' -> Just (pprTyCon pefas ss' tc)
239 Nothing -> Nothing
240
241 pprClassMethod :: PrintExplicitForalls -> Id -> SDoc
242 pprClassMethod pefas id
243 = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
244 where
245 -- Here's the magic incantation to strip off the dictionary
246 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
247 --
248 -- It's important to tidy it *before* splitting it up, so that if
249 -- we have class C a b where
250 -- op :: forall a. a -> b
251 -- then the inner forall on op gets renamed to a1, and we print
252 -- (when dropping foralls)
253 -- class C a b where
254 -- op :: a1 -> b
255
256 tidy_sel_ty = tidyTopType (GHC.idType id)
257 (_sel_tyvars, rho_ty) = GHC.splitForAllTys tidy_sel_ty
258 op_ty = GHC.funResultTy rho_ty
259
260 ppr_trim :: [Maybe SDoc] -> [SDoc]
261 -- Collapse a group of Nothings to a single "..."
262 ppr_trim xs
263 = snd (foldr go (False, []) xs)
264 where
265 go (Just doc) (_, so_far) = (False, doc : so_far)
266 go Nothing (True, so_far) = (True, so_far)
267 go Nothing (False, so_far) = (True, ptext (sLit "...") : so_far)
268
269 add_bars :: [SDoc] -> SDoc
270 add_bars [] = empty
271 add_bars [c] = equals <+> c
272 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
273
274 -- Wrap operators in ()
275 ppr_bndr :: GHC.NamedThing a => a -> SDoc
276 ppr_bndr a = GHC.pprParenSymName a
277
278 showWithLoc :: SDoc -> SDoc -> SDoc
279 showWithLoc loc doc
280 = hang doc 2 (char '\t' <> comment <+> loc)
281 -- The tab tries to make them line up a bit
282 where
283 comment = ptext (sLit "--")
284