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,
22 #include "HsVersions.h"
45 %************************************************************************
47 \subsection{Public interfaces for Core printing (excluding instances)}
49 %************************************************************************
51 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
54 pprCoreBindings :: OutputableBndr b => [Bind b] -> SDoc
55 pprCoreBinding :: OutputableBndr b => Bind b -> SDoc
56 pprCoreExpr :: OutputableBndr b => Expr b -> SDoc
57 pprParendExpr :: OutputableBndr b => Expr b -> SDoc
59 pprCoreBindings = pprTopBinds
60 pprCoreBinding = pprTopBind
62 instance OutputableBndr b => Outputable (Bind b) where
63 ppr bind = ppr_bind bind
65 instance OutputableBndr b => Outputable (Expr b) where
66 ppr expr = pprCoreExpr expr
70 %************************************************************************
74 %************************************************************************
77 pprTopBinds binds = vcat (map pprTopBind binds)
79 pprTopBind (NonRec binder expr)
80 = ppr_binding (binder,expr) $$ text ""
82 pprTopBind (Rec binds)
83 = vcat [ptext SLIT("Rec {"),
84 vcat (map ppr_binding binds),
85 ptext SLIT("end Rec }"),
90 ppr_bind :: OutputableBndr b => Bind b -> SDoc
92 ppr_bind (NonRec val_bdr expr) = ppr_binding (val_bdr, expr)
93 ppr_bind (Rec binds) = vcat (map pp binds)
95 pp bind = ppr_binding bind <> semi
97 ppr_binding :: OutputableBndr b => (b, Expr b) -> SDoc
98 ppr_binding (val_bdr, expr)
99 = pprBndr LetBind val_bdr $$
100 hang (ppr val_bdr <+> equals) 2 (pprCoreExpr expr)
104 pprParendExpr expr = ppr_expr parens expr
105 pprCoreExpr expr = ppr_expr noParens expr
107 noParens :: SDoc -> SDoc
112 ppr_expr :: OutputableBndr b => (SDoc -> SDoc) -> Expr b -> SDoc
113 -- The function adds parens in context that need
114 -- an atomic value (e.g. function args)
116 ppr_expr add_par (Type ty) = add_par (ptext SLIT("TYPE") <+> ppr ty) -- Wierd
118 ppr_expr add_par (Var name) = ppr name
119 ppr_expr add_par (Lit lit) = ppr lit
121 ppr_expr add_par (Cast expr co)
123 sep [pprParendExpr expr,
124 ptext SLIT("`cast`") <+> parens (pprCo co)]
126 pprCo co = sep [ppr co, dcolon <+> ppr (coercionKindPredTy co)]
129 ppr_expr add_par expr@(Lam _ _)
131 (bndrs, body) = collectBinders expr
134 hang (ptext SLIT("\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
137 ppr_expr add_par expr@(App fun arg)
138 = case collectArgs expr of { (fun, args) ->
140 pp_args = sep (map pprArg args)
141 val_args = dropWhile isTypeArg args -- Drop the type arguments for tuples
142 pp_tup_args = sep (punctuate comma (map pprCoreExpr val_args))
145 Var f -> case isDataConWorkId_maybe f of
146 -- Notice that we print the *worker*
147 -- for tuples in paren'd format.
148 Just dc | saturated && isTupleTyCon tc
149 -> tupleParens (tupleTyConBoxity tc) pp_tup_args
152 saturated = val_args `lengthIs` idArity f
154 other -> add_par (hang (ppr f) 2 pp_args)
156 other -> add_par (hang (pprParendExpr fun) 2 pp_args)
159 ppr_expr add_par (Case expr var ty [(con,args,rhs)])
161 sep [sep [ptext SLIT("case") <+> pprCoreExpr expr,
162 ifPprDebug (braces (ppr ty)),
163 sep [ptext SLIT("of") <+> ppr_bndr var,
164 char '{' <+> ppr_case_pat con args]
170 ppr_bndr = pprBndr CaseBind
172 ppr_expr add_par (Case expr var ty alts)
174 sep [sep [ptext SLIT("case")
176 <+> ifPprDebug (braces (ppr ty)),
177 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
178 nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
182 ppr_bndr = pprBndr CaseBind
185 -- special cases: let ... in let ...
186 -- ("disgusting" SLPJ)
189 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
192 hsep [ptext SLIT("let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
193 nest 2 (pprCoreExpr rhs),
197 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
199 (hang (ptext SLIT("let {"))
200 2 (hsep [ppr_binding (val_bdr,rhs),
206 -- General case (recursive case, too)
207 ppr_expr add_par (Let bind expr)
209 sep [hang (ptext keyword) 2 (ppr_bind bind <+> ptext SLIT("} in")),
212 keyword = case bind of
213 Rec _ -> SLIT("letrec {")
214 NonRec _ _ -> SLIT("let {")
216 ppr_expr add_par (Note (SCC cc) expr)
217 = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
219 ppr_expr add_par (Note InlineMe expr)
220 = add_par $ ptext SLIT("__inline_me") <+> pprParendExpr expr
222 ppr_expr add_par (Note (CoreNote s) expr)
224 sep [sep [ptext SLIT("__core_note"), pprHsString (mkFastString s)],
227 pprCoreAlt (con, args, rhs)
228 = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
230 ppr_case_pat con@(DataAlt dc) args
232 = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
234 ppr_bndr = pprBndr CaseBind
237 ppr_case_pat con args
238 = ppr con <+> sep (map ppr_bndr args) <+> arrow
240 ppr_bndr = pprBndr CaseBind
242 pprArg (Type ty) = ptext SLIT("@") <+> pprParendType ty
243 pprArg expr = pprParendExpr expr
246 Other printing bits-and-bobs used with the general @pprCoreBinding@
247 and @pprCoreExpr@ functions.
250 instance OutputableBndr Var where
251 pprBndr = pprCoreBinder
253 pprCoreBinder :: BindingSite -> Var -> SDoc
254 pprCoreBinder LetBind 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 `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)