From d3b6fd0ae1b3e9ed30df97a7e03e389bd3f8d26b Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Mon, 6 Apr 2009 13:48:31 +0200 Subject: [PATCH] Add the new GhcTools module. This module provides a number of functions to work with GHC and the Ghc monad. --- GhcTools.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 GhcTools.hs diff --git a/GhcTools.hs b/GhcTools.hs new file mode 100644 index 0000000..dcfab0f --- /dev/null +++ b/GhcTools.hs @@ -0,0 +1,31 @@ +module GhcTools where +-- Standard modules +import qualified System.IO.Unsafe + +-- GHC API +import qualified GHC +import qualified GHC.Paths +import qualified DynFlags + +-- Change a DynFlag from within the Ghc monad. Strangely enough there seems to +-- be no standard function to do exactly this. +setDynFlag :: DynFlags.DynFlag -> GHC.Ghc () +setDynFlag dflag = do + dflags <- GHC.getSessionDynFlags + let dflags' = DynFlags.dopt_set dflags dflag + GHC.setSessionDynFlags dflags' + return () + +-- We don't want the IO monad sprinkled around everywhere, so we hide it. +-- This should be safe as long as we only do simple things in the GhcMonad +-- such as interface lookups and evaluating simple expressions that +-- don't have side effects themselves (Or rather, that don't use +-- unsafePerformIO themselves, since normal side effectful function would +-- just return an IO monad when they are evaluated). +unsafeRunGhc :: GHC.Ghc a -> a +unsafeRunGhc m = + System.IO.Unsafe.unsafePerformIO $ + GHC.runGhc (Just GHC.Paths.libdir) $ do + dflags <- GHC.getSessionDynFlags + GHC.setSessionDynFlags dflags + m -- 2.30.2