[project @ 2000-09-22 15:56:12 by simonpj]
[ghc-hetmet.git] / ghc / compiler / prelude / TysWiredIn.lhs
index 7a76a1a..d9b7e9d 100644 (file)
@@ -48,11 +48,9 @@ module TysWiredIn (
 
        -- tuples
        mkTupleTy,
-       tupleTyCon, tupleCon, unitTyCon, unitDataConId, pairTyCon, 
-
-       -- unboxed tuples
-       mkUnboxedTupleTy,
-       unboxedTupleTyCon, unboxedTupleCon, 
+       tupleTyCon, tupleCon, 
+       unitTyCon, unitDataConId, pairTyCon, 
+       unboxedSingletonTyCon, unboxedSingletonDataCon,
        unboxedPairTyCon, unboxedPairDataCon,
 
        stablePtrTyCon,
@@ -64,11 +62,14 @@ module TysWiredIn (
        wordTy,
        wordTyCon,
 
-       isFFIArgumentTy,  -- :: Bool -> Type -> Bool
-       isFFIResultTy,    -- :: Type -> Bool
-       isFFIExternalTy,  -- :: Type -> Bool
-       isAddrTy,         -- :: Type -> Bool
-       isForeignObjTy    -- :: Type -> Bool
+       isFFIArgumentTy,    -- :: Bool -> Type -> Bool
+       isFFIResultTy,      -- :: Type -> Bool
+       isFFIExternalTy,    -- :: Type -> Bool
+        isFFIDynArgumentTy, -- :: Type -> Bool
+       isFFIDynResultTy,   -- :: Type -> Bool
+       isFFILabelTy,       -- :: Type -> Bool
+       isAddrTy,           -- :: Type -> Bool
+       isForeignObjTy      -- :: Type -> Bool
 
     ) where
 
@@ -77,7 +78,7 @@ module TysWiredIn (
 import {-# SOURCE #-} MkId( mkDataConId, mkDataConWrapId )
 
 -- friends:
-import PrelMods
+import PrelNames
 import TysPrim
 
 -- others:
@@ -89,17 +90,14 @@ import Var          ( TyVar, tyVarKind )
 import TyCon           ( TyCon, AlgTyConFlavour(..), ArgVrcs, tyConDataCons,
                          mkAlgTyCon, mkSynTyCon, mkTupleTyCon, isUnLiftedTyCon
                        )
-import BasicTypes      ( Arity, NewOrData(..), RecFlag(..) )
+import BasicTypes      ( Arity, RecFlag(..), Boxity(..), isBoxed )
 import Type            ( Type, mkTyConTy, mkTyConApp, mkSigmaTy, mkTyVarTys, 
                          mkArrowKinds, boxedTypeKind, unboxedTypeKind,
                          mkFunTy, mkFunTys,
                          splitTyConApp_maybe, repType,
                          TauType, ClassContext )
-import PrimRep         ( PrimRep(..) )
 import Unique
 import CmdLineOpts      ( opt_GlasgowExts )
-import Util            ( assoc )
-import Panic           ( panic )
 import Array
 
 alpha_tyvar      = [alphaTyVar]
@@ -121,6 +119,7 @@ pcTyCon new_or_data is_rec key mod str tyvars argvrcs cons
                []              -- No context
                 argvrcs
                cons
+               (length cons)
                []              -- No derivings
                new_or_data
                is_rec
@@ -165,88 +164,49 @@ pcDataCon wrap_key mod str tyvars context arg_tys tycon
 %************************************************************************
 
 \begin{code}
-tupleTyCon :: Arity -> TyCon
-tupleTyCon i | i > mAX_TUPLE_SIZE = fst (mk_tuple i)   -- Build one specially
-            | otherwise          = tupleTyConArr!i
-
-tupleCon :: Arity -> DataCon
-tupleCon i | i > mAX_TUPLE_SIZE = snd (mk_tuple i)     -- Build one specially
-          | otherwise          = tupleConArr!i
-
-tupleTyCons :: [TyCon]
-tupleTyCons = elems tupleTyConArr
-
-tupleTyConArr :: Array Int TyCon
-tupleTyConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map fst tuples)
-
-tupleConArr :: Array Int DataCon
-tupleConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map snd tuples)
-
-tuples :: [(TyCon,DataCon)]
-tuples = [mk_tuple i | i <- [0..mAX_TUPLE_SIZE]]
-
-mk_tuple :: Int -> (TyCon,DataCon)
-mk_tuple arity = (tycon, tuple_con)
+tupleTyCon :: Boxity -> Arity -> TyCon
+tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i)     -- Build one specially
+tupleTyCon Boxed   i = fst (boxedTupleArr   ! i)
+tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
+
+tupleCon :: Boxity -> Arity -> DataCon
+tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i)       -- Build one specially
+tupleCon Boxed   i = snd (boxedTupleArr   ! i)
+tupleCon Unboxed i = snd (unboxedTupleArr ! i)
+
+boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
+boxedTupleArr   = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Boxed i)   | i <- [0..mAX_TUPLE_SIZE]]
+unboxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Unboxed i) | i <- [0..mAX_TUPLE_SIZE]]
+
+mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
+mk_tuple boxity arity = (tycon, tuple_con)
   where
