+cmGetModInfo cmstate = (cm_mg cmstate, hsc_HPT (cm_hsc cmstate))
+cmGetBindings cmstate = nameEnvElts (ic_type_env (cm_ic cmstate))
+cmGetPrintUnqual cmstate = icPrintUnqual (cm_ic cmstate)
+cmHPT cmstate = hsc_HPT (cm_hsc cmstate)
+#endif
+
+cmInit :: GhciMode -> DynFlags -> IO CmState
+cmInit ghci_mode dflags
+ = do { hsc_env <- newHscEnv ghci_mode dflags
+ ; return (CmState { cm_hsc = hsc_env,
+ cm_mg = emptyMG,
+ cm_ic = emptyInteractiveContext })}
+
+discardCMInfo :: CmState -> CmState
+-- Forget the compilation manager's state, including the home package table
+-- but retain the persistent info in HscEnv
+discardCMInfo cm_state
+ = cm_state { cm_mg = emptyMG, cm_ic = emptyInteractiveContext,
+ cm_hsc = (cm_hsc cm_state) { hsc_HPT = emptyHomePackageTable } }
+
+-------------------------------------------------------------------
+-- The unlinked image
+--
+-- The compilation manager keeps a list of compiled, but as-yet unlinked
+-- binaries (byte code or object code). Even when it links bytecode
+-- it keeps the unlinked version so it can re-link it later without
+-- recompiling.
+
+type UnlinkedImage = [Linkable] -- the unlinked images (should be a set, really)
+
+findModuleLinkable_maybe :: [Linkable] -> ModuleName -> Maybe Linkable
+findModuleLinkable_maybe lis mod
+ = case [LM time nm us | LM time nm us <- lis, nm == mod] of
+ [] -> Nothing
+ [li] -> Just li
+ many -> pprPanic "findModuleLinkable" (ppr mod)
+\end{code}
+
+
+%************************************************************************
+%* *
+ GHCI stuff
+%* *
+%************************************************************************
+
+\begin{code}
+#ifdef GHCI
+-----------------------------------------------------------------------------
+-- Setting the context doesn't throw away any bindings; the bindings
+-- we've built up in the InteractiveContext simply move to the new
+-- module. They always shadow anything in scope in the current context.
+
+cmSetContext
+ :: CmState
+ -> [String] -- take the top-level scopes of these modules
+ -> [String] -- and the just the exports from these
+ -> IO CmState
+cmSetContext cmstate toplevs exports = do
+ let old_ic = cm_ic cmstate
+ hsc_env = cm_hsc cmstate
+ hpt = hsc_HPT hsc_env
+
+ export_env <- mkExportEnv hsc_env (map mkModuleName exports)
+ toplev_envs <- mapM (mkTopLevEnv hpt) toplevs
+
+ let all_env = foldr plusGlobalRdrEnv export_env toplev_envs
+ return cmstate{ cm_ic = old_ic { ic_toplev_scope = toplevs,
+ ic_exports = exports,
+ ic_rn_gbl_env = all_env } }
+
+mkTopLevEnv :: HomePackageTable -> String -> IO GlobalRdrEnv
+mkTopLevEnv hpt mod
+ = case lookupModuleEnvByName hpt (mkModuleName mod) of
+ Nothing -> throwDyn (ProgramError ("mkTopLevEnv: not a home module " ++ mod))
+ Just details -> case hm_globals details of
+ Nothing -> throwDyn (ProgramError ("mkTopLevEnv: not interpreted " ++ mod))
+ Just env -> return env
+
+cmGetContext :: CmState -> IO ([String],[String])
+cmGetContext CmState{cm_ic=ic} =
+ return (ic_toplev_scope ic, ic_exports ic)
+
+cmModuleIsInterpreted :: CmState -> String -> IO Bool
+cmModuleIsInterpreted cmstate str
+ = case lookupModuleEnvByName (cmHPT cmstate) (mkModuleName str) of
+ Just details -> return (isJust (hm_globals details))
+ _not_a_home_module -> return False
+
+-----------------------------------------------------------------------------
+cmSetDFlags :: CmState -> DynFlags -> CmState
+cmSetDFlags cm_state dflags
+ = cm_state { cm_hsc = (cm_hsc cm_state) { hsc_dflags = dflags } }
+
+-----------------------------------------------------------------------------
+-- cmInfoThing: convert a String to a TyThing
+
+-- A string may refer to more than one TyThing (eg. a constructor,
+-- and type constructor), so we return a list of all the possible TyThings.
+
+cmInfoThing :: CmState -> String -> IO [(IfaceDecl,Fixity,SrcLoc)]
+cmInfoThing cmstate id
+ = hscThing (cm_hsc cmstate) (cm_ic cmstate) id
+
+-- ---------------------------------------------------------------------------
+-- cmBrowseModule: get all the TyThings defined in a module
+
+cmBrowseModule :: CmState -> String -> Bool -> IO [IfaceDecl]
+cmBrowseModule cmstate str exports_only
+ = do { mb_decls <- getModuleContents (cm_hsc cmstate) (cm_ic cmstate)
+ (mkModuleName str) exports_only
+ ; case mb_decls of
+ Nothing -> return [] -- An error of some kind
+ Just ds -> return ds
+ }
+
+
+-----------------------------------------------------------------------------
+-- cmRunStmt: Run a statement/expr.
+
+data CmRunResult
+ = CmRunOk [Name] -- names bound by this evaluation
+ | CmRunFailed
+ | CmRunException Exception -- statement raised an exception
+
+cmRunStmt :: CmState -> String -> IO (CmState, CmRunResult)
+cmRunStmt cmstate@CmState{ cm_hsc=hsc_env, cm_ic=icontext } expr
+ = do
+ maybe_stuff <- hscStmt hsc_env icontext expr
+