[project @ 2002-03-04 17:01:26 by simonmar]
[ghc-hetmet.git] / ghc / compiler / coreSyn / PprCore.lhs
index d17e8b7..25d79f4 100644 (file)
@@ -9,9 +9,10 @@
 
 \begin{code}
 module PprCore (
-       pprCoreExpr, pprParendExpr, pprIfaceUnfolding, 
+       pprCoreExpr, pprParendExpr,
        pprCoreBinding, pprCoreBindings, pprIdBndr,
-       pprCoreRules, pprCoreRule
+       pprCoreBinding, pprCoreBindings, pprCoreAlt,
+       pprCoreRules, pprCoreRule, pprIdCoreRule
     ) where
 
 #include "HsVersions.h"
@@ -19,19 +20,31 @@ module PprCore (
 import CoreSyn
 import CostCentre      ( pprCostCentreCore )
 import Id              ( Id, idType, isDataConId_maybe, idLBVarInfo, idArity,
-                         idInfo, idInlinePragma, idDemandInfo, idOccInfo
+                         idInfo, idInlinePragma, idOccInfo,
+#ifdef DEBUG
+                         idDemandInfo, 
+#endif
+                         globalIdDetails, isGlobalId, isExportedId, 
+                         isSpecPragmaId, idNewDemandInfo
                        )
 import Var             ( isTyVar )
-import IdInfo          ( IdInfo, megaSeqIdInfo,
-                         arityInfo, ppArityInfo, ppFlavourInfo, flavourInfo,
-                         demandInfo, updateInfo, ppUpdateInfo, specInfo, 
-                         strictnessInfo, ppStrictnessInfo, cafInfo, ppCafInfo,
-                         cprInfo, ppCprInfo, lbvarInfo,
-                         workerInfo, ppWorkerInfo
+import IdInfo          ( IdInfo, megaSeqIdInfo, 
+                         arityInfo, ppArityInfo, 
+                         specInfo, ppStrictnessInfo, 
+                         workerInfo, ppWorkerInfo,
+                          tyGenInfo, ppTyGenInfo,
+                         newStrictnessInfo,
+#ifdef DEBUG
+                         cprInfo, ppCprInfo, 
+                         strictnessInfo,
+#endif
                        )
-import DataCon         ( isTupleCon, isUnboxedTupleCon )
+import DataCon         ( dataConTyCon )
+import TyCon           ( tupleTyConBoxity, isTupleTyCon )
 import PprType         ( pprParendType, pprTyVarBndr )
+import BasicTypes      ( tupleParens )
 import PprEnv
+import Util             ( lengthIs )
 import Outputable
 \end{code}
 
@@ -66,6 +79,8 @@ pprCoreBindings = pprTopBinds pprCoreEnv
 pprCoreBinding  = pprTopBind pprCoreEnv
 pprCoreExpr     = ppr_noparend_expr pprCoreEnv
 pprParendExpr   = ppr_parend_expr   pprCoreEnv
+pprArg                 = ppr_arg pprCoreEnv
+pprCoreAlt      = ppr_alt pprCoreEnv
 
 pprCoreEnv = initCoreEnv pprCoreBinder
 \end{code}
@@ -73,16 +88,6 @@ pprCoreEnv = initCoreEnv pprCoreBinder
 Printer for unfoldings in interfaces
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 \begin{code}
-pprIfaceUnfolding :: CoreExpr -> SDoc
-pprIfaceUnfolding = ppr_parend_expr pprIfaceEnv
-       -- Notice that it's parenthesised
-
-pprIfaceArg = ppr_arg pprIfaceEnv
-
-pprIfaceEnv = initCoreEnv pprIfaceBinder
-\end{code}
-
-\begin{code}
 instance Outputable b => Outputable (Bind b) where
     ppr bind = ppr_bind pprGenericEnv bind
 
@@ -169,7 +174,7 @@ ppr_expr add_par pe expr@(Lam _ _)
     in
     add_par $
     hang (ptext SLIT("\\") <+> sep (map (pBndr pe LambdaBind) bndrs) <+> arrow)
-        4 (ppr_noparend_expr pe body)
+        2 (ppr_noparend_expr pe body)
 
 ppr_expr add_par pe expr@(App fun arg)
   = case collectArgs expr of { (fun, args) -> 
@@ -182,13 +187,15 @@ ppr_expr add_par pe expr@(App fun arg)
        Var f -> case isDataConId_maybe f of
                        -- Notice that we print the *worker*
                        -- for tuples in paren'd format.
-                  Just dc | saturated && isTupleCon dc        -> parens pp_tup_args
-                          | saturated && isUnboxedTupleCon dc -> text "(#" <+> pp_tup_args <+> text "#)"
-                  other                                       -> add_par (hang (pOcc pe f) 4 pp_args)
-             where
-               saturated   = length val_args == idArity f
+                  Just dc | saturated && isTupleTyCon tc
+                          -> tupleParens (tupleTyConBoxity tc) pp_tup_args
+                          where
+                            tc        = dataConTyCon dc
+                            saturated = val_args `lengthIs` idArity f
 
-       other -> add_par (hang (ppr_parend_expr pe fun) 4 pp_args)
+                  other -> add_par (hang (pOcc pe f) 2 pp_args)
+
+       other -> add_par (hang (ppr_parend_expr pe fun) 2 pp_args)
     }
 
 ppr_expr add_par pe (Case expr var [(con,args,rhs)])
@@ -209,14 +216,12 @@ ppr_expr add_par pe (Case expr var alts)
   = add_par $
     sep [sep [ptext SLIT("case") <+> ppr_noparend_expr pe expr,
              ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
-        nest 4 (sep (punctuate semi (map ppr_alt alts))),
+        nest 2 (sep (punctuate semi (map (ppr_alt pe) alts))),
         char '}'
     ]
   where
     ppr_bndr = pBndr pe CaseBind
  
-    ppr_alt (con, args, rhs) = hang (ppr_case_pat pe con args)
-                                   4 (ppr_noparend_expr pe rhs)
 
 -- special cases: let ... in let ...
 -- ("disgusting" SLPJ)
@@ -233,7 +238,7 @@ ppr_expr add_par pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
   = add_par
     (hang (ptext SLIT("let {"))
          2 (hsep [hang (hsep [pBndr pe LetBind val_bdr, equals])
-                          4 (ppr_noparend_expr pe rhs),
+                          2 (ppr_noparend_expr pe rhs),
        ptext SLIT("} in")])
      $$
      ppr_noparend_expr pe expr)
@@ -264,7 +269,7 @@ ppr_expr add_par pe (Note (Coerce to_ty from_ty) expr)
 #else
 ppr_expr add_par pe (Note (Coerce to_ty from_ty) expr)
   = add_par $
-    sep [sep [ptext SLIT("__coerce"), nest 4 (pTy pe to_ty)],
+    sep [sep [ptext SLIT("__coerce"), nest 2 (pTy pe to_ty)],
         ppr_parend_expr pe expr]
 #endif
 
@@ -274,23 +279,15 @@ ppr_expr add_par pe (Note InlineCall expr)
 ppr_expr add_par pe (Note InlineMe expr)
   = add_par $ ptext SLIT("__inline_me") <+> ppr_parend_expr pe expr
 
-ppr_expr add_par pe (Note (TermUsg u) expr)
-  = getPprStyle $ \ sty ->
-    if ifaceStyle sty then
-      ppr_expr add_par pe expr
-    else
-      add_par (ppr u <+> ppr_noparend_expr pe expr)
+ppr_alt pe (con, args, rhs) 
+  = hang (ppr_case_pat pe con args) 2 (ppr_noparend_expr pe rhs)
 
 ppr_case_pat pe con@(DataAlt dc) args
-  | isTupleCon dc
-  = parens (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
-  | isUnboxedTupleCon dc
-  = hsep [text "(# " <> 
-         hsep (punctuate comma (map ppr_bndr args)) <>
-         text " #)",
-         arrow]
+  | isTupleTyCon tc
+  = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
   where
     ppr_bndr = pBndr pe CaseBind
+    tc = dataConTyCon dc
 
 ppr_case_pat pe con args
   = ppr con <+> hsep (map ppr_bndr args) <+> arrow
@@ -309,10 +306,10 @@ and @pprCoreExpr@ functions.
 \begin{code}
 -- Used for printing dump info
 pprCoreBinder LetBind binder
-  = vcat [sig, pragmas, ppr binder]
+  = vcat [sig, pprIdDetails binder, pragmas, ppr binder]
   where
     sig     = pprTypedBinder binder
-    pragmas = ppIdInfo (idInfo binder)
+    pragmas = ppIdInfo binder (idInfo binder)
 
 -- Lambda bound type variables are preceded by "@"
 pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
@@ -320,12 +317,8 @@ pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
 -- Case bound things don't get a signature or a herald
 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
 
--- Used for printing interface-file unfoldings
-pprIfaceBinder CaseBind binder = pprUntypedBinder binder
-pprIfaceBinder other    binder = pprTypedBinder binder
-
 pprUntypedBinder binder
-  | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
+  | isTyVar binder = ptext SLIT("@") <+> ppr binder    -- NB: don't print kind
   | otherwise      = pprIdBndr binder
 
 pprTypedBinder binder
@@ -337,62 +330,69 @@ pprTypedBinder binder
        -- It's important that the type is parenthesised too, at least when
        -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
 
+-- pprIdBndr does *not* print the type
 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
 pprIdBndr id = ppr id <+> 
               (megaSeqIdInfo (idInfo id) `seq`
                        -- Useful for poking on black holes
                ifPprDebug (ppr (idInlinePragma id) <+> ppr (idOccInfo id) <+> 
-                                     ppr (idDemandInfo id)) <+> ppr (idLBVarInfo id))
+#ifdef DEBUG
+                           ppr (idDemandInfo id) <+>
+#endif
+                           ppr (idNewDemandInfo id) <+>
+                           ppr (idLBVarInfo id)))
 \end{code}
 
 
 \begin{code}
-ppIdInfo :: IdInfo -> SDoc
-ppIdInfo info
-  = hsep [
-           ppFlavourInfo (flavourInfo info),
-           ppArityInfo a,
-           ppUpdateInfo u,
+pprIdDetails :: Id -> SDoc
+pprIdDetails id | isGlobalId id     = ppr (globalIdDetails id)
+               | isExportedId id   = ptext SLIT("[Exported]")
+               | isSpecPragmaId id = ptext SLIT("[SpecPrag]")
+               | otherwise         = empty
+
+ppIdInfo :: Id -> IdInfo -> SDoc
+ppIdInfo b info
+  = hsep [  ppArityInfo a,
+            ppTyGenInfo g,
            ppWorkerInfo (workerInfo info),
+#ifdef DEBUG
            ppStrictnessInfo s,
-           ppr d,
-           ppCafInfo c,
             ppCprInfo m,
-           ppr (lbvarInfo info),
-           pprIfaceCoreRules p
-       -- Inline pragma printed out with all binders; see PprCore.pprIdBndr
+#endif
+           ppr (newStrictnessInfo info),
+           pprCoreRules b p
+       -- Inline pragma, occ, demand, lbvar info
+       -- printed out with all binders (when debug is on); 
+       -- see PprCore.pprIdBndr
        ]
   where
     a = arityInfo info
-    d = demandInfo info
+    g = tyGenInfo info
+#ifdef DEBUG
     s = strictnessInfo info
-    u = updateInfo info
-    c = cafInfo info
     m = cprInfo info
+#endif
     p = specInfo info
 \end{code}
 
 
 \begin{code}
 pprCoreRules :: Id -> CoreRules -> SDoc
-pprCoreRules var (Rules rules _) = vcat (map (pprCoreRule (Just var)) rules)
+pprCoreRules var (Rules rules _) = vcat (map (pprCoreRule (ppr var)) rules)
 
-pprIfaceCoreRules :: CoreRules -> SDoc
-pprIfaceCoreRules (Rules rules _) = vcat (map (pprCoreRule Nothing) rules)
+pprIdCoreRule :: IdCoreRule -> SDoc
+pprIdCoreRule (id,rule) = pprCoreRule (ppr id) rule
 
-pprCoreRule :: Maybe Id -> CoreRule -> SDoc
-pprCoreRule maybe_fn (BuiltinRule _)
-  = ifPprDebug (ptext SLIT("A built in rule"))
+pprCoreRule :: SDoc -> CoreRule -> SDoc
+pprCoreRule pp_fn (BuiltinRule name _)
+  = ifPprDebug (ptext SLIT("Built in rule for") <+> pp_fn <> colon <+> doubleQuotes (ptext name))
 
-pprCoreRule maybe_fn (Rule name tpl_vars tpl_args rhs)
-  = doubleQuotes (ptext name) <+> 
+pprCoreRule pp_fn (Rule name act tpl_vars tpl_args rhs)
+  = doubleQuotes (ptext name) <+> ppr act <+>
     sep [
          ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
-         nest 4 (pp_fn <+> sep (map pprIfaceArg tpl_args)),
-         nest 4 (ptext SLIT("=") <+> pprIfaceUnfolding rhs)
+         nest 2 (pp_fn <+> sep (map pprArg tpl_args)),
+         nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)
     ] <+> semi
-  where
-    pp_fn = case maybe_fn of
-               Just id -> ppr id
-               Nothing -> empty                -- Interface file
 \end{code}