ghc-8.2.2: The GHC API

Safe Haskell | None
Language | Haskell2010

GhcMonad

Contents

- `Ghc` monad stuff
  - Warnings

Synopsis

- class (Functor m, MonadIO m, ExceptionMonad m, HasDynFlags m) => GhcMonad m where
- newtype Ghc a = Ghc {
  - unGhc :: Session -> IO a
  }
- newtype GhcT m a = GhcT {
  - unGhcT :: Session -> m a
  }
- liftGhcT :: m a -> GhcT m a
- reflectGhc :: Ghc a -> Session -> IO a
- reifyGhc :: (Session -> IO a) -> Ghc a
- getSessionDynFlags :: GhcMonad m => m DynFlags
- liftIO :: MonadIO m => forall a. IO a -> m a
- data Session = Session !(IORef HscEnv)
- withSession :: GhcMonad m => (HscEnv -> m a) -> m a
- modifySession :: GhcMonad m => (HscEnv -> HscEnv) -> m ()
- withTempSession :: GhcMonad m => (HscEnv -> HscEnv) -> m a -> m a
- logWarnings :: GhcMonad m => WarningMessages -> m ()
- printException :: GhcMonad m => SourceError -> m ()
- type WarnErrLogger = forall m. GhcMonad m => Maybe SourceError -> m ()
- defaultWarnErrLogger :: WarnErrLogger

# `Ghc` monad stuff

class (Functor m, MonadIO m, ExceptionMonad m, HasDynFlags m) => GhcMonad m where #

A monad that has all the features needed by GHC API calls.

In short, a GHC monad

- allows embedding of IO actions,
- can log warnings,
- allows handling of (extensible) exceptions, and
- maintains a current session.

If you do not use `Ghc` or `GhcT`, make sure to call `initGhcMonad` before any call to the GHC API functions can occur.

Minimal complete definition

getSession, setSession

Methods

getSession :: m HscEnv #

setSession :: HscEnv -> m () #

Instances

GhcMonad Ghc # |
Methods getSession :: Ghc HscEnv # setSession :: HscEnv -> Ghc () #
ExceptionMonad m => GhcMonad (GhcT m) # |
Methods getSession :: GhcT m HscEnv # setSession :: HscEnv -> GhcT m () #

newtype Ghc a #

A minimal implementation of a `GhcMonad`. If you need a custom monad, e.g., to maintain additional state consider wrapping this monad or using `GhcT`.

Constructors

Ghc |

Fields

- unGhc :: Session -> IO a

Instances

Monad Ghc # |
Methods (>>=) :: Ghc a -> (a -> Ghc b) -> Ghc b Source # (>>) :: Ghc a -> Ghc b -> Ghc b Source # return :: a -> Ghc a Source # fail :: String -> Ghc a Source #
Functor Ghc # |
Methods fmap :: (a -> b) -> Ghc a -> Ghc b Source # (<$) :: a -> Ghc b -> Ghc a Source #
MonadFix Ghc # |
Methods mfix :: (a -> Ghc a) -> Ghc a Source #
Applicative Ghc # |
Methods pure :: a -> Ghc a Source # (<*>) :: Ghc (a -> b) -> Ghc a -> Ghc b Source # liftA2 :: (a -> b -> c) -> Ghc a -> Ghc b -> Ghc c Source # (*>) :: Ghc a -> Ghc b -> Ghc b Source # (<*) :: Ghc a -> Ghc b -> Ghc a Source #
MonadIO Ghc # |
Methods liftIO :: IO a -> Ghc a Source #
ExceptionMonad Ghc # |
Methods gcatch :: Exception e => Ghc a -> (e -> Ghc a) -> Ghc a # gmask :: ((Ghc a -> Ghc a) -> Ghc b) -> Ghc b # gbracket :: Ghc a -> (a -> Ghc b) -> (a -> Ghc c) -> Ghc c # gfinally :: Ghc a -> Ghc b -> Ghc a #
HasDynFlags Ghc # |
Methods getDynFlags :: Ghc DynFlags #
GhcMonad Ghc # |
Methods getSession :: Ghc HscEnv # setSession :: HscEnv -> Ghc () #

