{-# LANGUAGE Rank2Types #-}
module Plugins (
      FrontendPlugin(..), defaultFrontendPlugin, FrontendPluginAction
    , Plugin(..), CommandLineOption, LoadedPlugin(..), lpModuleName
    , defaultPlugin, keepRenamedSource, withPlugins, withPlugins_
    , PluginRecompile(..)
    , purePlugin, impurePlugin, flagRecompile
    ) where


import Control.Monad
import Data.List
import Data.Semigroup qualified
import StubTypes
import TcRnTypes qualified

plugins :: DynFlags -> [LoadedPlugin]
plugins :: DynFlags -> [LoadedPlugin]
plugins DynFlags
_ = []

type CommandLineOption = String

-- | 'Plugin' is the compiler plugin data type. Try to avoid
-- constructing one of these directly, and just modify some fields of
-- 'defaultPlugin' instead: this is to try and preserve source-code
-- compatibility when we add fields to this.
--
-- Nonetheless, this API is preliminary and highly likely to change in
-- the future.
data Plugin = Plugin {
    Plugin -> CorePlugin
installCoreToDos :: CorePlugin
    -- ^ Modify the Core pipeline that will be used for compilation.
    -- This is called as the Core pipeline is built for every module
    -- being compiled, and plugins get the opportunity to modify the
    -- pipeline in a nondeterministic order.
  , Plugin -> TcPlugin
tcPlugin :: TcPlugin
    -- ^ An optional typechecker plugin, which may modify the
    -- behaviour of the constraint solver.
  , Plugin -> [CommandLineOption] -> IO PluginRecompile
pluginRecompile :: [CommandLineOption] -> IO PluginRecompile
    -- ^ Specify how the plugin should affect recompilation.
  , Plugin
-> [CommandLineOption]
-> ModSummary
-> HsParsedModule
-> Hsc HsParsedModule
parsedResultAction :: [CommandLineOption] -> ModSummary -> HsParsedModule
                            -> Hsc HsParsedModule
    -- ^ Modify the module when it is parsed. This is called by
    -- HscMain when the parsing is successful.
  , Plugin
-> [CommandLineOption]
-> TcGblEnv
-> HsGroup GhcRn
-> TcM (TcGblEnv, HsGroup GhcRn)
renamedResultAction :: [CommandLineOption] -> TcGblEnv
                                -> HsGroup GhcRn -> TcM (TcGblEnv, HsGroup GhcRn)
    -- ^ Modify each group after it is renamed. This is called after each
    -- `HsGroup` has been renamed.
  , Plugin
-> [CommandLineOption] -> ModSummary -> TcGblEnv -> TcM TcGblEnv
typeCheckResultAction :: [CommandLineOption] -> ModSummary -> TcGblEnv
                               -> TcM TcGblEnv
    -- ^ Modify the module when it is type checked. This is called add the
    -- very end of typechecking.
  , Plugin
-> [CommandLineOption] -> LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
spliceRunAction :: [CommandLineOption] -> LHsExpr GhcTc
                         -> TcM (LHsExpr GhcTc)
    -- ^ Modify the TH splice or quasiqoute before it is run.
  , Plugin
-> forall lcl. [CommandLineOption] -> ModIface -> IfM lcl ModIface
interfaceLoadAction :: forall lcl . [CommandLineOption] -> ModIface
                                          -> IfM lcl ModIface
    -- ^ Modify an interface that have been loaded. This is called by
    -- LoadIface when an interface is successfully loaded. Not applied to
    -- the loading of the plugin interface. Tools that rely on information from
    -- modules other than the currently compiled one should implement this
    -- function.
  }

-- Note [Source plugins]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- The `Plugin` datatype have been extended by fields that allow access to the
-- different inner representations that are generated during the compilation
-- process. These fields are `parsedResultAction`, `renamedResultAction`,
-- `typeCheckResultAction`, `spliceRunAction` and `interfaceLoadAction`.
--
-- The main purpose of these plugins is to help tool developers. They allow
-- development tools to extract the information about the source code of a big
-- Haskell project during the normal build procedure. In this case the plugin
-- acts as the tools access point to the compiler that can be controlled by
-- compiler flags. This is important because the manipulation of compiler flags
-- is supported by most build environment.
--
-- For the full discussion, check the full proposal at:
-- https://ghc.haskell.org/trac/ghc/wiki/ExtendedPluginsProposal


-- | A plugin with its arguments. The result of loading the plugin.
data LoadedPlugin = LoadedPlugin {
    LoadedPlugin -> Plugin
lpPlugin    :: Plugin
    -- ^ the actual callable plugin
  , LoadedPlugin -> ModIface
lpModule    :: ModIface
    -- ^ the module containing the plugin
  , LoadedPlugin -> [CommandLineOption]
lpArguments :: [CommandLineOption]
    -- ^ command line arguments for the plugin
  }

lpModuleName :: LoadedPlugin -> ModuleName
lpModuleName :: LoadedPlugin -> ModuleName
lpModuleName = Module -> ModuleName
moduleName (Module -> ModuleName)
-> (LoadedPlugin -> Module) -> LoadedPlugin -> ModuleName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModIface -> Module
mi_module (ModIface -> Module)
-> (LoadedPlugin -> ModIface) -> LoadedPlugin -> Module
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LoadedPlugin -> ModIface
lpModule


data PluginRecompile = ForceRecompile | NoForceRecompile | MaybeRecompile Fingerprint

instance Outputable PluginRecompile where
  ppr :: PluginRecompile -> SDoc
ppr PluginRecompile
ForceRecompile      = CommandLineOption -> SDoc
text CommandLineOption
"ForceRecompile"
  ppr PluginRecompile
NoForceRecompile    = CommandLineOption -> SDoc
text CommandLineOption
"NoForceRecompile"
  ppr (MaybeRecompile Fingerprint
fp) = CommandLineOption -> SDoc
text CommandLineOption
"MaybeRecompile" SDoc -> SDoc -> SDoc
<+> Fingerprint -> SDoc
forall a. Outputable a => a -> SDoc
ppr Fingerprint
fp

instance Semigroup PluginRecompile where
  PluginRecompile
ForceRecompile <> :: PluginRecompile -> PluginRecompile -> PluginRecompile
<> PluginRecompile
_                     = PluginRecompile
ForceRecompile
  PluginRecompile
NoForceRecompile <> PluginRecompile
r                   = PluginRecompile
r
  MaybeRecompile Fingerprint
fp <> PluginRecompile
NoForceRecompile   = Fingerprint -> PluginRecompile
MaybeRecompile Fingerprint
fp
  MaybeRecompile Fingerprint
fp <> MaybeRecompile Fingerprint
fp' = Fingerprint -> PluginRecompile
MaybeRecompile ([Fingerprint] -> Fingerprint
fingerprintFingerprints [Fingerprint
fp, Fingerprint
fp'])
  MaybeRecompile Fingerprint
_fp <> PluginRecompile
ForceRecompile    = PluginRecompile
ForceRecompile

instance Monoid PluginRecompile where
  mempty :: PluginRecompile
mempty = PluginRecompile
NoForceRecompile
  mappend :: PluginRecompile -> PluginRecompile -> PluginRecompile
mappend = PluginRecompile -> PluginRecompile -> PluginRecompile
forall a. Semigroup a => a -> a -> a
(Data.Semigroup.<>)

type CorePlugin = [CommandLineOption] -> [CoreToDo] -> CoreM [CoreToDo]
type TcPlugin = [CommandLineOption] -> Maybe TcRnTypes.TcPlugin

purePlugin, impurePlugin, flagRecompile :: [CommandLineOption] -> IO PluginRecompile
purePlugin :: [CommandLineOption] -> IO PluginRecompile
purePlugin [CommandLineOption]
_args = PluginRecompile -> IO PluginRecompile
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return PluginRecompile
NoForceRecompile

impurePlugin :: [CommandLineOption] -> IO PluginRecompile
impurePlugin [CommandLineOption]
_args = PluginRecompile -> IO PluginRecompile
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return PluginRecompile
ForceRecompile

flagRecompile :: [CommandLineOption] -> IO PluginRecompile
flagRecompile =
  PluginRecompile -> IO PluginRecompile
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (PluginRecompile -> IO PluginRecompile)
-> ([CommandLineOption] -> PluginRecompile)
-> [CommandLineOption]
-> IO PluginRecompile
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fingerprint -> PluginRecompile
MaybeRecompile (Fingerprint -> PluginRecompile)
-> ([CommandLineOption] -> Fingerprint)
-> [CommandLineOption]
-> PluginRecompile
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Fingerprint] -> Fingerprint
fingerprintFingerprints ([Fingerprint] -> Fingerprint)
-> ([CommandLineOption] -> [Fingerprint])
-> [CommandLineOption]
-> Fingerprint
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CommandLineOption -> Fingerprint)
-> [CommandLineOption] -> [Fingerprint]
forall a b. (a -> b) -> [a] -> [b]
map CommandLineOption -> Fingerprint
fingerprintString ([CommandLineOption] -> [Fingerprint])
-> ([CommandLineOption] -> [CommandLineOption])
-> [CommandLineOption]
-> [Fingerprint]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CommandLineOption] -> [CommandLineOption]
forall a. Ord a => [a] -> [a]
sort

