2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1996-1998
6 Printing of Core syntax
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
17 pprCoreExpr, pprParendExpr,
18 pprCoreBinding, pprCoreBindings, pprCoreAlt,
43 %************************************************************************
45 \subsection{Public interfaces for Core printing (excluding instances)}
47 %************************************************************************
49 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
52 pprCoreBindings :: OutputableBndr b => [Bind b] -> SDoc
53 pprCoreBinding :: OutputableBndr b => Bind b -> SDoc
54 pprCoreExpr :: OutputableBndr b => Expr b -> SDoc
55 pprParendExpr :: OutputableBndr b => Expr b -> SDoc
57 pprCoreBindings = pprTopBinds
58 pprCoreBinding = pprTopBind
60 instance OutputableBndr b => Outputable (Bind b) where
61 ppr bind = ppr_bind bind
63 instance OutputableBndr b => Outputable (Expr b) where
64 ppr expr = pprCoreExpr expr
68 %************************************************************************
72 %************************************************************************
75 pprTopBinds binds = vcat (map pprTopBind binds)
77 pprTopBind (NonRec binder expr)
78 = ppr_binding (binder,expr) $$ text ""
80 pprTopBind (Rec binds)
81 = vcat [ptext (sLit "Rec {"),
82 vcat (map ppr_binding binds),
83 ptext (sLit "end Rec }"),
88 ppr_bind :: OutputableBndr b => Bind b -> SDoc
90 ppr_bind (NonRec val_bdr expr) = ppr_binding (val_bdr, expr)
91 ppr_bind (Rec binds) = vcat (map pp binds)
93 pp bind = ppr_binding bind <> semi
95 ppr_binding :: OutputableBndr b => (b, Expr b) -> SDoc
96 ppr_binding (val_bdr, expr)
97 = pprBndr LetBind val_bdr $$
98 hang (ppr val_bdr <+> equals) 2 (pprCoreExpr expr)
102 pprParendExpr expr = ppr_expr parens expr
103 pprCoreExpr expr = ppr_expr noParens expr
105 noParens :: SDoc -> SDoc
110 ppr_expr :: OutputableBndr b => (SDoc -> SDoc) -> Expr b -> SDoc
111 -- The function adds parens in context that need
112 -- an atomic value (e.g. function args)
114 ppr_expr add_par (Type ty) = add_par (ptext (sLit "TYPE") <+> ppr ty) -- Wierd
116 ppr_expr add_par (Var name) = ppr name
117 ppr_expr add_par (Lit lit) = ppr lit
119 ppr_expr add_par (Cast expr co)
121 sep [pprParendExpr expr,
122 ptext (sLit "`cast`") <+> parens (pprCo co)]
124 pprCo co = sep [ppr co, dcolon <+> ppr (coercionKindPredTy co)]
127 ppr_expr add_par expr@(Lam _ _)
129 (bndrs, body) = collectBinders expr
132 hang (ptext (sLit "\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
135 ppr_expr add_par expr@(App fun arg)
136 = case collectArgs expr of { (fun, args) ->
138 pp_args = sep (map pprArg args)
139 val_args = dropWhile isTypeArg args -- Drop the type arguments for tuples
140 pp_tup_args = sep (punctuate comma (map pprCoreExpr val_args))
143 Var f -> case isDataConWorkId_maybe f of
144 -- Notice that we print the *worker*
145 -- for tuples in paren'd format.
146 Just dc | saturated && isTupleTyCon tc
147 -> tupleParens (tupleTyConBoxity tc) pp_tup_args
150 saturated = val_args `lengthIs` idArity f
152 other -> add_par (hang (ppr f) 2 pp_args)
154 other -> add_par (hang (pprParendExpr fun) 2 pp_args)
157 ppr_expr add_par (Case expr var ty [(con,args,rhs)])
159 sep [sep [ptext (sLit "case") <+> pprCoreExpr expr,
160 ifPprDebug (braces (ppr ty)),
161 sep [ptext (sLit "of") <+> ppr_bndr var,
162 char '{' <+> 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),
195 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
197 (hang (ptext (sLit "let {"))
198 2 (hsep [ppr_binding (val_bdr,rhs),
199 ptext (sLit "} in")])
204 -- General case (recursive case, too)
205 ppr_expr add_par (Let bind expr)
207 sep [hang (ptext keyword) 2 (ppr_bind bind <+> ptext (sLit "} in")),
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])
217 ppr_expr add_par (Note InlineMe expr)
218 = add_par $ ptext (sLit "__inline_me") <+> pprParendExpr expr
220 ppr_expr add_par (Note (CoreNote s) expr)
222 sep [sep [ptext (sLit "__core_note"), pprHsString (mkFastString s)],
225 pprCoreAlt (con, args, rhs)
226 = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
228 ppr_case_pat con@(DataAlt dc) args
230 = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
232 ppr_bndr = pprBndr CaseBind
235 ppr_case_pat con args
236 = ppr con <+> sep (map ppr_bndr args) <+> arrow
238 ppr_bndr = pprBndr CaseBind
240 pprArg (Type ty) = ptext (sLit "@") <+> pprParendType ty
241 pprArg expr = pprParendExpr expr
244 Other printing bits-and-bobs used with the general @pprCoreBinding@
245 and @pprCoreExpr@ functions.
248 instance OutputableBndr Var where
249 pprBndr = pprCoreBinder
251 pprCoreBinder :: BindingSite -> Var -> SDoc
252 pprCoreBinder LetBind binder
253 | isTyVar binder = pprTypedBinder binder
255 = vcat [sig, pprIdDetails binder, pragmas]
257 sig = pprTypedBinder binder
258 pragmas = ppIdInfo binder (idInfo binder)
260 -- Lambda bound type variables are preceded by "@"
261 pprCoreBinder LambdaBind bndr = parens (pprTypedBinder bndr)
263 -- Case bound things don't get a signature or a herald, unless we have debug on
264 pprCoreBinder CaseBind bndr
265 = getPprStyle $ \ sty ->
266 if debugStyle sty then
267 parens (pprTypedBinder bndr)
269 pprUntypedBinder bndr
271 pprUntypedBinder binder
272 | isTyVar binder = ptext (sLit "@") <+> ppr binder -- NB: don't print kind
273 | otherwise = pprIdBndr binder
275 pprTypedBinder binder
276 | isTyVar binder = ptext (sLit "@") <+> pprTyVarBndr binder
277 | otherwise = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
279 pprTyVarBndr :: TyVar -> SDoc
281 = getPprStyle $ \ sty ->
282 if debugStyle sty then
283 hsep [ppr tyvar, dcolon, pprParendKind kind]
284 -- See comments with ppDcolon in PprCore.lhs
288 kind = tyVarKind tyvar
290 -- pprIdBndr does *not* print the type
291 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
292 pprIdBndr id = ppr id <+> pprIdBndrInfo (idInfo id)
295 = megaSeqIdInfo info `seq` doc -- The seq is useful for poking on black holes
297 prag_info = inlinePragInfo info
298 occ_info = occInfo info
299 dmd_info = newDemandInfo info
300 lbv_info = lbvarInfo info
302 no_info = isAlwaysActive prag_info && isNoOcc occ_info &&
303 (case dmd_info of { Nothing -> True; Just d -> isTop d }) &&
304 hasNoLBVarInfo lbv_info
306 doc | no_info = empty
308 = brackets $ hsep [ppr prag_info, ppr occ_info,
309 ppr dmd_info, ppr lbv_info
310 #ifdef OLD_STRICTNESS
311 , ppr (demandInfo id)
318 pprIdDetails :: Id -> SDoc
319 pprIdDetails id | isGlobalId id = ppr (globalIdDetails id)
320 | isExportedId id = ptext (sLit "[Exported]")
323 ppIdInfo :: Id -> IdInfo -> SDoc
326 vcat [ ppArityInfo a,
327 ppWorkerInfo (workerInfo info),
328 ppCafInfo (cafInfo info),
329 #ifdef OLD_STRICTNESS
333 pprNewStrictness (newStrictnessInfo info),
334 if null rules then empty
335 else ptext (sLit "RULES:") <+> vcat (map pprRule rules)
336 -- Inline pragma, occ, demand, lbvar info
337 -- printed out with all binders (when debug is on);
338 -- see PprCore.pprIdBndr
342 #ifdef OLD_STRICTNESS
343 s = strictnessInfo info
346 rules = specInfoRules (specInfo info)
351 instance Outputable CoreRule where
354 pprRules :: [CoreRule] -> SDoc
355 pprRules rules = vcat (map pprRule rules)
357 pprRule :: CoreRule -> SDoc
358 pprRule (BuiltinRule { ru_fn = fn, ru_name = name})
359 = ptext (sLit "Built in rule for") <+> ppr fn <> colon <+> doubleQuotes (ftext name)
361 pprRule (Rule { ru_name = name, ru_act = act, ru_fn = fn,
362 ru_bndrs = tpl_vars, ru_args = tpl_args,
364 = hang (doubleQuotes (ftext name) <+> ppr act)
365 4 (sep [ptext (sLit "forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
366 nest 2 (ppr fn <+> sep (map pprArg tpl_args)),
367 nest 2 (ptext (sLit "=") <+> pprCoreExpr rhs)