-       tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con True
+       tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity
        tc_name = mkWiredInTyConName tc_uniq mod name_str tycon
-       tc_kind = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind
+       tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
+       res_kind | isBoxed boxity = boxedTypeKind
+                | otherwise      = unboxedTypeKind
+
+       tyvars   | isBoxed boxity = take arity alphaTyVars
+                | otherwise      = take arity openAlphaTyVars
 
        tuple_con = pcDataCon dc_uniq mod name_str tyvars [] tyvar_tys tycon
-       tyvars    = take arity alphaTyVars
        tyvar_tys = mkTyVarTys tyvars
-       (mod_name, name_str) = mkTupNameStr arity
-       tc_uniq   = mkTupleTyConUnique   arity
-       dc_uniq   = mkTupleDataConUnique arity
+       (mod_name, name_str) = mkTupNameStr boxity arity
+       tc_uniq   = mkTupleTyConUnique   boxity arity
+       dc_uniq   = mkTupleDataConUnique boxity arity
        mod       = mkPrelModule mod_name
 
-unitTyCon     = tupleTyCon 0
+unitTyCon     = tupleTyCon Boxed 0
 unitDataConId = dataConId (head (tyConDataCons unitTyCon))
 
-pairTyCon = tupleTyCon 2
-\end{code}
-
-%************************************************************************
-%*                                                                     *
-\subsection[TysWiredIn-ubx-tuples]{Unboxed Tuple Types}
-%*                                                                     *
-%************************************************************************
-
-\begin{code}
-unboxedTupleTyCon :: Arity -> TyCon
-unboxedTupleTyCon i | i > mAX_TUPLE_SIZE = fst (mk_unboxed_tuple i)
-                   | otherwise          = unboxedTupleTyConArr!i
-
-unboxedTupleCon :: Arity -> DataCon
-unboxedTupleCon i | i > mAX_TUPLE_SIZE = snd (mk_unboxed_tuple i)
-                 | otherwise          = unboxedTupleConArr!i
+pairTyCon = tupleTyCon Boxed 2
 
-unboxedTupleTyConArr :: Array Int TyCon
-unboxedTupleTyConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map fst ubx_tuples)
+unboxedSingletonTyCon   = tupleTyCon Unboxed 1
+unboxedSingletonDataCon = tupleCon   Unboxed 1
 
-unboxedTupleConArr :: Array Int DataCon
-unboxedTupleConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map snd ubx_tuples)
-
-ubx_tuples :: [(TyCon,DataCon)]
-ubx_tuples = [mk_unboxed_tuple i | i <- [0..mAX_TUPLE_SIZE]]
-
-mk_unboxed_tuple :: Int -> (TyCon,DataCon)
-mk_unboxed_tuple arity = (tycon, tuple_con)
-  where
-       tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con False
-       tc_name = mkWiredInTyConName tc_uniq mod name_str tycon
-       tc_kind = mkArrowKinds (map tyVarKind tyvars) unboxedTypeKind
-
-       tuple_con = pcDataCon dc_uniq mod name_str tyvars [] tyvar_tys tycon
-       tyvars    = take arity openAlphaTyVars
-       tyvar_tys = mkTyVarTys tyvars
-       (mod_name, name_str) = mkUbxTupNameStr arity
-       tc_uniq   = mkUbxTupleTyConUnique   arity
-       dc_uniq   = mkUbxTupleDataConUnique arity
-       mod       = mkPrelModule mod_name
-
-unboxedPairTyCon   = unboxedTupleTyCon 2
-unboxedPairDataCon = unboxedTupleCon 2
+unboxedPairTyCon   = tupleTyCon Unboxed 2
+unboxedPairDataCon = tupleCon   Unboxed 2
 \end{code}
 
 %************************************************************************
