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