2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 %************************************************************************
6 \section[PprCore]{Printing of Core syntax, including for interfaces}
8 %************************************************************************
12 pprCoreExpr, pprParendExpr, pprIdBndr,
13 pprCoreBinding, pprCoreBindings, pprCoreAlt,
14 pprIdRules, pprCoreRule
17 #include "HsVersions.h"
20 import CostCentre ( pprCostCentreCore )
22 import Id ( Id, idType, isDataConWorkId_maybe, idLBVarInfo, idArity,
23 idInfo, idInlinePragma, idOccInfo,
27 globalIdDetails, isGlobalId, isExportedId,
28 isSpecPragmaId, idNewDemandInfo
30 import Var ( isTyVar )
31 import IdInfo ( IdInfo, megaSeqIdInfo,
32 arityInfo, ppArityInfo,
33 specInfo, pprNewStrictness,
34 workerInfo, ppWorkerInfo,
35 newStrictnessInfo, cafInfo, ppCafInfo,
38 strictnessInfo, ppStrictnessInfo,
41 import DataCon ( dataConTyCon )
42 import TyCon ( tupleTyConBoxity, isTupleTyCon )
43 import PprType ( pprParendType, pprType, pprTyVarBndr )
44 import BasicTypes ( tupleParens )
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 (ppr val_bdr <+> equals <+> 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 expr@(Lam _ _)
128 (bndrs, body) = collectBinders expr
131 hang (ptext SLIT("\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
134 ppr_expr add_par expr@(App fun arg)
135 = case collectArgs expr of { (fun, args) ->
137 pp_args = sep (map pprArg args)
138 val_args = dropWhile isTypeArg args -- Drop the type arguments for tuples
139 pp_tup_args = sep (punctuate comma (map pprArg val_args))
142 Var f -> case isDataConWorkId_maybe f of
143 -- Notice that we print the *worker*
144 -- for tuples in paren'd format.
145 Just dc | saturated && isTupleTyCon tc
146 -> tupleParens (tupleTyConBoxity tc) pp_tup_args
149 saturated = val_args `lengthIs` idArity f
151 other -> add_par (hang (ppr f) 2 pp_args)
153 other -> add_par (hang (pprParendExpr fun) 2 pp_args)
156 ppr_expr add_par (Case expr var [(con,args,rhs)])
158 sep [sep [ptext SLIT("case") <+> pprCoreExpr expr,
159 hsep [ptext SLIT("of"),
162 ppr_case_pat con args
168 ppr_bndr = pprBndr CaseBind
170 ppr_expr add_par (Case expr var alts)
172 sep [sep [ptext SLIT("case") <+> pprCoreExpr expr,
173 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
174 nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
178 ppr_bndr = pprBndr CaseBind
181 -- special cases: let ... in let ...
182 -- ("disgusting" SLPJ)
185 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
188 hsep [ptext SLIT("let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
189 nest 2 (pprCoreExpr rhs),
194 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
196 (hang (ptext SLIT("let {"))
197 2 (hsep [ppr_binding (val_bdr,rhs),
202 -- general case (recursive case, too)
203 ppr_expr add_par (Let bind expr)
205 sep [hang (ptext keyword) 2 (ppr_bind bind),
206 hang (ptext SLIT("} in ")) 2 (pprCoreExpr expr)]
208 keyword = case bind of
209 Rec _ -> SLIT("__letrec {")
210 NonRec _ _ -> SLIT("let {")
212 ppr_expr add_par (Note (SCC cc) expr)
213 = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
216 ppr_expr add_par (Note (Coerce to_ty from_ty) expr)
218 getPprStyle $ \ sty ->
219 if debugStyle sty then
220 sep [ptext SLIT("__coerce") <+>
221 sep [pprParendType to_ty, pprParendType from_ty],
224 sep [hsep [ptext SLIT("__coerce"), pprParendType to_ty],
227 ppr_expr add_par (Note (Coerce to_ty from_ty) expr)
229 sep [sep [ptext SLIT("__coerce"), nest 2 (pprParendType to_ty)],
233 ppr_expr add_par (Note InlineCall expr)
234 = add_par (ptext SLIT("__inline_call") <+> pprParendExpr expr)
236 ppr_expr add_par (Note InlineMe expr)
237 = add_par $ ptext SLIT("__inline_me") <+> pprParendExpr expr
239 ppr_expr add_par (Note (CoreNote s) expr)
241 sep [sep [ptext SLIT("__core_note"), pprHsString (mkFastString s)],
244 pprCoreAlt (con, args, rhs)
245 = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
247 ppr_case_pat con@(DataAlt dc) args
249 = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
251 ppr_bndr = pprBndr CaseBind
254 ppr_case_pat con args
255 = ppr con <+> hsep (map ppr_bndr args) <+> arrow
257 ppr_bndr = pprBndr CaseBind
259 pprArg (Type ty) = ptext SLIT("@") <+> pprParendType ty
260 pprArg expr = pprParendExpr expr
262 arrow = ptext SLIT("->")
265 Other printing bits-and-bobs used with the general @pprCoreBinding@
266 and @pprCoreExpr@ functions.
269 instance OutputableBndr Var where
270 pprBndr = pprCoreBinder
272 pprCoreBinder :: BindingSite -> Var -> SDoc
273 pprCoreBinder LetBind binder
274 = vcat [sig, pprIdDetails binder, pragmas]
276 sig = pprTypedBinder binder
277 pragmas = ppIdInfo binder (idInfo binder)
279 -- Lambda bound type variables are preceded by "@"
280 pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
282 -- Case bound things don't get a signature or a herald
283 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
285 pprUntypedBinder binder
286 | isTyVar binder = ptext SLIT("@") <+> ppr binder -- NB: don't print kind
287 | otherwise = pprIdBndr binder
289 pprTypedBinder binder
290 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
291 | otherwise = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
292 -- The space before the :: is important; it helps the lexer
293 -- when reading inferfaces. Otherwise it would lex "a::b" as one thing.
295 -- It's important that the type is parenthesised too, at least when
296 -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
297 -- [Jun 2002: interfaces are now binary, so this doesn't matter]
299 -- pprIdBndr does *not* print the type
300 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
301 pprIdBndr id = ppr id <+>
302 (megaSeqIdInfo (idInfo id) `seq`
303 -- Useful for poking on black holes
304 ifPprDebug (ppr (idInlinePragma id) <+> ppr (idOccInfo id) <+>
305 #ifdef OLD_STRICTNESS
306 ppr (idDemandInfo id) <+>
308 ppr (idNewDemandInfo id) <+>
309 ppr (idLBVarInfo id)))
314 pprIdDetails :: Id -> SDoc
315 pprIdDetails id | isGlobalId id = ppr (globalIdDetails id)
316 | isExportedId id = ptext SLIT("[Exported]")
317 | isSpecPragmaId id = ptext SLIT("[SpecPrag]")
320 ppIdInfo :: Id -> IdInfo -> SDoc
322 = hsep [ ppArityInfo a,
323 ppWorkerInfo (workerInfo info),
324 ppCafInfo (cafInfo info),
325 #ifdef OLD_STRICTNESS
329 pprNewStrictness (newStrictnessInfo info),
330 vcat (map (pprCoreRule (ppr b)) (rulesRules p))
331 -- Inline pragma, occ, demand, lbvar info
332 -- printed out with all binders (when debug is on);
333 -- see PprCore.pprIdBndr
337 #ifdef OLD_STRICTNESS
338 s = strictnessInfo info
346 pprIdRules :: [IdCoreRule] -> SDoc
347 pprIdRules rules = vcat (map pprIdRule rules)
349 pprIdRule :: IdCoreRule -> SDoc
350 pprIdRule (id,rule) = pprCoreRule (ppr id) rule
352 pprCoreRule :: SDoc -> CoreRule -> SDoc
353 pprCoreRule pp_fn (BuiltinRule name _)
354 = ifPprDebug (ptext SLIT("Built in rule for") <+> pp_fn <> colon
355 <+> doubleQuotes (ftext name))
357 pprCoreRule pp_fn (Rule name act tpl_vars tpl_args rhs)
358 = doubleQuotes (ftext name) <+> ppr act <+>
360 ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
361 nest 2 (pp_fn <+> sep (map pprArg tpl_args)),
362 nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)