{-# LANGUAGE CPP #-}

-- | Plugin Compat utils.
module Development.IDE.GHC.Compat.Plugins (
    Plugin(..),
    defaultPlugin,
#if __GLASGOW_HASKELL__ >= 808
    PluginWithArgs(..),
#endif
    applyPluginsParsedResultAction,
    initializePlugins,

    -- * Static plugins
#if MIN_VERSION_ghc(8,8,0)
    StaticPlugin(..),
    hsc_static_plugins,
#endif
    ) where

#if MIN_VERSION_ghc(9,0,0)
#if MIN_VERSION_ghc(9,2,0)
import qualified GHC.Driver.Env                    as Env
#endif
import           GHC.Driver.Plugins                (Plugin (..),
                                                    PluginWithArgs (..),
                                                    StaticPlugin (..),
                                                    defaultPlugin, withPlugins)
import qualified GHC.Runtime.Loader                as Loader
#elif MIN_VERSION_ghc(8,8,0)
import qualified DynamicLoading                    as Loader
import           Plugins
#else
import qualified DynamicLoading                    as Loader
import           Plugins                           (Plugin (..), defaultPlugin,
                                                    withPlugins)
#endif
import           Development.IDE.GHC.Compat.Core
import           Development.IDE.GHC.Compat.Env    (hscSetFlags, hsc_dflags)
import           Development.IDE.GHC.Compat.Parser as Parser

applyPluginsParsedResultAction :: HscEnv -> DynFlags -> ModSummary -> Parser.ApiAnns -> ParsedSource -> IO ParsedSource
applyPluginsParsedResultAction :: HscEnv
-> DynFlags
-> ModSummary
-> ApiAnns
-> ParsedSource
-> IO ParsedSource
applyPluginsParsedResultAction HscEnv
env DynFlags
dflags ModSummary
ms ApiAnns
hpm_annotations ParsedSource
parsed = do
  -- Apply parsedResultAction of plugins
  let applyPluginAction :: Plugin
-> [CommandLineOption] -> HsParsedModule -> Hsc HsParsedModule
applyPluginAction Plugin
p [CommandLineOption]
opts = Plugin
-> [CommandLineOption]
-> ModSummary
-> HsParsedModule
-> Hsc HsParsedModule
parsedResultAction Plugin
p [CommandLineOption]
opts ModSummary
ms
  (HsParsedModule -> ParsedSource)
-> IO HsParsedModule -> IO ParsedSource
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap HsParsedModule -> ParsedSource
hpm_module (IO HsParsedModule -> IO ParsedSource)
-> IO HsParsedModule -> IO ParsedSource
forall a b. (a -> b) -> a -> b
$
    HscEnv -> Hsc HsParsedModule -> IO HsParsedModule
forall a. HscEnv -> Hsc a -> IO a
runHsc HscEnv
env (Hsc HsParsedModule -> IO HsParsedModule)
-> Hsc HsParsedModule -> IO HsParsedModule
forall a b. (a -> b) -> a -> b
$ DynFlags
-> (Plugin
    -> [CommandLineOption] -> HsParsedModule -> Hsc HsParsedModule)
-> HsParsedModule
-> Hsc HsParsedModule
forall (m :: * -> *) a.
Monad m =>
DynFlags -> PluginOperation m a -> a -> m a
withPlugins
#if MIN_VERSION_ghc(9,2,0)
      env
#else
      DynFlags
dflags
#endif
      Plugin
-> [CommandLineOption] -> HsParsedModule -> Hsc HsParsedModule
applyPluginAction
      (ParsedSource -> [CommandLineOption] -> ApiAnns -> HsParsedModule
HsParsedModule ParsedSource
parsed [] ApiAnns
hpm_annotations)

initializePlugins :: HscEnv -> IO HscEnv
initializePlugins :: HscEnv -> IO HscEnv
initializePlugins HscEnv
env = do
#if MIN_VERSION_ghc(9,2,0)
    Loader.initializePlugins env
#else
    DynFlags
newDf <- HscEnv -> DynFlags -> IO DynFlags
Loader.initializePlugins HscEnv
env (HscEnv -> DynFlags
hsc_dflags HscEnv
env)
    HscEnv -> IO HscEnv
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HscEnv -> IO HscEnv) -> HscEnv -> IO HscEnv
forall a b. (a -> b) -> a -> b
$ DynFlags -> HscEnv -> HscEnv
hscSetFlags DynFlags
newDf HscEnv
env
#endif


#if MIN_VERSION_ghc(8,8,0)
hsc_static_plugins :: HscEnv -> [StaticPlugin]
#if MIN_VERSION_ghc(9,2,0)
hsc_static_plugins = Env.hsc_static_plugins
#else
hsc_static_plugins :: HscEnv -> [StaticPlugin]
hsc_static_plugins = DynFlags -> [StaticPlugin]
staticPlugins (DynFlags -> [StaticPlugin])
-> (HscEnv -> DynFlags) -> HscEnv -> [StaticPlugin]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HscEnv -> DynFlags
hsc_dflags
#endif
#endif