2014-07-03 05:22:43 +00:00
|
|
|
{-# LANGUAGE CPP, GeneralizedNewtypeDeriving, FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses, RankNTypes, TypeFamilies #-}
|
2014-05-08 06:26:26 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2014-07-03 05:22:43 +00:00
|
|
|
|
2014-05-03 14:08:28 +00:00
|
|
|
module Language.Haskell.GhcMod.Monad (
|
|
|
|
GhcMod
|
|
|
|
, GhcModEnv(..)
|
|
|
|
, GhcModWriter
|
|
|
|
, GhcModState(..)
|
|
|
|
, runGhcMod'
|
|
|
|
, runGhcMod
|
2014-05-10 13:10:34 +00:00
|
|
|
, withErrorHandler
|
2014-05-03 14:08:28 +00:00
|
|
|
, toGhcMod
|
2014-05-10 11:51:35 +00:00
|
|
|
, options
|
2014-05-03 14:08:28 +00:00
|
|
|
, module Control.Monad.Reader.Class
|
|
|
|
, module Control.Monad.Writer.Class
|
|
|
|
, module Control.Monad.State.Class
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Language.Haskell.GhcMod.Cradle
|
|
|
|
import Language.Haskell.GhcMod.GHCApi
|
2014-07-03 05:22:43 +00:00
|
|
|
import Language.Haskell.GhcMod.Types
|
2014-05-03 14:08:28 +00:00
|
|
|
|
2014-07-03 05:22:43 +00:00
|
|
|
import DynFlags
|
|
|
|
import Exception
|
2014-05-03 14:08:28 +00:00
|
|
|
import GHC
|
|
|
|
import GHC.Paths (libdir)
|
|
|
|
import GhcMonad
|
2014-07-03 05:19:36 +00:00
|
|
|
#if __GLASGOW_HASKELL__ <= 702
|
2014-05-08 11:11:29 +00:00
|
|
|
import HscTypes
|
|
|
|
#endif
|
2014-05-09 18:38:35 +00:00
|
|
|
|
2014-07-03 05:26:39 +00:00
|
|
|
-- MonadUtils of GHC 7.6 or earlier defines its own MonadIO.
|
|
|
|
-- RWST does not automatically become an instance of MonadIO.
|
|
|
|
-- MonadUtils of GHC 7.8 or later imports MonadIO in Monad.Control.IO.Class.
|
|
|
|
-- So, RWST automatically becomes an instance of MonadIO.
|
|
|
|
import MonadUtils
|
|
|
|
|
2014-07-03 05:19:36 +00:00
|
|
|
#if __GLASGOW_HASKELL__ < 708
|
2014-07-03 05:26:39 +00:00
|
|
|
-- To make RWST an instance of MonadIO.
|
2014-05-09 18:38:35 +00:00
|
|
|
import Control.Monad.Trans.Class (lift)
|
2014-07-03 05:22:43 +00:00
|
|
|
import Data.Monoid (Monoid)
|
2014-05-09 18:38:35 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-03 14:08:28 +00:00
|
|
|
import Control.Monad (liftM)
|
|
|
|
import Control.Monad.Base (MonadBase,liftBase)
|
|
|
|
import Control.Monad.Reader.Class
|
|
|
|
import Control.Monad.State.Class
|
2014-07-03 05:22:43 +00:00
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl(..), StM, liftBaseWith, control, liftBaseOp, liftBaseOp_)
|
|
|
|
import Control.Monad.Trans.RWS.Lazy (RWST(..),runRWST)
|
|
|
|
import Control.Monad.Writer.Class
|
2014-05-03 14:08:28 +00:00
|
|
|
|
2014-07-03 05:22:43 +00:00
|
|
|
import Data.IORef (IORef, readIORef, writeIORef, newIORef)
|
2014-05-10 13:10:34 +00:00
|
|
|
import System.Exit (exitSuccess)
|
2014-07-03 05:22:43 +00:00
|
|
|
import System.IO (hPutStr, hPrint, stderr)
|
2014-05-10 13:10:34 +00:00
|
|
|
|
2014-07-03 05:22:43 +00:00
|
|
|
----------------------------------------------------------------
|
2014-05-10 13:10:34 +00:00
|
|
|
|
2014-05-03 14:08:28 +00:00
|
|
|
data GhcModEnv = GhcModEnv {
|
|
|
|
gmGhcSession :: !(IORef HscEnv)
|
|
|
|
, gmOptions :: Options
|
|
|
|
, gmCradle :: Cradle
|
|
|
|
}
|
|
|
|
|
|
|
|
data GhcModState = GhcModState
|
|
|
|
|
|
|
|
defaultState :: GhcModState
|
|
|
|
defaultState = GhcModState
|
|
|
|
|
|
|
|
type GhcModWriter = ()
|
|
|
|
|
2014-07-03 05:22:43 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-05-03 14:08:28 +00:00
|
|
|
newtype GhcMod a = GhcMod {
|
2014-07-03 05:22:43 +00:00
|
|
|
unGhcMod :: RWST GhcModEnv GhcModWriter GhcModState IO a
|
|
|
|
} deriving (Functor
|
|
|
|
,Applicative
|
|
|
|
,Monad
|
|
|
|
,MonadIO
|
|
|
|
,MonadReader GhcModEnv
|
|
|
|
,MonadWriter GhcModWriter
|
|
|
|
,MonadState GhcModState
|
|
|
|
)
|
2014-05-03 14:08:28 +00:00
|
|
|
|
2014-05-08 06:26:26 +00:00
|
|
|
#if __GLASGOW_HASKELL__ < 708
|
|
|
|
instance (Monoid w, MonadIO m) => MonadIO (RWST r w s m) where
|
2014-05-08 07:51:15 +00:00
|
|
|
-- liftIO :: MonadIO m => IO a -> m a
|
2014-05-08 06:26:26 +00:00
|
|
|
liftIO = lift . liftIO
|
|
|
|
#endif
|
|
|
|
|
2014-07-03 05:22:43 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-05-03 14:08:28 +00:00
|
|
|
runGhcMod' :: GhcModEnv
|
|
|
|
-> GhcModState
|
|
|
|
-> GhcMod a
|
|
|
|
-> IO (a,(GhcModState, GhcModWriter))
|
|
|
|
runGhcMod' r s a = do
|
|
|
|
(a', s',w) <- runRWST (unGhcMod $ initGhcMonad (Just libdir) >> a) r s
|
|
|
|
return (a',(s',w))
|
|
|
|
|
2014-05-10 13:10:34 +00:00
|
|
|
|
2014-05-03 14:08:28 +00:00
|
|
|
runGhcMod :: Options -> GhcMod a -> IO a
|
2014-05-10 13:10:34 +00:00
|
|
|
runGhcMod opt action = do
|
2014-05-03 14:08:28 +00:00
|
|
|
session <- newIORef (error "empty session")
|
|
|
|
cradle <- findCradle
|
|
|
|
let env = GhcModEnv { gmGhcSession = session
|
|
|
|
, gmOptions = opt
|
|
|
|
, gmCradle = cradle }
|
2014-05-10 13:10:34 +00:00
|
|
|
(a,(_,_)) <- runGhcMod' env defaultState $ do
|
|
|
|
dflags <- getSessionDynFlags
|
|
|
|
defaultCleanupHandler dflags $ do
|
|
|
|
toGhcMod $ initializeFlagsWithCradle opt cradle
|
|
|
|
action
|
|
|
|
return a
|
|
|
|
|
2014-07-03 05:22:43 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-05-10 13:10:34 +00:00
|
|
|
withErrorHandler :: String -> GhcMod a -> GhcMod a
|
|
|
|
withErrorHandler label = ghandle ignore
|
|
|
|
where
|
|
|
|
ignore :: SomeException -> GhcMod a
|
|
|
|
ignore e = liftIO $ do
|
|
|
|
hPutStr stderr $ label ++ ":0:0:Error:"
|
|
|
|
hPrint stderr e
|
|
|
|
exitSuccess
|
2014-05-03 14:08:28 +00:00
|
|
|
|
2014-06-27 17:31:34 +00:00
|
|
|
-- | This is only a transitional mechanism don't use it for new code.
|
2014-05-03 14:08:28 +00:00
|
|
|
toGhcMod :: Ghc a -> GhcMod a
|
|
|
|
toGhcMod a = do
|
|
|
|
s <- gmGhcSession <$> ask
|
|
|
|
liftIO $ unGhc a $ Session s
|
|
|
|
|
2014-07-03 05:22:43 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-05-10 11:51:35 +00:00
|
|
|
options :: GhcMod Options
|
|
|
|
options = gmOptions <$> ask
|
|
|
|
|
2014-05-03 14:08:28 +00:00
|
|
|
instance MonadBase IO GhcMod where
|
|
|
|
liftBase = GhcMod . liftBase
|
|
|
|
|
|
|
|
instance MonadBaseControl IO GhcMod where
|
|
|
|
newtype StM GhcMod a = StGhcMod {
|
|
|
|
unStGhcMod :: StM (RWST GhcModEnv () GhcModState IO) a }
|
|
|
|
|
|
|
|
liftBaseWith f = GhcMod . liftBaseWith $ \runInBase ->
|
|
|
|
f $ liftM StGhcMod . runInBase . unGhcMod
|
|
|
|
|
|
|
|
restoreM = GhcMod . restoreM . unStGhcMod
|
|
|
|
{-# INLINE liftBaseWith #-}
|
|
|
|
{-# INLINE restoreM #-}
|
|
|
|
|
|
|
|
instance GhcMonad GhcMod where
|
|
|
|
getSession = liftIO . readIORef . gmGhcSession =<< ask
|
|
|
|
setSession a = liftIO . flip writeIORef a . gmGhcSession =<< ask
|
|
|
|
|
2014-05-08 08:01:01 +00:00
|
|
|
#if __GLASGOW_HASKELL__ >= 706
|
2014-05-03 14:08:28 +00:00
|
|
|
instance HasDynFlags GhcMod where
|
|
|
|
getDynFlags = getSessionDynFlags
|
2014-05-08 08:01:01 +00:00
|
|
|
#endif
|
2014-05-03 14:08:28 +00:00
|
|
|
|
|
|
|
instance ExceptionMonad GhcMod where
|
|
|
|
gcatch act handler = control $ \run ->
|
|
|
|
run act `gcatch` (run . handler)
|
|
|
|
|
|
|
|
gmask = liftBaseOp gmask . liftRestore
|
|
|
|
where liftRestore f r = f $ liftBaseOp_ r
|