Template Haskell: allow type splices
[ghc-hetmet.git] / compiler / typecheck / TcForeign.lhs
index bbf181c..185e592 100644 (file)
@@ -25,6 +25,7 @@ import HsSyn
 import TcRnMonad
 import TcHsType
 import TcExpr
+import TcEnv
 
 import ForeignCall
 import ErrUtils
@@ -32,7 +33,6 @@ import Id
 #if alpha_TARGET_ARCH
 import Type
 import SMRep
-import MachOp
 #endif
 import Name
 import OccName
@@ -41,7 +41,6 @@ import DynFlags
 import Outputable
 import SrcLoc
 import Bag
-import Unique
 import FastString
 \end{code}
 
@@ -70,22 +69,22 @@ tcForeignImports decls
 
 tcFImport :: ForeignDecl Name -> TcM (Id, ForeignDecl Id)
 tcFImport fo@(ForeignImport (L loc nm) hs_ty imp_decl)
- = addErrCtxt (foreignDeclCtxt fo)  $ do
-   sig_ty <- tcHsSigType (ForSigCtxt nm) hs_ty
-   let 
-      -- drop the foralls before inspecting the structure
-      -- of the foreign type.
-       (_, t_ty)         = tcSplitForAllTys sig_ty
-       (arg_tys, res_ty) = tcSplitFunTys t_ty
-       id                = mkLocalId nm sig_ty
+ = addErrCtxt (foreignDeclCtxt fo)  $ 
+   do { sig_ty <- tcHsSigType (ForSigCtxt nm) hs_ty
+      ; let 
+          -- Drop the foralls before inspecting the
+          -- structure of the foreign type.
+           (_, t_ty)         = tcSplitForAllTys sig_ty
+           (arg_tys, res_ty) = tcSplitFunTys t_ty
+           id                = mkLocalId nm sig_ty
                -- Use a LocalId to obey the invariant that locally-defined 
                -- things are LocalIds.  However, it does not need zonking,
                -- (so TcHsSyn.zonkForeignExports ignores it).
    
-   imp_decl' <- tcCheckFIType sig_ty arg_tys res_ty imp_decl
-   -- can't use sig_ty here because it :: Type and we need HsType Id
-   -- hence the undefined
-   return (id, ForeignImport (L loc id) undefined imp_decl')
+      ; imp_decl' <- tcCheckFIType sig_ty arg_tys res_ty imp_decl
+         -- Can't use sig_ty here because sig_ty :: Type and 
+        -- we need HsType Id hence the undefined
+      ; return (id, ForeignImport (L loc id) undefined imp_decl') }
 tcFImport d = pprPanic "tcFImport" (ppr d)
 \end{code}
 
@@ -97,7 +96,7 @@ tcCheckFIType _ arg_tys res_ty (DNImport spec) = do
     checkCg checkDotnet
     dflags <- getDOpts
     checkForeignArgs (isFFIDotnetTy dflags) arg_tys
-    checkForeignRes True{-non IO ok-} (isFFIDotnetTy dflags) res_ty
+    checkForeignRes nonIOok (isFFIDotnetTy dflags) res_ty
     let (DNCallSpec isStatic kind _ _ _ _) = spec
     case kind of
        DNMethod | not isStatic ->
@@ -109,10 +108,12 @@ tcCheckFIType _ arg_tys res_ty (DNImport spec) = do
        _ -> return ()
     return (DNImport (withDNTypes spec (map toDNType arg_tys) (toDNType res_ty)))
 
-tcCheckFIType sig_ty _ _ idecl@(CImport _ _ _ _ (CLabel _)) = do
-    checkCg checkCOrAsm
-    check (isFFILabelTy sig_ty) (illegalForeignTyErr empty sig_ty)
-    return idecl
+tcCheckFIType sig_ty arg_tys res_ty idecl@(CImport _ _ _ _ (CLabel _)) 
+  = ASSERT( null arg_tys )
+    do { checkCg checkCOrAsm
+       ; check (isFFILabelTy res_ty) (illegalForeignTyErr empty sig_ty)
+       ; return idecl }             -- NB check res_ty not sig_ty!
+                                    --    In case sig_ty is (forall a. ForeignPtr a)
 
 tcCheckFIType sig_ty arg_tys res_ty idecl@(CImport cconv _ _ _ CWrapper) = do
        -- Foreign wrapper (former f.e.d.)
@@ -189,7 +190,7 @@ checkFEDArgs :: [Type] -> TcM ()
 checkFEDArgs arg_tys
   = check (integral_args <= 32) err
   where
-    integral_args = sum [ (machRepByteWidth . argMachRep . primRepToCgRep) prim_rep
+    integral_args = sum [ (widthInBytes . argMachRep . primRepToCgRep) prim_rep
                        | prim_rep <- map typePrimRep arg_tys,
                          primRepHint prim_rep /= FloatHint ]
     err = ptext (sLit "On Alpha, I can only handle 32 bytes of non-floating-point arguments to foreign export dynamic")
@@ -229,24 +230,12 @@ tcFExport fo@(ForeignExport (L loc nm) hs_ty spec) =
          -- to create a local binding which will call the exported function
          -- at a particular type (and, maybe, overloading).
 
-   uniq <- newUnique
-   mod <- getModule
-   let
-          -- We need to give a name to the new top-level binding that
-          -- is *stable* (i.e. the compiler won't change it later),
-          -- because this name will be referred to by the C code stub.
-          -- Furthermore, the name must be unique (see #1533).  If the
-          -- same function is foreign-exported multiple times, the
-          -- top-level bindings generated must not have the same name.
-          -- Hence we create an External name (doesn't change), and we
-          -- append a Unique to the string right here.
-        uniq_str = showSDoc (pprUnique uniq)
-        occ = mkVarOcc (occNameString (getOccName nm) ++ '_' : uniq_str)
-        gnm  = mkExternalName uniq mod (mkForeignExportOcc occ) loc
-       id   = mkExportedLocalId gnm sig_ty
-       bind = L loc (VarBind id rhs)
-
-   return (bind, ForeignExport (L loc id) undefined spec)
+
+   -- We need to give a name to the new top-level binding that
+   -- is *stable* (i.e. the compiler won't change it later),
+   -- because this name will be referred to by the C code stub.
+   id  <- mkStableIdFromName nm sig_ty loc mkForeignExportOcc
+   return (L loc (VarBind id rhs), ForeignExport (L loc id) undefined spec)
 tcFExport d = pprPanic "tcFExport" (ppr d)
 \end{code}