-- | Default plugin: does nothing at all! For compatibility reasons
-- you should base all your plugin definitions on this default value.
defaultPlugin :: Plugin
defaultPlugin :: Plugin
defaultPlugin = Plugin {
        installCoreToDos :: CorePlugin
installCoreToDos      = ([CoreToDo] -> CoreM [CoreToDo]) -> CorePlugin
forall a b. a -> b -> a
const [CoreToDo] -> CoreM [CoreToDo]
forall a. a -> CoreM a
forall (m :: * -> *) a. Monad m => a -> m a
return
      , tcPlugin :: TcPlugin
tcPlugin              = Maybe TcPlugin -> TcPlugin
forall a b. a -> b -> a
const Maybe TcPlugin
forall a. Maybe a
Nothing
      , pluginRecompile :: [CommandLineOption] -> IO PluginRecompile
pluginRecompile  = [CommandLineOption] -> IO PluginRecompile
impurePlugin
      , renamedResultAction :: [CommandLineOption]
-> TcGblEnv -> HsGroup GhcRn -> TcM (TcGblEnv, HsGroup GhcRn)
renamedResultAction   = \[CommandLineOption]
_ TcGblEnv
env HsGroup GhcRn
grp -> (TcGblEnv, HsGroup GhcRn) -> TcM (TcGblEnv, HsGroup GhcRn)
forall a. a -> TcM a
forall (m :: * -> *) a. Monad m => a -> m a
return (TcGblEnv
env, HsGroup GhcRn
grp)
      , parsedResultAction :: [CommandLineOption]
-> ModSummary -> HsParsedModule -> Hsc HsParsedModule
parsedResultAction    = \[CommandLineOption]
_ ModSummary
_ -> HsParsedModule -> Hsc HsParsedModule
forall a. a -> Hsc a
forall (m :: * -> *) a. Monad m => a -> m a
return
      , typeCheckResultAction :: [CommandLineOption] -> ModSummary -> TcGblEnv -> TcM TcGblEnv
typeCheckResultAction = \[CommandLineOption]
_ ModSummary
_ -> TcGblEnv -> TcM TcGblEnv
forall a. a -> TcM a
forall (m :: * -> *) a. Monad m => a -> m a
return
      , spliceRunAction :: [CommandLineOption] -> LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
spliceRunAction       = \[CommandLineOption]
_ -> LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
forall a. a -> TcM a
forall (m :: * -> *) a. Monad m => a -> m a
return
      , interfaceLoadAction :: forall lcl. [CommandLineOption] -> ModIface -> IfM lcl ModIface
interfaceLoadAction   = \[CommandLineOption]
_ -> ModIface -> IfM lcl ModIface
forall a. a -> IfM lcl a
forall (m :: * -> *) a. Monad m => a -> m a
return
    }


