+-----------------------------------------------------------------------------
+-- Unloading old objects ready for a new compilation sweep.
+--
+-- The compilation manager provides us with a list of linkables that it
+-- considers "stable", i.e. won't be recompiled this time around. For
+-- each of the modules current linked in memory,
+--
+-- * if the linkable is stable (and it's the same one - the
+-- user may have recompiled the module on the side), we keep it,
+--
+-- * otherwise, we unload it.
+--
+
+unload :: GhciMode
+ -> DynFlags
+ -> [Linkable] -- stable linkables
+ -> PersistentLinkerState
+ -> IO PersistentLinkerState
+
+unload Batch dflags linkables pls = return pls
+unload Interactive dflags linkables pls
+ = do new_loaded <- filterM maybeUnload (objects_loaded pls)
+ let mods_retained = map linkableModName new_loaded
+ itbl_env' = filterNameMap mods_retained (itbl_env pls)
+ closure_env' = filterNameMap mods_retained (closure_env pls)
+
+ let verb = verbosity dflags
+ when (verb >= 3) $ do
+ hPutStrLn stderr (showSDoc
+ (text "CmLink.unload: retaining" <+> ppr mods_retained))
+
+ return pls{ objects_loaded = new_loaded,
+ itbl_env = itbl_env',
+ closure_env = closure_env' }
+ where
+ maybeUnload :: Linkable -> IO Bool
+ maybeUnload (LM time mod objs) = do
+ case findModuleLinkable_maybe linkables mod of
+ Nothing -> do unloadObjs; return False
+ Just l | linkableTime l /= time -> do unloadObjs; return False
+ | otherwise -> return True
+ where
+ unloadObjs = mapM unloadObj [ f | DotO f <- objs ]
+
+-----------------------------------------------------------------------------
+-- Linking
+