2 % (c) The AQUA Project, Glasgow University, 1996
4 %************************************************************************
6 \section[PprCore]{Printing of Core syntax, including for interfaces}
8 %************************************************************************
12 pprCoreExpr, pprIfaceUnfolding,
13 pprCoreBinding, pprCoreBindings,
17 #include "HsVersions.h"
20 import CostCentre ( showCostCentre )
21 import Id ( idType, idInfo, isTupleCon,
22 GenId{-instances-}, Id
24 import IdInfo ( ppIdInfo )
25 import Outputable -- quite a few things
27 import PprType ( pprParendType, pprTyVarBndr )
31 %************************************************************************
33 \subsection{Public interfaces for Core printing (excluding instances)}
35 %************************************************************************
37 @pprCoreBinding@ and @pprCoreExpr@ let you give special printing
38 function for ``major'' val_bdrs (those next to equal signs :-),
39 ``minor'' ones (lambda-bound, case-bound), and bindees. They would
40 usually be called through some intermediary.
42 The binder/occ printers take the default ``homogenized'' (see
43 @PprEnv@...) @Doc@ and the binder/occ. They can either use the
44 homogenized one, or they can ignore it completely. In other words,
45 the things passed in act as ``hooks'', getting the last word on how to
48 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
50 Un-annotated core dumps
51 ~~~~~~~~~~~~~~~~~~~~~~~
53 pprCoreBindings :: [CoreBinding] -> SDoc
54 pprCoreBinding :: CoreBinding -> SDoc
55 pprCoreExpr :: CoreExpr -> SDoc
57 pprCoreBindings = pprTopBinds pprCoreEnv
58 pprCoreBinding = pprTopBind pprCoreEnv
59 pprCoreExpr = ppr_expr pprCoreEnv
61 pprCoreEnv = init_ppr_env ppr pprCoreBinder ppr
64 Printer for unfoldings in interfaces
65 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67 pprIfaceUnfolding :: CoreExpr -> SDoc
68 pprIfaceUnfolding = ppr_expr pprIfaceEnv
70 pprIfaceEnv = init_ppr_env pprTyVarBndr pprIfaceBinder ppr
73 Generic Core (possibly annotated binders etc)
74 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76 pprGenericBindings :: (Outputable bndr, Outputable occ) => [GenCoreBinding bndr occ flexi] -> SDoc
77 pprGenericBindings = pprTopBinds pprGenericEnv
79 pprGenericEnv :: (Outputable bndr, Outputable occ) => PprEnv flexi bndr occ
80 pprGenericEnv = init_ppr_env ppr (\_ -> ppr) ppr
82 pprGenericArgEnv :: (Outputable occ) => PprEnv flexi bndr occ
83 pprGenericArgEnv = init_ppr_env ppr (error "ppr_bndr") ppr
85 instance (Outputable bndr, Outputable occ) => Outputable (GenCoreBinding bndr occ flexi) where
86 ppr bind = ppr_bind pprGenericEnv bind
88 instance (Outputable bndr, Outputable occ) => Outputable (GenCoreExpr bndr occ flexi) where
89 ppr expr = ppr_expr pprGenericEnv expr
91 instance (Outputable occ) => Outputable (GenCoreArg occ flexi) where
92 ppr arg = ppr_arg pprGenericArgEnv arg
94 instance (Outputable bndr, Outputable occ) => Outputable (GenCoreCaseAlts bndr occ flexi) where
95 ppr alts = ppr_alts pprGenericEnv alts
97 instance (Outputable bndr, Outputable occ) => Outputable (GenCoreCaseDefault bndr occ flexi) where
98 ppr deflt = ppr_default pprGenericEnv deflt
102 %************************************************************************
104 \subsection{Instance declarations for Core printing}
106 %************************************************************************
110 init_ppr_env tvbndr pbdr pocc
112 (Just ppr) -- literals
113 (Just ppr) -- data cons
114 (Just ppr_prim) -- primops
115 (Just (\ cc -> text (showCostCentre True cc)))
117 (Just tvbndr) -- tyvar binders
118 (Just ppr) -- tyvar occs
119 (Just pprParendType) -- types
121 (Just pbdr) (Just pocc) -- value vars
124 -- We add a "!" to distinguish Primitive applications from ordinary applications.
125 -- But not when printing for interfaces, where they are treated
126 -- as ordinary applications
127 ppr_prim prim = getPprStyle (\sty -> if ifaceStyle sty then
130 ppr prim <> char '!')
134 %************************************************************************
136 \subsection{The guts}
138 %************************************************************************
141 pprTopBinds pe binds = vcat (map (pprTopBind pe) binds)
143 pprTopBind pe (NonRec binder expr)
144 = sep [ppr_binding_pe pe (binder,expr)] $$ text ""
146 pprTopBind pe (Rec binds)
147 = vcat [ptext SLIT("Rec {"),
148 vcat (map (ppr_binding_pe pe) binds),
149 ptext SLIT("end Rec }"),
154 ppr_bind pe (NonRec val_bdr expr) = ppr_binding_pe pe (val_bdr, expr)
155 ppr_bind pe (Rec binds) = vcat (map pp binds)
157 pp bind = ppr_binding_pe pe bind <> semi
159 ppr_binding_pe pe (val_bdr, expr)
160 = sep [pValBndr pe LetBind val_bdr,
161 nest 2 (equals <+> ppr_expr pe expr)]
165 ppr_parend_expr pe expr
169 Var _ -> id -- leave unchanged
171 _ -> parens -- wraps in parens
173 parenify (ppr_expr pe expr)
177 ppr_expr pe (Var name) = pOcc pe name
178 ppr_expr pe (Lit lit) = pLit pe lit
180 ppr_expr pe (Con con args)
181 = pCon pe con <+> (braces $ sep (map (ppr_arg pe) args))
183 ppr_expr pe (Prim prim args)
184 = pPrim pe prim <+> (sep (map (ppr_arg pe) args))
186 ppr_expr pe expr@(Lam _ _)
188 (tyvars, vars, body) = collectBinders expr
190 hang (hsep [pp_vars SLIT("_/\\_") (pTyVarB pe) tyvars,
191 pp_vars SLIT("\\") (pValBndr pe LambdaBind) vars])
194 pp_vars lam pp [] = empty
196 = hsep [ptext lam, vcat (map pp vs), ptext SLIT("->")]
198 ppr_expr pe expr@(App fun arg)
200 (final_fun, final_args) = go fun [arg]
201 go (App fun arg) args_so_far = go fun (arg:args_so_far)
202 go fun args_so_far = (fun, args_so_far)
204 hang (ppr_parend_expr pe final_fun) 4 (sep (map (ppr_arg pe) final_args))
206 ppr_expr pe (Case expr alts)
208 -- johan thinks that single case patterns should be on same line as case,
209 -- and no indent; all sane persons agree with him.
211 ppr_bndr = pValBndr pe CaseBind
213 ppr_alt (AlgAlts [] (BindDefault n _)) = (<>) (ppr_bndr n) ppr_arrow
214 ppr_alt (PrimAlts [] (BindDefault n _)) = (<>) (ppr_bndr n) ppr_arrow
215 ppr_alt (PrimAlts ((l, _):[]) NoDefault)= (<>) (pLit pe l) ppr_arrow
216 ppr_alt (AlgAlts ((con, params, _):[]) NoDefault)
218 hsep (map ppr_bndr params),
221 ppr_rhs (AlgAlts [] (BindDefault _ expr)) = ppr_expr pe expr
222 ppr_rhs (AlgAlts ((_,_,expr):[]) NoDefault) = ppr_expr pe expr
223 ppr_rhs (PrimAlts [] (BindDefault _ expr)) = ppr_expr pe expr
224 ppr_rhs (PrimAlts ((_,expr):[]) NoDefault) = ppr_expr pe expr
227 ppr_arrow = ptext SLIT(" ->")
230 [sep [pp_keyword, nest 4 (ppr_expr pe expr), text "of {", ppr_alt alts],
231 (<>) (ppr_rhs alts) (text ";}")]
233 | otherwise -- default "case" printing
235 [sep [pp_keyword, nest 4 (ppr_expr pe expr), ptext SLIT("of {")],
236 nest 2 (ppr_alts pe alts),
239 pp_keyword = case alts of
240 AlgAlts _ _ -> ptext SLIT("case")
241 PrimAlts _ _ -> ptext SLIT("case#")
243 -- special cases: let ... in let ...
244 -- ("disgusting" SLPJ)
246 ppr_expr pe (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
248 hsep [ptext SLIT("let {"), pValBndr pe LetBind val_bdr, equals],
249 nest 2 (ppr_expr pe rhs),
253 ppr_expr pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
255 (hang (ptext SLIT("let {"))
256 2 (hsep [hang (hsep [pValBndr pe LetBind val_bdr, equals])
258 ptext SLIT("} in")]))
261 -- general case (recursive case, too)
262 ppr_expr pe (Let bind expr)
263 = sep [hang (ptext keyword) 2 (ppr_bind pe bind),
264 hang (ptext SLIT("} in ")) 2 (ppr_expr pe expr)]
266 keyword = case bind of
267 Rec _ -> SLIT("_letrec_ {")
268 NonRec _ _ -> SLIT("let {")
270 ppr_expr pe (Note (SCC cc) expr)
271 = sep [hsep [ptext SLIT("_scc_"), pSCC pe cc],
272 ppr_parend_expr pe expr ]
275 ppr_expr pe (Note (Coerce to_ty from_ty) expr)
277 if debugStyle sty && not (ifaceStyle sty) then
278 sep [hsep [ptext SLIT("_coerce_"), pTy pe to_ty, pTy pe from_ty],
279 ppr_parend_expr pe expr] sty
281 sep [hsep [ptext SLIT("_coerce_"), pTy pe to_ty],
282 ppr_parend_expr pe expr] sty
284 ppr_expr pe (Note (Coerce to_ty from_ty) expr)
285 = sep [hsep [ptext SLIT("_coerce_"), pTy pe to_ty],
286 ppr_parend_expr pe expr]
289 ppr_expr pe (Note InlineCall expr)
290 = ptext SLIT("_inline_") <+> ppr_parend_expr pe expr
292 only_one_alt (AlgAlts [] (BindDefault _ _)) = True
293 only_one_alt (AlgAlts (_:[]) NoDefault) = True
294 only_one_alt (PrimAlts [] (BindDefault _ _)) = True
295 only_one_alt (PrimAlts (_:[]) NoDefault) = True
296 only_one_alt _ = False
300 ppr_alts pe (AlgAlts alts deflt)
301 = vcat [ vcat (map ppr_alt alts), ppr_default pe deflt ]
303 ppr_arrow = ptext SLIT("->")
304 ppr_bndr = pValBndr pe CaseBind
306 ppr_alt (con, params, expr)
307 = hang (if isTupleCon con then
308 hsep [parens (hsep (punctuate comma (map ppr_bndr params))),
312 hsep (map ppr_bndr params),
315 4 (ppr_expr pe expr <> semi)
317 ppr_alts pe (PrimAlts alts deflt)
318 = vcat [ vcat (map ppr_alt alts), ppr_default pe deflt ]
321 = hang (hsep [pLit pe lit, ptext SLIT("->")])
322 4 (ppr_expr pe expr <> semi)
326 ppr_default pe NoDefault = empty
328 ppr_default pe (BindDefault val_bdr expr)
329 = hang (hsep [pValBndr pe CaseBind val_bdr, ptext SLIT("->")])
330 4 (ppr_expr pe expr <> semi)
334 ppr_arg pe (LitArg lit) = pLit pe lit
335 ppr_arg pe (VarArg v) = pOcc pe v
336 ppr_arg pe (TyArg ty) = ptext SLIT("_@_ ") <> pTy pe ty
339 Other printing bits-and-bobs used with the general @pprCoreBinding@
340 and @pprCoreExpr@ functions.
343 -- Used for printing dump info
344 pprCoreBinder LetBind binder
345 = vcat [sig, pragmas, ppr binder]
347 sig = pprTypedBinder binder
348 pragmas = ppIdInfo False{-no specs, thanks-} (idInfo binder)
350 pprCoreBinder LambdaBind binder = pprTypedBinder binder
351 pprCoreBinder CaseBind binder = ppr binder
354 -- Used for printing interface-file unfoldings
355 pprIfaceBinder CaseBind binder = ppr binder
356 pprIfaceBinder other binder = pprTypedBinder binder
358 pprTypedBinder binder
359 = ppr binder <+> ptext SLIT("::") <+> pprParendType (idType binder)
360 -- The space before the :: is important; it helps the lexer
361 -- when reading inferfaces. Otherwise it would lex "a::b" as one thing.
363 -- It's important that the type is parenthesised too, at least when
364 -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...