+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 (emptyBag, []) (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