-- | A renamer plugin which mades the renamed source available in
-- a typechecker plugin.
keepRenamedSource :: [CommandLineOption] -> TcGblEnv
                  -> HsGroup GhcRn -> TcM (TcGblEnv, HsGroup GhcRn)
keepRenamedSource :: [CommandLineOption]
-> TcGblEnv -> HsGroup GhcRn -> TcM (TcGblEnv, HsGroup GhcRn)
keepRenamedSource [CommandLineOption]
_ TcGblEnv
gbl_env HsGroup GhcRn
group = StubM (TcGblEnv, HsGroup GhcRn) -> TcM (TcGblEnv, HsGroup GhcRn)
forall a. StubM a -> TcM a
TcM_ StubM (TcGblEnv, HsGroup GhcRn)
forall a. StubM a
StubM_
{-
  return (gbl_env { tcg_rn_decls = update (tcg_rn_decls gbl_env)
                  , tcg_rn_exports = update_exports (tcg_rn_exports gbl_env) }, group)
  where
    update_exports Nothing = Just []
    update_exports m = m

    update Nothing = Just emptyRnGroup
    update m       = m
-}

type PluginOperation m a = Plugin -> [CommandLineOption] -> a -> m a
type ConstPluginOperation m a = Plugin -> [CommandLineOption] -> a -> m ()

