Use OPTIONS rather than OPTIONS_GHC for pragmas
[ghc-hetmet.git] / compiler / typecheck / TcSplice.lhs
index 56eb637..b3da4fb 100644 (file)
@@ -6,6 +6,13 @@
 TcSplice: Template Haskell splices
 
 \begin{code}
+{-# OPTIONS -w #-}
+-- The above warning supression flag is a temporary kludge.
+-- While working on this module you are encouraged to remove it and fix
+-- any warnings in the module. See
+--     http://hackage.haskell.org/trac/ghc/wiki/CodingStyle#Warnings
+-- for details
+
 module TcSplice( tcSpliceExpr, tcSpliceDecls, tcBracket ) where
 
 #include "HsVersions.h"
@@ -91,6 +98,18 @@ tcSpliceDecls e     = pprPanic "Cant do tcSpliceDecls without GHCi" (ppr e)
 %*                                                                     *
 %************************************************************************
 
+Note [Handling brackets]
+~~~~~~~~~~~~~~~~~~~~~~~~
+Source:                f = [| Just $(g 3) |]
+  The [| |] part is a HsBracket
+
+Typechecked:   f = [| Just ${s7}(g 3) |]{s7 = g Int 3}
+  The [| |] part is a HsBracketOut, containing *renamed* (not typechecked) expression
+  The "s7" is the "splice point"; the (g Int 3) part is a typechecked expression
+
+Desugared:     f = do { s7 <- g Int 3
+                      ; return (ConE "Data.Maybe.Just" s7) }
+
 \begin{code}
 tcBracket :: HsBracket Name -> BoxyRhoType -> TcM (LHsExpr TcId)
 tcBracket brack res_ty
@@ -364,8 +383,7 @@ runMeta :: (SrcSpan -> th_syn -> Either Message hs_syn)
        -> TcM hs_syn           -- Of type t
 runMeta convert expr
   = do {       -- Desugar
-         ds_expr <- unsetOptM Opt_Debugging $ initDsTc (dsLExpr expr)
-
+         ds_expr <- initDsTc (dsLExpr expr)
        -- Compile and link it; might fail if linking fails
        ; hsc_env <- getTopEnv
        ; src_span <- getSrcSpanM
@@ -383,17 +401,17 @@ runMeta convert expr
                -- exception-cacthing thing so that if there are any lurking 
                -- exceptions in the data structure returned by hval, we'll
                -- encounter them inside the try
-         either_tval <- tryAllM $ do
-               { th_syn <- TH.runQ (unsafeCoerce# hval)
-               ; case convert (getLoc expr) th_syn of
-                   Left err     -> do { addErrTc err; return Nothing }
-                   Right hs_syn -> return (Just hs_syn) }
-
-       ; case either_tval of
-             Right (Just v) -> return v
-             Right Nothing  -> failM   -- Error already in Tc monad
-             Left exn       -> failWithTc (mk_msg "run" exn)   -- Exception
-       }}}
+          either_th_syn <- tryAllM $ tryM $ TH.runQ $ unsafeCoerce# hval
+        ; case either_th_syn of
+            Left exn             -> failWithTc (mk_msg "run" exn)
+            Right (Left exn)     -> failM  -- Error already in Tc monad
+            Right (Right th_syn) -> do
+        { either_hs_syn <- tryAllM $ return $! convert (getLoc expr) th_syn
+        ; case either_hs_syn of
+            Left exn             -> failWithTc (mk_msg "interpret result of" exn)
+            Right (Left err)     -> do { addErrTc err; failM }
+            Right (Right hs_syn) -> return hs_syn
+        }}}}
   where
     mk_msg s exn = vcat [text "Exception when trying to" <+> text s <+> text "compile-time code:",
                         nest 2 (text (Panic.showException exn)),
@@ -490,8 +508,7 @@ lookupThName th_name@(TH.Name occ flavour)
            Nothing | not (isSrcRdrName rdr_name)       -- Exact, Orig
                    -> lookupImportedName rdr_name
                    | otherwise                         -- Unqual, Qual
-                   -> do { 
-                                 mb_name <- lookupSrcOcc_maybe rdr_name
+                   -> do { mb_name <- lookupSrcOcc_maybe rdr_name
                          ; case mb_name of
                              Just name -> return name
                              Nothing   -> failWithTc (notInScope th_name) }
@@ -585,18 +602,19 @@ reifyTyCon tc
 
 reifyTyCon tc
   = do         { cxt <- reifyCxt (tyConStupidTheta tc)
-       ; cons <- mapM reifyDataCon (tyConDataCons tc)
+       ; let tvs = tyConTyVars tc
+       ; cons <- mapM (reifyDataCon (mkTyVarTys tvs)) (tyConDataCons tc)
        ; let name = reifyName tc
-             tvs  = reifyTyVars (tyConTyVars tc)
+             r_tvs  = reifyTyVars tvs
              deriv = []        -- Don't know about deriving
-             decl | isNewTyCon tc = TH.NewtypeD cxt name tvs (head cons) deriv
-                  | otherwise     = TH.DataD    cxt name tvs cons        deriv
+             decl | isNewTyCon tc = TH.NewtypeD cxt name r_tvs (head cons) deriv
+                  | otherwise     = TH.DataD    cxt name r_tvs cons      deriv
        ; return (TH.TyConI decl) }
 
-reifyDataCon :: DataCon -> TcM TH.Con
-reifyDataCon dc
+reifyDataCon :: [Type] -> DataCon -> TcM TH.Con
+reifyDataCon tys dc
   | isVanillaDataCon dc
-  = do         { arg_tys <- reifyTypes (dataConOrigArgTys dc)
+  = do         { arg_tys <- reifyTypes (dataConInstOrigArgTys dc tys)
        ; let stricts = map reifyStrict (dataConStrictMarks dc)
              fields  = dataConFieldLabels dc
              name    = reifyName dc