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