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