1 -----------------------------------------------------------------------------
3 -- Pretty-printing TyThings
5 -- (c) The GHC Team 2005
7 -----------------------------------------------------------------------------
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
21 pprTyThingInContextLoc,
25 #include "HsVersions.h"
29 import TyCon ( tyConFamInst_maybe )
30 import Type ( pprTypeApp )
31 import GHC ( TyThing(..), SrcSpan )
35 -- -----------------------------------------------------------------------------
36 -- Pretty-printing entities that we get from the GHC API
38 -- This should be a good source of sample code for using the GHC API to
39 -- inspect source code entities.
41 type PrintExplicitForalls = Bool
43 -- | Pretty-prints a 'TyThing' with its defining location.
44 pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
45 pprTyThingLoc pefas tyThing
46 = showWithLoc loc (pprTyThing pefas tyThing)
47 where loc = GHC.nameSrcSpan (GHC.getName tyThing)
49 -- | Pretty-prints a 'TyThing'.
50 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
51 pprTyThing pefas (AnId id) = pprId pefas id
52 pprTyThing pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
53 pprTyThing pefas (ATyCon tyCon) = pprTyCon pefas tyCon
54 pprTyThing pefas (AClass cls) = pprClass pefas cls
56 -- | Like 'pprTyThingInContext', but adds the defining location.
57 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
58 pprTyThingInContextLoc pefas tyThing
59 = showWithLoc loc (pprTyThingInContext pefas tyThing)
60 where loc = GHC.nameSrcSpan (GHC.getName tyThing)
62 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
63 -- is a data constructor, record selector, or class method, then
64 -- the entity's parent declaration is pretty-printed with irrelevant
66 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
67 pprTyThingInContext pefas (AnId id) = pprIdInContext pefas id
68 pprTyThingInContext pefas (ADataCon dataCon) = pprDataCon pefas dataCon
69 pprTyThingInContext pefas (ATyCon tyCon) = pprTyCon pefas tyCon
70 pprTyThingInContext pefas (AClass cls) = pprClass pefas cls
72 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
73 -- the function is equivalent to 'pprTyThing' but for type constructors
74 -- and classes it prints only the header part of the declaration.
75 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
76 pprTyThingHdr pefas (AnId id) = pprId pefas id
77 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
78 pprTyThingHdr pefas (ATyCon tyCon) = pprTyConHdr pefas tyCon
79 pprTyThingHdr pefas (AClass cls) = pprClassHdr pefas cls
81 pprTyConHdr pefas tyCon
82 | Just (fam_tc, tys) <- tyConFamInst_maybe tyCon
83 = ptext keyword <+> ptext SLIT("instance") <+> pprTypeApp tyCon (ppr_bndr tyCon) tys
85 = ptext keyword <+> opt_family <+> ppr_bndr tyCon <+> hsep (map ppr vars)
87 vars | GHC.isPrimTyCon tyCon ||
88 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
89 | otherwise = GHC.tyConTyVars tyCon
91 keyword | GHC.isSynTyCon tyCon = SLIT("type")
92 | GHC.isNewTyCon tyCon = SLIT("newtype")
93 | otherwise = SLIT("data")
96 | GHC.isOpenTyCon tyCon = ptext SLIT("family")
99 pprDataConSig pefas dataCon =
100 ppr_bndr dataCon <+> dcolon <+> pprType pefas (GHC.dataConType dataCon)
102 pprClassHdr pefas cls =
103 let (tyVars, funDeps) = GHC.classTvsFds cls
104 in ptext SLIT("class") <+>
105 GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
107 hsep (map ppr tyVars) <+>
108 GHC.pprFundeps funDeps
110 pprIdInContext pefas id
111 | GHC.isRecordSelector id = pprRecordSelector pefas id
112 | Just cls <- GHC.isClassOpId_maybe id = pprClassOneMethod pefas cls id
113 | otherwise = pprId pefas id
115 pprRecordSelector pefas id
116 = pprAlgTyCon pefas tyCon show_con show_label
118 (tyCon,label) = GHC.recordSelectorFieldLabel id
119 show_con dataCon = label `elem` GHC.dataConFieldLabels dataCon
120 show_label label' = label == label'
122 pprId :: PrintExplicitForalls -> Var -> SDoc
124 = hang (ppr_bndr ident <+> dcolon) 2
125 (pprType pefas (GHC.idType ident))
127 pprType :: PrintExplicitForalls -> GHC.Type -> SDoc
128 pprType True ty = ppr ty
129 pprType False ty = ppr (GHC.dropForAlls ty)
132 | GHC.isSynTyCon tyCon
133 = if GHC.isOpenTyCon tyCon
134 then pprTyConHdr pefas tyCon <+> dcolon <+>
135 pprType pefas (GHC.synTyConResKind tyCon)
137 let rhs_type = GHC.synTyConType tyCon
138 in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprType pefas rhs_type)
140 = pprAlgTyCon pefas tyCon (const True) (const True)
142 pprAlgTyCon pefas tyCon ok_con ok_label
143 | gadt = pprTyConHdr pefas tyCon <+> ptext SLIT("where") $$
144 nest 2 (vcat (ppr_trim show_con datacons))
145 | otherwise = hang (pprTyConHdr pefas tyCon)
146 2 (add_bars (ppr_trim show_con datacons))
148 datacons = GHC.tyConDataCons tyCon
149 gadt = any (not . GHC.isVanillaDataCon) datacons
152 | ok_con dataCon = Just (pprDataConDecl pefas gadt ok_label dataCon)
153 | otherwise = Nothing
155 pprDataCon pefas dataCon = pprAlgTyCon pefas tyCon (== dataCon) (const True)
156 where tyCon = GHC.dataConTyCon dataCon
158 pprDataConDecl pefas gadt_style show_label dataCon
159 | not gadt_style = ppr_fields tys_w_strs
160 | otherwise = ppr_bndr dataCon <+> dcolon <+>
161 sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
163 (tyvars, theta, argTypes, res_ty) = GHC.dataConSig dataCon
164 tyCon = GHC.dataConTyCon dataCon
165 labels = GHC.dataConFieldLabels dataCon
166 qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
167 stricts = GHC.dataConStrictMarks dataCon
168 tys_w_strs = zip stricts argTypes
171 | null qualVars = empty
172 | otherwise = ptext SLIT("forall") <+>
173 hsep (map ppr qualVars) <> dot
175 -- printing out the dataCon as a type signature, in GADT style
176 pp_tau = foldr add (ppr res_ty) tys_w_strs
177 add (str,ty) pp_ty = pprBangTy str ty <+> arrow <+> pp_ty
179 pprParendBangTy (strict,ty)
180 | GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty
181 | otherwise = GHC.pprParendType ty
184 | GHC.isMarkedStrict strict = char '!' <> ppr ty
187 maybe_show_label (lbl,(strict,tp))
188 | show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
189 | otherwise = Nothing
191 ppr_fields [ty1, ty2]
192 | GHC.dataConIsInfix dataCon && null labels
193 = sep [pprParendBangTy ty1, ppr dataCon, pprParendBangTy ty2]
196 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
198 = ppr_bndr dataCon <+>
199 braces (sep (punctuate comma (ppr_trim maybe_show_label
200 (zip labels fields))))
204 pprClassHdr pefas cls
206 hang (pprClassHdr pefas cls <+> ptext SLIT("where"))
207 2 (vcat (map (pprClassMethod pefas) methods))
209 methods = GHC.classMethods cls
211 pprClassOneMethod pefas cls this_one =
212 hang (pprClassHdr pefas cls <+> ptext SLIT("where"))
213 2 (vcat (ppr_trim show_meth methods))
215 methods = GHC.classMethods cls
216 show_meth id | id == this_one = Just (pprClassMethod pefas id)
217 | otherwise = Nothing
219 pprClassMethod pefas id =
220 hang (ppr_bndr id <+> dcolon) 2 (pprType pefas (classOpType id))
222 -- Here's the magic incantation to strip off the dictionary
223 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
224 classOpType id = GHC.funResultTy rho_ty
225 where (_sel_tyvars, rho_ty) = GHC.splitForAllTys (GHC.idType id)
227 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
229 = snd (foldr go (False, []) xs)
231 go x (eliding, so_far)
232 | Just doc <- show x = (False, doc : so_far)
233 | otherwise = if eliding then (True, so_far)
234 else (True, ptext SLIT("...") : so_far)
237 add_bars [c] = equals <+> c
238 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
240 -- Wrap operators in ()
241 ppr_bndr :: GHC.NamedThing a => a -> SDoc
242 ppr_bndr a = GHC.pprParenSymName a
244 showWithLoc :: SrcSpan -> SDoc -> SDoc
246 = hang doc 2 (char '\t' <> comment <+> GHC.pprDefnLoc loc)
247 -- The tab tries to make them line up a bit
249 comment = ptext SLIT("--")