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