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