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 ( coercionKindTyConApp )
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 (coercionKindTyConApp 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 hsep [ptext SLIT("of"),
171 ppr_case_pat con args
177 ppr_bndr = pprBndr CaseBind
179 ppr_expr add_par (Case expr var ty alts)
181 sep [sep [ptext SLIT("case")
183 <+> ifPprDebug (braces (ppr ty)),
184 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
185 nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
189 ppr_bndr = pprBndr CaseBind
192 -- special cases: let ... in let ...
193 -- ("disgusting" SLPJ)
196 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
199 hsep [ptext SLIT("let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
200 nest 2 (pprCoreExpr rhs),
205 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
207 (hang (ptext SLIT("let {"))
208 2 (hsep [ppr_binding (val_bdr,rhs),
213 -- general case (recursive case, too)
214 ppr_expr add_par (Let bind expr)
216 sep [hang (ptext keyword) 2 (ppr_bind bind),
217 hang (ptext SLIT("} in ")) 2 (pprCoreExpr expr)]
219 keyword = case bind of
220 Rec _ -> SLIT("__letrec {")
221 NonRec _ _ -> SLIT("let {")
223 ppr_expr add_par (Note (SCC cc) expr)
224 = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
226 ppr_expr add_par (Note InlineMe expr)
227 = add_par $ ptext SLIT("__inline_me") <+> pprParendExpr expr
229 ppr_expr add_par (Note (CoreNote s) expr)
231 sep [sep [ptext SLIT("__core_note"), pprHsString (mkFastString s)],
234 pprCoreAlt (con, args, rhs)
235 = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
237 ppr_case_pat con@(DataAlt dc) args
239 = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
241 ppr_bndr = pprBndr CaseBind
244 ppr_case_pat con args
245 = ppr con <+> hsep (map ppr_bndr args) <+> arrow
247 ppr_bndr = pprBndr CaseBind
249 pprArg (Type ty) = ptext SLIT("@") <+> pprParendType ty
250 pprArg expr = pprParendExpr expr
253 Other printing bits-and-bobs used with the general @pprCoreBinding@
254 and @pprCoreExpr@ functions.
257 instance OutputableBndr Var where
258 pprBndr = pprCoreBinder
260 pprCoreBinder :: BindingSite -> Var -> SDoc
261 pprCoreBinder LetBind binder
262 = vcat [sig, pprIdDetails binder, pragmas]
264 sig = pprTypedBinder binder
265 pragmas = ppIdInfo binder (idInfo binder)
267 -- Lambda bound type variables are preceded by "@"
268 pprCoreBinder LambdaBind bndr = parens (pprTypedBinder bndr)
270 -- Case bound things don't get a signature or a herald, unless we have debug on
271 pprCoreBinder CaseBind bndr
272 = getPprStyle $ \ sty ->
273 if debugStyle sty then
274 parens (pprTypedBinder bndr)
276 pprUntypedBinder bndr
278 pprUntypedBinder binder
279 | isTyVar binder = ptext SLIT("@") <+> ppr binder -- NB: don't print kind
280 | otherwise = pprIdBndr binder
282 pprTypedBinder binder
283 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
284 | otherwise = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
286 pprTyVarBndr :: TyVar -> SDoc
288 = getPprStyle $ \ sty ->
289 if debugStyle sty then
290 hsep [ppr tyvar, dcolon, pprParendKind kind]
291 -- See comments with ppDcolon in PprCore.lhs
295 kind = tyVarKind tyvar
297 -- pprIdBndr does *not* print the type
298 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
299 pprIdBndr id = ppr id <+> pprIdBndrInfo (idInfo id)
302 = megaSeqIdInfo `seq` doc -- The seq is useful for poking on black holes
304 prag_info = inlinePragInfo info
305 occ_info = occInfo info
306 dmd_info = newDemandInfo info
307 lbv_info = lbvarInfo info
309 no_info = isAlwaysActive prag_info && isNoOcc occ_info &&
310 (case dmd_info of { Nothing -> True; Just d -> isTop d }) &&
311 hasNoLBVarInfo lbv_info
313 doc | no_info = empty
315 = brackets $ hsep [ppr prag_info, ppr occ_info,
316 ppr dmd_info, ppr lbv_info
317 #ifdef OLD_STRICTNESS
318 , ppr (demandInfo id)
325 pprIdDetails :: Id -> SDoc
326 pprIdDetails id | isGlobalId id = ppr (globalIdDetails id)
327 | isExportedId id = ptext SLIT("[Exported]")
330 ppIdInfo :: Id -> IdInfo -> SDoc
333 vcat [ ppArityInfo a,
334 ppWorkerInfo (workerInfo info),
335 ppCafInfo (cafInfo info),
336 #ifdef OLD_STRICTNESS
340 pprNewStrictness (newStrictnessInfo info),
341 if null rules then empty
342 else ptext SLIT("RULES:") <+> vcat (map pprRule rules)
343 -- Inline pragma, occ, demand, lbvar info
344 -- printed out with all binders (when debug is on);
345 -- see PprCore.pprIdBndr
349 #ifdef OLD_STRICTNESS
350 s = strictnessInfo info
353 rules = specInfoRules (specInfo info)
358 instance Outputable CoreRule where
361 pprRules :: [CoreRule] -> SDoc
362 pprRules rules = vcat (map pprRule rules)
364 pprRule :: CoreRule -> SDoc
365 pprRule (BuiltinRule { ru_fn = fn, ru_name = name})
366 = ptext SLIT("Built in rule for") <+> ppr fn <> colon <+> doubleQuotes (ftext name)
368 pprRule (Rule { ru_name = name, ru_act = act, ru_fn = fn,
369 ru_bndrs = tpl_vars, ru_args = tpl_args,
371 = doubleQuotes (ftext name) <+> ppr act <+>
373 ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
374 nest 2 (ppr fn <+> sep (map pprArg tpl_args)),
375 nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)