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, pprIdBndr,
13
	pprCoreBinding, pprCoreBindings, pprCoreAlt,
14
	pprIdRules, pprCoreRule
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
			  idInfo, idInlinePragma, idOccInfo,
24
#ifdef OLD_STRICTNESS
25 26 27 28
			  idDemandInfo, 
#endif
			  globalIdDetails, isGlobalId, isExportedId, 
			  isSpecPragmaId, idNewDemandInfo
29
			)
30
import Var		( isTyVar )
31
import IdInfo		( IdInfo, megaSeqIdInfo, 
32
			  arityInfo, ppArityInfo, 
33
			  specInfo, pprNewStrictness,
34
			  workerInfo, ppWorkerInfo,
35
			  newStrictnessInfo,
36
#ifdef OLD_STRICTNESS
37
			  cprInfo, ppCprInfo, 
38
			  strictnessInfo, ppStrictnessInfo, 
39
#endif
40
			)
41 42
import DataCon		( dataConTyCon )
import TyCon		( tupleTyConBoxity, isTupleTyCon )
43
import PprType		( pprParendType, pprType, pprTyVarBndr )
44
import BasicTypes	( tupleParens )
sof's avatar
sof committed
45
import Util             ( lengthIs )
46
import Outputable
47
import FastString       ( mkFastString )
48 49 50 51 52 53 54 55 56 57 58
\end{code}

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

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

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

64 65
pprCoreBindings = pprTopBinds
pprCoreBinding  = pprTopBind 
66

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

70 71
instance OutputableBndr b => Outputable (Expr b) where
    ppr expr = pprCoreExpr expr
72
\end{code}
73

74 75 76

%************************************************************************
%*									*
77
\subsection{The guts}
78 79 80 81
%*									*
%************************************************************************

\begin{code}
82
pprTopBinds binds = vcat (map pprTopBind binds)
83

84 85
pprTopBind (NonRec binder expr)
 = ppr_binding (binder,expr) $$ text ""
86

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

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

97 98 99 100
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
101

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

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

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

\begin{code}
117
ppr_expr :: OutputableBndr b => (SDoc -> SDoc) -> Expr b -> SDoc
118 119
	-- The function adds parens in context that need
	-- an atomic value (e.g. function args)
120

121
ppr_expr add_par (Type ty)  = add_par (ptext SLIT("TYPE") <+> ppr ty)	-- Wierd
122
	           
123 124
ppr_expr add_par (Var name) = ppr name
ppr_expr add_par (Lit lit)  = ppr lit
125

126
ppr_expr add_par expr@(Lam _ _)
127
  = let
128
	(bndrs, body) = collectBinders expr
129
    in
130
    add_par $
131 132
    hang (ptext SLIT("\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
	 2 (pprCoreExpr body)
133

134
ppr_expr add_par expr@(App fun arg)
135 136
  = case collectArgs expr of { (fun, args) -> 
    let
137
	pp_args     = sep (map pprArg args)
138
	val_args    = dropWhile isTypeArg args	 -- Drop the type arguments for tuples
139
	pp_tup_args = sep (punctuate comma (map pprArg val_args))
140
    in
141
    case fun of
142
	Var f -> case isDataConWorkId_maybe f of
143 144
			-- Notice that we print the *worker*
			-- for tuples in paren'd format.
145 146 147 148
		   Just dc | saturated && isTupleTyCon tc
			   -> tupleParens (tupleTyConBoxity tc) pp_tup_args
			   where
			     tc	       = dataConTyCon dc
sof's avatar
sof committed
149
			     saturated = val_args `lengthIs` idArity f
150

151
		   other -> add_par (hang (ppr f) 2 pp_args)
152

153
	other -> add_par (hang (pprParendExpr fun) 2 pp_args)
154 155
    }

156
ppr_expr add_par (Case expr var [(con,args,rhs)])
157
  = add_par $
158
    sep [sep [ptext SLIT("case") <+> pprCoreExpr expr,
159 160 161
	      hsep [ptext SLIT("of"),
		    ppr_bndr var,
		    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 alts)
171
  = add_par $
172
    sep [sep [ptext SLIT("case") <+> 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
arrow = ptext SLIT("->")
263 264 265 266 267 268
\end{code}

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

\begin{code}
269 270 271 272
instance OutputableBndr Var where
  pprBndr = pprCoreBinder

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

279
-- Lambda bound type variables are preceded by "@"
280
pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
281

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

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

289
pprTypedBinder binder
290
  | isTyVar binder  = ptext SLIT("@") <+> pprTyVarBndr binder
291
  | otherwise	    = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
292 293 294 295 296
	-- The space before the :: is important; it helps the lexer
	-- when reading inferfaces.  Otherwise it would lex "a::b" as one thing.
	--
	-- It's important that the type is parenthesised too, at least when
	-- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
297
	--	[Jun 2002: interfaces are now binary, so this doesn't matter]
298

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


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

320 321
ppIdInfo :: Id -> IdInfo -> SDoc
ppIdInfo b info
322
  = hsep [  ppArityInfo a,
323
	    ppWorkerInfo (workerInfo info),
324
#ifdef OLD_STRICTNESS
325
	    ppStrictnessInfo s,
326
            ppCprInfo m,
327
#endif
328
	    pprNewStrictness (newStrictnessInfo info),
329
	    vcat (map (pprCoreRule (ppr b)) (rulesRules p))
330 331 332
	-- Inline pragma, occ, demand, lbvar info
	-- printed out with all binders (when debug is on); 
	-- see PprCore.pprIdBndr
333 334 335
	]
  where
    a = arityInfo info
336
#ifdef OLD_STRICTNESS
337
    s = strictnessInfo info
338
    m = cprInfo info
339
#endif
340 341 342
    p = specInfo info
\end{code}

343

344
\begin{code}
345 346
pprIdRules :: [IdCoreRule] -> SDoc
pprIdRules rules = vcat (map pprIdRule rules)
347

348 349
pprIdRule :: IdCoreRule -> SDoc
pprIdRule (id,rule) = pprCoreRule (ppr id) rule
350

351
pprCoreRule :: SDoc -> CoreRule -> SDoc
352
pprCoreRule pp_fn (BuiltinRule name _)
353 354
  = ifPprDebug (ptext SLIT("Built in rule for") <+> pp_fn <> colon
		 <+> doubleQuotes (ftext name))
355

356
pprCoreRule pp_fn (Rule name act tpl_vars tpl_args rhs)
357
  = doubleQuotes (ftext name) <+> ppr act <+>
358 359
    sep [
	  ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
chak's avatar
chak committed
360 361
	  nest 2 (pp_fn <+> sep (map pprArg tpl_args)),
	  nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)
362
    ] <+> semi
363
\end{code}