[project @ 2003-03-03 12:43:31 by simonmar]
[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, pprIdBndr,
13         pprCoreBinding, pprCoreBindings, pprCoreAlt,
14         pprIdRules, pprCoreRule
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              ( isTyVar )
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 PprType          ( pprParendType, pprType, pprTyVarBndr )
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 ppr_expr add_par (Case expr var [(con,args,rhs)])
157   = add_par $
158     sep [sep [ptext SLIT("case") <+> pprCoreExpr expr,
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 alts)
171   = add_par $
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))),
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
262 arrow = ptext SLIT("->")
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 = 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         -- The space before the :: is important; it helps the lexer
293         -- when reading inferfaces.  Otherwise it would lex "a::b" as one thing.
294         --
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]
298
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) <+>
307 #endif
308                             ppr (idNewDemandInfo id) <+>
309                             ppr (idLBVarInfo id)))
310 \end{code}
311
312
313 \begin{code}
314 pprIdDetails :: Id -> SDoc
315 pprIdDetails id | isGlobalId id     = ppr (globalIdDetails id)
316                 | isExportedId id   = ptext SLIT("[Exported]")
317                 | isSpecPragmaId id = ptext SLIT("[SpecPrag]")
318                 | otherwise         = empty
319
320 ppIdInfo :: Id -> IdInfo -> SDoc
321 ppIdInfo b info
322   = hsep [  ppArityInfo a,
323             ppWorkerInfo (workerInfo info),
324             ppCafInfo (cafInfo info),
325 #ifdef OLD_STRICTNESS
326             ppStrictnessInfo s,
327             ppCprInfo m,
328 #endif
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
334         ]
335   where
336     a = arityInfo info
337 #ifdef OLD_STRICTNESS
338     s = strictnessInfo info
339     m = cprInfo info
340 #endif
341     p = specInfo info
342 \end{code}
343
344
345 \begin{code}
346 pprIdRules :: [IdCoreRule] -> SDoc
347 pprIdRules rules = vcat (map pprIdRule rules)
348
349 pprIdRule :: IdCoreRule -> SDoc
350 pprIdRule (id,rule) = pprCoreRule (ppr id) rule
351
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))
356
357 pprCoreRule pp_fn (Rule name act tpl_vars tpl_args rhs)
358   = doubleQuotes (ftext name) <+> ppr act <+>
359     sep [
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)
363     ] <+> semi
364 \end{code}