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 Coercion ( coercionKindPredTy )
44 import BasicTypes ( tupleParens, isNoOcc, isAlwaysActive )
45 import Util ( lengthIs )
47 import FastString ( mkFastString )
50 %************************************************************************
52 \subsection{Public interfaces for Core printing (excluding instances)}
54 %************************************************************************
56 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
59 pprCoreBindings :: OutputableBndr b => [Bind b] -> SDoc
60 pprCoreBinding :: OutputableBndr b => Bind b -> SDoc
61 pprCoreExpr :: OutputableBndr b => Expr b -> SDoc
62 pprParendExpr :: OutputableBndr b => Expr b -> SDoc
64 pprCoreBindings = pprTopBinds
65 pprCoreBinding = pprTopBind
67 instance OutputableBndr b => Outputable (Bind b) where
68 ppr bind = ppr_bind bind
70 instance OutputableBndr b => Outputable (Expr b) where
71 ppr expr = pprCoreExpr expr
75 %************************************************************************
79 %************************************************************************
82 pprTopBinds binds = vcat (map pprTopBind binds)
84 pprTopBind (NonRec binder expr)
85 = ppr_binding (binder,expr) $$ text ""
87 pprTopBind (Rec binds)
88 = vcat [ptext SLIT("Rec {"),
89 vcat (map ppr_binding binds),
90 ptext SLIT("end Rec }"),
95 ppr_bind :: OutputableBndr b => Bind b -> SDoc
97 ppr_bind (NonRec val_bdr expr) = ppr_binding (val_bdr, expr)
98 ppr_bind (Rec binds) = vcat (map pp binds)
100 pp bind = ppr_binding bind <> semi
102 ppr_binding :: OutputableBndr b => (b, Expr b) -> SDoc
103 ppr_binding (val_bdr, expr)
104 = pprBndr LetBind val_bdr $$
105 hang (ppr val_bdr <+> equals) 2 (pprCoreExpr expr)
109 pprParendExpr expr = ppr_expr parens expr
110 pprCoreExpr expr = ppr_expr noParens expr
112 noParens :: SDoc -> SDoc
117 ppr_expr :: OutputableBndr b => (SDoc -> SDoc) -> Expr b -> SDoc
118 -- The function adds parens in context that need
119 -- an atomic value (e.g. function args)
121 ppr_expr add_par (Type ty) = add_par (ptext SLIT("TYPE") <+> ppr ty) -- Wierd
123 ppr_expr add_par (Var name) = ppr name
124 ppr_expr add_par (Lit lit) = ppr lit
126 ppr_expr add_par (Cast expr co)
128 sep [pprParendExpr expr,
129 ptext SLIT("`cast`") <+> parens (pprCo co)]
131 pprCo co = sep [ppr co, dcolon <+> ppr (coercionKindPredTy co)]
134 ppr_expr add_par expr@(Lam _ _)
136 (bndrs, body) = collectBinders expr
139 hang (ptext SLIT("\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
142 ppr_expr add_par expr@(App fun arg)
143 = case collectArgs expr of { (fun, args) ->
145 pp_args = sep (map pprArg args)
146 val_args = dropWhile isTypeArg args -- Drop the type arguments for tuples
147 pp_tup_args = sep (punctuate comma (map pprArg val_args))
150 Var f -> case isDataConWorkId_maybe f of
151 -- Notice that we print the *worker*
152 -- for tuples in paren'd format.
153 Just dc | saturated && isTupleTyCon tc
154 -> tupleParens (tupleTyConBoxity tc) pp_tup_args
157 saturated = val_args `lengthIs` idArity f
159 other -> add_par (hang (ppr f) 2 pp_args)
161 other -> add_par (hang (pprParendExpr fun) 2 pp_args)
164 ppr_expr add_par (Case expr var ty [(con,args,rhs)])
166 sep [sep [ptext SLIT("case") <+> pprCoreExpr expr,
167 ifPprDebug (braces (ppr ty)),
168 sep [ptext SLIT("of") <+> ppr_bndr var,
169 char '{' <+> ppr_case_pat con args]
175 ppr_bndr = pprBndr CaseBind
177 ppr_expr add_par (Case expr var ty alts)
179 sep [sep [ptext SLIT("case")
181 <+> ifPprDebug (braces (ppr ty)),
182 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
183 nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
187 ppr_bndr = pprBndr CaseBind
190 -- special cases: let ... in let ...
191 -- ("disgusting" SLPJ)
194 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
197 hsep [ptext SLIT("let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
198 nest 2 (pprCoreExpr rhs),
203 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
205 (hang (ptext SLIT("let {"))
206 2 (hsep [ppr_binding (val_bdr,rhs),
211 -- general case (recursive case, too)
212 ppr_expr add_par (Let bind expr)
214 sep [hang (ptext keyword) 2 (ppr_bind bind),
215 hang (ptext SLIT("} in ")) 2 (pprCoreExpr expr)]
217 keyword = case bind of
218 Rec _ -> SLIT("__letrec {")
219 NonRec _ _ -> SLIT("let {")
221 ppr_expr add_par (Note (SCC cc) expr)
222 = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
224 ppr_expr add_par (Note InlineMe expr)
225 = add_par $ ptext SLIT("__inline_me") <+> pprParendExpr expr
227 ppr_expr add_par (Note (CoreNote s) expr)
229 sep [sep [ptext SLIT("__core_note"), pprHsString (mkFastString s)],
232 pprCoreAlt (con, args, rhs)
233 = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
235 ppr_case_pat con@(DataAlt dc) args
237 = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
239 ppr_bndr = pprBndr CaseBind
242 ppr_case_pat con args
243 = ppr con <+> sep (map ppr_bndr args) <+> arrow
245 ppr_bndr = pprBndr CaseBind
247 pprArg (Type ty) = ptext SLIT("@") <+> pprParendType ty
248 pprArg expr = pprParendExpr expr
251 Other printing bits-and-bobs used with the general @pprCoreBinding@
252 and @pprCoreExpr@ functions.
255 instance OutputableBndr Var where
256 pprBndr = pprCoreBinder
258 pprCoreBinder :: BindingSite -> Var -> SDoc
259 pprCoreBinder LetBind binder
260 = vcat [sig, pprIdDetails binder, pragmas]
262 sig = pprTypedBinder binder
263 pragmas = ppIdInfo binder (idInfo binder)
265 -- Lambda bound type variables are preceded by "@"
266 pprCoreBinder LambdaBind bndr = parens (pprTypedBinder bndr)
268 -- Case bound things don't get a signature or a herald, unless we have debug on
269 pprCoreBinder CaseBind bndr
270 = getPprStyle $ \ sty ->
271 if debugStyle sty then
272 parens (pprTypedBinder bndr)
274 pprUntypedBinder bndr
276 pprUntypedBinder binder
277 | isTyVar binder = ptext SLIT("@") <+> ppr binder -- NB: don't print kind
278 | otherwise = pprIdBndr binder
280 pprTypedBinder binder
281 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
282 | otherwise = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
284 pprTyVarBndr :: TyVar -> SDoc
286 = getPprStyle $ \ sty ->
287 if debugStyle sty then
288 hsep [ppr tyvar, dcolon, pprParendKind kind]
289 -- See comments with ppDcolon in PprCore.lhs
293 kind = tyVarKind tyvar
295 -- pprIdBndr does *not* print the type
296 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
297 pprIdBndr id = ppr id <+> pprIdBndrInfo (idInfo id)
300 = megaSeqIdInfo `seq` doc -- The seq is useful for poking on black holes
302 prag_info = inlinePragInfo info
303 occ_info = occInfo info
304 dmd_info = newDemandInfo info
305 lbv_info = lbvarInfo info
307 no_info = isAlwaysActive prag_info && isNoOcc occ_info &&
308 (case dmd_info of { Nothing -> True; Just d -> isTop d }) &&
309 hasNoLBVarInfo lbv_info
311 doc | no_info = empty
313 = brackets $ hsep [ppr prag_info, ppr occ_info,
314 ppr dmd_info, ppr lbv_info
315 #ifdef OLD_STRICTNESS
316 , ppr (demandInfo id)
323 pprIdDetails :: Id -> SDoc
324 pprIdDetails id | isGlobalId id = ppr (globalIdDetails id)
325 | isExportedId id = ptext SLIT("[Exported]")
328 ppIdInfo :: Id -> IdInfo -> SDoc
331 vcat [ ppArityInfo a,
332 ppWorkerInfo (workerInfo info),
333 ppCafInfo (cafInfo info),
334 #ifdef OLD_STRICTNESS
338 pprNewStrictness (newStrictnessInfo info),
339 if null rules then empty
340 else ptext SLIT("RULES:") <+> vcat (map pprRule rules)
341 -- Inline pragma, occ, demand, lbvar info
342 -- printed out with all binders (when debug is on);
343 -- see PprCore.pprIdBndr
347 #ifdef OLD_STRICTNESS
348 s = strictnessInfo info
351 rules = specInfoRules (specInfo info)
356 instance Outputable CoreRule where
359 pprRules :: [CoreRule] -> SDoc
360 pprRules rules = vcat (map pprRule rules)
362 pprRule :: CoreRule -> SDoc
363 pprRule (BuiltinRule { ru_fn = fn, ru_name = name})
364 = ptext SLIT("Built in rule for") <+> ppr fn <> colon <+> doubleQuotes (ftext name)
366 pprRule (Rule { ru_name = name, ru_act = act, ru_fn = fn,
367 ru_bndrs = tpl_vars, ru_args = tpl_args,
369 = doubleQuotes (ftext name) <+> ppr act <+>
371 ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
372 nest 2 (ppr fn <+> sep (map pprArg tpl_args)),
373 nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)