+tcCheckFIType sig_ty arg_tys res_ty idecl@(CImport _ _ _ _ (CLabel _))
+ = checkCg checkCOrAsm `thenM_`
+ check (isFFILabelTy sig_ty) (illegalForeignTyErr empty sig_ty) `thenM_`
+ return idecl
+
+tcCheckFIType sig_ty arg_tys res_ty idecl@(CImport cconv _ _ _ CWrapper)
+ = -- Foreign wrapper (former f.e.d.)
+ -- The type must be of the form ft -> IO (FunPtr ft), where ft is a
+ -- valid foreign type. For legacy reasons ft -> IO (Ptr ft) as well
+ -- as ft -> IO Addr is accepted, too. The use of the latter two forms
+ -- is DEPRECATED, though.
+ checkCg checkCOrAsmOrInterp `thenM_`
+ (case arg_tys of
+ [arg1_ty] -> checkForeignArgs isFFIExternalTy arg1_tys `thenM_`
+ checkForeignRes nonIOok isFFIExportResultTy res1_ty `thenM_`
+ checkForeignRes mustBeIO isFFIDynResultTy res_ty `thenM_`
+ checkFEDArgs arg1_tys
+ where
+ (arg1_tys, res1_ty) = tcSplitFunTys arg1_ty
+ other -> addErrTc (illegalForeignTyErr empty sig_ty) ) `thenM_`
+ return idecl
+
+tcCheckFIType sig_ty arg_tys res_ty idecl@(CImport _ safety _ _ (CFunction target))
+ | isDynamicTarget target -- Foreign import dynamic
+ = checkCg checkCOrAsmOrInterp `thenM_`
+ case arg_tys of -- The first arg must be Ptr, FunPtr, or Addr
+ [] ->
+ check False (illegalForeignTyErr empty sig_ty) `thenM_`
+ return idecl
+ (arg1_ty:arg_tys) ->
+ getDOpts `thenM` \ dflags ->
+ check (isFFIDynArgumentTy arg1_ty)
+ (illegalForeignTyErr argument arg1_ty) `thenM_`
+ checkForeignArgs (isFFIArgumentTy dflags safety) arg_tys `thenM_`
+ checkForeignRes nonIOok (isFFIImportResultTy dflags) res_ty `thenM_`
+ return idecl
+ | otherwise -- Normal foreign import
+ = checkCg (checkCOrAsmOrDotNetOrInterp) `thenM_`
+ checkCTarget target `thenM_`
+ getDOpts `thenM` \ dflags ->
+ checkForeignArgs (isFFIArgumentTy dflags safety) arg_tys `thenM_`
+ checkForeignRes nonIOok (isFFIImportResultTy dflags) res_ty `thenM_`
+ return idecl
+
+-- This makes a convenient place to check
+-- that the C identifier is valid for C
+checkCTarget (StaticTarget str)
+ = checkCg checkCOrAsmOrDotNetOrInterp `thenM_`
+ check (isCLabelString str) (badCName str)
+\end{code}
+
+On an Alpha, with foreign export dynamic, due to a giant hack when
+building adjustor thunks, we only allow 4 integer arguments with
+foreign export dynamic (i.e., 32 bytes of arguments after padding each
+argument to a quadword, excluding floating-point arguments).
+
+The check is needed for both via-C and native-code routes
+
+\begin{code}
+#include "nativeGen/NCG.h"
+#if alpha_TARGET_ARCH
+checkFEDArgs arg_tys
+ = check (integral_args <= 32) err
+ where
+ integral_args = sum [ machRepByteWidth rep
+ | (rep,hint) <- map typeMachRepRep arg_tys,
+ hint /= FloatHint ]
+ err = ptext SLIT("On Alpha, I can only handle 4 non-floating-point arguments to foreign export dynamic")
+#else
+checkFEDArgs arg_tys = returnM ()
+#endif
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{Exports}
+%* *
+%************************************************************************
+
+\begin{code}
+tcForeignExports :: [LForeignDecl Name]
+ -> TcM (LHsBinds TcId, [LForeignDecl TcId])
+tcForeignExports decls
+ = foldlM combine (emptyLHsBinds, []) (filter isForeignExport decls)
+ where
+ combine (binds, fs) fe =
+ wrapLocSndM tcFExport fe `thenM` \ (b, f) ->
+ returnM (b `consBag` binds, f:fs)
+
+tcFExport :: ForeignDecl Name -> TcM (LHsBind Id, ForeignDecl Id)
+tcFExport fo@(ForeignExport (L loc nm) hs_ty spec isDeprec) =
+ addErrCtxt (foreignDeclCtxt fo) $
+
+ tcHsSigType (ForSigCtxt nm) hs_ty `thenM` \ sig_ty ->
+ tcCheckSigma (nlHsVar nm) sig_ty `thenM` \ rhs ->
+
+ tcCheckFEType sig_ty spec `thenM_`
+
+ -- we're exporting a function, but at a type possibly more
+ -- constrained than its declared/inferred type. Hence the need