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