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, pprCoreAlt,
17 #include "HsVersions.h"
20 import CostCentre ( pprCostCentreCore )
22 import Id ( Id, idType, isDataConWorkId_maybe, idArity,
23 idInfo, globalIdDetails, isGlobalId, isExportedId
25 import Var ( TyVar, isTyVar, tyVarKind )
26 import IdInfo ( IdInfo, megaSeqIdInfo,
27 inlinePragInfo, occInfo, newDemandInfo,
28 lbvarInfo, hasNoLBVarInfo,
29 arityInfo, ppArityInfo,
30 specInfo, pprNewStrictness,
31 workerInfo, ppWorkerInfo,
32 newStrictnessInfo, cafInfo, ppCafInfo, specInfoRules
34 import NewDemand ( isTop )
36 import Id ( idDemandInfo )
37 import IdInfo ( cprInfo, ppCprInfo, strictnessInfo, ppStrictnessInfo )
40 import DataCon ( dataConTyCon )
41 import TyCon ( tupleTyConBoxity, isTupleTyCon )
42 import Type ( pprParendType, pprType, pprParendKind )
43 import BasicTypes ( tupleParens, isNoOcc, isAlwaysActive )
44 import Util ( lengthIs )
46 import FastString ( mkFastString )
49 %************************************************************************
51 \subsection{Public interfaces for Core printing (excluding instances)}
53 %************************************************************************
55 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
58 pprCoreBindings :: OutputableBndr b => [Bind b] -> SDoc
59 pprCoreBinding :: OutputableBndr b => Bind b -> SDoc
60 pprCoreExpr :: OutputableBndr b => Expr b -> SDoc
61 pprParendExpr :: OutputableBndr b => Expr b -> SDoc
63 pprCoreBindings = pprTopBinds
64 pprCoreBinding = pprTopBind
66 instance OutputableBndr b => Outputable (Bind b) where
67 ppr bind = ppr_bind bind
69 instance OutputableBndr b => Outputable (Expr b) where
70 ppr expr = pprCoreExpr expr
74 %************************************************************************
78 %************************************************************************
81 pprTopBinds binds = vcat (map pprTopBind binds)
83 pprTopBind (NonRec binder expr)
84 = ppr_binding (binder,expr) $$ text ""
86 pprTopBind (Rec binds)
87 = vcat [ptext SLIT("Rec {"),
88 vcat (map ppr_binding binds),
89 ptext SLIT("end Rec }"),
94 ppr_bind :: OutputableBndr b => Bind b -> SDoc
96 ppr_bind (NonRec val_bdr expr) = ppr_binding (val_bdr, expr)
97 ppr_bind (Rec binds) = vcat (map pp binds)
99 pp bind = ppr_binding bind <> semi
101 ppr_binding :: OutputableBndr b => (b, Expr b) -> SDoc
102 ppr_binding (val_bdr, expr)
103 = pprBndr LetBind val_bdr $$
104 hang (ppr val_bdr <+> equals) 2 (pprCoreExpr expr)
108 pprParendExpr expr = ppr_expr parens expr
109 pprCoreExpr expr = ppr_expr noParens expr
111 noParens :: SDoc -> SDoc
116 ppr_expr :: OutputableBndr b => (SDoc -> SDoc) -> Expr b -> SDoc
117 -- The function adds parens in context that need
118 -- an atomic value (e.g. function args)
120 ppr_expr add_par (Type ty) = add_par (ptext SLIT("TYPE") <+> ppr ty) -- Wierd
122 ppr_expr add_par (Var name) = ppr name
123 ppr_expr add_par (Lit lit) = ppr lit
125 ppr_expr add_par expr@(Lam _ _)
127 (bndrs, body) = collectBinders expr
130 hang (ptext SLIT("\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
133 ppr_expr add_par expr@(App fun arg)
134 = case collectArgs expr of { (fun, args) ->
136 pp_args = sep (map pprArg args)
137 val_args = dropWhile isTypeArg args -- Drop the type arguments for tuples
138 pp_tup_args = sep (punctuate comma (map pprArg val_args))
141 Var f -> case isDataConWorkId_maybe f of
142 -- Notice that we print the *worker*
143 -- for tuples in paren'd format.
144 Just dc | saturated && isTupleTyCon tc
145 -> tupleParens (tupleTyConBoxity tc) pp_tup_args
148 saturated = val_args `lengthIs` idArity f
150 other -> add_par (hang (ppr f) 2 pp_args)
152 other -> add_par (hang (pprParendExpr fun) 2 pp_args)
155 ppr_expr add_par (Case expr var ty [(con,args,rhs)])
157 sep [sep [ptext SLIT("case") <+> pprCoreExpr expr,
158 ifPprDebug (braces (ppr ty)),
159 hsep [ptext SLIT("of"),
162 ppr_case_pat con args
168 ppr_bndr = pprBndr CaseBind
170 ppr_expr add_par (Case expr var ty alts)
172 sep [sep [ptext SLIT("case")
174 <+> ifPprDebug (braces (ppr ty)),
175 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
176 nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
180 ppr_bndr = pprBndr CaseBind
183 -- special cases: let ... in let ...
184 -- ("disgusting" SLPJ)
187 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
190 hsep [ptext SLIT("let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
191 nest 2 (pprCoreExpr rhs),
196 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
198 (hang (ptext SLIT("let {"))
199 2 (hsep [ppr_binding (val_bdr,rhs),
204 -- general case (recursive case, too)
205 ppr_expr add_par (Let bind expr)
207 sep [hang (ptext keyword) 2 (ppr_bind bind),
208 hang (ptext SLIT("} in ")) 2 (pprCoreExpr expr)]
210 keyword = case bind of
211 Rec _ -> SLIT("__letrec {")
212 NonRec _ _ -> SLIT("let {")
214 ppr_expr add_par (Note (SCC cc) expr)
215 = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
218 ppr_expr add_par (Note (Coerce to_ty from_ty) expr)
220 getPprStyle $ \ sty ->
221 if debugStyle sty then
222 sep [ptext SLIT("__coerce") <+>
223 sep [pprParendType to_ty, pprParendType from_ty],
226 sep [hsep [ptext SLIT("__coerce"), pprParendType to_ty],
229 ppr_expr add_par (Note (Coerce to_ty from_ty) expr)
231 sep [sep [ptext SLIT("__coerce"), nest 2 (pprParendType to_ty)],
235 ppr_expr add_par (Note InlineMe expr)
236 = add_par $ ptext SLIT("__inline_me") <+> pprParendExpr expr
238 ppr_expr add_par (Note (CoreNote s) expr)
240 sep [sep [ptext SLIT("__core_note"), pprHsString (mkFastString s)],
243 pprCoreAlt (con, args, rhs)
244 = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
246 ppr_case_pat con@(DataAlt dc) args
248 = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
250 ppr_bndr = pprBndr CaseBind
253 ppr_case_pat con args
254 = ppr con <+> hsep (map ppr_bndr args) <+> arrow
256 ppr_bndr = pprBndr CaseBind
258 pprArg (Type ty) = ptext SLIT("@") <+> pprParendType ty
259 pprArg expr = pprParendExpr expr
262 Other printing bits-and-bobs used with the general @pprCoreBinding@
263 and @pprCoreExpr@ functions.
266 instance OutputableBndr Var where
267 pprBndr = pprCoreBinder
269 pprCoreBinder :: BindingSite -> Var -> SDoc
270 pprCoreBinder LetBind binder
271 = vcat [sig, pprIdDetails binder, pragmas]
273 sig = pprTypedBinder binder
274 pragmas = ppIdInfo binder (idInfo binder)
276 -- Lambda bound type variables are preceded by "@"
277 pprCoreBinder LambdaBind bndr = parens (pprTypedBinder bndr)
279 -- Case bound things don't get a signature or a herald, unless we have debug on
280 pprCoreBinder CaseBind bndr
281 = getPprStyle $ \ sty ->
282 if debugStyle sty then
283 parens (pprTypedBinder bndr)
285 pprUntypedBinder bndr
287 pprUntypedBinder binder
288 | isTyVar binder = ptext SLIT("@") <+> ppr binder -- NB: don't print kind
289 | otherwise = pprIdBndr binder
291 pprTypedBinder binder
292 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
293 | otherwise = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
295 pprTyVarBndr :: TyVar -> SDoc
297 = getPprStyle $ \ sty ->
298 if debugStyle sty then
299 hsep [ppr tyvar, dcolon, pprParendKind kind]
300 -- See comments with ppDcolon in PprCore.lhs
304 kind = tyVarKind tyvar
306 -- pprIdBndr does *not* print the type
307 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
308 pprIdBndr id = ppr id <+> pprIdBndrInfo (idInfo id)
311 = megaSeqIdInfo `seq` doc -- The seq is useful for poking on black holes
313 prag_info = inlinePragInfo info
314 occ_info = occInfo info
315 dmd_info = newDemandInfo info
316 lbv_info = lbvarInfo info
318 no_info = isAlwaysActive prag_info && isNoOcc occ_info &&
319 (case dmd_info of { Nothing -> True; Just d -> isTop d }) &&
320 hasNoLBVarInfo lbv_info
322 doc | no_info = empty
324 = brackets $ hsep [ppr prag_info, ppr occ_info,
325 ppr dmd_info, ppr lbv_info
326 #ifdef OLD_STRICTNESS
327 , ppr (demandInfo id)
334 pprIdDetails :: Id -> SDoc
335 pprIdDetails id | isGlobalId id = ppr (globalIdDetails id)
336 | isExportedId id = ptext SLIT("[Exported]")
339 ppIdInfo :: Id -> IdInfo -> SDoc
342 vcat [ ppArityInfo a,
343 ppWorkerInfo (workerInfo info),
344 ppCafInfo (cafInfo info),
345 #ifdef OLD_STRICTNESS
349 pprNewStrictness (newStrictnessInfo info),
350 if null rules then empty
351 else ptext SLIT("RULES:") <+> vcat (map pprRule rules)
352 -- Inline pragma, occ, demand, lbvar info
353 -- printed out with all binders (when debug is on);
354 -- see PprCore.pprIdBndr
358 #ifdef OLD_STRICTNESS
359 s = strictnessInfo info
362 rules = specInfoRules (specInfo info)
367 instance Outputable CoreRule where
370 pprRules :: [CoreRule] -> SDoc
371 pprRules rules = vcat (map pprRule rules)
373 pprRule :: CoreRule -> SDoc
374 pprRule (BuiltinRule { ru_fn = fn, ru_name = name})
375 = ptext SLIT("Built in rule for") <+> ppr fn <> colon <+> doubleQuotes (ftext name)
377 pprRule (Rule { ru_name = name, ru_act = act, ru_fn = fn,
378 ru_bndrs = tpl_vars, ru_args = tpl_args,
380 = doubleQuotes (ftext name) <+> ppr act <+>
382 ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
383 nest 2 (ppr fn <+> sep (map pprArg tpl_args)),
384 nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)