Hooks.hs 2.98 KB
Newer Older
Austin Seipp's avatar
Austin Seipp committed
1
-- \section[Hooks]{Low level API hooks}
2

3 4 5 6 7
-- NB: this module is SOURCE-imported by DynFlags, and should primarily
--     refer to *types*, rather than *code*
-- If you import too muchhere , then the revolting compiler_stage2_dll0_MODULES
-- stuff in compiler/ghc.mk makes DynFlags link to too much stuff

8 9 10 11 12 13 14 15 16 17 18 19 20
module Hooks ( Hooks
             , emptyHooks
             , lookupHook
             , getHooked
               -- the hooks:
             , dsForeignsHook
             , tcForeignImportsHook
             , tcForeignExportsHook
             , hscFrontendHook
             , hscCompileOneShotHook
             , hscCompileCoreExprHook
             , ghcPrimIfaceHook
             , runPhaseHook
Luite Stegeman's avatar
Luite Stegeman committed
21
             , runMetaHook
22
             , linkHook
23
             , runRnSpliceHook
24 25 26 27 28 29 30 31 32
             , getValueSafelyHook
             ) where

import DynFlags
import Name
import PipelineMonad
import HscTypes
import HsDecls
import HsBinds
33
import HsExpr
34 35 36 37 38 39 40 41 42 43 44 45
import OrdList
import Id
import TcRnTypes
import Bag
import RdrName
import CoreSyn
import BasicTypes
import Type
import SrcLoc

import Data.Maybe

Austin Seipp's avatar
Austin Seipp committed
46 47 48
{-
************************************************************************
*                                                                      *
49
\subsection{Hooks}
Austin Seipp's avatar
Austin Seipp committed
50 51 52
*                                                                      *
************************************************************************
-}
53 54 55 56 57 58

-- | Hooks can be used by GHC API clients to replace parts of
--   the compiler pipeline. If a hook is not installed, GHC
--   uses the default built-in behaviour

emptyHooks :: Hooks
59
emptyHooks = Hooks Nothing Nothing Nothing Nothing Nothing
60
                   Nothing Nothing Nothing Nothing Nothing Nothing
Luite Stegeman's avatar
Luite Stegeman committed
61
                   Nothing
62 63 64 65 66 67

data Hooks = Hooks
  { dsForeignsHook         :: Maybe ([LForeignDecl Id] -> DsM (ForeignStubs, OrdList (Id, CoreExpr)))
  , tcForeignImportsHook   :: Maybe ([LForeignDecl Name] -> TcM ([Id], [LForeignDecl Id], Bag GlobalRdrElt))
  , tcForeignExportsHook   :: Maybe ([LForeignDecl Name] -> TcM (LHsBinds TcId, [LForeignDecl TcId], Bag GlobalRdrElt))
  , hscFrontendHook        :: Maybe (ModSummary -> Hsc TcGblEnv)
Austin Seipp's avatar
Austin Seipp committed
68
  , hscCompileOneShotHook  :: Maybe (HscEnv -> ModSummary -> SourceModified -> IO HscStatus)
69 70 71
  , hscCompileCoreExprHook :: Maybe (HscEnv -> SrcSpan -> CoreExpr -> IO HValue)
  , ghcPrimIfaceHook       :: Maybe ModIface
  , runPhaseHook           :: Maybe (PhasePlus -> FilePath -> DynFlags -> CompPipeline (PhasePlus, FilePath))
Luite Stegeman's avatar
Luite Stegeman committed
72
  , runMetaHook            :: Maybe (MetaHook TcM)
73
  , linkHook               :: Maybe (GhcLink -> DynFlags -> Bool -> HomePackageTable -> IO SuccessFlag)
74
  , runRnSpliceHook        :: Maybe (HsSplice Name -> RnM (HsSplice Name))
75 76 77 78 79 80 81 82
  , getValueSafelyHook     :: Maybe (HscEnv -> Name -> Type -> IO (Maybe HValue))
  }

getHooked :: (Functor f, HasDynFlags f) => (Hooks -> Maybe a) -> a -> f a
getHooked hook def = fmap (lookupHook hook def) getDynFlags

lookupHook :: (Hooks -> Maybe a) -> a -> DynFlags -> a
lookupHook hook def = fromMaybe def . hook . hooks