1 -----------------------------------------------------------------------------
3 -- Pretty-printing TyThings
5 -- (c) The GHC Team 2005
7 -----------------------------------------------------------------------------
13 pprTyThingInContextLoc,
17 #include "HsVersions.h"
20 import GHC ( TyThing(..), SrcLoc )
23 -- -----------------------------------------------------------------------------
24 -- Pretty-printing entities that we get from the GHC API
26 -- This should be a good source of sample code for using the GHC API to
27 -- inspect source code entities.
29 -- | Pretty-prints a 'TyThing' with its defining location.
30 pprTyThingLoc :: Bool -> TyThing -> SDoc
31 pprTyThingLoc exts tyThing
32 = showWithLoc loc (pprTyThing exts tyThing)
33 where loc = GHC.nameSrcLoc (GHC.getName tyThing)
35 -- | Pretty-prints a 'TyThing'.
36 pprTyThing :: Bool -> TyThing -> SDoc
37 pprTyThing exts (AnId id) = pprId exts id
38 pprTyThing exts (ADataCon dataCon) = pprDataConSig exts dataCon
39 pprTyThing exts (ATyCon tyCon) = pprTyCon exts tyCon
40 pprTyThing exts (AClass cls) = pprClass exts cls
42 -- | Like 'pprTyThingInContext', but adds the defining location.
43 pprTyThingInContextLoc :: Bool -> TyThing -> SDoc
44 pprTyThingInContextLoc exts tyThing
45 = showWithLoc loc (pprTyThingInContext exts tyThing)
46 where loc = GHC.nameSrcLoc (GHC.getName tyThing)
48 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
49 -- is a data constructor, record selector, or class method, then
50 -- the entity's parent declaration is pretty-printed with irrelevant
52 pprTyThingInContext :: Bool -> TyThing -> SDoc
53 pprTyThingInContext exts (AnId id) = pprIdInContext exts id
54 pprTyThingInContext exts (ADataCon dataCon) = pprDataCon exts dataCon
55 pprTyThingInContext exts (ATyCon tyCon) = pprTyCon exts tyCon
56 pprTyThingInContext exts (AClass cls) = pprClass exts cls
58 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
59 -- the function is equivalent to 'pprTyThing' but for type constructors
60 -- and classes it prints only the header part of the declaration.
61 pprTyThingHdr :: Bool -> TyThing -> SDoc
62 pprTyThingHdr exts (AnId id) = pprId exts id
63 pprTyThingHdr exts (ADataCon dataCon) = pprDataConSig exts dataCon
64 pprTyThingHdr exts (ATyCon tyCon) = pprTyConHdr exts tyCon
65 pprTyThingHdr exts (AClass cls) = pprClassHdr exts cls
67 pprTyConHdr exts tyCon =
68 ptext keyword <+> ppr_bndr tyCon <+> hsep (map ppr vars)
70 vars | GHC.isPrimTyCon tyCon ||
71 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
72 | otherwise = GHC.tyConTyVars tyCon
74 keyword | GHC.isSynTyCon tyCon = SLIT("type")
75 | GHC.isNewTyCon tyCon = SLIT("newtype")
76 | otherwise = SLIT("data")
78 pprDataConSig exts dataCon =
79 ppr_bndr dataCon <+> dcolon <+> pprType exts (GHC.dataConType dataCon)
81 pprClassHdr exts cls =
82 let (tyVars, funDeps) = GHC.classTvsFds cls
83 in ptext SLIT("class") <+>
84 GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
86 hsep (map ppr tyVars) <+>
87 GHC.pprFundeps funDeps
89 pprIdInContext exts id
90 | GHC.isRecordSelector id = pprRecordSelector exts id
91 | Just cls <- GHC.isClassOpId_maybe id = pprClassOneMethod exts cls id
92 | otherwise = pprId exts id
94 pprRecordSelector exts id
95 = pprAlgTyCon exts tyCon show_con show_label
97 (tyCon,label) = GHC.recordSelectorFieldLabel id
98 show_con dataCon = label `elem` GHC.dataConFieldLabels dataCon
99 show_label label' = label == label'
102 = hang (ppr_bndr id <+> dcolon) 2
103 (pprType exts (GHC.idType id))
105 pprType True ty = ppr ty
106 pprType False ty = ppr (GHC.dropForAlls ty)
109 | GHC.isSynTyCon tyCon
110 = let rhs_type = GHC.synTyConRhs tyCon
111 in hang (pprTyConHdr exts tyCon <+> equals) 2 (pprType exts rhs_type)
113 = pprAlgTyCon exts tyCon (const True) (const True)
115 pprAlgTyCon exts tyCon ok_con ok_label
116 | gadt = pprTyConHdr exts tyCon <+> ptext SLIT("where") $$
117 nest 2 (vcat (ppr_trim show_con datacons))
118 | otherwise = hang (pprTyConHdr exts tyCon)
119 2 (add_bars (ppr_trim show_con datacons))
121 datacons = GHC.tyConDataCons tyCon
122 gadt = any (not . GHC.isVanillaDataCon) datacons
125 | ok_con dataCon = Just (pprDataConDecl exts gadt ok_label dataCon)
126 | otherwise = Nothing
128 pprDataCon exts dataCon = pprAlgTyCon exts tyCon (== dataCon) (const True)
129 where tyCon = GHC.dataConTyCon dataCon
131 pprDataConDecl exts gadt_style show_label dataCon
132 | not gadt_style = ppr_fields tys_w_strs
133 | otherwise = ppr_bndr dataCon <+> dcolon <+>
134 sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
136 (tyvars, theta, argTypes, tyCon, res_tys) = GHC.dataConSig dataCon
137 labels = GHC.dataConFieldLabels dataCon
138 qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
139 stricts = GHC.dataConStrictMarks dataCon
140 tys_w_strs = zip stricts argTypes
143 | null qualVars = empty
144 | otherwise = ptext SLIT("forall") <+>
145 hsep (map ppr qualVars) <> dot
147 -- printing out the dataCon as a type signature, in GADT style
148 pp_tau = foldr add pp_res_ty tys_w_strs
149 pp_res_ty = ppr_bndr tyCon <+> hsep (map GHC.pprParendType res_tys)
150 add (str,ty) pp_ty = pprBangTy str ty <+> arrow <+> pp_ty
152 pprParendBangTy (strict,ty)
153 | GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty
154 | otherwise = GHC.pprParendType ty
157 | GHC.isMarkedStrict strict = char '!' <> ppr ty
160 maybe_show_label (lbl,(strict,tp))
161 | show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
162 | otherwise = Nothing
164 ppr_fields [ty1, ty2]
165 | GHC.dataConIsInfix dataCon && null labels
166 = sep [pprParendBangTy ty1, ppr dataCon, pprParendBangTy ty2]
169 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
171 = ppr_bndr dataCon <+>
172 braces (sep (punctuate comma (ppr_trim maybe_show_label
173 (zip labels fields))))
179 hang (pprClassHdr exts cls <+> ptext SLIT("where"))
180 2 (vcat (map (pprClassMethod exts) methods))
182 methods = GHC.classMethods cls
184 pprClassOneMethod exts cls this_one =
185 hang (pprClassHdr exts cls <+> ptext SLIT("where"))
186 2 (vcat (ppr_trim show_meth methods))
188 methods = GHC.classMethods cls
189 show_meth id | id == this_one = Just (pprClassMethod exts id)
190 | otherwise = Nothing
192 pprClassMethod exts id =
193 hang (ppr_bndr id <+> dcolon) 2 (pprType exts (classOpType id))
195 -- Here's the magic incantation to strip off the dictionary
196 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
197 classOpType id = GHC.funResultTy rho_ty
198 where (_sel_tyvars, rho_ty) = GHC.splitForAllTys (GHC.idType id)
200 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
202 = snd (foldr go (False, []) xs)
204 go x (eliding, so_far)
205 | Just doc <- show x = (False, doc : so_far)
206 | otherwise = if eliding then (True, so_far)
207 else (True, ptext SLIT("...") : so_far)
210 add_bars [c] = equals <+> c
211 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
213 -- Wrap operators in ()
214 ppr_bndr :: GHC.NamedThing a => a -> SDoc
215 ppr_bndr a = GHC.pprParenSymName a
217 showWithLoc :: SrcLoc -> SDoc -> SDoc
219 = hang doc 2 (char '\t' <> comment <+> GHC.pprDefnLoc loc)
220 -- The tab tries to make them line up a bit
222 comment = ptext SLIT("--")