newtype GhcT m a #

A monad transformer to add GHC specific features to another monad.

Note that the wrapped monad must support IO and handling of exceptions.

Constructors

GhcT |

Fields

- unGhcT :: Session -> m a

Instances

Monad m => Monad (GhcT m) # |
Methods (>>=) :: GhcT m a -> (a -> GhcT m b) -> GhcT m b Source # (>>) :: GhcT m a -> GhcT m b -> GhcT m b Source # return :: a -> GhcT m a Source # fail :: String -> GhcT m a Source #
Functor m => Functor (GhcT m) # |
Methods fmap :: (a -> b) -> GhcT m a -> GhcT m b Source # (<$) :: a -> GhcT m b -> GhcT m a Source #
Applicative m => Applicative (GhcT m) # |
Methods pure :: a -> GhcT m a Source # (<*>) :: GhcT m (a -> b) -> GhcT m a -> GhcT m b Source # liftA2 :: (a -> b -> c) -> GhcT m a -> GhcT m b -> GhcT m c Source # (*>) :: GhcT m a -> GhcT m b -> GhcT m b Source # (<*) :: GhcT m a -> GhcT m b -> GhcT m a Source #
MonadIO m => MonadIO (GhcT m) # |
Methods liftIO :: IO a -> GhcT m a Source #
ExceptionMonad m => ExceptionMonad (GhcT m) # |
Methods gcatch :: Exception e => GhcT m a -> (e -> GhcT m a) -> GhcT m a # gmask :: ((GhcT m a -> GhcT m a) -> GhcT m b) -> GhcT m b # gbracket :: GhcT m a -> (a -> GhcT m b) -> (a -> GhcT m c) -> GhcT m c # gfinally :: GhcT m a -> GhcT m b -> GhcT m a #
MonadIO m => HasDynFlags (GhcT m) # |
Methods getDynFlags :: GhcT m DynFlags #
ExceptionMonad m => GhcMonad (GhcT m) # |
Methods getSession :: GhcT m HscEnv # setSession :: HscEnv -> GhcT m () #

liftGhcT :: m a -> GhcT m a #

reflectGhc :: Ghc a -> Session -> IO a #

Reflect a computation in the `Ghc` monad into the `IO` monad.

You can use this to call functions returning an action in the `Ghc` monad inside an `IO` action. This is needed for some (too restrictive) callback arguments of some library functions:

    libFunc :: String -> (Int -> IO a) -> IO a
    ghcFunc :: Int -> Ghc a

    ghcFuncUsingLibFunc :: String -> Ghc a -> Ghc a
    ghcFuncUsingLibFunc str =
      reifyGhc $ \s ->
        libFunc $ \i -> do
          reflectGhc (ghcFunc i) s

reifyGhc :: (Session -> IO a) -> Ghc a #

getSessionDynFlags :: GhcMonad m => m DynFlags #

Grabs the DynFlags from the Session

liftIO :: MonadIO m => forall a. IO a -> m a Source #

Lift a computation from the `IO` monad.

data Session #

The Session is a handle to the complete state of a compilation session. A compilation session consists of a set of modules constituting the current program or library, the context for interactive evaluation, and various caches.

Constructors

Session !(IORef HscEnv) |

withSession :: GhcMonad m => (HscEnv -> m a) -> m a #

Call the argument with the current session.

modifySession :: GhcMonad m => (HscEnv -> HscEnv) -> m () #

Set the current session to the result of applying the current session to the argument.

withTempSession :: GhcMonad m => (HscEnv -> HscEnv) -> m a -> m a #

Call an action with a temporarily modified Session.

## Warnings

logWarnings :: GhcMonad m => WarningMessages -> m () #

A monad that allows logging of warnings.

printException :: GhcMonad m => SourceError -> m () #

Print the error message and all warnings. Useful inside exception handlers. Clears warnings after printing.

type WarnErrLogger = forall m. GhcMonad m => Maybe SourceError -> m () #

A function called to log warnings and errors.

defaultWarnErrLogger :: WarnErrLogger #