-- | Perform an operation by using all of the plugins in turn.
withPlugins :: Monad m => DynFlags -> PluginOperation m a -> a -> m a
withPlugins :: forall (m :: * -> *) a.
Monad m =>
DynFlags -> PluginOperation m a -> a -> m a
withPlugins DynFlags
df PluginOperation m a
transformation a
input
  = (a -> LoadedPlugin -> m a) -> a -> [LoadedPlugin] -> m a
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (\a
arg (LoadedPlugin Plugin
p ModIface
_ [CommandLineOption]
opts) -> PluginOperation m a
transformation Plugin
p [CommandLineOption]
opts a
arg)
          a
input (DynFlags -> [LoadedPlugin]
plugins DynFlags
df)

-- | Perform a constant operation by using all of the plugins in turn.
withPlugins_ :: Monad m => DynFlags -> ConstPluginOperation m a -> a -> m ()
withPlugins_ :: forall (m :: * -> *) a.
Monad m =>
DynFlags -> ConstPluginOperation m a -> a -> m ()
withPlugins_ DynFlags
df ConstPluginOperation m a
transformation a
input
  = (LoadedPlugin -> m ()) -> [LoadedPlugin] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\(LoadedPlugin Plugin
p ModIface
_ [CommandLineOption]
opts) -> ConstPluginOperation m a
transformation Plugin
p [CommandLineOption]
opts a
input)
          (DynFlags -> [LoadedPlugin]
plugins DynFlags
df)

type FrontendPluginAction = [String] -> [(String, Maybe Phase)] -> Ghc ()
data FrontendPlugin = FrontendPlugin {
      FrontendPlugin -> FrontendPluginAction
frontend :: FrontendPluginAction
    }
defaultFrontendPlugin :: FrontendPlugin
defaultFrontendPlugin :: FrontendPlugin
defaultFrontendPlugin = FrontendPlugin { frontend :: FrontendPluginAction
frontend = \[CommandLineOption]
_ [(CommandLineOption, Maybe Phase)]
_ -> () -> Ghc ()
forall a. a -> Ghc a
forall (m :: * -> *) a. Monad m => a -> m a
return () }