doubleTy,
isDoubleTy,
doubleTyCon,
- falseDataCon,
+ falseDataCon, falseDataConId,
floatDataCon,
floatTy,
isFloatTy,
intTy,
intTyCon,
isIntTy,
- inIntRange,
integerTy,
integerTyCon,
-- tuples
mkTupleTy,
- tupleTyCon, tupleCon, unitTyCon, unitDataCon, pairTyCon, pairDataCon,
-
- -- unboxed tuples
- mkUnboxedTupleTy,
- unboxedTupleTyCon, unboxedTupleCon,
+ tupleTyCon, tupleCon,
+ unitTyCon, unitDataConId, pairTyCon,
+ unboxedSingletonTyCon, unboxedSingletonDataCon,
unboxedPairTyCon, unboxedPairDataCon,
stablePtrTyCon,
stringTy,
- trueDataCon,
+ trueDataCon, trueDataConId,
unitTy,
voidTy,
wordDataCon,
wordTy,
wordTyCon,
- isFFIArgumentTy, -- :: Type -> Bool
+ isFFIArgumentTy, -- :: Bool -> Type -> Bool
isFFIResultTy, -- :: Type -> Bool
isFFIExternalTy, -- :: Type -> Bool
isAddrTy, -- :: Type -> Bool
+ isForeignObjTy -- :: Type -> Bool
) where
#include "HsVersions.h"
-import {-# SOURCE #-} MkId( mkDataConId )
+import {-# SOURCE #-} MkId( mkDataConId, mkDataConWrapId )
-- friends:
-import PrelMods
+import PrelNames
import TysPrim
-- others:
import Constants ( mAX_TUPLE_SIZE )
-import Module ( Module )
-import Name ( mkWiredInTyConName, mkWiredInIdName, mkSrcOccFS, dataName )
-import DataCon ( DataCon, mkDataCon )
+import Module ( Module, mkPrelModule )
+import Name ( mkWiredInTyConName, mkWiredInIdName, mkSrcOccFS, mkWorkerOcc, dataName )
+import DataCon ( DataCon, StrictnessMark(..), mkDataCon, dataConId )
import Var ( TyVar, tyVarKind )
-import TyCon ( TyCon, ArgVrcs, mkAlgTyCon, mkSynTyCon, mkTupleTyCon )
-import BasicTypes ( Arity, NewOrData(..),
- RecFlag(..), StrictnessMark(..) )
+import TyCon ( TyCon, AlgTyConFlavour(..), ArgVrcs, tyConDataCons,
+ mkAlgTyCon, mkSynTyCon, mkTupleTyCon, isUnLiftedTyCon
+ )
+import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed )
import Type ( Type, mkTyConTy, mkTyConApp, mkSigmaTy, mkTyVarTys,
mkArrowKinds, boxedTypeKind, unboxedTypeKind,
- mkFunTy, mkFunTys, isUnLiftedType,
- splitTyConApp_maybe, splitAlgTyConApp_maybe,
- ThetaType, TauType )
-import PrimRep ( PrimRep(..) )
+ mkFunTy, mkFunTys,
+ splitTyConApp_maybe, repType,
+ TauType, ClassContext )
import Unique
import CmdLineOpts ( opt_GlasgowExts )
-import Util ( assoc )
-import Panic ( panic )
import Array
alpha_tyvar = [alphaTyVar]
alpha_ty = [alphaTy]
alpha_beta_tyvars = [alphaTyVar, betaTyVar]
-pcRecDataTyCon, pcNonRecDataTyCon, pcNonRecNewTyCon
+pcRecDataTyCon, pcNonRecDataTyCon
:: Unique{-TyConKey-} -> Module -> FAST_STRING
-> [TyVar] -> ArgVrcs -> [DataCon] -> TyCon
-pcRecDataTyCon = pcTyCon DataType Recursive
-pcNonRecDataTyCon = pcTyCon DataType NonRecursive
-pcNonRecNewTyCon = pcTyCon NewType NonRecursive
+pcRecDataTyCon = pcTyCon DataTyCon Recursive
+pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
pcTyCon new_or_data is_rec key mod str tyvars argvrcs cons
= tycon
[] -- No context
argvrcs
cons
+ (length cons)
[] -- No derivings
- Nothing -- Not a dictionary
new_or_data
is_rec
name = mkWiredInTyConName key mod str tycon
pcDataCon :: Unique{-DataConKey-} -> Module -> FAST_STRING
- -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> DataCon
-pcDataCon key mod str tyvars context arg_tys tycon
+ -> [TyVar] -> ClassContext -> [TauType] -> TyCon -> DataCon
+-- The unique is the first of two free uniques;
+-- the first is used for the datacon itself and the worker;
+-- the second is used for the wrapper.
+pcDataCon wrap_key mod str tyvars context arg_tys tycon
= data_con
where
- data_con = mkDataCon name
+ data_con = mkDataCon wrap_name
[ NotMarkedStrict | a <- arg_tys ]
[ {- no labelled fields -} ]
- tyvars context [] [] arg_tys tycon id
- name = mkWiredInIdName key mod (mkSrcOccFS dataName str) id
- id = mkDataConId data_con
+ tyvars context [] [] arg_tys tycon work_id wrap_id
+
+ work_occ = mkWorkerOcc wrap_occ
+ work_key = incrUnique wrap_key
+ work_name = mkWiredInIdName work_key mod work_occ work_id
+ work_id = mkDataConId work_name data_con
+
+ wrap_occ = mkSrcOccFS dataName str
+ wrap_name = mkWiredInIdName wrap_key mod wrap_occ wrap_id
+ wrap_id = mkDataConWrapId data_con
\end{code}
+
%************************************************************************
%* *
\subsection[TysWiredIn-tuples]{The tuple types}
%************************************************************************
\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
- tc_name = mkWiredInTyConName tc_uniq mod_name name_str tycon
- tc_kind = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind
-
- tuple_con = pcDataCon dc_uniq mod_name 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
-
-unitTyCon = tupleTyCon 0
-pairTyCon = tupleTyCon 2
-
-unitDataCon = tupleCon 0
-pairDataCon = tupleCon 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
+ 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) res_kind
+ res_kind | isBoxed boxity = boxedTypeKind
+ | otherwise = unboxedTypeKind
-unboxedTupleTyConArr :: Array Int TyCon
-unboxedTupleTyConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map fst ubx_tuples)
+ tyvars | isBoxed boxity = take arity alphaTyVars
+ | otherwise = take arity openAlphaTyVars
-unboxedTupleConArr :: Array Int DataCon
-unboxedTupleConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map snd ubx_tuples)
+ tuple_con = pcDataCon dc_uniq mod name_str tyvars [] tyvar_tys tycon
+ tyvar_tys = mkTyVarTys tyvars
+ (mod_name, name_str) = mkTupNameStr boxity arity
+ tc_uniq = mkTupleTyConUnique boxity arity
+ dc_uniq = mkTupleDataConUnique boxity arity
+ mod = mkPrelModule mod_name
-ubx_tuples :: [(TyCon,DataCon)]
-ubx_tuples = [mk_unboxed_tuple i | i <- [0..mAX_TUPLE_SIZE]]
+unitTyCon = tupleTyCon Boxed 0
+unitDataConId = dataConId (head (tyConDataCons unitTyCon))
-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 name_str tycon
- tc_kind = mkArrowKinds (map tyVarKind tyvars) unboxedTypeKind
+pairTyCon = tupleTyCon Boxed 2
- tuple_con = pcDataCon dc_uniq mod_name 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
+unboxedSingletonTyCon = tupleTyCon Unboxed 1
+unboxedSingletonDataCon = tupleCon Unboxed 1
-unboxedPairTyCon = unboxedTupleTyCon 2
-unboxedPairDataCon = unboxedTupleCon 2
+unboxedPairTyCon = tupleTyCon Unboxed 2
+unboxedPairDataCon = tupleCon Unboxed 2
\end{code}
%************************************************************************
intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon
isIntTy :: Type -> Bool
-isIntTy ty
- = case (splitAlgTyConApp_maybe ty) of
- Just (tycon, [], _) -> getUnique tycon == intTyConKey
- _ -> False
-
-inIntRange :: Integer -> Bool -- Tells if an integer lies in the legal range of Ints
-inIntRange i = (min_int <= i) && (i <= max_int)
-
-max_int, min_int :: Integer
-max_int = toInteger maxInt
-min_int = toInteger minInt
-
+isIntTy = isTyCon intTyConKey
\end{code}
\begin{code}
addrDataCon = pcDataCon addrDataConKey pREL_ADDR SLIT("A#") [] [] [addrPrimTy] addrTyCon
isAddrTy :: Type -> Bool
-isAddrTy ty
- = case (splitAlgTyConApp_maybe ty) of
- Just (tycon, [], _) -> getUnique tycon == addrTyConKey
- _ -> False
-
+isAddrTy = isTyCon addrTyConKey
\end{code}
\begin{code}
floatTy = mkTyConTy floatTyCon
-floatTyCon = pcNonRecDataTyCon floatTyConKey pREL_BASE SLIT("Float") [] [] [floatDataCon]
-floatDataCon = pcDataCon floatDataConKey pREL_BASE SLIT("F#") [] [] [floatPrimTy] floatTyCon
+floatTyCon = pcNonRecDataTyCon floatTyConKey pREL_FLOAT SLIT("Float") [] [] [floatDataCon]
+floatDataCon = pcDataCon floatDataConKey pREL_FLOAT SLIT("F#") [] [] [floatPrimTy] floatTyCon
isFloatTy :: Type -> Bool
-isFloatTy ty
- = case (splitAlgTyConApp_maybe ty) of
- Just (tycon, [], _) -> getUnique tycon == floatTyConKey
- _ -> False
-
+isFloatTy = isTyCon floatTyConKey
\end{code}
\begin{code}
doubleTy = mkTyConTy doubleTyCon
isDoubleTy :: Type -> Bool
-isDoubleTy ty
- = case (splitAlgTyConApp_maybe ty) of
- Just (tycon, [], _) -> getUnique tycon == doubleTyConKey
- _ -> False
+isDoubleTy = isTyCon doubleTyConKey
-doubleTyCon = pcNonRecDataTyCon doubleTyConKey pREL_BASE SLIT("Double") [] [] [doubleDataCon]
-doubleDataCon = pcDataCon doubleDataConKey pREL_BASE SLIT("D#") [] [] [doublePrimTy] doubleTyCon
+doubleTyCon = pcNonRecDataTyCon doubleTyConKey pREL_FLOAT SLIT("Double") [] [] [doubleDataCon]
+doubleDataCon = pcDataCon doubleDataConKey pREL_FLOAT SLIT("D#") [] [] [doublePrimTy] doubleTyCon
\end{code}
\begin{code}
foreignObjDataCon
= pcDataCon foreignObjDataConKey pREL_IO_BASE SLIT("ForeignObj")
[] [] [foreignObjPrimTy] foreignObjTyCon
+
+isForeignObjTy :: Type -> Bool
+isForeignObjTy = isTyCon foreignObjTyConKey
\end{code}
%************************************************************************
integerTy :: Type
integerTy = mkTyConTy integerTyCon
-integerTyCon = pcNonRecDataTyCon integerTyConKey pREL_BASE SLIT("Integer")
+integerTyCon = pcNonRecDataTyCon integerTyConKey pREL_NUM SLIT("Integer")
[] [] [smallIntegerDataCon, largeIntegerDataCon]
-smallIntegerDataCon = pcDataCon smallIntegerDataConKey pREL_BASE SLIT("S#")
+smallIntegerDataCon = pcDataCon smallIntegerDataConKey pREL_NUM SLIT("S#")
[] [] [intPrimTy] integerTyCon
-largeIntegerDataCon = pcDataCon largeIntegerDataConKey pREL_BASE SLIT("J#")
+largeIntegerDataCon = pcDataCon largeIntegerDataConKey pREL_NUM SLIT("J#")
[] [] [intPrimTy, byteArrayPrimTy] integerTyCon
isIntegerTy :: Type -> Bool
-isIntegerTy ty
- = case (splitAlgTyConApp_maybe ty) of
- Just (tycon, [], _) -> getUnique tycon == integerTyConKey
- _ -> False
+isIntegerTy = isTyCon integerTyConKey
\end{code}
being the )
\begin{code}
-isFFIArgumentTy :: Type -> Bool
-isFFIArgumentTy ty =
- (opt_GlasgowExts && isUnLiftedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
- case (splitAlgTyConApp_maybe ty) of
- Just (tycon, _, _) -> (getUnique tycon) `elem` primArgTyConKeys
- _ -> False
-
--- types that can be passed as arguments to "foreign" functions
-primArgTyConKeys
- = [ intTyConKey, int8TyConKey, int16TyConKey, int32TyConKey, int64TyConKey
- , wordTyConKey, word8TyConKey, word16TyConKey, word32TyConKey, word64TyConKey
- , floatTyConKey, doubleTyConKey
- , addrTyConKey, charTyConKey, foreignObjTyConKey
- , stablePtrTyConKey
- , byteArrayTyConKey, mutableByteArrayTyConKey
- ]
-
--- types that can be passed from the outside world into Haskell.
--- excludes (mutable) byteArrays.
-isFFIExternalTy :: Type -> Bool
-isFFIExternalTy ty =
- (opt_GlasgowExts && isUnLiftedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
- case (splitAlgTyConApp_maybe ty) of
- Just (tycon, _, _) ->
- let
- u_tycon = getUnique tycon
- in
- (u_tycon `elem` primArgTyConKeys) &&
- not (u_tycon `elem` notLegalExternalTyCons)
- _ -> False
+isFFIArgumentTy :: Bool -> Type -> Bool
+-- Checks for valid argument type for a 'foreign import'
+isFFIArgumentTy is_safe ty = checkRepTyCon (legalOutgoingTyCon is_safe) ty
+isFFIExternalTy :: Type -> Bool
+-- Types that are allowed as arguments of a 'foreign export'
+isFFIExternalTy ty = checkRepTyCon legalIncomingTyCon ty
isFFIResultTy :: Type -> Bool
-isFFIResultTy ty =
- not (isUnLiftedType ty) &&
- case (splitAlgTyConApp_maybe ty) of
- Just (tycon, _, _) ->
- let
- u_tycon = getUnique tycon
- in
- (u_tycon == getUnique unitTyCon) ||
- ((u_tycon `elem` primArgTyConKeys) &&
- not (u_tycon `elem` notLegalExternalTyCons))
- _ -> False
-
--- it's illegal to return foreign objects and (mutable)
+-- 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 = checkRepTyCon (\tc -> tc == unitTyCon || legalIncomingTyCon tc) ty
+
+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 ty of
+ Just (tycon, _) -> check_tc tycon
+ Nothing -> False
+
+isTyCon :: Unique -> Type -> Bool
+isTyCon uniq ty = checkTyCon (\tc -> uniq == getUnique tc) ty
+\end{code}
+
+----------------------------------------------
+These chaps do the work; they are not exported
+----------------------------------------------
+
+\begin{code}
+legalIncomingTyCon :: TyCon -> Bool
+-- It's illegal to return foreign objects and (mutable)
-- bytearrays from a _ccall_ / foreign declaration
-- (or be passed them as arguments in foreign exported functions).
-notLegalExternalTyCons =
- [ foreignObjTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey ]
-
+legalIncomingTyCon tc
+ | getUnique tc `elem` [ foreignObjTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey ]
+ = False
+ | otherwise
+ = marshalableTyCon tc
+
+legalOutgoingTyCon :: Bool -> TyCon -> Bool
+-- Checks validity of types going from Haskell -> external world
+-- The boolean is true for a 'safe' call (when we don't want to
+-- pass Haskell pointers to the world)
+legalOutgoingTyCon be_safe tc
+ | be_safe && getUnique tc `elem` [byteArrayTyConKey, mutableByteArrayTyConKey]
+ = False
+ | otherwise
+ = marshalableTyCon tc
+
+marshalableTyCon tc
+ = (opt_GlasgowExts && isUnLiftedTyCon tc)
+ || getUnique tc `elem` [ intTyConKey, int8TyConKey, int16TyConKey, int32TyConKey, int64TyConKey
+ , wordTyConKey, word8TyConKey, word16TyConKey, word32TyConKey, word64TyConKey
+ , floatTyConKey, doubleTyConKey
+ , addrTyConKey, charTyConKey, foreignObjTyConKey
+ , stablePtrTyConKey
+ , byteArrayTyConKey, mutableByteArrayTyConKey
+ , boolTyConKey
+ ]
\end{code}
+
%************************************************************************
%* *
\subsection[TysWiredIn-Bool]{The @Bool@ type}
\begin{code}
boolTy = mkTyConTy boolTyCon
-boolTyCon = pcTyCon EnumType NonRecursive boolTyConKey
+boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConKey
pREL_BASE SLIT("Bool") [] [] [falseDataCon, trueDataCon]
falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon
trueDataCon = pcDataCon trueDataConKey pREL_BASE SLIT("True") [] [] [] boolTyCon
+
+falseDataConId = dataConId falseDataCon
+trueDataConId = dataConId trueDataCon
\end{code}
%************************************************************************
\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}