Fix scoped type variables for expression type signatures
[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  = error "kevind stub"
143 {-
144   | not gadt_style = ppr_fields tys_w_strs
145   | otherwise      = ppr_bndr dataCon <+> dcolon <+> 
146                         sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
147   where
148     (tyvars, theta, argTypes, tyCon) = GHC.dataConSig dataCon
149     labels = GHC.dataConFieldLabels dataCon
150     res_tys = dataConResTys dataCon
151     qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
152     stricts = GHC.dataConStrictMarks dataCon
153     tys_w_strs = zip stricts argTypes
154
155     ppr_tvs 
156         | null qualVars = empty
157         | otherwise     = ptext SLIT("forall") <+> 
158                                 hsep (map ppr qualVars) <> dot
159
160         -- printing out the dataCon as a type signature, in GADT style
161     pp_tau = foldr add pp_res_ty tys_w_strs
162     pp_res_ty = ppr_bndr tyCon <+> hsep (map GHC.pprParendType res_tys)
163     add (str,ty) pp_ty = pprBangTy str ty <+> arrow <+> pp_ty
164
165     pprParendBangTy (strict,ty)
166         | GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty
167         | otherwise                 = GHC.pprParendType ty
168
169     pprBangTy strict ty
170         | GHC.isMarkedStrict strict = char '!' <> ppr ty
171         | otherwise                 = ppr ty
172
173     maybe_show_label (lbl,(strict,tp))
174         | show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
175         | otherwise      = Nothing
176
177     ppr_fields [ty1, ty2]
178         | GHC.dataConIsInfix dataCon && null labels
179         = sep [pprParendBangTy ty1, ppr dataCon, pprParendBangTy ty2]
180     ppr_fields fields
181         | null labels
182         = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
183         | otherwise
184         = ppr_bndr dataCon <+> 
185                 braces (sep (punctuate comma (ppr_trim maybe_show_label 
186                                         (zip labels fields))))
187 -}
188 pprClass exts cls
189   | null methods = 
190         pprClassHdr exts cls
191   | otherwise = 
192         hang (pprClassHdr exts cls <+> ptext SLIT("where"))
193             2 (vcat (map (pprClassMethod exts) methods))
194   where
195         methods = GHC.classMethods cls
196
197 pprClassOneMethod exts cls this_one = 
198   hang (pprClassHdr exts cls <+> ptext SLIT("where"))
199         2 (vcat (ppr_trim show_meth methods))
200   where
201         methods = GHC.classMethods cls
202         show_meth id | id == this_one = Just (pprClassMethod exts id)
203                      | otherwise      = Nothing
204
205 pprClassMethod exts id =
206   hang (ppr_bndr id <+> dcolon) 2 (pprType exts (classOpType id))
207   where
208   -- Here's the magic incantation to strip off the dictionary
209   -- from the class op type.  Stolen from IfaceSyn.tyThingToIfaceDecl.
210   classOpType id = GHC.funResultTy rho_ty
211      where (_sel_tyvars, rho_ty) = GHC.splitForAllTys (GHC.idType id)
212
213 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
214 ppr_trim show xs
215   = snd (foldr go (False, []) xs)
216   where
217     go x (eliding, so_far)
218         | Just doc <- show x = (False, doc : so_far)
219         | otherwise = if eliding then (True, so_far)
220                                  else (True, ptext SLIT("...") : so_far)
221
222 add_bars []      = empty
223 add_bars [c]     = equals <+> c
224 add_bars (c:cs)  = sep ((equals <+> c) : map (char '|' <+>) cs)
225
226 -- Wrap operators in ()
227 ppr_bndr :: GHC.NamedThing a => a -> SDoc
228 ppr_bndr a = GHC.pprParenSymName a
229
230 showWithLoc :: SrcLoc -> SDoc -> SDoc
231 showWithLoc loc doc 
232     = hang doc 2 (char '\t' <> comment <+> GHC.pprDefnLoc loc)
233                 -- The tab tries to make them line up a bit
234   where
235     comment = ptext SLIT("--")
236