diff options
Diffstat (limited to 'compiler/main/Plugins.hs')
-rw-r--r-- | compiler/main/Plugins.hs | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/compiler/main/Plugins.hs b/compiler/main/Plugins.hs index 430b0790f9..de04415244 100644 --- a/compiler/main/Plugins.hs +++ b/compiler/main/Plugins.hs @@ -32,8 +32,10 @@ module Plugins ( , keepRenamedSource -- * Internal + , PluginWithArgs(..), plugins, pluginRecompile' , LoadedPlugin(..), lpModuleName - , withPlugins, withPlugins_ + , StaticPlugin(..) + , mapPlugins, withPlugins, withPlugins_ ) where import GhcPrelude @@ -120,20 +122,33 @@ data Plugin = Plugin { -- For the full discussion, check the full proposal at: -- https://ghc.haskell.org/trac/ghc/wiki/ExtendedPluginsProposal +data PluginWithArgs = PluginWithArgs + { paPlugin :: Plugin + -- ^ the actual callable plugin + , paArguments :: [CommandLineOption] + -- ^ command line arguments for the plugin + } -- | A plugin with its arguments. The result of loading the plugin. -data LoadedPlugin = LoadedPlugin { - lpPlugin :: Plugin - -- ^ the actual callable plugin +data LoadedPlugin = LoadedPlugin + { lpPlugin :: PluginWithArgs + -- ^ the actual plugin together with its commandline arguments , lpModule :: ModIface - -- ^ the module containing the plugin - , lpArguments :: [CommandLineOption] - -- ^ command line arguments for the plugin + -- ^ the module containing the plugin + } + +-- | A static plugin with its arguments. For registering compiled-in plugins +-- through the GHC API. +data StaticPlugin = StaticPlugin + { spPlugin :: PluginWithArgs + -- ^ the actual plugin together with its commandline arguments } lpModuleName :: LoadedPlugin -> ModuleName lpModuleName = moduleName . mi_module . lpModule +pluginRecompile' :: PluginWithArgs -> IO PluginRecompile +pluginRecompile' (PluginWithArgs plugin args) = pluginRecompile plugin args data PluginRecompile = ForceRecompile | NoForceRecompile | MaybeRecompile Fingerprint @@ -196,16 +211,24 @@ keepRenamedSource _ gbl_env group = type PluginOperation m a = Plugin -> [CommandLineOption] -> a -> m a type ConstPluginOperation m a = Plugin -> [CommandLineOption] -> a -> m () +plugins :: DynFlags -> [PluginWithArgs] +plugins df = + map lpPlugin (cachedPlugins df) ++ + map spPlugin (staticPlugins df) + -- | Perform an operation by using all of the plugins in turn. withPlugins :: Monad m => DynFlags -> PluginOperation m a -> a -> m a -withPlugins df transformation input - = foldM (\arg (LoadedPlugin p _ opts) -> transformation p opts arg) - input (plugins df) +withPlugins df transformation input = foldM go input (plugins df) + where + go arg (PluginWithArgs p opts) = transformation p opts arg + +mapPlugins :: DynFlags -> (Plugin -> [CommandLineOption] -> a) -> [a] +mapPlugins df f = map (\(PluginWithArgs p opts) -> f p opts) (plugins df) -- | Perform a constant operation by using all of the plugins in turn. withPlugins_ :: Monad m => DynFlags -> ConstPluginOperation m a -> a -> m () withPlugins_ df transformation input - = mapM_ (\(LoadedPlugin p _ opts) -> transformation p opts input) + = mapM_ (\(PluginWithArgs p opts) -> transformation p opts input) (plugins df) type FrontendPluginAction = [String] -> [(String, Maybe Phase)] -> Ghc () |