[project @ 1996-07-25 20:43:49 by partain]
[ghc-hetmet.git] / ghc / compiler / types / PprType.lhs
index 4720605..3001600 100644 (file)
@@ -16,17 +16,18 @@ module PprType(
        getTypeString,
        typeMaybeString,
        specMaybeTysSuffix,
+       getTyDescription,
        GenClass, 
        GenClassOp, pprGenClassOp,
        
-       addTyVar, nmbrTyVar,
+       addTyVar{-ToDo:don't export-}, nmbrTyVar,
        addUVar,  nmbrUsage,
        nmbrType, nmbrTyCon, nmbrClass
  ) where
 
-import Ubiq
-import IdLoop  -- for paranoia checking
-import TyLoop  -- for paranoia checking
+IMP_Ubiq()
+IMPORT_DELOOPER(IdLoop)
+--IMPORT_DELOOPER(TyLoop)      -- for paranoia checking
 
 -- friends:
 -- (PprType can see all the representations it's trying to print)
@@ -34,26 +35,24 @@ import Type         ( GenType(..), maybeAppTyCon,
                          splitForAllTy, splitSigmaTy, splitRhoTy, splitAppTy )
 import TyVar           ( GenTyVar(..) )
 import TyCon           ( TyCon(..), NewOrData )
-import Class           ( Class(..), GenClass(..),
-                         ClassOp(..), GenClassOp(..) )
+import Class           ( SYN_IE(Class), GenClass(..),
+                         SYN_IE(ClassOp), GenClassOp(..) )
 import Kind            ( Kind(..) )
-import Usage           ( GenUsage(..) )
+import Usage           ( pprUVar, GenUsage(..), SYN_IE(Usage), SYN_IE(UVar) )
 
 -- others:
 import CStrings                ( identToC )
 import CmdLineOpts     ( opt_OmitInterfacePragmas )
 import Maybes          ( maybeToBool )
-import Name            ( isLexVarSym, isLexSpecialSym, isPreludeDefined, origName, moduleOf,
-                         nameOrigName, nameOf, Name{-instance Outputable-}
+import Name            ( isLexVarSym, isLexSpecialSym, origName, moduleOf,
+                         getLocalName, Name{-instance Outputable-}
                        )
 import Outputable      ( ifPprShowAll, interpp'SP )
 import PprEnv
 import PprStyle                ( PprStyle(..), codeStyle, showUserishTypes )
 import Pretty
-import TysWiredIn      ( listTyCon )
-import UniqFM          ( addToUFM_Directly, lookupUFM_Directly, ufmToList{-ToDo:rm-} )
-import Unique          ( pprUnique10, pprUnique, incrUnique )
-import Usage           ( UVar(..), pprUVar )
+import UniqFM          ( addToUFM_Directly, lookupUFM_Directly{-, ufmToList ToDo:rm-} )
+import Unique          ( pprUnique10, pprUnique, incrUnique, listTyConKey )
 import Util
 \end{code}
 
@@ -147,8 +146,12 @@ ppr_ty sty env ctxt_prec ty@(FunTy (DictTy _ _ _) _ _)
   where
     (theta, body_ty) = splitRhoTy ty
 
-    ppr_theta [ct] = ppr_dict sty env tOP_PREC ct
-    ppr_theta cts  = ppParens (ppInterleave ppComma (map (ppr_dict sty env tOP_PREC) cts))
+    ppr_theta = case sty of { PprInterface -> ppr_theta_2 ; _ -> ppr_theta_1 }
+
+    ppr_theta_1 [ct] = ppr_dict sty env tOP_PREC ct
+    ppr_theta_1 cts  = ppParens (ppInterleave ppComma (map (ppr_dict sty env tOP_PREC) cts))
+
+    ppr_theta_2 cts  = ppBesides [ppStr "{{", ppInterleave ppComma (map (ppr_dict sty env tOP_PREC) cts), ppStr "}}"]
 
 ppr_ty sty env ctxt_prec (FunTy ty1 ty2 usage)
     -- We fiddle the precedences passed to left/right branches,
@@ -163,11 +166,12 @@ ppr_ty sty env ctxt_prec ty@(AppTy _ _)
   where
     (fun_ty, arg_tys) = splitAppTy ty
 
-ppr_ty PprInterface env ctxt_prec (SynTy tycon tys expansion)
-  -- always expand types in an interface
-  = ppr_ty PprInterface env ctxt_prec expansion
-
 ppr_ty sty env ctxt_prec (SynTy tycon tys expansion)
+  | codeStyle sty
+       -- always expand types that squeak into C-variable names
+  = ppr_ty sty env ctxt_prec expansion
+
+  | otherwise
   = ppBeside
      (ppr_app sty env ctxt_prec (ppr sty tycon) tys)
      (ifPprShowAll sty (ppCat [ppStr " {- expansion:",
@@ -177,7 +181,6 @@ ppr_ty sty env ctxt_prec (SynTy tycon tys expansion)
 ppr_ty sty env ctxt_prec (DictTy clas ty usage)
   = ppr_dict sty env ctxt_prec (clas, ty)
 
-
 -- Some help functions
 ppr_corner sty env ctxt_prec (TyConTy FunTyCon usage) arg_tys
   | length arg_tys == 2
@@ -186,6 +189,7 @@ ppr_corner sty env ctxt_prec (TyConTy FunTyCon usage) arg_tys
     (ty1:ty2:_) = arg_tys
 
 ppr_corner sty env ctxt_prec (TyConTy (TupleTyCon _ _ a) usage) arg_tys
+  | not (codeStyle sty) -- no magic in that case
   = --ASSERT(length arg_tys == a)
     (if (length arg_tys /= a) then pprTrace "ppr_corner:" (ppCat [ppInt a, ppInterleave ppComma (map (pprGenType PprDebug) arg_tys)]) else id) $
     ppBesides [ppLparen, arg_tys_w_commas, ppRparen]
@@ -193,7 +197,7 @@ ppr_corner sty env ctxt_prec (TyConTy (TupleTyCon _ _ a) usage) arg_tys
     arg_tys_w_commas = ppIntersperse pp'SP (map (ppr_ty sty env tOP_PREC) arg_tys)
 
 ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
-  | tycon == listTyCon
+  | not (codeStyle sty) && uniqueOf tycon == listTyConKey
   = ASSERT(length arg_tys == 1)
     ppBesides [ppLbrack, ppr_ty sty env tOP_PREC ty1, ppRbrack]                    
   where
@@ -204,7 +208,7 @@ ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
                      
 ppr_corner sty env ctxt_prec (TyVarTy tyvar) arg_tys
   = ppr_app sty env ctxt_prec (ppr_tyvar env tyvar) arg_tys
-   
+  
 
 ppr_app sty env ctxt_prec pp_fun []      
   = pp_fun
@@ -261,13 +265,16 @@ maybeParen ctxt_prec inner_prec pretty
 
 \begin{code}
 pprGenTyVar sty (TyVar uniq kind name usage)
+  | codeStyle sty
+  = pp_u
+  | otherwise
   = case sty of
       PprInterface -> pp_u
       _                   -> ppBesides [pp_name, ppStr "{-", pp_u, ppStr "-}"]
   where
     pp_u    = pprUnique uniq
     pp_name = case name of
-               Just n  -> ppPStr (nameOf (nameOrigName n))
+               Just n  -> ppPStr (getLocalName n)
                Nothing -> case kind of
                                TypeKind        -> ppChar 'o'
                                BoxedTypeKind   -> ppChar 't'
@@ -287,20 +294,42 @@ ToDo; all this is suspiciously like getOccName!
 showTyCon :: PprStyle -> TyCon -> String
 showTyCon sty tycon = ppShow 80 (pprTyCon sty tycon)
 
+maybe_code sty x
+  = if codeStyle sty
+    then ppBesides (ppPStr SLIT("Prelude_") : map mangle x)
+    else ppStr x
+  where
+    -- ToDo: really should be in CStrings
+    mangle '(' = ppPStr SLIT("Z40") -- decimal ascii #s
+    mangle ')' = ppPStr SLIT("Z41")
+    mangle '[' = ppPStr SLIT("Z91")
+    mangle ']' = ppPStr SLIT("Z93")
+    mangle ',' = ppPStr SLIT("Z44")
+    mangle '-' = ppPStr SLIT("Zm")
+    mangle '>' = ppPStr SLIT("Zg")
+
 pprTyCon :: PprStyle -> TyCon -> Pretty
 
-pprTyCon sty FunTyCon                  = ppStr "(->)"
-pprTyCon sty (TupleTyCon _ name _)      = ppr sty name
-pprTyCon sty (PrimTyCon uniq name kind) = ppr sty name
+pprTyCon sty (PrimTyCon _ name _ _) = ppr sty name
+
+pprTyCon sty FunTyCon              = maybe_code sty "->"
+pprTyCon sty (TupleTyCon _ _ arity) = case arity of
+                                       0 -> maybe_code sty "()"
+                                       2 -> maybe_code sty "(,)"
+                                       3 -> maybe_code sty "(,,)"
+                                       4 -> maybe_code sty "(,,,)"
+                                       5 -> maybe_code sty "(,,,,)"
+                                       n -> maybe_code sty ( "(" ++ nOfThem (n-1) ',' ++ ")" )
 
 pprTyCon sty tycon@(DataTyCon uniq name kind tyvars ctxt cons derivings nd)
+  | uniq == listTyConKey
+  = maybe_code sty "[]"
+  | otherwise
   = ppr sty name
 
 pprTyCon sty (SpecTyCon tc ty_maybes)
   = ppBeside (pprTyCon sty tc)
-            (if (codeStyle sty)
-             then identToC tys_stuff
-             else ppPStr   tys_stuff)
+            ((if (codeStyle sty) then identToC else ppPStr) tys_stuff)
   where
     tys_stuff = specMaybeTysSuffix ty_maybes
 
@@ -330,14 +359,15 @@ ppr_class_op sty tyvars (ClassOp op_name i ty)
   = case sty of
       PprForC      -> pp_C
       PprForAsm _ _ -> pp_C
-      PprInterface  -> ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
-      PprShowAll    -> ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
+      PprInterface  -> pp_sigd
+      PprShowAll    -> pp_sigd
       _                    -> pp_user
   where
     pp_C    = ppPStr op_name
     pp_user = if isLexVarSym op_name && not (isLexSpecialSym op_name)
              then ppParens pp_C
              else pp_C
+    pp_sigd = ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
 \end{code}
 
 
@@ -350,25 +380,30 @@ ppr_class_op sty tyvars (ClassOp op_name i ty)
 \begin{code}
     -- Shallowly magical; converts a type into something
     -- vaguely close to what can be used in C identifier.
-    -- Don't forget to include the module name!!!
-getTypeString :: Type -> [FAST_STRING]
-getTypeString ty
-  | is_prelude_ty = [string]
-  | otherwise     = [mod, string]
-  where
-    string = _PK_ (tidy (ppShow 1000 ppr_t))
-    ppr_t  = pprGenType PprForC ty
-                       -- PprForC expands type synonyms as it goes
+    -- Produces things like what we have in mkCompoundName,
+    -- which can be "dot"ted together...
 
-    (is_prelude_ty, mod)
-      = case (maybeAppTyCon ty) of
-         Nothing -> true_bottom
-         Just (tycon,_) ->
-           if isPreludeDefined tycon
-           then true_bottom
-           else (False, moduleOf (origName tycon))
+getTypeString :: Type -> [Either OrigName FAST_STRING]
 
-    true_bottom = (True, panic "getTypeString")
+getTypeString ty
+  = case (splitAppTy ty) of { (tc, args) ->
+    do_tc tc : map do_arg_ty args }
+  where
+    do_tc (TyConTy tc _) = Left (origName "do_tc" tc)
+    do_tc (SynTy _ _ ty) = do_tc ty
+    do_tc other = --pprTrace "getTypeString:do_tc:" (pprType PprDebug other) $
+                 Right (_PK_ (ppShow 1000 (pprType PprForC other)))
+
+    do_arg_ty (TyConTy tc _) = Left (origName "do_arg_ty" tc)
+    do_arg_ty (TyVarTy tv)   = Right (_PK_ (ppShow 80 (ppr PprForC tv)))
+    do_arg_ty (SynTy _ _ ty) = do_arg_ty ty
+    do_arg_ty other         = --pprTrace "getTypeString:do_arg_ty:" (pprType PprDebug other) $
+                              Right (_PK_ (ppShow 1000 (pprType PprForC other)))
+
+       -- PprForC expands type synonyms as it goes;
+       -- it also forces consistent naming of tycons
+       -- (e.g., can't have both "(,) a b" and "(a,b)":
+       -- must be consistent!
 
     --------------------------------------------------
     -- tidy: very ad-hoc
@@ -388,17 +423,42 @@ getTypeString ty
     no_leading_sps (' ':xs) = no_leading_sps xs
     no_leading_sps other = other
 
-typeMaybeString :: Maybe Type -> [FAST_STRING]
-typeMaybeString Nothing  = [SLIT("!")]
+typeMaybeString :: Maybe Type -> [Either OrigName FAST_STRING]
+typeMaybeString Nothing  = [Right SLIT("!")]
 typeMaybeString (Just t) = getTypeString t
 
 specMaybeTysSuffix :: [Maybe Type] -> FAST_STRING
 specMaybeTysSuffix ty_maybes
+  = panic "PprType.specMaybeTysSuffix"
+{- LATER:
   = let
        ty_strs  = concat (map typeMaybeString ty_maybes)
        dotted_tys = [ _CONS_ '.' str | str <- ty_strs ]
     in
     _CONCAT_ dotted_tys
+-}
+\end{code}
+
+Grab a name for the type. This is used to determine the type
+description for profiling.
+\begin{code}
+getTyDescription :: Type -> String
+
+getTyDescription ty
+  = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
+    case tau_ty of
+      TyVarTy _              -> "*"
+      AppTy fun _     -> getTyDescription fun
+      FunTy _ res _   -> '-' : '>' : fun_result res
+      TyConTy tycon _ -> _UNPK_ (getLocalName tycon)
+      SynTy tycon _ _ -> _UNPK_ (getLocalName tycon)
+      DictTy _ _ _    -> "dict"
+      ForAllTy _ ty   -> getTyDescription ty
+      _                      -> pprPanic "getTyDescription: other" (pprType PprDebug tau_ty)
+    }
+  where
+    fun_result (FunTy _ res _) = '>' : fun_result res
+    fun_result other          = getTyDescription other
 \end{code}
 
 ToDo: possibly move:
@@ -455,7 +515,13 @@ addTyVar, nmbrTyVar :: TyVar -> NmbrM TyVar
 addTyVar tv@(TyVar u k maybe_name use) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
   = --pprTrace "addTyVar:" (ppCat [pprUnique u, pprUnique ut]) $
     case (lookupUFM_Directly tvenv u) of
-      Just xx -> pprTrace "addTyVar: already in map!" (ppr PprDebug tv) $
+      Just xx -> -- pprTrace "addTyVar: already in map!" (ppr PprDebug tv) $
+                -- (It gets triggered when we do a datatype: first we
+                -- "addTyVar" the tyvars for the datatype as a whole;
+                -- we will subsequently "addId" the data cons, including
+                -- the type for each of them -- each of which includes
+                -- _forall_ ...tvs..., which we will addTyVar.
+                -- Harmless, if that's all that happens....
                 (nenv, xx)
       Nothing ->
        let
@@ -474,15 +540,15 @@ nmbrTyVar tv@(TyVar u _ _ _) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
   = case (lookupUFM_Directly tvenv u) of
       Just xx -> (nenv, xx)
       Nothing ->
-       pprTrace "nmbrTyVar: lookup failed:" (ppCat (ppr PprDebug u : [ppCat [ppr PprDebug x, ppStr "=>", ppr PprDebug tv] | (x,tv) <- ufmToList tvenv])) $
+       --pprTrace "nmbrTyVar: lookup failed:" (ppCat (ppr PprDebug u : [ppCat [ppr PprDebug x, ppStr "=>", ppr PprDebug tv] | (x,tv) <- ufmToList tvenv])) $
        (nenv, tv)
 \end{code}
 
 nmbrTyCon : only called from ``top-level'', if you know what I mean.
 \begin{code}
-nmbrTyCon tc@FunTyCon          = returnNmbr tc
-nmbrTyCon tc@(TupleTyCon _ _ _)        = returnNmbr tc
-nmbrTyCon tc@(PrimTyCon  _ _ _)        = returnNmbr tc
+nmbrTyCon tc@FunTyCon            = returnNmbr tc
+nmbrTyCon tc@(TupleTyCon _ _ _)          = returnNmbr tc
+nmbrTyCon tc@(PrimTyCon  _ _ _ _) = returnNmbr tc
 
 nmbrTyCon (DataTyCon u n k tvs theta cons clss nod)
   = --pprTrace "nmbrDataTyCon:" (ppCat (map (ppr PprDebug) tvs)) $
@@ -540,7 +606,7 @@ addUVar, nmbrUVar :: UVar -> NmbrM UVar
 
 addUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
   = case (lookupUFM_Directly uvenv u) of
-      Just xx -> _trace "addUVar: already in map!" $
+      Just xx -> trace "addUVar: already in map!" $
                 (nenv, xx)
       Nothing ->
        let
@@ -556,6 +622,6 @@ nmbrUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
   = case (lookupUFM_Directly uvenv u) of
       Just xx -> (nenv, xx)
       Nothing ->
-       _trace "nmbrUVar: lookup failed" $
+       trace "nmbrUVar: lookup failed" $
        (nenv, u)
 \end{code}