Add dump flag for timing output
[ghc.git] / compiler / main / GhcMonad.hs
1 {-# LANGUAGE CPP, RankNTypes #-}
2 {-# OPTIONS_GHC -funbox-strict-fields #-}
3 -- -----------------------------------------------------------------------------
4 --
5 -- (c) The University of Glasgow, 2010
6 --
7 -- The Session type and related functionality
8 --
9 -- -----------------------------------------------------------------------------
10
11 module GhcMonad (
12 -- * 'Ghc' monad stuff
13 GhcMonad(..),
14 Ghc(..),
15 GhcT(..), liftGhcT,
16 reflectGhc, reifyGhc,
17 getSessionDynFlags,
18 liftIO,
19 Session(..), withSession, modifySession, withTempSession,
20
21 -- ** Warnings
22 logWarnings, printException,
23 WarnErrLogger, defaultWarnErrLogger
24 ) where
25
26 import GhcPrelude
27
28 import MonadUtils
29 import HscTypes
30 import DynFlags
31 import Exception
32 import ErrUtils
33
34 import Control.Monad
35 import Data.IORef
36
37 -- -----------------------------------------------------------------------------
38 -- | A monad that has all the features needed by GHC API calls.
39 --
40 -- In short, a GHC monad
41 --
42 -- - allows embedding of IO actions,
43 --
44 -- - can log warnings,
45 --
46 -- - allows handling of (extensible) exceptions, and
47 --
48 -- - maintains a current session.
49 --
50 -- If you do not use 'Ghc' or 'GhcT', make sure to call 'GHC.initGhcMonad'
51 -- before any call to the GHC API functions can occur.
52 --
53 class (Functor m, MonadIO m, ExceptionMonad m, HasDynFlags m) => GhcMonad m where
54 getSession :: m HscEnv
55 setSession :: HscEnv -> m ()
56
57 -- | Call the argument with the current session.
58 withSession :: GhcMonad m => (HscEnv -> m a) -> m a
59 withSession f = getSession >>= f
60
61 -- | Grabs the DynFlags from the Session
62 getSessionDynFlags :: GhcMonad m => m DynFlags
63 getSessionDynFlags = withSession (return . hsc_dflags)
64
65 -- | Set the current session to the result of applying the current session to
66 -- the argument.
67 modifySession :: GhcMonad m => (HscEnv -> HscEnv) -> m ()
68 modifySession f = do h <- getSession
69 setSession $! f h
70
71 withSavedSession :: GhcMonad m => m a -> m a
72 withSavedSession m = do
73 saved_session <- getSession
74 m `gfinally` setSession saved_session
75
76 -- | Call an action with a temporarily modified Session.
77 withTempSession :: GhcMonad m => (HscEnv -> HscEnv) -> m a -> m a
78 withTempSession f m =
79 withSavedSession $ modifySession f >> m
80
81 -- -----------------------------------------------------------------------------
82 -- | A monad that allows logging of warnings.
83
84 logWarnings :: GhcMonad m => WarningMessages -> m ()
85 logWarnings warns = do
86 dflags <- getSessionDynFlags
87 liftIO $ printOrThrowWarnings dflags warns
88
89 -- -----------------------------------------------------------------------------
90 -- | A minimal implementation of a 'GhcMonad'. If you need a custom monad,
91 -- e.g., to maintain additional state consider wrapping this monad or using
92 -- 'GhcT'.
93 newtype Ghc a = Ghc { unGhc :: Session -> IO a }
94
95 -- | The Session is a handle to the complete state of a compilation
96 -- session. A compilation session consists of a set of modules
97 -- constituting the current program or library, the context for
98 -- interactive evaluation, and various caches.
99 data Session = Session !(IORef HscEnv)
100
101 instance Functor Ghc where
102 fmap f m = Ghc $ \s -> f `fmap` unGhc m s
103
104 instance Applicative Ghc where
105 pure a = Ghc $ \_ -> return a
106 g <*> m = do f <- g; a <- m; return (f a)
107
108 instance Monad Ghc where
109 m >>= g = Ghc $ \s -> do a <- unGhc m s; unGhc (g a) s
110
111 instance MonadIO Ghc where
112 liftIO ioA = Ghc $ \_ -> ioA
113
114 instance MonadFix Ghc where
115 mfix f = Ghc $ \s -> mfix (\x -> unGhc (f x) s)
116
117 instance ExceptionMonad Ghc where
118 gcatch act handle =
119 Ghc $ \s -> unGhc act s `gcatch` \e -> unGhc (handle e) s
120 gmask f =
121 Ghc $ \s -> gmask $ \io_restore ->
122 let
123 g_restore (Ghc m) = Ghc $ \s -> io_restore (m s)
124 in
125 unGhc (f g_restore) s
126
127 instance HasDynFlags Ghc where
128 getDynFlags = getSessionDynFlags
129
130 instance GhcMonad Ghc where
131 getSession = Ghc $ \(Session r) -> readIORef r
132 setSession s' = Ghc $ \(Session r) -> writeIORef r s'
133
134 -- | Reflect a computation in the 'Ghc' monad into the 'IO' monad.
135 --
136 -- You can use this to call functions returning an action in the 'Ghc' monad
137 -- inside an 'IO' action. This is needed for some (too restrictive) callback
138 -- arguments of some library functions:
139 --
140 -- > libFunc :: String -> (Int -> IO a) -> IO a
141 -- > ghcFunc :: Int -> Ghc a
142 -- >
143 -- > ghcFuncUsingLibFunc :: String -> Ghc a -> Ghc a
144 -- > ghcFuncUsingLibFunc str =
145 -- > reifyGhc $ \s ->
146 -- > libFunc $ \i -> do
147 -- > reflectGhc (ghcFunc i) s
148 --
149 reflectGhc :: Ghc a -> Session -> IO a
150 reflectGhc m = unGhc m
151
152 -- > Dual to 'reflectGhc'. See its documentation.
153 reifyGhc :: (Session -> IO a) -> Ghc a
154 reifyGhc act = Ghc $ act
155
156 -- -----------------------------------------------------------------------------
157 -- | A monad transformer to add GHC specific features to another monad.
158 --
159 -- Note that the wrapped monad must support IO and handling of exceptions.
160 newtype GhcT m a = GhcT { unGhcT :: Session -> m a }
161
162 liftGhcT :: m a -> GhcT m a
163 liftGhcT m = GhcT $ \_ -> m
164
165 instance Functor m => Functor (GhcT m) where
166 fmap f m = GhcT $ \s -> f `fmap` unGhcT m s
167
168 instance Applicative m => Applicative (GhcT m) where
169 pure x = GhcT $ \_ -> pure x
170 g <*> m = GhcT $ \s -> unGhcT g s <*> unGhcT m s
171
172 instance Monad m => Monad (GhcT m) where
173 m >>= k = GhcT $ \s -> do a <- unGhcT m s; unGhcT (k a) s
174
175 instance MonadIO m => MonadIO (GhcT m) where
176 liftIO ioA = GhcT $ \_ -> liftIO ioA
177
178 instance ExceptionMonad m => ExceptionMonad (GhcT m) where
179 gcatch act handle =
180 GhcT $ \s -> unGhcT act s `gcatch` \e -> unGhcT (handle e) s
181 gmask f =
182 GhcT $ \s -> gmask $ \io_restore ->
183 let
184 g_restore (GhcT m) = GhcT $ \s -> io_restore (m s)
185 in
186 unGhcT (f g_restore) s
187
188 instance MonadIO m => HasDynFlags (GhcT m) where
189 getDynFlags = GhcT $ \(Session r) -> liftM hsc_dflags (liftIO $ readIORef r)
190
191 instance ExceptionMonad m => GhcMonad (GhcT m) where
192 getSession = GhcT $ \(Session r) -> liftIO $ readIORef r
193 setSession s' = GhcT $ \(Session r) -> liftIO $ writeIORef r s'
194
195
196 -- | Print the error message and all warnings. Useful inside exception
197 -- handlers. Clears warnings after printing.
198 printException :: GhcMonad m => SourceError -> m ()
199 printException err = do
200 dflags <- getSessionDynFlags
201 liftIO $ printBagOfErrors dflags (srcErrorMessages err)
202
203 -- | A function called to log warnings and errors.
204 type WarnErrLogger = forall m. GhcMonad m => Maybe SourceError -> m ()
205
206 defaultWarnErrLogger :: WarnErrLogger
207 defaultWarnErrLogger Nothing = return ()
208 defaultWarnErrLogger (Just e) = printException e
209