Refactor TcRnDriver, and check exports on hi-boot files
[ghc-hetmet.git] / compiler / deSugar / Coverage.lhs
index 9a53b2b..e1373d0 100644 (file)
@@ -9,36 +9,17 @@ module Coverage (addCoverageTicksToBinds) where
 #include "HsVersions.h"
 
 import HsSyn
-import Id              ( Id )
-import DynFlags                ( DynFlags, mainModIs, mainFunIs )
 import Module
-import HscTypes                ( HpcInfo, noHpcInfo )
-
-import IdInfo
 import Outputable
-import DynFlags                ( DynFlag(Opt_D_dump_hpc), hpcDir )
+import DynFlags
 import Monad           
-
 import SrcLoc
-import ErrUtils        (doIfSet_dyn)
-import HsUtils         ( mkHsApp )
-import Unique
-import UniqSupply
-import Id
+import ErrUtils
 import Name
-import TcType          
-import TysPrim         
-import CoreUtils
-import TyCon
-import Type
-import TysWiredIn      ( intTy , stringTy, unitTy, intDataCon, falseDataConId, mkListTy, pairTyCon, tupleCon, mkTupleTy, unboxedSingletonDataCon )
 import Bag
-import Var             ( TyVar, mkTyVar )
-import DataCon         ( dataConWrapId )
-import MkId
-import PrimOp
-import BasicTypes      ( RecFlag(..), Activation(NeverActive), Boxity(..) )
-import Data.List        ( isSuffixOf )
+import Var
+import Data.List
+import FastString
 
 import System.Time (ClockTime(..))
 import System.Directory (getModificationTime)
@@ -58,10 +39,14 @@ import System.Directory ( createDirectoryIfMissing )
 
 \begin{code}
 addCoverageTicksToBinds dflags mod mod_loc binds = do 
-  let main_mod = mainModIs dflags
-      main_is  = case mainFunIs dflags of
-                 Nothing -> "main"
-                 Just main -> main 
+  let orig_file = 
+             case ml_hs_file mod_loc of
+                   Just file -> file
+                   Nothing -> panic "can not find the original file during hpc trans"
+
+  if "boot" `isSuffixOf` orig_file then return (binds, 0) else do
+
+  modTime <- getModificationTime' orig_file
 
   let mod_name = moduleNameString (moduleName mod)
 
@@ -78,12 +63,6 @@ addCoverageTicksToBinds dflags mod mod_loc binds = do
   -- write the mix entries for this module
   let tabStop = 1 -- <tab> counts as a normal char in GHC's location ranges.
 
-  let orig_file = case ml_hs_file mod_loc of
-                   Just file -> file
-                   Nothing -> error "can not find the original file during hpc trans"
-
-  modTime <- getModificationTime' orig_file
-
   createDirectoryIfMissing True hpc_dir
 
   mixCreate hpc_dir mod_name (Mix orig_file modTime tabStop $ reverse $ mixEntries st)
