[project @ 2003-02-12 15:01:31 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, 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,
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 \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     (ppr val_bdr <+> equals <+> 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 [(con,args,rhs)])
156   = add_par $
157     sep [sep [ptext SLIT("case") <+> pprCoreExpr expr,
158               hsep [ptext SLIT("of"),
159                     ppr_bndr var,
160                     char '{',
161                     ppr_case_pat con args
162           ]],
163          pprCoreExpr rhs,
164          char '}'
165     ]
166   where
167     ppr_bndr = pprBndr CaseBind
168
169 ppr_expr add_par (Case expr var alts)
170   = add_par $
171     sep [sep [ptext SLIT("case") <+> pprCoreExpr expr,
172               ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
173          nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
174          char '}'
175     ]
176   where
177     ppr_bndr = pprBndr CaseBind
178  
179
180 -- special cases: let ... in let ...
181 -- ("disgusting" SLPJ)
182
183 {-
184 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
185   = add_par $
186     vcat [
187       hsep [ptext SLIT("let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
188       nest 2 (pprCoreExpr rhs),
189       ptext SLIT("} in"),
190       pprCoreExpr body ]
191 -}
192
193 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
194   = add_par
195     (hang (ptext SLIT("let {"))
196           2 (hsep [ppr_binding (val_bdr,rhs),
197                    ptext SLIT("} in")])
198      $$
199      pprCoreExpr expr)
200
201 -- general case (recursive case, too)
202 ppr_expr add_par (Let bind expr)
203   = add_par $
204     sep [hang (ptext keyword) 2 (ppr_bind bind),
205          hang (ptext SLIT("} in ")) 2 (pprCoreExpr expr)]
206   where
207     keyword = case bind of
208                 Rec _      -> SLIT("__letrec {")
209                 NonRec _ _ -> SLIT("let {")
210
211 ppr_expr add_par (Note (SCC cc) expr)
212   = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
213
214 #ifdef DEBUG
215 ppr_expr add_par (Note (Coerce to_ty from_ty) expr)
216  = add_par $
217    getPprStyle $ \ sty ->
218    if debugStyle sty then
219       sep [ptext SLIT("__coerce") <+> 
220                 sep [pprParendType to_ty, pprParendType from_ty],
221            pprParendExpr expr]
222    else
223       sep [hsep [ptext SLIT("__coerce"), pprParendType to_ty],
224                   pprParendExpr expr]
225 #else
226 ppr_expr add_par (Note (Coerce to_ty from_ty) expr)
227   = add_par $
228     sep [sep [ptext SLIT("__coerce"), nest 2 (pprParendType to_ty)],
229          pprParendExpr expr]
230 #endif
231
232 ppr_expr add_par (Note InlineCall expr)
233   = add_par (ptext SLIT("__inline_call") <+> pprParendExpr expr)
234
235 ppr_expr add_par (Note InlineMe expr)
236   = add_par $ ptext SLIT("__inline_me") <+> pprParendExpr expr
237
238 pprCoreAlt (con, args, rhs) 
239   = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
240
241 ppr_case_pat con@(DataAlt dc) args
242   | isTupleTyCon tc
243   = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
244   where
245     ppr_bndr = pprBndr CaseBind
246     tc = dataConTyCon dc
247
248 ppr_case_pat con args
249   = ppr con <+> hsep (map ppr_bndr args) <+> arrow
250   where
251     ppr_bndr = pprBndr CaseBind
252
253 pprArg (Type ty) = ptext SLIT("@") <+> pprParendType ty
254 pprArg expr      = pprParendExpr expr
255
256 arrow = ptext SLIT("->")
257 \end{code}
258
259 Other printing bits-and-bobs used with the general @pprCoreBinding@
260 and @pprCoreExpr@ functions.
261
262 \begin{code}
263 instance OutputableBndr Var where
264   pprBndr = pprCoreBinder
265
266 pprCoreBinder :: BindingSite -> Var -> SDoc
267 pprCoreBinder LetBind binder
268   = vcat [sig, pprIdDetails binder, pragmas]
269   where
270     sig     = pprTypedBinder binder
271     pragmas = ppIdInfo binder (idInfo binder)
272
273 -- Lambda bound type variables are preceded by "@"
274 pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
275
276 -- Case bound things don't get a signature or a herald
277 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
278
279 pprUntypedBinder binder
280   | isTyVar binder = ptext SLIT("@") <+> ppr binder     -- NB: don't print kind
281   | otherwise      = pprIdBndr binder
282
283 pprTypedBinder binder
284   | isTyVar binder  = ptext SLIT("@") <+> pprTyVarBndr binder
285   | otherwise       = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
286         -- The space before the :: is important; it helps the lexer
287         -- when reading inferfaces.  Otherwise it would lex "a::b" as one thing.
288         --
289         -- It's important that the type is parenthesised too, at least when
290         -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
291         --      [Jun 2002: interfaces are now binary, so this doesn't matter]
292
293 -- pprIdBndr does *not* print the type
294 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
295 pprIdBndr id = ppr id <+> 
296                (megaSeqIdInfo (idInfo id) `seq`
297                         -- Useful for poking on black holes
298                 ifPprDebug (ppr (idInlinePragma id) <+> ppr (idOccInfo id) <+> 
299 #ifdef OLD_STRICTNESS
300                             ppr (idDemandInfo id) <+>
301 #endif
302                             ppr (idNewDemandInfo id) <+>
303                             ppr (idLBVarInfo id)))
304 \end{code}
305
306
307 \begin{code}
308 pprIdDetails :: Id -> SDoc
309 pprIdDetails id | isGlobalId id     = ppr (globalIdDetails id)
310                 | isExportedId id   = ptext SLIT("[Exported]")
311                 | isSpecPragmaId id = ptext SLIT("[SpecPrag]")
312                 | otherwise         = empty
313
314 ppIdInfo :: Id -> IdInfo -> SDoc
315 ppIdInfo b info
316   = hsep [  ppArityInfo a,
317             ppWorkerInfo (workerInfo info),
318 #ifdef OLD_STRICTNESS
319             ppStrictnessInfo s,
320             ppCprInfo m,
321 #endif
322             pprNewStrictness (newStrictnessInfo info),
323             vcat (map (pprCoreRule (ppr b)) (rulesRules p))
324         -- Inline pragma, occ, demand, lbvar info
325         -- printed out with all binders (when debug is on); 
326         -- see PprCore.pprIdBndr
327         ]
328   where
329     a = arityInfo info
330 #ifdef OLD_STRICTNESS
331     s = strictnessInfo info
332     m = cprInfo info
333 #endif
334     p = specInfo info
335 \end{code}
336
337
338 \begin{code}
339 pprIdRules :: [IdCoreRule] -> SDoc
340 pprIdRules rules = vcat (map pprIdRule rules)
341
342 pprIdRule :: IdCoreRule -> SDoc
343 pprIdRule (id,rule) = pprCoreRule (ppr id) rule
344
345 pprCoreRule :: SDoc -> CoreRule -> SDoc
346 pprCoreRule pp_fn (BuiltinRule name _)
347   = ifPprDebug (ptext SLIT("Built in rule for") <+> pp_fn <> colon
348                  <+> doubleQuotes (ftext name))
349
350 pprCoreRule pp_fn (Rule name act tpl_vars tpl_args rhs)
351   = doubleQuotes (ftext name) <+> ppr act <+>
352     sep [
353           ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
354           nest 2 (pp_fn <+> sep (map pprArg tpl_args)),
355           nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)
356     ] <+> semi
357 \end{code}