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 )
36 -- -----------------------------------------------------------------------------
37 -- Pretty-printing entities that we get from the GHC API
39 -- This should be a good source of sample code for using the GHC API to
40 -- inspect source code entities.
42 type PrintExplicitForalls = Bool
44 -- | Pretty-prints a 'TyThing' with its defining location.
45 pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
46 pprTyThingLoc pefas tyThing
47 = showWithLoc loc (pprTyThing pefas tyThing)
48 where loc = pprNameLoc (GHC.getName tyThing)
50 -- | Pretty-prints a 'TyThing'.
51 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
52 pprTyThing pefas (AnId id) = pprId pefas id
53 pprTyThing pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
54 pprTyThing pefas (ATyCon tyCon) = pprTyCon pefas tyCon
55 pprTyThing pefas (AClass cls) = pprClass pefas cls
57 -- | Like 'pprTyThingInContext', but adds the defining location.
58 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
59 pprTyThingInContextLoc pefas tyThing
60 = showWithLoc loc (pprTyThingInContext pefas tyThing)
61 where loc = pprNameLoc (GHC.getName tyThing)
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
67 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
68 pprTyThingInContext pefas (AnId id) = pprIdInContext pefas id
69 pprTyThingInContext pefas (ADataCon dataCon) = pprDataCon pefas dataCon
70 pprTyThingInContext pefas (ATyCon tyCon) = pprTyCon pefas tyCon
71 pprTyThingInContext pefas (AClass cls) = pprClass pefas cls
73 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
74 -- the function is equivalent to 'pprTyThing' but for type constructors
75 -- and classes it prints only the header part of the declaration.
76 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
77 pprTyThingHdr pefas (AnId id) = pprId pefas id
78 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
79 pprTyThingHdr pefas (ATyCon tyCon) = pprTyConHdr pefas tyCon
80 pprTyThingHdr pefas (AClass cls) = pprClassHdr pefas cls
82 pprTyConHdr pefas tyCon
83 | Just (fam_tc, tys) <- tyConFamInst_maybe tyCon
84 = ptext keyword <+> ptext SLIT("instance") <+> pprTypeApp tyCon (ppr_bndr tyCon) tys
86 = ptext keyword <+> opt_family <+> ppr_bndr tyCon <+> hsep (map ppr vars)
88 vars | GHC.isPrimTyCon tyCon ||
89 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
90 | otherwise = GHC.tyConTyVars tyCon
92 keyword | GHC.isSynTyCon tyCon = SLIT("type")
93 | GHC.isNewTyCon tyCon = SLIT("newtype")
94 | otherwise = SLIT("data")
97 | GHC.isOpenTyCon tyCon = ptext SLIT("family")
100 pprDataConSig pefas dataCon =
101 ppr_bndr dataCon <+> dcolon <+> pprType pefas (GHC.dataConType dataCon)
103 pprClassHdr pefas cls =
104 let (tyVars, funDeps) = GHC.classTvsFds cls
105 in ptext SLIT("class") <+>
106 GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
108 hsep (map ppr tyVars) <+>
109 GHC.pprFundeps funDeps
111 pprIdInContext pefas id
112 | GHC.isRecordSelector id = pprRecordSelector pefas id
113 | Just cls <- GHC.isClassOpId_maybe id = pprClassOneMethod pefas cls id
114 | otherwise = pprId pefas id
116 pprRecordSelector pefas id
117 = pprAlgTyCon pefas tyCon show_con show_label
119 (tyCon,label) = GHC.recordSelectorFieldLabel id
120 show_con dataCon = label `elem` GHC.dataConFieldLabels dataCon
121 show_label label' = label == label'
123 pprId :: PrintExplicitForalls -> Var -> SDoc
125 = hang (ppr_bndr ident <+> dcolon) 2
126 (pprType pefas (GHC.idType ident))
128 pprType :: PrintExplicitForalls -> GHC.Type -> SDoc
129 pprType True ty = ppr ty
130 pprType False ty = ppr (GHC.dropForAlls ty)
133 | GHC.isSynTyCon tyCon
134 = if GHC.isOpenTyCon tyCon
135 then pprTyConHdr pefas tyCon <+> dcolon <+>
136 pprType pefas (GHC.synTyConResKind tyCon)
138 let rhs_type = GHC.synTyConType tyCon
139 in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprType pefas rhs_type)
141 = pprAlgTyCon pefas tyCon (const True) (const True)
143 pprAlgTyCon pefas tyCon ok_con ok_label
144 | gadt = pprTyConHdr pefas tyCon <+> ptext SLIT("where") $$
145 nest 2 (vcat (ppr_trim show_con datacons))
146 | otherwise = hang (pprTyConHdr pefas tyCon)
147 2 (add_bars (ppr_trim show_con datacons))
149 datacons = GHC.tyConDataCons tyCon
150 gadt = any (not . GHC.isVanillaDataCon) datacons
153 | ok_con dataCon = Just (pprDataConDecl pefas gadt ok_label dataCon)
154 | otherwise = Nothing
156 pprDataCon pefas dataCon = pprAlgTyCon pefas tyCon (== dataCon) (const True)
157 where tyCon = GHC.dataConTyCon dataCon
159 pprDataConDecl pefas gadt_style show_label dataCon
160 | not gadt_style = ppr_fields tys_w_strs
161 | otherwise = ppr_bndr dataCon <+> dcolon <+>
162 sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
164 (tyvars, theta, argTypes, res_ty) = GHC.dataConSig dataCon
165 tyCon = GHC.dataConTyCon dataCon
166 labels = GHC.dataConFieldLabels dataCon
167 qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
168 stricts = GHC.dataConStrictMarks dataCon
169 tys_w_strs = zip stricts argTypes
172 | null qualVars = empty
173 | otherwise = ptext SLIT("forall") <+>
174 hsep (map ppr qualVars) <> dot
176 -- printing out the dataCon as a type signature, in GADT style
177 pp_tau = foldr add (ppr res_ty) tys_w_strs
178 add (str,ty) pp_ty = pprBangTy str ty <+> arrow <+> pp_ty
180 pprParendBangTy (strict,ty)
181 | GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty
182 | otherwise = GHC.pprParendType ty
185 | GHC.isMarkedStrict strict = char '!' <> ppr ty
188 maybe_show_label (lbl,(strict,tp))
189 | show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
190 | otherwise = Nothing
192 ppr_fields [ty1, ty2]
193 | GHC.dataConIsInfix dataCon && null labels
194 = sep [pprParendBangTy ty1, ppr dataCon, pprParendBangTy ty2]
197 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
199 = ppr_bndr dataCon <+>
200 braces (sep (punctuate comma (ppr_trim maybe_show_label
201 (zip labels fields))))
205 pprClassHdr pefas cls
207 hang (pprClassHdr pefas cls <+> ptext SLIT("where"))
208 2 (vcat (map (pprClassMethod pefas) methods))
210 methods = GHC.classMethods cls
212 pprClassOneMethod pefas cls this_one =
213 hang (pprClassHdr pefas cls <+> ptext SLIT("where"))
214 2 (vcat (ppr_trim show_meth methods))
216 methods = GHC.classMethods cls
217 show_meth id | id == this_one = Just (pprClassMethod pefas id)
218 | otherwise = Nothing
220 pprClassMethod pefas id =
221 hang (ppr_bndr id <+> dcolon) 2 (pprType pefas (classOpType id))
223 -- Here's the magic incantation to strip off the dictionary
224 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
225 classOpType id = GHC.funResultTy rho_ty
226 where (_sel_tyvars, rho_ty) = GHC.splitForAllTys (GHC.idType id)
228 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
230 = snd (foldr go (False, []) xs)
232 go x (eliding, so_far)
233 | Just doc <- show x = (False, doc : so_far)
234 | otherwise = if eliding then (True, so_far)
235 else (True, ptext SLIT("...") : so_far)
238 add_bars [c] = equals <+> c
239 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
241 -- Wrap operators in ()
242 ppr_bndr :: GHC.NamedThing a => a -> SDoc
243 ppr_bndr a = GHC.pprParenSymName a
245 showWithLoc :: SDoc -> SDoc -> SDoc
247 = hang doc 2 (char '\t' <> comment <+> loc)
248 -- The tab tries to make them line up a bit
250 comment = ptext SLIT("--")