[project @ 2004-11-25 11:36:34 by simonpj]
[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         pprIdRules
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, idLBVarInfo, idArity,
23                           idInfo, idInlinePragma, idOccInfo,
24 #ifdef OLD_STRICTNESS
25                           idDemandInfo, 
26 #endif
27                           globalIdDetails, isGlobalId, isExportedId, 
28                           isSpecPragmaId, idNewDemandInfo
29                         )
30 import Var              ( TyVar, isTyVar, tyVarKind )
31 import IdInfo           ( IdInfo, megaSeqIdInfo, 
32                           arityInfo, ppArityInfo, 
33                           specInfo, pprNewStrictness,
34                           workerInfo, ppWorkerInfo,
35                           newStrictnessInfo, cafInfo, ppCafInfo,
36 #ifdef OLD_STRICTNESS
37                           cprInfo, ppCprInfo, 
38                           strictnessInfo, ppStrictnessInfo, 
39 #endif
40                         )
41 import DataCon          ( dataConTyCon )
42 import TyCon            ( tupleTyConBoxity, isTupleTyCon )
43 import Type             ( pprParendType, pprType, pprParendKind )
44 import BasicTypes       ( tupleParens )
45 import Util             ( lengthIs )
46 import Outputable
47 import FastString       ( mkFastString )
48 \end{code}
49
50 %************************************************************************
51 %*                                                                      *
52 \subsection{Public interfaces for Core printing (excluding instances)}
53 %*                                                                      *
54 %************************************************************************
55
56 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
57
58 \begin{code}
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
63
64 pprCoreBindings = pprTopBinds
65 pprCoreBinding  = pprTopBind 
66
67 instance OutputableBndr b => Outputable (Bind b) where
68     ppr bind = ppr_bind bind
69
70 instance OutputableBndr b => Outputable (Expr b) where
71     ppr expr = pprCoreExpr expr
72 \end{code}
73
74
75 %************************************************************************
76 %*                                                                      *
77 \subsection{The guts}
78 %*                                                                      *
79 %************************************************************************
80
81 \begin{code}
82 pprTopBinds binds = vcat (map pprTopBind binds)
83
84 pprTopBind (NonRec binder expr)
85  = ppr_binding (binder,expr) $$ text ""
86
87 pprTopBind (Rec binds)
88   = vcat [ptext SLIT("Rec {"),
89           vcat (map ppr_binding binds),
90           ptext SLIT("end Rec }"),
91           text ""]
92 \end{code}
93
94 \begin{code}
95 ppr_bind :: OutputableBndr b => Bind b -> SDoc
96
97 ppr_bind (NonRec val_bdr expr) = ppr_binding (val_bdr, expr)
98 ppr_bind (Rec binds)           = vcat (map pp binds)
99                                where
100                                  pp bind = ppr_binding bind <> semi
101
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)
106 \end{code}
107
108 \begin{code}
109 pprParendExpr   expr = ppr_expr parens expr
110 pprCoreExpr expr = ppr_expr noParens expr
111
112 noParens :: SDoc -> SDoc
113 noParens pp = pp
114 \end{code}
115
116 \begin{code}
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)
120
121 ppr_expr add_par (Type ty)  = add_par (ptext SLIT("TYPE") <+> ppr ty)   -- Wierd
122                    
123 ppr_expr add_par (Var name) = ppr name
124 ppr_expr add_par (Lit lit)  = ppr lit
125
126 ppr_expr add_par expr@(Lam _ _)
127   = let
128         (bndrs, body) = collectBinders expr
129     in
130     add_par $
131     hang (ptext SLIT("\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
132          2 (pprCoreExpr body)
133
134 ppr_expr add_par expr@(App fun arg)
135   = case collectArgs expr of { (fun, args) -> 
136     let
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))
140     in
141     case fun of
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
147                            where
148                              tc        = dataConTyCon dc
149                              saturated = val_args `lengthIs` idArity f
150
151                    other -> add_par (hang (ppr f) 2 pp_args)
152
153         other -> add_par (hang (pprParendExpr fun) 2 pp_args)
154     }
155
156 -- gaw 2004
157 ppr_expr add_par (Case expr var ty [(con,args,rhs)])
158   = add_par $
159     sep [sep [ptext SLIT("case") <+> parens (ppr ty) <+> pprCoreExpr expr,
160               hsep [ptext SLIT("of"),
161                     ppr_bndr var, 
162                     char '{',
163                     ppr_case_pat con args
164           ]],
165          pprCoreExpr rhs,
166          char '}'
167     ]
168   where
169     ppr_bndr = pprBndr CaseBind
170
171 -- gaw 2004
172 ppr_expr add_par (Case expr var ty alts)
173   = add_par $
174     sep [sep [ptext SLIT("case") <+> parens (ppr ty) <+> pprCoreExpr expr,
175               ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
176          nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
177          char '}'
178     ]
179   where
180     ppr_bndr = pprBndr CaseBind
181  
182
183 -- special cases: let ... in let ...
184 -- ("disgusting" SLPJ)
185
186 {-
187 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
188   = add_par $
189     vcat [
190       hsep [ptext SLIT("let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
191       nest 2 (pprCoreExpr rhs),
192       ptext SLIT("} in"),
193       pprCoreExpr body ]
194 -}
195
196 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
197   = add_par
198     (hang (ptext SLIT("let {"))
199           2 (hsep [ppr_binding (val_bdr,rhs),
200                    ptext SLIT("} in")])
201      $$
202      pprCoreExpr expr)
203
204 -- general case (recursive case, too)
205 ppr_expr add_par (Let bind expr)
206   = add_par $
207     sep [hang (ptext keyword) 2 (ppr_bind bind),
208          hang (ptext SLIT("} in ")) 2 (pprCoreExpr expr)]
209   where
210     keyword = case bind of
211                 Rec _      -> SLIT("__letrec {")
212                 NonRec _ _ -> SLIT("let {")
213
214 ppr_expr add_par (Note (SCC cc) expr)
215   = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
216
217 #ifdef DEBUG
218 ppr_expr add_par (Note (Coerce to_ty from_ty) expr)
219  = add_par $
220    getPprStyle $ \ sty ->
221    if debugStyle sty then
222       sep [ptext SLIT("__coerce") <+> 
223                 sep [pprParendType to_ty, pprParendType from_ty],
224            pprParendExpr expr]
225    else
226       sep [hsep [ptext SLIT("__coerce"), pprParendType to_ty],
227                   pprParendExpr expr]
228 #else
229 ppr_expr add_par (Note (Coerce to_ty from_ty) expr)
230   = add_par $
231     sep [sep [ptext SLIT("__coerce"), nest 2 (pprParendType to_ty)],
232          pprParendExpr expr]
233 #endif
234
235 ppr_expr add_par (Note InlineCall expr)
236   = add_par (ptext SLIT("__inline_call") <+> pprParendExpr expr)
237
238 ppr_expr add_par (Note InlineMe expr)
239   = add_par $ ptext SLIT("__inline_me") <+> pprParendExpr expr
240
241 ppr_expr add_par (Note (CoreNote s) expr)
242   = add_par $ 
243     sep [sep [ptext SLIT("__core_note"), pprHsString (mkFastString s)],
244          pprParendExpr expr]
245
246 pprCoreAlt (con, args, rhs) 
247   = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
248
249 ppr_case_pat con@(DataAlt dc) args
250   | isTupleTyCon tc
251   = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
252   where
253     ppr_bndr = pprBndr CaseBind
254     tc = dataConTyCon dc
255
256 ppr_case_pat con args
257   = ppr con <+> hsep (map ppr_bndr args) <+> arrow
258   where
259     ppr_bndr = pprBndr CaseBind
260
261 pprArg (Type ty) = ptext SLIT("@") <+> pprParendType ty
262 pprArg expr      = pprParendExpr expr
263 \end{code}
264
265 Other printing bits-and-bobs used with the general @pprCoreBinding@
266 and @pprCoreExpr@ functions.
267
268 \begin{code}
269 instance OutputableBndr Var where
270   pprBndr = pprCoreBinder
271
272 pprCoreBinder :: BindingSite -> Var -> SDoc
273 pprCoreBinder LetBind binder
274   = vcat [sig, pprIdDetails binder, pragmas]
275   where
276     sig     = pprTypedBinder binder
277     pragmas = ppIdInfo binder (idInfo binder)
278
279 -- Lambda bound type variables are preceded by "@"
280 pprCoreBinder LambdaBind bndr = parens (pprTypedBinder bndr)
281
282 -- Case bound things don't get a signature or a herald
283 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
284
285 pprUntypedBinder binder
286   | isTyVar binder = ptext SLIT("@") <+> ppr binder     -- NB: don't print kind
287   | otherwise      = pprIdBndr binder
288
289 pprTypedBinder binder
290   | isTyVar binder  = ptext SLIT("@") <+> pprTyVarBndr binder
291   | otherwise       = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
292
293 pprTyVarBndr :: TyVar -> SDoc
294 pprTyVarBndr tyvar
295   = getPprStyle $ \ sty ->
296     if debugStyle sty then
297         hsep [ppr tyvar, dcolon, pprParendKind kind]
298                 -- See comments with ppDcolon in PprCore.lhs
299     else
300         ppr tyvar
301   where
302     kind = tyVarKind tyvar
303
304 -- pprIdBndr does *not* print the type
305 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
306 pprIdBndr id = ppr id <+> 
307                (megaSeqIdInfo (idInfo id) `seq`
308                         -- Useful for poking on black holes
309                 ifPprDebug (ppr (idInlinePragma id) <+> ppr (idOccInfo id) <+> 
310 #ifdef OLD_STRICTNESS
311                             ppr (idDemandInfo id) <+>
312 #endif
313                             ppr (idNewDemandInfo id) <+>
314                             ppr (idLBVarInfo id)))
315 \end{code}
316
317
318 \begin{code}
319 pprIdDetails :: Id -> SDoc
320 pprIdDetails id | isGlobalId id     = ppr (globalIdDetails id)
321                 | isExportedId id   = ptext SLIT("[Exported]")
322                 | isSpecPragmaId id = ptext SLIT("[SpecPrag]")
323                 | otherwise         = empty
324
325 ppIdInfo :: Id -> IdInfo -> SDoc
326 ppIdInfo b info
327   = hsep [  ppArityInfo a,
328             ppWorkerInfo (workerInfo info),
329             ppCafInfo (cafInfo info),
330 #ifdef OLD_STRICTNESS
331             ppStrictnessInfo s,
332             ppCprInfo m,
333 #endif
334             pprNewStrictness (newStrictnessInfo info),
335             vcat (map (pprCoreRule (ppr b)) (rulesRules p))
336         -- Inline pragma, occ, demand, lbvar info
337         -- printed out with all binders (when debug is on); 
338         -- see PprCore.pprIdBndr
339         ]
340   where
341     a = arityInfo info
342 #ifdef OLD_STRICTNESS
343     s = strictnessInfo info
344     m = cprInfo info
345 #endif
346     p = specInfo info
347 \end{code}
348
349
350 \begin{code}
351 pprIdRules :: [IdCoreRule] -> SDoc
352 pprIdRules rules = vcat (map pprIdRule rules)
353
354 pprIdRule :: IdCoreRule -> SDoc
355 pprIdRule (IdCoreRule id _ rule) = pprCoreRule (ppr id) rule
356
357 pprCoreRule :: SDoc -> CoreRule -> SDoc
358 pprCoreRule pp_fn (BuiltinRule name _)
359   = ptext SLIT("Built in rule for") <+> pp_fn <> colon <+> doubleQuotes (ftext name)
360
361 pprCoreRule pp_fn (Rule name act tpl_vars tpl_args rhs)
362   = doubleQuotes (ftext name) <+> ppr act <+>
363     sep [
364           ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
365           nest 2 (pp_fn <+> sep (map pprArg tpl_args)),
366           nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)
367     ] <+> semi
368 \end{code}