FIX #903: mkWWcpr: not a product
[ghc-hetmet.git] / compiler / main / PprTyThing.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Pretty-printing TyThings
4 --
5 -- (c) The GHC Team 2005
6 --
7 -----------------------------------------------------------------------------
8
9 {-# OPTIONS -w #-}
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
14 -- for details
15
16 module PprTyThing (
17         PrintExplicitForalls,
18         pprTyThing,
19         pprTyThingInContext,
20         pprTyThingLoc,
21         pprTyThingInContextLoc,
22         pprTyThingHdr
23   ) where
24
25 #include "HsVersions.h"
26
27 import qualified GHC
28
29 import TyCon    ( tyConFamInst_maybe )
30 import Type     ( pprTypeApp )
31 import GHC      ( TyThing(..), SrcSpan )
32 import Var
33 import Name
34 import Outputable
35
36 -- -----------------------------------------------------------------------------
37 -- Pretty-printing entities that we get from the GHC API
38
39 -- This should be a good source of sample code for using the GHC API to
40 -- inspect source code entities.
41
42 type PrintExplicitForalls = Bool
43
44 -- | Pretty-prints a 'TyThing' with its defining location.
45 pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
46 pprTyThingLoc pefas tyThing 
47   = showWithLoc loc (pprTyThing pefas tyThing)
48   where loc = pprNameLoc (GHC.getName tyThing)
49
50 -- | Pretty-prints a 'TyThing'.
51 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
52 pprTyThing pefas (AnId id)          = pprId         pefas id
53 pprTyThing pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
54 pprTyThing pefas (ATyCon tyCon)     = pprTyCon      pefas tyCon
55 pprTyThing pefas (AClass cls)       = pprClass      pefas cls
56
57 -- | Like 'pprTyThingInContext', but adds the defining location.
58 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
59 pprTyThingInContextLoc pefas tyThing
60   = showWithLoc loc (pprTyThingInContext pefas tyThing)
61   where loc = pprNameLoc (GHC.getName tyThing)
62
63 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
64 -- is a data constructor, record selector, or class method, then 
65 -- the entity's parent declaration is pretty-printed with irrelevant
66 -- parts omitted.
67 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
68 pprTyThingInContext pefas (AnId id)          = pprIdInContext pefas id
69 pprTyThingInContext pefas (ADataCon dataCon) = pprDataCon pefas dataCon
70 pprTyThingInContext pefas (ATyCon tyCon)     = pprTyCon   pefas tyCon
71 pprTyThingInContext pefas (AClass cls)       = pprClass   pefas cls
72
73 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
74 -- the function is equivalent to 'pprTyThing' but for type constructors
75 -- and classes it prints only the header part of the declaration.
76 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
77 pprTyThingHdr pefas (AnId id)          = pprId         pefas id
78 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
79 pprTyThingHdr pefas (ATyCon tyCon)     = pprTyConHdr   pefas tyCon
80 pprTyThingHdr pefas (AClass cls)       = pprClassHdr   pefas cls
81         
82 pprTyConHdr pefas tyCon
83   | Just (fam_tc, tys) <- tyConFamInst_maybe tyCon
84   = ptext keyword <+> ptext SLIT("instance") <+> pprTypeApp tyCon (ppr_bndr tyCon) tys
85   | otherwise
86   = ptext keyword <+> opt_family <+> ppr_bndr tyCon <+> hsep (map ppr vars)
87   where
88     vars | GHC.isPrimTyCon tyCon || 
89            GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
90          | otherwise = GHC.tyConTyVars tyCon
91
92     keyword | GHC.isSynTyCon tyCon = SLIT("type")
93             | GHC.isNewTyCon tyCon = SLIT("newtype")
94             | otherwise            = SLIT("data")
95
96     opt_family
97       | GHC.isOpenTyCon tyCon = ptext SLIT("family")
98       | otherwise             = empty
99
100 pprDataConSig pefas dataCon =
101   ppr_bndr dataCon <+> dcolon <+> pprType pefas (GHC.dataConType dataCon)
102
103 pprClassHdr pefas cls =
104   let (tyVars, funDeps) = GHC.classTvsFds cls
105   in ptext SLIT("class") <+> 
106      GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
107      ppr_bndr cls <+>
108      hsep (map ppr tyVars) <+>
109      GHC.pprFundeps funDeps
110
111 pprIdInContext pefas id
112   | GHC.isRecordSelector id               = pprRecordSelector pefas id
113   | Just cls <- GHC.isClassOpId_maybe id  = pprClassOneMethod pefas cls id
114   | otherwise                             = pprId pefas id
115
116 pprRecordSelector pefas id
117   = pprAlgTyCon pefas tyCon show_con show_label
118   where
119         (tyCon,label) = GHC.recordSelectorFieldLabel id
120         show_con dataCon  = label `elem` GHC.dataConFieldLabels dataCon
121         show_label label' = label == label'
122
123 pprId :: PrintExplicitForalls -> Var -> SDoc
124 pprId pefas ident
125   = hang (ppr_bndr ident <+> dcolon) 2 
126         (pprType pefas (GHC.idType ident))
127
128 pprType :: PrintExplicitForalls -> GHC.Type -> SDoc
129 pprType True  ty = ppr ty
130 pprType False ty = ppr (GHC.dropForAlls ty)
131
132 pprTyCon pefas tyCon
133   | GHC.isSynTyCon tyCon
134   = if GHC.isOpenTyCon tyCon
135     then pprTyConHdr pefas tyCon <+> dcolon <+> 
136          pprType pefas (GHC.synTyConResKind tyCon)
137     else 
138       let rhs_type = GHC.synTyConType tyCon
139       in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprType pefas rhs_type)
140   | otherwise
141   = pprAlgTyCon pefas tyCon (const True) (const True)
142
143 pprAlgTyCon pefas tyCon ok_con ok_label
144   | gadt      = pprTyConHdr pefas tyCon <+> ptext SLIT("where") $$ 
145                    nest 2 (vcat (ppr_trim show_con datacons))
146   | otherwise = hang (pprTyConHdr pefas tyCon)
147                    2 (add_bars (ppr_trim show_con datacons))
148   where
149     datacons = GHC.tyConDataCons tyCon
150     gadt = any (not . GHC.isVanillaDataCon) datacons
151
152     show_con dataCon
153       | ok_con dataCon = Just (pprDataConDecl pefas gadt ok_label dataCon)
154       | otherwise      = Nothing
155
156 pprDataCon pefas dataCon = pprAlgTyCon pefas tyCon (== dataCon) (const True)
157   where tyCon = GHC.dataConTyCon dataCon
158
159 pprDataConDecl pefas gadt_style show_label dataCon
160   | not gadt_style = ppr_fields tys_w_strs
161   | otherwise      = ppr_bndr dataCon <+> dcolon <+> 
162                         sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
163   where
164     (tyvars, theta, argTypes, res_ty) = GHC.dataConSig dataCon
165     tyCon = GHC.dataConTyCon dataCon
166     labels = GHC.dataConFieldLabels dataCon
167     qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
168     stricts = GHC.dataConStrictMarks dataCon
169     tys_w_strs = zip stricts argTypes
170
171     ppr_tvs 
172         | null qualVars = empty
173         | otherwise     = ptext SLIT("forall") <+> 
174                                 hsep (map ppr qualVars) <> dot
175
176         -- printing out the dataCon as a type signature, in GADT style
177     pp_tau = foldr add (ppr res_ty) tys_w_strs
178     add (str,ty) pp_ty = pprBangTy str ty <+> arrow <+> pp_ty
179
180     pprParendBangTy (strict,ty)
181         | GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty
182         | otherwise                 = GHC.pprParendType ty
183
184     pprBangTy strict ty
185         | GHC.isMarkedStrict strict = char '!' <> ppr ty
186         | otherwise                 = ppr ty
187
188     maybe_show_label (lbl,(strict,tp))
189         | show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
190         | otherwise      = Nothing
191
192     ppr_fields [ty1, ty2]
193         | GHC.dataConIsInfix dataCon && null labels
194         = sep [pprParendBangTy ty1, ppr dataCon, pprParendBangTy ty2]
195     ppr_fields fields
196         | null labels
197         = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
198         | otherwise
199         = ppr_bndr dataCon <+> 
200                 braces (sep (punctuate comma (ppr_trim maybe_show_label 
201                                         (zip labels fields))))
202
203 pprClass pefas cls
204   | null methods = 
205         pprClassHdr pefas cls
206   | otherwise = 
207         hang (pprClassHdr pefas cls <+> ptext SLIT("where"))
208             2 (vcat (map (pprClassMethod pefas) methods))
209   where
210         methods = GHC.classMethods cls
211
212 pprClassOneMethod pefas cls this_one = 
213   hang (pprClassHdr pefas cls <+> ptext SLIT("where"))
214         2 (vcat (ppr_trim show_meth methods))
215   where
216         methods = GHC.classMethods cls
217         show_meth id | id == this_one = Just (pprClassMethod pefas id)
218                      | otherwise      = Nothing
219
220 pprClassMethod pefas id =
221   hang (ppr_bndr id <+> dcolon) 2 (pprType pefas (classOpType id))
222   where
223   -- Here's the magic incantation to strip off the dictionary
224   -- from the class op type.  Stolen from IfaceSyn.tyThingToIfaceDecl.
225   classOpType id = GHC.funResultTy rho_ty
226      where (_sel_tyvars, rho_ty) = GHC.splitForAllTys (GHC.idType id)
227
228 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
229 ppr_trim show xs
230   = snd (foldr go (False, []) xs)
231   where
232     go x (eliding, so_far)
233         | Just doc <- show x = (False, doc : so_far)
234         | otherwise = if eliding then (True, so_far)
235                                  else (True, ptext SLIT("...") : so_far)
236
237 add_bars []      = empty
238 add_bars [c]     = equals <+> c
239 add_bars (c:cs)  = sep ((equals <+> c) : map (char '|' <+>) cs)
240
241 -- Wrap operators in ()
242 ppr_bndr :: GHC.NamedThing a => a -> SDoc
243 ppr_bndr a = GHC.pprParenSymName a
244
245 showWithLoc :: SDoc -> SDoc -> SDoc
246 showWithLoc loc doc 
247     = hang doc 2 (char '\t' <> comment <+> loc)
248                 -- The tab tries to make them line up a bit
249   where
250     comment = ptext SLIT("--")
251