2 % (c) The AQUA Project, Glasgow University, 1998
4 \section[TcForeign]{Typechecking \tr{foreign} declarations}
6 A foreign declaration is used to either give an externally
7 implemented function a Haskell type (and calling interface) or
8 give a Haskell function an external calling interface. Either way,
9 the range of argument and result types these functions can accommodate
10 is restricted to what the outside world understands (read C), and this
11 module checks to see if a foreign declaration has got a legal type.
20 #include "HsVersions.h"
22 import HsSyn ( HsDecl(..), ForeignDecl(..), HsExpr(..),
23 ExtName(..), isDynamic, MonoBinds(..),
24 OutPat(..), ForKind(..)
26 import RnHsSyn ( RenamedHsDecl, RenamedForeignDecl )
29 import TcEnv ( tcLookupClassByKey, newLocalId, tcLookupGlobalValue )
30 import TcType ( tcInstTcType, tcInstSigType, tcSplitRhoTy, zonkTcTypeToType )
31 import TcMonoType ( tcHsType )
32 import TcHsSyn ( TcMonoBinds, maybeBoxedPrimType, TypecheckedForeignDecl, TcIdOcc(..),
34 import TcExpr ( tcId, tcPolyExpr )
35 import Inst ( emptyLIE, LIE, plusLIE )
38 import ErrUtils ( Message )
39 import Id ( Id, idName )
40 import Name ( nameOccName )
41 import MkId ( mkUserId )
42 import Type ( isUnpointedType
50 import TyVar ( emptyTyVarEnv )
53 import TysWiredIn ( isFFIArgumentTy, isFFIResultTy,
54 isFFIExternalTy, isAddrTy
58 import Unify ( unifyTauTy )
61 import CmdLineOpts ( opt_GlasgowExts )
62 import Maybes ( maybeToBool )
67 tcForeignImports :: [RenamedHsDecl] -> TcM s ([Id], [TypecheckedForeignDecl])
68 tcForeignImports decls =
69 mapAndUnzipTc tcFImport [ foreign_decl | ForD foreign_decl <- decls, isForeignImport foreign_decl]
71 tcForeignExports :: [RenamedHsDecl] -> TcM s (LIE s, TcMonoBinds s, [TcForeignExportDecl s])
72 tcForeignExports decls =
73 foldlTc combine (emptyLIE, EmptyMonoBinds, [])
74 [ foreign_decl | ForD foreign_decl <- decls, isForeignExport foreign_decl]
76 combine (lie, binds, fs) fe =
77 tcFExport fe `thenTc ` \ (a_lie, b, f) ->
78 returnTc (lie `plusLIE` a_lie, b `AndMonoBinds` binds, f:fs)
81 isForeignImport :: ForeignDecl name -> Bool
82 isForeignImport (ForeignDecl _ k _ dyn _ _) =
85 FoExport -> case dyn of { Dynamic -> True ; _ -> False }
89 isForeignExport :: ForeignDecl name -> Bool
90 isForeignExport (ForeignDecl _ FoExport _ ext_nm _ _) = not (isDynamic ext_nm)
91 isForeignExport _ = False
96 tcFImport :: RenamedForeignDecl -> TcM s (Id, TypecheckedForeignDecl)
97 tcFImport fo@(ForeignDecl nm FoExport hs_ty Dynamic cconv src_loc) =
99 tcAddErrCtxt (foreignDeclCtxt fo) $
100 tcHsType hs_ty `thenTc` \ sig_ty ->
102 -- drop the foralls before inspecting the structure
103 -- of the foreign type.
104 (_, t_ty) = splitForAllTys sig_ty
106 case splitFunTys t_ty of
108 checkForeignExport True t_ty arg_tys res_ty `thenTc_`
109 let i = (mkUserId nm sig_ty) in
110 returnTc (i, (ForeignDecl i FoExport undefined Dynamic cconv src_loc))
112 tcFImport fo@(ForeignDecl nm FoLabel hs_ty ext_nm cconv src_loc) =
113 tcAddSrcLoc src_loc $
114 tcAddErrCtxt (foreignDeclCtxt fo) $
115 tcHsType hs_ty `thenTc` \ sig_ty ->
117 -- drop the foralls before inspecting the structure
118 -- of the foreign type.
119 (_, t_ty) = splitForAllTys sig_ty
121 check (isAddrTy t_ty) (illegalForeignTyErr False{-result-} sig_ty) `thenTc_`
122 let i = (mkUserId nm sig_ty) in
123 returnTc (i, (ForeignDecl i FoLabel undefined ext_nm cconv src_loc))
125 tcFImport fo@(ForeignDecl nm imp_exp hs_ty ext_nm cconv src_loc) =
126 tcAddSrcLoc src_loc $
127 tcAddErrCtxt (foreignDeclCtxt fo) $
129 tcHsType hs_ty `thenTc` \ ty ->
130 -- Check that the type has the right shape
131 -- and that the argument and result types are acceptable.
133 -- drop the foralls before inspecting the structure
134 -- of the foreign type.
135 (_, t_ty) = splitForAllTys ty
137 case splitFunTys t_ty of
139 checkForeignImport (isDynamic ext_nm) ty arg_tys res_ty `thenTc_`
140 let i = (mkUserId nm ty) in
141 returnTc (i, (ForeignDecl i imp_exp undefined ext_nm cconv src_loc))
143 tcFExport :: RenamedForeignDecl -> TcM s (LIE s, TcMonoBinds s, TcForeignExportDecl s)
144 tcFExport fo@(ForeignDecl nm imp_exp hs_ty ext_nm cconv src_loc) =
145 tcAddSrcLoc src_loc $
146 tcAddErrCtxt (foreignDeclCtxt fo) $
148 tcHsType hs_ty `thenTc` \ sig_ty ->
149 tcInstSigType sig_ty `thenNF_Tc` \ sig_tc_ty ->
150 tcPolyExpr (HsVar nm) sig_tc_ty `thenTc` \ (rhs, lie, _, _, _) ->
153 -- drop the foralls before inspecting the structure
154 -- of the foreign type.
155 (_, t_ty) = splitForAllTys sig_ty
157 case splitFunTys t_ty of
159 checkForeignExport False t_ty arg_tys res_ty `thenTc_`
160 -- we're exporting a function, but at a type possibly more constrained
161 -- than its declared/inferred type. Hence the need
162 -- to create a local binding which will call the exported function
163 -- at a particular type (and, maybe, overloading).
164 newLocalId (nameOccName nm) sig_tc_ty `thenNF_Tc` \ i ->
167 bind = VarMonoBind i2 rhs
169 returnTc (lie, bind, ForeignDecl i2 imp_exp undefined ext_nm cconv src_loc)
171 -- ToDo: fill the type field in with something sensible.
177 checkForeignImport :: Bool -> Type -> [Type] -> Type -> TcM s ()
178 checkForeignImport is_dynamic ty args res
180 -- * first arg has got to be an Addr
182 [] -> check False (illegalForeignTyErr True{-Arg-} ty)
184 check (isAddrTy x) (illegalForeignTyErr True{-Arg-} ty) `thenTc_`
185 mapTc (checkForeignArg isFFIArgumentTy) xs `thenTc_`
186 checkForeignRes (isFFIResultTy) res
188 mapTc (checkForeignArg isFFIArgumentTy) args `thenTc_`
189 checkForeignRes (isFFIResultTy) res
191 checkForeignExport :: Bool -> Type -> [Type] -> Type -> TcM s ()
192 checkForeignExport is_dynamic ty args res
194 -- * the first (and only!) arg has got to be a function type
195 -- * result type is an Addr
198 case splitFunTys arg of
200 mapTc (checkForeignArg isFFIExternalTy) arg_tys `thenTc_`
201 checkForeignRes (isFFIResultTy) res_ty `thenTc_`
202 checkForeignRes (isAddrTy) res
203 _ -> check False (illegalForeignTyErr True{-Arg-} ty)
205 mapTc (checkForeignArg isFFIExternalTy) args `thenTc_`
206 checkForeignRes (isFFIResultTy) res
208 check :: Bool -> Message -> TcM s ()
209 check True _ = returnTc ()
210 check _ the_err = addErrTc the_err `thenNF_Tc_` returnTc ()
212 checkForeignArg :: (Type -> Bool) -> Type -> TcM s ()
213 checkForeignArg pred ty = check (pred ty) (illegalForeignTyErr True{-Arg-} ty)
215 -- Check that the type has the form
216 -- (IO t) and that t satisfies the given predicate.
218 checkForeignRes :: (Type -> Bool) -> Type -> TcM s ()
219 checkForeignRes pred_res_ty ty =
220 case (splitTyConApp_maybe ty) of
222 | (uniqueOf io) == ioTyConKey &&
225 _ -> check False (illegalForeignTyErr False{-Res-} ty)
232 illegalForeignTyErr isArg ty
233 = hang (hsep [ptext SLIT("Unacceptable"), arg_or_res, ptext SLIT("type in foreign declaration")])
234 4 (hsep [ ptext SLIT("type:"), ppr ty])
237 | isArg = ptext SLIT("argument")
238 | otherwise = ptext SLIT("result")
241 hang (ptext SLIT("When checking a foreign declaration:"))