@@ -389,21 +349,40 @@ being the )
 \begin{code}
 isFFIArgumentTy :: Bool -> Type -> Bool
 -- Checks for valid argument type for a 'foreign import'
-isFFIArgumentTy is_safe ty = checkTyCon (legalOutgoingTyCon is_safe) ty
+isFFIArgumentTy is_safe ty = checkRepTyCon (legalOutgoingTyCon is_safe) ty
 
 isFFIExternalTy :: Type -> Bool
 -- Types that are allowed as arguments of a 'foreign export'
-isFFIExternalTy ty = checkTyCon legalIncomingTyCon ty
+isFFIExternalTy ty = checkRepTyCon legalIncomingTyCon ty
 
 isFFIResultTy :: Type -> Bool
 -- Types that are allowed as a result of a 'foreign import' or of a 'foreign export'
 -- Maybe we should distinguish between import and export, but 
 -- here we just choose the more restrictive 'incoming' predicate
 -- But we allow () as well
-isFFIResultTy ty = checkTyCon (\tc -> tc == unitTyCon || legalIncomingTyCon tc) ty
+isFFIResultTy ty = checkRepTyCon (\tc -> tc == unitTyCon || legalIncomingTyCon tc) ty
+
+isFFIDynArgumentTy :: Type -> Bool
+-- The argument type of a foreign import dynamic must be either Addr, or
+-- a newtype of Addr.
+isFFIDynArgumentTy = checkRepTyCon (== addrTyCon)
+
+isFFIDynResultTy :: Type -> Bool
+-- The result type of a foreign export dynamic must be either Addr, or
+-- a newtype of Addr.
+isFFIDynResultTy = checkRepTyCon (== addrTyCon)
+
+isFFILabelTy :: Type -> Bool
+-- The type of a foreign label must be either Addr, or
+-- a newtype of Addr.
+isFFILabelTy = checkRepTyCon (== addrTyCon)
+
+checkRepTyCon :: (TyCon -> Bool) -> Type -> Bool
+       -- look through newtypes
+checkRepTyCon check_tc ty = checkTyCon check_tc (repType ty)
 
 checkTyCon :: (TyCon -> Bool) -> Type -> Bool
-checkTyCon check_tc ty = case splitTyConApp_maybe (repType ty) of
+checkTyCon check_tc ty = case splitTyConApp_maybe ty of
                                Just (tycon, _) -> check_tc tycon
                                Nothing         -> False
 
@@ -423,8 +402,10 @@ legalIncomingTyCon :: TyCon -> Bool
 legalIncomingTyCon tc
   | getUnique tc `elem` [ foreignObjTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey ] 
   = False
+  -- It's also illegal to make foreign exports that take unboxed
+  -- arguments.  The RTS API currently can't invoke such things.  --SDM 7/2000
   | otherwise
-  = marshalableTyCon tc
+  = boxedMarshalableTyCon tc
 
 legalOutgoingTyCon :: Bool -> TyCon -> Bool
 -- Checks validity of types going from Haskell -> external world
@@ -438,7 +419,10 @@ legalOutgoingTyCon be_safe tc
 
 marshalableTyCon tc
   =  (opt_GlasgowExts && isUnLiftedTyCon tc)
-  || getUnique tc `elem` [ intTyConKey, int8TyConKey, int16TyConKey, int32TyConKey, int64TyConKey
+  || boxedMarshalableTyCon tc
+
+boxedMarshalableTyCon tc
+   = getUnique tc `elem` [ intTyConKey, int8TyConKey, int16TyConKey, int32TyConKey, int64TyConKey
                         , wordTyConKey, word8TyConKey, word16TyConKey, word32TyConKey, word64TyConKey
                         , floatTyConKey, doubleTyConKey
                         , addrTyConKey, charTyConKey, foreignObjTyConKey
@@ -589,11 +573,8 @@ done by enumeration\srcloc{lib/prelude/InTup?.hs}.
 \end{itemize}
 
 \begin{code}
-mkTupleTy :: Int -> [Type] -> Type
-mkTupleTy arity tys = mkTyConApp (tupleTyCon arity) tys
-
-mkUnboxedTupleTy :: Int -> [Type] -> Type
-mkUnboxedTupleTy arity tys = mkTyConApp (unboxedTupleTyCon arity) tys
+mkTupleTy :: Boxity -> Int -> [Type] -> Type
+mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
 
-unitTy    = mkTupleTy 0 []
+unitTy    = mkTupleTy Boxed 0 []
 \end{code}