2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 %************************************************************************
6 \section[PprCore]{Printing of Core syntax, including for interfaces}
8 %************************************************************************
12 pprCoreExpr, pprParendExpr,
13 pprCoreBinding, pprCoreBindings, pprIdBndr,
14 pprCoreBinding, pprCoreBindings, pprCoreAlt,
15 pprCoreRules, pprCoreRule, pprIdCoreRule
18 #include "HsVersions.h"
21 import CostCentre ( pprCostCentreCore )
22 import Id ( Id, idType, isDataConId_maybe, idLBVarInfo, idArity,
23 idInfo, idInlinePragma, idDemandInfo, idOccInfo,
24 globalIdDetails, isGlobalId, isExportedId, isSpecPragmaId
26 import Var ( isTyVar )
27 import IdInfo ( IdInfo, megaSeqIdInfo,
28 arityInfo, ppArityInfo,
29 specInfo, cprInfo, ppCprInfo,
30 strictnessInfo, ppStrictnessInfo, cgInfo, pprCgInfo,
32 workerInfo, ppWorkerInfo,
33 tyGenInfo, ppTyGenInfo
35 import DataCon ( dataConTyCon )
36 import TyCon ( tupleTyConBoxity, isTupleTyCon )
37 import PprType ( pprParendType, pprTyVarBndr )
38 import BasicTypes ( tupleParens )
43 %************************************************************************
45 \subsection{Public interfaces for Core printing (excluding instances)}
47 %************************************************************************
49 @pprCoreBinding@ and @pprCoreExpr@ let you give special printing
50 function for ``major'' val_bdrs (those next to equal signs :-),
51 ``minor'' ones (lambda-bound, case-bound), and bindees. They would
52 usually be called through some intermediary.
54 The binder/occ printers take the default ``homogenized'' (see
55 @PprEnv@...) @Doc@ and the binder/occ. They can either use the
56 homogenized one, or they can ignore it completely. In other words,
57 the things passed in act as ``hooks'', getting the last word on how to
60 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
62 Un-annotated core dumps
63 ~~~~~~~~~~~~~~~~~~~~~~~
65 pprCoreBindings :: [CoreBind] -> SDoc
66 pprCoreBinding :: CoreBind -> SDoc
67 pprCoreExpr :: CoreExpr -> SDoc
68 pprParendExpr :: CoreExpr -> SDoc
70 pprCoreBindings = pprTopBinds pprCoreEnv
71 pprCoreBinding = pprTopBind pprCoreEnv
72 pprCoreExpr = ppr_noparend_expr pprCoreEnv
73 pprParendExpr = ppr_parend_expr pprCoreEnv
74 pprArg = ppr_arg pprCoreEnv
75 pprCoreAlt = ppr_alt pprCoreEnv
77 pprCoreEnv = initCoreEnv pprCoreBinder
80 Printer for unfoldings in interfaces
81 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83 instance Outputable b => Outputable (Bind b) where
84 ppr bind = ppr_bind pprGenericEnv bind
86 instance Outputable b => Outputable (Expr b) where
87 ppr expr = ppr_noparend_expr pprGenericEnv expr
89 pprGenericEnv :: Outputable b => PprEnv b
90 pprGenericEnv = initCoreEnv (\site -> ppr)
93 %************************************************************************
95 \subsection{Instance declarations for Core printing}
97 %************************************************************************
103 (Just pprCostCentreCore) -- Cost centres
105 (Just ppr) -- tyvar occs
106 (Just pprParendType) -- types
108 (Just pbdr) (Just ppr) -- value vars
109 -- Use pprIdBndr for this last one as a debugging device.
112 %************************************************************************
114 \subsection{The guts}
116 %************************************************************************
119 pprTopBinds pe binds = vcat (map (pprTopBind pe) binds)
121 pprTopBind pe (NonRec binder expr)
122 = ppr_binding_pe pe (binder,expr) $$ text ""
124 pprTopBind pe (Rec binds)
125 = vcat [ptext SLIT("Rec {"),
126 vcat (map (ppr_binding_pe pe) binds),
127 ptext SLIT("end Rec }"),
132 ppr_bind :: PprEnv b -> Bind b -> SDoc
134 ppr_bind pe (NonRec val_bdr expr) = ppr_binding_pe pe (val_bdr, expr)
135 ppr_bind pe (Rec binds) = vcat (map pp binds)
137 pp bind = ppr_binding_pe pe bind <> semi
139 ppr_binding_pe :: PprEnv b -> (b, Expr b) -> SDoc
140 ppr_binding_pe pe (val_bdr, expr)
141 = sep [pBndr pe LetBind val_bdr,
142 nest 2 (equals <+> ppr_noparend_expr pe expr)]
146 ppr_parend_expr pe expr = ppr_expr parens pe expr
147 ppr_noparend_expr pe expr = ppr_expr noParens pe expr
149 noParens :: SDoc -> SDoc
154 ppr_expr :: (SDoc -> SDoc) -> PprEnv b -> Expr b -> SDoc
155 -- The function adds parens in context that need
156 -- an atomic value (e.g. function args)
158 ppr_expr add_par pe (Type ty) = add_par (ptext SLIT("TYPE") <+> ppr ty) -- Wierd
160 ppr_expr add_par pe (Var name) = pOcc pe name
161 ppr_expr add_par pe (Lit lit) = ppr lit
163 ppr_expr add_par pe expr@(Lam _ _)
165 (bndrs, body) = collectBinders expr
168 hang (ptext SLIT("\\") <+> sep (map (pBndr pe LambdaBind) bndrs) <+> arrow)
169 2 (ppr_noparend_expr pe body)
171 ppr_expr add_par pe expr@(App fun arg)
172 = case collectArgs expr of { (fun, args) ->
174 pp_args = sep (map (ppr_arg pe) args)
175 val_args = dropWhile isTypeArg args -- Drop the type arguments for tuples
176 pp_tup_args = sep (punctuate comma (map (ppr_arg pe) val_args))
179 Var f -> case isDataConId_maybe f of
180 -- Notice that we print the *worker*
181 -- for tuples in paren'd format.
182 Just dc | saturated && isTupleTyCon tc
183 -> tupleParens (tupleTyConBoxity tc) pp_tup_args
186 saturated = length val_args == idArity f
188 other -> add_par (hang (pOcc pe f) 2 pp_args)
190 other -> add_par (hang (ppr_parend_expr pe fun) 2 pp_args)
193 ppr_expr add_par pe (Case expr var [(con,args,rhs)])
195 sep [sep [ptext SLIT("case") <+> ppr_noparend_expr pe expr,
196 hsep [ptext SLIT("of"),
199 ppr_case_pat pe con args
201 ppr_noparend_expr pe rhs,
205 ppr_bndr = pBndr pe CaseBind
207 ppr_expr add_par pe (Case expr var alts)
209 sep [sep [ptext SLIT("case") <+> ppr_noparend_expr pe expr,
210 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
211 nest 2 (sep (punctuate semi (map (ppr_alt pe) alts))),
215 ppr_bndr = pBndr pe CaseBind
218 -- special cases: let ... in let ...
219 -- ("disgusting" SLPJ)
221 ppr_expr add_par pe (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
224 hsep [ptext SLIT("let {"), pBndr pe LetBind val_bdr, equals],
225 nest 2 (ppr_noparend_expr pe rhs),
227 ppr_noparend_expr pe body ]
229 ppr_expr add_par pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
231 (hang (ptext SLIT("let {"))
232 2 (hsep [hang (hsep [pBndr pe LetBind val_bdr, equals])
233 2 (ppr_noparend_expr pe rhs),
236 ppr_noparend_expr pe expr)
238 -- general case (recursive case, too)
239 ppr_expr add_par pe (Let bind expr)
241 sep [hang (ptext keyword) 2 (ppr_bind pe bind),
242 hang (ptext SLIT("} in ")) 2 (ppr_noparend_expr pe expr)]
244 keyword = case bind of
245 Rec _ -> SLIT("__letrec {")
246 NonRec _ _ -> SLIT("let {")
248 ppr_expr add_par pe (Note (SCC cc) expr)
249 = add_par (sep [pSCC pe cc, ppr_noparend_expr pe expr])
252 ppr_expr add_par pe (Note (Coerce to_ty from_ty) expr)
254 getPprStyle $ \ sty ->
255 if debugStyle sty && not (ifaceStyle sty) then
256 sep [ptext SLIT("__coerce") <+> sep [pTy pe to_ty, pTy pe from_ty],
257 ppr_parend_expr pe expr]
259 sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty],
260 ppr_parend_expr pe expr]
262 ppr_expr add_par pe (Note (Coerce to_ty from_ty) expr)
264 sep [sep [ptext SLIT("__coerce"), nest 2 (pTy pe to_ty)],
265 ppr_parend_expr pe expr]
268 ppr_expr add_par pe (Note InlineCall expr)
269 = add_par (ptext SLIT("__inline_call") <+> ppr_parend_expr pe expr)
271 ppr_expr add_par pe (Note InlineMe expr)
272 = add_par $ ptext SLIT("__inline_me") <+> ppr_parend_expr pe expr
274 ppr_alt pe (con, args, rhs)
275 = hang (ppr_case_pat pe con args) 2 (ppr_noparend_expr pe rhs)
277 ppr_case_pat pe con@(DataAlt dc) args
279 = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
281 ppr_bndr = pBndr pe CaseBind
284 ppr_case_pat pe con args
285 = ppr con <+> hsep (map ppr_bndr args) <+> arrow
287 ppr_bndr = pBndr pe CaseBind
289 ppr_arg pe (Type ty) = ptext SLIT("@") <+> pTy pe ty
290 ppr_arg pe expr = ppr_parend_expr pe expr
292 arrow = ptext SLIT("->")
295 Other printing bits-and-bobs used with the general @pprCoreBinding@
296 and @pprCoreExpr@ functions.
299 -- Used for printing dump info
300 pprCoreBinder LetBind binder
301 = vcat [sig, pprIdDetails binder, pragmas, ppr binder]
303 sig = pprTypedBinder binder
304 pragmas = ppIdInfo binder (idInfo binder)
306 -- Lambda bound type variables are preceded by "@"
307 pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
309 -- Case bound things don't get a signature or a herald
310 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
312 pprUntypedBinder binder
313 | isTyVar binder = ptext SLIT("@") <+> ppr binder -- NB: don't print kind
314 | otherwise = pprIdBndr binder
316 pprTypedBinder binder
317 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
318 | otherwise = pprIdBndr binder <+> dcolon <+> pprParendType (idType binder)
319 -- The space before the :: is important; it helps the lexer
320 -- when reading inferfaces. Otherwise it would lex "a::b" as one thing.
322 -- It's important that the type is parenthesised too, at least when
323 -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
325 -- pprIdBndr does *not* print the type
326 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
327 pprIdBndr id = ppr id <+>
328 (megaSeqIdInfo (idInfo id) `seq`
329 -- Useful for poking on black holes
330 ifPprDebug (ppr (idInlinePragma id) <+> ppr (idOccInfo id) <+>
331 ppr (idDemandInfo id)) <+> ppr (idLBVarInfo id))
336 pprIdDetails :: Id -> SDoc
337 pprIdDetails id | isGlobalId id = ppr (globalIdDetails id)
338 | isExportedId id = ptext SLIT("[Exported]")
339 | isSpecPragmaId id = ptext SLIT("[SpecPrag]")
342 ppIdInfo :: Id -> IdInfo -> SDoc
344 = hsep [ ppArityInfo a,
346 ppWorkerInfo (workerInfo info),
351 -- Inline pragma, occ, demand, lbvar info
352 -- printed out with all binders (when debug is on);
353 -- see PprCore.pprIdBndr
358 s = strictnessInfo info
366 pprCoreRules :: Id -> CoreRules -> SDoc
367 pprCoreRules var (Rules rules _) = vcat (map (pprCoreRule (ppr var)) rules)
369 pprIdCoreRule :: IdCoreRule -> SDoc
370 pprIdCoreRule (id,rule) = pprCoreRule (ppr id) rule
372 pprCoreRule :: SDoc -> CoreRule -> SDoc
373 pprCoreRule pp_fn (BuiltinRule _)
374 = ifPprDebug (ptext SLIT("A built in rule"))
376 pprCoreRule pp_fn (Rule name tpl_vars tpl_args rhs)
377 = doubleQuotes (ptext name) <+>
379 ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
380 nest 2 (pp_fn <+> sep (map pprArg tpl_args)),
381 nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)