Cleanup loadTarget logic

This commit is contained in:
Daniel Gröber 2015-12-16 00:23:51 +01:00
parent d25908d3cb
commit 6de02ea470
5 changed files with 26 additions and 51 deletions

View File

@ -25,8 +25,8 @@ setDebugLogger put df = do
-- * Friendly to foreign export -- * Friendly to foreign export
-- * Not friendly to -XTemplateHaskell and -XPatternSynonyms -- * Not friendly to -XTemplateHaskell and -XPatternSynonyms
-- * Uses little memory -- * Uses little memory
setModeSimple :: DynFlags -> DynFlags setHscNothing :: DynFlags -> DynFlags
setModeSimple df = df { setHscNothing df = df {
ghcMode = CompManager ghcMode = CompManager
, ghcLink = NoLink , ghcLink = NoLink
, hscTarget = HscNothing , hscTarget = HscNothing
@ -37,8 +37,8 @@ setModeSimple df = df {
-- * Not friendly to foreign export -- * Not friendly to foreign export
-- * Friendly to -XTemplateHaskell and -XPatternSynonyms -- * Friendly to -XTemplateHaskell and -XPatternSynonyms
-- * Uses lots of memory -- * Uses lots of memory
setModeIntelligent :: DynFlags -> DynFlags setHscInterpreted :: DynFlags -> DynFlags
setModeIntelligent df = df { setHscInterpreted df = df {
ghcMode = CompManager ghcMode = CompManager
, ghcLink = LinkInMemory , ghcLink = LinkInMemory
, hscTarget = HscInterpreted , hscTarget = HscInterpreted

View File

@ -17,7 +17,6 @@ module Language.Haskell.GhcMod.Internal (
-- * Environment, state and logging -- * Environment, state and logging
, GhcModEnv(..) , GhcModEnv(..)
, GhcModState , GhcModState
, CompilerMode(..)
, GhcModLog , GhcModLog
, GmLog(..) , GmLog(..)
, GmLogLevel(..) , GmLogLevel(..)
@ -34,8 +33,6 @@ module Language.Haskell.GhcMod.Internal (
-- ** Accessing 'GhcModEnv' and 'GhcModState' -- ** Accessing 'GhcModEnv' and 'GhcModState'
, options , options
, cradle , cradle
, getCompilerMode
, setCompilerMode
, targetGhcOptions , targetGhcOptions
, withOptions , withOptions
-- * 'GhcModError' -- * 'GhcModError'

View File

@ -36,7 +36,6 @@ module Language.Haskell.GhcMod.Monad.Types (
, defaultGhcModState , defaultGhcModState
, GmGhcSession(..) , GmGhcSession(..)
, GmComponent(..) , GmComponent(..)
, CompilerMode(..)
-- * Accessing 'GhcModEnv', 'GhcModState' and 'GhcModLog' -- * Accessing 'GhcModEnv', 'GhcModState' and 'GhcModLog'
, GmLogLevel(..) , GmLogLevel(..)
, GhcModLog(..) , GhcModLog(..)
@ -50,8 +49,6 @@ module Language.Haskell.GhcMod.Monad.Types (
, options , options
, outputOpts , outputOpts
, withOptions , withOptions
, getCompilerMode
, setCompilerMode
, getMMappedFiles , getMMappedFiles
, setMMappedFiles , setMMappedFiles
, addMMappedFile , addMMappedFile
@ -549,12 +546,6 @@ outputOpts = gmoOptions `liftM` gmoAsk
cradle :: GmEnv m => m Cradle cradle :: GmEnv m => m Cradle
cradle = gmCradle `liftM` gmeAsk cradle = gmCradle `liftM` gmeAsk
getCompilerMode :: GmState m => m CompilerMode
getCompilerMode = gmCompilerMode `liftM` gmsGet
setCompilerMode :: GmState m => CompilerMode -> m ()
setCompilerMode mode = (\s -> gmsPut s { gmCompilerMode = mode } ) =<< gmsGet
getMMappedFiles :: GmState m => m FileMappingMap getMMappedFiles :: GmState m => m FileMappingMap
getMMappedFiles = gmMMappedFiles `liftM` gmsGet getMMappedFiles = gmMMappedFiles `liftM` gmsGet

View File

@ -143,7 +143,7 @@ runGmlTWith efnmns' mdf wrapper action = do
| otherwise = setEmptyLogger | otherwise = setEmptyLogger
initSession opts' $ initSession opts' $
setModeSimple >>> setLogger >>> mdf setHscNothing >>> setLogger >>> mdf
mappedStrs <- getMMappedFilePaths mappedStrs <- getMMappedFilePaths
let targetStrs = mappedStrs ++ map moduleNameString mns ++ cfns let targetStrs = mappedStrs ++ map moduleNameString mns ++ cfns
@ -430,20 +430,24 @@ loadTargets opts targetStrs = do
setTargets targets setTargets targets
mode <- getCompilerMode mg <- depanal [] False
if mode == Intelligent
then loadTargets' Intelligent let interp = needsHscInterpreted mg
else do target <- hscTarget <$> getSessionDynFlags
mdls <- depanal [] False when (interp && target /= HscInterpreted) $ do
let fallback = needsFallback mdls resetTargets targets
if fallback then do _ <- setSessionDynFlags . setHscInterpreted =<< getSessionDynFlags
resetTargets targets gmLog GmInfo "loadTargets" $ text "Target needs interpeter, switching to LinkInMemory/HscInterpreted. Perfectly normal if anything is using TemplateHaskell, QuasiQuotes or PatternSynonyms."
setIntelligent
gmLog GmInfo "loadTargets" $ target' <- hscTarget <$> getSessionDynFlags
text "Target needs interpeter, switching to LinkInMemory/HscInterpreted. Perfectly normal if anything is using TemplateHaskell, QuasiQuotes or PatternSynonyms."
loadTargets' Intelligent case target' of
else HscNothing -> do
loadTargets' Simple void $ load LoadAllTargets
mapM_ (parseModule >=> typecheckModule >=> desugarModule) mg
HscInterpreted -> do
void $ load LoadAllTargets
_ -> error ("loadTargets: unsupported hscTarget")
gmLog GmDebug "loadTargets" $ text "Loading done" gmLog GmDebug "loadTargets" $ text "Loading done"
@ -455,30 +459,16 @@ loadTargets opts targetStrs = do
return $ Target tid taoc src return $ Target tid taoc src
relativize tgt = return tgt relativize tgt = return tgt
loadTargets' Simple = do
void $ load LoadAllTargets
mapM_ (parseModule >=> typecheckModule >=> desugarModule) =<< getModuleGraph
loadTargets' Intelligent = do
df <- getSessionDynFlags
void $ setSessionDynFlags (setModeIntelligent df)
void $ load LoadAllTargets
resetTargets targets' = do resetTargets targets' = do
setTargets [] setTargets []
void $ load LoadAllTargets void $ load LoadAllTargets
setTargets targets' setTargets targets'
setIntelligent = do
newdf <- setModeIntelligent <$> getSessionDynFlags
void $ setSessionDynFlags newdf
setCompilerMode Intelligent
showTargetId (Target (TargetModule s) _ _) = moduleNameString s showTargetId (Target (TargetModule s) _ _) = moduleNameString s
showTargetId (Target (TargetFile s _) _ _) = s showTargetId (Target (TargetFile s _) _ _) = s
needsFallback :: ModuleGraph -> Bool needsHscInterpreted :: ModuleGraph -> Bool
needsFallback = any $ \ms -> needsHscInterpreted = any $ \ms ->
let df = ms_hspp_opts ms in let df = ms_hspp_opts ms in
Opt_TemplateHaskell `xopt` df Opt_TemplateHaskell `xopt` df
|| Opt_QuasiQuotes `xopt` df || Opt_QuasiQuotes `xopt` df

View File

@ -199,16 +199,13 @@ data GhcModCaches = GhcModCaches {
data GhcModState = GhcModState { data GhcModState = GhcModState {
gmGhcSession :: !(Maybe GmGhcSession) gmGhcSession :: !(Maybe GmGhcSession)
, gmCompilerMode :: !CompilerMode
, gmCaches :: !GhcModCaches , gmCaches :: !GhcModCaches
, gmMMappedFiles :: !FileMappingMap , gmMMappedFiles :: !FileMappingMap
} }
data CompilerMode = Simple | Intelligent deriving (Eq,Show,Read)
defaultGhcModState :: GhcModState defaultGhcModState :: GhcModState
defaultGhcModState = defaultGhcModState =
GhcModState n Simple (GhcModCaches n n n n) Map.empty GhcModState n (GhcModCaches n n n n) Map.empty
where n = Nothing where n = Nothing
---------------------------------------------------------------- ----------------------------------------------------------------