PprCore.lhs 10.3 KB
Newer Older
1
%
2
% (c) The AQUA Project, Glasgow University, 1996-1998
3
4
5
6
7
8
9
10
11
%
%************************************************************************
%*									*
\section[PprCore]{Printing of Core syntax, including for interfaces}
%*									*
%************************************************************************

\begin{code}
module PprCore (
12
	pprCoreExpr, pprParendExpr,
13
	pprCoreBinding, pprCoreBindings, pprCoreAlt,
14
	pprRules
15
16
    ) where

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
24
			  idInfo, idInlinePragma, idOccInfo,
			  globalIdDetails, isGlobalId, isExportedId, 
25
			  idNewDemandInfo
26
			)
27
import Var		( TyVar, isTyVar, tyVarKind )
28
import IdInfo		( IdInfo, megaSeqIdInfo, 
29
			  arityInfo, ppArityInfo, 
30
			  specInfo, pprNewStrictness,
31
			  workerInfo, ppWorkerInfo,
32
			  newStrictnessInfo, cafInfo, ppCafInfo, specInfoRules
33
34
			)

35
#ifdef OLD_STRICTNESS
36
37
import Id		( idDemandInfo )
import IdInfo		( cprInfo, ppCprInfo, strictnessInfo, ppStrictnessInfo ) 
38
#endif
39

40
41
import DataCon		( dataConTyCon )
import TyCon		( tupleTyConBoxity, isTupleTyCon )
42
import Type		( pprParendType, pprType, pprParendKind )
43
import BasicTypes	( tupleParens )
sof's avatar
sof committed
44
import Util             ( lengthIs )
45
import Outputable
46
import FastString       ( mkFastString )
47
48
49
50
51
52
53
54
55
56
57
\end{code}

%************************************************************************
%*									*
\subsection{Public interfaces for Core printing (excluding instances)}
%*									*
%************************************************************************

@pprParendCoreExpr@ puts parens around non-atomic Core expressions.

\begin{code}
58
59
60
61
pprCoreBindings :: OutputableBndr b => [Bind b] -> SDoc
pprCoreBinding  :: OutputableBndr b => Bind b  -> SDoc
pprCoreExpr     :: OutputableBndr b => Expr b  -> SDoc
pprParendExpr   :: OutputableBndr b => Expr b  -> SDoc
62

63
64
pprCoreBindings = pprTopBinds
pprCoreBinding  = pprTopBind 
65

66
67
instance OutputableBndr b => Outputable (Bind b) where
    ppr bind = ppr_bind bind
68

69
70
instance OutputableBndr b => Outputable (Expr b) where
    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
84
pprTopBind (NonRec binder expr)
 = ppr_binding (binder,expr) $$ text ""
85

86
pprTopBind (Rec binds)
87
  = vcat [ptext SLIT("Rec {"),
88
	  vcat (map ppr_binding binds),
89
90
	  ptext SLIT("end Rec }"),
	  text ""]
91
92
93
\end{code}

\begin{code}
94
ppr_bind :: OutputableBndr b => Bind b -> SDoc
95

96
97
98
99
ppr_bind (NonRec val_bdr expr) = ppr_binding (val_bdr, expr)
ppr_bind (Rec binds)  	       = vcat (map pp binds)
			       where
				 pp bind = ppr_binding bind <> semi
100

101
102
103
ppr_binding :: OutputableBndr b => (b, Expr b) -> SDoc
ppr_binding (val_bdr, expr)
  = pprBndr LetBind val_bdr $$ 
104
    hang (ppr val_bdr <+> equals) 2 (pprCoreExpr expr)
105
106
107
\end{code}

\begin{code}
108
109
pprParendExpr   expr = ppr_expr parens expr
pprCoreExpr expr = ppr_expr noParens expr
110
111
112

noParens :: SDoc -> SDoc
noParens pp = pp
113
114
115
\end{code}

\begin{code}
116
ppr_expr :: OutputableBndr b => (SDoc -> SDoc) -> Expr b -> SDoc
117
118
	-- The function adds parens in context that need
	-- 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
