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