[project @ 2003-11-03 16:00:57 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcSplice.lhs
index 5f05962..45d071c 100644 (file)
@@ -9,32 +9,31 @@ module TcSplice( tcSpliceExpr, tcSpliceDecls, tcBracket ) where
 #include "HsVersions.h"
 
 import HscMain         ( compileExpr )
-import TcRnDriver      ( importSupportingDecls, tcTopSrcDecls )
+import TcRnDriver      ( tcTopSrcDecls )
        -- These imports are the reason that TcSplice 
        -- is very high up the module hierarchy
 
 import qualified Language.Haskell.THSyntax as Meta
 
-import HscTypes                ( HscEnv(..), GhciMode(..), PersistentCompilerState(..), unQualInScope )
+import HscTypes                ( HscEnv(..) )
 import HsSyn           ( HsBracket(..), HsExpr(..) )
 import Convert         ( convertToHsExpr, convertToHsDecls )
 import RnExpr          ( rnExpr )
 import RdrHsSyn                ( RdrNameHsExpr, RdrNameHsDecl )
 import RnHsSyn         ( RenamedHsExpr )
-import TcExpr          ( tcMonoExpr )
+import TcExpr          ( tcCheckRho, tcMonoExpr )
 import TcHsSyn         ( TcExpr, TypecheckedHsExpr, mkHsLet, zonkTopExpr )
 import TcSimplify      ( tcSimplifyTop, tcSimplifyBracket )
-import TcUnify         ( unifyTauTy )
+import TcUnify         ( Expected, zapExpectedTo, zapExpectedType )
 import TcType          ( TcType, openTypeKind, mkAppTy )
-import TcEnv           ( spliceOK, tcMetaTy, tcWithTempInstEnv, bracketOK )
-import TcRnTypes       ( TopEnv(..) )
-import TcMType         ( newTyVarTy, zapToType, UserTypeCtxt(ExprSigCtxt) )
-import TcMonoType      ( tcHsSigType )
+import TcEnv           ( spliceOK, tcMetaTy, bracketOK )
+import TcMType         ( newTyVarTy, UserTypeCtxt(ExprSigCtxt) )
+import TcHsType                ( tcHsSigType )
 import Name            ( Name )
 import TcRnMonad
 
 import TysWiredIn      ( mkListTy )
-import DsMeta          ( exprTyConName, declTyConName, typeTyConName, decTyConName, qTyConName )
+import DsMeta          ( expQTyConName, typeQTyConName, decTyConName, qTyConName )
 import ErrUtils (Message)
 import Outputable
 import Panic           ( showException )
@@ -54,7 +53,7 @@ tcSpliceDecls :: RenamedHsExpr -> TcM [RdrNameHsDecl]
 
 tcSpliceExpr :: Name 
             -> RenamedHsExpr
-            -> TcType
+            -> Expected TcType
             -> TcM TcExpr
 
 #ifndef GHCI
@@ -70,7 +69,7 @@ tcSpliceDecls e     = pprPanic "Cant do tcSpliceDecls without GHCi" (ppr e)
 %************************************************************************
 
 \begin{code}
-tcBracket :: HsBracket Name -> TcType -> TcM TcExpr
+tcBracket :: HsBracket Name -> Expected TcType -> TcM TcExpr
 tcBracket brack res_ty
   = getStage                           `thenM` \ level ->
     case bracketOK level of {
@@ -88,7 +87,8 @@ tcBracket brack res_ty
     )                                  `thenM` \ (meta_ty, lie) ->
     tcSimplifyBracket lie              `thenM_`  
 
-    unifyTauTy res_ty meta_ty          `thenM_`
+       -- Make the expected type have the right shape
+    zapExpectedTo res_ty meta_ty       `thenM_`
 
        -- Return the original expression, not the type-decorated one
     readMutVar pending_splices         `thenM` \ pendings ->
@@ -97,25 +97,23 @@ tcBracket brack res_ty
 
 tc_bracket :: HsBracket Name -> TcM TcType
 tc_bracket (ExpBr expr) 
-  = newTyVarTy openTypeKind            `thenM` \ any_ty ->
-    tcMonoExpr expr any_ty             `thenM_`
-    tcMetaTy exprTyConName
+  = newTyVarTy openTypeKind    `thenM` \ any_ty ->
+    tcCheckRho expr any_ty     `thenM_`
+    tcMetaTy expQTyConName
        -- Result type is Expr (= Q Exp)
 
 tc_bracket (TypBr typ) 
   = tcHsSigType ExprSigCtxt typ                `thenM_`
-    tcMetaTy typeTyConName
+    tcMetaTy typeQTyConName
        -- Result type is Type (= Q Typ)
 
 tc_bracket (DecBr decls)
-  = tcWithTempInstEnv (tcTopSrcDecls decls)    `thenM_`
-       -- Typecheck the declarations, dicarding any side effects
-       -- on the instance environment (which is in a mutable variable)
-       -- and the extended environment.  We'll get all that stuff
-       -- later, when we splice it in
-
-    tcMetaTy decTyConName              `thenM` \ decl_ty ->
-    tcMetaTy qTyConName                        `thenM` \ q_ty ->
+  = tcTopSrcDecls decls                `thenM_`
+       -- Typecheck the declarations, dicarding the result
+       -- We'll get all that stuff later, when we splice it in
+
+    tcMetaTy decTyConName      `thenM` \ decl_ty ->
+    tcMetaTy qTyConName                `thenM` \ q_ty ->
     returnM (mkAppTy q_ty (mkListTy decl_ty))
        -- Result type is Q [Dec]
 \end{code}
@@ -144,11 +142,11 @@ tcSpliceExpr name expr res_ty
        -- Here (h 4) :: Q Exp
        -- but $(h 4) :: forall a.a     i.e. anything!
 
-    zapToType res_ty                           `thenM_`
-    tcMetaTy exprTyConName                     `thenM` \ meta_exp_ty ->
+    zapExpectedType res_ty                     `thenM_`
+    tcMetaTy expQTyConName                     `thenM` \ meta_exp_ty ->
     setStage (Splice next_level) (
        setLIEVar lie_var          $
-       tcMonoExpr expr meta_exp_ty
+       tcCheckRho expr meta_exp_ty
     )                                          `thenM` \ expr' ->
 
        -- Write the pending splice into the bucket
@@ -166,7 +164,7 @@ tcSpliceExpr name expr res_ty
 -- inner escape before dealing with the outer one
 
 tcTopSplice expr res_ty
-  = tcMetaTy exprTyConName             `thenM` \ meta_exp_ty ->
+  = tcMetaTy expQTyConName             `thenM` \ meta_exp_ty ->
 
        -- Typecheck the expression
     tcTopSpliceExpr expr meta_exp_ty   `thenM` \ zonked_q_expr ->
@@ -185,10 +183,9 @@ tcTopSplice expr res_ty
 
     showSplice "expression" 
               zonked_q_expr (ppr expr2)        `thenM_`
-    initRn SourceMode (rnExpr expr2)           `thenM` \ (exp3, fvs) ->
-    importSupportingDecls fvs                  `thenM` \ env ->
+    rnExpr expr2                               `thenM` \ (exp3, fvs) ->
 
-    setGblEnv env (tcMonoExpr exp3 res_ty)
+    tcMonoExpr exp3 res_ty
 
 
 tcTopSpliceExpr :: RenamedHsExpr -> TcType -> TcM TypecheckedHsExpr
@@ -201,7 +198,7 @@ tcTopSpliceExpr expr meta_ty
     setStage topSpliceStage $
 
        -- Typecheck the expression
-    getLIE (tcMonoExpr expr meta_ty)   `thenM` \ (expr', lie) ->
+    getLIE (tcCheckRho expr meta_ty)   `thenM` \ (expr', lie) ->
 
        -- Solve the constraints
     tcSimplifyTop lie                  `thenM` \ const_binds ->
@@ -264,19 +261,10 @@ runMetaD e = runMeta e
 runMeta :: TypecheckedHsExpr   -- Of type X
        -> TcM t                -- Of type t
 runMeta expr
-  = getTopEnv          `thenM` \ top_env ->
+  = getTopEnv          `thenM` \ hsc_env ->
     getGblEnv          `thenM` \ tcg_env ->
-    getEps             `thenM` \ eps ->
-    getNameCache       `thenM` \ name_cache -> 
     getModule          `thenM` \ this_mod ->
     let
-       ghci_mode = top_mode top_env
-
-       hsc_env = HscEnv { hsc_mode = ghci_mode, hsc_HPT = top_hpt top_env,
-                          hsc_dflags = top_dflags top_env }
-
-       pcs = PCS { pcs_nc = name_cache, pcs_EPS = eps }
-
        type_env = tcg_type_env tcg_env
        rdr_env  = tcg_rdr_env tcg_env
     in
@@ -285,7 +273,7 @@ runMeta expr
        -- Running might fail if it throws an exception
     tryM (ioToTcRn (do
        hval <- HscMain.compileExpr 
-                     hsc_env pcs this_mod 
+                     hsc_env this_mod 
                      rdr_env type_env expr
         Meta.runQ (unsafeCoerce# hval)         -- Coerce it to Q t, and run it
     ))                                 `thenM` \ either_tval ->