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