[project @ 2002-03-14 15:27:15 by simonpj]
[ghc-hetmet.git] / ghc / compiler / types / Generics.lhs
index 4af9f41..ca05c39 100644 (file)
@@ -5,38 +5,40 @@ module Generics ( mkTyConGenInfo, mkGenericRhs,
 
 
 import RnHsSyn         ( RenamedHsExpr )
-import HsSyn           ( HsExpr(..), InPat(..), mkSimpleMatch )
+import HsSyn           ( HsExpr(..), InPat(..), mkSimpleMatch, placeHolderType )
 
-import Type             ( Type, isUnLiftedType, applyTys, tyVarsOfType, tyVarsOfTypes,
+import Type             ( Type, isUnLiftedType, tyVarsOfType, tyVarsOfTypes,
                          mkTyVarTys, mkForAllTys, mkTyConApp, 
                          mkFunTy, isTyVarTy, getTyVar_maybe,
-                         splitSigmaTy, splitTyConApp_maybe, funTyCon
+                         funTyCon
                        )
-
+import TcType          ( tcSplitTyConApp_maybe, tcSplitSigmaTy, tcSplitSigmaTy )
 import DataCon          ( DataCon, dataConOrigArgTys, dataConWrapId, dataConId, isExistentialDataCon )
 
-import TyCon            ( TyCon, tyConTyVars, tyConDataConsIfAvailable, 
+import TyCon            ( TyCon, tyConTyVars, tyConDataCons_maybe, 
                          tyConGenInfo, isNewTyCon, newTyConRep, isBoxedTupleTyCon
                        )
-import Name            ( Name, mkSysLocalName )
-import CoreSyn          ( mkLams, Expr(..), CoreExpr, AltCon(..), Note(..),
+import Name            ( Name, mkSystemName )
+import CoreSyn          ( mkLams, Expr(..), CoreExpr, AltCon(..), 
                          mkConApp, Alt, mkTyApps, mkVarApps )
+import CoreUtils       ( exprArity )
 import BasicTypes       ( EP(..), Boxity(..) )
 import Var              ( TyVar )
 import VarSet          ( varSetElems )
-import Id               ( Id, mkTemplateLocal, idType, idName, 
-                         mkTemplateLocalsNum, mkId
+import Id               ( Id, mkVanillaGlobal, idType, idName, 
+                         mkTemplateLocal, mkTemplateLocalsNum
                        ) 
 import TysWiredIn       ( genericTyCons,
                          genUnitTyCon, genUnitDataCon, plusTyCon, inrDataCon,
                          inlDataCon, crossTyCon, crossDataCon
                        )
-import IdInfo           ( constantIdInfo, setUnfoldingInfo )
+import IdInfo           ( noCafNoTyGenIdInfo, setUnfoldingInfo, setArityInfo )
 import CoreUnfold       ( mkTopUnfolding ) 
 
-import Unique          ( mkBuiltinUnique )
+import Maybe           ( isNothing )
 import SrcLoc          ( builtinSrcLoc )
-import Maybes          ( expectJust )
+import Unique          ( mkBuiltinUnique )
+import Util             ( takeList )
 import Outputable 
 
 #include "HsVersions.h"
@@ -187,7 +189,7 @@ validGenericInstanceType :: Type -> Bool
   --   f {| a + Int |}
 
 validGenericInstanceType inst_ty
-  = case splitTyConApp_maybe inst_ty of
+  = case tcSplitTyConApp_maybe inst_ty of
        Just (tycon, tys) ->  all isTyVarTy tys && tycon `elem` genericTyCons
        Nothing           ->  False
 
@@ -202,12 +204,12 @@ validGenericMethodType :: Type -> Bool
 validGenericMethodType ty 
   = valid tau
   where
-    (local_tvs, _, tau) = splitSigmaTy ty
+    (local_tvs, _, tau) = tcSplitSigmaTy ty
 
     valid ty
       | isTyVarTy ty    = True
       | no_tyvars_in_ty        = True
-      | otherwise      = case splitTyConApp_maybe ty of
+      | otherwise      = case tcSplitTyConApp_maybe ty of
                                Just (tc,tys) -> valid_tycon tc && all valid tys
                                Nothing       -> False
       where
@@ -237,8 +239,8 @@ mkTyConGenInfo :: TyCon -> [Name] -> Maybe (EP Id)
 -- for the fromT and toT conversion functions.
 
 mkTyConGenInfo tycon [from_name, to_name]
-  | null datacons      -- Abstractly imported types don't have
-  = Nothing            -- to/from operations, (and should not need them)
+  | isNothing maybe_datacons   -- Abstractly imported types don't have
+  = Nothing                    -- to/from operations, (and should not need them)
 
        -- If any of the constructor has an unboxed type as argument,
        -- then we can't build the embedding-projection pair, because
@@ -250,23 +252,30 @@ mkTyConGenInfo tycon [from_name, to_name]
   = Nothing
 
   | otherwise
-  = Just (EP { fromEP = mkId from_name from_ty from_id_info,
-              toEP   = mkId to_name   to_ty   to_id_info })
+  = Just (EP { fromEP = mkVanillaGlobal from_name from_ty from_id_info,
+              toEP   = mkVanillaGlobal to_name   to_ty   to_id_info })
   where
-    tyvars      = tyConTyVars tycon                    -- [a, b, c]
-    datacons    = tyConDataConsIfAvailable tycon       -- [C, D]
-    tycon_ty    = mkTyConApp tycon tyvar_tys           -- T a b c
-    tyvar_tys    = mkTyVarTys tyvars
+    maybe_datacons = tyConDataCons_maybe tycon
+    Just datacons  = maybe_datacons            -- [C, D]
+
+    tyvars        = tyConTyVars tycon          -- [a, b, c]
+    tycon_ty      = mkTyConApp tycon tyvar_tys -- T a b c
+    tyvar_tys      = mkTyVarTys tyvars
 
-    from_id_info = constantIdInfo `setUnfoldingInfo` mkTopUnfolding from_fn
-    to_id_info   = constantIdInfo `setUnfoldingInfo` mkTopUnfolding to_fn
+    from_id_info = noCafNoTyGenIdInfo `setUnfoldingInfo` mkTopUnfolding from_fn
+                                     `setArityInfo`     exprArity from_fn
+    to_id_info   = noCafNoTyGenIdInfo `setUnfoldingInfo` mkTopUnfolding to_fn
+                                     `setArityInfo`     exprArity to_fn
+       -- It's important to set the arity info, so that
+       -- the calling convention (gotten from arity) 
+       -- matches reality.
 
     from_ty = mkForAllTys tyvars (mkFunTy tycon_ty rep_ty)
     to_ty   = mkForAllTys tyvars (mkFunTy rep_ty tycon_ty)
 
     (from_fn, to_fn, rep_ty) 
        | isNewTyCon tycon
-       = ( mkLams tyvars $ Lam x  $ Note (Coerce newrep_ty tycon_ty) (Var x),
+       = ( mkLams tyvars $ Lam x  $ Var x,
            Var (dataConWrapId the_datacon),
            newrep_ty )
 
@@ -281,7 +290,7 @@ mkTyConGenInfo tycon [from_name, to_name]
            ----------------------
            --  Newtypes only
     [the_datacon]  = datacons
-    newrep_ty = applyTys (expectJust "mkGenTyConInfo" (newTyConRep tycon)) tyvar_tys
+    (_, newrep_ty) = newTyConRep tycon
        
            ----------------------
            --  Non-newtypes only
@@ -340,13 +349,6 @@ mk_sum_stuff i tyvars datacons
        where
          datacon_app = mkTyApps (Var (dataConWrapId datacon)) rep_tys
 
-
--- This constructs the c_of datatype from a DataCon and a Type
--- The identity function at the moment.
-cOfConstr :: DataCon -> Type -> Type
-cOfConstr y z = z
-
-
 ----------------------------------------------------
 --     Dealing with products
 ----------------------------------------------------
@@ -470,11 +472,11 @@ mkGenericRhs sel_id tyvar tycon
 
         -- Takes out the ForAll and the Class restrictions 
         -- in front of the type of the method.
-       (_,_,op_ty) = splitSigmaTy (idType sel_id)
+       (_,_,op_ty) = tcSplitSigmaTy (idType sel_id)
 
         -- Do it again!  This deals with the case where the method type 
        -- is polymorphic -- see notes above
-       (local_tvs,_,final_ty) = splitSigmaTy op_ty
+       (local_tvs,_,final_ty) = tcSplitSigmaTy op_ty
 
        -- Now we probably have a tycon in front
         -- of us, quite probably a FunTyCon.
@@ -495,7 +497,7 @@ generate_bimap env@(tv,ep,local_tvs) ty
        Just tv1 |  tv == tv1 -> ep                             -- The class tyvar
                 |  otherwise -> ASSERT( tv1 `elem` local_tvs)  -- One of the polymorphic tyvars of the method
                                 idEP   
-       Nothing  -> bimapApp env (splitTyConApp_maybe ty)
+       Nothing  -> bimapApp env (tcSplitTyConApp_maybe ty)
 
 -------------------
 bimapApp :: EPEnv -> Maybe (TyCon, [Type]) -> EP RenamedHsExpr
@@ -525,7 +527,7 @@ bimapTuple eps
   = EP { fromEP = mk_hs_lam [tuple_pat] from_body,
         toEP   = mk_hs_lam [tuple_pat] to_body }
   where
-    names      = take (length eps) genericNames
+    names      = takeList eps genericNames
     tuple_pat  = TuplePatIn (map VarPatIn names) Boxed
     eps_w_names = eps `zip` names
     to_body     = ExplicitTuple [toEP   ep `HsApp` HsVar g | (ep,g) <- eps_w_names] Boxed
@@ -533,10 +535,10 @@ bimapTuple eps
 
 -------------------
 genericNames :: [Name]
-genericNames = [mkSysLocalName (mkBuiltinUnique i) (_PK_ ('g' : show i)) | i <- [1..]]
+genericNames = [mkSystemName (mkBuiltinUnique i) (_PK_ ('g' : show i)) | i <- [1..]]
 (g1:g2:g3:_) = genericNames
 
-mk_hs_lam pats body = HsPar (HsLam (mkSimpleMatch pats body Nothing builtinSrcLoc))
+mk_hs_lam pats body = HsPar (HsLam (mkSimpleMatch pats body placeHolderType builtinSrcLoc))
 
 idEP :: EP RenamedHsExpr
 idEP = EP idexpr idexpr