Refactor the handling of quasi-quotes
[ghc.git] / compiler / main / Hooks.hs
1 -- \section[Hooks]{Low level API hooks}
2
3 -- NB: this module is SOURCE-imported by DynFlags, and should primarily
4 -- refer to *types*, rather than *code*
5 -- If you import too muchhere , then the revolting compiler_stage2_dll0_MODULES
6 -- stuff in compiler/ghc.mk makes DynFlags link to too much stuff
7
8 module Hooks ( Hooks
9 , emptyHooks
10 , lookupHook
11 , getHooked
12 -- the hooks:
13 , dsForeignsHook
14 , tcForeignImportsHook
15 , tcForeignExportsHook
16 , hscFrontendHook
17 , hscCompileOneShotHook
18 , hscCompileCoreExprHook
19 , ghcPrimIfaceHook
20 , runPhaseHook
21 , runMetaHook
22 , linkHook
23 , runRnSpliceHook
24 , getValueSafelyHook
25 ) where
26
27 import DynFlags
28 import Name
29 import PipelineMonad
30 import HscTypes
31 import HsDecls
32 import HsBinds
33 import HsExpr
34 import OrdList
35 import Id
36 import TcRnTypes
37 import Bag
38 import RdrName
39 import CoreSyn
40 import BasicTypes
41 import Type
42 import SrcLoc
43
44 import Data.Maybe
45
46 {-
47 ************************************************************************
48 * *
49 \subsection{Hooks}
50 * *
51 ************************************************************************
52 -}
53
54 -- | Hooks can be used by GHC API clients to replace parts of
55 -- the compiler pipeline. If a hook is not installed, GHC
56 -- uses the default built-in behaviour
57
58 emptyHooks :: Hooks
59 emptyHooks = Hooks Nothing Nothing Nothing Nothing Nothing
60 Nothing Nothing Nothing Nothing Nothing Nothing
61 Nothing
62
63 data Hooks = Hooks
64 { dsForeignsHook :: Maybe ([LForeignDecl Id] -> DsM (ForeignStubs, OrdList (Id, CoreExpr)))
65 , tcForeignImportsHook :: Maybe ([LForeignDecl Name] -> TcM ([Id], [LForeignDecl Id], Bag GlobalRdrElt))
66 , tcForeignExportsHook :: Maybe ([LForeignDecl Name] -> TcM (LHsBinds TcId, [LForeignDecl TcId], Bag GlobalRdrElt))
67 , hscFrontendHook :: Maybe (ModSummary -> Hsc TcGblEnv)
68 , hscCompileOneShotHook :: Maybe (HscEnv -> ModSummary -> SourceModified -> IO HscStatus)
69 , hscCompileCoreExprHook :: Maybe (HscEnv -> SrcSpan -> CoreExpr -> IO HValue)
70 , ghcPrimIfaceHook :: Maybe ModIface
71 , runPhaseHook :: Maybe (PhasePlus -> FilePath -> DynFlags -> CompPipeline (PhasePlus, FilePath))
72 , runMetaHook :: Maybe (MetaHook TcM)
73 , linkHook :: Maybe (GhcLink -> DynFlags -> Bool -> HomePackageTable -> IO SuccessFlag)
74 , runRnSpliceHook :: Maybe (HsSplice Name -> RnM (HsSplice Name))
75 , getValueSafelyHook :: Maybe (HscEnv -> Name -> Type -> IO (Maybe HValue))
76 }
77
78 getHooked :: (Functor f, HasDynFlags f) => (Hooks -> Maybe a) -> a -> f a
79 getHooked hook def = fmap (lookupHook hook def) getDynFlags
80
81 lookupHook :: (Hooks -> Maybe a) -> a -> DynFlags -> a
82 lookupHook hook def = fromMaybe def . hook . hooks