123
ppr_expr add_par (Var name) = ppr name
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
131
    hang (ptext SLIT("\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
	 2 (pprCoreExpr body)
132

133
ppr_expr add_par expr@(App fun arg)
134
135
  = case collectArgs expr of { (fun, args) -> 
    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
143
			-- Notice that we print the *worker*
			-- for tuples in paren'd format.
144
145
146
147
		   Just dc | saturated && isTupleTyCon tc
			   -> tupleParens (tupleTyConBoxity tc) pp_tup_args
			   where
			     tc	       = dataConTyCon dc
sof's avatar
sof committed
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 ty [(con,args,rhs)])
156
  = add_par $
157
158
    sep [sep [ptext SLIT("case") {- <+> pprParendType ty -} <+> pprCoreExpr expr,
	-- Printing the result type is excessive!
159
	      hsep [ptext SLIT("of"),
160
		    ppr_bndr var, 
161
		    char '{',
162
		    ppr_case_pat con args
163
	  ]],
164
	 pprCoreExpr rhs,
165
166
	 char '}'
    ]
167
  where
168
    ppr_bndr = pprBndr CaseBind
169

170
ppr_expr add_par (Case expr var ty alts)
171
  = add_par $
172
    sep [sep [ptext SLIT("case") {- <+> pprParendType ty -} <+> pprCoreExpr expr,
173
	      ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
174
	 nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
175
176
177
	 char '}'
    ]
  where
178
    ppr_bndr = pprBndr CaseBind
179
 
180
181
182
183

-- special cases: let ... in let ...
-- ("disgusting" SLPJ)

184
185
{-
ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
186
187
  = add_par $
    vcat [
188
189
      hsep [ptext SLIT("let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
      nest 2 (pprCoreExpr rhs),
sof's avatar
sof committed
190
      ptext SLIT("} in"),
191
192
      pprCoreExpr body ]
-}
193

194
ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
195
196
  = add_par
    (hang (ptext SLIT("let {"))
197
198
	  2 (hsep [ppr_binding (val_bdr,rhs),
		   ptext SLIT("} in")])
199
     $$
200
     pprCoreExpr expr)
201
202

-- general case (recursive case, too)
203
ppr_expr add_par (Let bind expr)
204
  = add_par $
205
206
    sep [hang (ptext keyword) 2 (ppr_bind bind),
	 hang (ptext SLIT("} in ")) 2 (pprCoreExpr expr)]
207
208
  where
    keyword = case bind of
209
		Rec _      -> SLIT("__letrec {")
210
		NonRec _ _ -> SLIT("let {")
211

212
213
ppr_expr add_par (Note (SCC cc) expr)
  = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
214

sof's avatar
sof committed
215
#ifdef DEBUG
216
ppr_expr add_par (Note (Coerce to_ty from_ty) expr)
217
218
 = add_par $
   getPprStyle $ \ sty ->
219
   if debugStyle sty then
220
221
222
      sep [ptext SLIT("__coerce") <+> 
		sep [pprParendType to_ty, pprParendType from_ty],
	   pprParendExpr expr]
sof's avatar
sof committed
223
   else
224
225
      sep [hsep [ptext SLIT("__coerce"), pprParendType to_ty],
	          pprParendExpr expr]
sof's avatar
sof committed
226
#else
227
ppr_expr add_par (Note (Coerce to_ty from_ty) expr)
228
  = add_par $
229
230
    sep [sep [ptext SLIT("__coerce"), nest 2 (pprParendType to_ty)],
	 pprParendExpr expr]
sof's avatar
sof committed
231
#endif
232

233
234
ppr_expr add_par (Note InlineCall expr)
  = add_par (ptext SLIT("__inline_call") <+> pprParendExpr expr)
235

236
237
ppr_expr add_par (Note InlineMe expr)
  = add_par $ ptext SLIT("__inline_me") <+> pprParendExpr expr
238

239
240
241
242
243
ppr_expr add_par (Note (CoreNote s) expr)
  = add_par $ 
    sep [sep [ptext SLIT("__core_note"), pprHsString (mkFastString s)],
         pprParendExpr expr]

244
245
pprCoreAlt (con, args, rhs) 
  = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
246

247
ppr_case_pat con@(DataAlt dc) args
248
249
  | isTupleTyCon tc
  = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
250
  where
251
    ppr_bndr = pprBndr CaseBind
252
    tc = dataConTyCon dc
253

254
ppr_case_pat con args
255
  = ppr con <+> hsep (map ppr_bndr args) <+> arrow
256
  where
257
    ppr_bndr = pprBndr CaseBind
258

259
260
pprArg (Type ty) = ptext SLIT("@") <+> pprParendType ty
pprArg expr      = pprParendExpr expr
261
262
263
264
265
266
\end{code}

Other printing bits-and-bobs used with the general @pprCoreBinding@
and @pprCoreExpr@ functions.

\begin{code}
267
268
269
270
instance OutputableBndr Var where
  pprBndr = pprCoreBinder

pprCoreBinder :: BindingSite -> Var -> SDoc
271
pprCoreBinder LetBind binder
272
  = vcat [sig, pprIdDetails binder, pragmas]
273
  where
274
    sig     = pprTypedBinder binder
275
    pragmas = ppIdInfo binder (idInfo binder)
276

277
-- Lambda bound type variables are preceded by "@"
278
pprCoreBinder LambdaBind bndr = parens (pprTypedBinder bndr)
279

280
281
-- Case bound things don't get a signature or a herald
pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
282

283
pprUntypedBinder binder
284
  | isTyVar binder = ptext SLIT("@") <+> ppr binder	-- NB: don't print kind
285
286
  | otherwise      = pprIdBndr binder

287
pprTypedBinder binder
288
  | isTyVar binder  = ptext SLIT("@") <+> pprTyVarBndr binder
289
  | otherwise	    = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
290

291
292
293
294
295
296
297
298
299
300
301
pprTyVarBndr :: TyVar -> SDoc
pprTyVarBndr tyvar
  = getPprStyle $ \ sty ->
    if debugStyle sty then
        hsep [ppr tyvar, dcolon, pprParendKind kind]
		-- See comments with ppDcolon in PprCore.lhs
    else
        ppr tyvar
  where
    kind = tyVarKind tyvar

302
-- pprIdBndr does *not* print the type
303
-- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
304
305
306
pprIdBndr id = ppr id <+> 
	       (megaSeqIdInfo (idInfo id) `seq`
			-- Useful for poking on black holes
307
	        brackets (ppr (idInlinePragma id) <+> ppr (idOccInfo id) <+> 
308
#ifdef OLD_STRICTNESS
309
310
311
312
			    ppr (idDemandInfo id) <+>
#endif
			    ppr (idNewDemandInfo id) <+>
			    ppr (idLBVarInfo id)))
313
\end{code}
314
315
316


\begin{code}
317
318
319
320
321
pprIdDetails :: Id -> SDoc
pprIdDetails id | isGlobalId id     = ppr (globalIdDetails id)
		| isExportedId id   = ptext SLIT("[Exported]")
		| otherwise	    = empty

322
323
ppIdInfo :: Id -> IdInfo -> SDoc
ppIdInfo b info
324
325
  = brackets $
    vcat [  ppArityInfo a,
326
	    ppWorkerInfo (workerInfo info),
327
	    ppCafInfo (cafInfo info),
328
#ifdef OLD_STRICTNESS
329
	    ppStrictnessInfo s,
330
            ppCprInfo m,
331
#endif
332
	    pprNewStrictness (newStrictnessInfo info),
333
	    if null rules then empty
334
	    else ptext SLIT("RULES:") <+> vcat (map pprRule rules)
335
336
337
	-- Inline pragma, occ, demand, lbvar info
	-- printed out with all binders (when debug is on); 
	-- see PprCore.pprIdBndr
338
339
340
	]
  where
    a = arityInfo info
341
#ifdef OLD_STRICTNESS
342
    s = strictnessInfo info
343
    m = cprInfo info
344
#endif
345
    rules = specInfoRules (specInfo info)
346
347
\end{code}

348

349
\begin{code}
350
351
instance Outputable CoreRule where
   ppr = pprRule
352

353
354
pprRules :: [CoreRule] -> SDoc
pprRules rules = vcat (map pprRule rules)
355

356
357
358
pprRule :: CoreRule -> SDoc
pprRule (BuiltinRule { ru_fn = fn, ru_name = name})
  = ptext SLIT("Built in rule for") <+> ppr fn <> colon <+> doubleQuotes (ftext name)
359

360
361
362
pprRule (Rule { ru_name = name, ru_act = act, ru_fn = fn,
		ru_bndrs = tpl_vars, ru_args = tpl_args,
		ru_rhs = rhs })
363
  = doubleQuotes (ftext name) <+> ppr act <+>
364
365
    sep [
	  ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
366
	  nest 2 (ppr fn <+> sep (map pprArg tpl_args)),
chak's avatar
chak committed
367
	  nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)
368
    ] <+> semi
369
\end{code}