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 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")
80 pprDataConSig exts dataCon =
81 ppr_bndr dataCon <+> dcolon <+> pprType exts (GHC.dataConType dataCon)
83 pprClassHdr exts cls =
84 let (tyVars, funDeps) = GHC.classTvsFds cls
85 in ptext SLIT("class") <+>
86 GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
88 hsep (map ppr tyVars) <+>
89 GHC.pprFundeps funDeps
91 pprIdInContext exts id
92 | GHC.isRecordSelector id = pprRecordSelector exts id
93 | Just cls <- GHC.isClassOpId_maybe id = pprClassOneMethod exts cls id
94 | otherwise = pprId exts id
96 pprRecordSelector exts id
97 = pprAlgTyCon exts tyCon show_con show_label
99 (tyCon,label) = GHC.recordSelectorFieldLabel id
100 show_con dataCon = label `elem` GHC.dataConFieldLabels dataCon
101 show_label label' = label == label'
104 = hang (ppr_bndr id <+> dcolon) 2
105 (pprType exts (GHC.idType id))
107 pprType True ty = ppr ty
108 pprType False ty = ppr (GHC.dropForAlls ty)
111 | GHC.isSynTyCon tyCon
112 = let rhs_type = GHC.synTyConRhs tyCon
113 in hang (pprTyConHdr exts tyCon <+> equals) 2 (pprType exts rhs_type)
115 = pprAlgTyCon exts tyCon (const True) (const True)
117 pprAlgTyCon exts tyCon ok_con ok_label
118 | gadt = pprTyConHdr exts tyCon <+> ptext SLIT("where") $$
119 nest 2 (vcat (ppr_trim show_con datacons))
120 | otherwise = hang (pprTyConHdr exts tyCon)
121 2 (add_bars (ppr_trim show_con datacons))
123 datacons = GHC.tyConDataCons tyCon
124 gadt = any (not . GHC.isVanillaDataCon) datacons
127 | ok_con dataCon = Just (pprDataConDecl exts gadt ok_label dataCon)
128 | otherwise = Nothing
130 pprDataCon exts dataCon = pprAlgTyCon exts tyCon (== dataCon) (const True)
131 where tyCon = GHC.dataConTyCon dataCon
133 pprDataConDecl exts gadt_style show_label dataCon
134 = error "kevind stub"
136 | not gadt_style = ppr_fields tys_w_strs
137 | otherwise = ppr_bndr dataCon <+> dcolon <+>
138 sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
140 (tyvars, theta, argTypes, tyCon) = GHC.dataConSig dataCon
141 labels = GHC.dataConFieldLabels dataCon
142 res_tys = dataConResTys dataCon
143 qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
144 stricts = GHC.dataConStrictMarks dataCon
145 tys_w_strs = zip stricts argTypes
148 | null qualVars = empty
149 | otherwise = ptext SLIT("forall") <+>
150 hsep (map ppr qualVars) <> dot
152 -- printing out the dataCon as a type signature, in GADT style
153 pp_tau = foldr add pp_res_ty tys_w_strs
154 pp_res_ty = ppr_bndr tyCon <+> hsep (map GHC.pprParendType res_tys)
155 add (str,ty) pp_ty = pprBangTy str ty <+> arrow <+> pp_ty
157 pprParendBangTy (strict,ty)
158 | GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty
159 | otherwise = GHC.pprParendType ty
162 | GHC.isMarkedStrict strict = char '!' <> ppr ty
165 maybe_show_label (lbl,(strict,tp))
166 | show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
167 | otherwise = Nothing
169 ppr_fields [ty1, ty2]
170 | GHC.dataConIsInfix dataCon && null labels
171 = sep [pprParendBangTy ty1, ppr dataCon, pprParendBangTy ty2]
174 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
176 = ppr_bndr dataCon <+>
177 braces (sep (punctuate comma (ppr_trim maybe_show_label
178 (zip labels fields))))
184 hang (pprClassHdr exts cls <+> ptext SLIT("where"))
185 2 (vcat (map (pprClassMethod exts) methods))
187 methods = GHC.classMethods cls
189 pprClassOneMethod exts cls this_one =
190 hang (pprClassHdr exts cls <+> ptext SLIT("where"))
191 2 (vcat (ppr_trim show_meth methods))
193 methods = GHC.classMethods cls
194 show_meth id | id == this_one = Just (pprClassMethod exts id)
195 | otherwise = Nothing
197 pprClassMethod exts id =
198 hang (ppr_bndr id <+> dcolon) 2 (pprType exts (classOpType id))
200 -- Here's the magic incantation to strip off the dictionary
201 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
202 classOpType id = GHC.funResultTy rho_ty
203 where (_sel_tyvars, rho_ty) = GHC.splitForAllTys (GHC.idType id)
205 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
207 = snd (foldr go (False, []) xs)
209 go x (eliding, so_far)
210 | Just doc <- show x = (False, doc : so_far)
211 | otherwise = if eliding then (True, so_far)
212 else (True, ptext SLIT("...") : so_far)
215 add_bars [c] = equals <+> c
216 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
218 -- Wrap operators in ()
219 ppr_bndr :: GHC.NamedThing a => a -> SDoc
220 ppr_bndr a = GHC.pprParenSymName a
222 showWithLoc :: SrcLoc -> SDoc -> SDoc
224 = hang doc 2 (char '\t' <> comment <+> GHC.pprDefnLoc loc)
225 -- The tab tries to make them line up a bit
227 comment = ptext SLIT("--")