Fix Trac #3966: warn about useless UNPACK pragmas
[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         PrintExplicitForalls,
11         pprTyThing,
12         pprTyThingInContext,
13         pprTyThingLoc,
14         pprTyThingInContextLoc,
15         pprTyThingHdr,
16         pprTypeForUser
17   ) where
18
19 import qualified GHC
20
21 import GHC ( TyThing(..) )
22 import TyCon
23 import TcType
24 import Var
25 import Name
26 import Outputable
27 import FastString
28
29 -- -----------------------------------------------------------------------------
30 -- Pretty-printing entities that we get from the GHC API
31
32 -- This should be a good source of sample code for using the GHC API to
33 -- inspect source code entities.
34
35 type PrintExplicitForalls = Bool
36
37 -- | Pretty-prints a 'TyThing' with its defining location.
38 pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
39 pprTyThingLoc pefas tyThing 
40   = showWithLoc loc (pprTyThing pefas tyThing)
41   where loc = pprNameLoc (GHC.getName tyThing)
42
43 -- | Pretty-prints a 'TyThing'.
44 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
45 pprTyThing pefas (AnId id)          = pprId         pefas id
46 pprTyThing pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
47 pprTyThing pefas (ATyCon tyCon)     = pprTyCon      pefas tyCon
48 pprTyThing pefas (AClass cls)       = pprClass      pefas cls
49
50 -- | Like 'pprTyThingInContext', but adds the defining location.
51 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
52 pprTyThingInContextLoc pefas tyThing
53   = showWithLoc loc (pprTyThingInContext pefas tyThing)
54   where loc = pprNameLoc (GHC.getName tyThing)
55
56 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
57 -- is a data constructor, record selector, or class method, then 
58 -- the entity's parent declaration is pretty-printed with irrelevant
59 -- parts omitted.
60 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
61 pprTyThingInContext pefas (AnId id)          = pprIdInContext pefas id
62 pprTyThingInContext pefas (ADataCon dataCon) = pprDataCon pefas dataCon
63 pprTyThingInContext pefas (ATyCon tyCon)     = pprTyCon   pefas tyCon
64 pprTyThingInContext pefas (AClass cls)       = pprClass   pefas cls
65
66 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
67 -- the function is equivalent to 'pprTyThing' but for type constructors
68 -- and classes it prints only the header part of the declaration.
69 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
70 pprTyThingHdr pefas (AnId id)          = pprId         pefas id
71 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
72 pprTyThingHdr pefas (ATyCon tyCon)     = pprTyConHdr   pefas tyCon
73 pprTyThingHdr pefas (AClass cls)       = pprClassHdr   pefas cls
74
75 pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc
76 pprTyConHdr _ tyCon
77   | Just (_fam_tc, tys) <- tyConFamInst_maybe tyCon
78   = ptext keyword <+> ptext (sLit "instance") <+> pprTypeApp tyCon tys
79   | otherwise
80   = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
81   where
82     vars | GHC.isPrimTyCon tyCon || 
83            GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
84          | otherwise = GHC.tyConTyVars tyCon
85
86     keyword | GHC.isSynTyCon tyCon = sLit "type"
87             | GHC.isNewTyCon tyCon = sLit "newtype"
88             | otherwise            = sLit "data"
89
90     opt_family
91       | GHC.isOpenTyCon tyCon = ptext (sLit "family")
92       | otherwise             = empty
93
94     opt_stupid  -- The "stupid theta" part of the declaration
95         | isAlgTyCon tyCon = GHC.pprThetaArrow (tyConStupidTheta tyCon)
96         | otherwise        = empty      -- Returns 'empty' if null theta
97
98 pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc
99 pprDataConSig pefas dataCon =
100   ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
101
102 pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc
103 pprClassHdr _ 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 :: PrintExplicitForalls -> Var -> SDoc
112 pprIdInContext pefas id
113   | GHC.isRecordSelector id               = pprRecordSelector pefas id
114   | Just cls <- GHC.isClassOpId_maybe id  = pprClassOneMethod pefas cls id
115   | otherwise                             = pprId pefas id
116
117 pprRecordSelector :: PrintExplicitForalls -> Id -> SDoc
118 pprRecordSelector pefas id
119   = pprAlgTyCon pefas tyCon show_con show_label
120   where
121         (tyCon,label) = GHC.recordSelectorFieldLabel id
122         show_con dataCon  = label `elem` GHC.dataConFieldLabels dataCon
123         show_label label' = label == label'
124
125 pprId :: PrintExplicitForalls -> Var -> SDoc
126 pprId pefas ident
127   = hang (ppr_bndr ident <+> dcolon)
128          2 (pprTypeForUser pefas (GHC.idType ident))
129
130 pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc
131 -- We do two things here.
132 -- a) We tidy the type, regardless
133 -- b) If PrintExplicitForAlls is True, we discard the foralls
134 --      but we do so `deeply'
135 -- Prime example: a class op might have type
136 --      forall a. C a => forall b. Ord b => stuff
137 -- Then we want to display
138 --      (C a, Ord b) => stuff
139 pprTypeForUser print_foralls ty 
140   | print_foralls = ppr tidy_ty
141   | otherwise     = ppr (mkPhiTy [p | (_tvs, ps) <- ctxt, p <- ps] ty')
142   where
143     tidy_ty     = tidyTopType ty
144     (ctxt, ty') = tcMultiSplitSigmaTy tidy_ty
145
146 pprTyCon :: PrintExplicitForalls -> TyCon -> SDoc
147 pprTyCon pefas tyCon
148   | GHC.isSynTyCon tyCon
149   = if GHC.isOpenTyCon tyCon
150     then pprTyConHdr pefas tyCon <+> dcolon <+> 
151          pprTypeForUser pefas (GHC.synTyConResKind tyCon)
152     else 
153       let rhs_type = GHC.synTyConType tyCon
154       in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type)
155   | otherwise
156   = pprAlgTyCon pefas tyCon (const True) (const True)
157
158 pprAlgTyCon :: PrintExplicitForalls -> TyCon -> (GHC.DataCon -> Bool)
159             -> (FieldLabel -> Bool) -> SDoc
160 pprAlgTyCon pefas tyCon ok_con ok_label
161   | gadt      = pprTyConHdr pefas tyCon <+> ptext (sLit "where") $$ 
162                    nest 2 (vcat (ppr_trim show_con datacons))
163   | otherwise = hang (pprTyConHdr pefas tyCon)
164                    2 (add_bars (ppr_trim show_con datacons))
165   where
166     datacons = GHC.tyConDataCons tyCon
167     gadt = any (not . GHC.isVanillaDataCon) datacons
168
169     show_con dataCon
170       | ok_con dataCon = Just (pprDataConDecl pefas gadt ok_label dataCon)
171       | otherwise      = Nothing
172
173 pprDataCon :: PrintExplicitForalls -> GHC.DataCon -> SDoc
174 pprDataCon pefas dataCon = pprAlgTyCon pefas tyCon (== dataCon) (const True)
175   where tyCon = GHC.dataConTyCon dataCon
176
177 pprDataConDecl :: PrintExplicitForalls -> Bool -> (FieldLabel -> Bool)
178                -> GHC.DataCon -> SDoc
179 pprDataConDecl _ gadt_style show_label dataCon
180   | not gadt_style = ppr_fields tys_w_strs
181   | otherwise      = ppr_bndr dataCon <+> dcolon <+> 
182                         sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
183   where
184     (tyvars, theta, argTypes, res_ty) = GHC.dataConSig dataCon
185     tyCon = GHC.dataConTyCon dataCon
186     labels = GHC.dataConFieldLabels dataCon
187     qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
188     stricts = GHC.dataConStrictMarks dataCon
189     tys_w_strs = zip stricts argTypes
190
191     ppr_tvs 
192         | null qualVars = empty
193         | otherwise     = ptext (sLit "forall") <+> 
194                                 hsep (map ppr qualVars) <> dot
195
196         -- printing out the dataCon as a type signature, in GADT style
197     pp_tau = foldr add (ppr res_ty) tys_w_strs
198     add str_ty pp_ty = pprParendBangTy str_ty <+> arrow <+> pp_ty
199
200     pprParendBangTy (bang,ty) = ppr bang <> GHC.pprParendType ty
201
202     pprBangTy bang ty = ppr bang <> ppr ty
203
204     maybe_show_label (lbl,(strict,tp))
205         | show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
206         | otherwise      = Nothing
207
208     ppr_fields [ty1, ty2]
209         | GHC.dataConIsInfix dataCon && null labels
210         = sep [pprParendBangTy ty1, pprInfixName dataCon, pprParendBangTy ty2]
211     ppr_fields fields
212         | null labels
213         = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
214         | otherwise
215         = ppr_bndr dataCon <+> 
216                 braces (sep (punctuate comma (ppr_trim maybe_show_label 
217                                         (zip labels fields))))
218
219 pprClass :: PrintExplicitForalls -> GHC.Class -> SDoc
220 pprClass pefas cls
221   | null methods = 
222         pprClassHdr pefas cls
223   | otherwise = 
224         hang (pprClassHdr pefas cls <+> ptext (sLit "where"))
225             2 (vcat (map (pprClassMethod pefas) methods))
226   where
227         methods = GHC.classMethods cls
228
229 pprClassOneMethod :: PrintExplicitForalls -> GHC.Class -> Id -> SDoc
230 pprClassOneMethod pefas cls this_one
231   = hang (pprClassHdr pefas cls <+> ptext (sLit "where"))
232          2 (vcat (ppr_trim show_meth methods))
233   where
234         methods = GHC.classMethods cls
235         show_meth id | id == this_one = Just (pprClassMethod pefas id)
236                      | otherwise      = Nothing
237
238 pprClassMethod :: PrintExplicitForalls -> Id -> SDoc
239 pprClassMethod pefas id
240   = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
241   where
242   -- Here's the magic incantation to strip off the dictionary
243   -- from the class op type.  Stolen from IfaceSyn.tyThingToIfaceDecl.
244   --
245   -- It's important to tidy it *before* splitting it up, so that if 
246   -- we have    class C a b where
247   --              op :: forall a. a -> b
248   -- then the inner forall on op gets renamed to a1, and we print
249   -- (when dropping foralls)
250   --            class C a b where
251   --              op :: a1 -> b
252
253   tidy_sel_ty = tidyTopType (GHC.idType id)
254   (_sel_tyvars, rho_ty) = GHC.splitForAllTys tidy_sel_ty
255   op_ty = GHC.funResultTy rho_ty
256
257 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
258 ppr_trim show xs
259   = snd (foldr go (False, []) xs)
260   where
261     go x (eliding, so_far)
262         | Just doc <- show x = (False, doc : so_far)
263         | otherwise = if eliding then (True, so_far)
264                                  else (True, ptext (sLit "...") : so_far)
265
266 add_bars :: [SDoc] -> SDoc
267 add_bars []      = empty
268 add_bars [c]     = equals <+> c
269 add_bars (c:cs)  = sep ((equals <+> c) : map (char '|' <+>) cs)
270
271 -- Wrap operators in ()
272 ppr_bndr :: GHC.NamedThing a => a -> SDoc
273 ppr_bndr a = GHC.pprParenSymName a
274
275 showWithLoc :: SDoc -> SDoc -> SDoc
276 showWithLoc loc doc 
277     = hang doc 2 (char '\t' <> comment <+> loc)
278                 -- The tab tries to make them line up a bit
279   where
280     comment = ptext (sLit "--")
281