[project @ 2001-03-03 02:50:04 by chak]
[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, pprIdBndr,
14         pprCoreBinding, pprCoreBindings, pprCoreAlt,
15         pprCoreRules, pprCoreRule, pprIdCoreRule
16     ) where
17
18 #include "HsVersions.h"
19
20 import CoreSyn
21 import CostCentre       ( pprCostCentreCore )
22 import Id               ( Id, idType, isDataConId_maybe, idLBVarInfo, idArity,
23                           idInfo, idInlinePragma, idDemandInfo, idOccInfo
24                         )
25 import Var              ( isTyVar )
26 import IdInfo           ( IdInfo, megaSeqIdInfo, 
27                           arityInfo, ppArityInfo, ppFlavourInfo, flavourInfo,
28                           specInfo, cprInfo, ppCprInfo, 
29                           strictnessInfo, ppStrictnessInfo, cafInfo, ppCafInfo,
30                           cprInfo, ppCprInfo, 
31                           workerInfo, ppWorkerInfo,
32                           tyGenInfo, ppTyGenInfo
33                         )
34 import DataCon          ( dataConTyCon )
35 import TyCon            ( tupleTyConBoxity, isTupleTyCon )
36 import PprType          ( pprParendType, pprTyVarBndr )
37 import BasicTypes       ( tupleParens )
38 import PprEnv
39 import Outputable
40 \end{code}
41
42 %************************************************************************
43 %*                                                                      *
44 \subsection{Public interfaces for Core printing (excluding instances)}
45 %*                                                                      *
46 %************************************************************************
47
48 @pprCoreBinding@ and @pprCoreExpr@ let you give special printing
49 function for ``major'' val_bdrs (those next to equal signs :-),
50 ``minor'' ones (lambda-bound, case-bound), and bindees.  They would
51 usually be called through some intermediary.
52
53 The binder/occ printers take the default ``homogenized'' (see
54 @PprEnv@...) @Doc@ and the binder/occ.  They can either use the
55 homogenized one, or they can ignore it completely.  In other words,
56 the things passed in act as ``hooks'', getting the last word on how to
57 print something.
58
59 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
60
61 Un-annotated core dumps
62 ~~~~~~~~~~~~~~~~~~~~~~~
63 \begin{code}
64 pprCoreBindings :: [CoreBind] -> SDoc
65 pprCoreBinding  :: CoreBind   -> SDoc
66 pprCoreExpr     :: CoreExpr   -> SDoc
67 pprParendExpr   :: CoreExpr   -> SDoc
68
69 pprCoreBindings = pprTopBinds pprCoreEnv
70 pprCoreBinding  = pprTopBind pprCoreEnv
71 pprCoreExpr     = ppr_noparend_expr pprCoreEnv
72 pprParendExpr   = ppr_parend_expr   pprCoreEnv
73 pprArg          = ppr_arg pprCoreEnv
74 pprCoreAlt      = ppr_alt pprCoreEnv
75
76 pprCoreEnv = initCoreEnv pprCoreBinder
77 \end{code}
78
79 Printer for unfoldings in interfaces
80 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81 \begin{code}
82 instance Outputable b => Outputable (Bind b) where
83     ppr bind = ppr_bind pprGenericEnv bind
84
85 instance Outputable b => Outputable (Expr b) where
86     ppr expr = ppr_noparend_expr pprGenericEnv expr
87
88 pprGenericEnv :: Outputable b => PprEnv b
89 pprGenericEnv = initCoreEnv (\site -> ppr)
90 \end{code}
91
92 %************************************************************************
93 %*                                                                      *
94 \subsection{Instance declarations for Core printing}
95 %*                                                                      *
96 %************************************************************************
97
98
99 \begin{code}
100 initCoreEnv pbdr
101   = initPprEnv
102         (Just pprCostCentreCore)        -- Cost centres
103
104         (Just ppr)              -- tyvar occs
105         (Just pprParendType)    -- types
106
107         (Just pbdr) (Just ppr) -- value vars
108         -- Use pprIdBndr for this last one as a debugging device.
109 \end{code}
110
111 %************************************************************************
112 %*                                                                      *
113 \subsection{The guts}
114 %*                                                                      *
115 %************************************************************************
116
117 \begin{code}
118 pprTopBinds pe binds = vcat (map (pprTopBind pe) binds)
119
120 pprTopBind pe (NonRec binder expr)
121  = ppr_binding_pe pe (binder,expr) $$ text ""
122
123 pprTopBind pe (Rec binds)
124   = vcat [ptext SLIT("Rec {"),
125           vcat (map (ppr_binding_pe pe) binds),
126           ptext SLIT("end Rec }"),
127           text ""]
128 \end{code}
129
130 \begin{code}
131 ppr_bind :: PprEnv b -> Bind b -> SDoc
132
133 ppr_bind pe (NonRec val_bdr expr) = ppr_binding_pe pe (val_bdr, expr)
134 ppr_bind pe (Rec binds)           = vcat (map pp binds)
135                                   where
136                                     pp bind = ppr_binding_pe pe bind <> semi
137
138 ppr_binding_pe :: PprEnv b -> (b, Expr b) -> SDoc
139 ppr_binding_pe pe (val_bdr, expr)
140   = sep [pBndr pe LetBind val_bdr, 
141          nest 2 (equals <+> ppr_noparend_expr pe expr)]
142 \end{code}
143
144 \begin{code}
145 ppr_parend_expr   pe expr = ppr_expr parens pe expr
146 ppr_noparend_expr pe expr = ppr_expr noParens pe expr
147
148 noParens :: SDoc -> SDoc
149 noParens pp = pp
150 \end{code}
151
152 \begin{code}
153 ppr_expr :: (SDoc -> SDoc) -> PprEnv b -> Expr b -> SDoc
154         -- The function adds parens in context that need
155         -- an atomic value (e.g. function args)
156
157 ppr_expr add_par pe (Type ty)  = add_par (ptext SLIT("TYPE") <+> ppr ty)        -- Wierd
158                    
159 ppr_expr add_par pe (Var name) = pOcc pe name
160 ppr_expr add_par pe (Lit lit)  = ppr lit
161
162 ppr_expr add_par pe expr@(Lam _ _)
163   = let
164         (bndrs, body) = collectBinders expr
165     in
166     add_par $
167     hang (ptext SLIT("\\") <+> sep (map (pBndr pe LambdaBind) bndrs) <+> arrow)
168          2 (ppr_noparend_expr pe body)
169
170 ppr_expr add_par pe expr@(App fun arg)
171   = case collectArgs expr of { (fun, args) -> 
172     let
173         pp_args     = sep (map (ppr_arg pe) args)
174         val_args    = dropWhile isTypeArg args   -- Drop the type arguments for tuples
175         pp_tup_args = sep (punctuate comma (map (ppr_arg pe) val_args))
176     in
177     case fun of
178         Var f -> case isDataConId_maybe f of
179                         -- Notice that we print the *worker*
180                         -- for tuples in paren'd format.
181                    Just dc | saturated && isTupleTyCon tc
182                            -> tupleParens (tupleTyConBoxity tc) pp_tup_args
183                            where
184                              tc        = dataConTyCon dc
185                              saturated = length val_args == idArity f
186
187                    other -> add_par (hang (pOcc pe f) 2 pp_args)
188
189         other -> add_par (hang (ppr_parend_expr pe fun) 2 pp_args)
190     }
191
192 ppr_expr add_par pe (Case expr var [(con,args,rhs)])
193   = add_par $
194     sep [sep [ptext SLIT("case") <+> ppr_noparend_expr pe expr,
195               hsep [ptext SLIT("of"),
196                     ppr_bndr var,
197                     char '{',
198                     ppr_case_pat pe con args
199           ]],
200          ppr_noparend_expr pe rhs,
201          char '}'
202     ]
203   where
204     ppr_bndr = pBndr pe CaseBind
205
206 ppr_expr add_par pe (Case expr var alts)
207   = add_par $
208     sep [sep [ptext SLIT("case") <+> ppr_noparend_expr pe expr,
209               ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
210          nest 2 (sep (punctuate semi (map (ppr_alt pe) alts))),
211          char '}'
212     ]
213   where
214     ppr_bndr = pBndr pe CaseBind
215  
216
217 -- special cases: let ... in let ...
218 -- ("disgusting" SLPJ)
219
220 ppr_expr add_par pe (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
221   = add_par $
222     vcat [
223       hsep [ptext SLIT("let {"), pBndr pe LetBind val_bdr, equals],
224       nest 2 (ppr_noparend_expr pe rhs),
225       ptext SLIT("} in"),
226       ppr_noparend_expr pe body ]
227
228 ppr_expr add_par pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
229   = add_par
230     (hang (ptext SLIT("let {"))
231           2 (hsep [hang (hsep [pBndr pe LetBind val_bdr, equals])
232                            2 (ppr_noparend_expr pe rhs),
233        ptext SLIT("} in")])
234      $$
235      ppr_noparend_expr pe expr)
236
237 -- general case (recursive case, too)
238 ppr_expr add_par pe (Let bind expr)
239   = add_par $
240     sep [hang (ptext keyword) 2 (ppr_bind pe bind),
241          hang (ptext SLIT("} in ")) 2 (ppr_noparend_expr pe expr)]
242   where
243     keyword = case bind of
244                 Rec _      -> SLIT("__letrec {")
245                 NonRec _ _ -> SLIT("let {")
246
247 ppr_expr add_par pe (Note (SCC cc) expr)
248   = add_par (sep [pSCC pe cc, ppr_noparend_expr pe expr])
249
250 #ifdef DEBUG
251 ppr_expr add_par pe (Note (Coerce to_ty from_ty) expr)
252  = add_par $
253    getPprStyle $ \ sty ->
254    if debugStyle sty && not (ifaceStyle sty) then
255       sep [ptext SLIT("__coerce") <+> sep [pTy pe to_ty, pTy pe from_ty],
256            ppr_parend_expr pe expr]
257    else
258       sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty],
259                   ppr_parend_expr pe expr]
260 #else
261 ppr_expr add_par pe (Note (Coerce to_ty from_ty) expr)
262   = add_par $
263     sep [sep [ptext SLIT("__coerce"), nest 2 (pTy pe to_ty)],
264          ppr_parend_expr pe expr]
265 #endif
266
267 ppr_expr add_par pe (Note InlineCall expr)
268   = add_par (ptext SLIT("__inline_call") <+> ppr_parend_expr pe expr)
269
270 ppr_expr add_par pe (Note InlineMe expr)
271   = add_par $ ptext SLIT("__inline_me") <+> ppr_parend_expr pe expr
272
273 ppr_alt pe (con, args, rhs) 
274   = hang (ppr_case_pat pe con args) 2 (ppr_noparend_expr pe rhs)
275
276 ppr_case_pat pe con@(DataAlt dc) args
277   | isTupleTyCon tc
278   = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
279   where
280     ppr_bndr = pBndr pe CaseBind
281     tc = dataConTyCon dc
282
283 ppr_case_pat pe con args
284   = ppr con <+> hsep (map ppr_bndr args) <+> arrow
285   where
286     ppr_bndr = pBndr pe CaseBind
287
288 ppr_arg pe (Type ty) = ptext SLIT("@") <+> pTy pe ty
289 ppr_arg pe expr      = ppr_parend_expr pe expr
290
291 arrow = ptext SLIT("->")
292 \end{code}
293
294 Other printing bits-and-bobs used with the general @pprCoreBinding@
295 and @pprCoreExpr@ functions.
296
297 \begin{code}
298 -- Used for printing dump info
299 pprCoreBinder LetBind binder
300   = vcat [sig, pragmas, ppr binder]
301   where
302     sig     = pprTypedBinder binder
303     pragmas = ppIdInfo binder (idInfo binder)
304
305 -- Lambda bound type variables are preceded by "@"
306 pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
307
308 -- Case bound things don't get a signature or a herald
309 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
310
311 pprUntypedBinder binder
312   | isTyVar binder = ptext SLIT("@") <+> ppr binder     -- NB: don't print kind
313   | otherwise      = pprIdBndr binder
314
315 pprTypedBinder binder
316   | isTyVar binder  = ptext SLIT("@") <+> pprTyVarBndr binder
317   | otherwise       = pprIdBndr binder <+> dcolon <+> pprParendType (idType binder)
318         -- The space before the :: is important; it helps the lexer
319         -- when reading inferfaces.  Otherwise it would lex "a::b" as one thing.
320         --
321         -- It's important that the type is parenthesised too, at least when
322         -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
323
324 -- pprIdBndr does *not* print the type
325 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
326 pprIdBndr id = ppr id <+> 
327                (megaSeqIdInfo (idInfo id) `seq`
328                         -- Useful for poking on black holes
329                 ifPprDebug (ppr (idInlinePragma id) <+> ppr (idOccInfo id) <+> 
330                             ppr (idDemandInfo id)) <+> ppr (idLBVarInfo id))
331 \end{code}
332
333
334 \begin{code}
335 ppIdInfo :: Id -> IdInfo -> SDoc
336 ppIdInfo b info
337   = hsep [
338             ppFlavourInfo (flavourInfo info),
339             ppArityInfo a,
340             ppTyGenInfo g,
341             ppWorkerInfo (workerInfo info),
342             ppStrictnessInfo s,
343             ppCafInfo c,
344             ppCprInfo m,
345             pprCoreRules b p
346         -- Inline pragma, occ, demand, lbvar info
347         -- printed out with all binders (when debug is on); 
348         -- see PprCore.pprIdBndr
349         ]
350   where
351     a = arityInfo info
352     g = tyGenInfo info
353     s = strictnessInfo info
354     c = cafInfo info
355     m = cprInfo info
356     p = specInfo info
357 \end{code}
358
359
360 \begin{code}
361 pprCoreRules :: Id -> CoreRules -> SDoc
362 pprCoreRules var (Rules rules _) = vcat (map (pprCoreRule (ppr var)) rules)
363
364 pprIdCoreRule :: IdCoreRule -> SDoc
365 pprIdCoreRule (id,rule) = pprCoreRule (ppr id) rule
366
367 pprCoreRule :: SDoc -> CoreRule -> SDoc
368 pprCoreRule pp_fn (BuiltinRule _)
369   = ifPprDebug (ptext SLIT("A built in rule"))
370
371 pprCoreRule pp_fn (Rule name tpl_vars tpl_args rhs)
372   = doubleQuotes (ptext name) <+> 
373     sep [
374           ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
375           nest 2 (pp_fn <+> sep (map pprArg tpl_args)),
376           nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)
377     ] <+> semi
378 \end{code}