08fbdc417e2600de91bed24a71421e9c08a2fa74
[ghc-hetmet.git] / compiler / coreSyn / PprCore.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1996-1998
4 %
5
6 Printing of Core syntax
7
8 \begin{code}
9 module PprCore (
10         pprCoreExpr, pprParendExpr,
11         pprCoreBinding, pprCoreBindings, pprCoreAlt,
12         pprRules
13     ) where
14
15 #include "HsVersions.h"
16
17 import CoreSyn
18 import CostCentre
19 import Var
20 import Id
21 import IdInfo
22 import NewDemand
23 #ifdef OLD_STRICTNESS
24 import Id
25 import IdInfo
26 #endif
27
28 import DataCon
29 import TyCon
30 import Type
31 import Coercion
32 import BasicTypes
33 import Util
34 import Outputable
35 import FastString
36 \end{code}
37
38 %************************************************************************
39 %*                                                                      *
40 \subsection{Public interfaces for Core printing (excluding instances)}
41 %*                                                                      *
42 %************************************************************************
43
44 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
45
46 \begin{code}
47 pprCoreBindings :: OutputableBndr b => [Bind b] -> SDoc
48 pprCoreBinding  :: OutputableBndr b => Bind b  -> SDoc
49 pprCoreExpr     :: OutputableBndr b => Expr b  -> SDoc
50 pprParendExpr   :: OutputableBndr b => Expr b  -> SDoc
51
52 pprCoreBindings = pprTopBinds
53 pprCoreBinding  = pprTopBind 
54
55 instance OutputableBndr b => Outputable (Bind b) where
56     ppr bind = ppr_bind bind
57
58 instance OutputableBndr b => Outputable (Expr b) where
59     ppr expr = pprCoreExpr expr
60 \end{code}
61
62
63 %************************************************************************
64 %*                                                                      *
65 \subsection{The guts}
66 %*                                                                      *
67 %************************************************************************
68
69 \begin{code}
70 pprTopBinds binds = vcat (map pprTopBind binds)
71
72 pprTopBind (NonRec binder expr)
73  = ppr_binding (binder,expr) $$ text ""
74
75 pprTopBind (Rec binds)
76   = vcat [ptext SLIT("Rec {"),
77           vcat (map ppr_binding binds),
78           ptext SLIT("end Rec }"),
79           text ""]
80 \end{code}
81
82 \begin{code}
83 ppr_bind :: OutputableBndr b => Bind b -> SDoc
84
85 ppr_bind (NonRec val_bdr expr) = ppr_binding (val_bdr, expr)
86 ppr_bind (Rec binds)           = vcat (map pp binds)
87                                where
88                                  pp bind = ppr_binding bind <> semi
89
90 ppr_binding :: OutputableBndr b => (b, Expr b) -> SDoc
91 ppr_binding (val_bdr, expr)
92   = pprBndr LetBind val_bdr $$ 
93     hang (ppr val_bdr <+> equals) 2 (pprCoreExpr expr)
94 \end{code}
95
96 \begin{code}
97 pprParendExpr   expr = ppr_expr parens expr
98 pprCoreExpr expr = ppr_expr noParens expr
99
100 noParens :: SDoc -> SDoc
101 noParens pp = pp
102 \end{code}
103
104 \begin{code}
105 ppr_expr :: OutputableBndr b => (SDoc -> SDoc) -> Expr b -> SDoc
106         -- The function adds parens in context that need
107         -- an atomic value (e.g. function args)
108
109 ppr_expr add_par (Type ty)  = add_par (ptext SLIT("TYPE") <+> ppr ty)   -- Wierd
110                    
111 ppr_expr add_par (Var name) = ppr name
112 ppr_expr add_par (Lit lit)  = ppr lit
113
114 ppr_expr add_par (Cast expr co) 
115   = add_par $
116     sep [pprParendExpr expr, 
117          ptext SLIT("`cast`") <+> parens (pprCo co)]
118   where
119     pprCo co = sep [ppr co, dcolon <+> ppr (coercionKindPredTy co)]
120          
121
122 ppr_expr add_par expr@(Lam _ _)
123   = let
124         (bndrs, body) = collectBinders expr
125     in
126     add_par $
127     hang (ptext SLIT("\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
128          2 (pprCoreExpr body)
129
130 ppr_expr add_par expr@(App fun arg)
131   = case collectArgs expr of { (fun, args) -> 
132     let
133         pp_args     = sep (map pprArg args)
134         val_args    = dropWhile isTypeArg args   -- Drop the type arguments for tuples
135         pp_tup_args = sep (punctuate comma (map pprArg val_args))
136     in
137     case fun of
138         Var f -> case isDataConWorkId_maybe f of
139                         -- Notice that we print the *worker*
140                         -- for tuples in paren'd format.
141                    Just dc | saturated && isTupleTyCon tc
142                            -> tupleParens (tupleTyConBoxity tc) pp_tup_args
143                            where
144                              tc        = dataConTyCon dc
145                              saturated = val_args `lengthIs` idArity f
146
147                    other -> add_par (hang (ppr f) 2 pp_args)
148
149         other -> add_par (hang (pprParendExpr fun) 2 pp_args)
150     }
151
152 ppr_expr add_par (Case expr var ty [(con,args,rhs)])
153   = add_par $
154     sep [sep [ptext SLIT("case") <+> pprCoreExpr expr,
155               ifPprDebug (braces (ppr ty)),
156               sep [ptext SLIT("of") <+> ppr_bndr var, 
157                    char '{' <+> ppr_case_pat con args]
158           ],
159          pprCoreExpr rhs,
160          char '}'
161     ]
162   where
163     ppr_bndr = pprBndr CaseBind
164
165 ppr_expr add_par (Case expr var ty alts)
166   = add_par $
167     sep [sep [ptext SLIT("case")
168                 <+> pprCoreExpr expr
169                 <+> ifPprDebug (braces (ppr ty)),
170               ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
171          nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
172          char '}'
173     ]
174   where
175     ppr_bndr = pprBndr CaseBind
176  
177
178 -- special cases: let ... in let ...
179 -- ("disgusting" SLPJ)
180
181 {-
182 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
183   = add_par $
184     vcat [
185       hsep [ptext SLIT("let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
186       nest 2 (pprCoreExpr rhs),
187       ptext SLIT("} in"),
188       pprCoreExpr body ]
189 -}
190
191 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
192   = add_par
193     (hang (ptext SLIT("let {"))
194           2 (hsep [ppr_binding (val_bdr,rhs),
195                    ptext SLIT("} in")])
196      $$
197      pprCoreExpr expr)
198
199 -- general case (recursive case, too)
200 ppr_expr add_par (Let bind expr)
201   = add_par $
202     sep [hang (ptext keyword) 2 (ppr_bind bind),
203          hang (ptext SLIT("} in ")) 2 (pprCoreExpr expr)]
204   where
205     keyword = case bind of
206                 Rec _      -> SLIT("__letrec {")
207                 NonRec _ _ -> SLIT("let {")
208
209 ppr_expr add_par (Note (SCC cc) expr)
210   = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
211
212 ppr_expr add_par (Note InlineMe expr)
213   = add_par $ ptext SLIT("__inline_me") <+> pprParendExpr expr
214
215 ppr_expr add_par (Note (CoreNote s) expr)
216   = add_par $ 
217     sep [sep [ptext SLIT("__core_note"), pprHsString (mkFastString s)],
218          pprParendExpr expr]
219
220 pprCoreAlt (con, args, rhs) 
221   = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
222
223 ppr_case_pat con@(DataAlt dc) args
224   | isTupleTyCon tc
225   = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
226   where
227     ppr_bndr = pprBndr CaseBind
228     tc = dataConTyCon dc
229
230 ppr_case_pat con args
231   = ppr con <+> sep (map ppr_bndr args) <+> arrow
232   where
233     ppr_bndr = pprBndr CaseBind
234
235 pprArg (Type ty) = ptext SLIT("@") <+> pprParendType ty
236 pprArg expr      = pprParendExpr expr
237 \end{code}
238
239 Other printing bits-and-bobs used with the general @pprCoreBinding@
240 and @pprCoreExpr@ functions.
241
242 \begin{code}
243 instance OutputableBndr Var where
244   pprBndr = pprCoreBinder
245
246 pprCoreBinder :: BindingSite -> Var -> SDoc
247 pprCoreBinder LetBind binder
248   = vcat [sig, pprIdDetails binder, pragmas]
249   where
250     sig     = pprTypedBinder binder
251     pragmas = ppIdInfo binder (idInfo binder)
252
253 -- Lambda bound type variables are preceded by "@"
254 pprCoreBinder LambdaBind bndr = parens (pprTypedBinder bndr)
255
256 -- Case bound things don't get a signature or a herald, unless we have debug on
257 pprCoreBinder CaseBind bndr 
258   = getPprStyle $ \ sty ->
259     if debugStyle sty then
260         parens (pprTypedBinder bndr)
261     else
262         pprUntypedBinder bndr
263
264 pprUntypedBinder binder
265   | isTyVar binder = ptext SLIT("@") <+> ppr binder     -- NB: don't print kind
266   | otherwise      = pprIdBndr binder
267
268 pprTypedBinder binder
269   | isTyVar binder  = ptext SLIT("@") <+> pprTyVarBndr binder
270   | otherwise       = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
271
272 pprTyVarBndr :: TyVar -> SDoc
273 pprTyVarBndr tyvar
274   = getPprStyle $ \ sty ->
275     if debugStyle sty then
276         hsep [ppr tyvar, dcolon, pprParendKind kind]
277                 -- See comments with ppDcolon in PprCore.lhs
278     else
279         ppr tyvar
280   where
281     kind = tyVarKind tyvar
282
283 -- pprIdBndr does *not* print the type
284 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
285 pprIdBndr id = ppr id <+> pprIdBndrInfo (idInfo id)
286
287 pprIdBndrInfo info 
288   = megaSeqIdInfo `seq` doc -- The seq is useful for poking on black holes
289   where
290     prag_info = inlinePragInfo info
291     occ_info  = occInfo info
292     dmd_info  = newDemandInfo info
293     lbv_info  = lbvarInfo info
294
295     no_info = isAlwaysActive prag_info && isNoOcc occ_info && 
296               (case dmd_info of { Nothing -> True; Just d -> isTop d }) &&
297               hasNoLBVarInfo lbv_info
298
299     doc | no_info = empty
300         | otherwise
301         = brackets $ hsep [ppr prag_info, ppr occ_info, 
302                            ppr dmd_info, ppr lbv_info
303 #ifdef OLD_STRICTNESS
304                            , ppr (demandInfo id)
305 #endif
306                           ]
307 \end{code}
308
309
310 \begin{code}
311 pprIdDetails :: Id -> SDoc
312 pprIdDetails id | isGlobalId id     = ppr (globalIdDetails id)
313                 | isExportedId id   = ptext SLIT("[Exported]")
314                 | otherwise         = empty
315
316 ppIdInfo :: Id -> IdInfo -> SDoc
317 ppIdInfo b info
318   = brackets $
319     vcat [  ppArityInfo a,
320             ppWorkerInfo (workerInfo info),
321             ppCafInfo (cafInfo info),
322 #ifdef OLD_STRICTNESS
323             ppStrictnessInfo s,
324             ppCprInfo m,
325 #endif
326             pprNewStrictness (newStrictnessInfo info),
327             if null rules then empty
328             else ptext SLIT("RULES:") <+> vcat (map pprRule rules)
329         -- Inline pragma, occ, demand, lbvar info
330         -- printed out with all binders (when debug is on); 
331         -- see PprCore.pprIdBndr
332         ]
333   where
334     a = arityInfo info
335 #ifdef OLD_STRICTNESS
336     s = strictnessInfo info
337     m = cprInfo info
338 #endif
339     rules = specInfoRules (specInfo info)
340 \end{code}
341
342
343 \begin{code}
344 instance Outputable CoreRule where
345    ppr = pprRule
346
347 pprRules :: [CoreRule] -> SDoc
348 pprRules rules = vcat (map pprRule rules)
349
350 pprRule :: CoreRule -> SDoc
351 pprRule (BuiltinRule { ru_fn = fn, ru_name = name})
352   = ptext SLIT("Built in rule for") <+> ppr fn <> colon <+> doubleQuotes (ftext name)
353
354 pprRule (Rule { ru_name = name, ru_act = act, ru_fn = fn,
355                 ru_bndrs = tpl_vars, ru_args = tpl_args,
356                 ru_rhs = rhs })
357   = hang (doubleQuotes (ftext name) <+> ppr act)
358        4 (sep [ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
359                nest 2 (ppr fn <+> sep (map pprArg tpl_args)),
360                nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)
361             ])
362 \end{code}