... | ... | @@ -11,23 +11,118 @@ This Wiki page shall serve as a central place to collect all issues and ideas re |
|
|
# Current GHC API design
|
|
|
|
|
|
|
|
|
Most exported API functions that were previously in `IO` are now
|
|
|
in the `Ghc` monad. Those functions now no longer require a `Session`
|
|
|
parameter. To start a GHC API session you now use:
|
|
|
There were two main issues which were large cross-cutting concerns and, unfortunately, took up most of my time: (a) consistent error handling and (b) explicit passing around of a `Session`.
|
|
|
|
|
|
|
|
|
The latter issue is rather straightforward to fix by introducing a monad, however, functions expecting `IO` callbacks complicated things a little bit. Consistent error handling is trickier. I decided to use extensible exceptions which required some `#ifdef`s to properly bootstrap and some special `handle*` type functions to portably handle certain exceptions (akin to `handleGhcException`.) But first, an overview:
|
|
|
|
|
|
|
|
|
A `GhcMonad` is a class, and a default datatype which implements it is the `Ghc` monad. Most interface functions will be of type `GhcMonad m => ... -> m a`. This should make it easier to use these functions in custom monads which most non-trivial clients will likely need. Very simple clients can just reuse the `Ghc` monad. The `GhcMonad` class is defined as follows:
|
|
|
|
|
|
```wiki
|
|
|
class (MonadIO m, WarnLogMonad m, ExceptionMonad m) => GhcMonad m where
|
|
|
getSession :: m HscEnv
|
|
|
setSession :: HscEnv -> m ()
|
|
|
```
|
|
|
|
|
|
|
|
|
The three required classes ensure that: we can use `liftIO` to call `IO` actions inside the GHC monad, accumulate warnings, and handle (extensible) exceptions in that monad. The three classes are defined as follows:
|
|
|
|
|
|
```wiki
|
|
|
class Monad m => MonadIO m where -- util/MonadUtils.hs
|
|
|
liftIO :: IO a -> m a
|
|
|
|
|
|
type WarningMessages = Bag WarnMsg
|
|
|
class Monad m => WarnLogMonad m where -- main/HscTypes.lhs
|
|
|
setWarnings :: WarningMessages -> m ()
|
|
|
getWarnings :: m WarningMessages
|
|
|
-- An alternative interface with 'addWarnings' and 'clearWarnings'
|
|
|
-- instead of 'setWarnings' may be nicer if we just want to dump
|
|
|
-- warnings somewhere and not accumulate (in which case
|
|
|
-- 'getWarnings' would always return the emptyBag)
|
|
|
|
|
|
class Monad m => ExceptionMonad m where -- utils/Exception.hs
|
|
|
gcatch :: Exception e => m a -> (e -> m a) -> m a
|
|
|
gbracket :: m a -> (a -> m b) -> (a -> m c) -> m c
|
|
|
gfinally :: m a -> m b -> m a
|
|
|
-- 'gfinally' and 'gbracket' may be implemented in terms of
|
|
|
-- 'gcatch' if we add 'gblock' and 'gunblock'.
|
|
|
-- The version for GHC < 6.9 also contains 'gcatchDyn'.
|
|
|
```
|
|
|
|
|
|
|
|
|
There are two GHC-API-specific exceptions:
|
|
|
|
|
|
```wiki
|
|
|
data SourceError = SourceError ErrorMessages
|
|
|
data GhcApiError = GhcApiError SDoc
|
|
|
|
|
|
mkSrcErr :: ErrorMessages -> SourceError
|
|
|
srcErrorMessages :: SourceError -> ErrorMessages
|
|
|
mkApiErr :: SDoc -> GhcApiError
|
|
|
```
|
|
|
|
|
|
|
|
|
A source error corresponds to a problem with the compiled code and contains all accumulated error messages (but no warnings). An API error is used to signal failure of an API call and replace many 'Maybe' results. The choice wasn't always obvious. In general API errors should be seldom, but catchable, i.e., they should be rare, but not entirely unexpected. I guess, the choice which functions return which error needs some fine tuning.
|
|
|
|
|
|
|
|
|
The 'WarnLogMonad' does what it's name says. It accumulate warnings, which can be queried with 'getWarnings' and 'clearWarnings'. Deciding when to clear warnings is a bit delicate. ATM, I provide a default function that should be invoked in case of a source error (i.e., compilation failure) which prints all errors and warnings and clears the accumulated warnings.
|
|
|
|
|
|
```wiki
|
|
|
printExceptionAndWarnings :: GhcMonad m => SourceError -> m ()
|
|
|
printExceptionAndWarnings err = do
|
|
|
let errs = srcErrorMessages err
|
|
|
warns <- getWarnings
|
|
|
dflags <- getSessionDynFlags
|
|
|
liftIO $ printErrorsAndWarnings dflags (warns, errs)
|
|
|
clearWarnings
|
|
|
```
|
|
|
|
|
|
|
|
|
To start a GHC API session you now use:
|
|
|
|
|
|
```wiki
|
|
|
withGhc :: Maybe FilePath -- path to GHC library
|
|
|
-> Maybe [String] -- ^ Optional list of static flags.
|
|
|
-> Ghc a -- ^ The action(s) to perform.
|
|
|
-> IO a
|
|
|
```
|
|
|
|
|
|
|
|
|
The first parameter can be determined automatically with the ghc-path
|
|
|
package. The second is a set of "static" command line flags, for
|
|
|
example, profiling options. Having those part of the run function for
|
|
|
the monad avoids complicated usage rules (e.g. before parseStaticFlags
|
|
|
had to be called before `newSession`).
|
|
|
package.
|
|
|
|
|
|
TODO We miss a `GhcT` monad transformer and a init function for custom monads `initSession :: GhcMonad m => Maybe FilePath -> m a`.
|
|
|
|
|
|
## Callbacks
|
|
|
|
|
|
|
|
|
Most of GHC's internal IO callbacks have been changed to use the proper class, e.g., `(MonadIO m, ExceptionMonad m) => ... -> m a -> m a`. However, this cannot be done for external functions with callbacks. A particularly complicated case are the asynchronous (FFI-initiated) callbacks of the Readline package. For this case I added two functions to reflect into and reify from the `IO` monad:
|
|
|
|
|
|
```wiki
|
|
|
-- | 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 a '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
|
|
|
--
|
|
|
reflectGhc :: Ghc a -> Session -> IO a
|
|
|
reflectGhc m = unGhc m
|
|
|
|
|
|
-- > Dual to 'reflectGhc'. See its documentation.
|
|
|
reifyGhc :: (Session -> IO a) -> Ghc a
|
|
|
reifyGhc act = Ghc $ act
|
|
|
```
|
|
|
|
|
|
## Interface Changes
|
|
|
|
|
|
`load` and `setTarget` work like before. `checkModule`
|
|
|
has been split up into:
|
... | ... | |