X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=ghc%2Fcompiler%2Fprelude%2FTysWiredIn.lhs;h=d9b7e9d7766ef2cfe6f6b33a06bfce401b7329bc;hb=67a402e25d3707ce4e031e809b874f8341032d23;hp=e689b5333d1696f0d36d84ae1c52b178a946c768;hpb=df10403c92440a304198b3096e65d52a1fe482ae;p=ghc-hetmet.git diff --git a/ghc/compiler/prelude/TysWiredIn.lhs b/ghc/compiler/prelude/TysWiredIn.lhs index e689b53..d9b7e9d 100644 --- a/ghc/compiler/prelude/TysWiredIn.lhs +++ b/ghc/compiler/prelude/TysWiredIn.lhs @@ -1,5 +1,5 @@ % -% (c) The GRASP Project, Glasgow University, 1994-1995 +% (c) The GRASP Project, Glasgow University, 1994-1998 % \section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types} @@ -10,8 +10,6 @@ This module tracks the ``state interface'' document, ``GHC prelude: types and operations.'' \begin{code} -#include "HsVersions.h" - module TysWiredIn ( addrDataCon, addrTy, @@ -24,156 +22,141 @@ module TysWiredIn ( consDataCon, doubleDataCon, doubleTy, + isDoubleTy, doubleTyCon, - falseDataCon, + falseDataCon, falseDataConId, floatDataCon, floatTy, + isFloatTy, floatTyCon, - getStatePairingConInfo, intDataCon, intTy, intTyCon, isIntTy, - inIntRange, integerTy, integerTyCon, - integerDataCon, + smallIntegerDataCon, + largeIntegerDataCon, isIntegerTy, - liftDataCon, - liftTyCon, listTyCon, - foreignObjTyCon, - mkLiftTy, + mkListTy, - mkPrimIoTy, - mkStateTy, - mkStateTransformerTy, - tupleTyCon, tupleCon, unitTyCon, unitDataCon, pairTyCon, pairDataCon, - mkTupleTy, nilDataCon, - primIoTyCon, - realWorldStateTy, - return2GMPsTyCon, - returnIntAndGMPTyCon, - stTyCon, - stDataCon, + + -- tuples + mkTupleTy, + tupleTyCon, tupleCon, + unitTyCon, unitDataConId, pairTyCon, + unboxedSingletonTyCon, unboxedSingletonDataCon, + unboxedPairTyCon, unboxedPairDataCon, + stablePtrTyCon, - stateAndAddrPrimTyCon, - stateAndArrayPrimTyCon, - stateAndByteArrayPrimTyCon, - stateAndCharPrimTyCon, - stateAndDoublePrimTyCon, - stateAndFloatPrimTyCon, - stateAndIntPrimTyCon, - stateAndForeignObjPrimTyCon, - stateAndMutableArrayPrimTyCon, - stateAndMutableByteArrayPrimTyCon, - stateAndPtrPrimTyCon, - stateAndStablePtrPrimTyCon, - stateAndSynchVarPrimTyCon, - stateAndWordPrimTyCon, - stateDataCon, - stateTyCon, - stRetDataCon, - stRetTyCon, - mkSTretTy, stringTy, - trueDataCon, + trueDataCon, trueDataConId, unitTy, + voidTy, wordDataCon, wordTy, - wordTyCon + wordTyCon, + + 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 ---ToDo:rm ---import Pretty ---import Util ---import PprType ---import Kind - -IMP_Ubiq() -#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201 -IMPORT_DELOOPER(TyLoop) ( mkDataCon, mkTupleCon, StrictnessMark(..) ) -IMPORT_DELOOPER(IdLoop) ( SpecEnv, nullSpecEnv, - mkTupleCon, mkDataCon, - StrictnessMark(..) ) -#else -import {-# SOURCE #-} Id ( Id, mkDataCon, mkTupleCon, StrictnessMark(..) ) -import {-# SOURCE #-} SpecEnv ( SpecEnv, nullSpecEnv ) -#endif +#include "HsVersions.h" + +import {-# SOURCE #-} MkId( mkDataConId, mkDataConWrapId ) -- friends: -import PrelMods +import PrelNames import TysPrim -- others: -import FieldLabel () -- -import Kind ( mkBoxedTypeKind, mkArrowKind ) -import Name ( mkWiredInTyConName, mkWiredInIdName ) -import TyCon ( mkDataTyCon, mkTupleTyCon, mkSynTyCon, - TyCon, SYN_IE(Arity) +import Constants ( mAX_TUPLE_SIZE ) +import Module ( Module, mkPrelModule ) +import Name ( mkWiredInTyConName, mkWiredInIdName, mkSrcOccFS, mkWorkerOcc, dataName ) +import DataCon ( DataCon, StrictnessMark(..), mkDataCon, dataConId ) +import Var ( TyVar, tyVarKind ) +import TyCon ( TyCon, AlgTyConFlavour(..), ArgVrcs, tyConDataCons, + mkAlgTyCon, mkSynTyCon, mkTupleTyCon, isUnLiftedTyCon ) -import BasicTypes ( SYN_IE(Module), NewOrData(..) ) -import Type ( SYN_IE(Type), mkTyConTy, applyTyCon, mkSigmaTy, mkTyVarTys, - mkFunTy, mkFunTys, maybeAppTyCon, maybeAppDataTyCon, - GenType(..), SYN_IE(ThetaType), SYN_IE(TauType) ) -import TyVar ( GenTyVar, SYN_IE(TyVar), tyVarKind, alphaTyVars, alphaTyVar, betaTyVar ) -import Lex ( mkTupNameStr ) +import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed ) +import Type ( Type, mkTyConTy, mkTyConApp, mkSigmaTy, mkTyVarTys, + mkArrowKinds, boxedTypeKind, unboxedTypeKind, + mkFunTy, mkFunTys, + splitTyConApp_maybe, repType, + TauType, ClassContext ) import Unique -import Util ( assoc, panic ) - ---nullSpecEnv = error "TysWiredIn:nullSpecEnv = " -addOneToSpecEnv = error "TysWiredIn:addOneToSpecEnv = " -pc_gen_specs = error "TysWiredIn:pc_gen_specs " -mkSpecInfo = error "TysWiredIn:SpecInfo" +import CmdLineOpts ( opt_GlasgowExts ) +import Array alpha_tyvar = [alphaTyVar] alpha_ty = [alphaTy] alpha_beta_tyvars = [alphaTyVar, betaTyVar] -pcDataTyCon, pcNewTyCon +pcRecDataTyCon, pcNonRecDataTyCon :: Unique{-TyConKey-} -> Module -> FAST_STRING - -> [TyVar] -> [Id] -> TyCon + -> [TyVar] -> ArgVrcs -> [DataCon] -> TyCon -pcDataTyCon = pc_tycon DataType -pcNewTyCon = pc_tycon NewType +pcRecDataTyCon = pcTyCon DataTyCon Recursive +pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive -pc_tycon new_or_data key mod str tyvars cons +pcTyCon new_or_data is_rec key mod str tyvars argvrcs cons = tycon where - tycon = mkDataTyCon name tycon_kind - tyvars [{-no context-}] cons [{-no derivings-}] + tycon = mkAlgTyCon name kind + tyvars + [] -- No context + argvrcs + cons + (length cons) + [] -- No derivings new_or_data + is_rec + name = mkWiredInTyConName key mod str tycon - tycon_kind = foldr (mkArrowKind . tyVarKind) mkBoxedTypeKind tyvars + kind = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind -pcSynTyCon key mod str kind arity tyvars expansion +pcSynTyCon key mod str kind arity tyvars expansion argvrcs -- this fun never used! = tycon where - tycon = mkSynTyCon name kind arity tyvars expansion + tycon = mkSynTyCon name kind arity tyvars expansion argvrcs name = mkWiredInTyConName key mod str tycon pcDataCon :: Unique{-DataConKey-} -> Module -> FAST_STRING - -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> SpecEnv -> Id -pcDataCon key mod str tyvars context arg_tys tycon specenv + -> [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 - name = mkWiredInIdName key mod str data_con - -pcGenerateDataSpecs :: Type -> SpecEnv -pcGenerateDataSpecs ty - = pc_gen_specs --False err err err ty - where - err = panic "PrelUtils:GenerateDataSpecs" + 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} @@ -181,37 +164,50 @@ pcGenerateDataSpecs ty %************************************************************************ \begin{code} -tupleTyCon :: Arity -> TyCon -tupleTyCon arity - = tycon - where - tycon = mkTupleTyCon uniq name arity - uniq = mkTupleTyConUnique arity - name = mkWiredInTyConName uniq mod_name (mkTupNameStr arity) tycon - mod_name | arity == 0 = pREL_BASE - | otherwise = pREL_TUP - -tupleCon :: Arity -> Id -tupleCon arity - = 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 - tuple_con = mkTupleCon arity name ty - uniq = mkTupleDataConUnique arity - name = mkWiredInIdName uniq mod_name (mkTupNameStr arity) tuple_con - mod_name | arity == 0 = pREL_BASE - | otherwise = pREL_TUP - ty = mkSigmaTy tyvars [] (mkFunTys tyvar_tys (applyTyCon tycon tyvar_tys)) - tyvars = take arity alphaTyVars - tyvar_tys = mkTyVarTys tyvars - tycon = tupleTyCon arity - -unitTyCon = tupleTyCon 0 -pairTyCon = tupleTyCon 2 - -unitDataCon = tupleCon 0 -pairDataCon = tupleCon 2 -\end{code} + 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 + + tyvars | isBoxed boxity = take arity alphaTyVars + | otherwise = take arity openAlphaTyVars + + 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 + +unitTyCon = tupleTyCon Boxed 0 +unitDataConId = dataConId (head (tyConDataCons unitTyCon)) + +pairTyCon = tupleTyCon Boxed 2 +unboxedSingletonTyCon = tupleTyCon Unboxed 1 +unboxedSingletonDataCon = tupleCon Unboxed 1 + +unboxedPairTyCon = tupleTyCon Unboxed 2 +unboxedPairDataCon = tupleCon Unboxed 2 +\end{code} %************************************************************************ %* * @@ -220,10 +216,26 @@ pairDataCon = tupleCon 2 %************************************************************************ \begin{code} +-- The Void type is represented as a data type with no constructors +-- It's a built in type (i.e. there's no way to define it in Haskell; +-- the nearest would be +-- +-- data Void = -- No constructors! +-- +-- ) It's boxed; there is only one value of this +-- type, namely "void", whose semantics is just bottom. +-- +-- Haskell 98 drops the definition of a Void type, so we just 'simulate' +-- voidTy using (). +voidTy = unitTy +\end{code} + + +\begin{code} charTy = mkTyConTy charTyCon -charTyCon = pcDataTyCon charTyConKey pREL_BASE SLIT("Char") [] [charDataCon] -charDataCon = pcDataCon charDataConKey pREL_BASE SLIT("C#") [] [] [charPrimTy] charTyCon nullSpecEnv +charTyCon = pcNonRecDataTyCon charTyConKey pREL_BASE SLIT("Char") [] [] [charDataCon] +charDataCon = pcDataCon charDataConKey pREL_BASE SLIT("C#") [] [] [charPrimTy] charTyCon stringTy = mkListTy charTy -- convenience only \end{code} @@ -231,91 +243,72 @@ stringTy = mkListTy charTy -- convenience only \begin{code} intTy = mkTyConTy intTyCon -intTyCon = pcDataTyCon intTyConKey pREL_BASE SLIT("Int") [] [intDataCon] -intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon nullSpecEnv +intTyCon = pcNonRecDataTyCon intTyConKey pREL_BASE SLIT("Int") [] [] [intDataCon] +intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon -isIntTy :: GenType (GenTyVar flexi) uvar -> Bool -isIntTy ty - = case (maybeAppDataTyCon ty) of - Just (tycon, [], _) -> uniqueOf 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 :: Type -> Bool +isIntTy = isTyCon intTyConKey \end{code} \begin{code} + wordTy = mkTyConTy wordTyCon -wordTyCon = pcDataTyCon wordTyConKey fOREIGN SLIT("Word") [] [wordDataCon] -wordDataCon = pcDataCon wordDataConKey fOREIGN SLIT("W#") [] [] [wordPrimTy] wordTyCon nullSpecEnv +wordTyCon = pcNonRecDataTyCon wordTyConKey pREL_ADDR SLIT("Word") [] [] [wordDataCon] +wordDataCon = pcDataCon wordDataConKey pREL_ADDR SLIT("W#") [] [] [wordPrimTy] wordTyCon \end{code} \begin{code} addrTy = mkTyConTy addrTyCon -addrTyCon = pcDataTyCon addrTyConKey fOREIGN SLIT("Addr") [] [addrDataCon] -addrDataCon = pcDataCon addrDataConKey fOREIGN SLIT("A#") [] [] [addrPrimTy] addrTyCon nullSpecEnv +addrTyCon = pcNonRecDataTyCon addrTyConKey pREL_ADDR SLIT("Addr") [] [] [addrDataCon] +addrDataCon = pcDataCon addrDataConKey pREL_ADDR SLIT("A#") [] [] [addrPrimTy] addrTyCon + +isAddrTy :: Type -> Bool +isAddrTy = isTyCon addrTyConKey \end{code} \begin{code} floatTy = mkTyConTy floatTyCon -floatTyCon = pcDataTyCon floatTyConKey pREL_BASE SLIT("Float") [] [floatDataCon] -floatDataCon = pcDataCon floatDataConKey pREL_BASE SLIT("F#") [] [] [floatPrimTy] floatTyCon nullSpecEnv -\end{code} +floatTyCon = pcNonRecDataTyCon floatTyConKey pREL_FLOAT SLIT("Float") [] [] [floatDataCon] +floatDataCon = pcDataCon floatDataConKey pREL_FLOAT SLIT("F#") [] [] [floatPrimTy] floatTyCon -\begin{code} -doubleTy = mkTyConTy doubleTyCon - -doubleTyCon = pcDataTyCon doubleTyConKey pREL_BASE SLIT("Double") [] [doubleDataCon] -doubleDataCon = pcDataCon doubleDataConKey pREL_BASE SLIT("D#") [] [] [doublePrimTy] doubleTyCon nullSpecEnv +isFloatTy :: Type -> Bool +isFloatTy = isTyCon floatTyConKey \end{code} \begin{code} -mkStateTy ty = applyTyCon stateTyCon [ty] -realWorldStateTy = mkStateTy realWorldTy -- a common use +doubleTy = mkTyConTy doubleTyCon -stateTyCon = pcDataTyCon stateTyConKey sT_BASE SLIT("State") alpha_tyvar [stateDataCon] -stateDataCon - = pcDataCon stateDataConKey sT_BASE SLIT("S#") - alpha_tyvar [] [mkStatePrimTy alphaTy] stateTyCon nullSpecEnv -\end{code} +isDoubleTy :: Type -> Bool +isDoubleTy = isTyCon doubleTyConKey -\begin{code} -mkSTretTy alpha beta = applyTyCon stRetTyCon [alpha,beta] - -stRetTyCon - = pcDataTyCon stRetTyConKey sT_BASE SLIT("STret") - alpha_beta_tyvars [stRetDataCon] -stRetDataCon - = pcDataCon stRetDataConKey sT_BASE SLIT("STret") - alpha_beta_tyvars [] [mkStatePrimTy alphaTy, betaTy] - stRetTyCon nullSpecEnv +doubleTyCon = pcNonRecDataTyCon doubleTyConKey pREL_FLOAT SLIT("Double") [] [] [doubleDataCon] +doubleDataCon = pcDataCon doubleDataConKey pREL_FLOAT SLIT("D#") [] [] [doublePrimTy] doubleTyCon \end{code} \begin{code} stablePtrTyCon - = pcDataTyCon stablePtrTyConKey fOREIGN SLIT("StablePtr") - alpha_tyvar [stablePtrDataCon] + = pcNonRecDataTyCon stablePtrTyConKey pREL_STABLE SLIT("StablePtr") + alpha_tyvar [(True,False)] [stablePtrDataCon] where stablePtrDataCon - = pcDataCon stablePtrDataConKey fOREIGN SLIT("StablePtr") - alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon nullSpecEnv + = pcDataCon stablePtrDataConKey pREL_STABLE SLIT("StablePtr") + alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon \end{code} \begin{code} foreignObjTyCon - = pcDataTyCon foreignObjTyConKey fOREIGN SLIT("ForeignObj") - [] [foreignObjDataCon] + = pcNonRecDataTyCon foreignObjTyConKey pREL_IO_BASE SLIT("ForeignObj") + [] [] [foreignObjDataCon] where foreignObjDataCon - = pcDataCon foreignObjDataConKey fOREIGN SLIT("ForeignObj") - [] [] [foreignObjPrimTy] foreignObjTyCon nullSpecEnv + = pcDataCon foreignObjDataConKey pREL_IO_BASE SLIT("ForeignObj") + [] [] [foreignObjPrimTy] foreignObjTyCon + +isForeignObjTy :: Type -> Bool +isForeignObjTy = isTyCon foreignObjTyConKey \end{code} %************************************************************************ @@ -326,243 +319,120 @@ foreignObjTyCon @Integer@ and its pals are not really primitive. @Integer@ itself, first: \begin{code} -integerTy :: GenType t u -integerTy = mkTyConTy integerTyCon - -integerTyCon = pcDataTyCon integerTyConKey pREL_BASE SLIT("Integer") [] [integerDataCon] - -integerDataCon = pcDataCon integerDataConKey pREL_BASE SLIT("J#") - [] [] [intPrimTy, intPrimTy, byteArrayPrimTy] integerTyCon nullSpecEnv - -isIntegerTy :: GenType (GenTyVar flexi) uvar -> Bool -isIntegerTy ty - = case (maybeAppDataTyCon ty) of - Just (tycon, [], _) -> uniqueOf tycon == integerTyConKey - _ -> False -\end{code} - -And the other pairing types: -\begin{code} -return2GMPsTyCon = pcDataTyCon return2GMPsTyConKey - pREL_NUM SLIT("Return2GMPs") [] [return2GMPsDataCon] - -return2GMPsDataCon - = pcDataCon return2GMPsDataConKey pREL_NUM SLIT("Return2GMPs") [] [] - [intPrimTy, intPrimTy, byteArrayPrimTy, - intPrimTy, intPrimTy, byteArrayPrimTy] return2GMPsTyCon nullSpecEnv - -returnIntAndGMPTyCon = pcDataTyCon returnIntAndGMPTyConKey - pREL_NUM SLIT("ReturnIntAndGMP") [] [returnIntAndGMPDataCon] +integerTy :: Type +integerTy = mkTyConTy integerTyCon -returnIntAndGMPDataCon - = pcDataCon returnIntAndGMPDataConKey pREL_NUM SLIT("ReturnIntAndGMP") [] [] - [intPrimTy, intPrimTy, intPrimTy, byteArrayPrimTy] returnIntAndGMPTyCon nullSpecEnv -\end{code} - -%************************************************************************ -%* * -\subsection[TysWiredIn-state-pairing]{``State-pairing'' types} -%* * -%************************************************************************ +integerTyCon = pcNonRecDataTyCon integerTyConKey pREL_NUM SLIT("Integer") + [] [] [smallIntegerDataCon, largeIntegerDataCon] -These boring types pair a \tr{State#} with another primitive type. -They are not really primitive, so they are given here, not in -\tr{TysPrim.lhs}. +smallIntegerDataCon = pcDataCon smallIntegerDataConKey pREL_NUM SLIT("S#") + [] [] [intPrimTy] integerTyCon +largeIntegerDataCon = pcDataCon largeIntegerDataConKey pREL_NUM SLIT("J#") + [] [] [intPrimTy, byteArrayPrimTy] integerTyCon -We fish one of these \tr{StateAnd#} things with -@getStatePairingConInfo@ (given a little way down). -\begin{code} -stateAndPtrPrimTyCon - = pcDataTyCon stateAndPtrPrimTyConKey sT_BASE SLIT("StateAndPtr#") - alpha_beta_tyvars [stateAndPtrPrimDataCon] -stateAndPtrPrimDataCon - = pcDataCon stateAndPtrPrimDataConKey sT_BASE SLIT("StateAndPtr#") - alpha_beta_tyvars [] [mkStatePrimTy alphaTy, betaTy] - stateAndPtrPrimTyCon nullSpecEnv - -stateAndCharPrimTyCon - = pcDataTyCon stateAndCharPrimTyConKey sT_BASE SLIT("StateAndChar#") - alpha_tyvar [stateAndCharPrimDataCon] -stateAndCharPrimDataCon - = pcDataCon stateAndCharPrimDataConKey sT_BASE SLIT("StateAndChar#") - alpha_tyvar [] [mkStatePrimTy alphaTy, charPrimTy] - stateAndCharPrimTyCon nullSpecEnv - -stateAndIntPrimTyCon - = pcDataTyCon stateAndIntPrimTyConKey sT_BASE SLIT("StateAndInt#") - alpha_tyvar [stateAndIntPrimDataCon] -stateAndIntPrimDataCon - = pcDataCon stateAndIntPrimDataConKey sT_BASE SLIT("StateAndInt#") - alpha_tyvar [] [mkStatePrimTy alphaTy, intPrimTy] - stateAndIntPrimTyCon nullSpecEnv - -stateAndWordPrimTyCon - = pcDataTyCon stateAndWordPrimTyConKey sT_BASE SLIT("StateAndWord#") - alpha_tyvar [stateAndWordPrimDataCon] -stateAndWordPrimDataCon - = pcDataCon stateAndWordPrimDataConKey sT_BASE SLIT("StateAndWord#") - alpha_tyvar [] [mkStatePrimTy alphaTy, wordPrimTy] - stateAndWordPrimTyCon nullSpecEnv - -stateAndAddrPrimTyCon - = pcDataTyCon stateAndAddrPrimTyConKey sT_BASE SLIT("StateAndAddr#") - alpha_tyvar [stateAndAddrPrimDataCon] -stateAndAddrPrimDataCon - = pcDataCon stateAndAddrPrimDataConKey sT_BASE SLIT("StateAndAddr#") - alpha_tyvar [] [mkStatePrimTy alphaTy, addrPrimTy] - stateAndAddrPrimTyCon nullSpecEnv - -stateAndStablePtrPrimTyCon - = pcDataTyCon stateAndStablePtrPrimTyConKey fOREIGN SLIT("StateAndStablePtr#") - alpha_beta_tyvars [stateAndStablePtrPrimDataCon] -stateAndStablePtrPrimDataCon - = pcDataCon stateAndStablePtrPrimDataConKey fOREIGN SLIT("StateAndStablePtr#") - alpha_beta_tyvars [] - [mkStatePrimTy alphaTy, applyTyCon stablePtrPrimTyCon [betaTy]] - stateAndStablePtrPrimTyCon nullSpecEnv - -stateAndForeignObjPrimTyCon - = pcDataTyCon stateAndForeignObjPrimTyConKey fOREIGN SLIT("StateAndForeignObj#") - alpha_tyvar [stateAndForeignObjPrimDataCon] -stateAndForeignObjPrimDataCon - = pcDataCon stateAndForeignObjPrimDataConKey fOREIGN SLIT("StateAndForeignObj#") - alpha_tyvar [] - [mkStatePrimTy alphaTy, applyTyCon foreignObjPrimTyCon []] - stateAndForeignObjPrimTyCon nullSpecEnv - -stateAndFloatPrimTyCon - = pcDataTyCon stateAndFloatPrimTyConKey sT_BASE SLIT("StateAndFloat#") - alpha_tyvar [stateAndFloatPrimDataCon] -stateAndFloatPrimDataCon - = pcDataCon stateAndFloatPrimDataConKey sT_BASE SLIT("StateAndFloat#") - alpha_tyvar [] [mkStatePrimTy alphaTy, floatPrimTy] - stateAndFloatPrimTyCon nullSpecEnv - -stateAndDoublePrimTyCon - = pcDataTyCon stateAndDoublePrimTyConKey sT_BASE SLIT("StateAndDouble#") - alpha_tyvar [stateAndDoublePrimDataCon] -stateAndDoublePrimDataCon - = pcDataCon stateAndDoublePrimDataConKey sT_BASE SLIT("StateAndDouble#") - alpha_tyvar [] [mkStatePrimTy alphaTy, doublePrimTy] - stateAndDoublePrimTyCon nullSpecEnv +isIntegerTy :: Type -> Bool +isIntegerTy = isTyCon integerTyConKey \end{code} -\begin{code} -stateAndArrayPrimTyCon - = pcDataTyCon stateAndArrayPrimTyConKey aRR_BASE SLIT("StateAndArray#") - alpha_beta_tyvars [stateAndArrayPrimDataCon] -stateAndArrayPrimDataCon - = pcDataCon stateAndArrayPrimDataConKey aRR_BASE SLIT("StateAndArray#") - alpha_beta_tyvars [] [mkStatePrimTy alphaTy, mkArrayPrimTy betaTy] - stateAndArrayPrimTyCon nullSpecEnv - -stateAndMutableArrayPrimTyCon - = pcDataTyCon stateAndMutableArrayPrimTyConKey aRR_BASE SLIT("StateAndMutableArray#") - alpha_beta_tyvars [stateAndMutableArrayPrimDataCon] -stateAndMutableArrayPrimDataCon - = pcDataCon stateAndMutableArrayPrimDataConKey aRR_BASE SLIT("StateAndMutableArray#") - alpha_beta_tyvars [] [mkStatePrimTy alphaTy, mkMutableArrayPrimTy alphaTy betaTy] - stateAndMutableArrayPrimTyCon nullSpecEnv - -stateAndByteArrayPrimTyCon - = pcDataTyCon stateAndByteArrayPrimTyConKey aRR_BASE SLIT("StateAndByteArray#") - alpha_tyvar [stateAndByteArrayPrimDataCon] -stateAndByteArrayPrimDataCon - = pcDataCon stateAndByteArrayPrimDataConKey aRR_BASE SLIT("StateAndByteArray#") - alpha_tyvar [] [mkStatePrimTy alphaTy, byteArrayPrimTy] - stateAndByteArrayPrimTyCon nullSpecEnv - -stateAndMutableByteArrayPrimTyCon - = pcDataTyCon stateAndMutableByteArrayPrimTyConKey aRR_BASE SLIT("StateAndMutableByteArray#") - alpha_tyvar [stateAndMutableByteArrayPrimDataCon] -stateAndMutableByteArrayPrimDataCon - = pcDataCon stateAndMutableByteArrayPrimDataConKey aRR_BASE SLIT("StateAndMutableByteArray#") - alpha_tyvar [] [mkStatePrimTy alphaTy, applyTyCon mutableByteArrayPrimTyCon alpha_ty] - stateAndMutableByteArrayPrimTyCon nullSpecEnv - -stateAndSynchVarPrimTyCon - = pcDataTyCon stateAndSynchVarPrimTyConKey cONC_BASE SLIT("StateAndSynchVar#") - alpha_beta_tyvars [stateAndSynchVarPrimDataCon] -stateAndSynchVarPrimDataCon - = pcDataCon stateAndSynchVarPrimDataConKey cONC_BASE SLIT("StateAndSynchVar#") - alpha_beta_tyvars [] [mkStatePrimTy alphaTy, mkSynchVarPrimTy alphaTy betaTy] - stateAndSynchVarPrimTyCon nullSpecEnv -\end{code} - -The ccall-desugaring mechanism uses this function to figure out how to -rebox the result. It's really a HACK, especially the part about -how many types to drop from \tr{tys_applied}. - -\begin{code} -getStatePairingConInfo - :: Type -- primitive type - -> (Id, -- state pair constructor for prim type - Type) -- type of state pair - -getStatePairingConInfo prim_ty - = case (maybeAppTyCon prim_ty) of - Nothing -> panic "getStatePairingConInfo:1" - Just (prim_tycon, tys_applied) -> - let - (pair_con, pair_tycon, num_tys) = assoc "getStatePairingConInfo" tbl prim_tycon - pair_ty = applyTyCon pair_tycon (realWorldTy : drop num_tys tys_applied) - in - (pair_con, pair_ty) - where - tbl = [ - (charPrimTyCon, (stateAndCharPrimDataCon, stateAndCharPrimTyCon, 0)), - (intPrimTyCon, (stateAndIntPrimDataCon, stateAndIntPrimTyCon, 0)), - (wordPrimTyCon, (stateAndWordPrimDataCon, stateAndWordPrimTyCon, 0)), - (addrPrimTyCon, (stateAndAddrPrimDataCon, stateAndAddrPrimTyCon, 0)), - (stablePtrPrimTyCon, (stateAndStablePtrPrimDataCon, stateAndStablePtrPrimTyCon, 0)), - (foreignObjPrimTyCon, (stateAndForeignObjPrimDataCon, stateAndForeignObjPrimTyCon, 0)), - (floatPrimTyCon, (stateAndFloatPrimDataCon, stateAndFloatPrimTyCon, 0)), - (doublePrimTyCon, (stateAndDoublePrimDataCon, stateAndDoublePrimTyCon, 0)), - (arrayPrimTyCon, (stateAndArrayPrimDataCon, stateAndArrayPrimTyCon, 0)), - (mutableArrayPrimTyCon, (stateAndMutableArrayPrimDataCon, stateAndMutableArrayPrimTyCon, 1)), - (byteArrayPrimTyCon, (stateAndByteArrayPrimDataCon, stateAndByteArrayPrimTyCon, 0)), - (mutableByteArrayPrimTyCon, (stateAndMutableByteArrayPrimDataCon, stateAndMutableByteArrayPrimTyCon, 1)), - (synchVarPrimTyCon, (stateAndSynchVarPrimDataCon, stateAndSynchVarPrimTyCon, 1)) - -- (PtrPrimTyCon, (stateAndPtrPrimDataCon, stateAndPtrPrimTyCon, 0)), - ] -\end{code} %************************************************************************ %* * -\subsection[TysWiredIn-ST]{The basic @_ST@ state-transformer type} +\subsection[TysWiredIn-ext-type]{External types} %* * %************************************************************************ -This is really just an ordinary synonym, except it is ABSTRACT. +The compiler's foreign function interface supports the passing of a +restricted set of types as arguments and results (the restricting factor +being the ) \begin{code} -mkStateTransformerTy s a = applyTyCon stTyCon [s, a] - -stTyCon = pcNewTyCon stTyConKey sT_BASE SLIT("ST") alpha_beta_tyvars [stDataCon] - -stDataCon = pcDataCon stDataConKey sT_BASE SLIT("ST") - alpha_beta_tyvars [] [ty] stTyCon nullSpecEnv - where - ty = mkFunTy (mkStatePrimTy alphaTy) (mkSTretTy alphaTy betaTy) +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 +-- 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 + +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 ty of + Just (tycon, _) -> check_tc tycon + Nothing -> False + +isTyCon :: Unique -> Type -> Bool +isTyCon uniq ty = checkTyCon (\tc -> uniq == getUnique tc) ty \end{code} -%************************************************************************ -%* * -\subsection[TysWiredIn-IO]{The @PrimIO@ monadic-I/O type} -%* * -%************************************************************************ +---------------------------------------------- +These chaps do the work; they are not exported +---------------------------------------------- \begin{code} -mkPrimIoTy a = mkStateTransformerTy realWorldTy a - -primIoTyCon - = pcSynTyCon - primIoTyConKey sT_BASE SLIT("PrimIO") - (mkBoxedTypeKind `mkArrowKind` mkBoxedTypeKind) - 1 alpha_tyvar (mkPrimIoTy alphaTy) +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). +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 + = boxedMarshalableTyCon 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) + || boxedMarshalableTyCon tc + +boxedMarshalableTyCon 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} @@ -614,10 +484,14 @@ primitive counterpart. \begin{code} boolTy = mkTyConTy boolTyCon -boolTyCon = pcDataTyCon boolTyConKey pREL_BASE SLIT("Bool") [] [falseDataCon, trueDataCon] +boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConKey + pREL_BASE SLIT("Bool") [] [] [falseDataCon, trueDataCon] -falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon nullSpecEnv -trueDataCon = pcDataCon trueDataConKey pREL_BASE SLIT("True") [] [] [] boolTyCon nullSpecEnv +falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon +trueDataCon = pcDataCon trueDataConKey pREL_BASE SLIT("True") [] [] [] boolTyCon + +falseDataConId = dataConId falseDataCon +trueDataConId = dataConId trueDataCon \end{code} %************************************************************************ @@ -636,19 +510,17 @@ data (,) a b = (,,) a b \end{verbatim} \begin{code} -mkListTy :: GenType t u -> GenType t u -mkListTy ty = applyTyCon listTyCon [ty] +mkListTy :: Type -> Type +mkListTy ty = mkTyConApp listTyCon [ty] -alphaListTy = mkSigmaTy alpha_tyvar [] (applyTyCon listTyCon alpha_ty) +alphaListTy = mkSigmaTy alpha_tyvar [] (mkTyConApp listTyCon alpha_ty) -listTyCon = pcDataTyCon listTyConKey pREL_BASE SLIT("[]") - alpha_tyvar [nilDataCon, consDataCon] +listTyCon = pcRecDataTyCon listTyConKey pREL_BASE SLIT("[]") + alpha_tyvar [(True,False)] [nilDataCon, consDataCon] nilDataCon = pcDataCon nilDataConKey pREL_BASE SLIT("[]") alpha_tyvar [] [] listTyCon - (pcGenerateDataSpecs alphaListTy) consDataCon = pcDataCon consDataConKey pREL_BASE SLIT(":") - alpha_tyvar [] [alphaTy, applyTyCon listTyCon alpha_ty] listTyCon - (pcGenerateDataSpecs alphaListTy) + alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon -- Interesting: polymorphic recursion would help here. -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy -- gets the over-specific type (Type -> Type) @@ -701,49 +573,8 @@ done by enumeration\srcloc{lib/prelude/InTup?.hs}. \end{itemize} \begin{code} -mkTupleTy :: Int -> [GenType t u] -> GenType t u - -mkTupleTy arity tys = applyTyCon (tupleTyCon arity) tys - -unitTy = mkTupleTy 0 [] -\end{code} - -%************************************************************************ -%* * -\subsection[TysWiredIn-_Lift]{@_Lift@ type: to support array indexing} -%* * -%************************************************************************ +mkTupleTy :: Boxity -> Int -> [Type] -> Type +mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys -Again, deeply turgid: \tr{data _Lift a = _Lift a}. - -\begin{code} -mkLiftTy ty = applyTyCon liftTyCon [ty] - -{- -mkLiftTy ty - = mkSigmaTy tvs theta (applyTyCon liftTyCon [tau]) - where - (tvs, theta, tau) = splitSigmaTy ty - -isLiftTy ty - = case (maybeAppDataTyConExpandingDicts tau) of - Just (tycon, tys, _) -> tycon == liftTyCon - Nothing -> False - where - (tvs, theta, tau) = splitSigmaTy ty --} - - -alphaLiftTy = mkSigmaTy alpha_tyvar [] (applyTyCon liftTyCon alpha_ty) - -liftTyCon - = pcDataTyCon liftTyConKey pREL_BASE SLIT("Lift") alpha_tyvar [liftDataCon] - -liftDataCon - = pcDataCon liftDataConKey pREL_BASE SLIT("Lift") - alpha_tyvar [] alpha_ty liftTyCon - ((pcGenerateDataSpecs alphaLiftTy) `addOneToSpecEnv` - (mkSpecInfo [Just realWorldStatePrimTy] 0 bottom)) - where - bottom = panic "liftDataCon:State# _RealWorld" +unitTy = mkTupleTy Boxed 0 [] \end{code}