1 -----------------------------------------------------------------------------
3 -- Pretty-printing TyThings
5 -- (c) The GHC Team 2005
7 -----------------------------------------------------------------------------
13 pprTyThingInContextLoc,
17 #include "HsVersions.h"
21 import GHC ( TyThing(..), SrcLoc )
22 import DataCon ( dataConResTys )
25 -- -----------------------------------------------------------------------------
26 -- Pretty-printing entities that we get from the GHC API
28 -- This should be a good source of sample code for using the GHC API to
29 -- inspect source code entities.
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)
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
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)
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
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
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
69 pprTyConHdr exts tyCon =
70 addFamily (ptext keyword) <+> ppr_bndr tyCon <+> hsep (map ppr vars)
72 vars | GHC.isPrimTyCon tyCon ||
73 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
74 | otherwise = GHC.tyConTyVars tyCon
76 keyword | GHC.isSynTyCon tyCon = SLIT("type")
77 | GHC.isNewTyCon tyCon = SLIT("newtype")
78 | otherwise = SLIT("data")
81 | GHC.isOpenTyCon tyCon = keytext <> ptext SLIT(" family")
84 pprDataConSig exts dataCon =
85 ppr_bndr dataCon <+> dcolon <+> pprType exts (GHC.dataConType dataCon)
87 pprClassHdr exts cls =
88 let (tyVars, funDeps) = GHC.classTvsFds cls
89 in ptext SLIT("class") <+>
90 GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
92 hsep (map ppr tyVars) <+>
93 GHC.pprFundeps funDeps
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
100 pprRecordSelector exts id
101 = pprAlgTyCon exts tyCon show_con show_label
103 (tyCon,label) = GHC.recordSelectorFieldLabel id
104 show_con dataCon = label `elem` GHC.dataConFieldLabels dataCon
105 show_label label' = label == label'
108 = hang (ppr_bndr id <+> dcolon) 2
109 (pprType exts (GHC.idType id))
111 pprType True ty = ppr ty
112 pprType False ty = ppr (GHC.dropForAlls ty)
115 | GHC.isSynTyCon tyCon
116 = if GHC.isOpenTyCon tyCon
117 then pprTyConHdr exts tyCon <+> dcolon <+>
118 pprType exts (GHC.synTyConResKind tyCon)
120 let rhs_type = GHC.synTyConType tyCon
121 in hang (pprTyConHdr exts tyCon <+> equals) 2 (pprType exts rhs_type)
123 = pprAlgTyCon exts tyCon (const True) (const True)
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))
131 datacons = GHC.tyConDataCons tyCon
132 gadt = any (not . GHC.isVanillaDataCon) datacons
135 | ok_con dataCon = Just (pprDataConDecl exts gadt ok_label dataCon)
136 | otherwise = Nothing
138 pprDataCon exts dataCon = pprAlgTyCon exts tyCon (== dataCon) (const True)
139 where tyCon = GHC.dataConTyCon dataCon
141 pprDataConDecl exts gadt_style show_label dataCon
142 | not gadt_style = ppr_fields tys_w_strs
143 | otherwise = ppr_bndr dataCon <+> dcolon <+>
144 sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
146 (tyvars, theta, argTypes) = GHC.dataConSig dataCon
147 tyCon = GHC.dataConTyCon dataCon
148 labels = GHC.dataConFieldLabels dataCon
149 res_tys = dataConResTys dataCon
150 qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
151 stricts = GHC.dataConStrictMarks dataCon
152 tys_w_strs = zip stricts argTypes
155 | null qualVars = empty
156 | otherwise = ptext SLIT("forall") <+>
157 hsep (map ppr qualVars) <> dot
159 -- printing out the dataCon as a type signature, in GADT style
160 pp_tau = foldr add pp_res_ty tys_w_strs
161 pp_res_ty = GHC.pprTypeApp (ppr_bndr tyCon) res_tys
162 add (str,ty) pp_ty = pprBangTy str ty <+> arrow <+> pp_ty
164 pprParendBangTy (strict,ty)
165 | GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty
166 | otherwise = GHC.pprParendType ty
169 | GHC.isMarkedStrict strict = char '!' <> ppr ty
172 maybe_show_label (lbl,(strict,tp))
173 | show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
174 | otherwise = Nothing
176 ppr_fields [ty1, ty2]
177 | GHC.dataConIsInfix dataCon && null labels
178 = sep [pprParendBangTy ty1, ppr dataCon, pprParendBangTy ty2]
181 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
183 = ppr_bndr dataCon <+>
184 braces (sep (punctuate comma (ppr_trim maybe_show_label
185 (zip labels fields))))
191 hang (pprClassHdr exts cls <+> ptext SLIT("where"))
192 2 (vcat (map (pprClassMethod exts) methods))
194 methods = GHC.classMethods cls
196 pprClassOneMethod exts cls this_one =
197 hang (pprClassHdr exts cls <+> ptext SLIT("where"))
198 2 (vcat (ppr_trim show_meth methods))
200 methods = GHC.classMethods cls
201 show_meth id | id == this_one = Just (pprClassMethod exts id)
202 | otherwise = Nothing
204 pprClassMethod exts id =
205 hang (ppr_bndr id <+> dcolon) 2 (pprType exts (classOpType id))
207 -- Here's the magic incantation to strip off the dictionary
208 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
209 classOpType id = GHC.funResultTy rho_ty
210 where (_sel_tyvars, rho_ty) = GHC.splitForAllTys (GHC.idType id)
212 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
214 = snd (foldr go (False, []) xs)
216 go x (eliding, so_far)
217 | Just doc <- show x = (False, doc : so_far)
218 | otherwise = if eliding then (True, so_far)
219 else (True, ptext SLIT("...") : so_far)
222 add_bars [c] = equals <+> c
223 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
225 -- Wrap operators in ()
226 ppr_bndr :: GHC.NamedThing a => a -> SDoc
227 ppr_bndr a = GHC.pprParenSymName a
229 showWithLoc :: SrcLoc -> SDoc -> SDoc
231 = hang doc 2 (char '\t' <> comment <+> GHC.pprDefnLoc loc)
232 -- The tab tries to make them line up a bit
234 comment = ptext SLIT("--")