@@ -119,10 +98,7 @@ addTickLHsBind (L pos (funBind@(FunBind { fun_id = (L _ id)  })))  = do
 
   mg@(MatchGroup matches' ty) <- addPathEntry (getOccString id)  
                                 $ addTickMatchGroup (fun_matches funBind)
-  let arg_count = matchGroupArity mg
-  let (tys,res_ty) = splitFunTysN arg_count ty
-
-  return $ L pos $ funBind { fun_matches = MatchGroup ({-L pos fn_entry:-}matches') ty 
+  return $ L pos $ funBind { fun_matches = MatchGroup matches' ty 
                           , fun_tick = tick_no
                           }
 
@@ -252,6 +228,11 @@ addTickHsExpr (ArithSeq     ty arith_seq) =
        liftM2 ArithSeq 
                (return ty)
                (addTickArithSeqInfo arith_seq)
+addTickHsExpr (HsTickPragma (file,(l1,c1),(l2,c2)) (L pos e0)) = do
+    e1 <- addTickHsExpr e0
+    fn <- allocTickBox (ExternalBox (unpackFS file) (P l1 c1 l2 c2)) pos
+    let (L _ e2) = fn $ L pos e1
+    return $ e2
 addTickHsExpr (PArrSeq  {}) = error "addTickHsExpr: PArrSeq    "
 addTickHsExpr (HsSCC    {}) = error "addTickHsExpr: HsSCC      "
 addTickHsExpr (HsCoreAnn   {}) = error "addTickHsExpr: HsCoreAnn  "
@@ -266,22 +247,30 @@ addTickHsExpr (HsWrap w e) =
        liftM2 HsWrap
                (return w)
                (addTickHsExpr e)       -- explicitly no tick on inside
-addTickHsExpr (HsArrApp         {}) = error "addTickHsExpr:  HsArrApp  "
-addTickHsExpr (HsArrForm {}) = error "addTickHsExpr:  HsArrForm"
+addTickHsExpr (HsArrApp         e1 e2 ty1 arr_ty lr) = 
+        liftM5 HsArrApp
+              (addTickLHsExpr e1)
+              (addTickLHsExpr e2)
+              (return ty1)
+              (return arr_ty)
+              (return lr)
+addTickHsExpr (HsArrForm e fix cmdtop) = 
+        liftM3 HsArrForm
+              (addTickLHsExpr e)
+              (return fix)
+              (mapM (liftL addTickHsCmdTop) cmdtop)
+
+addTickHsExpr e@(HsType ty) = return e
+
+-- Should never happen in expression content.
 addTickHsExpr (EAsPat _ _) = error "addTickHsExpr: EAsPat _ _"
 addTickHsExpr (ELazyPat _) = error "addTickHsExpr: ELazyPat _"
 addTickHsExpr (EWildPat) = error "addTickHsExpr: EWildPat"
 addTickHsExpr (HsBinTick _ _ _) = error "addTickhsExpr: HsBinTick _ _ _"
 addTickHsExpr (HsTick _ _) = error "addTickhsExpr: HsTick _ _"
 
-addTickHsExpr e@(HsType ty) = return e
-
--- catch all, and give an error message.
---addTickHsExpr e = error ("addTickLhsExpr: " ++ showSDoc (ppr e))
-
-
 addTickMatchGroup (MatchGroup matches ty) = do
-  let isOneOfMany = True -- AJG: for now
+  let isOneOfMany = matchesOneOfMany matches
   matches' <- mapM (liftL (addTickMatch isOneOfMany)) matches
   return $ MatchGroup matches' ty
 
@@ -390,7 +379,7 @@ addTickDictBinds :: DictBinds Id -> TM (DictBinds Id)
 addTickDictBinds x = addTickLHsBinds x
 
 addTickHsRecordBinds :: HsRecordBinds Id -> TM (HsRecordBinds Id)
-addTickHsRecordBinds pairs = mapM process pairs
+addTickHsRecordBinds (HsRecordBinds pairs) = liftM HsRecordBinds (mapM process pairs)
     where
        process (ids,expr) = 
                liftM2 (,) 
@@ -417,8 +406,6 @@ addTickArithSeqInfo (FromThenTo e1 e2 e3) =
 \end{code}
 
 \begin{code}
-data TixFlags = TixFlags
-
 data TickTransState = TT { modName     :: String
                          , declPath    :: [String]
                          , tickBoxCount:: Int
@@ -498,10 +485,14 @@ mkHpcPos pos
                     )
 
 hpcSrcSpan = mkGeneralSrcSpan (FSLIT("Haskell Program Coverage internals"))
+\end{code}
 
--- all newly allocated locations have an HPC tag on them, to help debuging
-hpcLoc :: e -> Located e
-hpcLoc = L hpcSrcSpan
+
+\begin{code}
+matchesOneOfMany :: [LMatch Id] -> Bool
+matchesOneOfMany lmatches = sum (map matchCount lmatches) > 1
+  where
+       matchCount (L _ (Match _pats _ty (GRHSs grhss _binds))) = length grhss
 \end{code}
 
 
@@ -533,23 +524,19 @@ data BoxLabel = ExpBox
               | AltBox
               | TopLevelBox [String]
               | LocalBox [String]
-           -- | UserBox (Maybe String)
               | GuardBinBox Bool
               | CondBinBox Bool
               | QualBinBox Bool
-           -- | PreludeBinBox String Bool
-           -- | UserBinBox (Maybe String) Bool
+             | ExternalBox String HpcPos
+                  -- ^The position was generated from the named file/module,
+                  -- with the stated position (inside the named file/module).
+                  -- The HpcPos inside this MixEntry refers to the generated Haskell location.
               deriving (Read, Show)
                          
 mixCreate :: String -> String -> Mix -> IO ()
 mixCreate dirName modName mix =
    writeFile (mixName dirName modName) (show mix)
 
-readMix :: FilePath -> String -> IO Mix
-readMix dirName modName = do
-   contents <- readFile (mixName dirName modName)
-   return (read contents)
-
 mixName :: FilePath -> String -> String
 mixName dirName name = dirName ++ "/" ++ name ++ ".mix"
 
@@ -558,75 +545,11 @@ getModificationTime' file = do
   (TOD sec _) <- System.Directory.getModificationTime file
   return $ sec
 
-data Tix = Tix [PixEntry]      -- The number of tickboxes in each module
-              [TixEntry]       -- The tick boxes
-       deriving (Read, Show,Eq)
-
-type TixEntry = Integer
-
--- always read and write Tix from the current working directory.
-
-readTix :: String -> IO (Maybe Tix)
-readTix pname = 
-  catch (do contents <- readFile $ tixName pname 
-           return $ Just $ read contents)
-       (\ _ -> return $ Nothing)
-
-writeTix :: String -> Tix -> IO ()
-writeTix pname tix = 
-  writeFile (tixName pname) (show tix)
-
-tixName :: String -> String
-tixName name = name ++ ".tix"
-
 -- a program index records module names and numbers of tick-boxes
 -- introduced in each module that has been transformed for coverage 
 
-data Pix = Pix [PixEntry] deriving (Read, Show)
-
-type PixEntry = ( String       -- module name
-               , Int           -- number of boxes
-               )
-
-pixUpdate :: FilePath -> String -> String -> Int -> IO ()
-pixUpdate dirName progName modName boxCount = do
-   fileUpdate (pixName dirName progName) pixAssign (Pix [])
-   where
-   pixAssign :: Pix -> Pix
-   pixAssign (Pix pes) =
-     Pix ((modName,boxCount) : filter ((/=) modName . fst) pes)
-
-readPix :: FilePath -> String -> IO Pix
-readPix dirName pname = do
-  contents <- readFile (pixName dirName pname)
-  return (read contents)
-
-tickCount :: Pix -> Int
-tickCount (Pix mp) = sum $ map snd mp
-
-pixName :: FilePath -> String -> String
-pixName dirName name = dirName ++ "/" ++ name ++ ".pix"
-
--- updating a value stored in a file via read and show
-fileUpdate :: (Read a, Show a) => String -> (a->a) -> a -> IO()
-fileUpdate fname update init =
-   catch
-     (do
-        valueText <- readFile fname
-        ( case finite valueText of
-          True ->
-            writeFile fname (show (update (read valueText))) ))
-     (const (writeFile fname (show (update init))))
-
-finite :: [a] -> Bool
-finite []     = True
-finite (x:xs) = finite xs
-
 data HpcPos = P !Int !Int !Int !Int deriving (Eq)
 
-fromHpcPos :: HpcPos -> (Int,Int,Int,Int)
-fromHpcPos (P l1 c1 l2 c2) = (l1,c1,l2,c2)
-
 toHpcPos :: (Int,Int,Int,Int) -> HpcPos
 toHpcPos (l1,c1,l2,c2) = P l1 c1 l2 c2