Add the notion of "constructor-like" Ids for rule-matching
[ghc-hetmet.git] / compiler / coreSyn / PprCore.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1996-1998
4 %
5
6 Printing of Core syntax
7
8 \begin{code}
9 module PprCore (
10         pprCoreExpr, pprParendExpr,
11         pprCoreBinding, pprCoreBindings, pprCoreAlt,
12         pprRules
13     ) where
14
15 import CoreSyn
16 import CostCentre
17 import Var
18 import Id
19 import IdInfo
20 import NewDemand
21 #ifdef OLD_STRICTNESS
22 import Id
23 import IdInfo
24 #endif
25
26 import DataCon
27 import TyCon
28 import Type
29 import Coercion
30 import BasicTypes
31 import Util
32 import Outputable
33 import FastString
34 \end{code}
35
36 %************************************************************************
37 %*                                                                      *
38 \subsection{Public interfaces for Core printing (excluding instances)}
39 %*                                                                      *
40 %************************************************************************
41
42 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
43
44 \begin{code}
45 pprCoreBindings :: OutputableBndr b => [Bind b] -> SDoc
46 pprCoreBinding  :: OutputableBndr b => Bind b  -> SDoc
47 pprCoreExpr     :: OutputableBndr b => Expr b  -> SDoc
48 pprParendExpr   :: OutputableBndr b => Expr b  -> SDoc
49
50 pprCoreBindings = pprTopBinds
51 pprCoreBinding  = pprTopBind 
52
53 instance OutputableBndr b => Outputable (Bind b) where
54     ppr bind = ppr_bind bind
55
56 instance OutputableBndr b => Outputable (Expr b) where
57     ppr expr = pprCoreExpr expr
58 \end{code}
59
60
61 %************************************************************************
62 %*                                                                      *
63 \subsection{The guts}
64 %*                                                                      *
65 %************************************************************************
66
67 \begin{code}
68 pprTopBinds :: OutputableBndr a => [Bind a] -> SDoc
69 pprTopBinds binds = vcat (map pprTopBind binds)
70
71 pprTopBind :: OutputableBndr a => Bind a -> SDoc
72 pprTopBind (NonRec binder expr)
73  = ppr_binding (binder,expr) $$ text ""
74
75 pprTopBind (Rec binds)
76   = vcat [ptext (sLit "Rec {"),
77           vcat (map ppr_binding binds),
78           ptext (sLit "end Rec }"),
79           text ""]
80 \end{code}
81
82 \begin{code}
83 ppr_bind :: OutputableBndr b => Bind b -> SDoc
84
85 ppr_bind (NonRec val_bdr expr) = ppr_binding (val_bdr, expr)
86 ppr_bind (Rec binds)           = vcat (map pp binds)
87                                where
88                                  pp bind = ppr_binding bind <> semi
89
90 ppr_binding :: OutputableBndr b => (b, Expr b) -> SDoc
91 ppr_binding (val_bdr, expr)
92   = pprBndr LetBind val_bdr $$ 
93     hang (ppr val_bdr <+> equals) 2 (pprCoreExpr expr)
94 \end{code}
95
96 \begin{code}
97 pprParendExpr   expr = ppr_expr parens expr
98 pprCoreExpr expr = ppr_expr noParens expr
99
100 noParens :: SDoc -> SDoc
101 noParens pp = pp
102 \end{code}
103
104 \begin{code}
105 ppr_expr :: OutputableBndr b => (SDoc -> SDoc) -> Expr b -> SDoc
106         -- The function adds parens in context that need
107         -- an atomic value (e.g. function args)
108
109 ppr_expr add_par (Type ty)  = add_par (ptext (sLit "TYPE") <+> ppr ty)  -- Wierd
110                    
111 ppr_expr _       (Var name) = ppr name
112 ppr_expr _       (Lit lit)  = ppr lit
113
114 ppr_expr add_par (Cast expr co) 
115   = add_par $
116     sep [pprParendExpr expr, 
117          ptext (sLit "`cast`") <+> parens (pprCo co)]
118   where
119     pprCo co = sep [ppr co, dcolon <+> ppr (coercionKindPredTy co)]
120          
121
122 ppr_expr add_par expr@(Lam _ _)
123   = let
124         (bndrs, body) = collectBinders expr
125     in
126     add_par $
127     hang (ptext (sLit "\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
128          2 (pprCoreExpr body)
129
130 ppr_expr add_par expr@(App {})
131   = case collectArgs expr of { (fun, args) -> 
132     let
133         pp_args     = sep (map pprArg args)
134         val_args    = dropWhile isTypeArg args   -- Drop the type arguments for tuples
135         pp_tup_args = sep (punctuate comma (map pprCoreExpr val_args))
136     in
137     case fun of
138         Var f -> case isDataConWorkId_maybe f of
139                         -- Notice that we print the *worker*
140                         -- for tuples in paren'd format.
141                    Just dc | saturated && isTupleTyCon tc
142                            -> tupleParens (tupleTyConBoxity tc) pp_tup_args
143                            where
144                              tc        = dataConTyCon dc
145                              saturated = val_args `lengthIs` idArity f
146
147                    _ -> add_par (hang (ppr f) 2 pp_args)
148
149         _ -> add_par (hang (pprParendExpr fun) 2 pp_args)
150     }
151
152 ppr_expr add_par (Case expr var ty [(con,args,rhs)])
153   = add_par $
154     sep [sep [ptext (sLit "case") <+> pprCoreExpr expr,
155               ifPprDebug (braces (ppr ty)),
156               sep [ptext (sLit "of") <+> ppr_bndr var, 
157                    char '{' <+> ppr_case_pat con args]
158           ],
159          pprCoreExpr rhs,
160          char '}'
161     ]
162   where
163     ppr_bndr = pprBndr CaseBind
164
165 ppr_expr add_par (Case expr var ty alts)
166   = add_par $
167     sep [sep [ptext (sLit "case")
168                 <+> pprCoreExpr expr
169                 <+> ifPprDebug (braces (ppr ty)),
170               ptext (sLit "of") <+> ppr_bndr var <+> char '{'],
171          nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
172          char '}'
173     ]
174   where
175     ppr_bndr = pprBndr CaseBind
176  
177
178 -- special cases: let ... in let ...
179 -- ("disgusting" SLPJ)
180
181 {-
182 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
183   = add_par $
184     vcat [
185       hsep [ptext (sLit "let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
186       nest 2 (pprCoreExpr rhs),
187       ptext (sLit "} in"),
188       pprCoreExpr body ]
189
190 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
191   = add_par
192     (hang (ptext (sLit "let {"))
193           2 (hsep [ppr_binding (val_bdr,rhs),
194                    ptext (sLit "} in")])
195      $$
196      pprCoreExpr expr)
197 -}
198
199 -- General case (recursive case, too)
200 ppr_expr add_par (Let bind expr)
201   = add_par $
202     sep [hang (ptext keyword) 2 (ppr_bind bind <+> ptext (sLit "} in")),
203          pprCoreExpr expr]
204   where
205     keyword = case bind of
206                 Rec _      -> (sLit "letrec {")
207                 NonRec _ _ -> (sLit "let {")
208
209 ppr_expr add_par (Note (SCC cc) expr)
210   = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
211
212 ppr_expr add_par (Note InlineMe expr)
213   = add_par $ ptext (sLit "__inline_me") <+> pprParendExpr expr
214
215 ppr_expr add_par (Note (CoreNote s) expr)
216   = add_par $ 
217     sep [sep [ptext (sLit "__core_note"), pprHsString (mkFastString s)],
218          pprParendExpr expr]
219
220 pprCoreAlt :: OutputableBndr a => (AltCon, [a] , Expr a) -> SDoc
221 pprCoreAlt (con, args, rhs) 
222   = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
223
224 ppr_case_pat :: OutputableBndr a => AltCon -> [a] -> SDoc
225 ppr_case_pat (DataAlt dc) args
226   | isTupleTyCon tc
227   = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
228   where
229     ppr_bndr = pprBndr CaseBind
230     tc = dataConTyCon dc
231
232 ppr_case_pat con args
233   = ppr con <+> sep (map ppr_bndr args) <+> arrow
234   where
235     ppr_bndr = pprBndr CaseBind
236
237 pprArg :: OutputableBndr a => Expr a -> SDoc
238 pprArg (Type ty) = ptext (sLit "@") <+> pprParendType ty
239 pprArg expr      = pprParendExpr expr
240 \end{code}
241
242 Other printing bits-and-bobs used with the general @pprCoreBinding@
243 and @pprCoreExpr@ functions.
244
245 \begin{code}
246 instance OutputableBndr Var where
247   pprBndr = pprCoreBinder
248
249 pprCoreBinder :: BindingSite -> Var -> SDoc
250 pprCoreBinder LetBind binder
251   | isTyVar binder = pprKindedTyVarBndr binder
252   | otherwise
253   = vcat [sig, pprIdExtras binder, pragmas]
254   where
255     sig     = pprTypedBinder binder
256     pragmas = ppIdInfo binder (idInfo binder)
257
258 -- Lambda bound type variables are preceded by "@"
259 pprCoreBinder LambdaBind bndr 
260   | isDeadBinder bndr
261   = getPprStyle $ \ sty ->
262     if debugStyle sty then
263         parens (pprTypedBinder bndr)
264     else
265         char '_'
266   | otherwise
267   = parens (pprTypedBinder bndr)
268
269 -- Case bound things don't get a signature or a herald, unless we have debug on
270 pprCoreBinder CaseBind bndr 
271   = getPprStyle $ \ sty ->
272     if debugStyle sty then
273         parens (pprTypedBinder bndr)
274     else
275         if isDeadBinder bndr then char '_'
276         else pprUntypedBinder bndr
277
278 pprUntypedBinder :: Var -> SDoc
279 pprUntypedBinder binder
280   | isTyVar binder = ptext (sLit "@") <+> ppr binder    -- NB: don't print kind
281   | otherwise      = pprIdBndr binder
282
283 pprTypedBinder :: Var -> SDoc
284 -- Print binder with a type or kind signature (not paren'd)
285 pprTypedBinder binder
286   | isTyVar binder  = pprKindedTyVarBndr binder
287   | otherwise       = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
288
289 pprKindedTyVarBndr :: TyVar -> SDoc
290 -- Print a type variable binder with its kind (but not if *)
291 pprKindedTyVarBndr tyvar
292   = ptext (sLit "@") <+> ppr tyvar <> opt_kind
293   where
294     opt_kind    -- Print the kind if not *
295         | isLiftedTypeKind kind = empty
296         | otherwise = dcolon <> pprKind kind
297     kind = tyVarKind tyvar
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 pprIdBndr :: Id -> SDoc
302 pprIdBndr id = ppr id <+> pprIdBndrInfo (idInfo id)
303
304 pprIdBndrInfo :: IdInfo -> SDoc
305 pprIdBndrInfo info 
306   = megaSeqIdInfo info `seq` doc -- The seq is useful for poking on black holes
307   where
308     prag_info = inlinePragInfo info
309     occ_info  = occInfo info
310     dmd_info  = newDemandInfo info
311     lbv_info  = lbvarInfo info
312
313     no_info = isDefaultInlinePragma prag_info && isNoOcc occ_info && 
314               (case dmd_info of { Nothing -> True; Just d -> isTop d }) &&
315               hasNoLBVarInfo lbv_info
316
317     doc | no_info = empty
318         | otherwise
319         = brackets $ hsep [ppr prag_info, ppr occ_info, 
320                            ppr dmd_info, ppr lbv_info
321 #ifdef OLD_STRICTNESS
322                            , ppr (demandInfo id)
323 #endif
324                           ]
325 \end{code}
326
327
328 \begin{code}
329 pprIdExtras :: Id -> SDoc
330 pprIdExtras id = pp_scope <> ppr (idDetails id)
331   where
332     pp_scope | isGlobalId id   = ptext (sLit "GblId")
333              | isExportedId id = ptext (sLit "LclIdX")
334              | otherwise       = ptext (sLit "LclId")
335
336 ppIdInfo :: Id -> IdInfo -> SDoc
337 ppIdInfo _ info
338   = brackets $
339     vcat [  ppArityInfo a,
340             ppWorkerInfo (workerInfo info),
341             ppCafInfo (cafInfo info),
342 #ifdef OLD_STRICTNESS
343             ppStrictnessInfo s,
344             ppCprInfo m,
345 #endif
346             pprNewStrictness (newStrictnessInfo info),
347             if null rules then empty
348             else ptext (sLit "RULES:") <+> vcat (map pprRule rules)
349         -- Inline pragma, occ, demand, lbvar info
350         -- printed out with all binders (when debug is on); 
351         -- see PprCore.pprIdBndr
352         ]
353   where
354     a = arityInfo info
355 #ifdef OLD_STRICTNESS
356     s = strictnessInfo info
357     m = cprInfo info
358 #endif
359     rules = specInfoRules (specInfo info)
360 \end{code}
361
362
363 \begin{code}
364 instance Outputable CoreRule where
365    ppr = pprRule
366
367 pprRules :: [CoreRule] -> SDoc
368 pprRules rules = vcat (map pprRule rules)
369
370 pprRule :: CoreRule -> SDoc
371 pprRule (BuiltinRule { ru_fn = fn, ru_name = name})
372   = ptext (sLit "Built in rule for") <+> ppr fn <> colon <+> doubleQuotes (ftext name)
373
374 pprRule (Rule { ru_name = name, ru_act = act, ru_fn = fn,
375                 ru_bndrs = tpl_vars, ru_args = tpl_args,
376                 ru_rhs = rhs })
377   = hang (doubleQuotes (ftext name) <+> ppr act)
378        4 (sep [ptext (sLit "forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
379                nest 2 (ppr fn <+> sep (map pprArg tpl_args)),
380                nest 2 (ptext (sLit "=") <+> pprCoreExpr rhs)
381             ])
382 \end{code}