63549841596b2fdb7494fd11a215833fa2580cb7
[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   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 pprDataConSig exts dataCon =
81   ppr_bndr dataCon <+> dcolon <+> pprType exts (GHC.dataConType dataCon)
82
83 pprClassHdr exts cls =
84   let (tyVars, funDeps) = GHC.classTvsFds cls
85   in ptext SLIT("class") <+> 
86      GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
87      ppr_bndr cls <+>
88      hsep (map ppr tyVars) <+>
89      GHC.pprFundeps funDeps
90
91 pprIdInContext exts id
92   | GHC.isRecordSelector id               = pprRecordSelector exts id
93   | Just cls <- GHC.isClassOpId_maybe id  = pprClassOneMethod exts cls id
94   | otherwise                             = pprId exts id
95
96 pprRecordSelector exts id
97   = pprAlgTyCon exts tyCon show_con show_label
98   where
99         (tyCon,label) = GHC.recordSelectorFieldLabel id
100         show_con dataCon  = label `elem` GHC.dataConFieldLabels dataCon
101         show_label label' = label == label'
102
103 pprId exts id
104   = hang (ppr_bndr id <+> dcolon) 2 
105         (pprType exts (GHC.idType id))
106
107 pprType True  ty = ppr ty
108 pprType False ty = ppr (GHC.dropForAlls ty)
109
110 pprTyCon exts tyCon
111   | GHC.isSynTyCon tyCon
112   = let rhs_type = GHC.synTyConRhs tyCon
113     in hang (pprTyConHdr exts tyCon <+> equals) 2 (pprType exts rhs_type)
114   | otherwise
115   = pprAlgTyCon exts tyCon (const True) (const True)
116
117 pprAlgTyCon exts tyCon ok_con ok_label
118   | gadt      = pprTyConHdr exts tyCon <+> ptext SLIT("where") $$ 
119                    nest 2 (vcat (ppr_trim show_con datacons))
120   | otherwise = hang (pprTyConHdr exts tyCon)
121                    2 (add_bars (ppr_trim show_con datacons))
122   where
123     datacons = GHC.tyConDataCons tyCon
124     gadt = any (not . GHC.isVanillaDataCon) datacons
125
126     show_con dataCon
127       | ok_con dataCon = Just (pprDataConDecl exts gadt ok_label dataCon)
128       | otherwise      = Nothing
129
130 pprDataCon exts dataCon = pprAlgTyCon exts tyCon (== dataCon) (const True)
131   where tyCon = GHC.dataConTyCon dataCon
132
133 pprDataConDecl exts gadt_style show_label dataCon
134  = error "kevind stub"
135 {-
136   | not gadt_style = ppr_fields tys_w_strs
137   | otherwise      = ppr_bndr dataCon <+> dcolon <+> 
138                         sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
139   where
140     (tyvars, theta, argTypes, tyCon) = GHC.dataConSig dataCon
141     labels = GHC.dataConFieldLabels dataCon
142     res_tys = dataConResTys dataCon
143     qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
144     stricts = GHC.dataConStrictMarks dataCon
145     tys_w_strs = zip stricts argTypes
146
147     ppr_tvs 
148         | null qualVars = empty
149         | otherwise     = ptext SLIT("forall") <+> 
150                                 hsep (map ppr qualVars) <> dot
151
152         -- printing out the dataCon as a type signature, in GADT style
153     pp_tau = foldr add pp_res_ty tys_w_strs
154     pp_res_ty = ppr_bndr tyCon <+> hsep (map GHC.pprParendType res_tys)
155     add (str,ty) pp_ty = pprBangTy str ty <+> arrow <+> pp_ty
156
157     pprParendBangTy (strict,ty)
158         | GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty
159         | otherwise                 = GHC.pprParendType ty
160
161     pprBangTy strict ty
162         | GHC.isMarkedStrict strict = char '!' <> ppr ty
163         | otherwise                 = ppr ty
164
165     maybe_show_label (lbl,(strict,tp))
166         | show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
167         | otherwise      = Nothing
168
169     ppr_fields [ty1, ty2]
170         | GHC.dataConIsInfix dataCon && null labels
171         = sep [pprParendBangTy ty1, ppr dataCon, pprParendBangTy ty2]
172     ppr_fields fields
173         | null labels
174         = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
175         | otherwise
176         = ppr_bndr dataCon <+> 
177                 braces (sep (punctuate comma (ppr_trim maybe_show_label 
178                                         (zip labels fields))))
179 -}
180 pprClass exts cls
181   | null methods = 
182         pprClassHdr exts cls
183   | otherwise = 
184         hang (pprClassHdr exts cls <+> ptext SLIT("where"))
185             2 (vcat (map (pprClassMethod exts) methods))
186   where
187         methods = GHC.classMethods cls
188
189 pprClassOneMethod exts cls this_one = 
190   hang (pprClassHdr exts cls <+> ptext SLIT("where"))
191         2 (vcat (ppr_trim show_meth methods))
192   where
193         methods = GHC.classMethods cls
194         show_meth id | id == this_one = Just (pprClassMethod exts id)
195                      | otherwise      = Nothing
196
197 pprClassMethod exts id =
198   hang (ppr_bndr id <+> dcolon) 2 (pprType exts (classOpType id))
199   where
200   -- Here's the magic incantation to strip off the dictionary
201   -- from the class op type.  Stolen from IfaceSyn.tyThingToIfaceDecl.
202   classOpType id = GHC.funResultTy rho_ty
203      where (_sel_tyvars, rho_ty) = GHC.splitForAllTys (GHC.idType id)
204
205 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
206 ppr_trim show xs
207   = snd (foldr go (False, []) xs)
208   where
209     go x (eliding, so_far)
210         | Just doc <- show x = (False, doc : so_far)
211         | otherwise = if eliding then (True, so_far)
212                                  else (True, ptext SLIT("...") : so_far)
213
214 add_bars []      = empty
215 add_bars [c]     = equals <+> c
216 add_bars (c:cs)  = sep ((equals <+> c) : map (char '|' <+>) cs)
217
218 -- Wrap operators in ()
219 ppr_bndr :: GHC.NamedThing a => a -> SDoc
220 ppr_bndr a = GHC.pprParenSymName a
221
222 showWithLoc :: SrcLoc -> SDoc -> SDoc
223 showWithLoc loc doc 
224     = hang doc 2 (char '\t' <> comment <+> GHC.pprDefnLoc loc)
225                 -- The tab tries to make them line up a bit
226   where
227     comment = ptext SLIT("--")
228