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(Dynamic), isDynamicExtName, MonoBinds(..),
26 import RnHsSyn ( RenamedHsDecl, RenamedForeignDecl )
29 import TcEnv ( newLocalId )
30 import TcMonoType ( tcHsBoxedSigType )
31 import TcHsSyn ( TcMonoBinds, TypecheckedForeignDecl,
33 import TcExpr ( tcPolyExpr )
34 import Inst ( emptyLIE, LIE, plusLIE )
36 import ErrUtils ( Message )
37 import Id ( Id, mkVanillaId )
38 import Name ( nameOccName )
39 import Type ( splitFunTys
43 import TysWiredIn ( isFFIArgumentTy, isFFIResultTy,
44 isFFIExternalTy, isFFIDynArgumentTy, isFFIDynResultTy,
48 import PrelNames ( hasKey, ioTyConKey )
54 tcForeignImports :: [RenamedHsDecl] -> TcM ([Id], [TypecheckedForeignDecl])
55 tcForeignImports decls =
56 mapAndUnzipTc tcFImport [ foreign_decl | ForD foreign_decl <- decls, isForeignImport foreign_decl]
58 tcForeignExports :: [RenamedHsDecl] -> TcM (LIE, TcMonoBinds, [TcForeignExportDecl])
59 tcForeignExports decls =
60 foldlTc combine (emptyLIE, EmptyMonoBinds, [])
61 [ foreign_decl | ForD foreign_decl <- decls, isForeignExport foreign_decl]
63 combine (lie, binds, fs) fe =
64 tcFExport fe `thenTc ` \ (a_lie, b, f) ->
65 returnTc (lie `plusLIE` a_lie, b `AndMonoBinds` binds, f:fs)
68 isForeignImport :: ForeignDecl name -> Bool
69 isForeignImport (ForeignDecl _ k _ dyn _ _) =
72 FoExport -> case dyn of { Dynamic -> True ; _ -> False }
76 isForeignExport :: ForeignDecl name -> Bool
77 isForeignExport (ForeignDecl _ FoExport _ ext_nm _ _) = not (isDynamicExtName ext_nm)
78 isForeignExport _ = False
83 tcFImport :: RenamedForeignDecl -> TcM (Id, TypecheckedForeignDecl)
84 tcFImport fo@(ForeignDecl nm FoExport hs_ty Dynamic cconv src_loc) =
86 tcAddErrCtxt (foreignDeclCtxt fo) $
87 tcHsBoxedSigType hs_ty `thenTc` \ sig_ty ->
89 -- drop the foralls before inspecting the structure
90 -- of the foreign type.
91 (_, t_ty) = splitForAllTys sig_ty
93 case splitFunTys t_ty of
95 checkForeignExport True t_ty arg_tys res_ty `thenTc_`
96 let i = (mkVanillaId nm sig_ty) in
97 returnTc (i, (ForeignDecl i FoExport undefined Dynamic cconv src_loc))
99 tcFImport fo@(ForeignDecl nm FoLabel hs_ty ext_nm cconv src_loc) =
100 tcAddSrcLoc src_loc $
101 tcAddErrCtxt (foreignDeclCtxt fo) $
102 tcHsBoxedSigType hs_ty `thenTc` \ sig_ty ->
104 -- drop the foralls before inspecting the structure
105 -- of the foreign type.
106 (_, t_ty) = splitForAllTys sig_ty
108 check (isFFILabelTy t_ty)
109 (illegalForeignTyErr False{-result-} sig_ty) `thenTc_`
110 let i = (mkVanillaId nm sig_ty) in
111 returnTc (i, (ForeignDecl i FoLabel undefined ext_nm cconv src_loc))
113 tcFImport fo@(ForeignDecl nm imp_exp@(FoImport isUnsafe) hs_ty ext_nm cconv src_loc) =
114 tcAddSrcLoc src_loc $
115 tcAddErrCtxt (foreignDeclCtxt fo) $
117 tcHsBoxedSigType hs_ty `thenTc` \ ty ->
118 -- Check that the type has the right shape
119 -- and that the argument and result types are acceptable.
121 -- drop the foralls before inspecting the structure
122 -- of the foreign type.
123 (_, t_ty) = splitForAllTys ty
125 case splitFunTys t_ty of
127 checkForeignImport (isDynamicExtName ext_nm) (not isUnsafe) ty arg_tys res_ty `thenTc_`
128 let i = (mkVanillaId nm ty) in
129 returnTc (i, (ForeignDecl i imp_exp undefined ext_nm cconv src_loc))
131 tcFExport :: RenamedForeignDecl -> TcM (LIE, TcMonoBinds, TcForeignExportDecl)
132 tcFExport fo@(ForeignDecl nm imp_exp hs_ty ext_nm cconv src_loc) =
133 tcAddSrcLoc src_loc $
134 tcAddErrCtxt (foreignDeclCtxt fo) $
136 tcHsBoxedSigType hs_ty `thenTc` \ sig_ty ->
137 tcPolyExpr (HsVar nm) sig_ty `thenTc` \ (rhs, lie, _, _, _) ->
140 -- drop the foralls before inspecting the structure
141 -- of the foreign type.
142 (_, t_ty) = splitForAllTys sig_ty
144 case splitFunTys t_ty of
146 checkForeignExport False t_ty arg_tys res_ty `thenTc_`
147 -- we're exporting a function, but at a type possibly more constrained
148 -- than its declared/inferred type. Hence the need
149 -- to create a local binding which will call the exported function
150 -- at a particular type (and, maybe, overloading).
151 newLocalId (nameOccName nm) sig_ty src_loc `thenNF_Tc` \ i ->
153 bind = VarMonoBind i rhs
155 returnTc (lie, bind, ForeignDecl i imp_exp undefined ext_nm cconv src_loc)
157 -- ToDo: fill the type field in with something sensible.
163 checkForeignImport :: Bool -> Bool -> Type -> [Type] -> Type -> TcM ()
164 checkForeignImport is_dynamic is_safe ty args res
166 -- * first arg has got to be an Addr
168 [] -> check False (illegalForeignTyErr True{-Arg-} ty)
170 check (isFFIDynArgumentTy x) (illegalForeignTyErr True{-Arg-} ty) `thenTc_`
171 mapTc (checkForeignArg (isFFIArgumentTy is_safe)) xs `thenTc_`
172 checkForeignRes True {-NonIO ok-} isFFIResultTy res
174 mapTc (checkForeignArg (isFFIArgumentTy is_safe)) args `thenTc_`
175 checkForeignRes True {-NonIO ok-} isFFIResultTy res
177 checkForeignExport :: Bool -> Type -> [Type] -> Type -> TcM ()
178 checkForeignExport is_dynamic ty args res
180 -- * the first (and only!) arg has got to be a function type
181 -- and it must return IO t
182 -- * result type is IO Addr
185 case splitFunTys arg of
187 mapTc (checkForeignArg isFFIExternalTy) arg_tys `thenTc_`
188 checkForeignRes True {-NonIO ok-} isFFIResultTy res_ty `thenTc_`
189 checkForeignRes False {-Must be IO-} isFFIDynResultTy res
190 _ -> check False (illegalForeignTyErr True{-Arg-} ty)
192 mapTc (checkForeignArg isFFIExternalTy) args `thenTc_`
193 checkForeignRes True {-NonIO ok-} isFFIResultTy res
195 checkForeignArg :: (Type -> Bool) -> Type -> TcM ()
196 checkForeignArg pred ty = check (pred ty) (illegalForeignTyErr True{-Arg-} ty)
198 -- Check that the type has the form
199 -- (IO t) or (t) , and that t satisfies the given predicate.
201 checkForeignRes :: Bool -> (Type -> Bool) -> Type -> TcM ()
202 checkForeignRes non_io_result_ok pred_res_ty ty =
203 case (splitTyConApp_maybe ty) of
205 | io `hasKey` ioTyConKey && pred_res_ty res_ty
208 -> check (non_io_result_ok && pred_res_ty ty)
209 (illegalForeignTyErr False{-Res-} ty)
215 check :: Bool -> Message -> TcM ()
216 check True _ = returnTc ()
217 check _ the_err = addErrTc the_err `thenNF_Tc_` returnTc ()
219 illegalForeignTyErr isArg ty
220 = hang (hsep [ptext SLIT("Unacceptable"), arg_or_res, ptext SLIT("type in foreign declaration:")])
224 | isArg = ptext SLIT("argument")
225 | otherwise = ptext SLIT("result")
228 hang (ptext SLIT("When checking declaration:"))